summaryrefslogtreecommitdiff
path: root/src/lib/reader.ml
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@chimrod.com>2021-08-23 14:37:53 +0200
committerSébastien Dailly <sebastien@chimrod.com>2021-08-23 14:37:53 +0200
commit546afdcf2148087f3a90b69c23ea756550f64433 (patch)
treeac56c71393aacf0fade729e98eeecb1e87a88534 /src/lib/reader.ml
Initial commit
Diffstat (limited to 'src/lib/reader.ml')
-rw-r--r--src/lib/reader.ml37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/lib/reader.ml b/src/lib/reader.ml
new file mode 100644
index 0000000..4b123d5
--- /dev/null
+++ b/src/lib/reader.ml
@@ -0,0 +1,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
+