tests: fix on windows (appveyor)
[sqlgg.git] / lib / parser_utils.ml
blob63fe0ecafe605fdbcff43ab8806236d1985e9f8b
1 (* Simple wrapper tying together parser and lexer *)
3 module type Parser_type =
4 sig
5 type token
6 type result
7 val input : (Lexing.lexbuf -> token) -> Lexing.lexbuf -> result
8 val rule : Lexing.lexbuf -> token
9 end
11 exception Error of exn * (int * int * string * string)
13 module Make(T : Parser_type) =
14 struct
15 let parse_buf_exn lexbuf =
16 try
17 T.input T.rule lexbuf
18 with exn ->
19 begin
20 let curr = lexbuf.Lexing.lex_curr_p in
21 let line = curr.Lexing.pos_lnum in
22 let cnum = curr.Lexing.pos_cnum - curr.Lexing.pos_bol in
23 let tok = Lexing.lexeme lexbuf in
24 let tail = Sql_lexer.ruleTail "" lexbuf in
25 raise (Error (exn,(line,cnum,tok,tail)))
26 end
28 let parse_buf lexbuf = try Some (parse_buf_exn lexbuf) with _ -> None
30 let parse_stdin () = parse_buf (Lexing.from_channel stdin)
31 let parse_string str = (*Error.log "Parsing : %s" str; *)
32 parse_buf (Lexing.from_string str)
34 let parse_file filename =
35 let contents = try Std.input_file filename with _ -> "" in
36 parse_string contents
38 end