Class State

java.lang.Object
eu.bandm.tools.ramus.ogma.State
All Implemented Interfaces:
Environment

public class State extends Object implements Environment
Immutable semantic parsing state for AST construction.

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 Classes
    Modifier and Type
    Class
    Description
    static interface 
    Functional interface for projections that extract data from a state.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Data elem)
    Returns a variant of this state with the given value appended as an element to the list accumulator value.
    addTo(Object key, Data elem)
    Returns a variant of this state with the given value appended as an element to the list associated with the given key.
    Returns a projection that extracts the value assigned to the given key from the table of a state.
    get(Object key)
    Returns the value associated with the given key in the table.
    int
    Returns the number of stacked scopes.
    Returns the key–value table of this state.
    Returns the accumulator value of this state.
    static State
    Returns an initial state.
    map(Function<? super Data,? extends Data> op)
    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> A
    Returns the extracted result of the given syntax–semantics pair.
    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.
    removeAll(Object... keys)
    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.
    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.
    withEntry(Object key, Data value)
    Returns a variant of this state that maps the given key to the given value in the table.
    withValue(Data value)
    Returns a new state with the given accumulator value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • initial

      public static State 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

      public State withValue(Data value)
      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 - if value is null
    • map

      public State map(Function<? super Data,? extends Data> op)
      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 - if op is null
    • withEntry

      public State withEntry(Object key, Data value)
      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 map
      value - the new value
      Returns:
      a variant of this state where that maps key to value
      Throws:
      NullPointerException - if key or value is null
    • withEntries

      public 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.

      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 entries to the corresponding value
      Throws:
      NullPointerException - if entries is or contains null
    • push

      public State 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

      public State 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

      public Map<Object,Data> 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

      public Data getValue()
      Returns the accumulator value of this state.
      Specified by:
      getValue in interface Environment
      Returns:
      the accumulator value of this state
    • addTo

      public State addTo(Object key, Data elem)
      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 key
      elem - the element value to be appended
      Returns:
      a variant of this state with elem appended as an element to the list associated with key
      Throws:
      NullPointerException - if key or elem is null
    • add

      public State add(Data elem)
      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 elem appended as an element to the list in the accumulator
      Throws:
      NullPointerException - if elem is null
    • remove

      public State remove(Object key)
      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 key removed from the table
      Throws:
      NullPointerException - if key is null
    • removeAll

      public State removeAll(Object... keys)
      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 keys removed from the table
      Throws:
      NullPointerException - if keys is or contains null
    • removeAll

      public State removeAll(Collection<? extends Object> keys)
      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 keys removed from the table
      Throws:
      NullPointerException - if keys is or contains null
    • get

      public @Opt Data get(Object key)
      Returns the value associated with the given key in the table.
      Specified by:
      get in interface Environment
      Parameters:
      key - the key
      Returns:
      the value associated with key in the table, or null if there is no entry for key
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • assigned

      public static State.Projection assigned(Object key)
      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 key from its table
      Throws:
      NullPointerException - if key is null
      See Also:
    • value

      public static State.Projection 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

      public <A> A project(AbstractExpression<?,?,A> expr)
      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 - if expr is null