Revision 22513 trunk/extensions/extSymbology/src/org/gvsig/symbology/fmap/rendering/filter/parser/ExpressionParser.jj

View differences:

ExpressionParser.jj
54 54
import org.gvsig.symbology.fmap.rendering.filter.operations.*;
55 55

  
56 56
/**
57
 * 
57
 *
58 58
 * @author Pepe Vidal Salvador - jose.vidal.salvador@iver.es
59 59
 *
60 60
 */
61 61
public class ExpressionParser{
62
  protected Stack stack = new Stack();
63
  protected static OperatorsFactory operators = OperatorsFactory.getInstance();
64
  protected Hashtable<String, Value> symbols = new Hashtable<String, Value>();
65
   protected ArrayList<String> classNames = new ArrayList<String>();
66
 
62

  
63
    private Stack stack = new Stack();
64
	private static OperatorsFactory operators = OperatorsFactory.getInstance();
65
	private Hashtable<String, Value> symbols = new Hashtable<String, Value>();
66
	private ArrayList<String> classNames = new ArrayList<String>();
67

  
68

  
67 69
 public ArrayList<String> getClassNames (){
68 70
 	return classNames;
69 71
 }
70
 
72

  
71 73
  public void addClassName(String name){
72 74
	 for (int i = 0; i < classNames.size(); i++) {
73 75
		if(classNames.get(i).compareTo(name)==0)
......
78 80
    public Expression getExpression(){
79 81
    return (Expression)stack.pop();
80 82
  }
81
  
83

  
82 84
	 public Stack getStack(){
83
        	  
85

  
84 86
                return this.stack;
85 87
        }
86 88

  
......
94 96
}
95 97

  
96 98
  public static void main(String[]args)throws ParseException,ExpressionException{
97
    Hashtable<String, Value> symbols = new Hashtable<String, Value>();
98
          ExpressionParser parser = new ExpressionParser(new StringReader("true && true"));
99
          symbols.put("RD_6",ValueFactory.createValue(2));
99
         ExpressionParser parser = new ExpressionParser(new StringReader("true && true"));
100 100
         parser.Expression();
101 101
         parser.getStack();
102 102
         Expression exp = parser.getExpression();
103
         exp.check(symbols);
104
     	System.out.println(exp.evaluate(symbols));
103

  
105 104
  }
106 105
}
107 106
PARSER_END(ExpressionParser)
108
SKIP:{
109
  " "
110
  | "\r"
111
  | "\t"
112
  | "\n"
107
SKIP :
108
{
109
 	" "
110
|	"\r"
111
|	"\t"
112
|	"\n"
113 113
}
114
TOKEN:/*Boolean Literals*/
114

  
115

  
116
TOKEN : /* OPERATORS */
115 117
{
116
  <TRUE:"true">
117
  | <FALSE:"false">
118
	< PLUS: "+" >
119
|	< MINUS: "-" >
120
|	< MULTIPLY: "*" >
121
|	< DIVIDE: "/" >
122
|	< GT: ">" >
123
|	< LT: "<" >
124
|	< NOT: "!" >
125
|	< EQ: "==" >
126
|	< LE: "<=" >
127
|	< GE: ">=" >
128
|	< NE: "!=" >
129
|	< OR: "||" >
130
|	< AND: "&&" >
118 131
}
119
TOKEN:{
120
  <NULL:"null">
121
}
122
TOKEN:/* Operators */
132
TOKEN : /* MISC */
123 133
{
124
  <AND:"&&">
125
  | <OR:"||">
126
  | <PLUS:"+">
127
  | <MINUS:"-">
128
  | <MULTIPLY:"*">
129
  | <DIVIDE:"/">
130
  | <LT:"<">
131
  | <LE:"<=">
132
  | <EQ:"==">
133
  | <NE:"!=">
134
  | <GT:">">
135
  | <GE:">=">
134
	< #DOT: "." >
135
|	< OPEN_SQ_BRACKETS : "[" >
136
|	< CLOSE_SQ_BRACKETS : "]" >
137
|	< OPEN_PARENTHESIS : "(" >
138
|	< CLOSE_PARENTHESIS : ")" >
139
|	< DOUBLE_QUOTE : "\"" >
140
|	< COMMA : "," >
141
|   < EOFIELD : ":" >
142
|	< EOEXPR : ";" >
136 143
}
137
TOKEN:/* Numeric literals */
144
TOKEN : /* CONSTANTS AND IDENTIFIERS */
138 145
{
139
  <INTEGER_LITERAL:(["0"-"9"])+>
140
  | <FLOATING_POINT_LITERAL:(["0"-"9"])+"."(["0"-"9"])*>
141
  | <STRING_LITERAL:"\""((~["\"", "\\", "\n", "\r"])
142
  | ("\\"(["n", "t", "b", "r", "f", "\\", "'", "\""]
143
  | ["0"-"7"](["0"-"7"])?
144
  | ["0"-"3"]["0"-"7"]["0"-"7"])))*"\"">
146
	< BOOLEAN: "true" | "false" >
147
|   < NULL: "null">
148
|	< IDENTIFIER: ( <LETTER> ) ( <LETTER>|<DIGIT> )* >
149
|	< #LETTER: ( ["_","$","%"] | ( ["a"-"z"] ) | ( ["A"-"Z"] ) ) >
145 150
}
146
TOKEN:/* Identifier */
151
TOKEN : /* NUMBER */
147 152
{
148
  <IDENTIFIER:<LETTER>(<LETTER>
149
  | <DIGIT>)*>
150
  | <#LETTER:["\u0041"-"\u005a", "\u005f", "\u0061"-"\u007a"]>
151
  | <#DIGIT:["\u0030"-"\u0039"]>
153
    < #DIGIT: ["0"-"9"] >
154
|	< INTEGER: ( <DIGIT> )+ >
155
|	< FLOATING_POINT: (<DIGIT>)+ "." (<DIGIT>)* (<EXPONENT>)? (["f","F","d","D"])?
156
      | <DOT> (<DIGIT>)+ (<EXPONENT>)? (["f","F","d","D"])?
157
      | (<DIGIT>)+ <EXPONENT> (["f","F","d","D"])?
158
      | (<DIGIT>)+ (<EXPONENT>)? ["f","F","d","D"] >
159
|	< #EXPONENT: ["e","E"] (["+","-"])? (<DIGIT>)+ >
152 160
}
153
TOKEN:/* Others*/
161
TOKEN: /* STRING */
154 162
{
155
  <COMMA:",">
156
  | <LPAREN:"(">
157
  | <RPAREN:")">
163
	< STRING: ( <DOUBLE_QUOTE> ( <CHAR_STRING> )* <DOUBLE_QUOTE> )>
164
|	< CHAR_STRING : ~[ "\"", ";","\n","\r"] >
158 165
}
159 166
void Expression():{}{
160 167
  AndExpression()(<OR>AndExpression(){
161 168
    Expression leftExpression = (Expression)stack.pop();
162 169
    Expression rightExpression = (Expression)stack.pop();
163
    
164
 	Expression orExpr = new OrOperator();
170

  
171
 	Expression orExpr = new OrOperator(symbols);
165 172
 	orExpr.addArgument(0, rightExpression);
166 173
 	orExpr.addArgument(0, leftExpression);
167
 	
174

  
168 175
    stack.push(orExpr);
169
   
176

  
170 177
  }
171 178
  )*
172 179
}
......
177 184
  EqCompExpression()((x = <AND>)EqCompExpression(){
178 185
    Expression rightExpression = (Expression)stack.pop();
179 186
    Expression leftExpression = (Expression)stack.pop();
180
    
181
    Expression func = new AndOperator();
182
    
187

  
188
    Expression func = new AndOperator(symbols);
189

  
183 190
    func.addArgument(0, rightExpression);
184 191
    func.addArgument(0, leftExpression);
185
 	
192

  
186 193
    stack.push(func);
187 194
  }
188 195
  )*
......
196 203
    Expression rightExpression = (Expression)stack.pop();
197 204
    Expression leftExpression = (Expression)stack.pop();
198 205
    Expression func = null;
199
    
200
    if (x.kind == EQ)func = new EqOperator();
201
    else if (x.kind == NE)func = new NeOperator();
202
    
206

  
207
    if (x.kind == EQ)func = new EqOperator(symbols);
208
    else if (x.kind == NE)func = new NeOperator(symbols);
209

  
203 210
    func.addArgument(0, rightExpression);
204 211
    func.addArgument(0, leftExpression);
205
 	
212

  
206 213
    stack.push(func);
207 214
  }
208 215
  )*
......
218 225
    Expression rightExpression = (Expression)stack.pop();
219 226
    Expression leftExpression = (Expression)stack.pop();
220 227
    Expression func = null;
221
   
222
    
223
    if (x.kind == LT)func = new LessThanOperator();
224
    else if (x.kind == LE)func = new LessThanOrEqualsOperator();
225
    else if (x.kind == GT)func = new GreaterThanOperator();
226
    else if (x.kind == GE)func = new GreaterThanOrEqualsOperator();
227
   
228

  
229

  
230
    if (x.kind == LT)func = new LessThanOperator(symbols);
231
    else if (x.kind == LE)func = new LessThanOrEqualsOperator(symbols);
232
    else if (x.kind == GT)func = new GreaterThanOperator(symbols);
233
    else if (x.kind == GE)func = new GreaterThanOrEqualsOperator(symbols);
234

  
228 235
    func.addArgument(0, rightExpression);
229 236
    func.addArgument(0, leftExpression);
230
 	
237

  
231 238
    stack.push(func);
232 239
  }
233 240
  )*
......
241 248
    Expression rightExpression = (Expression)stack.pop();
242 249
    Expression leftExpression = (Expression)stack.pop();
243 250
    Expression func = null;
244
    
245
    if (x.kind == PLUS)func = new AddOperator();
246
    else func = new MinusOperator();
247
    
251

  
252
    if (x.kind == PLUS)func = new AddOperator(symbols);
253
    else func = new MinusOperator(symbols);
254

  
248 255
    func.addArgument(0, rightExpression);
249 256
    func.addArgument(0, leftExpression);
250
 	
257

  
251 258
    stack.push(func);
252 259
  }
253 260
  )*
......
258 265
{
259 266
  UnaryElement()((x = <MULTIPLY>
260 267
  | x = <DIVIDE>)UnaryElement(){
261
    Expression rightExpression = (Expression)stack.pop();
262
    Expression leftExpression = (Expression)stack.pop();
263
    Expression func = null;
264
   
265
   if (x.kind == MULTIPLY)func = new MultOperator();
266
   else func = new DivOperator();
267
   
268
   	func.addArgument(0, rightExpression);
269
    func.addArgument(0, leftExpression);
270
 
271
    stack.push(func);
272
  }
273
  )*
268
  	try{
269
  			Expression A = (Expression) stack.pop();
270
			Expression B = (Expression) stack.pop();
271
			 boolean aInt = (((Expression) A).evaluate() instanceof Integer);
272
			 boolean bInt = (((Expression) B).evaluate() instanceof Integer);
273
			if (aInt &&	bInt) {
274
				int a = (Integer) A.evaluate();
275
				int b = (Integer) B.evaluate();
276
				if (x.kind == MULTIPLY)
277
					stack.push(new NumericalConstant(new Integer(b * a),symbols));
278
				else
279
					stack.push(new NumericalConstant(new Integer(b / a),symbols));
280
			} else {
281

  
282
				double a = new Double(((Expression)A).evaluate().toString());
283
                double b = new Double(((Expression)B).evaluate().toString());
284
				if (x.kind == MULTIPLY)
285
					stack.push(new NumericalConstant(new Double(b * a),symbols));
286
				else
287
					stack.push(new NumericalConstant(new Double(b / a),symbols));
288
			}}catch (ClassCastException ex) {
289
                  ex.printStackTrace();
290
            } catch (ExpressionException e) {
291
				  e.printStackTrace();
292
			}
293

  
294
	})*
274 295
}
275 296
void UnaryElement():{
276 297
  Token x = null;
......
278 299
{
279 300
  (x = <PLUS>
280 301
  | x = <MINUS>)UnaryElement(){
281
    if (x.kind == PLUS){
282
      Expression a = (Expression)stack.pop();
283
     try {
284
		DoubleConstant plusDoubleValue = new DoubleConstant(a.evaluate(symbols).toString());
285
		 stack.push(plusDoubleValue);
286
	} catch (ExpressionException e) {
287
		e.printStackTrace();
288
	}
289
     
290
    }
291
    else if (x.kind == MINUS){
292
      Expression a = (Expression)stack.pop();
293
      try {
294
		DoubleConstant doubleValue = new DoubleConstant("-1*", a.evaluate(symbols).toString());
295
		stack.push(doubleValue);
296
	} catch (ExpressionException e) {
297
		e.printStackTrace();
298
	}
299
      
300
    }
302
  	      try{
303
                 Object A = stack.pop();
304
                         if ((((Expression) A).evaluate() instanceof Integer)) {
305
                                int a = (Integer) ((Expression) A).evaluate();
306
                                if (x.kind == PLUS)
307
                                        stack.push(new NumericalConstant(new Integer(a),symbols));
308
                                else
309
                                        stack.push(new NumericalConstant(new Integer(-a),symbols));
310
                        } else {
311
                                double a = (Double) ((Expression) A).evaluate();
312
                                if (x.kind == PLUS)
313
                                        stack.push(new NumericalConstant(new Double(a),symbols));
314
                                else
315
                                        stack.push(new NumericalConstant(new Double(-a),symbols));
316
                        }
317
                         } catch (ClassCastException ex) {
318
               ex.printStackTrace();
319
             }catch (ExpressionException ex) {
320
                 ex.printStackTrace();
321
             }
301 322
  }
302
  | Element()
323

  
324
  | BooleanUnaryElement()
303 325
}
326

  
327
void BooleanUnaryElement():
328
{
329
	Token t;
330
}
331
{
332
	 t = <NOT> UnaryElement() {
333
	 	Expression rightExpression = (Expression)stack.pop();
334
		Expression func = new NotOperator(symbols);
335
		func.addArgument(0, rightExpression);
336
		stack.push(func);
337
	}
338
| Element()
339
}
304 340
void Element():{
305 341
  Token x;
306 342
  String y =  "[$,A-Z,_,a-z]([$,A-Z,_,a-z]|[0-9])*";
307 343
}
308 344
{
309
	
345

  
310 346
  ("["x = <IDENTIFIER>"]"){
311
     
312
    ReplaceOperator func = new ReplaceOperator();
313
     func.addArgument(x.image); 
347

  
348
    ReplaceOperator func = new ReplaceOperator(symbols);
349
     func.addArgument(x.image);
314 350
   	stack.push(func);
315 351
	addClassName(x.image);
316
   
352

  
317 353
  }
318
  | (x = <INTEGER_LITERAL> | x = <FLOATING_POINT_LITERAL>){
319
    DoubleConstant doubleValue = new DoubleConstant(x.image);
320
    stack.push(doubleValue);
354
  | (x = <INTEGER>){
355
  	NumericalConstant intC = new NumericalConstant(Integer.valueOf(x.image),symbols);
356
   	stack.push(intC);
321 357
  }
322
  | (x = <TRUE>| x = <FALSE>){
323
    BooleanConstant boolValue = new BooleanConstant(x.image);
358
  | (x = <FLOATING_POINT>){
359
  	NumericalConstant doubleC = new NumericalConstant(Double.valueOf(x.image),symbols);
360
   	stack.push(doubleC);
361
  }
362
  |(x = <STRING>) {
363
    StringConstant stringValue = new StringConstant(x.image.substring(1, x.image.length()-1),symbols);
364
    stack.push(stringValue);
365
  }
366
  | (x = <BOOLEAN>){
367
    BooleanConstant boolValue = new BooleanConstant(x.image,symbols);
324 368
    stack.push(boolValue);
325 369
  }
326 370
  | x = <NULL>{
327
  	NullConstant nullValue = new NullConstant();
371
  	NullConstant nullValue = new NullConstant(symbols);
328 372
    stack.push(nullValue);
329 373
  }
330 374
  | LOOKAHEAD(2)function()
......
358 402
    stack.push(function);
359 403
  }
360 404
  |(x = <IDENTIFIER>){
361
    StringConstant stringValue = new StringConstant(x.image);
405
    StringConstant stringValue = new StringConstant(x.image,symbols);
362 406
    stack.push(stringValue);
363 407
  }
364 408
}
......
366 410
  int argCount = 0;
367 411
}
368 412
{
369
  <LPAREN>[argCount = ArgumentList()]<RPAREN>{
413
  <OPEN_PARENTHESIS>[argCount = ArgumentList()]<CLOSE_PARENTHESIS>{
370 414
    return argCount;
371 415
  }
372 416
}

Also available in: Unified diff