123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424(******************************************************************************)(* *)(* Menhir *)(* *)(* Copyright Inria. All rights reserved. This file is distributed under *)(* the terms of the GNU Library General Public License version 2, with a *)(* special exception on linking, as described in the file LICENSE. *)(* *)(******************************************************************************)typeposition=Lexing.position(**This signature describes the incremental LR engine. When the engine
is used in this mode, the user controls the lexer, and the parser
suspends itself when it needs to read a new token. *)moduletypeINCREMENTAL_ENGINE=sigtypetoken(**A value of type {!production} is (an index for) a production. The start
productions (which do not exist in an [.mly] file, but are constructed by
Menhir internally) are not part of this type. *)typeproduction(**A value of type ['a env] represents a configuration of the automaton:
current state, stack, lookahead token, etc. The parameter ['a] is the
type of the semantic value that will eventually be produced if the parser
succeeds.
In normal operation, the parser works with checkpoints: see the functions
{!offer} and {!resume}. However, it is also possible to work directly with
environments (see the functions {!pop}, {!force_reduction}, and [feed]) and
to reconstruct a checkpoint out of an environment (see {!input_needed}).
This is considered advanced functionality; its purpose is to allow error
recovery strategies to be programmed by the user. *)type'aenv(**The type ['a checkpoint] represents an intermediate or final state of the
parser. An intermediate checkpoint is a suspension: it records the parser's
current state, and allows parsing to be resumed. The parameter ['a] is
the type of the semantic value that will eventually be produced if the
parser succeeds.
[Accepted] and [Rejected] are final checkpoints. [Accepted] carries a
semantic value.
[InputNeeded] is an intermediate checkpoint. It means that the parser wishes
to read one token before continuing.
[Shifting] is an intermediate checkpoint. It means that the parser is taking
a shift transition. It exposes the state of the parser before and after
the transition. The Boolean parameter tells whether the parser intends to
request a new token after this transition. (It always does, except when
it is about to accept.)
[AboutToReduce] is an intermediate checkpoint. It means that the parser is
about to perform a reduction step. It exposes the parser's current
state as well as the production that is about to be reduced.
[HandlingError] is an intermediate checkpoint. It means that the parser has
detected an error and is currently handling it, in several steps. *)type'acheckpoint=private|InputNeededof'aenv|Shiftingof'aenv*'aenv*bool|AboutToReduceof'aenv*production|HandlingErrorof'aenv|Acceptedof'a|Rejected(**[offer] allows the user to resume the parser after it has suspended
itself with a checkpoint of the form [InputNeeded env]. [offer] expects
the old checkpoint as well as a new token and produces a new checkpoint.
It does not raise any exception. *)valoffer:'acheckpoint->token*position*position->'acheckpoint(**The optional argument [strategy] influences the manner in which {!resume}
deals with checkpoints of the form [HandlingError _]. Its default value
is [`Legacy]. It can be briefly described as follows:
- If the [error] token is used only to report errors (that is, if the
[error] token appears only at the end of a production, whose semantic
action raises an exception) then the simplified strategy should be
preferred. (This includes the case where the [error] token does not
appear at all in the grammar.)
- If the [error] token is used to recover after an error, or if
perfect backward compatibility is required, the legacy strategy
should be selected.
More details on strategies appear in the file [Engine.ml]. *)typestrategy=[`Legacy|`Simplified](**[resume] allows the user to resume the parser after it has suspended
itself with a checkpoint of the form [Shifting _], [AboutToReduce _], or
[HandlingError _]. [resume] expects the old checkpoint and produces a
new checkpoint. It does not raise any exception. *)valresume:?strategy:strategy->'acheckpoint->'acheckpoint(**A token supplier is a function of no arguments which delivers a new token
(together with its start and end positions) every time it is called. *)typesupplier=unit->token*position*position(**A pair of a lexer and a lexing buffer can be turned into a supplier. *)vallexer_lexbuf_to_supplier:(Lexing.lexbuf->token)->Lexing.lexbuf->supplier(**The functions {!offer} and {!resume} are sufficient to write a parser loop.
One can imagine many variations (which is why we expose these functions
in the first place!). Here, we expose a few variations of the main loop,
ready for use. *)(**[loop supplier checkpoint] begins parsing from [checkpoint], reading
tokens from [supplier]. It continues parsing until it reaches a
checkpoint of the form [Accepted v] or [Rejected]. In the former case, it
returns [v]. In the latter case, it raises the exception [Error].
The optional argument [strategy], whose default value is [Legacy],
is passed to {!resume} and influences the error-handling strategy. *)valloop:?strategy:strategy->supplier->'acheckpoint->'a(**[loop_handle succeed fail supplier checkpoint] begins parsing from
[checkpoint], reading tokens from [supplier]. It continues parsing until
it reaches a checkpoint of the form [Accepted v] or [HandlingError env]
(or [Rejected], but that should not happen, as [HandlingError _] will be
observed first). In the former case, it calls [succeed v]. In the latter
case, it calls [fail] with this checkpoint. It cannot raise [Error].
This means that Menhir's error-handling procedure does not get a chance
to run. For this reason, there is no [strategy] parameter. Instead, the
user can implement her own error handling code, in the [fail]
continuation. *)valloop_handle:('a->'answer)->('acheckpoint->'answer)->supplier->'acheckpoint->'answer(**[loop_handle_undo] is analogous to [loop_handle], except it passes a pair
of checkpoints to the failure continuation.
The first (and oldest) checkpoint is the last [InputNeeded] checkpoint that
was encountered before the error was detected. The second (and newest)
checkpoint is where the error was detected, as in [loop_handle]. Going back
to the first checkpoint can be thought of as undoing any reductions that
were performed after seeing the problematic token. (These reductions must
be default reductions or spurious reductions.)
[loop_handle_undo] must initially be applied to an [InputNeeded] checkpoint.
The parser's initial checkpoints satisfy this constraint. *)valloop_handle_undo:('a->'answer)->('acheckpoint->'acheckpoint->'answer)->supplier->'acheckpoint->'answer(**[shifts checkpoint] assumes that [checkpoint] has been obtained by
submitting a token to the parser. It runs the parser from [checkpoint],
through an arbitrary number of reductions, until the parser either
accepts this token (i.e., shifts) or rejects it (i.e., signals an error).
If the parser decides to shift, then [Some env] is returned, where [env]
is the parser's state just before shifting. Otherwise, [None] is
returned.
It is desirable that the semantic actions be side-effect free, or that
their side-effects be harmless (replayable). *)valshifts:'acheckpoint->'aenvoption(**The function [acceptable] allows testing, after an error has been
detected, which tokens would have been accepted at this point. It is
implemented using [shifts]. Its argument should be an [InputNeeded]
checkpoint.
For completeness, one must undo any spurious reductions before carrying out
this test -- that is, one must apply [acceptable] to the FIRST checkpoint
that is passed by [loop_handle_undo] to its failure continuation.
This test causes some semantic actions to be run! The semantic actions
should be side-effect free, or their side-effects should be harmless.
The position [pos] is used as the start and end positions of the
hypothetical token, and may be picked up by the semantic actions. We
suggest using the position where the error was detected. *)valacceptable:'acheckpoint->token->position->bool(**The abstract type ['a lr1state] describes the non-initial states of the
LR(1) automaton. The index ['a] represents the type of the semantic value
associated with this state's incoming symbol. *)type'alr1state(**The states of the LR(1) automaton are numbered (from 0 and up). *)valnumber:_lr1state->int(* Productions are numbered. *)(**[production_index] maps a production to its integer index. *)valproduction_index:production->int(**[find_production] maps a production index to a production.
Its argument must be a valid index; use with care. *)valfind_production:int->production(**An element is a pair of a non-initial state [s] and a semantic value [v]
associated with the incoming symbol of this state. The idea is, the value
[v] was pushed onto the stack just before the state [s] was entered. Thus,
for some type ['a], the state [s] has type ['a lr1state] and the value [v]
has type ['a]. In other words, the type [element] is an existential type. *)typeelement=|Element:'alr1state*'a*position*position->element(**The parser's stack is (or, more precisely, can be viewed as) a stream of
elements. The functions {!top} and {!pop} offer access to this stream. *)(**[top env] returns the parser's top stack element. The state contained in
this stack element is the current state of the automaton. If the stack is
empty, [None] is returned. In that case, the current state of the
automaton must be an initial state. *)valtop:'aenv->elementoption(**[pop_many i env] pops [i] cells off the automaton's stack. This is done
via [i] successive invocations of [pop]. Thus, [pop_many 1] is [pop]. The
index [i] must be nonnegative. The time complexity is O(i). *)valpop_many:int->'aenv->'aenvoption(**[get i env] returns the parser's [i]-th stack element. The index [i] is
0-based: thus, [get 0] is [top]. If [i] is greater than or equal to the
number of elements in the stack, [None] is returned. The time complexity
is O(i). *)valget:int->'aenv->elementoption(**[current_state_number env] is (the integer number of) the automaton's
current state. This works even if the automaton's stack is empty, in
which case the current state is an initial state. This number can be
passed as an argument to a [message] function generated by [menhir
--compile-errors]. *)valcurrent_state_number:'aenv->int(**[equal env1 env2] tells whether the parser configurations [env1] and
[env2] are equal in the sense that the automaton's current state is the
same in [env1] and [env2] and the stack is *physically* the same in
[env1] and [env2]. If [equal env1 env2] is [true], then the sequence of
the stack elements, as observed via {!pop} and {!top}, must be the same in
[env1] and [env2]. Also, if [equal env1 env2] holds, then the checkpoints
[input_needed env1] and [input_needed env2] must be equivalent. The
function [equal] has time complexity O(1). *)valequal:'aenv->'aenv->bool(**[positions env] returns the start and end positions of the current
lookahead token. In an initial state, a pair of twice the initial
position is returned. *)valpositions:'aenv->position*position(**When applied to an environment taken from a checkpoint of the form
[AboutToReduce (env, prod)], the function [env_has_default_reduction]
tells whether the reduction that is about to take place is a default
reduction. *)valenv_has_default_reduction:'aenv->bool(**[state_has_default_reduction s] tells whether the state [s] has a default
reduction. This includes the case where [s] is an accepting state. *)valstate_has_default_reduction:_lr1state->bool(**[pop env] returns a new environment, where the parser's top stack cell
has been popped off. (If the stack is empty, [None] is returned.) This
amounts to pretending that the (terminal or nonterminal) symbol that
corresponds to this stack cell has not been read. *)valpop:'aenv->'aenvoption(**[force_reduction prod env] should be called only if in the state [env]
the parser is capable of reducing the production [prod]. If this
condition is satisfied, then this production is reduced, which means that
its semantic action is executed (this can have side effects!) and the
automaton makes a goto (nonterminal) transition. If this condition is not
satisfied, [Invalid_argument _] is raised. *)valforce_reduction:production->'aenv->'aenv(**[input_needed env] returns [InputNeeded env]. That is, out of an [env]
that might have been obtained via a series of calls to the functions
[pop], [force_reduction], [feed], etc., it produces a checkpoint, which
can be used to resume normal parsing, by supplying this checkpoint as an
argument to [offer].
This function should be used with some care. It could "mess up the
lookahead" in the sense that it allows parsing to resume in an arbitrary
state [s] with an arbitrary lookahead symbol [t], even though Menhir's
reachability analysis (menhir --list-errors) might well think that it is
impossible to reach this particular configuration. If one is using
Menhir's new error reporting facility, this could cause the parser to
reach an error state for which no error message has been prepared. *)valinput_needed:'aenv->'acheckpointend(**This signature is a fragment of the inspection API that is made available
to the user when [--inspection] is used. This fragment contains type
definitions for symbols. *)moduletypeSYMBOLS=sig(**The type ['a terminal] represents a terminal symbol. Its parameter ['a]
represents the type of the semantic values associated with this symbol.
The concrete definitions of this type is generated. *)type'aterminal(**The type ['a nonterminal] represents a nonterminal symbol. Its parameter
['a] represents the type of the semantic values associated with this
symbol. The concrete definitions of this type is generated. *)type'anonterminal(**The type ['a symbol] represents a terminal or nonterminal symbol. It is
the disjoint union of the types ['a terminal] and ['a nonterminal]. *)type'asymbol=|T:'aterminal->'asymbol|N:'anonterminal->'asymbol(**The type [xsymbol] is an existentially quantified version of the type ['a
symbol]. This type is useful in situations where ['a] is not statically
known. *)typexsymbol=|X:'asymbol->xsymbolend(**This signature describes the inspection API that is made available to the
user when [--inspection] is used. *)moduletypeINSPECTION=sig(* The types of symbols are described above. *)includeSYMBOLS(**The type ['a lr1state] is meant to be the same as in {!INCREMENTAL_ENGINE}. *)type'alr1state(**The type [production] is meant to be the same as in {!INCREMENTAL_ENGINE}.
It represents a production of the grammar. A production can be examined
via the functions {!lhs} and {!rhs} below. *)typeproduction(**An LR(0) item is a pair of a production [prod] and a valid index [i] into
this production. That is, if the length of [rhs prod] is [n], then [i] is
comprised between 0 and [n], inclusive. *)typeitem=production*int(** The following are total ordering functions. *)valcompare_terminals:_terminal->_terminal->intvalcompare_nonterminals:_nonterminal->_nonterminal->intvalcompare_symbols:xsymbol->xsymbol->intvalcompare_productions:production->production->intvalcompare_items:item->item->int(**[incoming_symbol s] is the incoming symbol of the state [s], that is,
the symbol that the parser must recognize before (has recognized when)
it enters the state [s]. This function gives access to the semantic
value [v] stored in a stack element [Element (s, v, _, _)]. Indeed,
by case analysis on the symbol [incoming_symbol s], one discovers the
type ['a] of the value [v]. *)valincoming_symbol:'alr1state->'asymbol(**[items s] is the set of the LR(0) items in the LR(0) core of the LR(1)
state [s]. This set is not epsilon-closed. This set is presented as a
list, in an arbitrary order. *)valitems:_lr1state->itemlist(**[lhs prod] is the left-hand side of the production [prod]. This is
always a non-terminal symbol. *)vallhs:production->xsymbol(**[rhs prod] is the right-hand side of the production [prod]. This is
a (possibly empty) sequence of (terminal or nonterminal) symbols. *)valrhs:production->xsymbollist(**[nullable nt] tells whether the non-terminal symbol [nt] is nullable.
That is, it is true if and only if this symbol produces the empty
word [epsilon]. *)valnullable:_nonterminal->bool(**[first nt t] tells whether the FIRST set of the nonterminal symbol [nt]
contains the terminal symbol [t]. That is, it is true if and only if
[nt] produces a word that begins with [t]. *)valfirst:_nonterminal->_terminal->bool(**[xfirst] is analogous to [first], but expects a first argument of type
[xsymbol] instead of [_ terminal]. *)valxfirst:xsymbol->_terminal->bool(**[foreach_terminal] enumerates the terminal symbols, including [error]. *)valforeach_terminal:(xsymbol->'a->'a)->'a->'a(**[foreach_terminal_but_error] enumerates the terminal symbols, excluding
[error]. *)valforeach_terminal_but_error:(xsymbol->'a->'a)->'a->'a(**The type [env] is meant to be the same as in {!INCREMENTAL_ENGINE}. *)type'aenv(**[feed symbol startp semv endp env] causes the parser to consume the
(terminal or nonterminal) symbol [symbol], accompanied with the semantic
value [semv] and with the start and end positions [startp] and [endp].
Thus, the automaton makes a transition, and reaches a new state. The
stack grows by one cell. This operation is permitted only if the current
state (as determined by [env]) has an outgoing transition labeled with
[symbol]. Otherwise, [Invalid_argument _] is raised. *)valfeed:'asymbol->position->'a->position->'benv->'benvend(**This signature combines the incremental API and the inspection API. *)moduletypeEVERYTHING=sigincludeINCREMENTAL_ENGINEincludeINSPECTIONwithtype'alr1state:='alr1statewithtypeproduction:=productionwithtype'aenv:='aenvend