类 ASTUtil


  • public final class ASTUtil
    extends Object
    Provides utility methods for AST traversal and manipulation.
    作者:
    Joshua Davis, Steve Ebersole
    • 方法概要

      所有方法 静态方法 具体方法 已过时的方法 
      修饰符和类型 方法 说明
      static void appendChild​(antlr.collections.AST parent, antlr.collections.AST child)  
      static void appendSibling​(antlr.collections.AST n, antlr.collections.AST s)  
      static List collectChildren​(antlr.collections.AST root, ASTUtil.FilterPredicate predicate)  
      static antlr.collections.AST create​(antlr.ASTFactory astFactory, int type, String text)
      已过时。
      silly
      static antlr.collections.AST createBinarySubtree​(antlr.ASTFactory factory, int parentType, String parentText, antlr.collections.AST child1, antlr.collections.AST child2)
      Creates a 'binary operator' subtree, given the information about the parent and the two child nodes.
      static antlr.collections.AST createParent​(antlr.ASTFactory factory, int parentType, String parentText, antlr.collections.AST child)
      Creates a single parent of the specified child (i.e. a 'unary operator' subtree).
      static antlr.collections.AST createSibling​(antlr.ASTFactory astFactory, int type, String text, antlr.collections.AST prevSibling)
      Creates a single node AST as a sibling of the passed prevSibling, taking care to reorganize the tree correctly to account for this newly created node.
      static antlr.collections.AST createTree​(antlr.ASTFactory factory, antlr.collections.AST[] nestedChildren)  
      static antlr.collections.AST findPreviousSibling​(antlr.collections.AST parent, antlr.collections.AST child)
      Find the previous sibling in the parent for the given child.
      static antlr.collections.AST findTypeInChildren​(antlr.collections.AST parent, int type)
      Finds the first node of the specified type in the chain of children.
      static String[] generateTokenNameCache​(Class tokenTypeInterface)
      Method to generate a map of token type names, keyed by their token type values.
      static String getConstantName​(Class owner, int value)
      已过时。
      Use #getTokenTypeName instead
      static String getDebugString​(antlr.collections.AST n)
      Returns the 'list' representation with some brackets around it for debugging.
      static antlr.collections.AST getLastChild​(antlr.collections.AST n)
      Returns the last direct child of 'n'.
      static String getPathText​(antlr.collections.AST n)  
      static String getTokenTypeName​(Class tokenTypeInterface, int tokenType)
      Intended to retrieve the name of an AST token type based on the token type interface.
      static boolean hasExactlyOneChild​(antlr.collections.AST n)  
      static void insertChild​(antlr.collections.AST parent, antlr.collections.AST child)
      Inserts the child as the first child of the parent, all other children are shifted over to the 'right'.
      static antlr.collections.AST insertSibling​(antlr.collections.AST node, antlr.collections.AST prevSibling)
      Inserts a node into a child subtree as a particularly positioned sibling taking care to properly reorganize the tree to account for this new addition.
      static boolean isSubtreeChild​(antlr.collections.AST fixture, antlr.collections.AST test)
      Determine if a given node (test) is contained anywhere in the subtree of another given node (fixture).
      static void makeSiblingOfParent​(antlr.collections.AST parent, antlr.collections.AST child)
      Makes the child node a sibling of the parent, reconnecting all siblings.
    • 方法详细资料

      • create

        @Deprecated
        public static antlr.collections.AST create​(antlr.ASTFactory astFactory,
                                                   int type,
                                                   String text)
        已过时。
        silly
        Creates a single node AST.

        TODO : this is silly, remove it...

        参数:
        astFactory - The factory.
        type - The node type.
        text - The node text.
        返回:
        AST - A single node tree.
      • createSibling

        public static antlr.collections.AST createSibling​(antlr.ASTFactory astFactory,
                                                          int type,
                                                          String text,
                                                          antlr.collections.AST prevSibling)
        Creates a single node AST as a sibling of the passed prevSibling, taking care to reorganize the tree correctly to account for this newly created node.
        参数:
        astFactory - The factory.
        type - The node type.
        text - The node text.
        prevSibling - The previous sibling.
        返回:
        The created AST node.
      • insertSibling

        public static antlr.collections.AST insertSibling​(antlr.collections.AST node,
                                                          antlr.collections.AST prevSibling)
        Inserts a node into a child subtree as a particularly positioned sibling taking care to properly reorganize the tree to account for this new addition.
        参数:
        node - The node to insert
        prevSibling - The previous node at the sibling position where we want this node inserted.
        返回:
        The return is the same as the node parameter passed in.
      • createBinarySubtree

        public static antlr.collections.AST createBinarySubtree​(antlr.ASTFactory factory,
                                                                int parentType,
                                                                String parentText,
                                                                antlr.collections.AST child1,
                                                                antlr.collections.AST child2)
        Creates a 'binary operator' subtree, given the information about the parent and the two child nodes.
        参数:
        factory - The AST factory.
        parentType - The type of the parent node.
        parentText - The text of the parent node.
        child1 - The first child.
        child2 - The second child.
        返回:
        AST - A new sub-tree of the form "(parent child1 child2)"
      • createParent

        public static antlr.collections.AST createParent​(antlr.ASTFactory factory,
                                                         int parentType,
                                                         String parentText,
                                                         antlr.collections.AST child)
        Creates a single parent of the specified child (i.e. a 'unary operator' subtree).
        参数:
        factory - The AST factory.
        parentType - The type of the parent node.
        parentText - The text of the parent node.
        child - The child.
        返回:
        AST - A new sub-tree of the form "(parent child)"
      • createTree

        public static antlr.collections.AST createTree​(antlr.ASTFactory factory,
                                                       antlr.collections.AST[] nestedChildren)
      • isSubtreeChild

        public static boolean isSubtreeChild​(antlr.collections.AST fixture,
                                             antlr.collections.AST test)
        Determine if a given node (test) is contained anywhere in the subtree of another given node (fixture).
        参数:
        fixture - The node against which to test to be checked for children.
        test - The node to be tested as being a subtree child of the parent.
        返回:
        True if child is contained in the parent's collection of children.
      • findTypeInChildren

        public static antlr.collections.AST findTypeInChildren​(antlr.collections.AST parent,
                                                               int type)
        Finds the first node of the specified type in the chain of children.
        参数:
        parent - The parent
        type - The type to find.
        返回:
        The first node of the specified type, or null if not found.
      • getLastChild

        public static antlr.collections.AST getLastChild​(antlr.collections.AST n)
        Returns the last direct child of 'n'.
        参数:
        n - The parent
        返回:
        The last direct child of 'n'.
      • getDebugString

        public static String getDebugString​(antlr.collections.AST n)
        Returns the 'list' representation with some brackets around it for debugging.
        参数:
        n - The tree.
        返回:
        The list representation of the tree.
      • findPreviousSibling

        public static antlr.collections.AST findPreviousSibling​(antlr.collections.AST parent,
                                                                antlr.collections.AST child)
        Find the previous sibling in the parent for the given child.
        参数:
        parent - the parent node
        child - the child to find the previous sibling of
        返回:
        the previous sibling of the child
      • makeSiblingOfParent

        public static void makeSiblingOfParent​(antlr.collections.AST parent,
                                               antlr.collections.AST child)
        Makes the child node a sibling of the parent, reconnecting all siblings.
        参数:
        parent - the parent
        child - the child
      • getPathText

        public static String getPathText​(antlr.collections.AST n)
      • hasExactlyOneChild

        public static boolean hasExactlyOneChild​(antlr.collections.AST n)
      • appendSibling

        public static void appendSibling​(antlr.collections.AST n,
                                         antlr.collections.AST s)
      • insertChild

        public static void insertChild​(antlr.collections.AST parent,
                                       antlr.collections.AST child)
        Inserts the child as the first child of the parent, all other children are shifted over to the 'right'.
        参数:
        parent - the parent
        child - the new first child
      • appendChild

        public static void appendChild​(antlr.collections.AST parent,
                                       antlr.collections.AST child)
      • generateTokenNameCache

        public static String[] generateTokenNameCache​(Class tokenTypeInterface)
        Method to generate a map of token type names, keyed by their token type values.
        参数:
        tokenTypeInterface - The *TokenTypes interface (or implementor of said interface).
        返回:
        A compact map int -> tokenName in array format
      • getConstantName

        @Deprecated
        public static String getConstantName​(Class owner,
                                             int value)
        已过时。
        Use #getTokenTypeName instead
        Get the name of a constant defined on the given class which has the given value.

        Note, if multiple constants have this value, the first will be returned which is known to be different on different JVM implementations.

        参数:
        owner - The class which defines the constant
        value - The value of the constant.
        返回:
        The token type name, *or* the integer value if the name could not be found.
      • getTokenTypeName

        public static String getTokenTypeName​(Class tokenTypeInterface,
                                              int tokenType)
        Intended to retrieve the name of an AST token type based on the token type interface. However, this method can be used to look up the name of any constant defined on a class/interface based on the constant value. Note that if multiple constants have this value, the first will be returned which is known to be different on different JVM implementations.
        参数:
        tokenTypeInterface - The *TokenTypes interface (or one of its implementors).
        tokenType - The token type value.
        返回:
        The corresponding name.