aboutsummaryrefslogtreecommitdiffstats
path: root/lib/front.ml
blob: b7288250b56385c56d5b662c7f786979e1fa7ae3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
type error = { line : int; column : int; message : string }

let position lexbuf =
  let p = lexbuf.Lexing.lex_curr_p in
  (p.Lexing.pos_lnum, p.Lexing.pos_cnum - p.Lexing.pos_bol)

let parse source =
  let lexbuf = Lexing.from_string source in
  match Parser.program Lexer.token lexbuf with
  | statements -> Ok statements
  | exception Lexer.Error message ->
      let line, column = position lexbuf in
      Error { line; column; message }
  | exception Parser.Error ->
      let line, column = position lexbuf in
      Error
        {
          line;
          column;
          message = Printf.sprintf "syntax error before %S" (Lexing.lexeme lexbuf);
        }

let parse_string source =
  match parse source with
  | Ok statements -> Ok statements
  | Error { line; message; _ } ->
      Error (Printf.sprintf "line %d: %s" line message)