common-util

local.tomas.expr.compiler
Class ExpressionCompiler

java.lang.Object
  extended by local.tomas.expr.compiler.ExpressionCompiler

public class ExpressionCompiler
extends Object

The ExpressionCompiler creates Expressions from strings.

Author:
tomas

Field Summary
protected  Locale locale
          Locale for compilation
protected  int nextToken
          Next token in compilation.
static int PRESERVE_NEWLINES_IN_STRINGS
          Option to preserve newline characters within strings.
protected  String[] tokenStream
          Token stream compiled.
 
Constructor Summary
ExpressionCompiler(String expr, Locale locale, int options)
          Constructor.
 
Method Summary
protected  Class<? extends BinaryTerm> additionOperator(String string)
          Get expression class for addition operator.
protected  Class<? extends BinaryTerm> comparisonOperator(String string)
          Get expression class for comparison operator.
protected  String currentToken()
          Get current token
protected  String followingToken()
          Get following token without moving the token cursor.
protected  Class<? extends FunctionCall> function(String name)
          Get expression class for function with at least two arguments.
protected  List<String> getTokenStream(String expr, int options)
          Convert string to token stream.
protected  Class<? extends BinaryTerm> multiplicationOperator(String string)
          Get expression class for multiplication operator.
protected  Class<? extends FunctionCall> noArgFunction(String name)
          Get expression class for argument-less function.
protected  Expression parse()
          Parse expression string.
static Expression parse(String expr, Locale locale, int options)
          Parse an expression.
protected  Expression parseAddition()
          Parse addition term.
protected  Expression parseAnd()
          Parse AND term.
protected  Expression parseAtom()
          Parse atomic expression.
protected  Expression parseComparison()
          Parse comparison term.
protected  Expression parseMultiplication()
          Parse multiplication term.
protected  Expression parseOr()
          Parse OR term.
protected  Expression parseUnaryTerm()
          Parse unary term.
protected  Expression resolveName(String name)
           
protected  Class<? extends UnaryTerm> unaryFunction(String name)
          Get expression class for function with a single argument.
protected  Class<? extends UnaryTerm> unaryPostfix(String string)
          Get expression class for unary postfix operator.
protected  Class<? extends UnaryTerm> unaryPrefix(String string)
          Get expression class for unary postfix operator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PRESERVE_NEWLINES_IN_STRINGS

public static final int PRESERVE_NEWLINES_IN_STRINGS
Option to preserve newline characters within strings.

See Also:
Constant Field Values

locale

protected Locale locale
Locale for compilation


tokenStream

protected String[] tokenStream
Token stream compiled.


nextToken

protected int nextToken
Next token in compilation.

Constructor Detail

ExpressionCompiler

public ExpressionCompiler(String expr,
                          Locale locale,
                          int options)
                   throws ExpressionException
Constructor.

Parameters:
expr - expression to compile
locale - locale used for compilation
options - compilation options like PRESERVE_NEWLINES_IN_STRINGS or'ed together
Throws:
ExpressionException
Method Detail

getTokenStream

protected List<String> getTokenStream(String expr,
                                      int options)
                               throws ExpressionException
Convert string to token stream.

Parameters:
expr - expression string
options - compilation options like PRESERVER_NEWLINES_IN_STRINGS or'ed together
Returns:
list of tokens
Throws:
ExpressionException

currentToken

protected String currentToken()
Get current token

Returns:
current token

followingToken

protected String followingToken()
Get following token without moving the token cursor.

Returns:
following token

parse

protected Expression parse()
                    throws ExpressionException
Parse expression string.

Returns:
expression parsed
Throws:
ExpressionException

parseOr

protected Expression parseOr()
                      throws ExpressionException
Parse OR term.

Returns:
expression parsed
Throws:
ExpressionException

parseAnd

protected Expression parseAnd()
                       throws ExpressionException
Parse AND term.

Returns:
epression parsed
Throws:
ExpressionException

parseComparison

protected Expression parseComparison()
                              throws ExpressionException
Parse comparison term.

Returns:
expression parsed
Throws:
ExpressionException

comparisonOperator

protected Class<? extends BinaryTerm> comparisonOperator(String string)
Get expression class for comparison operator.

Parameters:
string - comparison operator
Returns:
subclass of BinaryTerm or null if string is not a comparison operator

parseAddition

protected Expression parseAddition()
                            throws ExpressionException
Parse addition term.

Returns:
expression parsed
Throws:
ExpressionException

additionOperator

protected Class<? extends BinaryTerm> additionOperator(String string)
Get expression class for addition operator.

Parameters:
string - addition operator
Returns:
subclass of BinaryTerm or null if string is not an addition operator

parseMultiplication

protected Expression parseMultiplication()
                                  throws ExpressionException
Parse multiplication term.

Returns:
expression parsed
Throws:
ExpressionException

multiplicationOperator

protected Class<? extends BinaryTerm> multiplicationOperator(String string)
Get expression class for multiplication operator.

Parameters:
string - operator
Returns:
subclass of BinaryTerm or null if string is not a multiplication operator

parseUnaryTerm

protected Expression parseUnaryTerm()
                             throws ExpressionException
Parse unary term.

Returns:
expression parsed
Throws:
ExpressionException

unaryPostfix

protected Class<? extends UnaryTerm> unaryPostfix(String string)
Get expression class for unary postfix operator.

Parameters:
string - operator
Returns:
subclass of UnaryTerm or null if string is not a unary postfix operator

unaryPrefix

protected Class<? extends UnaryTerm> unaryPrefix(String string)
Get expression class for unary postfix operator.

Parameters:
string - operator
Returns:
subclass of UnaryTerm or null if string is not a unary prefix operator

parseAtom

protected Expression parseAtom()
                        throws ExpressionException
Parse atomic expression.

Returns:
expression parsed
Throws:
ExpressionException

resolveName

protected Expression resolveName(String name)
                          throws ExpressionException
Throws:
ExpressionException

noArgFunction

protected Class<? extends FunctionCall> noArgFunction(String name)
Get expression class for argument-less function.

Parameters:
name - function name
Returns:
subclass of FunctionCall or null if name is not an argument-less function

unaryFunction

protected Class<? extends UnaryTerm> unaryFunction(String name)
Get expression class for function with a single argument.

Parameters:
name - function name
Returns:
subclass of FunctionCall or null if name is not function with a single argument

function

protected Class<? extends FunctionCall> function(String name)
Get expression class for function with at least two arguments.

Parameters:
name - function name
Returns:
subclass of FunctionCall or null if name is not function with at least two arguments

parse

public static Expression parse(String expr,
                               Locale locale,
                               int options)
                        throws ExpressionException
Parse an expression.

Parameters:
expr - string representation of an expression
locale - locale used for compilation
options - compilation options or'ed together
Returns:
expression parsed
Throws:
ExpressionException

common-util

Copyright © 2005-2007 Tomas Teubner. All Rights Reserved.