diff options
author | Vladimir Azarov <avm@intermediate-node.net> | 2025-05-19 21:17:49 +0200 |
---|---|---|
committer | Vladimir Azarov <avm@intermediate-node.net> | 2025-05-19 21:17:49 +0200 |
commit | b2d8dcd8673cfcdbf1e8a02aa19c53e42b8a60b6 (patch) | |
tree | 7de178aec8ebeacc7b4effe3b09de4485487da65 /tokenizer.fun | |
parent | 88378509521b46e615986f8c82d10b9da88830d2 (diff) |
Transition from exception-based errors to printf-based
Diffstat (limited to 'tokenizer.fun')
-rw-r--r-- | tokenizer.fun | 54 |
1 files changed, 15 insertions, 39 deletions
diff --git a/tokenizer.fun b/tokenizer.fun index 5b5f436..24e0211 100644 --- a/tokenizer.fun +++ b/tokenizer.fun @@ -129,9 +129,8 @@ struct datatype tkErrorAuxInfo = TkiEOF | TkiDx of int | TkiStart exception TkError of tkErrorAuxInfo * string - exception TkErrorAug of S.pos * string - exception FsmTableIsTooSmall + fun error pos msg = (printf `"\n" S.Ppos pos `": " `msg `"\n" %; exit 1) (* Unreachable (should be) *) exception TokenWithoutRepr @@ -392,8 +391,9 @@ struct val tokenRepr = filterNeeded tokenRepr [] - fun fsmInsert' T curState tk repr = fsmInsert T curState tk repr handle - Subscript => raise FsmTableIsTooSmall + fun fsmInsert' T curState tk repr = fsmInsert T curState tk repr + handle Subscript => + die 2 `"fsm table is too small. Increate 'maxState' value\n" % in while !r < length buf do ( update (buf, !r, (Invalid, array(128, ~1))); @@ -472,12 +472,12 @@ struct (tk, pos, stream) end - fun tkError2aug stream (dx, msg) = + fun errorDx stream dx msg = let val off = S.getOffset stream - 1 + dx val (pos, _) = S.getPosRaw off stream in - TkErrorAug (pos, msg) + error pos msg end fun parserWrapper stream parser acc = @@ -492,13 +492,13 @@ struct val (c, stream) = S.getchar stream val (acc, tk, stream) = parser acc (stream, startOff) c handle - TkError (TkiDx dx, msg) => raise tkError2aug stream (dx, msg) - | TkError (TkiStart, msg) => raise TkErrorAug (pos, msg) + TkError (TkiDx dx, msg) => errorDx stream dx msg + | TkError (TkiStart, msg) => error pos msg | TkError (TkiEOF, msg) => let val (pos, _) = S.EOFpos stream in - raise TkErrorAug (pos, msg) + error pos msg end in case tk of @@ -868,7 +868,7 @@ struct in case line of SOME line => (PpcInclude (getDir stream, line), stream) - | NONE => raise TkErrorAug (pos, "line does not end with '\n'\n") + | NONE => error pos "line does not end with '\\n'" end fun isPpcDir (PpcInclude _) = true @@ -880,8 +880,7 @@ struct fun handlePpcDir (tk, pos) stream = let open String - fun error () = - raise TkErrorAug (pos, "expected preprocessor directive") + val error = fn () => error pos "expected preprocessor directive" fun getById id = let @@ -904,29 +903,6 @@ struct | _ => error () end - (* - fun formPpcDir (Id s) = - let - open String - in - case List.find - (fn (_, repr) => - sub (repr, 0) = ppcPrefix andalso - extract (repr, 1, NONE) = s) - tokenRepr - of - SOME (tk, _) => tk - | NONE => raise ExpectedPpcDir - end - | formPpcDir kwElse = PpcElse - | formPpcDir kwIf = PpcIf - | formPpcDir _ = raise ExpectedPpcDir - - fun handlePpcDir (pos, tk) stream = - formPpcDir tk stream handle ExpectedPpcDir => - raise TkErrorAug (pos, "expected preprocessor directive") - *) - fun unexpectedCharRaise stream c = let val (pos, _) = S.getPosAfterChar stream @@ -936,7 +912,7 @@ struct else "<" ^ Int.toString (ord c) ^ ">" in - raise TkErrorAug (pos, "unexpected character " ^ repr) + error pos ("unexpected character " ^ repr) end fun skipComment stream pos = @@ -945,7 +921,7 @@ struct let val (c, stream) = case S.getchar stream of - (NONE, _) => raise TkErrorAug (pos, "unfinished comment") + (NONE, _) => error pos "unfinished comment" | (SOME c, stream) => (c, stream) in if prevIsAsterisk andalso c = #"/" then @@ -965,7 +941,7 @@ struct let val (pos, _) = S.getPosAfterChar stream in - raise TkErrorAug (pos, "expected \\n after backslash") + error pos "expected \\n after backslash" end in case c of @@ -990,7 +966,7 @@ struct val (tk, pos', stream) = getToken stream in if tk = EOS then - raise TkErrorAug (pos, "unfinished preprecessor directive") + error pos "unfinished preprecessor directive" else let val (tk, stream) = handlePpcDir (tk, pos') stream |