Package redempt.redlex.data
Class Token
java.lang.Object
redempt.redlex.data.Token
- Direct Known Subclasses:
ObjectToken
Represents a Token created by a Lexer. Acts as a node in a tree.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addChildren
(Token... tokens) Appends the given tokens to this token's childrenFind all tokens in the tree by name, breadth-firstallByName
(TraversalOrder order, String name) Find all tokens in the tree by nameallByNames
(String... names) Find all tokens in the tree by any number of names and create a map, breadth-firstallByNames
(TraversalOrder order, String... names) Find all tokens in the tree by any number of names and create a mapvoid
cull
(TokenFilter... filters) Culls tokens in the tokens which are descendants of this one using filters.firstByName
(String name) Find the first token in the tree by name (breadth-first)Token[]
int
getEnd()
int
getIndex()
getNext()
int
getStart()
getType()
getValue()
joinChildren
(String sep) Joins the String values of the children of this node separated by a delimiterjoinLeaves
(String sep) Joins the leaf nodes of this subtree separated by a delimiterint
length()
void
Removes this token from its parent, replacing it with its childrenvoid
remove()
Removes this token from its parentvoid
removeChildren
(int start, int end) Removes a range of children from this token by indexvoid
removeChildren
(Token... children) Removes the given children from this tokenvoid
Replaces this token's parent with itselfReplaces this token in its parent with a token wrapper for the given object, or a token if the passed object is a tokenvoid
setChildren
(Token[] children) Sets the children of this Tokenvoid
Sets the String value of this TokensplitChildren
(String name) Splits child tokens on a given token by nametoString()
void
walk
(TraversalOrder order, Consumer<Token> forEachNode) Walks this Token tree in the given order
-
Field Details
-
EMPTY
-
value
-
-
Constructor Details
-
Token
-
Token
-
-
Method Details
-
setChildren
Sets the children of this Token- Parameters:
children
- The children of this Token
-
getChildren
- Returns:
- The children of this Token
-
getNext
- Returns:
- The next sibling of this Token, or null if none exists
-
getPrevious
- Returns:
- The previous sibling of this Token, or null if none exists
-
getParent
- Returns:
- The parent of this Token, or null if none exists
-
getIndex
public int getIndex()- Returns:
- The index of this Token in the array of its parent's children
-
getType
- Returns:
- The type of this Token
-
getValue
- Returns:
- The String value of this Token
-
getBaseString
- Returns:
- The base String this token was parsed from
-
length
public int length()- Returns:
- The length of this Token's String value
-
getEnd
public int getEnd()- Returns:
- The ending index of this Token
-
getStart
public int getStart()- Returns:
- The starting index of this Token
-
setValue
Sets the String value of this Token- Parameters:
value
- The String value to set
-
joinChildren
Joins the String values of the children of this node separated by a delimiter- Parameters:
sep
- The delimiter to separate values with- Returns:
- The joined string
-
joinLeaves
Joins the leaf nodes of this subtree separated by a delimiter- Parameters:
sep
- The delimiter to separate values with- Returns:
- The joined string
-
remove
public void remove()Removes this token from its parent -
removeChildren
Removes the given children from this token- Parameters:
children
- The children to remove
-
removeChildren
public void removeChildren(int start, int end) Removes a range of children from this token by index- Parameters:
start
- The starting index to remove children from, inclusiveend
- The ending index to remove children to, exclusive
-
replaceWith
Replaces this token in its parent with a token wrapper for the given object, or a token if the passed object is a token- Parameters:
o
- The object to replace this token with- Returns:
- The Token this one was replaced with
-
replaceParent
public void replaceParent()Replaces this token's parent with itself -
liftChildren
public void liftChildren()Removes this token from its parent, replacing it with its children -
firstByName
Find the first token in the tree by name (breadth-first)- Parameters:
name
- The name of the token to search for- Returns:
- The first token found, or null
-
allByName
Find all tokens in the tree by name- Parameters:
order
- The order to traverse the tree inname
- The name of the token to search for- Returns:
- All tokens found
-
allByName
Find all tokens in the tree by name, breadth-first- Parameters:
name
- The name of the token to search for- Returns:
- All tokens found
-
allByNames
Find all tokens in the tree by any number of names and create a map, breadth-first- Parameters:
names
- The names to look for- Returns:
- A map of names to the tokens with those names
-
allByNames
Find all tokens in the tree by any number of names and create a map- Parameters:
order
- The order to traverse the tree innames
- The names to look for- Returns:
- A map of names to the tokens with those names
-
walk
Walks this Token tree in the given order- Parameters:
order
- The order to walk the tree inforEachNode
- The lambda to apply to each token
-
getObject
- Returns:
- Nothing, unless this is an ObjectToken
-
splitChildren
Splits child tokens on a given token by name- Parameters:
name
- The name of the token to split on- Returns:
- A 2d list of tokens split by the given token
-
cull
Culls tokens in the tokens which are descendants of this one using filters.- Parameters:
filters
- The filters to cull tokens with
-
addChildren
Appends the given tokens to this token's children- Parameters:
tokens
- The tokens to add
-
toString
-