parse.ml 998 B

123456789101112131415161718192021222324252627
  1. open Lexing
  2. open Ast
  3. let get_loc lexbuf =
  4. Util.loc_from_lexpos lexbuf.lex_curr_p lexbuf.lex_curr_p
  5. let shift_loc (fname, ystart, yend, xstart, xend) yshift xshift =
  6. (fname, ystart + yshift, yend + yshift, xstart + xshift, xend + xshift)
  7. let parse_with_error lexbuf =
  8. try Some (Parser.program Lexer.token lexbuf) with
  9. | Lexer.SyntaxError msg ->
  10. raise (LocError (shift_loc (get_loc lexbuf) 0 (-1), msg))
  11. | Parser.Error ->
  12. raise (LocError (get_loc lexbuf, "syntax error"))
  13. let phase input =
  14. prerr_endline "- Parse input";
  15. match input with
  16. | FileContent (display_name, content, args) ->
  17. let lexbuf = Lexing.from_string content in
  18. lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = display_name };
  19. let ast = parse_with_error lexbuf in
  20. (match ast with
  21. | None -> raise (CompileError "no syntax tree was constructed")
  22. | Some node -> Ast (node, args))
  23. | _ -> raise (InvalidInput "parse")