Package org.antlr.tool
Class GrammarAST
- java.lang.Object
-
- antlr.BaseAST
-
- org.antlr.tool.GrammarAST
-
- All Implemented Interfaces:
antlr.collections.AST,java.io.Serializable
public class GrammarAST extends antlr.BaseASTGrammars 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:
- Serialized Form
-
-
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.antlr.stringtemplate.StringTemplatecodeif this is a TOKEN_REF or RULE_REF node, this is the code StringTemplate generated for this node.(package private) static intcountjava.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.intruleStartTokenIndexIf this is a RULE node then track rule's start, stop tokens' index.intruleStopTokenIndexprotected IntSetsetValueIf this is a SET node, what are the elements?java.util.Map<java.lang.String,java.lang.Object>terminalOptionsantlr.TokentokenThis AST node was created from what token?
-
Constructor Summary
Constructors Constructor Description GrammarAST()GrammarAST(int t, java.lang.String txt)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static GrammarASTdup(antlr.collections.AST t)static GrammarASTdupListNoActions(GrammarAST t, GrammarAST parent)Duplicate tree including siblings of root.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.GrammarASTfindFirstType(int ttype)Return a reference to the first node (depth-first) that has token type ttype.java.lang.ObjectgetBlockOption(java.lang.String key)java.util.Map<java.lang.String,java.lang.Object>getBlockOptions()GrammarASTgetChild(int i)Get the ith child from 0GrammarAST[]getChildrenAsArray()intgetColumn()GrammarASTgetFirstChildWithType(int ttype)GrammarASTgetLastChild()GrammarASTgetLastSibling()intgetLine()DFAgetLookaheadDFA()NFAStategetNFAStartState()intgetNumberOfChildrenWithType(int ttype)IntSetgetSetValue()java.lang.StringgetText()antlr.TokengetToken()intgetType()booleanhasSameListStructure(antlr.collections.AST t)booleanhasSameTreeStructure(antlr.collections.AST t)See if tree has exact token types and structure; no textvoidinitialize(int i, java.lang.String s)voidinitialize(antlr.collections.AST ast)voidinitialize(antlr.Token token)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)voidsetColumn(int col)voidsetLine(int line)voidsetLookaheadDFA(DFA lookaheadDFA)voidsetNFAStartState(NFAState nfaStartState)java.lang.StringsetOption(java.util.Map options, java.util.Set legalOptions, Grammar grammar, java.lang.String key, java.lang.Object value)voidsetOptions(Grammar grammar, java.util.Map options)voidsetSetValue(IntSet setValue)java.lang.StringsetTerminalOption(Grammar grammar, java.lang.String key, java.lang.Object value)voidsetText(java.lang.String text)voidsetTreeEnclosingRuleNameDeeply(java.lang.String rname)voidsetType(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 Detail
-
count
static int count
-
ID
public int ID
-
token
public antlr.Token token
This AST node was created from what token?
-
enclosingRuleName
public java.lang.String enclosingRuleName
-
ruleStartTokenIndex
public int ruleStartTokenIndex
If this is a RULE node then track rule's start, stop tokens' index.
-
ruleStopTokenIndex
public int ruleStopTokenIndex
-
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.antlr.stringtemplate.StringTemplate code
if 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.
-
-
Method Detail
-
getBlockOptions
public java.util.Map<java.lang.String,java.lang.Object> getBlockOptions()
- Returns:
-
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)- Specified by:
initializein interfaceantlr.collections.AST- Specified by:
initializein classantlr.BaseAST
-
initialize
public void initialize(antlr.collections.AST ast)
- Specified by:
initializein interfaceantlr.collections.AST- Specified by:
initializein classantlr.BaseAST
-
initialize
public void initialize(antlr.Token token)
- Specified by:
initializein interfaceantlr.collections.AST- Specified by:
initializein classantlr.BaseAST
-
getLookaheadDFA
public DFA getLookaheadDFA()
-
setLookaheadDFA
public void setLookaheadDFA(DFA lookaheadDFA)
-
getToken
public antlr.Token getToken()
-
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 options, java.util.Set 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 options)
-
getText
public java.lang.String getText()
- Specified by:
getTextin interfaceantlr.collections.AST- Overrides:
getTextin classantlr.BaseAST
-
setType
public void setType(int type)
- Specified by:
setTypein interfaceantlr.collections.AST- Overrides:
setTypein classantlr.BaseAST
-
setText
public void setText(java.lang.String text)
- Specified by:
setTextin interfaceantlr.collections.AST- Overrides:
setTextin classantlr.BaseAST
-
getType
public int getType()
- Specified by:
getTypein interfaceantlr.collections.AST- Overrides:
getTypein classantlr.BaseAST
-
getLine
public int getLine()
- Specified by:
getLinein interfaceantlr.collections.AST- Overrides:
getLinein classantlr.BaseAST
-
getColumn
public int getColumn()
- Specified by:
getColumnin interfaceantlr.collections.AST- Overrides:
getColumnin classantlr.BaseAST
-
setLine
public void setLine(int line)
-
setColumn
public void setColumn(int col)
-
getSetValue
public IntSet getSetValue()
-
setSetValue
public void setSetValue(IntSet setValue)
-
getLastChild
public GrammarAST getLastChild()
-
getLastSibling
public GrammarAST getLastSibling()
-
getChild
public GrammarAST getChild(int i)
Get the ith child from 0
-
getFirstChildWithType
public GrammarAST getFirstChildWithType(int ttype)
-
getChildrenAsArray
public GrammarAST[] getChildrenAsArray()
-
findFirstType
public GrammarAST findFirstType(int ttype)
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
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
-
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
public static GrammarAST dup(antlr.collections.AST t)
-
dupListNoActions
public static GrammarAST dupListNoActions(GrammarAST t, GrammarAST parent)
Duplicate tree including siblings of root.
-
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.
-
setTreeEnclosingRuleNameDeeply
public void setTreeEnclosingRuleNameDeeply(java.lang.String rname)
-
-