Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file parsexp_intf.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321(** Parsing of s-expression *)openImportopenPpx_sexp_conv_libmoduletypeParser_state=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->unitendmoduletypeParser_stack=sig(** Parser stack. The stack is not in [state] for optimization purposes. *)typetvalempty:tendmoduletypeParser=sig(** Values produced by the parser *)typeparsed_valuemoduleState:Parser_statemoduleStack:Parser_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} *)moduleError:sigtypetendvalparse_string:string->(parsed_value,Error.t)resultvalparse_string_exn:string->parsed_valueendmoduletypeEager_parser=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:sigincludeParser_statemoduleRead_only:sigtypet(** Read-only handle to a parser state *)valoffset: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->Parser_automaton_internal.Public.Old_parser_cont_state.t(**/**)endmoduleStack:Parser_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_valueoptionendendmoduletypeConv=sigtype'arestypechunk_to_convtypeparsed_sexpmoduleParse_error:sigtypetendmoduleOf_sexp_error:sigtypetendmoduleConv_error:sigtypetendvalparse_string:string->(chunk_to_conv->'a)->('ares,Conv_error.t)resultvalparse_string_exn:string->(chunk_to_conv->'a)->'aresvalconv:parsed_sexp*Positions.t->(chunk_to_conv->'a)->('ares,Of_sexp_error.t)resultvalconv_exn:parsed_sexp*Positions.t->(chunk_to_conv->'a)->'ares(** Convenience function for merging parsing and conversion errors.
For instance if you have a [load] function as follow:
{[
val load : string -> (Sexp.t list * Positions.t, Parse_error.t) result
]}
then you can create a [load_conv] function as follow:
{[
let load_conv : string -> (Sexp.t -> 'a) -> ('a list, Conv_error.t) result
= fun filename f -> conv_combine (load filename) f
]}
*)valconv_combine:(parsed_sexp*Positions.t,Parse_error.t)result->(chunk_to_conv->'a)->('ares,Conv_error.t)resultendmoduletypeParsexp=sigmodulePositions=PositionsmoduleCst=CstmoduleParse_error:sigtypet[@@deriving_inlinesexp_of]includesig[@@@ocaml.warning"-32"]valsexp_of_t:t->Ppx_sexp_conv_lib.Sexp.tend[@@ocaml.doc"@inline"][@@@end]valposition:t->Positions.posvalmessage:t->string(** Report an error in a style similar to OCaml, for instance:
File "blah", line 42, character 10:
Error: s-expression parsing error;
unterminated quoted string.
*)valreport:Format.formatter->filename:string->t->unitendmoduletypeParser=ParserwithmoduleError:=Parse_errormoduletypeEager_parser=Eager_parser(** Exception raised in case of a parsing error *)exceptionParse_errorofParse_error.tmoduleSingle:Parserwithtypeparsed_value=Sexp.tmoduleMany:Parserwithtypeparsed_value=Sexp.tlistmoduleEager:Eager_parserwithtypeparsed_value=Sexp.tmoduleSingle_and_positions:Parserwithtypeparsed_value=Sexp.t*Positions.tmoduleMany_and_positions:Parserwithtypeparsed_value=Sexp.tlist*Positions.tmoduleEager_and_positions:Eager_parserwithtypeparsed_value=Sexp.t*Positions.tmoduleSingle_just_positions:Parserwithtypeparsed_value=Positions.tmoduleMany_just_positions:Parserwithtypeparsed_value=Positions.tmoduleEager_just_positions:Eager_parserwithtypeparsed_value=Positions.tmoduleMany_cst:Parserwithtypeparsed_value=Cst.t_or_commentlistmoduleEager_cst:Eager_parserwithtypeparsed_value=Cst.t_or_commentmoduleOf_sexp_error:sigtypet[@@deriving_inlinesexp_of]includesig[@@@ocaml.warning"-32"]valsexp_of_t:t->Ppx_sexp_conv_lib.Sexp.tend[@@ocaml.doc"@inline"][@@@end](** Exception raised by the user function *)valuser_exn:t->exn(** S-expression that failed to be converted *)valsub_sexp:t->Sexp.t(** Position of [sub_sexp t] in the original source, if found *)vallocation:t->Positions.rangeoption(** Similar to [Parse_error.report] *)valreport:Format.formatter->filename:string->t->unitend(** Exception raised in case of a conversion error *)exceptionOf_sexp_errorofOf_sexp_error.tmoduleConv_error:sigtypet=|Parse_errorofParse_error.t|Of_sexp_errorofOf_sexp_error.t[@@deriving_inlinesexp_of]includesig[@@@ocaml.warning"-32"]valsexp_of_t:t->Ppx_sexp_conv_lib.Sexp.tend[@@ocaml.doc"@inline"][@@@end](** Similar to [Parse_error.report] *)valreport:Format.formatter->filename:string->t->unitendmoduletypeConv=ConvwithmoduleParse_error:=Parse_errorwithmoduleOf_sexp_error:=Of_sexp_errorwithmoduleConv_error:=Conv_error(*_ These type synonyms are introduced because older versions of OCaml
do not support destructive substitutions with `type 'a t1 = t2`
or `type t1 = 'a t2`. *)type'aid='atypesexp_list=Sexp.tlistmoduleConv_single:Convwithtype'ares:='aidandtypeparsed_sexp:=Sexp.tandtypechunk_to_conv:=Sexp.tmoduleConv_many:Convwithtype'ares:='alistandtypeparsed_sexp:=sexp_listandtypechunk_to_conv:=Sexp.tmoduleConv_many_at_once:Convwithtype'ares:='aidandtypeparsed_sexp:=sexp_listandtypechunk_to_conv:=sexp_listend