Interface TokenTree

  • All Superinterfaces:
    FromSourceLocation

    public interface TokenTree
    extends FromSourceLocation
    Provides a labeled tree of tokens returned from IdlTokenizer.

    This abstraction is a kind of parse tree based on lexer tokens, with the key difference being it bottoms-out at IdlToken values rather than the more granular grammar productions for identifiers, strings, etc.

    Each consumed IDL token is present in the tree, and grouped together into nodes with labels defined by its TreeType.

    • Method Detail

      • of

        static TokenTree of​(IdlTokenizer tokenizer)
        Create the root of a TokenTree from an IdlTokenizer.
        Parameters:
        tokenizer - Tokenizer to traverse.
        Returns:
        Returns the root of the created tree.
      • of

        static TokenTree of​(IdlTokenizer tokenizer,
                            TreeType type)
        Create a TokenTree of the given TreeType from an IdlTokenizer.

        The following example shows how to create a TokenTree for a trait.

        
         IdlTokenizer tokenizer = IdlTokenizer.create("@someTrait");
         TokenTree traitTree = TokenTree.of(tokenizer, TreeType.TRAIT);
         
        Parameters:
        tokenizer - Tokenizer to traverse.
        type - Type of tree to create.
        Returns:
        Returns the created tree.
      • of

        static TokenTree of​(CapturedToken token)
        Create a leaf tree from a single token.
        Parameters:
        token - Token to wrap into a tree.
        Returns:
        Returns the created tree.
      • of

        static TokenTree of​(TreeType type)
        Create an empty tree of a specific type.
        Parameters:
        type - Tree type to create.
        Returns:
        Returns the created tree.
      • fromError

        static TokenTree fromError​(java.lang.String error)
        Create an error tree with the given error message.
        Parameters:
        error - Error message.
        Returns:
        Returns the created tree.
      • getType

        TreeType getType()
        Get the token tree type.
        Returns:
        Returns the type.
      • getChildren

        java.util.List<TokenTree> getChildren()
        Get direct children of the tree.
        Returns:
        Returns direct children.
      • isEmpty

        boolean isEmpty()
        Detect if the tree is empty (that is, a non-leaf that has no children or tokens).
        Returns:
        Return true if the tree has no children or tokens.
      • appendChild

        void appendChild​(TokenTree tree)
        Append a child to the tree.
        Parameters:
        tree - Tree to append.
        Throws:
        java.lang.UnsupportedOperationException - if the tree is a leaf.
      • removeChild

        boolean removeChild​(TokenTree tree)
        Remove a child tree by referential equality.
        Parameters:
        tree - Tree to remove.
        Returns:
        Return true only if this child was found and removed.
      • replaceChild

        boolean replaceChild​(TokenTree find,
                             TokenTree replacement)
        Replace a matching child with the given replacement using referential equality.
        Parameters:
        find - Child to find and replace, using referential equality.
        replacement - Replacement to use instead.
        Returns:
        Returns true only if a child was replaced.
      • tokens

        java.util.stream.Stream<CapturedToken> tokens()
        Get a flattened stream of all captured tokens contained within the tree.
        Returns:
        Returns the contained tokens.
      • concatTokens

        java.lang.String concatTokens()
        Get the tokens contained in the tree as a single concatenated string.
        Returns:
        Returns a concatenated string of tokens.
      • getError

        default java.lang.String getError()
        Get the error associated with the tree, or null if not present.
        Returns:
        Returns the nullable error message.
      • zipper

        default TreeCursor zipper()
        Create a zipper for the current tree node, treating it as the root of the tree.
        Returns:
        Returns the zipper cursor for the current node.
      • getStartPosition

        int getStartPosition()
        Get the absolute start position of the tree, starting at 0.
        Returns:
        Returns the start position of this tree.
      • getStartLine

        int getStartLine()
        Get the line the tree starts, starting at 1.
        Returns:
        Returns the start line.
      • getStartColumn

        int getStartColumn()
        Get the column the tree starts, starting at 1.
        Returns:
        Returns the start column.
      • getEndLine

        int getEndLine()
        Get the line the tree ends, starting at 1.
        Returns:
        Returns the end line.
      • getEndColumn

        int getEndColumn()
        Get the column the tree ends, starting at 1.
        Returns:
        Returns the end column.