open Lexing open Printf open Ast let get_position lexbuf = let pos = lexbuf.lex_curr_p in sprintf "%s:%d:%d" pos.pos_fname pos.pos_lnum (pos.pos_cnum - pos.pos_bol + 1) let parse_with_error lexbuf = try Some (Parser.program Lexer.token lexbuf) with | Lexer.SyntaxError msg -> raise (CompileError (sprintf "%s: %s" (get_position lexbuf) msg)) | Parser.Error -> raise (CompileError (sprintf "%s: syntax error" (get_position lexbuf))) let phase repr = print_endline "- Parse input"; 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 -> failwith "error during parsing" | Some ast -> Node (ast, verbose)) | _ -> failwith "invalid input for this phase"