Top Description Fields Constructors Methods
org.antlr.tool

public Class GrammarAST

extends CommonTree
Class Inheritance
Known Direct Subclasses
org.antlr.grammar.v3.ANTLRParser.GrammarASTErrorNode
Imports
org.antlr.analysis.DFA, .NFAState, org.antlr.grammar.v3.ANTLRParser, org.antlr.misc.IntSet, org.antlr.runtime.CommonToken, .Token, org.antlr.runtime.tree.CommonTree, .Tree, org.stringtemplate.v4.ST, java.util.*

Grammars are first converted to ASTs using this class and then are converted to NFAs via a tree walker. The reader may notice that I have made a very non-OO decision in this class to track variables for many different kinds of nodes. It wastes space for nodes that don't need the values and OO principles cry out for a new class type for each kind of node in my tree. I am doing this on purpose for a variety of reasons. I don't like using the type system for different node types; it yields too many damn class files which I hate. Perhaps if I put them all in one file. Most importantly though I hate all the type casting that would have to go on. I would have all sorts of extra work to do. Ick. Anyway, I'm doing all this on purpose, not out of ignorance. ;)

Field Summary

Modifier and TypeField and Description
protected Map<String, Object>
blockOptions

If this is a BLOCK node, track options here

public ST
code

if this is a TOKEN_REF or RULE_REF node, this is the code ST generated for this node.

pack-priv static int
private static final GrammarAST
private static final GrammarAST
public String
public NFAState
followingNFAState

Rule ref nodes, token refs, set, and NOT set refs need to track their location in the generated NFA so that local FOLLOW sets can be computed during code gen for automatic error recovery.

public int
public DFA
lookaheadDFA

If this is a decision node, what is the lookahead DFA?

public NFAState
NFAStartState

What NFA start state was built from this node?

public NFAState
NFATreeDownState

This is used for TREE_BEGIN nodes to point into the NFA.

public int
outerAltNum

if this is an ACTION node, this is the outermost enclosing alt num in rule.

public Set<GrammarAST>
public Set<GrammarAST>
rewriteRefsShallow

If this is a BLOCK node for a rewrite rule, track referenced elements here.

protected IntSet
setValue

If this is a SET node, what are the elements?

public Map<String, Object>
private String
Inherited from org.antlr.runtime.tree.CommonTree:
childIndexparentstartIndexstopIndextoken

Constructor Summary

AccessConstructor and Description
public
public
GrammarAST(int t, String txt)

public

Method Summary

Modifier and TypeMethod and Description
public void
_findAllType(int ttype, List<GrammarAST> nodes)

public static List<Tree>
public static List<Tree>
descendants(Tree root, boolean insertDownUpNodes)

public static GrammarAST
dup(Tree t)

public Tree
public static GrammarAST
public static GrammarAST
dupTreeNoActions(GrammarAST t, GrammarAST parent)

Duplicate a tree, assuming this is a root node of a tree-- duplicate that node and what's below; ignore siblings of root node.

public boolean
equals(Object
the reference object with which to compare.
ast
)

Overrides java.lang.Object.equals.

Make nodes unique based upon Token so we can add them to a Set; if not a GrammarAST, check type.
public List<GrammarAST>
findAllType(int ttype)

public GrammarAST
findFirstType(int ttype)

public GrammarAST
getBlockALT(int i)

public Object
public Map<String, Object>
public int
public GrammarAST[]
private static List<GrammarAST>
public GrammarAST
public GrammarAST
public int
getLine()

Overrides org.antlr.runtime.tree.CommonTree.getLine.

Implements org.antlr.runtime.tree.Tree.getLine.

In case we don't have a token payload, what is the line for errors?
public DFA
public GrammarAST
public NFAState
public IntSet
public String
public int
getType()

Overrides org.antlr.runtime.tree.CommonTree.getType.

Implements org.antlr.runtime.tree.Tree.getType.

Return a token type; needed for tree parsing
public int
hashCode()

Overrides java.lang.Object.hashCode.

Make nodes unique based upon Token so we can add them to a Set; if not a GrammarAST, check type.
public boolean
hasSameTreeStructure(Tree other)

See if tree has exact token types and structure; no text

public void
initialize(int i, String s)

public void
public void
public String
setBlockOption(Grammar grammar, String key, Object value)

Save the option key/value pair and process it; return the key or null if invalid option.

public void
setBlockOptions(Map<String, Object> blockOptions)

public void
public void
setLine(int line)

public void
setLookaheadDFA(DFA lookaheadDFA)

public void
setNFAStartState(NFAState nfaStartState)

public String
setOption(Map<String, Object> options, Set<String> legalOptions, Grammar grammar, String key, Object value)

public void
setOptions(Grammar grammar, Map<String, Object> options)

public void
setSetValue(IntSet setValue)

public String
setTerminalOption(Grammar grammar, String key, Object value)

public void
public void
setTokenBoundaries(Token startToken, Token stopToken)

Track start/stop token for subtree root created for a rule.

public void
public void
setType(int type)

public String
Inherited from org.antlr.runtime.tree.CommonTree:
getChildIndexgetParentgetTokengetTokenStartIndexgetTokenStopIndexisNilsetChildIndexsetParentsetTokenStartIndexsetTokenStopIndexsetUnknownTokenBoundariestoString

Field Detail

blockOptionsback to summary
protected Map<String, Object> blockOptions

If this is a BLOCK node, track options here

codeback to summary
public ST code

if this is a TOKEN_REF or RULE_REF node, this is the code ST generated for this node. We need to update it later to add a label if someone does $tokenref or $ruleref in an action.

countback to summary
pack-priv static int count
DescendantDownNodeback to summary
private static final GrammarAST DescendantDownNode
DescendantUpNodeback to summary
private static final GrammarAST DescendantUpNode
enclosingRuleNameback to summary
public String enclosingRuleName
followingNFAStateback to summary
public NFAState followingNFAState

Rule ref nodes, token refs, set, and NOT set refs need to track their location in the generated NFA so that local FOLLOW sets can be computed during code gen for automatic error recovery.

IDback to summary
public int ID
lookaheadDFAback to summary
public DFA lookaheadDFA

If this is a decision node, what is the lookahead DFA?

NFAStartStateback to summary
public NFAState NFAStartState

What NFA start state was built from this node?

NFATreeDownStateback to summary
public NFAState NFATreeDownState

This is used for TREE_BEGIN nodes to point into the NFA. TREE_BEGINs point at left edge of DOWN for LOOK computation purposes (Nullable tree child list needs special code gen when matching).

outerAltNumback to summary
public int outerAltNum

if this is an ACTION node, this is the outermost enclosing alt num in rule. For actions, define.g sets these (used to be codegen.g). We need these set so we can examine actions early, before code gen, for refs to rule predefined properties and rule labels. For most part define.g sets outerAltNum, but codegen.g does the ones for %foo(a={$ID.text}) type refs as the {$ID...} is not seen as an action until code gen pulls apart.

rewriteRefsDeepback to summary
public Set<GrammarAST> rewriteRefsDeep
rewriteRefsShallowback to summary
public Set<GrammarAST> rewriteRefsShallow

If this is a BLOCK node for a rewrite rule, track referenced elements here. Don't track elements in nested subrules.

setValueback to summary
protected IntSet setValue

If this is a SET node, what are the elements?

terminalOptionsback to summary
public Map<String, Object> terminalOptions
textOverrideback to summary
private String textOverride

Constructor Detail

GrammarASTback to summary
public GrammarAST()
GrammarASTback to summary
public GrammarAST(int t, String txt)
Annotations
@SuppressWarnings:OverridableMethodCallInConstructor
GrammarASTback to summary
public GrammarAST(Token token)
Annotations
@SuppressWarnings:OverridableMethodCallInConstructor

Method Detail

_findAllTypeback to summary
public void _findAllType(int ttype, List<GrammarAST> nodes)
descendantsback to summary
public static List<Tree> descendants(Tree root)
descendantsback to summary
public static List<Tree> descendants(Tree root, boolean insertDownUpNodes)
dupback to summary
public static GrammarAST dup(Tree t)
dupNodeback to summary
public Tree dupNode()

Overrides org.antlr.runtime.tree.CommonTree.dupNode.

Implements org.antlr.runtime.tree.Tree.dupNode.

Annotations
@Override
dupTreeback to summary
public static GrammarAST dupTree(GrammarAST t)
dupTreeNoActionsback to summary
public static GrammarAST dupTreeNoActions(GrammarAST t, GrammarAST parent)

Duplicate a tree, assuming this is a root node of a tree-- duplicate that node and what's below; ignore siblings of root node.

equalsback to summary
public boolean equals(Object ast)

Overrides java.lang.Object.equals.

Make nodes unique based upon Token so we can add them to a Set; if not a GrammarAST, check type.

Parameters
ast:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
findAllTypeback to summary
public List<GrammarAST> findAllType(int ttype)
findFirstTypeback to summary
public GrammarAST findFirstType(int ttype)
getBlockALTback to summary
public GrammarAST getBlockALT(int i)
getBlockOptionback to summary
public Object getBlockOption(String key)
getBlockOptionsback to summary
public Map<String, Object> getBlockOptions()
getCharPositionInLineback to summary
public int getCharPositionInLine()

Overrides org.antlr.runtime.tree.CommonTree.getCharPositionInLine.

Implements org.antlr.runtime.tree.Tree.getCharPositionInLine.

Annotations
@Override
getChildrenAsArrayback to summary
public GrammarAST[] getChildrenAsArray()
getChildrenForDupTreeback to summary
private static List<GrammarAST> getChildrenForDupTree(GrammarAST t)
getLastChildback to summary
public GrammarAST getLastChild()
getLastSiblingback to summary
public GrammarAST getLastSibling()
getLineback to summary
public int getLine()

Overrides org.antlr.runtime.tree.CommonTree.getLine.

Implements org.antlr.runtime.tree.Tree.getLine.

Doc from org.antlr.runtime.tree.Tree.getLine.

In case we don't have a token payload, what is the line for errors?

Annotations
@Override
getLookaheadDFAback to summary
public DFA getLookaheadDFA()
getNextSiblingback to summary
public GrammarAST getNextSibling()
getNFAStartStateback to summary
public NFAState getNFAStartState()
getSetValueback to summary
public IntSet getSetValue()
getTextback to summary
public String getText()

Overrides org.antlr.runtime.tree.CommonTree.getText.

Implements org.antlr.runtime.tree.Tree.getText.

Annotations
@Override
getTypeback to summary
public int getType()

Overrides org.antlr.runtime.tree.CommonTree.getType.

Implements org.antlr.runtime.tree.Tree.getType.

Doc from org.antlr.runtime.tree.Tree.getType.

Return a token type; needed for tree parsing

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Make nodes unique based upon Token so we can add them to a Set; if not a GrammarAST, check type.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object

Annotations
@Override
hasSameTreeStructureback to summary
public boolean hasSameTreeStructure(Tree other)

See if tree has exact token types and structure; no text

initializeback to summary
public void initialize(int i, String s)
initializeback to summary
public void initialize(Tree ast)
initializeback to summary
public void initialize(Token token)
setBlockOptionback to summary
public String setBlockOption(Grammar grammar, String key, Object value)

Save the option key/value pair and process it; return the key or null if invalid option.

setBlockOptionsback to summary
public void setBlockOptions(Map<String, Object> blockOptions)
setCharPositionInLineback to summary
public void setCharPositionInLine(int value)
setLineback to summary
public void setLine(int line)
setLookaheadDFAback to summary
public void setLookaheadDFA(DFA lookaheadDFA)
setNFAStartStateback to summary
public void setNFAStartState(NFAState nfaStartState)
setOptionback to summary
public String setOption(Map<String, Object> options, Set<String> legalOptions, Grammar grammar, String key, Object value)
setOptionsback to summary
public void setOptions(Grammar grammar, Map<String, Object> options)
setSetValueback to summary
public void setSetValue(IntSet setValue)
setTerminalOptionback to summary
public String setTerminalOption(Grammar grammar, String key, Object value)
setTextback to summary
public void setText(String text)
setTokenBoundariesback to summary
public void setTokenBoundaries(Token startToken, Token stopToken)

Track start/stop token for subtree root created for a rule. Only works with Tree nodes. For rules that match nothing, seems like this will yield start=i and stop=i-1 in a nil node. Might be useful info so I'll not force to be i..i.

setTreeEnclosingRuleNameDeeplyback to summary
public void setTreeEnclosingRuleNameDeeply(String rname)
setTypeback to summary
public void setType(int type)
toStringListback to summary
public String toStringList()