Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file parser_intf.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194open!ImportmoduleA=Parser_automatonmoduletypeState=sig(** State of the parser *)typet(** Create a new parser state. [pos] is the initial position, it defaults to
[{line=1;col=0;offset=0}]. *)valcreate:?pos:Positions.pos->unit->t(** Reset the given parsing state. The following always succeed:
{[
reset t ?pos;
assert (t = create ?pos ())
]}
*)valreset:?pos:Positions.pos->t->unit(** Number of characters fed to the parser *)valoffset:t->int(** Position in the text *)valline:t->intvalcolumn:t->intvalposition:t->Positions.pos(** Prevent the state from receiving any more characters. Trying to feed more characters
will result in an exception, unless the state is reset. *)valstop:t->unitendmoduletypeStack=Kind.StackmoduletypeS=sig(** Values produced by the parser *)typeparsed_valuemoduleState:StatemoduleStack:Stack(** Feed one character to the parser. In case of error, it raises [Parse_error] *)valfeed:State.t->char->Stack.t->Stack.t(** Instruct the parser that the end of input was reached. In case of error, it raises
[Parse_error] *)valfeed_eoi:State.t->Stack.t->parsed_value(** {3 Convenience functions} *)valfeed_string:State.t->string->Stack.t->Stack.tvalfeed_substring:State.t->string->pos:int->len:int->Stack.t->Stack.tvalfeed_bytes:State.t->bytes->Stack.t->Stack.tvalfeed_subbytes:State.t->bytes->pos:int->len:int->Stack.t->Stack.t(** {3 High-level functions} *)valparse_string:string->(parsed_value,Parse_error.t)resultvalparse_string_exn:string->parsed_valueendmoduletypeS_eager=sig(** 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 *)typeparsed_valuemoduleState:sigincludeStatemoduleRead_only:sig(** Read-only handle to a parser state *)typetvaloffset:t->intvalline:t->intvalcolumn:t->intvalposition:t->Positions.posend(** [create ~f] create a new eager parser state. [f] will be called on each
s-expression found. If [f] raises, then the parser is made unusable ([stop t] is
invoked).
[no_sexp_is_error] controls the behavior of the parse when the end of input is
reached and no s-expression has been found. When [no_sexp_is_error] is [false]
(the default) [feed_eoi] just returns [()], when it is [false] [feed_eoi]
raises. In any case, if the end of input is reached while parsing an incomplete
s-expression such as [(abc], error is raised.
[f] must not save the read-only parser state it receives to access it after
returning. It is unspecified what values it will read if it does so. *)valcreate:?pos:Positions.pos->?no_sexp_is_error:bool(** default: false *)->(Read_only.t->parsed_value->unit)->t(**/**)valold_parser_cont_state:t->Old_parser_cont_state.t(**/**)endmoduleStack:Stackvalfeed:State.t->char->Stack.t->Stack.tvalfeed_eoi:State.t->Stack.t->unitvalfeed_string:State.t->string->Stack.t->Stack.tvalfeed_substring:State.t->string->pos:int->len:int->Stack.t->Stack.tvalfeed_bytes:State.t->bytes->Stack.t->Stack.tvalfeed_subbytes:State.t->bytes->pos:int->len:int->Stack.t->Stack.tmoduleLexbuf_consumer:sigtypetvalcreate:unit->t(** Consume exactly one s-expression from the given lexing buffer *)valparse:t->Lexing.lexbuf->parsed_value(** Consume exactly one s-expression from the given lexing buffer. Returns [None] if
the end of input is reached before seeing any s-expression. *)valparse_opt:t->Lexing.lexbuf->parsed_valueoptionendendmoduleMode(Kind:Kind.S)=structmoduletypeS=sigtypeparsed_valuevalmode:(Kind.state,Kind.Stack.t)A.modevalmake_value:(Kind.state,Kind.Stack.t)A.state->Kind.Stack.t->parsed_valueendendmoduleMode_eager(Kind:Kind.S)=structmoduletypeS=sigtypeparsed_valuevalmake_value:(Kind.state,Kind.Stack.t)A.state->Kind.Stack.t->parsed_valueendendmoduletypeParser=sigmoduleMode=ModemoduleMode_eager=Mode_eagermoduletypeS=SmoduletypeS_eager=S_eagermoduletypeStack=StackmoduletypeState=StatemoduleMake(Kind:Kind.S)(Mode:Mode(Kind).S):Swithtypeparsed_value=Mode.parsed_valuemoduleMake_eager(Kind:Kind.S)(Mode:Mode_eager(Kind).S):S_eagerwithtypeparsed_value=Mode.parsed_valueend