jon.recoil.org

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
  ;;
Sourcetype parsed_value = Sexplib0.Sexp.t * Positions.t

Values produces by the parser

Sourcemodule State : sig ... end
Sourcemodule Stack : sig ... end
Sourcemodule Lexbuf_consumer : sig ... end