diff options
author | Malte Voos <git@mal.tc> | 2024-07-08 22:01:42 +0200 |
---|---|---|
committer | Malte Voos <git@mal.tc> | 2024-07-08 22:01:42 +0200 |
commit | 97f84ccace4e634b4e02178a702818e69292dc9f (patch) | |
tree | 9cef95c62e3fa078db256c7fe657732fecef40a8 /src/parser/Parser.ml | |
parent | 57de10d8728f51942f676b68f1f3ea29d9b78e6e (diff) | |
download | toytt-97f84ccace4e634b4e02178a702818e69292dc9f.tar.gz toytt-97f84ccace4e634b4e02178a702818e69292dc9f.zip |
implement top-level definitions
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 |