forked from standardml/cmlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coord.sml
executable file
·43 lines (29 loc) · 1.17 KB
/
coord.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
(* Adapted from Chris Okasaki, Robert Harper, and Tom Murphy VII's position
* library. *)
structure Coord :> COORD =
struct
type coord = {file: string, char: int, line: int, abs: int}
type t = coord
fun init s = {file = s, char = 1, line = 1, abs = 1}
fun nextchar {file, char, line, abs} =
{file = file, char = char + 1, line = line, abs = abs + 1}
fun nextline {file, char, line, abs} =
{file = file, char = 1, line = line + 1, abs = abs}
fun file (pos: coord) = #file pos
fun abs (pos: coord) = #abs pos
fun line (pos: coord) = #line pos
fun char (pos: coord) = #char pos
fun eq (pos1, pos2) = abs pos1 = abs pos2
fun compare (pos1, pos2) = Int.compare (abs pos1, abs pos2)
fun hash pos = Word.fromInt (abs pos)
fun toString {file, char, line, abs} =
file ^ ":" ^ Int.toString line ^ "." ^ Int.toString char
fun leftmost pos1 pos2 =
case compare (pos1, pos2) of
GREATER => pos2
| _ => pos1
fun rightmost pos1 pos2 =
case compare (pos1, pos2) of
LESS => pos2
| _ => pos1
end