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 get_loc lexbuf = let pos = lexbuf.lex_curr_p in let colnum = (pos.pos_cnum - pos.pos_bol + 1) 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))) (*raise (LocError ("syntax error" (get_loc lexbuf)))*) 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")