Top Description Fields Constructors Methods
org.antlr.runtime.tree

public Class TreeParser

extends BaseRecognizer
Class Inheritance
Known Direct Subclasses
org.antlr.runtime.tree.TreeRewriter, org.antlr.runtime.debug.DebugTreeParser, org.antlr.runtime.tree.TreeFilter
Imports
org.antlr.runtime.*, java.util.regex.Matcher, .Pattern

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

Modifier and TypeField and Description
pack-priv static String
pack-priv static Pattern
pack-priv static String
pack-priv static Pattern
public static final int
protected TreeNodeStream
public static final int
Inherited from org.antlr.runtime.BaseRecognizer:
DEFAULT_TOKEN_CHANNELHIDDENINITIAL_FOLLOW_STACK_SIZEMEMO_RULE_FAILEDMEMO_RULE_UNKNOWNNEXT_TOKEN_RULE_NAMEstate

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

Modifier and TypeMethod and Description
protected static Object
getAncestor(TreeAdaptor adaptor, String[] tokenNames, Object t, String goal)

Helper for static inContext

protected Object
getCurrentInputSymbol(IntStream input)

Overrides org.antlr.runtime.BaseRecognizer.getCurrentInputSymbol.

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.
public String
getErrorHeader(RecognitionException e)

Overrides org.antlr.runtime.BaseRecognizer.getErrorHeader.

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.
public String
getErrorMessage(RecognitionException e, String[] tokenNames)

Overrides org.antlr.runtime.BaseRecognizer.getErrorMessage.

Tree parsers parse nodes they usually have a token object as payload.
protected Object
getMissingSymbol(IntStream input, RecognitionException e, int expectedTokenType, BitSet follow)

Overrides org.antlr.runtime.BaseRecognizer.getMissingSymbol.

Conjure up a missing token during error recovery.
public String
public TreeNodeStream
public boolean
inContext(String context)

Check if current node in input has a context.

public static boolean
inContext(TreeAdaptor adaptor, String[] tokenNames, Object t, String context)

The worker for inContext.

public void
matchAny(IntStream ignore)

Overrides org.antlr.runtime.BaseRecognizer.matchAny.

Match '.' in tree parser has special meaning.
protected Object
recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow)

Overrides org.antlr.runtime.BaseRecognizer.recoverFromMismatchedToken.

We have DOWN/UP nodes in the stream that have no line info; override.
public void
reset()

Overrides org.antlr.runtime.BaseRecognizer.reset.

reset the parser's state; subclasses must rewinds the input stream
public void
setTreeNodeStream(TreeNodeStream input)

Set the input stream

public void
traceIn(String ruleName, int ruleIndex)

public void
traceOut(String ruleName, int ruleIndex)

Inherited from org.antlr.runtime.BaseRecognizer:
alreadyParsedRulebeginResynccombineFollowscomputeContextSensitiveRuleFOLLOWcomputeErrorRecoverySetconsumeUntilconsumeUntildisplayRecognitionErroremitErrorMessageendResyncfailedgetBacktrackingLevelgetGrammarFileNamegetNumberOfSyntaxErrorsgetRuleInvocationStackgetRuleInvocationStackgetRuleMemoizationgetRuleMemoizationCacheSizegetTokenErrorDisplaygetTokenNamesmatchmemoizemismatchIsMissingTokenmismatchIsUnwantedTokenpushFollowrecoverrecoverFromMismatchedSetreportErrorsetBacktrackingLeveltoStringstraceIntraceOut

Field Detail

dotdotback to summary
pack-priv static String dotdot
dotdotPatternback to summary
pack-priv static Pattern dotdotPattern
doubleEtcback to summary
pack-priv static String doubleEtc
doubleEtcPatternback to summary
pack-priv static Pattern doubleEtcPattern
DOWNback to summary
public static final int DOWN
inputback to summary
protected TreeNodeStream input
UPback to summary
public static final int UP

Constructor Detail

TreeParserback to summary
public TreeParser(TreeNodeStream input)
TreeParserback to summary
public TreeParser(TreeNodeStream input, RecognizerSharedState state)

Method Detail

getAncestorback to summary
protected static Object getAncestor(TreeAdaptor adaptor, String[] tokenNames, Object t, String goal)

Helper for static inContext

getCurrentInputSymbolback to summary
protected Object getCurrentInputSymbol(IntStream input)

Overrides org.antlr.runtime.BaseRecognizer.getCurrentInputSymbol.

Doc from org.antlr.runtime.BaseRecognizer.getCurrentInputSymbol.

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.

Annotations
@Override
getErrorHeaderback to summary
public String getErrorHeader(RecognitionException e)

Overrides org.antlr.runtime.BaseRecognizer.getErrorHeader.

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.

Annotations
@Override
getErrorMessageback to summary
public String getErrorMessage(RecognitionException e, String[] tokenNames)

Overrides org.antlr.runtime.BaseRecognizer.getErrorMessage.

Tree parsers parse nodes they usually have a token object as payload. Set the exception token and do the default behavior.

Annotations
@Override
getMissingSymbolback to summary
protected Object getMissingSymbol(IntStream input, RecognitionException e, int expectedTokenType, BitSet follow)

Overrides org.antlr.runtime.BaseRecognizer.getMissingSymbol.

Doc from org.antlr.runtime.BaseRecognizer.getMissingSymbol.

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.

Annotations
@Override
getSourceNameback to summary
public String getSourceName()

Implements abstract org.antlr.runtime.BaseRecognizer.getSourceName.

Annotations
@Override
getTreeNodeStreamback to summary
public TreeNodeStream getTreeNodeStream()
inContextback to summary
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.

inContextback to summary
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.

matchAnyback to summary
public void matchAny(IntStream ignore)

Overrides org.antlr.runtime.BaseRecognizer.matchAny.

Match '.' in tree parser has special meaning. Skip node or entire tree if node has children. If children, scan until corresponding UP node.

Annotations
@Override
recoverFromMismatchedTokenback to summary
protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) throws RecognitionException

Overrides org.antlr.runtime.BaseRecognizer.recoverFromMismatchedToken.

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...

Annotations
@Override
resetback to summary
public void reset()

Overrides org.antlr.runtime.BaseRecognizer.reset.

Doc from org.antlr.runtime.BaseRecognizer.reset.

reset the parser's state; subclasses must rewinds the input stream

Annotations
@Override
setTreeNodeStreamback to summary
public void setTreeNodeStream(TreeNodeStream input)

Set the input stream

traceInback to summary
public void traceIn(String ruleName, int ruleIndex)
traceOutback to summary
public void traceOut(String ruleName, int ruleIndex)