Parse scalar expressions with source spans

This commit is contained in:
milner committed 2017-01-10 16:42:00 +00:00
1 parent 582b2c8451
commit a036abb227
9 files changed
+363 -12

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
let one_line text =
match String.index_opt text '\n' with
| Some index -> String.sub text 0 index
| None -> text
let unexpected text lexbuf =
let start = Lexing.lexeme_start_p lexbuf in
let stop = Lexing.lexeme_end_p lexbuf in
let token =
if start.Lexing.pos_cnum >= String.length text then "end of file"
else "`" ^ one_line (Lexing.lexeme lexbuf) ^ "`"
in
Diagnostic.error (Location.span_of_lexing start stop) "syntax error: unexpected %s" token
let expression text =
let lexbuf = Lexing.from_string text in
try Parser.expr Lexer.token lexbuf with Parsing.Parse_error -> unexpected text lexbuf