Class TNode

  • All Implemented Interfaces:
    antlr.collections.AST, ASTLocusTag.ASTLocusTagProvider, Serializable

    public class TNode
    extends antlr.CommonAST
    implements ASTLocusTag.ASTLocusTagProvider
    Class TNode is an implementation of the AST interface and adds many useful features: It is double-linked for reverse searching. (this is currently incomplete, in that method doubleLink() must be called after any changes to the tree to maintain the reverse links). It can store a definition node (defNode), so that nodes such as scoped names can refer to the node that defines the name. It stores line numbers for nodes. Searches for parents and children of a tree can be done based on their type. The tree can be printed to System.out using a lisp-style syntax.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      TNode()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addSibling​(antlr.collections.AST node)
      add the new node as a new sibling, inserting it ahead of any existing next sibling.
      TNode deepCopy()
      return a deep copy of this node, and all sub nodes.
      TNode deepCopyWithRightSiblings()
      return a deep copy of this node, all sub nodes, and right siblings.
      void doubleLink()
      set up reverse links between this node and its first child and its first sibling, and link those as well
      TNode firstChildOfType​(int type)
      find the first child of the node of the given type, return null on failure
      TNode firstSiblingOfType​(int type)
      find the first sibling of the node of the given type, return null on failure
      String getAllChildrenText​(String name)
      Returns the text for this node, its children and siblings.
      ASTLocusTag getASTLocusTag()
      Returns this instance's ASTLocusTag, if available, otherwise returns null.
      Object getAttribute​(String attrName)
      lookup the attribute name in the attribute table.
      Hashtable<String,​Object> getAttributesTable()
      get the hashtable that holds attribute values.
      TNode getDefNode()
      return the def node for this node
      TNode getFirstSibling()
      return the first sibling of this node, which is this if the prev sibling is null
      TNode getLastChild()
      return the last child of this node, or null if there is none
      TNode getLastSibling()
      return the last sibling of this node, which is this if the next sibling is null
      int getLineNum()
      Get the line number for this node.
      int getLocalLineNum()  
      boolean getMarker()
      Get the marker value for this node.
      static String getNameForType​(int t)
      converts an int tree token type to a name.
      TNode getParent()
      return the parent node of this node
      String getText()
      Get the token text for this node
      int getType()
      Get the token type for this node
      void initialize​(antlr.collections.AST tr)  
      void initialize​(antlr.Token token)  
      int numberOfChildren()
      return the number of children of this node
      TNode parentOfType​(int type)
      find first parent of the given type, return null on failure
      protected static void printASTNode​(antlr.collections.AST t, int indent)
      protected method that does the work of printing
      static void printTree​(antlr.collections.AST t)
      print given tree to System.out
      void removeSelf()
      remove this node from the tree, resetting sibling and parent pointers as necessary.
      void setAttribute​(String attrName, Object value)
      set an attribute in the attribute table.
      void setDefNode​(TNode n)
      set the def node for this node
      void setLineNum​(int lineNum_)
      Set the line number for this node
      void setMarker​(boolean marker_)
      Set the marker value for this node.
      void setText​(String text_)
      Set the token text for this node
      static void setTokenVocabulary​(String s)
      Set the token vocabulary to a tokentypes class generated by antlr.
      void setType​(int ttype_)
      Set the token type for this node
      String toString()
      return a short string representation of the node
      • Methods inherited from class antlr.CommonAST

        initialize
      • Methods inherited from class antlr.BaseAST

        addChild, decode, encode, equals, equalsList, equalsListPartial, equalsTree, equalsTreePartial, findAll, findAllPartial, getColumn, getFirstChild, getLine, getNextSibling, getNumberOfChildren, getTokenNames, removeChildren, setFirstChild, setNextSibling, setVerboseStringConversion, toStringList, toStringTree, xmlSerialize, xmlSerializeNode, xmlSerializeRootClose, xmlSerializeRootOpen
    • Field Detail

      • ttype

        protected int ttype
      • lineNum

        protected int lineNum
      • defNode

        protected TNode defNode
      • left

        protected TNode left
      • marker

        protected boolean marker
    • Constructor Detail

      • TNode

        public TNode()
    • Method Detail

      • setTokenVocabulary

        public static void setTokenVocabulary​(String s)
        Set the token vocabulary to a tokentypes class generated by antlr.
      • initialize

        public void initialize​(antlr.Token token)
        Specified by:
        initialize in interface antlr.collections.AST
        Overrides:
        initialize in class antlr.CommonAST
      • initialize

        public void initialize​(antlr.collections.AST tr)
        Specified by:
        initialize in interface antlr.collections.AST
        Overrides:
        initialize in class antlr.CommonAST
      • getType

        public int getType()
        Get the token type for this node
        Specified by:
        getType in interface antlr.collections.AST
        Overrides:
        getType in class antlr.CommonAST
      • setType

        public void setType​(int ttype_)
        Set the token type for this node
        Specified by:
        setType in interface antlr.collections.AST
        Overrides:
        setType in class antlr.CommonAST
      • getMarker

        public boolean getMarker()
        Get the marker value for this node. This member is a general-use marker.
      • setMarker

        public void setMarker​(boolean marker_)
        Set the marker value for this node. This property is a general-use boolean marker.
      • getAttributesTable

        public Hashtable<String,​Object> getAttributesTable()
        get the hashtable that holds attribute values.
      • setAttribute

        public void setAttribute​(String attrName,
                                 Object value)
        set an attribute in the attribute table.
      • getAttribute

        public Object getAttribute​(String attrName)
        lookup the attribute name in the attribute table. If the value does not exist, it returns null.
      • getLineNum

        public int getLineNum()
        Get the line number for this node. If the line number is 0, search for a non-zero line num among children
      • getLocalLineNum

        public int getLocalLineNum()
      • setLineNum

        public void setLineNum​(int lineNum_)
        Set the line number for this node
      • getText

        public String getText()
        Get the token text for this node
        Specified by:
        getText in interface antlr.collections.AST
        Overrides:
        getText in class antlr.CommonAST
      • setText

        public void setText​(String text_)
        Set the token text for this node
        Specified by:
        setText in interface antlr.collections.AST
        Overrides:
        setText in class antlr.CommonAST
      • getAllChildrenText

        public String getAllChildrenText​(String name)
        Returns the text for this node, its children and siblings.

        Implementation converts the AST LISP notation to serialized form.

      • getLastChild

        public TNode getLastChild()
        return the last child of this node, or null if there is none
      • getLastSibling

        public TNode getLastSibling()
        return the last sibling of this node, which is this if the next sibling is null
      • getFirstSibling

        public TNode getFirstSibling()
        return the first sibling of this node, which is this if the prev sibling is null
      • getParent

        public TNode getParent()
        return the parent node of this node
      • addSibling

        public void addSibling​(antlr.collections.AST node)
        add the new node as a new sibling, inserting it ahead of any existing next sibling. This method maintains double-linking. if node is null, nothing happens. If the node has siblings, then they are added in as well.
      • numberOfChildren

        public int numberOfChildren()
        return the number of children of this node
      • removeSelf

        public void removeSelf()
        remove this node from the tree, resetting sibling and parent pointers as necessary. This method maintains double-linking
      • getDefNode

        public TNode getDefNode()
        return the def node for this node
      • setDefNode

        public void setDefNode​(TNode n)
        set the def node for this node
      • deepCopy

        public TNode deepCopy()
        return a deep copy of this node, and all sub nodes. New tree is doubleLinked, with no parent or siblings. Marker value is not copied!
      • deepCopyWithRightSiblings

        public TNode deepCopyWithRightSiblings()
        return a deep copy of this node, all sub nodes, and right siblings. New tree is doubleLinked, with no parent or left siblings. defNode is not copied
      • toString

        public String toString()
        return a short string representation of the node
        Specified by:
        toString in interface antlr.collections.AST
        Overrides:
        toString in class antlr.BaseAST
      • printTree

        public static void printTree​(antlr.collections.AST t)
        print given tree to System.out
      • printASTNode

        protected static void printASTNode​(antlr.collections.AST t,
                                           int indent)
        protected method that does the work of printing
      • getNameForType

        public static String getNameForType​(int t)
        converts an int tree token type to a name. Does this by reflecting on nsdidl.IDLTreeTokenTypes, and is dependent on how ANTLR 2.00 outputs that class.
      • doubleLink

        public void doubleLink()
        set up reverse links between this node and its first child and its first sibling, and link those as well
      • parentOfType

        public TNode parentOfType​(int type)
        find first parent of the given type, return null on failure
      • firstChildOfType

        public TNode firstChildOfType​(int type)
        find the first child of the node of the given type, return null on failure
      • firstSiblingOfType

        public TNode firstSiblingOfType​(int type)
        find the first sibling of the node of the given type, return null on failure