From b798ac29c37299b2f761243ae92ab8f7c4c4d7f1 Mon Sep 17 00:00:00 2001 From: Paweł Dybiec Date: Tue, 30 Oct 2018 15:32:56 +0100 Subject: Initial commit --- source/xi/parser_wrapper.ml | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 source/xi/parser_wrapper.ml (limited to 'source/xi/parser_wrapper.ml') diff --git a/source/xi/parser_wrapper.ml b/source/xi/parser_wrapper.ml new file mode 100644 index 0000000..4b371c5 --- /dev/null +++ b/source/xi/parser_wrapper.ml @@ -0,0 +1,42 @@ +open Xi_lib +open Iface + +module Make(LP:LEXER_AND_PARSER) = struct + + module L = LP.Lexer + + module P = LP.Parser + + let open_file_lexbuf file = + let channel = open_in file in + let lexbuf = Lexing.from_channel channel in + (* Wpisujemy nazwe pliku (katalog ze ścieżki ucina Filename.basename) + * do lexbuf. Dzięki temu Parser_utils.makeLocation będzie mógł lokacje + * uzupełniać o prawidłową nazwę pliku. + *) + lexbuf.Lexing.lex_curr_p <- { + lexbuf.Lexing.lex_curr_p with + Lexing.pos_fname = Filename.basename file + }; + lexbuf + + let parse_lexbuf f lexbuf = + try + Ok (P.file L.token lexbuf); + with + | P.Error -> + let loc = Parser_utils.mkLocation lexbuf.Lexing.lex_curr_p in + let token = Lexing.lexeme lexbuf in + let s = if String.length token > 0 + then Printf.sprintf "syntax error: unexpected token: %s" token + else Printf.sprintf "syntax error: unexpected end" + in + Error (loc, s) + + | Parser_utils.InvalidToken (loc, str) -> + let s = Printf.sprintf "syntax error: invalid token: %s" str in + Error (loc, s) + + let parse_file f = parse_lexbuf f (open_file_lexbuf f) +end + -- cgit 1.4.1