public class Environment
extends java.lang.Object
An instance of the Environment is given as a parameter to constructors of parsers and lexers. This is how they can access this information later in grammar actions.
This class also provides a number of useful shortcuts for method calls. These shortcuts reduce the verbosity of the grammar actions and make the grammar files more readable.
The latest revision of this class maintains the line and column information for the output stream. These can be read and set. This functionality is required for tracking the portions of the output generated by a specific include file.
Modifier and Type | Field and Description |
---|---|
private java.util.Map |
args
reference to a Map used to parse arguments only
|
private int |
colNo
current column number for the preprocessor output
|
private boolean |
deferMarkers
Queue markers instead of pushing them back on the input stream.
|
private java.io.PrintWriter |
err
Stream for error messages.
|
private int |
errors
counter for the preprocessor errors
|
private boolean |
expandBraces
Disable/enable braces parser expansions.
|
private boolean |
file
Type of the environment - file (true) or reference (false).
|
private Hints |
hints
reference to Hints
|
private boolean |
ifCondValue
The value of the last executed IF or ELSEIF statement.
|
private ClearStream |
ins
input stream with a large pushback buffer
|
private boolean |
inSkipBlock
Flag indicating the parser is operating a skipBlock.
|
private boolean |
inStat
true if in a preprocessor statement. |
private antlr.CharScanner |
lexer
reference to a lexer for calling
newline() etc |
private int |
lineNo
current line number for the preprocessor output
|
private antlr.LexerSharedInputState |
lsi
ANTLR lexer shared input state object
|
private char |
marker
Configurable marker character.
|
private Options |
opt
preprocessor options
|
private java.io.Writer |
ous
Stream for preprocessor output.
|
private Environment |
parent
Parent node or
null . |
private antlr.Parser |
parser
reference to a parser for calling
setFileName() |
private boolean |
pendingSpaceRemoval
true if space removal is pending. |
private antlr.ParserSharedInputState |
psi
ANTLR parser shared input state object
|
private java.lang.StringBuffer |
refMarks
buffered reference hint markers
|
private Environment |
rootEnv
Top level (parent of all parents) or
null . |
private ScopedSymbolDictionary |
sym
Scoped symbol dictionary.
|
private java.io.PrintWriter |
warn
Stream for warning messages.
|
private int |
warnings
Warnings counter.
|
Constructor and Description |
---|
Environment(Environment parent,
ClearStream ins,
java.io.Writer ous,
java.io.Writer err,
java.io.Writer warn,
ScopedSymbolDictionary sym,
Options opt)
Construct an instance.
|
Modifier and Type | Method and Description |
---|---|
void |
addConstantDefinitions()
Add all preprocessor level symbol definitions into the hints, for the current scope.
|
void |
clearDeferred()
Process any deferred (queued) markers and force the hints to be
generated.
|
void |
eprint(java.lang.String message)
Prints an error message without any formatting.
|
void |
eprint(java.lang.String message,
int lineno)
Formats and prints an error message.
|
void |
eprint(java.lang.String message,
int lineno,
int colno)
Formats and prints an error message.
|
java.lang.String |
filter(java.lang.StringBuffer buf,
boolean adjust)
Scans the message for markers and newlines, handles and removes markers
and adjusts current line and column on the output stream.
|
int |
getColumn()
Gets the current output column number.
|
java.io.PrintWriter |
getErr()
Returns the error output stream.
|
int |
getErrorCounter()
Get the error counter.
|
Hints |
getHints()
Returns the hints list attached to this environment.
|
java.io.Reader |
getIns()
Returns the input source.
|
antlr.CharScanner |
getLex()
Returns the lexer attached to this environment.
|
int |
getLine()
Gets the current output line number.
|
antlr.LexerSharedInputState |
getLsi()
Returns the LexerSharedInputState object.
|
java.util.Map |
getMap()
Returns the Map attached to this environment.
|
java.lang.String |
getMarkers()
Gets all buffered reference markers as a single string.
|
Options |
getOpt()
Returns options object.
|
antlr.Parser |
getPar()
Returns the parser attached to this environment.
|
Environment |
getParent()
Gets the parent environment.
|
antlr.ParserSharedInputState |
getPsi()
Returns the ParserSharedInputState object.
|
ScopedSymbolDictionary |
getSym()
Returns scoped symbol dictionary.
|
java.io.PrintWriter |
getWarn()
Returns the warning output stream.
|
int |
getWarningCounter()
Get the warning counter.
|
private void |
handleMarker(java.lang.String label)
Identifies the marker and adds the corresponding element into the hints
tree.
|
private void |
incrErrorCounter()
Increment the error counter.
|
private void |
incrWarningCounter()
Increment the warning counter.
|
boolean |
isDeferMarkers()
Gets the flag to defer marker processing.
|
boolean |
isExpandBraces()
Gets the flag that enables or disables the braces parser's expansion of
braces.
|
boolean |
isFile()
Gets the environment type.
|
boolean |
isIfCondValue()
Get the value of last checked IF condition.
|
boolean |
isInSkipBlock()
Get the value of the
inSkipBlock flag. |
boolean |
isInStatement()
Gets the inStatement state.
|
boolean |
isInString()
Detects if the processing is inside a string literal.
|
java.lang.String |
lookahead(antlr.CharScanner lexer)
Obtain the lookahead of the given or current lexer as a string.
|
void |
mprint(java.lang.StringBuffer message,
int lineno)
Edits and prints a programmer's message.
|
void |
mprint(java.lang.String message,
int lineno)
Edits and prints a programmer's message.
|
void |
print(java.lang.String message)
Prints the filtered message to the output stream.
|
void |
print(java.lang.StringBuffer message)
Prints the filtered message to the output stream.
|
void |
pushBack(char c)
Pushes a character back into the input reader.
|
void |
pushBack(java.lang.String data)
Pushes back some string data into the input reader.
|
void |
queueMarker(java.lang.String marker)
Buffers the markers for deferred insertion.
|
void |
queueMarker(java.lang.String prefix,
int index)
Buffers the markers for deferred insertion.
|
void |
setColumn(int colNo)
Sets the output column number.
|
void |
setDeferMarkers(boolean deferMarkers)
Sets the flag to defer marker processing.
|
void |
setExpandBraces(boolean expandBraces)
Sets the flag to enable or disable the braces parser's expansion of
braces.
|
void |
setFile(boolean file)
Sets the environment type.
|
void |
setHints(Hints hints)
Attaches a hints list to this environment.
|
void |
setIfCondValue(boolean val)
Set the value of last checked IF condition.
|
void |
setInComment(boolean inComment)
Sets the inComment state.
|
void |
setInSkipBlock(boolean val)
Set the value of the
inSkipBlock flag. |
void |
setInStatement(boolean inStat)
Sets the inStatement state.
|
void |
setInString(boolean inString)
Sets the inString state.
|
void |
setLex(antlr.CharScanner lexer)
Attaches the specified lexer to this environment.
|
void |
setLine(int lineNo)
Sets the output line number.
|
void |
setLsi(antlr.LexerSharedInputState lsi)
Sets the LexerSharedInputState object.
|
void |
setMap(java.util.Map args)
Attaches the specified arguments Map to this environment.
|
void |
setPar(antlr.Parser parser)
Attaches the specified parser to this environment.
|
void |
setPsi(antlr.ParserSharedInputState psi)
Sets the ParserSharedInputState object.
|
void |
wprint(java.lang.String message,
int line,
int col)
Formats and prints a warning message.
|
private Environment parent
null
.private Environment rootEnv
null
.private boolean file
private antlr.LexerSharedInputState lsi
private antlr.ParserSharedInputState psi
private antlr.CharScanner lexer
newline()
etcprivate antlr.Parser parser
setFileName()
private ClearStream ins
private java.io.Writer ous
private int lineNo
private int colNo
private java.io.PrintWriter err
private java.io.PrintWriter warn
private ScopedSymbolDictionary sym
private Options opt
private int errors
private int warnings
private java.util.Map args
private char marker
private Hints hints
private java.lang.StringBuffer refMarks
private boolean inStat
true
if in a preprocessor statement.private boolean pendingSpaceRemoval
true
if space removal is pending.private boolean expandBraces
private boolean deferMarkers
private boolean ifCondValue
private boolean inSkipBlock
public Environment(Environment parent, ClearStream ins, java.io.Writer ous, java.io.Writer err, java.io.Writer warn, ScopedSymbolDictionary sym, Options opt) throws javax.xml.parsers.ParserConfigurationException, java.io.IOException
parent
- Parent Environment object or null
.ins
- Input stream, an instance of ClearStream providing
input for all lexers using this environment.ous
- Stream for preprocessor output.err
- Stream for preprocessor error messages.warn
- Stream for preprocessor warning messages.sym
- Scoped symbol dictionary.opt
- Configuration options.javax.xml.parsers.ParserConfigurationException
- Forwarded from XmlHelper.java.io.IOException
- Forwarded from XmlHelper.public void addConstantDefinitions()
public void setFile(boolean file)
file
- true
if this environment is for a file.
Otherwise is a reference.public boolean isFile()
true
if this environment is for a file.
Otherwise is a reference.public Environment getParent()
null
.public antlr.LexerSharedInputState getLsi()
public void setLsi(antlr.LexerSharedInputState lsi)
lsi
- ANTLR LexerSharedInputState to be associated with
this Environmentpublic antlr.ParserSharedInputState getPsi()
public void setPsi(antlr.ParserSharedInputState psi)
psi
- ANTLR ParserSharedInputState to be associated with
this Environmentpublic java.lang.String lookahead(antlr.CharScanner lexer)
EOF
or if there is no
lookahead, the empty string is returned.lexer
- The lexer from which to obtain the lookahead or
null
to use the currently defined lexer within
this instance.public void setLex(antlr.CharScanner lexer)
lexer
- ANTLR CharScanner lexer to be attached to
this Environmentpublic antlr.CharScanner getLex()
public void setPar(antlr.Parser parser)
parser
- ANTLR Parser to be attached to this Environmentpublic antlr.Parser getPar()
public java.io.Reader getIns()
public void setInComment(boolean inComment)
inComment
- new state of the flagpublic void setInString(boolean inString)
inString
- new state of the flagpublic boolean isInString()
true
if the current processing is inside a
string literal.public void setInStatement(boolean inStat)
inStat
- new state of the flagpublic boolean isInStatement()
true
if in statement flag is setpublic void setExpandBraces(boolean expandBraces)
expandBraces
- true
to enable.public boolean isExpandBraces()
true
if braces expansion is enabled.public boolean isIfCondValue()
ifCondValue
flag.public void setIfCondValue(boolean val)
val
- The result of the last checked IF condition.public boolean isInSkipBlock()
inSkipBlock
flag.inSkipBlock
flag.public void setInSkipBlock(boolean val)
inSkipBlock
flag.val
- The value.public void setDeferMarkers(boolean deferMarkers)
deferMarkers
- true
to defer marker processing.public boolean isDeferMarkers()
true
to defer marker processing.public void clearDeferred()
public void pushBack(java.lang.String data)
data
- sequence of characters to be put back into streampublic void pushBack(char c)
c
- character to be put back into streampublic void setLine(int lineNo)
lineNo
- new line number to be setpublic void setColumn(int colNo)
colNo
- new column number to be setpublic int getLine()
public int getColumn()
public void print(java.lang.String message) throws java.io.IOException
Markers are sequences of characters that start and end with the marker character, which is configurable through an option.
message
- text to be put on output stream this Environment prints to.java.io.IOException
- when an unclosed marker is detected in the textpublic void print(java.lang.StringBuffer message) throws java.io.IOException
Markers are sequences of characters that start and end with the marker character, which is configurable through an option.
message
- text to be put on output stream this Environment prints to.java.io.IOException
- when an unclosed marker is detected in the textpublic java.io.PrintWriter getErr()
public java.io.PrintWriter getWarn()
public void eprint(java.lang.String message)
message
- text to be put on error stream literallypublic void eprint(java.lang.String message, int lineno)
message
- text to be edited and put on error streamlineno
- line number to be inserted into the messagepublic void eprint(java.lang.String message, int lineno, int colno)
message
- text to be edited and put on error streamlineno
- line number to be inserted into the messagecolno
- column number to be inserted into the messagepublic void wprint(java.lang.String message, int line, int col)
message
- text to be edited and put on error streamline
- line number to be inserted into the messagecol
- column number to be inserted into the messagepublic int getWarningCounter()
public void mprint(java.lang.String message, int lineno)
&MESSAGE
preprocessor statement implementation.message
- text to be edited and put on error streamlineno
- line number to be inserted into the messagepublic void mprint(java.lang.StringBuffer message, int lineno)
&MESSAGE
preprocessor statement implementation.message
- text to be edited and put on error streamlineno
- line number to be inserted into the messagepublic ScopedSymbolDictionary getSym()
public Options getOpt()
public int getErrorCounter()
public void setMap(java.util.Map args)
args
- a Map that contains the results of arguments parsingpublic java.util.Map getMap()
public void setHints(Hints hints)
hints
- the hints objectpublic Hints getHints()
public void queueMarker(java.lang.String prefix, int index)
prefix
- prefix of the marker.index
- marker's index.public void queueMarker(java.lang.String marker)
marker
- the whole marker as a string.public java.lang.String getMarkers()
public java.lang.String filter(java.lang.StringBuffer buf, boolean adjust) throws java.io.IOException
Markers are followed by a space separating them from the text. Those spaces may come either as part of the same token (in comments and strings) or as part of a separate WS token. In any case, the separating space has to be removed.
buf
- text about to be put on output stream.adjust
- true
if line and column numbers have to be adjustedjava.io.IOException
- when an unclosed marker is detected in the textprivate void incrWarningCounter()
private void incrErrorCounter()
private void handleMarker(java.lang.String label)
The contents of the marker can be:
label
- contents of the marker