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:
Tree
- Direct Known Subclasses:
ANTLRParser.GrammarASTErrorNode
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
FieldsModifier and TypeFieldDescriptionIf this is a BLOCK node, track options hereorg.stringtemplate.v4.ST
if this is a TOKEN_REF or RULE_REF node, this is the code ST generated for this node.(package private) static int
private static final GrammarAST
private static final GrammarAST
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.int
If this is a decision node, what is the lookahead DFA?What NFA start state was built from this node?This is used for TREE_BEGIN nodes to point into the NFA.int
if this is an ACTION node, this is the outermost enclosing alt num in rule.If this is a BLOCK node for a rewrite rule, track referenced elements here.protected IntSet
If this is a SET node, what are the elements?private String
Fields inherited from class org.antlr.runtime.tree.CommonTree
childIndex, parent, startIndex, stopIndex, token
Fields inherited from interface org.antlr.runtime.tree.Tree
INVALID_NODE
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
_findAllType
(int ttype, List<GrammarAST> nodes) descendants
(Tree root) descendants
(Tree root, boolean insertDownUpNodes) static GrammarAST
dupNode()
static GrammarAST
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.boolean
Make nodes unique based upon Token so we can add them to a Set; if not a GrammarAST, check type.findAllType
(int ttype) findFirstType
(int ttype) getBlockALT
(int i) getBlockOption
(String key) int
private static List
<GrammarAST> int
getLine()
In case we don't have a token payload, what is the line for errors?getText()
int
getType()
Return a token type; needed for tree parsingint
hashCode()
Make nodes unique based upon Token so we can add them to a Set; if not a GrammarAST, check type.boolean
hasSameTreeStructure
(Tree other) See if tree has exact token types and structure; no textvoid
initialize
(int i, String s) void
initialize
(Token token) void
initialize
(Tree ast) setBlockOption
(Grammar grammar, String key, Object value) Save the option key/value pair and process it; return the key or null if invalid option.void
setBlockOptions
(Map<String, Object> blockOptions) void
setCharPositionInLine
(int value) void
setLine
(int line) void
setLookaheadDFA
(DFA lookaheadDFA) void
setNFAStartState
(NFAState nfaStartState) setOption
(Map<String, Object> options, Set<String> legalOptions, Grammar grammar, String key, Object value) void
setOptions
(Grammar grammar, Map<String, Object> options) void
setSetValue
(IntSet setValue) setTerminalOption
(Grammar grammar, String key, Object value) void
void
setTokenBoundaries
(Token startToken, Token stopToken) Track start/stop token for subtree root created for a rule.void
void
setType
(int type) 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 Details
-
count
static int count -
ID
public int ID -
textOverride
-
enclosingRuleName
-
lookaheadDFA
If this is a decision node, what is the lookahead DFA? -
NFAStartState
What NFA start state was built from this node? -
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
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
If this is a SET node, what are the elements? -
blockOptions
If this is a BLOCK node, track options here -
rewriteRefsShallow
If this is a BLOCK node for a rewrite rule, track referenced elements here. Don't track elements in nested subrules. -
rewriteRefsDeep
-
terminalOptions
-
outerAltNum
public int outerAltNumif 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 codeif 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
-
DescendantUpNode
-
-
Constructor Details
-
GrammarAST
public GrammarAST() -
GrammarAST
-
GrammarAST
-
-
Method Details
-
getBlockOptions
-
setBlockOptions
- Parameters:
blockOptions
-
-
initialize
-
initialize
-
initialize
-
getLookaheadDFA
-
setLookaheadDFA
-
getNFAStartState
-
setNFAStartState
-
setBlockOption
Save the option key/value pair and process it; return the key or null if invalid option. -
setTerminalOption
-
setOption
-
getBlockOption
-
setOptions
-
getText
- Specified by:
getText
in interfaceTree
- Overrides:
getText
in classCommonTree
-
setType
public void setType(int type) -
setText
-
getType
public int getType()Description copied from interface:Tree
Return a token type; needed for tree parsing- Specified by:
getType
in interfaceTree
- Overrides:
getType
in classCommonTree
-
getLine
public int getLine()Description copied from interface:Tree
In case we don't have a token payload, what is the line for errors?- Specified by:
getLine
in interfaceTree
- Overrides:
getLine
in classCommonTree
-
getCharPositionInLine
public int getCharPositionInLine()- Specified by:
getCharPositionInLine
in interfaceTree
- Overrides:
getCharPositionInLine
in classCommonTree
-
setLine
public void setLine(int line) -
setCharPositionInLine
public void setCharPositionInLine(int value) -
getSetValue
-
setSetValue
-
getLastChild
-
getNextSibling
-
getLastSibling
-
getChildrenAsArray
-
descendants
-
descendants
-
findFirstType
-
findAllType
-
_findAllType
-
equals
Make nodes unique based upon Token so we can add them to a Set; if not a GrammarAST, check type. -
hashCode
public int hashCode()Make nodes unique based upon Token so we can add them to a Set; if not a GrammarAST, check type. -
hasSameTreeStructure
See if tree has exact token types and structure; no text -
dup
-
dupNode
- Specified by:
dupNode
in interfaceTree
- Overrides:
dupNode
in classCommonTree
-
dupTreeNoActions
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
-
dupTree
-
setTreeEnclosingRuleNameDeeply
-
toStringList
-
setTokenBoundaries
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
-