package parsexp

  1. Overview
  2. Docs

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
type parsed_value = Parsexp__.Import.Ppx_sexp_conv_lib.Sexp.t

Values produces by the parser

module State : sig ... end
module Stack : sig ... end
val feed : State.t -> char -> Stack.t -> Stack.t
val feed_eoi : State.t -> Stack.t -> unit
val feed_string : State.t -> string -> Stack.t -> Stack.t
val feed_substring : State.t -> string -> pos:int -> len:int -> Stack.t -> Stack.t
val feed_bytes : State.t -> bytes -> Stack.t -> Stack.t
val feed_subbytes : State.t -> bytes -> pos:int -> len:int -> Stack.t -> Stack.t
module Lexbuf_consumer : sig ... end
OCaml

Innovation. Community. Security.