org.jaitools.jiffle.parser
Class SymbolScopeStack

java.lang.Object
  extended by org.jaitools.jiffle.parser.SymbolScopeStack

public class SymbolScopeStack
extends java.lang.Object

Used in the Jiffle tree parser grammars to track symbols defined at different scopes in a script. The name of this class comes from the ANTLR scope stack mechanism on which it is loosely based.

Jiffle's scoping rules are almost identical to Java. At the top level of a script the accessible variables are those declared in an init (image-scope variables) plus any declared in the script outside other blocks. Within a block, the accessible variables are those at the top level plus any additional variables defined in the block itself. The latter go out of scope when leaving the block. A foreach loop variable's scope is the statement or block associated with the loop.

Since:
0.1
Version:
$Id: SymbolScopeStack.java 1653 2011-06-16 10:23:02Z michael.bedward $
Author:
Michael Bedward

Constructor Summary
SymbolScopeStack()
          Creates a new scope stack.
 
Method Summary
 void addLevel()
          Pushes a new scope level onto the stack.
 void addLevel(java.lang.String label)
          Pushes a new scope level onto the stack.
 void addSymbol(java.lang.String name, SymbolType type, ScopeType scopeType)
          Adds a new symbol to the current scope level.
 SymbolScope dropLevel()
          Drops (ie.
 Symbol findSymbol(java.lang.String name)
          Gets the first symbol found with the given name.
 boolean isDefined(java.lang.String name)
          Tests if a symbol with the given name is defined within the top scope or any enclosing scopes.
 boolean isDefined(java.lang.String name, ScopeType scopeType)
          Tests if a symbol with the given name and scope type is defined within the top scope or any enclosing scopes.
 boolean isDefined(java.lang.String name, SymbolType type)
          Tests if a symbol with the given name and type is defined within the top scope or any enclosing scopes.
 boolean isEmpty()
          Tests if there are any scope levels.
 int size()
          Gets the number of scope levels.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SymbolScopeStack

public SymbolScopeStack()
Creates a new scope stack.

Method Detail

addLevel

public void addLevel()
Pushes a new scope level onto the stack. The level will be given a default label.


addLevel

public void addLevel(java.lang.String label)
Pushes a new scope level onto the stack.

Parameters:
label - label for the new scope

dropLevel

public SymbolScope dropLevel()
Drops (ie. pops) the top level from the stack.

Returns:
the level just dropped or null if the stack was empty

addSymbol

public void addSymbol(java.lang.String name,
                      SymbolType type,
                      ScopeType scopeType)
Adds a new symbol to the current scope level.

Parameters:
name - symbol name
type - symbol type
scopeType - symbol scope
Throws:
java.lang.IllegalArgumentException - if no scope levels have been added

isEmpty

public boolean isEmpty()
Tests if there are any scope levels.

Returns:
true if the stack is empty; false otherwise

size

public int size()
Gets the number of scope levels.

Returns:
number of levels

isDefined

public boolean isDefined(java.lang.String name)
Tests if a symbol with the given name is defined within the top scope or any enclosing scopes.

Parameters:
name - symbol name
Returns:
true if the symbol is found; false otherwise

isDefined

public boolean isDefined(java.lang.String name,
                         SymbolType type)
Tests if a symbol with the given name and type is defined within the top scope or any enclosing scopes.

Parameters:
name - symbol name
type - symbol type
Returns:
true if the symbol is found; false otherwise

isDefined

public boolean isDefined(java.lang.String name,
                         ScopeType scopeType)
Tests if a symbol with the given name and scope type is defined within the top scope or any enclosing scopes.

Parameters:
name - symbol name
scopeType - the symbol scope
Returns:
true if the symbol is found; false otherwise

findSymbol

public Symbol findSymbol(java.lang.String name)
Gets the first symbol found with the given name. The search begins at the top scope and moves through any enclosing scopes.

Parameters:
name - symbol name
Returns:
the symbol or null if there was no match


Copyright © 2011. All Rights Reserved.