Package org.antlr.tool
Class GrammarAST
- java.lang.Object
-
- org.antlr.runtime.tree.BaseTree
-
- org.antlr.runtime.tree.CommonTree
-
- org.antlr.tool.GrammarAST
-
- All Implemented Interfaces:
org.antlr.runtime.tree.Tree
- Direct Known Subclasses:
ANTLRParser.GrammarASTErrorNode
public class GrammarAST extends org.antlr.runtime.tree.CommonTreeGrammars 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
Fields Modifier and Type Field Description protected java.util.Map<java.lang.String,java.lang.Object>blockOptionsIf this is a BLOCK node, track options hereorg.stringtemplate.v4.STcodeif this is a TOKEN_REF or RULE_REF node, this is the code ST generated for this node.(package private) static intcountprivate static GrammarASTDescendantDownNodeprivate static GrammarASTDescendantUpNodejava.lang.StringenclosingRuleNameNFAStatefollowingNFAStateRule 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.intIDDFAlookaheadDFAIf this is a decision node, what is the lookahead DFA?NFAStateNFAStartStateWhat NFA start state was built from this node?NFAStateNFATreeDownStateThis is used for TREE_BEGIN nodes to point into the NFA.intouterAltNumif this is an ACTION node, this is the outermost enclosing alt num in rule.java.util.Set<GrammarAST>rewriteRefsDeepjava.util.Set<GrammarAST>rewriteRefsShallowIf this is a BLOCK node for a rewrite rule, track referenced elements here.protected IntSetsetValueIf this is a SET node, what are the elements?java.util.Map<java.lang.String,java.lang.Object>terminalOptionsprivate java.lang.StringtextOverride
-
Constructor Summary
Constructors Constructor Description GrammarAST()GrammarAST(int t, java.lang.String txt)GrammarAST(org.antlr.runtime.Token token)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void_findAllType(int ttype, java.util.List<GrammarAST> nodes)static java.util.List<org.antlr.runtime.tree.Tree>descendants(org.antlr.runtime.tree.Tree root)static java.util.List<org.antlr.runtime.tree.Tree>descendants(org.antlr.runtime.tree.Tree root, boolean insertDownUpNodes)static GrammarASTdup(org.antlr.runtime.tree.Tree t)org.antlr.runtime.tree.TreedupNode()static GrammarASTdupTree(GrammarAST t)static GrammarASTdupTreeNoActions(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.booleanequals(java.lang.Object ast)Make nodes unique based upon Token so we can add them to a Set; if not a GrammarAST, check type.java.util.List<GrammarAST>findAllType(int ttype)GrammarASTfindFirstType(int ttype)GrammarASTgetBlockALT(int i)java.lang.ObjectgetBlockOption(java.lang.String key)java.util.Map<java.lang.String,java.lang.Object>getBlockOptions()intgetCharPositionInLine()GrammarAST[]getChildrenAsArray()private static java.util.List<GrammarAST>getChildrenForDupTree(GrammarAST t)GrammarASTgetLastChild()GrammarASTgetLastSibling()intgetLine()DFAgetLookaheadDFA()GrammarASTgetNextSibling()NFAStategetNFAStartState()IntSetgetSetValue()java.lang.StringgetText()intgetType()inthashCode()Make nodes unique based upon Token so we can add them to a Set; if not a GrammarAST, check type.booleanhasSameTreeStructure(org.antlr.runtime.tree.Tree other)See if tree has exact token types and structure; no textvoidinitialize(int i, java.lang.String s)voidinitialize(org.antlr.runtime.Token token)voidinitialize(org.antlr.runtime.tree.Tree ast)java.lang.StringsetBlockOption(Grammar grammar, java.lang.String key, java.lang.Object value)Save the option key/value pair and process it; return the key or null if invalid option.voidsetBlockOptions(java.util.Map<java.lang.String,java.lang.Object> blockOptions)voidsetCharPositionInLine(int value)voidsetLine(int line)voidsetLookaheadDFA(DFA lookaheadDFA)voidsetNFAStartState(NFAState nfaStartState)java.lang.StringsetOption(java.util.Map<java.lang.String,java.lang.Object> options, java.util.Set<java.lang.String> legalOptions, Grammar grammar, java.lang.String key, java.lang.Object value)voidsetOptions(Grammar grammar, java.util.Map<java.lang.String,java.lang.Object> options)voidsetSetValue(IntSet setValue)java.lang.StringsetTerminalOption(Grammar grammar, java.lang.String key, java.lang.Object value)voidsetText(java.lang.String text)voidsetTokenBoundaries(org.antlr.runtime.Token startToken, org.antlr.runtime.Token stopToken)Track start/stop token for subtree root created for a rule.voidsetTreeEnclosingRuleNameDeeply(java.lang.String rname)voidsetType(int type)java.lang.StringtoStringList()-
Methods inherited from class org.antlr.runtime.tree.CommonTree
getChildIndex, getParent, getToken, getTokenStartIndex, getTokenStopIndex, isNil, setChildIndex, setParent, setTokenStartIndex, setTokenStopIndex, setUnknownTokenBoundaries, toString
-
Methods inherited from class org.antlr.runtime.tree.BaseTree
addChild, addChildren, createChildrenList, deleteChild, freshenParentAndChildIndexes, freshenParentAndChildIndexes, freshenParentAndChildIndexesDeeply, freshenParentAndChildIndexesDeeply, getAncestor, getAncestors, getChild, getChildCount, getChildren, getFirstChildWithType, hasAncestor, insertChild, replaceChildren, sanityCheckParentAndChildIndexes, sanityCheckParentAndChildIndexes, setChild, toStringTree
-
-
-
-
Field Detail
-
count
static int count
-
ID
public int ID
-
textOverride
private java.lang.String textOverride
-
enclosingRuleName
public java.lang.String enclosingRuleName
-
lookaheadDFA
public DFA lookaheadDFA
If this is a decision node, what is the lookahead DFA?
-
NFAStartState
public NFAState NFAStartState
What NFA start state was built from this node?
-
NFATreeDownState
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).
-
followingNFAState
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.
-
setValue
protected IntSet setValue
If this is a SET node, what are the elements?
-
blockOptions
protected java.util.Map<java.lang.String,java.lang.Object> blockOptions
If this is a BLOCK node, track options here
-
rewriteRefsShallow
public java.util.Set<GrammarAST> rewriteRefsShallow
If this is a BLOCK node for a rewrite rule, track referenced elements here. Don't track elements in nested subrules.
-
rewriteRefsDeep
public java.util.Set<GrammarAST> rewriteRefsDeep
-
terminalOptions
public java.util.Map<java.lang.String,java.lang.Object> terminalOptions
-
outerAltNum
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.
-
code
public org.stringtemplate.v4.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.
-
DescendantDownNode
private static final GrammarAST DescendantDownNode
-
DescendantUpNode
private static final GrammarAST DescendantUpNode
-
-
Method Detail
-
getBlockOptions
public java.util.Map<java.lang.String,java.lang.Object> getBlockOptions()
-
setBlockOptions
public void setBlockOptions(java.util.Map<java.lang.String,java.lang.Object> blockOptions)
- Parameters:
blockOptions-
-
initialize
public void initialize(int i, java.lang.String s)
-
initialize
public void initialize(org.antlr.runtime.tree.Tree ast)
-
initialize
public void initialize(org.antlr.runtime.Token token)
-
getLookaheadDFA
public DFA getLookaheadDFA()
-
setLookaheadDFA
public void setLookaheadDFA(DFA lookaheadDFA)
-
getNFAStartState
public NFAState getNFAStartState()
-
setNFAStartState
public void setNFAStartState(NFAState nfaStartState)
-
setBlockOption
public java.lang.String setBlockOption(Grammar grammar, java.lang.String key, java.lang.Object value)
Save the option key/value pair and process it; return the key or null if invalid option.
-
setTerminalOption
public java.lang.String setTerminalOption(Grammar grammar, java.lang.String key, java.lang.Object value)
-
setOption
public java.lang.String setOption(java.util.Map<java.lang.String,java.lang.Object> options, java.util.Set<java.lang.String> legalOptions, Grammar grammar, java.lang.String key, java.lang.Object value)
-
getBlockOption
public java.lang.Object getBlockOption(java.lang.String key)
-
setOptions
public void setOptions(Grammar grammar, java.util.Map<java.lang.String,java.lang.Object> options)
-
getText
public java.lang.String getText()
- Specified by:
getTextin interfaceorg.antlr.runtime.tree.Tree- Overrides:
getTextin classorg.antlr.runtime.tree.CommonTree
-
setType
public void setType(int type)
-
setText
public void setText(java.lang.String text)
-
getType
public int getType()
- Specified by:
getTypein interfaceorg.antlr.runtime.tree.Tree- Overrides:
getTypein classorg.antlr.runtime.tree.CommonTree
-
getLine
public int getLine()
- Specified by:
getLinein interfaceorg.antlr.runtime.tree.Tree- Overrides:
getLinein classorg.antlr.runtime.tree.CommonTree
-
getCharPositionInLine
public int getCharPositionInLine()
- Specified by:
getCharPositionInLinein interfaceorg.antlr.runtime.tree.Tree- Overrides:
getCharPositionInLinein classorg.antlr.runtime.tree.CommonTree
-
setLine
public void setLine(int line)
-
setCharPositionInLine
public void setCharPositionInLine(int value)
-
getSetValue
public IntSet getSetValue()
-
setSetValue
public void setSetValue(IntSet setValue)
-
getLastChild
public GrammarAST getLastChild()
-
getNextSibling
public GrammarAST getNextSibling()
-
getLastSibling
public GrammarAST getLastSibling()
-
getChildrenAsArray
public GrammarAST[] getChildrenAsArray()
-
descendants
public static java.util.List<org.antlr.runtime.tree.Tree> descendants(org.antlr.runtime.tree.Tree root)
-
descendants
public static java.util.List<org.antlr.runtime.tree.Tree> descendants(org.antlr.runtime.tree.Tree root, boolean insertDownUpNodes)
-
findFirstType
public GrammarAST findFirstType(int ttype)
-
findAllType
public java.util.List<GrammarAST> findAllType(int ttype)
-
_findAllType
public void _findAllType(int ttype, java.util.List<GrammarAST> nodes)
-
equals
public boolean equals(java.lang.Object ast)
Make nodes unique based upon Token so we can add them to a Set; if not a GrammarAST, check type.- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
Make nodes unique based upon Token so we can add them to a Set; if not a GrammarAST, check type.- Overrides:
hashCodein classjava.lang.Object
-
hasSameTreeStructure
public boolean hasSameTreeStructure(org.antlr.runtime.tree.Tree other)
See if tree has exact token types and structure; no text
-
dup
public static GrammarAST dup(org.antlr.runtime.tree.Tree t)
-
dupNode
public org.antlr.runtime.tree.Tree dupNode()
- Specified by:
dupNodein interfaceorg.antlr.runtime.tree.Tree- Overrides:
dupNodein classorg.antlr.runtime.tree.CommonTree
-
dupTreeNoActions
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.
-
getChildrenForDupTree
private static java.util.List<GrammarAST> getChildrenForDupTree(GrammarAST t)
-
dupTree
public static GrammarAST dupTree(GrammarAST t)
-
setTreeEnclosingRuleNameDeeply
public void setTreeEnclosingRuleNameDeeply(java.lang.String rname)
-
toStringList
public java.lang.String toStringList()
-
setTokenBoundaries
public void setTokenBoundaries(org.antlr.runtime.Token startToken, org.antlr.runtime.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.
-
getBlockALT
public GrammarAST getBlockALT(int i)
-
-