public class SymbolTableCompiler extends Object implements Compilable, SymbolTable
SymbolTableCompiler collects symbols represented as
String instances and compiles them into a SymbolTable. At any point after adding symbols, the symbol table
can be written to a data output stream. After being written, the
symbol table compiler properly implements the SymbolTable
interface through the result of compilation. A static method,
read, is provided to read a symbol table from a data
input stream. It works as a symbol table after a call to compileTo(ObjectOutput).UNKNOWN_SYMBOL_ID| Constructor and Description |
|---|
SymbolTableCompiler()
Construct a fresh symbol table compiler.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
addSymbol(String symbol)
Add a symbol to the symbol table to be compiled.
|
static SymbolTable |
asSymbolTable(String[] symbols)
Returns a compiled symbol table constructed from the specified
list of symbols with symbol identifiers determined by array
position.
|
void |
clear()
Throws an unsupported operation exception.
|
void |
compileTo(ObjectOutput objOut)
Compiles the symbol table to the specified object output.
|
int |
getOrAddSymbol(String symbol)
Throws an unsupported operation exception.
|
String |
idToSymbol(int id)
Return the symbol corresponding to the specified identifier.
|
int |
numSymbols()
Returns the number of symbols in this symbol table.
|
int |
removeSymbol(String symbol)
Throws an unsupported operation exception.
|
String[] |
symbols()
Returns the symbols in this symbol table.
|
int |
symbolToID(String symbol)
Return the identifier corresponding to the specified symbol, or
-1 if the symbol does not exist. |
String |
toString()
Returns a string-based representation of this
symbol table.
|
public SymbolTableCompiler()
public static SymbolTable asSymbolTable(String[] symbols)
The array of symbols is copied so that subsequent changes to the symbol array will not affect the returned symbol table.
The returned symbol table may be serialized.
symbols - Array of symbols to back the symbol table.IllegalArgumentException - If there are duplicate symbols in
the array.public void compileTo(ObjectOutput objOut) throws IOException
compileTo in interface CompilableobjOut - Object output to which symbol table is written.IOException - If there is an underlying I/O error.public int symbolToID(String symbol)
-1 if the symbol does not exist. This method
should only be called after compilation; calling it before will
throw an illegal state exception.symbolToID in interface SymbolTablesymbol - Symbol whose identifier is returned.-1 if the symbol does not exist.IllegalStateException - If this method is called before
compilation.public String[] symbols()
public String idToSymbol(int id)
0 and the
number of symbols in the table minus one, inclusive. Raises an
index out of bounds exception for identifiers out of range.
This method should only be called after compilation; calling it
before will throw an illegal state exception.idToSymbol in interface SymbolTableid - Identifier whose symbol is returned.IndexOutOfBoundsException - If there is no symbol for the
specified identifier.IllegalStateException - If this method is called before
compilation.public int numSymbols()
numSymbols in interface SymbolTableIllegalStateException - If this method is called before
compilation.public void clear()
clear in interface SymbolTableUnsupportedOperationException - Always.public int getOrAddSymbol(String symbol)
getOrAddSymbol in interface SymbolTablesymbol - Symbol is ignored.UnsupportedOperationException - Always.public boolean addSymbol(String symbol)
Short.MAX_VALUE;
attempts to add longer symbols will throw an
IllegalArgumentException.symbol - Symbol to add.true if the symbol was not already in the
table.IllegalArgumentException - If the symbol is longer than
Short.MAX_VALUE.public int removeSymbol(String symbol)
removeSymbol in interface SymbolTablesymbol - Symbol is ignored.UnsupportedOperationException - Always.Copyright © 2019 Alias-i, Inc.. All rights reserved.