Mercurial > urweb
comparison src/errormsg.sml @ 1:4202f6eda946
Initial parsing and pretty-printing
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Sat, 26 Jan 2008 12:35:32 -0500 |
parents | 502c6d622477 |
children | daa4f1d7a663 |
comparison
equal
deleted
inserted
replaced
0:502c6d622477 | 1:4202f6eda946 |
---|---|
34 first : pos, | 34 first : pos, |
35 last : pos} | 35 last : pos} |
36 | 36 |
37 type 'a located = 'a * span | 37 type 'a located = 'a * span |
38 | 38 |
39 | |
40 fun posToString {line, char} = | |
41 String.concat [Int.toString line, ":", Int.toString char] | |
42 | |
43 fun spanToString {file, first, last} = | |
44 String.concat [file, ":", posToString first, "-", posToString last] | |
45 | |
46 | |
47 val file = ref "" | |
48 val numLines = ref 1 | |
49 val lines : int list ref = ref [] | |
50 | |
51 fun resetPositioning fname = (file := fname; | |
52 numLines := 1; | |
53 lines := []) | |
54 | |
55 fun newline pos = (numLines := !numLines + 1; | |
56 lines := pos :: !lines) | |
57 | |
58 fun lastLineStart () = | |
59 case !lines of | |
60 [] => 0 | |
61 | n :: _ => n+1 | |
62 | |
63 fun posOf n = | |
64 let | |
65 fun search lineNum lines = | |
66 case lines of | |
67 [] => {line = 1, | |
68 char = n} | |
69 | bound :: rest => | |
70 if n > bound then | |
71 {line = lineNum, | |
72 char = n - bound - 1} | |
73 else | |
74 search (lineNum - 1) rest | |
75 in | |
76 search (!numLines) (!lines) | |
77 end | |
78 | |
79 fun spanOf (pos1, pos2) = {file = !file, | |
80 first = posOf pos1, | |
81 last = posOf pos2} | |
82 | |
83 | |
84 val errors = ref false | |
85 | |
86 fun resetErrors () = errors := false | |
87 fun anyErrors () = !errors | |
88 fun error s = (TextIO.output (TextIO.stdErr, s); | |
89 TextIO.output1 (TextIO.stdErr, #"\n"); | |
90 errors := true) | |
91 fun errorAt span s = (TextIO.output (TextIO.stdErr, spanToString span); | |
92 TextIO.output1 (TextIO.stdErr, #" "); | |
93 error s) | |
94 fun errorAt' span s = errorAt (spanOf span) s | |
95 | |
39 end | 96 end |