From 617d2ee554ccf502c718391aa05b7b72668b83e7 Mon Sep 17 00:00:00 2001 From: Chimrod <> Date: Sat, 14 Oct 2023 15:00:49 +0200 Subject: Added a new error case --- lib/qparser/analyzer.ml | 23 +++++++++++++++-------- lib/qparser/lexer.ml | 13 +++---------- lib/qparser/lexer.mli | 1 + 3 files changed, 19 insertions(+), 18 deletions(-) (limited to 'lib/qparser') diff --git a/lib/qparser/analyzer.ml b/lib/qparser/analyzer.ml index c165d03..fba96e0 100644 --- a/lib/qparser/analyzer.ml +++ b/lib/qparser/analyzer.ml @@ -17,14 +17,21 @@ let parse : let init = Parser.Incremental.main (fst (Lexbuf.positions l)) in let evaluation = - try IncrementalParser.of_lexbuf lexer l init - with Lexer.UnclosedQuote -> - let start_pos, end_pos = Lexbuf.positions l in - let err = - IncrementalParser. - { code = Interpreter.Custom "Unclosed text"; start_pos; end_pos } - in - Error err + try IncrementalParser.of_lexbuf lexer l init with + | Lexer.LexError message -> + let start_pos, end_pos = Lexbuf.positions l in + let err = + IncrementalParser. + { code = Interpreter.Custom message; start_pos; end_pos } + in + Error err + | Lexer.UnclosedQuote -> + let start_pos, end_pos = Lexbuf.positions l in + let err = + IncrementalParser. + { code = Interpreter.Custom "Unclosed text"; start_pos; end_pos } + in + Error err in Result.map_error diff --git a/lib/qparser/lexer.ml b/lib/qparser/lexer.ml index 3e1c05b..fe2b487 100644 --- a/lib/qparser/lexer.ml +++ b/lib/qparser/lexer.ml @@ -5,12 +5,9 @@ open Tokens exception UnclosedQuote -exception LexError of Lexing.position * string +exception LexError of string exception EOF -let pp_pos out { Lexing.pos_lnum; pos_cnum; pos_bol; _ } = - Format.fprintf out "line %d:%d" pos_lnum (pos_cnum - pos_bol) - (* Extract the location name from the pattern *) let location_name = Str.regexp {|.* \(.*\)|} @@ -177,14 +174,10 @@ let rec token : Lexbuf.t -> token = | '{' -> LITERAL (wait_balance (read_long_string 0) buffer) | eof -> raise EOF | _ -> - let position = fst @@ Sedlexing.lexing_positions lexbuf in let tok = Lexbuf.content buffer in + let msg = Format.asprintf "Unexpected character %S" tok in - let msg = - Format.asprintf "Unexpected character %S at %a" tok pp_pos position - in - - raise @@ LexError (position, msg) + raise @@ LexError msg let rec discard buffer = let lexbuf = Lexbuf.buffer buffer in diff --git a/lib/qparser/lexer.mli b/lib/qparser/lexer.mli index 41ecb16..30766e0 100644 --- a/lib/qparser/lexer.mli +++ b/lib/qparser/lexer.mli @@ -1,5 +1,6 @@ exception EOF exception UnclosedQuote +exception LexError of string val token : Lexbuf.t -> Tokens.token val discard : Lexbuf.t -> unit -- cgit v1.2.3