| 1234567891011121314151617181920212223242526272829303132333435 |
- open Lexing
- open Printf
- open Ast
- let get_loc lexbuf =
- Util.loc_from_lexpos lexbuf.lex_curr_p lexbuf.lex_curr_p
- let parse_with_error lexbuf =
- try Some (Parser.program Lexer.token lexbuf) with
- | Lexer.SyntaxError msg ->
- raise (LocError (get_loc lexbuf, msg))
- | Parser.Error ->
- raise (LocError (get_loc lexbuf, "syntax error"))
- let phase repr =
- let _ = print_endline "- Parse input" in
- match repr with
- | Inputfile (filename, verbose) ->
- (* TODO: run preprocessor *)
- let infile = match filename with
- | Some value -> open_in value
- | None -> stdin
- in
- let display_name = match filename with
- | Some value -> value
- | None -> "stdin"
- in
- let lexbuf = Lexing.from_channel infile in
- lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = display_name };
- let ast = parse_with_error lexbuf in
- close_in infile;
- (match ast with
- | None -> raise (CompileError "error during parsing")
- | Some ast -> Node (ast, verbose))
- | _ -> raise (CompileError "invalid input for this phase")
|