diff options
Diffstat (limited to 'src/parser/Parser.ml')
-rw-r--r-- | src/parser/Parser.ml | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/src/parser/Parser.ml b/src/parser/Parser.ml index 88448c9..ae86885 100644 --- a/src/parser/Parser.ml +++ b/src/parser/Parser.ml @@ -1,3 +1,9 @@ +let handle_syntax_error ~source ~lexbuf f = try f () with + | Lexer.IllegalCharacter illegal_char -> + Error.illegal_character ~loc:(Asai.Range.of_lexbuf ~source lexbuf) illegal_char + | Grammar.Error -> + Error.syntax_error ~loc:(Asai.Range.of_lexbuf ~source lexbuf) + let parse_expr (s : string) : Ast.expr = let lexbuf = Lexing.from_string s in let string_source : Asai.Range.string_source = { @@ -6,8 +12,15 @@ let parse_expr (s : string) : Ast.expr = } in let source = `String string_source in Eff.run ~env:source @@ fun () -> - try Grammar.start_expr Lexer.token lexbuf with - | Lexer.IllegalCharacter illegal_char -> - Error.illegal_character ~loc:(Asai.Range.of_lexbuf ~source lexbuf) illegal_char - | Grammar.Error -> - Error.syntax_error ~loc:(Asai.Range.of_lexbuf ~source lexbuf) + handle_syntax_error ~source ~lexbuf @@ fun () -> + Grammar.start_expr Lexer.token lexbuf + +let parse_file (path : string) : Ast.file = + let inchan = try open_in path with + | Sys_error msg -> Error.file_open_error ~path ~msg + in + let lexbuf = Lexing.from_channel inchan in + let source = `File path in + Eff.run ~env:source @@ fun () -> + handle_syntax_error ~source ~lexbuf @@ fun () -> + Grammar.start_file Lexer.token lexbuf |