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 input = print_endline "- Parse input"; match input with | Args args -> let infile = match args.filename with | Some value -> open_in value | None -> stdin in let display_name = match args.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 -> Ast (ast, args)) | _ -> raise (InvalidInput "parse")