From 97f84ccace4e634b4e02178a702818e69292dc9f Mon Sep 17 00:00:00 2001 From: Malte Voos Date: Mon, 8 Jul 2024 22:01:42 +0200 Subject: implement top-level definitions --- src/parser/Parser.ml | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'src/parser/Parser.ml') 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 -- cgit 1.4.1