org.parboiled.support
Class Var<T>

java.lang.Object
  extended by org.parboiled.common.Reference<T>
      extended by org.parboiled.support.Var<T>
Type Parameters:
T - the type wrapped by this Var
Direct Known Subclasses:
StringVar

public class Var<T>
extends Reference<T>

This class provides a "local variable"-like construct for action expressions in parser rule methods. Var objects wrap an internal value of an arbitrary (reference) type, can have an initial value, allow read/write access to their values and can be passed around as parameters to nested rule methods. Each rule invocation (i.e. rule matching attempt) receives its own Var scope (which is automatically initialized with the initial value), so actions in recursive rules work just like expected.

Var objects generally behave just like local variables with one exception:
When rule method A() passes a Var defined in its scope to another rule method B() as a parameter and an action in rule method B() writes to this Var all actions in rule method A() running after B() will "see" this newly written value (since values in Var objects are passed by reference)


Constructor Summary
Var()
          Initializes a new Var with a null initial value.
Var(Factory<T> initialValueFactory)
          Initializes a new Var.
Var(T value)
          Initializes a new Var with the given initial value.
 
Method Summary
 boolean enterFrame()
          Provides a new frame for the variable.
 boolean exitFrame()
          Exits a frame previously entered with enterFrame().
 int getLevel()
          Returns the current frame level of this variable, the very first level corresponding to zero.
 java.lang.String getName()
          Gets the name of this Var.
 void setName(java.lang.String name)
          Sets the name of this Var.
 java.lang.String toString()
           
 
Methods inherited from class org.parboiled.common.Reference
clear, get, getAndClear, getAndSet, isNotSet, isSet, set, setAndGet
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Var

public Var()
Initializes a new Var with a null initial value.


Var

public Var(T value)
Initializes a new Var with the given initial value.

Parameters:
value - the value

Var

public Var(Factory<T> initialValueFactory)
Initializes a new Var. The given factory will be used to create the initial value for each "execution frame" of the enclosing rule.

Parameters:
initialValueFactory - the factory used to create the initial value for a rule execution frame
Method Detail

getName

public java.lang.String getName()
Gets the name of this Var.

Returns:
the name

setName

public void setName(java.lang.String name)
Sets the name of this Var.

Parameters:
name - the name

getLevel

public int getLevel()
Returns the current frame level of this variable, the very first level corresponding to zero.

Returns:
the current level

enterFrame

public boolean enterFrame()
Provides a new frame for the variable. Potentially existing previous frames are saved. Normally you do not have to call this method manually as parboiled provides for automatic Var frame management.

Returns:
true

exitFrame

public boolean exitFrame()
Exits a frame previously entered with enterFrame(). Normally you do not have to call this method manually as parboiled provides for automatic Var frame management.

Returns:
true

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object