Package org.antlr.runtime.tree
Class TreeParser
- java.lang.Object
-
- org.antlr.runtime.BaseRecognizer
-
- org.antlr.runtime.tree.TreeParser
-
- Direct Known Subclasses:
DebugTreeParser
,TreeFilter
,TreeRewriter
public class TreeParser extends BaseRecognizer
A parser for a stream of tree nodes. "tree grammars" result in a subclass of this. All the error reporting and recovery is shared with Parser via the BaseRecognizer superclass.
-
-
Field Summary
Fields Modifier and Type Field Description static int
DOWN
protected TreeNodeStream
input
static int
UP
-
Fields inherited from class org.antlr.runtime.BaseRecognizer
DEFAULT_TOKEN_CHANNEL, HIDDEN, INITIAL_FOLLOW_STACK_SIZE, MEMO_RULE_FAILED, MEMO_RULE_UNKNOWN, NEXT_TOKEN_RULE_NAME, state
-
-
Constructor Summary
Constructors Constructor Description TreeParser(TreeNodeStream input)
TreeParser(TreeNodeStream input, RecognizerSharedState state)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected static Object
getAncestor(TreeAdaptor adaptor, String[] tokenNames, Object t, String goal)
Helper for static inContextprotected Object
getCurrentInputSymbol(IntStream input)
Match needs to return the current input symbol, which gets put into the label for the associated token ref; e.g., x=ID.String
getErrorHeader(RecognitionException e)
Prefix error message with the grammar name because message is always intended for the programmer because the parser built the input tree not the user.String
getErrorMessage(RecognitionException e, String[] tokenNames)
Tree parsers parse nodes they usually have a token object as payload.protected Object
getMissingSymbol(IntStream input, RecognitionException e, int expectedTokenType, BitSet follow)
Conjure up a missing token during error recovery.String
getSourceName()
TreeNodeStream
getTreeNodeStream()
boolean
inContext(String context)
Check if current node in input has a context.static boolean
inContext(TreeAdaptor adaptor, String[] tokenNames, Object t, String context)
The worker for inContext.void
matchAny(IntStream ignore)
Match '.' in tree parser has special meaning.protected Object
recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow)
We have DOWN/UP nodes in the stream that have no line info; override.void
reset()
reset the parser's state; subclasses must rewinds the input streamvoid
setTreeNodeStream(TreeNodeStream input)
Set the input streamvoid
traceIn(String ruleName, int ruleIndex)
void
traceOut(String ruleName, int ruleIndex)
-
Methods inherited from class org.antlr.runtime.BaseRecognizer
alreadyParsedRule, beginResync, combineFollows, computeContextSensitiveRuleFOLLOW, computeErrorRecoverySet, consumeUntil, consumeUntil, displayRecognitionError, emitErrorMessage, endResync, failed, getBacktrackingLevel, getGrammarFileName, getNumberOfSyntaxErrors, getRuleInvocationStack, getRuleInvocationStack, getRuleMemoization, getRuleMemoizationCacheSize, getTokenErrorDisplay, getTokenNames, match, memoize, mismatchIsMissingToken, mismatchIsUnwantedToken, pushFollow, recover, recoverFromMismatchedSet, reportError, setBacktrackingLevel, toStrings, traceIn, traceOut
-
-
-
-
Field Detail
-
DOWN
public static final int DOWN
- See Also:
- Constant Field Values
-
UP
public static final int UP
- See Also:
- Constant Field Values
-
input
protected TreeNodeStream input
-
-
Constructor Detail
-
TreeParser
public TreeParser(TreeNodeStream input)
-
TreeParser
public TreeParser(TreeNodeStream input, RecognizerSharedState state)
-
-
Method Detail
-
reset
public void reset()
Description copied from class:BaseRecognizer
reset the parser's state; subclasses must rewinds the input stream- Overrides:
reset
in classBaseRecognizer
-
setTreeNodeStream
public void setTreeNodeStream(TreeNodeStream input)
Set the input stream
-
getTreeNodeStream
public TreeNodeStream getTreeNodeStream()
-
getSourceName
public String getSourceName()
- Specified by:
getSourceName
in classBaseRecognizer
-
getCurrentInputSymbol
protected Object getCurrentInputSymbol(IntStream input)
Description copied from class:BaseRecognizer
Match needs to return the current input symbol, which gets put into the label for the associated token ref; e.g., x=ID. Token and tree parsers need to return different objects. Rather than test for input stream type or change the IntStream interface, I use a simple method to ask the recognizer to tell me what the current input symbol is. This is ignored for lexers.- Overrides:
getCurrentInputSymbol
in classBaseRecognizer
-
getMissingSymbol
protected Object getMissingSymbol(IntStream input, RecognitionException e, int expectedTokenType, BitSet follow)
Description copied from class:BaseRecognizer
Conjure up a missing token during error recovery. The recognizer attempts to recover from single missing symbols. But, actions might refer to that missing symbol. For example, x=ID {f($x);}. The action clearly assumes that there has been an identifier matched previously and that $x points at that token. If that token is missing, but the next token in the stream is what we want we assume that this token is missing and we keep going. Because we have to return some token to replace the missing token, we have to conjure one up. This method gives the user control over the tokens returned for missing tokens. Mostly, you will want to create something special for identifier tokens. For literals such as '{' and ',', the default action in the parser or tree parser works. It simply creates a CommonToken of the appropriate type. The text will be the token. If you change what tokens must be created by the lexer, override this method to create the appropriate tokens.- Overrides:
getMissingSymbol
in classBaseRecognizer
-
matchAny
public void matchAny(IntStream ignore)
Match '.' in tree parser has special meaning. Skip node or entire tree if node has children. If children, scan until corresponding UP node.- Overrides:
matchAny
in classBaseRecognizer
-
recoverFromMismatchedToken
protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) throws RecognitionException
We have DOWN/UP nodes in the stream that have no line info; override. plus we want to alter the exception type. Don't try to recover from tree parser errors inline...- Overrides:
recoverFromMismatchedToken
in classBaseRecognizer
- Throws:
RecognitionException
-
getErrorHeader
public String getErrorHeader(RecognitionException e)
Prefix error message with the grammar name because message is always intended for the programmer because the parser built the input tree not the user.- Overrides:
getErrorHeader
in classBaseRecognizer
-
getErrorMessage
public String getErrorMessage(RecognitionException e, String[] tokenNames)
Tree parsers parse nodes they usually have a token object as payload. Set the exception token and do the default behavior.- Overrides:
getErrorMessage
in classBaseRecognizer
-
inContext
public boolean inContext(String context)
Check if current node in input has a context. Context means sequence of nodes towards root of tree. For example, you might say context is "MULT" which means my parent must be MULT. "CLASS VARDEF" says current node must be child of a VARDEF and whose parent is a CLASS node. You can use "..." to mean zero-or-more nodes. "METHOD ... VARDEF" means my parent is VARDEF and somewhere above that is a METHOD node. The first node in the context is not necessarily the root. The context matcher stops matching and returns true when it runs out of context. There is no way to force the first node to be the root.
-
inContext
public static boolean inContext(TreeAdaptor adaptor, String[] tokenNames, Object t, String context)
The worker for inContext. It's static and full of parameters for testing purposes.
-
getAncestor
protected static Object getAncestor(TreeAdaptor adaptor, String[] tokenNames, Object t, String goal)
Helper for static inContext
-
traceIn
public void traceIn(String ruleName, int ruleIndex)
-
traceOut
public void traceOut(String ruleName, int ruleIndex)
-
-