summaryrefslogtreecommitdiff
path: root/src/lib/reader.ml
blob: 4b123d53a33f8e1229b316f7c7709f3e3ffbd51e (plain)
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
28
29
30
31
32
33
34
35
36
37
open StdLabels

module P = Parser
module Parser = P.Make(Sounds)
module I =   Parser.MenhirInterpreter

let sound_to_string
  : Sounds.t list -> string
  = fun t ->
    let buff = Buffer.create 16 in
    List.iter t
      ~f:(fun f -> Buffer.add_string buff f.Sounds.repr);
    Buffer.contents buff

let succeed (res : Sounds.t list) =
  Ok (sound_to_string res)

let fail lexbuf (_ : 'a I.checkpoint) =
  Error (
    Printf.sprintf
      "At offset %d: syntax error."
      (Lexing.lexeme_start lexbuf)
  )

(* The parser has suspended itself because of a syntax error. Stop. *)

let loop lexbuf result =
  let supplier = I.lexer_lexbuf_to_supplier Lexer.letter lexbuf in
  I.loop_handle succeed (fail lexbuf) supplier result

let process (line : string) =
  let lexbuf = Lexing.from_string line in
  try
    loop lexbuf (Parser.Incremental.main lexbuf.lex_curr_p)
  with
  | Lexer.Error msg -> Error msg