-
Notifications
You must be signed in to change notification settings - Fork 108
/
SourceFile.ML
63 lines (46 loc) · 1.57 KB
/
SourceFile.ML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(* SPDX-License-Identifier: HPND *)
(* Copyright (C) 1999-2002 Henry Cejtin, Matthew Fluet, Suresh
* Jagannathan, and Stephen Weeks.
* Copyright (C) 1997-1999 NEC Research Institute.
*
* Please see the file MLton-LICENSE for license information.
*)
signature SOURCE_FILE =
sig
type t
(* The pos in the following specs is a file position (e.g. yypos of mllex).
*)
val getPos: t * int -> SourcePos.t
val observe_line_directives : bool ref
val lineDirective:
t * string option * {lineNum: int, lineStart: int} -> unit
val lineStart: t -> SourcePos.t
val new: string -> t
val newline: t * int -> unit
end;
structure SourceFile: SOURCE_FILE =
struct
datatype t = T of {file: string ref,
lineNum: int ref,
lineStart: int ref}
fun getPos (T {file, lineNum, lineStart, ...}, n) =
SourcePos.make {column = n - !lineStart,
file = !file,
line = !lineNum}
fun lineStart (s as T {lineStart, ...}) = getPos (s, !lineStart)
val observe_line_directives = ref true
fun new file = T {file = ref file,
lineNum = ref 1,
lineStart = ref 0}
fun newline (T {lineStart, lineNum, ...}, n) =
(lineNum := !lineNum + 1
; lineStart := n)
fun lineDirective (src as T {file, lineNum, lineStart},
f,
{lineNum = n, lineStart = s}) =
if !observe_line_directives then
(Option.app (fn f => file := f) f
; lineNum := n
; lineStart := s)
else newline(src,s)
end;