parse.ml 613 B

1234567891011121314151617181920212223
  1. open Lexing
  2. open Types
  3. let loc_from_lexpos pstart pend =
  4. let (fname, ystart, xstart) = begin
  5. pstart.pos_fname,
  6. pstart.pos_lnum,
  7. (pstart.pos_cnum - pstart.pos_bol + 1)
  8. end in
  9. (fname, ystart, xstart)
  10. let loc_msg lexbuf msg =
  11. let p = lexbuf.lex_curr_p in
  12. let y = p.pos_lnum in
  13. let x = p.pos_cnum - p.pos_bol in
  14. Printf.sprintf "%s at line %d, character %d" msg y x
  15. let parse_with_error lexbuf =
  16. try Parser.program Lexer.token lexbuf with
  17. | Lexer.SyntaxError msg ->
  18. raise (FatalError (loc_msg lexbuf msg))
  19. | Parser.Error ->
  20. raise (FatalError (loc_msg lexbuf "syntax error"))