V
- Type of value stored in dictionary.public class ScopedSymbolDictionary<V> extends ScopedDictionary<java.lang.String,V>
String
representation and a user-
defined object that can store arbitrary user data. As a result of the
lookup process, if a symbol is found, the user-defined object is returned.
When symbols are added, they are stored in a flat list called a scope. Each
scope can store a set of symbols where each symbol name must be unique.
The user has control over how many scopes exist. By default, no scopes
are available. The user of this class is responsible for adding scopes
using the addScope
method. When the symbols stored in the
current scope should no longer be available, the scope can be removed
using the deleteScope
method. This multilevel scope
approach can be thought of as a stack of scopes where the current scope
is the top of the stack and the bottom of the stack has a special scope
that is considered "global". Symbols can be added to the current scope
or to the global scope using the addSymbol
method.
Since the global scope has a special meaning, a convenience constructor is provided that automatically creates the global scope.
Symbols must only be unique within a single scope. If the same symbol
exists in multiple scopes, the instance found closest to the current
scope (top of stack) is the one that is found on lookup. Likewise, when
a symbol is deleted using deleteSymbol
, the topmost symbol
is the one removed.
When instantiating objects of this class, one can chose between case-
sensitive and case-insensitive operations. Only the symbol names can be
case-(in)sensitive. Once instantiated, the mode of operation cannot be
changed. Case sensitivity affects addSymbol
,
deleteSymbol
and lookupSymbol
methods.
This class is designed to provide a symbol lookup facility such as a compiler would need to properly reference scoped variables or functions. Most functionality is delegated to the superclass.
ScopedDictionary.Node<K,V>
Modifier and Type | Field and Description |
---|---|
private boolean |
caseSensitive
Defines case sensitivity mode for lookups
|
Constructor and Description |
---|
ScopedSymbolDictionary()
Default constructor.
|
ScopedSymbolDictionary(boolean caseSensitive)
Constructor with explicit case-sensitivity requested.
|
ScopedSymbolDictionary(java.lang.Object extra)
Convenience constructor that automatically creates the global scope.
|
ScopedSymbolDictionary(java.lang.Object extra,
boolean caseSensitive)
Convenience constructor that automatically creates the global scope
with explicit case-sensitivity requested.
|
Modifier and Type | Method and Description |
---|---|
boolean |
addSymbol(boolean global,
java.lang.String name,
V value)
Adds a symbol to the current or global scope or replaces
its user-defined object.
|
void |
copyAll(ScopedSymbolDictionary<V> source,
boolean keepGlobal)
Clear this dictionary and copy all content from the source.
|
boolean |
deleteSymbol(java.lang.String name)
Searches all scopes from the top of the stack down and deletes
the first found occurrence of the symbol.
|
java.lang.Object |
getSymbolAtScope(java.lang.String name,
int scope)
Gets the value of the given named symbol in the scope specified.
|
boolean |
isCaseSensitive()
Reports if this dictionary is case-sensitive.
|
int |
locateSymbol(java.lang.String name)
Searches all scopes from the top of the stack down for a given symbol.
|
int |
locateSymbolValue(java.lang.String key,
V value)
Searches all scopes from the top of the stack down for the specified
key and value pair and returns 0-based index of the depth where
that pair is found.
|
java.lang.Object |
lookupSymbol(java.lang.String name)
Searches all scopes from the top of the stack down for a given symbol.
|
java.lang.String |
processKey(java.lang.String key)
Process the key, which is expected to be a string, to take the case
sensitivity setting into account.
|
boolean |
setSymbolAtScope(java.lang.String name,
int scope,
V value)
Sets the given named symbol in the scope specified, to the given
value.
|
java.util.Set<java.lang.String> |
symbolSet()
Returns a set containing all defined symbol names as strings.
|
java.util.Set<java.lang.String> |
symbolSet(int scope)
Returns a set containing all defined symbol names as strings, in the scope specified.
|
addEntry, addEntryAt, addScope, apply, clear, copyAll, deleteEntry, deleteScope, deleteScope, dump, dump, dump, dumpCurrentScope, dumpCurrentScope, entrySet, entrySet, getDictionaryAtScope, getScope, getScopeAt, getValueAtScope, keySet, keySet, locate, locate, lookup, lookup, removeEntryAtScope, removeEntryThroughScope, reverseLookup, reverseLookup, setScope, setScopeAt, setValueAtScope, size, values, values
public ScopedSymbolDictionary()
public ScopedSymbolDictionary(boolean caseSensitive)
caseSensitive
- true
makes all symbol lookups case-sensitive.public ScopedSymbolDictionary(java.lang.Object extra)
extra
- Object to be associated with the global scope.public ScopedSymbolDictionary(java.lang.Object extra, boolean caseSensitive)
extra
- Object to be associated with the global scope.caseSensitive
- true
makes all symbol lookups case-sensitive.public boolean isCaseSensitive()
true
if this dictionary is case-sensitive.public boolean addSymbol(boolean global, java.lang.String name, V value)
global
- Specifies the target scope. false
specifies the
current scope, and true
specifies the global
scope.name
- The symbol name.value
- The symbol value which may be any object.true
if the symbol has been added,
false
if its value has been replaced.public boolean deleteSymbol(java.lang.String name)
name
- The symbol name.true
if the symbol has been deleted.public int locateSymbolValue(java.lang.String key, V value)
key
- The symbol name.value
- The value to search.public boolean setSymbolAtScope(java.lang.String name, int scope, V value)
name
- The symbol name.scope
- The depth (in number of scopes from the top of the stack) at
which the value must be set.value
- The replacement value.true
if the value was successfully set, or
false
if any error occurred.public java.lang.Object getSymbolAtScope(java.lang.String name, int scope)
name
- The symbol name.scope
- The depth (in number of scopes from the top of the stack) at
which the value must be set. Use -1 to reference the global
scope.null
if no such name exists.public java.lang.Object lookupSymbol(java.lang.String name)
name
- The symbol name.null
if not found.public int locateSymbol(java.lang.String name)
name
- The symbol name.public java.util.Set<java.lang.String> symbolSet()
public java.util.Set<java.lang.String> symbolSet(int scope)
scope
- The depth (in number of scopes from the top of the stack) from which the symbols
are collected. Use -1 to reference the global scope.public java.lang.String processKey(java.lang.String key)
processKey
in class ScopedDictionary<java.lang.String,V>
key
- The lookup key, assumed to be a string.public void copyAll(ScopedSymbolDictionary<V> source, boolean keepGlobal)
source
- The source dictionary.keepGlobal
- When true
, the global dictionary is exposed directly to this copy;
otherwise is duplicated.