diff options
Diffstat (limited to '')
| -rw-r--r-- | lib/front.ml | 14 | ||||
| -rw-r--r-- | lib/front.mli | 7 |
2 files changed, 21 insertions, 0 deletions
diff --git a/lib/front.ml b/lib/front.ml new file mode 100644 index 0000000..7cacc20 --- /dev/null +++ b/lib/front.ml @@ -0,0 +1,14 @@ +let parse_string source = + let lexbuf = Lexing.from_string source in + match Parser.program Lexer.token lexbuf with + | statements -> Ok statements + | exception Lexer.Error message -> + let position = lexbuf.Lexing.lex_curr_p in + Error + (Printf.sprintf "lexical error at line %d: %s" + position.Lexing.pos_lnum message) + | exception Parser.Error -> + let position = lexbuf.Lexing.lex_curr_p in + Error + (Printf.sprintf "syntax error at line %d, before %S" + position.Lexing.pos_lnum (Lexing.lexeme lexbuf)) diff --git a/lib/front.mli b/lib/front.mli new file mode 100644 index 0000000..00fa65f --- /dev/null +++ b/lib/front.mli @@ -0,0 +1,7 @@ +(** Front door: text to statements. + + Wraps the generated lexer and parser so callers see one total + function from source text to syntax or a positioned message. + The REPL, the file runner, and the tests all enter here. *) + +val parse_string : string -> (Ast.statement list, string) result |
