Package software.amazon.smithy.syntax
Interface TokenTree
-
- All Superinterfaces:
FromSourceLocation
public interface TokenTree extends FromSourceLocation
Provides a labeled tree of tokens returned fromIdlTokenizer
.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 Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description void
appendChild(TokenTree tree)
Append a child to the tree.java.lang.String
concatTokens()
Get the tokens contained in the tree as a single concatenated string.static TokenTree
fromError(java.lang.String error)
Create an error tree with the givenerror
message.java.util.List<TokenTree>
getChildren()
Get direct children of the tree.int
getEndColumn()
Get the column the tree ends, starting at 1.int
getEndLine()
Get the line the tree ends, starting at 1.default java.lang.String
getError()
Get the error associated with the tree, ornull
if not present.int
getStartColumn()
Get the column the tree starts, starting at 1.int
getStartLine()
Get the line the tree starts, starting at 1.int
getStartPosition()
Get the absolute start position of the tree, starting at 0.TreeType
getType()
Get the token tree type.boolean
isEmpty()
Detect if the tree is empty (that is, a non-leaf that has no children or tokens).static TokenTree
of(IdlTokenizer tokenizer)
Create the root of a TokenTree from anIdlTokenizer
.static TokenTree
of(IdlTokenizer tokenizer, TreeType type)
Create a TokenTree of the givenTreeType
from anIdlTokenizer
.static TokenTree
of(CapturedToken token)
Create a leaf tree from a single token.static TokenTree
of(TreeType type)
Create an empty tree of a specifictype
.boolean
removeChild(TokenTree tree)
Remove a child tree by referential equality.boolean
replaceChild(TokenTree find, TokenTree replacement)
Replace a matching child with the given replacement using referential equality.java.util.stream.Stream<CapturedToken>
tokens()
Get a flattened stream of all captured tokens contained within the tree.default TreeCursor
zipper()
Create a zipper for the current tree node, treating it as the root of the tree.-
Methods inherited from interface software.amazon.smithy.model.FromSourceLocation
getSourceLocation
-
-
-
-
Method Detail
-
of
static TokenTree of(IdlTokenizer tokenizer)
Create the root of a TokenTree from anIdlTokenizer
.- 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 givenTreeType
from anIdlTokenizer
.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 specifictype
.- 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 givenerror
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, ornull
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.
-
-