Package software.amazon.smithy.syntax
Interface TokenTree
- All Superinterfaces:
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 Summary
Modifier and TypeMethodDescriptionvoid
appendChild
(TokenTree tree) Append a child to the tree.Get the tokens contained in the tree as a single concatenated string.static TokenTree
Create an error tree with the givenerror
message.Get direct children of the tree.int
Get the column the tree ends, starting at 1.int
Get the line the tree ends, starting at 1.default String
getError()
Get the error associated with the tree, ornull
if not present.int
Get the column the tree starts, starting at 1.int
Get the line the tree starts, starting at 1.int
Get the absolute start position of the tree, starting at 0.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
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.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 Details
-
of
Create the root of a TokenTree from anIdlTokenizer
.- Parameters:
tokenizer
- Tokenizer to traverse.- Returns:
- Returns the root of the created tree.
-
of
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
Create a leaf tree from a single token.- Parameters:
token
- Token to wrap into a tree.- Returns:
- Returns the created tree.
-
of
Create an empty tree of a specifictype
.- Parameters:
type
- Tree type to create.- Returns:
- Returns the created tree.
-
fromError
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
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
Append a child to the tree.- Parameters:
tree
- Tree to append.- Throws:
UnsupportedOperationException
- if the tree is a leaf.
-
removeChild
Remove a child tree by referential equality.- Parameters:
tree
- Tree to remove.- Returns:
- Return true only if this child was found and removed.
-
replaceChild
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
Stream<CapturedToken> tokens()Get a flattened stream of all captured tokens contained within the tree.- Returns:
- Returns the contained tokens.
-
concatTokens
String concatTokens()Get the tokens contained in the tree as a single concatenated string.- Returns:
- Returns a concatenated string of tokens.
-
getError
Get the error associated with the tree, ornull
if not present.- Returns:
- Returns the nullable error message.
-
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.
-