Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 057065de7022f322a09427b4c043d5cf5be71201 (plain) (blame)
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*******************************************************************************
 * Copyright (c) 2010, 2017 Cloudsmith Inc. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.metadata.expression.parser;

import java.util.*;
import org.eclipse.equinox.internal.p2.metadata.expression.IExpressionConstants;
import org.eclipse.equinox.internal.p2.metadata.expression.LDAPApproximation;
import org.eclipse.equinox.p2.metadata.expression.*;

public class ExpressionParser extends Stack<IExpression> implements IExpressionConstants, IExpressionParser {
	private static final long serialVersionUID = 5481439062356612378L;

	protected static final int TOKEN_OR = 1;
	protected static final int TOKEN_AND = 2;

	protected static final int TOKEN_EQUAL = 10;
	protected static final int TOKEN_NOT_EQUAL = 11;
	protected static final int TOKEN_LESS = 12;
	protected static final int TOKEN_LESS_EQUAL = 13;
	protected static final int TOKEN_GREATER = 14;
	protected static final int TOKEN_GREATER_EQUAL = 15;
	protected static final int TOKEN_MATCHES = 16;

	protected static final int TOKEN_NOT = 20;
	protected static final int TOKEN_DOT = 21;
	protected static final int TOKEN_COMMA = 22;
	protected static final int TOKEN_PIPE = 23;
	protected static final int TOKEN_DOLLAR = 24;
	protected static final int TOKEN_IF = 25;
	protected static final int TOKEN_ELSE = 26;

	protected static final int TOKEN_LP = 30;
	protected static final int TOKEN_RP = 31;
	protected static final int TOKEN_LB = 32;
	protected static final int TOKEN_RB = 33;
	protected static final int TOKEN_LC = 34;
	protected static final int TOKEN_RC = 35;

	protected static final int TOKEN_IDENTIFIER = 40;
	protected static final int TOKEN_LITERAL = 41;

	protected static final int TOKEN_NULL = 50;
	protected static final int TOKEN_TRUE = 51;
	protected static final int TOKEN_FALSE = 52;

	private static final int TOKEN_ALL = 60;
	private static final int TOKEN_EXISTS = 61;

	protected static final int TOKEN_END = 0;
	protected static final int TOKEN_ERROR = -1;

	protected static final Map<String, Integer> keywords;
	static {
		keywords = new HashMap<>();
		keywords.put(KEYWORD_FALSE, Integer.valueOf(TOKEN_FALSE));
		keywords.put(KEYWORD_NULL, Integer.valueOf(TOKEN_NULL));
		keywords.put(KEYWORD_TRUE, Integer.valueOf(TOKEN_TRUE));
		keywords.put(KEYWORD_ALL, Integer.valueOf(TOKEN_ALL));
		keywords.put(KEYWORD_EXISTS, Integer.valueOf(TOKEN_EXISTS));
	}

	protected final IExpressionFactory factory;

	protected String expression;
	protected int tokenPos;
	protected int currentToken;
	protected int lastTokenPos;
	protected Object tokenValue;
	protected String rootVariable;

	public ExpressionParser(IExpressionFactory factory) {
		this.factory = factory;
	}

	@Override
	public synchronized IExpression parse(String exprString) {
		expression = exprString;
		tokenPos = 0;
		currentToken = 0;
		tokenValue = null;
		IExpression thisVariable = factory.thisVariable();
		rootVariable = ExpressionUtil.getName(thisVariable);
		push(thisVariable);
		try {
			nextToken();
			IExpression expr = currentToken == TOKEN_END ? factory.constant(Boolean.TRUE) : parseCondition();
			assertToken(TOKEN_END);
			return expr;
		} finally {
			clear(); // pop all items
		}
	}

	@Override
	public synchronized IExpression parseQuery(String exprString) {
		expression = exprString;
		tokenPos = 0;
		currentToken = 0;
		tokenValue = null;
		rootVariable = VARIABLE_EVERYTHING;
		IExpression everythingVariable = factory.variable(VARIABLE_EVERYTHING);
		push(everythingVariable);
		try {
			nextToken();
			IExpression expr = parseCondition();
			assertToken(TOKEN_END);
			return expr;
		} finally {
			clear(); // pop all items
		}
	}

	protected Map<String, Integer> keywordToTokenMap() {
		return keywords;
	}

	protected IExpression parseCondition() {
		// Just a hook in this parser. Conditions are not supported
		return parseOr();
	}

	protected IExpression parseOr() {
		IExpression expr = parseAnd();
		if (currentToken != TOKEN_OR)
			return expr;

		ArrayList<IExpression> exprs = new ArrayList<>();
		exprs.add(expr);
		do {
			nextToken();
			exprs.add(parseAnd());
		} while (currentToken == TOKEN_OR);
		return factory.or(exprs.toArray(new IExpression[exprs.size()]));
	}

	protected IExpression parseAnd() {
		IExpression expr = parseBinary();
		if (currentToken != TOKEN_AND)
			return expr;

		ArrayList<IExpression> exprs = new ArrayList<>();
		exprs.add(expr);
		do {
			nextToken();
			exprs.add(parseBinary());
		} while (currentToken == TOKEN_AND);
		return factory.and(exprs.toArray(new IExpression[exprs.size()]));
	}

	protected IExpression parseBinary() {
		IExpression expr = parseNot();
		for (;;) {
			switch (currentToken) {
				case TOKEN_OR :
				case TOKEN_AND :
				case TOKEN_RP :
				case TOKEN_RB :
				case TOKEN_RC :
				case TOKEN_COMMA :
				case TOKEN_IF :
				case TOKEN_ELSE :
				case TOKEN_END :
					break;
				case TOKEN_EQUAL :
				case TOKEN_NOT_EQUAL :
				case TOKEN_GREATER :
				case TOKEN_GREATER_EQUAL :
				case TOKEN_LESS :
				case TOKEN_LESS_EQUAL :
				case TOKEN_MATCHES :
					int realToken = currentToken;
					nextToken();
					IExpression rhs;
					if (realToken == TOKEN_MATCHES && currentToken == TOKEN_LITERAL && tokenValue instanceof String)
						rhs = factory.constant(new LDAPApproximation((String) tokenValue));
					else
						rhs = parseNot();
					switch (realToken) {
						case TOKEN_EQUAL :
							expr = factory.equals(expr, rhs);
							break;
						case TOKEN_NOT_EQUAL :
							expr = factory.not(factory.equals(expr, rhs));
							break;
						case TOKEN_GREATER :
							expr = factory.greater(expr, rhs);
							break;
						case TOKEN_GREATER_EQUAL :
							expr = factory.greaterEqual(expr, rhs);
							break;
						case TOKEN_LESS :
							expr = factory.less(expr, rhs);
							break;
						case TOKEN_LESS_EQUAL :
							expr = factory.lessEqual(expr, rhs);
							break;
						default :
							expr = factory.matches(expr, rhs);
					}
					continue;
				default :
					throw syntaxError();
			}
			break;
		}
		return expr;
	}

	protected IExpression parseNot() {
		if (currentToken == TOKEN_NOT) {
			nextToken();
			IExpression expr = parseNot();
			return factory.not(expr);
		}
		return parseCollectionExpression();
	}

	protected IExpression parseCollectionExpression() {
		IExpression expr = parseCollectionLHS();
		if (expr == null) {
			expr = parseMember();
			if (currentToken != TOKEN_DOT)
				return expr;
			nextToken();
		}
		for (;;) {
			int funcToken = currentToken;
			nextToken();
			assertToken(TOKEN_LP);
			nextToken();
			expr = parseCollectionRHS(expr, funcToken);
			if (currentToken != TOKEN_DOT)
				break;
			nextToken();
		}
		return expr;
	}

	protected IExpression parseCollectionLHS() {
		IExpression expr = null;
		switch (currentToken) {
			case TOKEN_EXISTS :
			case TOKEN_ALL :
				expr = getVariableOrRootMember(rootVariable);
				break;
		}
		return expr;
	}

	protected IExpression parseCollectionRHS(IExpression expr, int funcToken) {
		switch (funcToken) {
			case TOKEN_EXISTS :
				expr = factory.exists(expr, parseLambdaDefinition());
				break;
			case TOKEN_ALL :
				expr = factory.all(expr, parseLambdaDefinition());
				break;
			default :
				throw syntaxError();
		}
		return expr;
	}

	protected IExpression parseLambdaDefinition() {
		assertToken(TOKEN_IDENTIFIER);
		IExpression each = factory.variable((String) tokenValue);
		push(each);
		try {
			nextToken();
			assertToken(TOKEN_PIPE);
			nextToken();
			IExpression body = parseCondition();
			assertToken(TOKEN_RP);
			nextToken();
			return factory.lambda(each, body);
		} finally {
			pop();
		}
	}

	protected IExpression parseMember() {
		IExpression expr = parseUnary();
		String name;
		while (currentToken == TOKEN_DOT || currentToken == TOKEN_LB) {
			int savePos = tokenPos;
			int saveToken = currentToken;
			Object saveTokenValue = tokenValue;
			nextToken();
			if (saveToken == TOKEN_DOT) {
				switch (currentToken) {
					case TOKEN_IDENTIFIER :
						name = (String) tokenValue;
						nextToken();
						expr = factory.member(expr, name);
						break;

					default :
						tokenPos = savePos;
						currentToken = saveToken;
						tokenValue = saveTokenValue;
						return expr;
				}
			} else {
				IExpression atExpr = parseMember();
				assertToken(TOKEN_RB);
				nextToken();
				expr = factory.at(expr, atExpr);
			}
		}
		return expr;
	}

	protected IExpression parseUnary() {
		IExpression expr;
		switch (currentToken) {
			case TOKEN_LP :
				nextToken();
				expr = parseCondition();
				assertToken(TOKEN_RP);
				nextToken();
				break;
			case TOKEN_LITERAL :
				expr = factory.constant(tokenValue);
				nextToken();
				break;
			case TOKEN_IDENTIFIER :
				expr = getVariableOrRootMember((String) tokenValue);
				nextToken();
				break;
			case TOKEN_NULL :
				expr = factory.constant(null);
				nextToken();
				break;
			case TOKEN_TRUE :
				expr = factory.constant(Boolean.TRUE);
				nextToken();
				break;
			case TOKEN_FALSE :
				expr = factory.constant(Boolean.FALSE);
				nextToken();
				break;
			case TOKEN_DOLLAR :
				expr = parseParameter();
				break;
			default :
				throw syntaxError();
		}
		return expr;
	}

	private IExpression parseParameter() {
		if (currentToken == TOKEN_DOLLAR) {
			nextToken();
			if (currentToken == TOKEN_LITERAL && tokenValue instanceof Integer) {
				IExpression param = factory.indexedParameter(((Integer) tokenValue).intValue());
				nextToken();
				return param;
			}
		}
		throw syntaxError();
	}

	protected IExpression[] parseArray() {
		IExpression expr = parseCondition();
		if (currentToken != TOKEN_COMMA)
			return new IExpression[] {expr};

		ArrayList<IExpression> operands = new ArrayList<>();
		operands.add(expr);
		do {
			nextToken();
			if (currentToken == TOKEN_LC)
				// We don't allow lambdas in the array
				break;
			operands.add(parseCondition());
		} while (currentToken == TOKEN_COMMA);
		return operands.toArray(new IExpression[operands.size()]);
	}

	protected void assertToken(int token) {
		if (currentToken != token)
			throw syntaxError();
	}

	protected IExpression getVariableOrRootMember(String id) {
		int idx = size();
		while (--idx >= 0) {
			IExpression v = get(idx);
			if (id.equals(v.toString()))
				return v;
		}

		if (rootVariable == null || rootVariable.equals(id))
			throw syntaxError("No such variable: " + id); //$NON-NLS-1$

		return factory.member(getVariableOrRootMember(rootVariable), id);
	}

	protected void nextToken() {
		tokenValue = null;
		int top = expression.length();
		char c = 0;
		while (tokenPos < top) {
			c = expression.charAt(tokenPos);
			if (!Character.isWhitespace(c))
				break;
			++tokenPos;
		}
		if (tokenPos >= top) {
			lastTokenPos = top;
			currentToken = TOKEN_END;
			return;
		}

		lastTokenPos = tokenPos;
		switch (c) {
			case '|' :
				if (tokenPos + 1 < top && expression.charAt(tokenPos + 1) == '|') {
					tokenValue = OPERATOR_OR;
					currentToken = TOKEN_OR;
					tokenPos += 2;
				} else {
					currentToken = TOKEN_PIPE;
					++tokenPos;
				}
				break;

			case '&' :
				if (tokenPos + 1 < top && expression.charAt(tokenPos + 1) == '&') {
					tokenValue = OPERATOR_AND;
					currentToken = TOKEN_AND;
					tokenPos += 2;
				} else
					currentToken = TOKEN_ERROR;
				break;

			case '=' :
				if (tokenPos + 1 < top && expression.charAt(tokenPos + 1) == '=') {
					tokenValue = OPERATOR_EQUALS;
					currentToken = TOKEN_EQUAL;
					tokenPos += 2;
				} else
					currentToken = TOKEN_ERROR;
				break;

			case '!' :
				if (tokenPos + 1 < top && expression.charAt(tokenPos + 1) == '=') {
					tokenValue = OPERATOR_NOT_EQUALS;
					currentToken = TOKEN_NOT_EQUAL;
					tokenPos += 2;
				} else {
					currentToken = TOKEN_NOT;
					++tokenPos;
				}
				break;

			case '~' :
				if (tokenPos + 1 < top && expression.charAt(tokenPos + 1) == '=') {
					tokenValue = OPERATOR_MATCHES;
					currentToken = TOKEN_MATCHES;
					tokenPos += 2;
				} else
					currentToken = TOKEN_ERROR;
				break;

			case '>' :
				if (tokenPos + 1 < top && expression.charAt(tokenPos + 1) == '=') {
					tokenValue = OPERATOR_GT_EQUAL;
					currentToken = TOKEN_GREATER_EQUAL;
					tokenPos += 2;
				} else {
					currentToken = TOKEN_GREATER;
					++tokenPos;
				}
				break;

			case '<' :
				if (tokenPos + 1 < top && expression.charAt(tokenPos + 1) == '=') {
					tokenValue = OPERATOR_LT_EQUAL;
					currentToken = TOKEN_LESS_EQUAL;
					tokenPos += 2;
				} else {
					currentToken = TOKEN_LESS;
					++tokenPos;
				}
				break;

			case '?' :
				currentToken = TOKEN_IF;
				++tokenPos;
				break;

			case ':' :
				currentToken = TOKEN_ELSE;
				++tokenPos;
				break;

			case '.' :
				currentToken = TOKEN_DOT;
				++tokenPos;
				break;

			case '$' :
				currentToken = TOKEN_DOLLAR;
				++tokenPos;
				break;

			case '{' :
				currentToken = TOKEN_LC;
				++tokenPos;
				break;

			case '}' :
				currentToken = TOKEN_RC;
				++tokenPos;
				break;

			case '(' :
				currentToken = TOKEN_LP;
				++tokenPos;
				break;

			case ')' :
				currentToken = TOKEN_RP;
				++tokenPos;
				break;

			case '[' :
				currentToken = TOKEN_LB;
				++tokenPos;
				break;

			case ']' :
				currentToken = TOKEN_RB;
				++tokenPos;
				break;

			case ',' :
				currentToken = TOKEN_COMMA;
				++tokenPos;
				break;

			case '"' :
			case '\'' :
				parseDelimitedString(c);
				break;

			case '/' :
				parseDelimitedString(c);
				if (currentToken == TOKEN_LITERAL)
					tokenValue = SimplePattern.compile((String) tokenValue);
				break;

			default :
				if (Character.isDigit(c)) {
					int start = tokenPos++;
					while (tokenPos < top && Character.isDigit(expression.charAt(tokenPos)))
						++tokenPos;
					tokenValue = Integer.valueOf(expression.substring(start, tokenPos));
					currentToken = TOKEN_LITERAL;
					break;
				}
				if (Character.isJavaIdentifierStart(c)) {
					int start = tokenPos++;
					while (tokenPos < top && Character.isJavaIdentifierPart(expression.charAt(tokenPos)))
						++tokenPos;
					String word = expression.substring(start, tokenPos);
					Integer token = keywordToTokenMap().get(word);
					if (token == null)
						currentToken = TOKEN_IDENTIFIER;
					else
						currentToken = token.intValue();
					tokenValue = word;
					break;
				}
				throw syntaxError();
		}
	}

	protected void popVariable() {
		if (isEmpty())
			throw syntaxError();
		pop();
	}

	protected ExpressionParseException syntaxError() {
		Object tv = tokenValue;
		if (tv == null) {
			if (lastTokenPos >= expression.length())
				return syntaxError("Unexpected end of expression"); //$NON-NLS-1$
			tv = expression.substring(lastTokenPos, lastTokenPos + 1);
		}
		return syntaxError("Unexpected token \"" + tv + '"'); //$NON-NLS-1$
	}

	protected ExpressionParseException syntaxError(String message) {
		return new ExpressionParseException(expression, message, tokenPos);
	}

	private void parseDelimitedString(char delim) {
		int start = ++tokenPos;
		StringBuffer buf = new StringBuffer();
		int top = expression.length();
		while (tokenPos < top) {
			char ec = expression.charAt(tokenPos);
			if (ec == delim)
				break;
			if (ec == '\\') {
				if (++tokenPos == top)
					break;
				ec = expression.charAt(tokenPos);
			}
			buf.append(ec);
			++tokenPos;
		}
		if (tokenPos == top) {
			tokenPos = start - 1;
			currentToken = TOKEN_ERROR;
		} else {
			++tokenPos;
			tokenValue = buf.toString();
			currentToken = TOKEN_LITERAL;
		}
	}
}

Back to the top