summaryrefslogtreecommitdiff
path: root/src/errormsg.sml
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2008-01-26 12:35:32 -0500
committerGravatar Adam Chlipala <adamc@hcoop.net>2008-01-26 12:35:32 -0500
commit28605345c88491627b7a34cea6e50c9e5b9b8b01 (patch)
tree3ff8deb5599074bb5f67cd1ccda2ad1bb74d5b5b /src/errormsg.sml
parent9c27c9d90a3f3593de07658a14581b66d08c8b75 (diff)
Initial parsing and pretty-printing
Diffstat (limited to 'src/errormsg.sml')
-rw-r--r--src/errormsg.sml57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/errormsg.sml b/src/errormsg.sml
index 9f41fff3..0f5b9b61 100644
--- a/src/errormsg.sml
+++ b/src/errormsg.sml
@@ -36,4 +36,61 @@ type span = {file : string,
type 'a located = 'a * span
+
+fun posToString {line, char} =
+ String.concat [Int.toString line, ":", Int.toString char]
+
+fun spanToString {file, first, last} =
+ String.concat [file, ":", posToString first, "-", posToString last]
+
+
+val file = ref ""
+val numLines = ref 1
+val lines : int list ref = ref []
+
+fun resetPositioning fname = (file := fname;
+ numLines := 1;
+ lines := [])
+
+fun newline pos = (numLines := !numLines + 1;
+ lines := pos :: !lines)
+
+fun lastLineStart () =
+ case !lines of
+ [] => 0
+ | n :: _ => n+1
+
+fun posOf n =
+ let
+ fun search lineNum lines =
+ case lines of
+ [] => {line = 1,
+ char = n}
+ | bound :: rest =>
+ if n > bound then
+ {line = lineNum,
+ char = n - bound - 1}
+ else
+ search (lineNum - 1) rest
+ in
+ search (!numLines) (!lines)
+ end
+
+fun spanOf (pos1, pos2) = {file = !file,
+ first = posOf pos1,
+ last = posOf pos2}
+
+
+val errors = ref false
+
+fun resetErrors () = errors := false
+fun anyErrors () = !errors
+fun error s = (TextIO.output (TextIO.stdErr, s);
+ TextIO.output1 (TextIO.stdErr, #"\n");
+ errors := true)
+fun errorAt span s = (TextIO.output (TextIO.stdErr, spanToString span);
+ TextIO.output1 (TextIO.stdErr, #" ");
+ error s)
+fun errorAt' span s = errorAt (spanOf span) s
+
end