Commit | Line | Data |
---|---|---|
7f918cf1 CE |
1 | (* Copyright (C) 2011,2015 Matthew Fluet. |
2 | * Copyright (C) 1999-2006, 2008 Henry Cejtin, Matthew Fluet, Suresh | |
3 | * Jagannathan, and Stephen Weeks. | |
4 | * Copyright (C) 1997-2000 NEC Research Institute. | |
5 | * | |
6 | * MLton is released under a BSD-style license. | |
7 | * See the file MLton-LICENSE for details. | |
8 | *) | |
9 | ||
10 | structure Source: SOURCE = | |
11 | struct | |
12 | ||
13 | datatype t = T of {file: File.t ref, | |
14 | lineNum: int ref, | |
15 | lineStart: int ref, | |
16 | origDir: Dir.t} | |
17 | ||
18 | fun getPos (T {file, lineNum, lineStart, ...}, n) = | |
19 | SourcePos.make {column = n - !lineStart, | |
20 | file = !file, | |
21 | line = !lineNum} | |
22 | ||
23 | fun lineStart (s as T {lineStart, ...}) = getPos (s, !lineStart) | |
24 | ||
25 | fun lineDirective (T {file, lineNum, lineStart, origDir}, | |
26 | f, | |
27 | {lineNum = n, lineStart = s}) = | |
28 | (Option.app (f, fn f => | |
29 | let | |
30 | val f = | |
31 | if OS.Path.isAbsolute f | |
32 | then f | |
33 | else OS.Path.mkCanonical (OS.Path.concat (origDir, f)) | |
34 | in | |
35 | file := f | |
36 | end) | |
37 | ; lineNum := n | |
38 | ; lineStart := s) | |
39 | ||
40 | fun new file = T {file = ref file, | |
41 | lineNum = ref 1, | |
42 | (* mllex file positions start at zero, while we report errors | |
43 | * starting in column 1, so we need to pretend the first line | |
44 | * starts at position ~1, which will translate position 0 to | |
45 | * column 1. | |
46 | *) | |
47 | lineStart = ref ~1, | |
48 | origDir = File.dirOf file} | |
49 | ||
50 | fun newline (T {lineStart, lineNum, ...}, n) = | |
51 | (Int.inc lineNum | |
52 | ; lineStart := n) | |
53 | ||
54 | fun name (T {file, ...}) = !file | |
55 | ||
56 | end |