Skip to main content
summaryrefslogtreecommitdiffstats
blob: ad257f9646f7261d923e7b56eab0f95ebbb18615 (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
/*******************************************************************************
 * Copyright (c) 2009, 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.Variable;
import org.eclipse.equinox.p2.metadata.expression.IExpression;
import org.eclipse.equinox.p2.metadata.expression.IExpressionFactory;

public class QLParser extends ExpressionParser {
	private static final long serialVersionUID = 882034383978853143L;

	private static final int TOKEN_ANY = 42;

	private static final int TOKEN_LATEST = 70;
	private static final int TOKEN_LIMIT = 71;
	private static final int TOKEN_FIRST = 72;
	private static final int TOKEN_FLATTEN = 73;
	private static final int TOKEN_UNIQUE = 74;
	private static final int TOKEN_SELECT = 75;
	private static final int TOKEN_COLLECT = 76;
	private static final int TOKEN_TRAVERSE = 77;
	private static final int TOKEN_INTERSECT = 78;
	private static final int TOKEN_UNION = 79;

	private static final Map<String, Integer> qlKeywords;
	static {
		qlKeywords = new HashMap<>();
		qlKeywords.putAll(keywords);
		qlKeywords.put(KEYWORD_COLLECT, Integer.valueOf(TOKEN_COLLECT));
		qlKeywords.put(KEYWORD_FALSE, Integer.valueOf(TOKEN_FALSE));
		qlKeywords.put(KEYWORD_FIRST, Integer.valueOf(TOKEN_FIRST));
		qlKeywords.put(KEYWORD_FLATTEN, Integer.valueOf(TOKEN_FLATTEN));
		qlKeywords.put(KEYWORD_LATEST, Integer.valueOf(TOKEN_LATEST));
		qlKeywords.put(KEYWORD_LIMIT, Integer.valueOf(TOKEN_LIMIT));
		qlKeywords.put(KEYWORD_NULL, Integer.valueOf(TOKEN_NULL));
		qlKeywords.put(KEYWORD_SELECT, Integer.valueOf(TOKEN_SELECT));
		qlKeywords.put(KEYWORD_TRAVERSE, Integer.valueOf(TOKEN_TRAVERSE));
		qlKeywords.put(KEYWORD_TRUE, Integer.valueOf(TOKEN_TRUE));
		qlKeywords.put(KEYWORD_UNIQUE, Integer.valueOf(TOKEN_UNIQUE));
		qlKeywords.put(KEYWORD_INTERSECT, Integer.valueOf(TOKEN_INTERSECT));
		qlKeywords.put(KEYWORD_UNION, Integer.valueOf(TOKEN_UNION));
		qlKeywords.put(OPERATOR_EACH, Integer.valueOf(TOKEN_ANY));
	}

	public QLParser(IExpressionFactory factory) {
		super(factory);
	}

	@Override
	protected Map<String, Integer> keywordToTokenMap() {
		return qlKeywords;
	}

	@Override
	protected IExpression parseCondition() {
		IExpression expr = parseOr();
		if (currentToken == TOKEN_IF) {
			nextToken();
			IExpression ifTrue = parseOr();
			assertToken(TOKEN_ELSE);
			nextToken();
			expr = factory.condition(expr, ifTrue, parseOr());
		}
		return expr;
	}

	@Override
	protected IExpression parseMember() {
		IExpression expr = parseFunction();
		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();
						if (currentToken == TOKEN_LP) {
							nextToken();
							IExpression[] callArgs = parseArray();
							assertToken(TOKEN_RP);
							nextToken();
							expr = factory.memberCall(expr, name, callArgs);
						} else
							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 parseFunction() {
		if (currentToken == TOKEN_IDENTIFIER) {
			Object function = factory.getFunctionMap().get(tokenValue);
			if (function != null) {
				int savePos = tokenPos;
				int saveToken = currentToken;
				Object saveTokenValue = tokenValue;

				nextToken();
				if (currentToken == TOKEN_LP) {
					nextToken();
					IExpression[] args = currentToken == TOKEN_RP ? IExpressionFactory.NO_ARGS : parseArray();
					assertToken(TOKEN_RP);
					nextToken();
					return factory.function(function, args);
				}
				tokenPos = savePos;
				currentToken = saveToken;
				tokenValue = saveTokenValue;
			}
		}
		return parseUnary();
	}

	@Override
	protected IExpression parseCollectionLHS() {
		IExpression expr;
		switch (currentToken) {
			case TOKEN_SELECT :
			case TOKEN_COLLECT :
			case TOKEN_FIRST :
			case TOKEN_FLATTEN :
			case TOKEN_TRAVERSE :
			case TOKEN_LATEST :
			case TOKEN_LIMIT :
			case TOKEN_INTERSECT :
			case TOKEN_UNION :
			case TOKEN_UNIQUE :
				expr = getVariableOrRootMember(rootVariable);
				break;
			default :
				expr = super.parseCollectionLHS();
		}
		return expr;
	}

	@Override
	protected IExpression parseCollectionRHS(IExpression expr, int funcToken) {
		switch (funcToken) {
			case TOKEN_SELECT :
				expr = factory.select(expr, parseLambdaDefinition());
				break;
			case TOKEN_COLLECT :
				expr = factory.collect(expr, parseLambdaDefinition());
				break;
			case TOKEN_FIRST :
				expr = factory.first(expr, parseLambdaDefinition());
				break;
			case TOKEN_TRAVERSE :
				expr = factory.traverse(expr, parseLambdaDefinition());
				break;
			case TOKEN_LATEST :
				if (currentToken == TOKEN_RP) {
					expr = factory.latest(expr);
					assertToken(TOKEN_RP);
					nextToken();
				} else
					expr = factory.latest(factory.select(expr, parseLambdaDefinition()));
				break;
			case TOKEN_FLATTEN :
				if (currentToken == TOKEN_RP) {
					expr = factory.flatten(expr);
					assertToken(TOKEN_RP);
					nextToken();
				} else
					expr = factory.flatten(factory.select(expr, parseLambdaDefinition()));
				break;
			case TOKEN_LIMIT :
				expr = factory.limit(expr, parseCondition());
				assertToken(TOKEN_RP);
				nextToken();
				break;
			case TOKEN_INTERSECT :
				expr = factory.intersect(expr, parseCondition());
				assertToken(TOKEN_RP);
				nextToken();
				break;
			case TOKEN_UNION :
				expr = factory.union(expr, parseCondition());
				assertToken(TOKEN_RP);
				nextToken();
				break;
			case TOKEN_UNIQUE :
				if (currentToken == TOKEN_RP)
					expr = factory.unique(expr, factory.constant(null));
				else {
					expr = factory.unique(expr, parseMember());
					assertToken(TOKEN_RP);
					nextToken();
				}
				break;
			default :
				expr = super.parseCollectionRHS(expr, funcToken);
		}
		return expr;
	}

	@Override
	protected IExpression parseUnary() {
		IExpression expr;
		switch (currentToken) {
			case TOKEN_LB :
				nextToken();
				expr = factory.array(parseArray());
				assertToken(TOKEN_RB);
				nextToken();
				break;
			case TOKEN_ANY :
				expr = factory.variable(OPERATOR_EACH);
				nextToken();
				break;
			default :
				expr = super.parseUnary();
		}
		return expr;
	}

	@Override
	protected IExpression parseLambdaDefinition() {
		boolean endingRC = false;
		int anyIndex = -1;
		IExpression[] initializers = IExpressionFactory.NO_ARGS;
		IExpression[] variables;
		if (currentToken == TOKEN_LC) {
			// Lambda starts without currying.
			endingRC = true;
			nextToken();
			anyIndex = 0;
			variables = parseVariables();
			if (variables == null)
				// empty means no pipe at the end.
				throw syntaxError();
		} else {
			anyIndex = 0;
			variables = parseVariables();
			if (variables == null) {
				anyIndex = -1;
				initializers = parseArray();
				assertToken(TOKEN_LC);
				nextToken();
				endingRC = true;
				for (int idx = 0; idx < initializers.length; ++idx) {
					IExpression initializer = initializers[idx];
					if (initializer instanceof Variable && OPERATOR_EACH.equals(initializer.toString())) {
						if (anyIndex == -1)
							anyIndex = idx;
						else
							anyIndex = -1; // Second Each. This is illegal
						break;
					}
				}
				if (anyIndex == -1)
					throw new IllegalArgumentException("Exaclty one _ must be present among the currying expressions"); //$NON-NLS-1$

				variables = parseVariables();
				if (variables == null)
					// empty means no pipe at the end.
					throw syntaxError();
			}

		}
		nextToken();
		IExpression body = parseCondition();
		if (endingRC) {
			assertToken(TOKEN_RC);
			nextToken();
		}

		assertToken(TOKEN_RP);
		nextToken();
		IExpression each;
		IExpression[] assignments;
		if (initializers.length == 0) {
			if (variables.length != 1)
				throw new IllegalArgumentException("Must have exactly one variable unless currying is used"); //$NON-NLS-1$
			each = variables[0];
			assignments = IExpressionFactory.NO_ARGS;
		} else {
			if (initializers.length != variables.length)
				throw new IllegalArgumentException("Number of currying expressions and variables differ"); //$NON-NLS-1$

			if (initializers.length == 1) {
				// This is just a map from _ to some variable
				each = variables[0];
				assignments = IExpressionFactory.NO_ARGS;
			} else {
				int idx;
				each = variables[anyIndex];
				assignments = new IExpression[initializers.length - 1];
				for (idx = 0; idx < anyIndex; ++idx)
					assignments[idx] = factory.assignment(variables[idx], initializers[idx]);
				for (++idx; idx < initializers.length; ++idx)
					assignments[idx] = factory.assignment(variables[idx], initializers[idx]);
			}
		}
		return factory.lambda(each, assignments, body);
	}

	private IExpression[] parseVariables() {
		int savePos = tokenPos;
		int saveToken = currentToken;
		Object saveTokenValue = tokenValue;
		List<Object> ids = null;
		while (currentToken == TOKEN_IDENTIFIER) {
			if (ids == null)
				ids = new ArrayList<>();
			ids.add(tokenValue);
			nextToken();
			if (currentToken == TOKEN_COMMA) {
				nextToken();
				continue;
			}
			break;
		}

		if (currentToken != TOKEN_PIPE) {
			// This was not a variable list
			tokenPos = savePos;
			currentToken = saveToken;
			tokenValue = saveTokenValue;
			return null;
		}

		if (ids == null)
			// Empty list but otherwise OK
			return IExpressionFactory.NO_ARGS;

		int top = ids.size();
		IExpression[] result = new IExpression[top];
		for (int idx = 0; idx < top; ++idx) {
			String name = (String) ids.get(idx);
			IExpression var = factory.variable(name);
			push(var);
			result[idx] = var;
		}
		return result;
	}
}

Back to the top