Hi all,

I have an expression in a string, and I use Cayenne to parse the expression 
into tokens, which are needed for a specific purpose.

However in addition to having the tokens I also need to evaluate an object 
against that expression, to see if that object matches the expression.

My problem is that the way I'm doing it causes the parsing to be done twice on 
the same expression, and I would like to avoid to parse the same expression 
twice.

The token creation I'm doing it like this:

-----------------------------------
String where = "myField=0";

Reader reader = new StringReader(where);

ExpressionParser parser = new ExpressionParser(reader);

List<Token> tokens = new ArrayList<>();

Token token = parser.getNextToken();

while (token != null) {

     tokens.add(token);

     token = parser.getNextToken();
}
-----------------------------------

The object matching I'm doing it like this:

-----------------------------------
String where = "myField=0";

Expression expression = Expression.fromString(where);

boolean matches = expression.match(object);
-----------------------------------

The call to Expression.fromString made in the object matching operation 
performs a parsing, but the parsing of the same expression had already been 
done in the token creation operation.

Is there a way to redesign this process in order to get the tokens and also 
match an object against the expression without parsing the same expression 
twice ?

For example, I believe that the call to Expression.fromString must have created 
the tokens, because it has parsed the string. So I thought I could reverse the 
order and do the object matching first, keep the Expression instance created in 
that process and use it to extract the tokens. But I can't see how to extract 
the tokens from an Expression instance instead of from an ExpressionParser 
instance as I'm currently doing.

Or another possibility could be that I keep creating the tokens first, and then 
I match my object against them, instead of against the string expression that 
generated those tokens. But I can't see how to match an object against tokens.

So I'm looking for some ideas.

Thanks in advance.

Davide Vecchi

Reply via email to