-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.ml
39 lines (30 loc) · 951 Bytes
/
parse.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
open Lexing
exception SyntaxError of string
let location_message lexbuf =
let start = lexeme_start_p lexbuf in
let finish = lexeme_end_p lexbuf in
Printf.sprintf "line %d, characters %d-%d"
start.pos_lnum
(start.pos_cnum - start.pos_bol)
(finish.pos_cnum - finish.pos_bol)
let syntax_error_message lexbuf =
Printf.sprintf
"Syntax error, %s: %s"
(location_message lexbuf)
(lexeme lexbuf)
let parse_error lexbuf =
raise (SyntaxError (syntax_error_message lexbuf))
let unexpected_error msg lexbuf =
failwith ("Unexpected parsing exception: " ^ msg
^ "\noccurred at " ^ (location_message lexbuf))
let parse parser_start s =
let lexbuf = from_string s in
try
parser_start Lexer.token lexbuf
with
| Parser.Error | Lexer.Error -> parse_error lexbuf
| Failure s -> unexpected_error s lexbuf
let parse_expr =
parse Parser.parse_expression
let parse_phrase =
parse Parser.parse_phrase