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