org.antlr.tool
Class GrammarAST

java.lang.Object
  extended by org.antlr.runtime.tree.BaseTree
      extended by org.antlr.runtime.tree.CommonTree
          extended by org.antlr.tool.GrammarAST
All Implemented Interfaces:
Tree

public class GrammarAST
extends CommonTree

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
protected  Map<String,Object> blockOptions
          If this is a BLOCK node, track options here
 org.stringtemplate.v4.ST code
          if this is a TOKEN_REF or RULE_REF node, this is the code ST generated for this node.
 String enclosingRuleName
           
 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.
 int ID
           
 DFA lookaheadDFA
          If this is a decision node, what is the lookahead DFA?
 NFAState NFAStartState
          What NFA start state was built from this node?
 NFAState NFATreeDownState
          This is used for TREE_BEGIN nodes to point into the NFA.
 int outerAltNum
          if this is an ACTION node, this is the outermost enclosing alt num in rule.
 Set<GrammarAST> rewriteRefsDeep
           
 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?
 Map<String,Object> terminalOptions
           
 
Fields inherited from class org.antlr.runtime.tree.CommonTree
childIndex, parent, startIndex, stopIndex, token
 
Fields inherited from class org.antlr.runtime.tree.BaseTree
children
 
Fields inherited from interface org.antlr.runtime.tree.Tree
INVALID_NODE
 
Constructor Summary
GrammarAST()
           
GrammarAST(int t, String txt)
           
GrammarAST(Token token)
           
 
Method Summary
 void _findAllType(int ttype, List<GrammarAST> nodes)
           
static List<Tree> descendants(Tree root)
           
static List<Tree> descendants(Tree root, boolean insertDownUpNodes)
           
static GrammarAST dup(Tree t)
           
 Tree dupNode()
           
static GrammarAST dupTree(GrammarAST t)
           
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 equals(Object ast)
          Make nodes unique based upon Token so we can add them to a Set; if not a GrammarAST, check type.
 List<GrammarAST> findAllType(int ttype)
           
 GrammarAST findFirstType(int ttype)
           
 GrammarAST getBlockALT(int i)
           
 Object getBlockOption(String key)
           
 Map<String,Object> getBlockOptions()
           
 int getCharPositionInLine()
           
 GrammarAST[] getChildrenAsArray()
           
 GrammarAST getLastChild()
           
 GrammarAST getLastSibling()
           
 int getLine()
           
 DFA getLookaheadDFA()
           
 GrammarAST getNextSibling()
           
 NFAState getNFAStartState()
           
 IntSet getSetValue()
           
 String getText()
           
 int getType()
           
 int 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 text
 void initialize(int i, String s)
           
 void initialize(Token token)
           
 void initialize(Tree ast)
           
 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.
 void setBlockOptions(Map<String,Object> blockOptions)
           
 void setCharPositionInLine(int value)
           
 void setLine(int line)
           
 void setLookaheadDFA(DFA lookaheadDFA)
           
 void setNFAStartState(NFAState nfaStartState)
           
 String setOption(Map options, Set legalOptions, Grammar grammar, String key, Object value)
           
 void setOptions(Grammar grammar, Map options)
           
 void setSetValue(IntSet setValue)
           
 String setTerminalOption(Grammar grammar, String key, Object value)
           
 void setText(String text)
           
 void setTokenBoundaries(Token startToken, Token stopToken)
          Track start/stop token for subtree root created for a rule.
 void setTreeEnclosingRuleNameDeeply(String rname)
           
 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
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ID

public int ID

enclosingRuleName

public 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 Map<String,Object> blockOptions
If this is a BLOCK node, track options here


rewriteRefsShallow

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.


rewriteRefsDeep

public Set<GrammarAST> rewriteRefsDeep

terminalOptions

public Map<String,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.

Constructor Detail

GrammarAST

public GrammarAST()

GrammarAST

public GrammarAST(int t,
                  String txt)

GrammarAST

public GrammarAST(Token token)
Method Detail

getBlockOptions

public Map<String,Object> getBlockOptions()
Returns:

setBlockOptions

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

initialize

public void initialize(int i,
                       String s)

initialize

public void initialize(Tree ast)

initialize

public void initialize(Token token)

getLookaheadDFA

public DFA getLookaheadDFA()

setLookaheadDFA

public void setLookaheadDFA(DFA lookaheadDFA)

getNFAStartState

public NFAState getNFAStartState()

setNFAStartState

public void setNFAStartState(NFAState nfaStartState)

setBlockOption

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.


setTerminalOption

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

setOption

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

getBlockOption

public Object getBlockOption(String key)

setOptions

public void setOptions(Grammar grammar,
                       Map options)

getText

public String getText()
Specified by:
getText in interface Tree
Overrides:
getText in class CommonTree

setType

public void setType(int type)

setText

public void setText(String text)

getType

public int getType()
Specified by:
getType in interface Tree
Overrides:
getType in class CommonTree

getLine

public int getLine()
Specified by:
getLine in interface Tree
Overrides:
getLine in class CommonTree

getCharPositionInLine

public int getCharPositionInLine()
Specified by:
getCharPositionInLine in interface Tree
Overrides:
getCharPositionInLine in class 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 List<Tree> descendants(Tree root)

descendants

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

findFirstType

public GrammarAST findFirstType(int ttype)

findAllType

public List<GrammarAST> findAllType(int ttype)

_findAllType

public void _findAllType(int ttype,
                         List<GrammarAST> nodes)

equals

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

Overrides:
equals in class 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:
hashCode in class Object

hasSameTreeStructure

public boolean hasSameTreeStructure(Tree other)
See if tree has exact token types and structure; no text


dup

public static GrammarAST dup(Tree t)

dupNode

public Tree dupNode()
Specified by:
dupNode in interface Tree
Overrides:
dupNode in class 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.


dupTree

public static GrammarAST dupTree(GrammarAST t)

setTreeEnclosingRuleNameDeeply

public void setTreeEnclosingRuleNameDeeply(String rname)

setTokenBoundaries

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.


getBlockALT

public GrammarAST getBlockALT(int i)


Copyright © 2011. All Rights Reserved.