com.eteks.parser
Class ExpressionParser
java.lang.Object
|
+--com.eteks.parser.Parser
|
+--com.eteks.parser.FunctionParser
|
+--com.eteks.parser.ExpressionParser
- Direct Known Subclasses:
- JeksExpressionParser
- public class ExpressionParser
- extends FunctionParser
Parser able to compile expressions with parameters of the form
= expression operating on parameters.
The expression may contain unary or binary operators, conditions, common functions
or other functions operating on literals, constants and paramaters checked on the fly.
A parser is associated to a syntax, instance of the Syntax
interface that
describes the different lexicals used by the parser and their type. This parser uses
also an instance of ExpressionParameter
used to check the parameters of
parsed expressions and to return their value at runtime.
The CompiledExpression compileExpression (String expressionDefinition)
method
of this class allows to parse and compile the definition expressionDefinition
of an expression, that must be made of the following elements :
- The expression must start with the operator of assignment returned
by the
getAssignmentOperator ()
method of the syntax of the parser
if it's not equal to null
.
- Following the operator of assignment is the expression that describes the
operations to compute, using the following syntactic elements :
- parameters validated by the
isValidIdentifier ()
method of the syntax
and accepted by the getParameterKey ()
method of the
instance of ExpressionParameter
of the parser.
- literals (numbers, quoted strings) accepted by the
getLiteral ()
method of the syntax
- constants (PI true false ...) accepted by the
getConstantKey ()
method of the syntax
- unary operators(- ! ...) accepted by the
getUnaryOperatorKey ()
method of the syntax
- binary operators (- + / ...) accepted by the
getBinaryOperatorKey ()
method of the syntax
and whose priority is returned by the getBinaryOperatorPriority ()
method
- conditions whose parts (? :) are accepted by the
getConditionPartKey ()
method of the syntax
- common functions (log sin ...) accepted by the
getCommonFunctionKey ()
method of the syntax
- other functions accepted by the
getFunction ()
method of the syntax.
Parameters passed to functions must be bracketed between the characters
returned by getOpeningBracket ()
and getClosingBracket ()
methods of the syntax, and separated by the character returned by the
getParameterSeparator ()
method.
These functions are instances of classes implementing the
Function
interface. They may be classes written in Java or functions
compiled previously with a parser able to compile functions.
Binary operators processed by this parser are infixed, unary operators and functions
are prefixed. Some parts of the expression may be bracketed between the characters
returned by getOpeningBracket ()
and getClosingBracket ()
methods.
White spaces returned by the getWhiteSpaceCharacters ()
method
of the syntax may be used anywhere in the definition of the function.
If the compileExpression ()
method fails to compile a definition,
it throws an CompilationException
exception
that describe the found error (syntax error or other error).
Once an expression is compiled, the computeExpression ()
methods available
for the returned object allow to compute its value with the value of its parameters
returned by the getParameterValue ()
method of its instance of
ExpressionParameter
.
Here are a few examples of expressions that can be compiled with this parser. This
example shows how to compute new column values from the columns of a JDBC result set :
// A class implementing ExpressionParameter supporting the name of columns of a ResultSet
class ResultSetColumnParameter implements ExpressionParameter
{
ResultSet resultSet;
public ResultSetColumnParameter (ResultSet resultSet)
{
this.resultSet = resultSet;
}
public Object getParameterKey (String parameter)
{
try
{
// Check if parameter exists among the names of columns
if (resultSet.findColumn (parameter) > 0)
return parameter;
}
catch (SQLException e)
{ }
return null;
}
public Object getParameterValue (Object parameterKey)
{
try
{
// Simply return the value of the column in current row
return resultSet.getObject ((String)parameterKey);
}
catch (SQLException e)
{
throw new IllegalArgumentException (e.getMessage ());
}
}
}
// Connect to a database and select values
Connection connection = DriverManager.getConnection ("jdbc:....");
Statement statement = connection.createStatement ();
ResultSet resultSet = statement.executeQuery ("SELECT X, Y, Z FROM TABLE1");
// Create a parser with the syntax DefaultSyntax
// and an instance of ResultSetColumnParameter
ExpressionParser parser = new ExpressionParser (new ResultSetColumnParameter (resultSet));
// Compile expressions with this parser
CompiledExpression ex1 = parser.compileExpression ("= X * LN (X)");
CompiledExpression ex2 = parser.compileExpression ("= 5.E-1 * (X + Y) ^ Z");
// Display the values of resultSet and
// the new values computed with default interpreter DoubleInterpreter
System.out.println ("X\tY\tZ\tX * LN (X)\t5.E-1 * (X + Y) ^ Z");
while (resultSet.next ())
System.out.println ( String.valueOf (resultSet.getObject ("X")) + "\t"
+ String.valueOf (resultSet.getObject ("Y")) + "\t"
+ String.valueOf (resultSet.getObject ("Z")) + "\t"
+ String.valueOf (ex1.computeExpression ()) + "\t"
+ String.valueOf (ex2.computeExpression ()));
The methods of this class are thread safe (if the methods of the class implementing
ExpressionParameter
are also thread safe).
- Since:
- Jeks 1.0
- Version:
- 1.0
- Author:
- Emmanuel Puybaret
- See Also:
Syntax
,
CompiledExpression
,
ExpressionParameter
,
JeksParameter
,
JeksCellEditor
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
ExpressionParser
public ExpressionParser(ExpressionParameter expressionParameter)
- Creates a parser for expressions whose syntax is an instance of
DefaultSyntax
.
- Parameters:
expressionParameter
- the instance of ExpressionParameter
used
to check parameters of parsed expressions.
ExpressionParser
public ExpressionParser(Syntax syntax,
ExpressionParameter expressionParameter)
- Creates a parser for expressions with the syntax
syntax
.
- Parameters:
syntax
- syntax of the parser.expressionParameter
- the instance of ExpressionParameter
used
to check parameters of parsed expressions.
getExpressionParameter
public ExpressionParameter getExpressionParameter()
- Returns the instance of
ExpressionParameter
of this parser.
- Returns:
- the instance of
ExpressionParameter
of this parser.
compileExpression
public CompiledExpression compileExpression(java.lang.String expressionDefinition)
throws CompilationException
- Compiles the definition of an expression
expressionDefinition
and
returns the instance of CompiledExpression
matching the definition.
This method builds a tree of type ExpressionNode
matching the expression.
This tree is used to compute the value of an expression at interpretation time.
- Parameters:
expressionDefinition
- the definition of the expression.- Returns:
- an instance of
CompiledExpression
, with which values can be computed. - Throws:
CompilationException
- if the syntax of the expression is incorrect or if some of its
elements (literals, constants, operators, functions,...) are not
accepted by the syntax of the parser.