Commit 12034bee by POTTIER Francois

Publish the new functions [pop], [force_reduction], [input_needed],

and [find_default_reduction] in [INCREMENTAL_ENGINE].
parent c5a0c6f6
......@@ -653,6 +653,12 @@ module Make (T : TABLE) = struct
type 'a lr1state =
state
let find_default_reduction (state : _ lr1state) : production option =
T.default_reduction state
(fun () prod -> Some prod)
(fun () -> None)
()
(* --------------------------------------------------------------------------- *)
(* Stack inspection. *)
......@@ -727,12 +733,10 @@ module Make (T : TABLE) = struct
(* --------------------------------------------------------------------------- *)
(* TEMPORARY comment *)
(* TEMPORARY add calls to new [Log] functions? : log [pop], [feed], [force] *)
(* TEMPORARY potential danger:
- messing up the lookahead *)
let _pop (env : env) : env option =
let pop (env : env) : env option =
let cell = env.stack in
let next = cell.next in
if next == cell then
......@@ -744,11 +748,7 @@ module Make (T : TABLE) = struct
- attempting to take a transition that does not exist
(checked at runtime; raises Invalid_argument)
- supplying a semantic value of incorrect type (statically checked
by correlating 'a nonterminal with 'a)
- messing up the lookahead (i.e. moving to a state where the lookahead
symbol cannot be [t], yet is [t]) (or moving to a state where we
we should not ask for one more symbol, yet constructing [InputNeeded])
-- NOT PREVENTED *)
by correlating 'a nonterminal with 'a) *)
let _feed_nonterminal nt startp semv endp (env : env) : env =
let source : state = env.current in
......@@ -792,7 +792,7 @@ module Make (T : TABLE) = struct
by calling [run env] or [initiate env]. Instead, it returns [env] to the
user, or raises [Error]. *)
let _force_reduction prod (env : env) : env =
let force_reduction prod (env : env) : env =
(* Check if this reduction is permitted. This check is REALLY important.
The stack must have the correct shape: that is, it must be sufficiently
high, and must contain semantic values of appropriate types, otherwise
......@@ -810,13 +810,11 @@ module Make (T : TABLE) = struct
{ env with stack; current }
end
let _has_default_reduction (state : _ lr1state) : production option =
T.default_reduction state
(fun () prod -> Some prod)
(fun () -> None)
()
(* TEMPORARY potential danger:
- messing up the lookahead (i.e. moving to a state where the lookahead
symbol cannot be [t], yet is [t]) (or moving to a state where we
we should not ask for one more symbol, yet constructing [InputNeeded])
-- NOT PREVENTED
- violates the invariant that an input token is normally demanded only
in a state [s] whose incoming symbol is a terminal symbol
and which does not have a default reduction on [#]
......@@ -824,7 +822,7 @@ module Make (T : TABLE) = struct
(not really problematic? but worth noting)
- for type safety, should correlate 'a env with 'a checkpoint
*)
let _input_needed (env : env) : 'a checkpoint =
let input_needed (env : env) : 'a checkpoint =
InputNeeded env
end
......@@ -170,6 +170,38 @@ module type INCREMENTAL_ENGINE = sig
val acceptable: 'a checkpoint -> token -> Lexing.position -> 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. *)
val pop: env -> env option
(* [force_reduction prod env] should be called only if in the current state
(as determined by [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. *)
val force_reduction: production -> env -> env
(* [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. *)
val input_needed: env -> 'a checkpoint
(* 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. *)
......@@ -180,6 +212,11 @@ module type INCREMENTAL_ENGINE = sig
val number: _ lr1state -> int
(* [find_default_reduction s] returns [Some prod] if the state [s] has a
default reduction of production [prod], and returns [None] otherwise. *)
val find_default_reduction: _ lr1state -> production option
(* 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,
......@@ -343,4 +380,3 @@ module type EVERYTHING = sig
with type production := production
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment