Class Tokenizer
java.lang.Object
studio.magemonkey.codex.util.eval.javaluator.Tokenizer
A String tokenizer that accepts delimiters that are greater than one character.
- Author:
- Jean-Marc Astesana
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Map<String, BracketPair> protected final Stringprotected final Map<String, BracketPair> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static PatterndelimitersToRegexp(List<String> delimiters) protected BracketPairgetBracketPair(String token) protected OperatorguessOperator(Token previous, List<Operator> candidates) When a token can be more than one operator (homonym operators), this method guesses the right operator.booleanTests whether this tokens trims the tokens returned bytokenize(String)method.voidsetTrimTokens(boolean trimTokens) Sets the trimTokens attribute.Converts a string into tokens.
-
Field Details
-
functions
-
operators
-
constants
-
functionArgumentSeparator
-
functionBrackets
-
expressionBrackets
-
-
Constructor Details
-
Tokenizer
public Tokenizer(List<String> delimiters, Map<String, Function> functions, Map<String, List<Operator>> operators, Map<String, Constant> constants, String functionArgumentSeparator, Map<String, BracketPair> functionBrackets, Map<String, BracketPair> expressionBrackets) Constructor.
By default, this tokenizer trims all the tokens.- Parameters:
delimiters- the delimiters of the tokenizer, usually, the operators symbols, the brackets and the function argument separator are used as delimiter in the string.functions-operators-constants-functionArgumentSeparator-functionBrackets-expressionBrackets-
-
-
Method Details
-
isTrimTokens
public boolean isTrimTokens()Tests whether this tokens trims the tokens returned bytokenize(String)method.- Returns:
- true if tokens are trimmed.
-
setTrimTokens
public void setTrimTokens(boolean trimTokens) Sets the trimTokens attribute.- Parameters:
trimTokens- true to have the tokens returned bytokenize(String)method trimmed.
Note that empty tokens are always omitted by this class.
-
delimitersToRegexp
-
tokenize
Converts a string into tokens.
Example: The result for the expression "-1+min(10,3)" evaluated for a DoubleEvaluator is an iterator on "-", "1", "+", "min", "(", "10", ",", "3", ")".- Parameters:
string- The string to be split into tokens- Returns:
- The tokens
-
toToken
-
getBracketPair
-
guessOperator
When a token can be more than one operator (homonym operators), this method guesses the right operator.
A very common case is the - sign in arithmetic computation which can be an unary or a binary operator, depending on what was the previous token.
Warning: maybe the arguments of this function are not enough to deal with all the cases. So, this part of the evaluation is in alpha state (method may change in the future).- Parameters:
previous- The last parsed tokens (the previous token in the infix expression we are evaluating).candidates- The candidate tokens.- Returns:
- A token
-