com.eteks.parser
Class CalculatorParser
java.lang.Object
|
+--com.eteks.parser.Parser
|
+--com.eteks.parser.CalculatorParser
- public class CalculatorParser
- extends Parser
Parser able to compute constant expressions operating unary and binary operators
and common functions on numbers. This is the kind of expressions often managed in calculators.
The two computeExpression ()
methods of this class allow to parse
and compute the value of an expression :
double computeExpression (String expressionDefinition)
is the easiest
method to compute values of type double
.
Object computeExpression (String expressionDefinition, Interpreter interpreter)
allows to choose the interpreter with which the numbers and the operators and common functions of
Syntax
will be computed.
A parser is associated to a syntax, instance of the Syntax
interface that
describes the different lexicals used by the parser and their type.
The expression may use the following syntactic elements :
- literals (numbers) accepted by the
getLiteral ()
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
- common functions (log sin ...) accepted by the
getCommonFunctionKey ()
method of the syntax.
The binary operators recognized by this parser are infixed, unary operators and common 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 expression.
Here are a few examples of expressions that can be computed by this parser :
// Create a parser with the default syntax DefaultSyntax
CalculatorParser parser = new CalculatorParser ();
// Compute expressions with default interpreter DoubleInterpreter
double doubleResult1 = parser.computeExpression ("3 + 4 * 5");
double doubleResult2 = parser.computeExpression ("(1 + 2 + 3 + 4) * 5");
double doubleResult3 = parser.computeExpression ("sin (0.2) ^ 2");
double doubleResult4 = parser.computeExpression ("cos (0.2 + 3.1E-1) / 2");
// This last instruction will throw a CompilationException
// because the expression to parse is invalid
double doubleResult5 = parser.computeExpression ("cos (3))");
Only the following methods of Syntax
are used by this parser :
Object getLiteral (String expression, StringBuffer extractedLiteral)
Object getUnaryOperatorKey (String unaryOperator)
Object getBinaryOperatorKey (String binaryOperator)
Object getCommonFunctionKey (String commonFunction)
int getBinaryOperatorPriority (Object binaryOperatorKey)
String getWhiteSpaceCharacters ()
char getOpeningBracket ()
char getClosingBracket ()
String getDelimiters ()
boolean isShortSyntax ()
Short cuts of a syntax supports only calls to common function without brackets
around their parameter.
The methods of this class are thread safe.
- Since:
- Jeks 1.0
- Version:
- 1.0.2
- Author:
- Emmanuel Puybaret
- See Also:
Syntax
,
Interpreter
,
com.eteks.tools.calculator.JeksCalculator
Constructor Summary |
CalculatorParser()
Creates a calculator's parser whose syntax is an instance of DefaultSyntax . |
CalculatorParser(Syntax syntax)
Creates a calculator's parser with the syntax syntax . |
Method Summary |
double |
computeExpression(java.lang.String expressionDefinition)
Returns the result of the constant expression expressionDefinition .
|
java.lang.Object |
computeExpression(java.lang.String expressionDefinition,
Interpreter interpreter)
Returns the result of the constant expression expressionDefinition .
|
Syntax |
getSyntax()
Returns the syntax used by this parser. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
CalculatorParser
public CalculatorParser()
- Creates a calculator's parser whose syntax is an instance of
DefaultSyntax
.
CalculatorParser
public CalculatorParser(Syntax syntax)
- Creates a calculator's parser with the syntax
syntax
.
- Parameters:
syntax
- syntax of the parser.
getSyntax
public Syntax getSyntax()
- Returns the syntax used by this parser.
- Returns:
- the parser's syntax
computeExpression
public double computeExpression(java.lang.String expressionDefinition)
throws CompilationException
- Returns the result of the constant expression
expressionDefinition
.
expressionDefinition
may contain numbers, unary and binary operators,
common functions and brackets, according to the syntax of this parser.
An interpreter of DoubleInterpreter
class is used internally to compute
values of type Double
and then return a value of type double
.
- Parameters:
expressionDefinition
- the constant expression to compute.- Returns:
- the result of the expression.
- Throws:
CompilationException
- if the syntax of the expression is incorrect or if some
elements (numbers, operators, functions,...) of the expression are not
accepted by the syntax of the parser.- See Also:
DoubleInterpreter
,
computeExpression(String, Interpreter)
computeExpression
public java.lang.Object computeExpression(java.lang.String expressionDefinition,
Interpreter interpreter)
throws CompilationException
- Returns the result of the constant expression
expressionDefinition
.
expressionDefinition
may contain numbers, unary and binary operators,
common functionss and brackets, according to the syntax of this parser.
- Parameters:
expressionDefinition
- the constant expression to compute.interpreter
- the runtime interpreter used to compute the operations of Syntax
.- Returns:
- the result of the expression. The returned object may be of any
class (
Double
, String
, ...) according to
interpreter
implementation. - Throws:
CompilationException
- if the syntax of the expression is incorrect or if some
elements (literals, constants, operators, functions,...) of the expression are not
accepted by the syntax of the parser.