From 28605345c88491627b7a34cea6e50c9e5b9b8b01 Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Sat, 26 Jan 2008 12:35:32 -0500 Subject: Initial parsing and pretty-printing --- src/errormsg.sml | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'src/errormsg.sml') 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 -- cgit v1.2.3