aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/qparser/analyzer.ml23
-rw-r--r--lib/qparser/lexer.ml13
-rw-r--r--lib/qparser/lexer.mli1
-rw-r--r--test/syntax_error.ml5
4 files changed, 24 insertions, 18 deletions
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
diff --git a/test/syntax_error.ml b/test/syntax_error.ml
index 6387530..e9e3fca 100644
--- a/test/syntax_error.ml
+++ b/test/syntax_error.ml
@@ -103,6 +103,10 @@ let unclose_comment () =
_test_instruction {| ! that's it|}
{ level = Error; loc = _position; message = "Unclosed text" }
+let syntax_error () =
+ _test_instruction {|*clr $ cla|}
+ { level = Error; loc = _position; message = "Unexpected character \"\"" }
+
let test =
( "Syntax Errors",
[
@@ -114,4 +118,5 @@ let test =
Alcotest.test_case "no &" `Quick missing_ampersand_2;
Alcotest.test_case "no &" `Quick missing_ampersand_3;
Alcotest.test_case "unclose_comment" `Quick unclose_comment;
+ Alcotest.test_case "Syntax error $" `Quick syntax_error;
] )