Package org.antlr.tool
Class GrammarAST
java.lang.Object
antlr.BaseAST
org.antlr.tool.GrammarAST
- All Implemented Interfaces:
antlr.collections.AST
,Serializable
public class GrammarAST
extends antlr.BaseAST
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. ;)
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionIf this is a BLOCK node, track options hereorg.antlr.stringtemplate.StringTemplate
if this is a TOKEN_REF or RULE_REF node, this is the code StringTemplate generated for this node.(package private) static int
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.int
If this is a RULE node then track rule's start, stop tokens' index.int
protected IntSet
If this is a SET node, what are the elements?antlr.Token
This AST node was created from what token?Fields inherited from class antlr.BaseAST
down, right
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic GrammarAST
dup
(antlr.collections.AST t) static GrammarAST
dupListNoActions
(GrammarAST t, GrammarAST parent) Duplicate tree including siblings of root.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.findFirstType
(int ttype) Return a reference to the first node (depth-first) that has token type ttype.getBlockOption
(String key) getChild
(int i) Get the ith child from 0int
getFirstChildWithType
(int ttype) int
getLine()
int
getNumberOfChildrenWithType
(int ttype) getText()
antlr.Token
getToken()
int
getType()
boolean
hasSameListStructure
(antlr.collections.AST t) boolean
hasSameTreeStructure
(antlr.collections.AST t) See if tree has exact token types and structure; no textvoid
initialize
(int i, String s) void
initialize
(antlr.collections.AST ast) void
initialize
(antlr.Token token) 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
setColumn
(int col) void
setLine
(int line) void
setLookaheadDFA
(DFA lookaheadDFA) void
setNFAStartState
(NFAState nfaStartState) void
setOptions
(Grammar grammar, Map options) void
setSetValue
(IntSet setValue) setTerminalOption
(Grammar grammar, String key, Object value) void
void
void
setType
(int type) Methods inherited from class antlr.BaseAST
addChild, decode, encode, equals, equalsList, equalsListPartial, equalsTree, equalsTreePartial, findAll, findAllPartial, getFirstChild, getNextSibling, getNumberOfChildren, getTokenNames, removeChildren, setFirstChild, setNextSibling, setVerboseStringConversion, toString, toStringList, toStringTree, xmlSerialize, xmlSerializeNode, xmlSerializeRootClose, xmlSerializeRootOpen
-
Field Details
-
count
static int count -
ID
public int ID -
token
public antlr.Token tokenThis AST node was created from what token? -
enclosingRuleName
-
ruleStartTokenIndex
public int ruleStartTokenIndexIf this is a RULE node then track rule's start, stop tokens' index. -
ruleStopTokenIndex
public int ruleStopTokenIndex -
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.antlr.stringtemplate.StringTemplate codeif this is a TOKEN_REF or RULE_REF node, this is the code StringTemplate generated for this node. We need to update it later to add a label if someone does $tokenref or $ruleref in an action.
-
-
Constructor Details
-
GrammarAST
public GrammarAST() -
GrammarAST
-
-
Method Details
-
getBlockOptions
- Returns:
-
setBlockOptions
- Parameters:
blockOptions
-
-
initialize
- Specified by:
initialize
in interfaceantlr.collections.AST
- Specified by:
initialize
in classantlr.BaseAST
-
initialize
public void initialize(antlr.collections.AST ast) - Specified by:
initialize
in interfaceantlr.collections.AST
- Specified by:
initialize
in classantlr.BaseAST
-
initialize
public void initialize(antlr.Token token) - Specified by:
initialize
in interfaceantlr.collections.AST
- Specified by:
initialize
in classantlr.BaseAST
-
getLookaheadDFA
-
setLookaheadDFA
-
getToken
public antlr.Token getToken() -
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 interfaceantlr.collections.AST
- Overrides:
getText
in classantlr.BaseAST
-
setType
public void setType(int type) - Specified by:
setType
in interfaceantlr.collections.AST
- Overrides:
setType
in classantlr.BaseAST
-
setText
- Specified by:
setText
in interfaceantlr.collections.AST
- Overrides:
setText
in classantlr.BaseAST
-
getType
public int getType()- Specified by:
getType
in interfaceantlr.collections.AST
- Overrides:
getType
in classantlr.BaseAST
-
getLine
public int getLine()- Specified by:
getLine
in interfaceantlr.collections.AST
- Overrides:
getLine
in classantlr.BaseAST
-
getColumn
public int getColumn()- Specified by:
getColumn
in interfaceantlr.collections.AST
- Overrides:
getColumn
in classantlr.BaseAST
-
setLine
public void setLine(int line) -
setColumn
public void setColumn(int col) -
getSetValue
-
setSetValue
-
getLastChild
-
getLastSibling
-
getChild
Get the ith child from 0 -
getFirstChildWithType
-
getChildrenAsArray
-
findFirstType
Return a reference to the first node (depth-first) that has token type ttype. Assume 'this' is a root node; don't visit siblings of root. Return null if no node found with ttype. -
getNumberOfChildrenWithType
public int getNumberOfChildrenWithType(int ttype) -
equals
Make nodes unique based upon Token so we can add them to a Set; if not a GrammarAST, check type. -
hasSameTreeStructure
public boolean hasSameTreeStructure(antlr.collections.AST t) See if tree has exact token types and structure; no text -
hasSameListStructure
public boolean hasSameListStructure(antlr.collections.AST t) -
dup
-
dupListNoActions
Duplicate tree including siblings of root. -
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. -
setTreeEnclosingRuleNameDeeply
-