Class State
- All Implemented Interfaces:
Environment
Every state holds the following elements:
- An accumulator value. At the successful end of the semantic actions of a parser, the accumulator holds the resulting AST.
- A table of key–value pairs that can be used as local variables.
The table contents are scoped; they can be saved and restored dynamically. By contrast, the accumulator is unscoped.
States are persistent data structures: Instead of performing a local modification of a state object, a mutated copy is created and the original is retained. This property is essential for safe and performant backtracking.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for projections that extract data from a state. -
Method Summary
Modifier and TypeMethodDescriptionReturns a variant of this state with the given value appended as an element to the list accumulator value.Returns a variant of this state with the given value appended as an element to the list associated with the given key.static State.ProjectionReturns a projection that extracts the value assigned to the given key from the table of a state.Returns the value associated with the given key in the table.intReturns the number of stacked scopes.getTable()Returns the key–value table of this state.getValue()Returns the accumulator value of this state.static Stateinitial()Returns an initial state.Returns a new state with the accumulator value transformed by a given function.pop()Returns a variant of this state where a nested scope has been left and discarded.<A> Aproject(AbstractExpression<?, ?, A> expr) Returns the extracted result of the given syntax–semantics pair.push()Returns a variant of this state where a new scope has been entered.Returns a variant of this state with the given key removed from the table.Returns a variant of this state with the given keys removed from the table.removeAll(Collection<? extends Object> keys) Returns a variant of this state with the given keys removed from the table.toString()static State.Projectionvalue()Returns a projection that extracts the accumulator value from the table of a state.withEntries(Map<?, ? extends Data> entries) Returns a variant of this state that maps the given keys to the given values in the table simultaneously.Returns a variant of this state that maps the given key to the given value in the table.Returns a new state with the given accumulator value.
-
Method Details
-
initial
Returns an initial state.The key–value table of the resulting state is empty, and the accumulator holds the atomic value
Unit.unit.- Returns:
- an initial state
-
withValue
Returns a new state with the given accumulator value.The key–value table is unchanged.
- Parameters:
value- the new accumulator value- Returns:
- a state with the same key–value table as this state, but the
accumulator value
value - Throws:
NullPointerException- ifvalueis null
-
map
Returns a new state with the accumulator value transformed by a given function.The key–value table is unchanged.
- Parameters:
op- the function to transform the accumulator value with- Returns:
- a state with the same key–value table as this state, but the
accumulator value transformed with
op - Throws:
NullPointerException- ifopis null
-
withEntry
Returns a variant of this state that maps the given key to the given value in the table.Any previous value associated with the given key is overwritten. Entries are scoped and can be undone with
pop().The accumulator value is unchanged.
- Parameters:
key- the key to mapvalue- the new value- Returns:
- a variant of this state where that maps
keytovalue - Throws:
NullPointerException- ifkeyorvalueis null
-
withEntries
Returns a variant of this state that maps the given keys to the given values in the table simultaneously.Any previous value associated with a given key is overwritten. Entries are scoped and can be undone with
pop().The accumulator value is unchanged.
- Parameters:
entries- the new key–value entries- Returns:
- a variant of this state that maps each key of
entriesto the corresponding value - Throws:
NullPointerException- ifentriesis or contains null
-
push
Returns a variant of this state where a new scope has been entered.The accumulator value is unchanged.
- Returns:
- a variant of this state where a new scope has been entered
- See Also:
-
pop
Returns a variant of this state where a nested scope has been left and discarded.All modification of the table that are local to the current scope are undone.
The accumulator value is unchanged.
- Returns:
- a variant of this state where a nested scope has been left and discarded
- Throws:
EmptyStackException- if this state is not nested but top-level- See Also:
-
getStackDepth
public int getStackDepth()Returns the number of stacked scopes.The nonnegative result indicates how many times
pop()can be called successfully. A value of zero indicates that this state is in the outermost scope.- Returns:
- the number of stacked scopes
-
getTable
Returns the key–value table of this state.The resulting map is immutable; neither the caller nor the owning state can change its contents.
- Returns:
- the key–value table of this state
-
getValue
Returns the accumulator value of this state.- Specified by:
getValuein interfaceEnvironment- Returns:
- the accumulator value of this state
-
addTo
Returns a variant of this state with the given value appended as an element to the list associated with the given key.The accumulator value is unchanged.
- Parameters:
key- the keyelem- the element value to be appended- Returns:
- a variant of this state with
elemappended as an element to the list associated withkey - Throws:
NullPointerException- ifkeyorelemis null
-
add
Returns a variant of this state with the given value appended as an element to the list accumulator value.The key–value table is unchanged.
- Parameters:
elem- the element value to be appended- Returns:
- a variant of this state with
elemappended as an element to the list in the accumulator - Throws:
NullPointerException- ifelemis null
-
remove
Returns a variant of this state with the given key removed from the table.Removals are scoped like entries, and can be undone with
pop().The accumulator value is unchanged.
- Parameters:
key- the key to remove- Returns:
- a variant of this state with
keyremoved from the table - Throws:
NullPointerException- ifkeyis null
-
removeAll
Returns a variant of this state with the given keys removed from the table.If a key occurs more than one time, there is no further effect.
Removals are scoped like entries, and can be undone with
pop().The accumulator value is unchanged.
- Parameters:
keys- the keys to remove- Returns:
- a variant of this state with all elements of
keysremoved from the table - Throws:
NullPointerException- ifkeysis or contains null
-
removeAll
Returns a variant of this state with the given keys removed from the table.If a key occurs more than one time, there is no further effect.
Removals are scoped like entries, and can be undone with
pop().The accumulator value is unchanged.
- Parameters:
keys- the keys to remove- Returns:
- a variant of this state with all elements of
keysremoved from the table - Throws:
NullPointerException- ifkeysis or contains null
-
get
Returns the value associated with the given key in the table.- Specified by:
getin interfaceEnvironment- Parameters:
key- the key- Returns:
- the value associated with
keyin the table, ornullif there is no entry forkey
-
toString
-
assigned
Returns a projection that extracts the value assigned to the given key from the table of a state.FIXME: Behavior if key undefined
- Parameters:
key- the key- Returns:
- a projection that, given a state, extracts the value assigned to
keyfrom its table - Throws:
NullPointerException- ifkeyis null- See Also:
-
value
Returns a projection that extracts the accumulator value from the table of a state.- Returns:
- a projection that, given a state, extracts the accumulator value
- See Also:
-
project
Returns the extracted result of the given syntax–semantics pair.This operation assumes that this state is the final state of a run of the parser that is the syntax part of
expr. Behavior is unspecified otherwise.- Type Parameters:
A- the type of parsing results- Parameters:
expr- the syntax–semantics pair- Returns:
- the parsing result, assuming that this state has been obtained by
parsing with
expr - Throws:
NullPointerException- ifexpris null
-