parse.ml 1.0 KB

1234567891011121314151617181920212223242526272829
  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 shift_back lexbuf = shift_loc (get_loc lexbuf) 0 (-1)
  8. let parse_with_error lexbuf =
  9. try Some (Parser.program Lexer.token lexbuf) with
  10. | Lexer.SyntaxError msg ->
  11. raise (LocError ((shift_back lexbuf), msg))
  12. | Parser.Error ->
  13. raise (LocError ((shift_back lexbuf), "syntax error"))
  14. let phase input =
  15. prerr_endline "- Parse input";
  16. match input with
  17. | FileContent (display_name, content) ->
  18. let lexbuf = Lexing.from_string content in
  19. lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = display_name };
  20. let ast = parse_with_error lexbuf in
  21. (match ast with
  22. | None -> raise (CompileError "no syntax tree was constructed")
  23. | Some node -> Ast node)
  24. | _ -> raise (InvalidInput "parse")