1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/*******************************************************************************
* Copyright (c) 2000, 2012 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.pde.internal.ui.wizards.templates;
import java.util.Hashtable;
import java.util.Stack;
import org.eclipse.pde.ui.templates.IVariableProvider;
public class PreprocessorParser {
private static final int T_VAR = 1;
private static final int T_LBR = 2;
private static final int T_RBR = 3;
private static final int T_NOT = 4;
private static final int T_AND = 5;
private static final int T_OR = 6;
private static final int T_EQ = 7;
private static final int T_NEQ = 8;
private static final int T_STRING = 9;
private static final int T_TRUE = 22;
private static final int T_FALSE = 23;
private static final int T_ERROR = 99;
private static final int T_EOF = 10;
//private static final int OP_LEAF = -1;
private static final int OP_AND = 1;
private static final int OP_OR = 2;
private static final int OP_EQ = 3;
private static final int OP_NEQ = 4;
private static final int OP_NOT = 5;
//private static final int OP_DEFER = 55;
private IVariableProvider provider;
private String line;
private Stack<RootEntry> exprStack;
private int loc;
private String tvalue;
abstract class Node {
abstract Object getValue();
}
class LeafNode extends Node {
Object value;
LeafNode(Object value) {
this.value = value;
}
public Object getValue() {
return value;
}
public String toString() {
if (value != null)
return "leaf[" + value.toString() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
return "leaf[null]"; //$NON-NLS-1$
}
}
class ExpressionNode extends Node {
int opcode;
Node left;
Node right;
public ExpressionNode(Node left, Node right, int opcode) {
this.opcode = opcode;
this.left = left;
this.right = right;
}
public Object getValue() {
boolean result = false;
Object leftValue = left != null ? left.getValue() : Boolean.FALSE;
Object rightValue = right != null ? right.getValue() : Boolean.FALSE;
if (opcode == OP_NOT && rightValue instanceof Boolean) {
result = rightValue.equals(Boolean.TRUE) ? false : true;
} else {
if (leftValue instanceof Boolean && rightValue instanceof Boolean) {
boolean bleft = ((Boolean) leftValue).booleanValue();
boolean bright = ((Boolean) rightValue).booleanValue();
switch (opcode) {
case OP_AND :
result = bleft && bright;
break;
case OP_OR :
result = bleft || bright;
break;
case OP_EQ :
result = bleft == bright;
break;
case OP_NEQ :
result = bleft != bright;
break;
}
}
if (leftValue instanceof String && rightValue instanceof String) {
switch (opcode) {
case OP_EQ :
result = leftValue.equals(rightValue);
break;
case OP_NEQ :
result = leftValue.equals(rightValue);
break;
}
}
}
return result ? Boolean.TRUE : Boolean.FALSE;
}
public String toString() {
String lstring = left != null ? left.toString() : "*"; //$NON-NLS-1$
String rstring = right != null ? right.toString() : "*"; //$NON-NLS-1$
return "(" + lstring + "<" + opcode + ">" + rstring + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}
}
class RootEntry {
Node root;
}
public PreprocessorParser() {
this(null);
}
public PreprocessorParser(IVariableProvider provider) {
this.provider = provider;
exprStack = new Stack<RootEntry>();
}
public void setVariableProvider(IVariableProvider provider) {
this.provider = provider;
}
@SuppressWarnings("rawtypes")
public static void main(String[] args) {
final Hashtable<String, Comparable> vars = new Hashtable<String, Comparable>();
vars.put("a", Boolean.FALSE); //$NON-NLS-1$
vars.put("b", "3"); //$NON-NLS-1$ //$NON-NLS-2$
vars.put("c", Boolean.TRUE); //$NON-NLS-1$
PreprocessorParser parser = new PreprocessorParser(new IVariableProvider() {
public Object getValue(String variable) {
return vars.get(variable);
}
});
try {
boolean value = parser.parseAndEvaluate("!a || (b==\"2\" && c)"); //$NON-NLS-1$
System.out.println("Result: " + value); //$NON-NLS-1$
} catch (Exception e) {
System.out.println(e);
}
}
public boolean parseAndEvaluate(String line) throws Exception {
reset();
this.line = line;
//System.out.println("Line: " + line);
parse();
//printExpression();
return evaluate();
}
private boolean evaluate() {
boolean result = false;
if (exprStack.isEmpty() == false) {
RootEntry entry = exprStack.peek();
if (entry.root != null) {
Object value = entry.root.getValue();
if (value != null && value instanceof Boolean) {
if (((Boolean) value).equals(Boolean.TRUE))
result = true;
}
}
}
return result;
}
private void reset() {
loc = 0;
tvalue = null;
exprStack.clear();
}
private void parse() throws Exception {
for (;;) {
int token = getNextToken();
//System.out.println("Token: " + token + ", val=\"" + tvalue+"\"");
if (token == T_EOF)
break;
if (token == T_VAR) {
Node node = new LeafNode(provider.getValue(tvalue.toString()));
pushNode(node);
continue;
}
if (token == T_TRUE || token == T_FALSE) {
Object value = token == T_TRUE ? Boolean.TRUE : Boolean.FALSE;
Node node = new LeafNode(value);
pushNode(node);
continue;
}
if (token == T_STRING) {
Node node = new LeafNode(tvalue);
pushNode(node);
continue;
}
if (token == T_NOT) {
pushNode(OP_NOT);
continue;
}
int opcode = 0;
switch (token) {
case T_AND :
opcode = OP_AND;
break;
case T_OR :
opcode = OP_OR;
break;
case T_EQ :
opcode = OP_EQ;
break;
case T_NEQ :
opcode = OP_NEQ;
break;
}
if (opcode != 0) {
pushNode(opcode);
continue;
}
if (token == T_LBR) {
pushRoot();
continue;
}
if (token == T_RBR) {
if (exprStack.isEmpty())
throwUnexpectedToken("not )", token); //$NON-NLS-1$
popRoot();
continue;
}
}
}
private RootEntry getCurrentRoot() {
if (exprStack.isEmpty()) {
RootEntry entry = new RootEntry();
exprStack.push(entry);
}
return exprStack.peek();
}
private void replaceRoot(ExpressionNode newRoot) {
RootEntry entry = getCurrentRoot();
if (entry.root != null)
newRoot.left = entry.root;
entry.root = newRoot;
}
private void pushNode(Node node) {
RootEntry entry = getCurrentRoot();
if (entry.root == null)
entry.root = node;
else {
ExpressionNode enode = (ExpressionNode) entry.root;
if (enode.opcode == OP_NOT)
enode.right = node;
else {
if (enode.left == null)
enode.left = node;
else
enode.right = node;
}
}
}
private void pushNode(int opcode) {
ExpressionNode node = new ExpressionNode(null, null, opcode);
replaceRoot(node);
}
private void pushRoot() {
exprStack.push(new RootEntry());
}
private void popRoot() {
RootEntry entry = getCurrentRoot();
exprStack.pop();
pushNode(entry.root);
}
private void throwUnexpectedToken(String expected, int token) throws Exception {
String message = "Expected " + expected + ", found " + token; //$NON-NLS-1$ //$NON-NLS-2$
throw new Exception(message);
}
private int getNextToken() {
boolean string = false;
boolean variable = false;
int vloc = loc;
tvalue = null;
for (;;) {
if (loc == line.length()) {
// check if we have panding identifier
if (variable) {
tvalue = line.substring(vloc, loc);
variable = false;
if (tvalue.equalsIgnoreCase("false")) //$NON-NLS-1$
return T_FALSE;
if (tvalue.equalsIgnoreCase("true")) //$NON-NLS-1$
return T_TRUE;
return T_VAR;
}
if (string) {
// EOF in string
string = false;
return T_ERROR;
}
// regular end of line
tvalue = "EOF"; //$NON-NLS-1$
return T_EOF;
}
char c = line.charAt(loc++);
if (c == '\"') {
if (string) {
tvalue = line.substring(vloc, loc - 1);
string = false;
return T_STRING;
}
vloc = loc;
string = true;
continue;
} else if (string)
continue;
if (!variable && Character.isJavaIdentifierStart(c)) {
variable = true;
vloc = loc - 1;
continue;
}
if (variable) {
if (!Character.isJavaIdentifierPart(c)) {
loc--;
tvalue = line.substring(vloc, loc);
variable = false;
if (tvalue.equalsIgnoreCase("false")) //$NON-NLS-1$
return T_FALSE;
if (tvalue.equalsIgnoreCase("true")) //$NON-NLS-1$
return T_TRUE;
return T_VAR;
}
continue;
}
if (testDoubleToken(c, "!=")) //$NON-NLS-1$
return T_NEQ;
if (testDoubleToken(c, "==")) //$NON-NLS-1$
return T_EQ;
if (testDoubleToken(c, "&&")) //$NON-NLS-1$
return T_AND;
if (testDoubleToken(c, "||")) //$NON-NLS-1$
return T_OR;
if (testSingleToken(c, '!'))
return T_NOT;
if (testSingleToken(c, '('))
return T_LBR;
if (testSingleToken(c, ')'))
return T_RBR;
if (c == ' ' || c == '\t' || c == '\n')
continue;
tvalue = "" + c; //$NON-NLS-1$
return T_ERROR;
}
}
private boolean testSingleToken(char c, char expected) {
if (c == expected) {
tvalue = "" + expected; //$NON-NLS-1$
return true;
}
return false;
}
private boolean testDoubleToken(char c1, String pattern) {
if (c1 != pattern.charAt(0))
return false;
char c2 = line.charAt(loc);
if (c2 == pattern.charAt(1)) {
loc++;
tvalue = pattern;
return true;
}
return false;
}
}
|