de.unkrig.commons.text.expression
Class Parser<T,E extends java.lang.Exception>
java.lang.Object
de.unkrig.commons.text.parser.AbstractParser<Scanner.TokenType>
de.unkrig.commons.text.expression.Parser<T,E>
- Type Parameters:
T - The type returned by parse()E - The exception thrown by the abstract handlers (e.g. conditional(Object, Object, Object))
public abstract class Parser<T,E extends java.lang.Exception>
- extends AbstractParser<Scanner.TokenType>
Parses an expression like
s == "abc" && (c == 'b' || !b == true)
Supported operators (in ascending priority) are:
a ? b : c
-
b if a, converted to boolean, is true, otherwise
c.
a || b
-
a if a, converted to boolean, is true, otherwise
b. (Equivalent to 'a ? a : b', except that a is evaluated only once.)
a && b
-
b if a, converted to boolean, is true, otherwise
false. (Equivalent to 'a ? b : false'.)
== != < <= > >=
-
Arithmetic comparison (if both operands are numeric) or lexicographic comparison (otherwise).
=*
-
Wildcard matching/replacing (see
Glob), evaluates to the replacement.
=~
-
Regex matching/replacing (see
Pattern), evaluates to the replacement.
+ -
-
Addition (if both operands are numeric) or string concatenation, subtraction.
* / %
-
Multiplication, division, remainder (modulo).
a instanceof reference-type
true iff a is non-null and a subtype of reference-type
(...) ! -
-
Grouping, logical negation, arithmetic negation.
Primaries are:
"abc"
- String literal
123
- Integer literal
123L 123l
- Long literal
1F 1.2E+3f
- Float literal
1D 1d 1.1 1.1e-3
- Double literal
true false null
- Special constant values
abc
- Variable reference, see
Expression.evaluate(de.unkrig.commons.lang.protocol.Mapping)
-
new pkg.Clazz(arg1, arg2)
new RootPackageClazz(arg1, arg2)
new ImportedClazz(arg1,
arg2)
- Class instance creation
new pkg.Clazz
pkg.Clazz
pkg.Clazz()
- Abbreviated forms of
new pkg.Clazz()
-
new pkg.Clazz[x]
new pkg.Clazz[x][y][][]
new int[x]
new int[x][y][][]
- Array creation
pkg.Clazz[x]
pkg.Clazz[x][y][][]
int[x]
int[x][y][][]
- Abbreviated array creation
x[y]
- Array element access
x.name
- Attribute reference; accesses field 'name', or invokes 'name()' or 'getName()'
x.meth(a, b, c)
- Method invocation
Null operands are handled leniently, e.g. "7 == null" is false, "null == null" is true.
To implement your parser, derive from this class and implement the abstract methods.
|
Method Summary |
protected abstract T |
arrayAccess(T lhs,
T rhs)
|
protected abstract T |
binaryOperation(T lhs,
Parser.BinaryOperator operator,
T rhs)
|
protected abstract T |
cast(java.lang.Class<?> targetType,
T operand)
|
protected abstract T |
conditional(T lhs,
T mhs,
T rhs)
|
void |
disableExtension(de.unkrig.commons.text.expression.Parser.Extension extension)
Disable one of the parser extensions listed in Parser.Extension. |
void |
enableExtension(de.unkrig.commons.text.expression.Parser.Extension extension)
Enable one of the parser extensions listed in Parser.Extension. |
protected abstract T |
fieldReference(T target,
java.lang.String fieldName)
|
java.lang.ClassLoader |
getClassLoader()
|
java.lang.String[] |
getImports()
|
protected abstract T |
instanceoF(T lhs,
java.lang.Class<?> rhs)
|
protected abstract T |
literal(java.lang.Object value)
|
protected abstract T |
methodInvocation(T target,
java.lang.String methodName,
java.util.List<T> arguments)
|
protected abstract T |
newArray(java.lang.Class<?> clasS,
java.util.List<T> dimensions)
|
protected abstract T |
newClass(java.lang.Class<?> clasS,
java.util.List<T> arguments)
|
protected abstract T |
parenthesized(T value)
|
T |
parse()
|
Parser<T,E> |
setClassLoader(java.lang.ClassLoader classLoader)
|
Parser<T,E> |
setImports(java.lang.String[] imports)
By default, there is only one import: "java.lang". |
protected abstract T |
staticFieldReference(java.lang.Class<?> type,
java.lang.String fieldName)
|
protected abstract T |
staticMethodInvocation(java.lang.Class<?> target,
java.lang.String methodName,
java.util.List<T> arguments)
|
protected abstract T |
unaryOperation(Parser.UnaryOperator operator,
T operand)
|
protected abstract T |
variableReference(java.lang.String variableName)
|
| Methods inherited from class de.unkrig.commons.text.parser.AbstractParser |
eoi, peek, peek, peek, peek, peekRead, peekRead, peekRead, peekRead, peekReadEnum, read, read, read, read |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Parser
public Parser(ProducerWhichThrows<AbstractScanner.Token<Scanner.TokenType>,ScanException> tokenProducer)
Parser
public Parser(java.io.Reader in)
Parser
public Parser(java.lang.String expression)
getImports
public java.lang.String[] getImports()
- Returns:
- The currently configured imports
setImports
public Parser<T,E> setImports(java.lang.String[] imports)
- By default, there is only one import: "java.lang".
getClassLoader
public java.lang.ClassLoader getClassLoader()
- Returns:
- The currently configured
ClassLoader
setClassLoader
public Parser<T,E> setClassLoader(java.lang.ClassLoader classLoader)
- Parameters:
classLoader - Used to load classes named in the expression; by default the class loader which loaded
the Parser class.
enableExtension
public void enableExtension(de.unkrig.commons.text.expression.Parser.Extension extension)
- Enable one of the parser extensions listed in
Parser.Extension. By default, all extensions are enabled.
disableExtension
public void disableExtension(de.unkrig.commons.text.expression.Parser.Extension extension)
- Disable one of the parser extensions listed in
Parser.Extension. By default, all extensions are enabled.
parse
public T parse()
throws ParseException,
E extends java.lang.Exception
- Returns:
- The parsed expression
- Throws:
ParseException
E extends java.lang.Exception
conditional
protected abstract T conditional(T lhs,
T mhs,
T rhs)
throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
unaryOperation
protected abstract T unaryOperation(Parser.UnaryOperator operator,
T operand)
throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
binaryOperation
protected abstract T binaryOperation(T lhs,
Parser.BinaryOperator operator,
T rhs)
throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
fieldReference
protected abstract T fieldReference(T target,
java.lang.String fieldName)
throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
staticFieldReference
protected abstract T staticFieldReference(java.lang.Class<?> type,
java.lang.String fieldName)
throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
methodInvocation
protected abstract T methodInvocation(T target,
java.lang.String methodName,
java.util.List<T> arguments)
throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
staticMethodInvocation
protected abstract T staticMethodInvocation(java.lang.Class<?> target,
java.lang.String methodName,
java.util.List<T> arguments)
throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
variableReference
protected abstract T variableReference(java.lang.String variableName)
throws E extends java.lang.Exception,
ParseException
- Throws:
E extends java.lang.Exception
ParseException
literal
protected abstract T literal(@Nullable
java.lang.Object value)
throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
parenthesized
protected abstract T parenthesized(T value)
throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
instanceoF
protected abstract T instanceoF(T lhs,
java.lang.Class<?> rhs)
throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
newClass
protected abstract T newClass(java.lang.Class<?> clasS,
java.util.List<T> arguments)
throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
newArray
protected abstract T newArray(java.lang.Class<?> clasS,
java.util.List<T> dimensions)
throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
cast
protected abstract T cast(java.lang.Class<?> targetType,
T operand)
throws E extends java.lang.Exception,
ParseException
- Throws:
E extends java.lang.Exception
ParseException
arrayAccess
protected abstract T arrayAccess(T lhs,
T rhs)
throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception