Module Parsexp.Eager_and_positionsSource
Same as Parser but gives back a s-expression as soon as they are found in the input.
For instance you can use this function to parse a stream and stop at the first s-expression:
exception Got_sexp of Sexp.t
let fetch_sexp stream =
let module P = Parsexp.Sexp_parsing.Eager in
let rec hot_loop state stream stack =
match Stream.peek stream with
| None -> P.feed_eoi state stack
| Some char ->
let stack = P.feed state char stack in
Stream.junk stream;
hot_loop state stream stack
in
let got_sexp state sexp = raise_notrace (Got_sexp sexp) in
let count = Stream.count stream in
let state = P.State.create ~f:got_sexp ~no_sexp_is_error:true in
match hot_loop state stream P.Stack.empty with
| () -> assert false
| exception Got_sexp sexp ->
(* This test is true if the s-expression includes the last character passed to
the parser *)
if P.State.offset state > Stream.count stream - count then Stream.junk stream;
sexp
;;Values produces by the parser
Source
val feed :
Parsexp.Eager_and_positions.State.t ->
char ->
Parsexp.Eager_and_positions.Stack.t ->
Parsexp.Eager_and_positions.Stack.t @@ portableSource
val feed_eoi :
Parsexp.Eager_and_positions.State.t ->
Parsexp.Eager_and_positions.Stack.t ->
unit @@ portableSource
val feed_string :
Parsexp.Eager_and_positions.State.t ->
string ->
Parsexp.Eager_and_positions.Stack.t ->
Parsexp.Eager_and_positions.Stack.t @@ portableSource
val feed_substring :
Parsexp.Eager_and_positions.State.t ->
string ->
pos:int ->
len:int ->
Parsexp.Eager_and_positions.Stack.t ->
Parsexp.Eager_and_positions.Stack.t @@ portableSource
val feed_bytes :
Parsexp.Eager_and_positions.State.t ->
bytes ->
Parsexp.Eager_and_positions.Stack.t ->
Parsexp.Eager_and_positions.Stack.t @@ portableSource
val feed_subbytes :
Parsexp.Eager_and_positions.State.t ->
bytes ->
pos:int ->
len:int ->
Parsexp.Eager_and_positions.Stack.t ->
Parsexp.Eager_and_positions.Stack.t @@ portable