-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilepos.sml
49 lines (41 loc) · 1.64 KB
/
filepos.sml
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
(* $Id$
*
* Copyright (c) 2008 Timothy Bourke (University of NSW and NICTA)
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the "BSD License" which is distributed with the
* software in the file LICENSE.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the BSD
* License for more details.
*)
structure FilePos :> FILE_POS =
struct
type state = {line: int, pos: int, depth: int} ref
type pos = int * int
val zero = (0, 0)
fun newstate () = ref {line=1, pos= ~1, depth=0}
fun nextline (st as ref {line, depth,...}, yypos) = st := {line=line + 1,
pos=yypos - 1,
depth=depth}
fun currpos (ref {line, pos, depth}, yypos) = (line, yypos - pos)
fun error prefix (str, (line, pos), (_, _)) = let
fun pr s = TextIO.output (TextIO.stdOut, s)
in
pr (prefix); pr ":";
pr (Int.toString line); pr ":";
pr (Int.toString pos); pr ":";
pr str; pr "\n"
end
fun incCommentDepth (st as ref {line, pos, depth}) = st := {line=line,
pos=pos,
depth=depth+1}
fun decCommentDepth (st as ref {line, pos, depth=0}) = true
| decCommentDepth (st as ref {line, pos, depth}) = let in
st := {line=line, pos=pos, depth=depth-1};
depth = 0
end
end