18 lines
625 B
OCaml
18 lines
625 B
OCaml
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
|