parse.ml 1.0 KB

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