Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5eddc2295a7b68e5ef465c0760460fec5e4487c4 (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
/*******************************************************************************
 * Copyright (c) 2005-2009 itemis AG (http://www.itemis.eu) 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
 *
 *******************************************************************************/

package org.eclipse.internal.xpand2.codeassist;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.internal.xpand2.ast.Advice;
import org.eclipse.internal.xpand2.model.XpandAdvice;
import org.eclipse.internal.xpand2.model.XpandDefinition;
import org.eclipse.internal.xpand2.model.XpandResource;
import org.eclipse.internal.xpand2.type.DefinitionType;
import org.eclipse.internal.xpand2.type.IteratorType;
import org.eclipse.internal.xtend.xtend.codeassist.Partition;
import org.eclipse.xpand2.XpandExecutionContext;
import org.eclipse.xtend.expression.AnalysationIssue;
import org.eclipse.xtend.expression.ExecutionContext;
import org.eclipse.xtend.expression.ExpressionFacade;
import org.eclipse.xtend.expression.Variable;
import org.eclipse.xtend.typesystem.ParameterizedType;
import org.eclipse.xtend.typesystem.Type;

public class FastAnalyzer {

	private static final Pattern PARAM_PATTERN = Pattern.compile("([\\[\\]:\\w]+)\\s+([\\w]+)");

	private final static Pattern IMPORT_PATTERN = Pattern.compile(XpandTokens.LT + "\\s*IMPORT" + "\\s+([\\w\\:]+)\\s*" + XpandTokens.RT);

	private final static Pattern EXTENSION_PATTERN = Pattern.compile(XpandTokens.LT + "\\s*EXTENSION\\s+([\\w\\:]+)\\s*" + XpandTokens.RT);

	private final static Pattern INCOMPLETE_IMPORT_PATTERN = Pattern.compile(XpandTokens.LT + "\\s*IMPORT\\s+[\\w\\:]*\\z");

	private final static Pattern INCOMPLETE_EXTENSION_PATTERN = Pattern.compile(XpandTokens.LT + "\\s*EXTENSION\\s+[\\w\\:]*\\z");

	private final static Pattern DEFINE_PATTERN = Pattern
			.compile("(DEFINE|AROUND)\\s*(([\\w\\*:]+)\\s*(\\(([\\[\\]:\\w\\s\\,]*)\\*?\\s*\\))?\\s*FOR\\s*([\\[\\]:\\w\\s]+))");

	private final static Pattern BLOCK_PATTERN = Pattern.compile(getBlockPattern());

	private final static Pattern FOREACH_PATTERN = Pattern.compile("FOREACH\\s+(.+)\\s+AS\\s+(\\w+)(\\s+ITERATOR\\s+(\\w+))?");

	public final static Pattern EXPAND_PATTERN = Pattern.compile("EXPAND\\s+([\\w:]*)\\z");

	private final static Pattern LET_PATTERN = Pattern.compile("LET\\s+(.+)\\s+AS\\s+(\\w+)");

	private final static Pattern TYPEDECL_DEFINE_PATTERN1 = Pattern.compile("(DEFINE|AROUND)\\s*[\\w\\*:]+\\s*\\(([^\\)]*)\\z");

	private final static Pattern TYPEDECL_DEFINE_PATTERN2 = Pattern
			.compile("(DEFINE|AROUND)\\s*[\\w\\*:]+\\s*(\\([\\[\\]:\\w\\s\\,]*\\*?\\s*\\))?\\s*FOR\\s+[^" + XpandTokens.RT + "\\s]*\\z");

	private final static Pattern TYPEDECL_PARAM_PATTERN = Pattern.compile("(,|\\(|\\A)\\s*[\\[\\]:\\w]*\\z");

	private final static Pattern TYPEDECL_TYPESELECT_PATTERN = Pattern.compile("typeSelect\\(\\s*[\\[\\]:\\w]*\\z");

	private final static Pattern IN_TAG_PATTERN = Pattern.compile(XpandTokens.LT + "([^" + XpandTokens.RT + "]*)\\z");

	protected FastAnalyzer() {
		// only protected for testcases
	}

	public static boolean isInExpand(final String str) {
		return EXPAND_PATTERN.matcher(str).find();
	}

	public static boolean isInComment(final String str) {
		final int index = str.lastIndexOf(XpandTokens.LT + "REM" + XpandTokens.RT);
		if (index >= 0) {
			final int index2 = str.lastIndexOf(XpandTokens.LT + "ENDREM" + XpandTokens.RT);
			if (index2 == -1)
				return true;
			return index2 < index;
		}
		return false;
	}

	public static boolean isInDefine(final String str) {
		return !computeStack(str).isEmpty();
	}

	public static boolean isInTypeDecl(final String str) {
		Matcher m = IN_TAG_PATTERN.matcher(str);
		if (!m.find())
			return false;
		final String tag = m.group(1);
		m = TYPEDECL_DEFINE_PATTERN1.matcher(tag);
		if (m.find()) {
			m = TYPEDECL_PARAM_PATTERN.matcher(m.group(2));
			return m.find();
		} else {
			m = TYPEDECL_DEFINE_PATTERN2.matcher(tag);
			if (m.find())
				return true;
			else {
				m = TYPEDECL_TYPESELECT_PATTERN.matcher(tag);
				return m.find();
			}
		}

	}

	private static String getBlockPattern() {
		final String[] parts = new String[] { XpandTokens.DEFINE, XpandTokens.AROUND, XpandTokens.FOREACH, XpandTokens.LET, XpandTokens.IF, XpandTokens.FILE,
				XpandTokens.PROTECT };
		final StringBuffer buff = new StringBuffer();
		for (int i = 0; i < parts.length; i++) {
			final String part = parts[i];
			buff.append(XpandTokens.LT).append("\\s*").append(part);
			buff.append("|");
			buff.append(XpandTokens.LT).append("\\s*").append("END").append(part);
			if (i < parts.length - 1)
				buff.append("|");
		}
		return buff.toString();
	}

	public final static List<String> findImports(final String template) {
		final Matcher m = IMPORT_PATTERN.matcher(template);
		final List<String> result = new ArrayList<String>();
		while (m.find()) {
			result.add(m.group(1));
		}
		return result;
	}

	public final static List<String> findExtensions(final String template) {
		final Matcher m = EXTENSION_PATTERN.matcher(template);
		final List<String> result = new ArrayList<String>();
		while (m.find()) {
			result.add(m.group(1));
		}
		return result;
	}

	public final static Stack<StackElement> computeStack(String templatePart) {

		int start = templatePart.lastIndexOf(XpandTokens.ENDDEFINE);
		final int start1 = templatePart.lastIndexOf(XpandTokens.ENDAROUND);
		if (start1 > start) {
			start = start1;
		}
		if (start > 0) {
			templatePart = templatePart.substring(start);
		}

		final Stack<StackElement> stack = new Stack<StackElement>();

		final Matcher matcher = BLOCK_PATTERN.matcher(templatePart);
		while (matcher.find()) {
			final String txt = matcher.group();
			// handle variable scope
			if (txt.endsWith(XpandTokens.ENDFOREACH)) {
				stack.pop();
			} else if (txt.endsWith(XpandTokens.ENDLET)) {
				stack.pop();
			} else if (txt.endsWith(XpandTokens.ENDIF)) {
				stack.pop();
			} else if (txt.endsWith(XpandTokens.ENDPROTECT)) {
				stack.pop();
			} else if (txt.endsWith(XpandTokens.ENDFILE)) {
				stack.pop();
			} else if (txt.endsWith(XpandTokens.ENDDEFINE)) {
				stack.pop();
			} else if (txt.endsWith(XpandTokens.ENDAROUND)) {
				stack.pop();

			} else if (txt.endsWith(XpandTokens.DEFINE) || txt.endsWith(XpandTokens.AROUND)) {
				final StackElement se = new StackElement();
				if (txt.endsWith(XpandTokens.AROUND)) {
					se.block = XpandTokens.AROUND;
				} else {
					se.block = XpandTokens.DEFINE;
				}
				Matcher m = DEFINE_PATTERN.matcher(templatePart.substring(matcher.start()));
				if (m.find()) {
					LazyVar ctx = new LazyVar();
					ctx.typeName = m.group(6).trim();
					ctx.name = ExecutionContext.IMPLICIT_VARIABLE;
					se.variables.put(ctx.name, ctx);

					final String params = m.group(5);
					if (params != null && !"".equals(params.trim())) {
						final StringTokenizer st = new StringTokenizer(params, ",");
						while (st.hasMoreTokens()) {
							final String param = st.nextToken();
							m = PARAM_PATTERN.matcher(param);
							m.find();
							ctx = new LazyVar();
							ctx.typeName = m.group(1).trim();
							ctx.name = m.group(2).trim();
							se.variables.put(ctx.name, ctx);
						}
					}
					if (se.block.equals(XpandTokens.AROUND)) {
						ctx = new LazyVar();
						ctx.typeName = DefinitionType.TYPE_NAME;
						ctx.name = Advice.DEF_VAR_NAME;
						se.variables.put(ctx.name, ctx);
					}
					stack.push(se);
				}
			} else if (txt.endsWith(XpandTokens.FOREACH)) {
				final StackElement se = new StackElement();
				se.block = XpandTokens.FOREACH;
				final Matcher m = FOREACH_PATTERN.matcher(templatePart.substring(matcher.start()));
				if (m.find()) {
					LazyVar ctx = new LazyVar();
					ctx.expression = m.group(1);
					ctx.name = m.group(2);
					ctx.forEach = true;
					se.variables.put(ctx.name, ctx);
					stack.push(se);

					if (m.group(3) != null) {
						ctx = new LazyVar();
						ctx.typeName = IteratorType.TYPE_NAME;
						ctx.name = m.group(4);
						se.variables.put(ctx.name, ctx);
					}
				}

			} else if (txt.endsWith(XpandTokens.LET)) {
				final StackElement se = new StackElement();
				se.block = XpandTokens.LET;
				final Matcher m = LET_PATTERN.matcher(templatePart.substring(matcher.start()));
				if (m.find()) {
					final LazyVar ctx = new LazyVar();
					ctx.expression = m.group(1);
					ctx.name = m.group(2);
					se.variables.put(ctx.name, ctx);
					stack.push(se);
				}
			} else if (txt.endsWith(XpandTokens.IF)) {
				final StackElement se = new StackElement();
				se.block = XpandTokens.IF;
				stack.push(se);
			} else if (txt.endsWith(XpandTokens.PROTECT)) {
				final StackElement se = new StackElement();
				se.block = XpandTokens.PROTECT;
				stack.push(se);
			} else if (txt.endsWith(XpandTokens.FILE)) {
				final StackElement se = new StackElement();
				se.block = XpandTokens.FILE;
				stack.push(se);
			}
		}
		return stack;
	}

	/**
	 * DO NOT CALL THIS METHOD. PUBLIC FOR TEST ONLY
	 */
	public static boolean isInExtensionImport(final String s) {
		final Matcher m = INCOMPLETE_EXTENSION_PATTERN.matcher(s);
		return m.find();
	}

	/**
	 * DO NOT CALL THIS METHOD. PUBLIC FOR TEST ONLY
	 */
	public static boolean isInImport(final String s) {
		final Matcher m = INCOMPLETE_IMPORT_PATTERN.matcher(s);
		return m.find();
	}

	public final static Partition computePartition(final String str) {
		if (!isInTag(str))
			return Partition.DEFAULT;

		if (isInComment(str))
			return Partition.COMMENT;

		if (isInExpand(str))
			return XpandPartition.EXPAND_STATEMENT;

		if (isInImport(str))
			return Partition.NAMESPACE_IMPORT;

		if (isInExtensionImport(str))
			return Partition.EXTENSION_IMPORT;

		if (isInTypeDecl(str))
			return Partition.TYPE_DECLARATION;

		return Partition.EXPRESSION;
	}

	public final static XpandExecutionContext computeExecutionContext(final String str, XpandExecutionContext ctx, final XpandDefinition[] definitions) {
		final Partition p = computePartition(str);
		if (p == Partition.TYPE_DECLARATION || p == Partition.EXPRESSION || p == XpandPartition.EXPAND_STATEMENT) {

			final List<String> imports = findImports(str);
			final List<String> extensions = findExtensions(str);
			final XpandResource tpl = new XpandResource() {

				public XpandDefinition[] getDefinitions() {
					return definitions;
				}

				public XpandDefinition[] getDefinitionsByName(String name) {
					List<XpandDefinition> result = new ArrayList<XpandDefinition>();
					for (int i = 0; i < definitions.length; i++) {
						XpandDefinition definition = definitions[i];
						if (definition.getName().equals(name)) {
							result.add(definition);
						}
					}
					return extensions.toArray(new XpandDefinition[extensions.size()]);
				}

				private String fqn;

				public String getFullyQualifiedName() {
					return fqn;
				}

				public void setFullyQualifiedName(String fqn) {
					this.fqn = fqn;
				}

				public String[] getImportedNamespaces() {
					return imports.toArray(new String[imports.size()]);
				}

				public String[] getImportedExtensions() {
					return extensions.toArray(new String[extensions.size()]);
				}

				public void analyze(XpandExecutionContext ctx, Set<AnalysationIssue> issues) {

				}

				public XpandAdvice[] getAdvices() {
					return new XpandAdvice[0];
				}
			};

			ctx = (XpandExecutionContext) ctx.cloneWithResource(tpl);

			final Stack<StackElement> s = computeStack(str);

			for (final Iterator<StackElement> iter = s.iterator(); iter.hasNext();) {
				final StackElement element = iter.next();
				final Collection<LazyVar> vars = element.variables.values();
				for (final Iterator<LazyVar> iterator = vars.iterator(); iterator.hasNext();) {
					final LazyVar v = iterator.next();
					Type vType = null;
					if (v.typeName != null) {
						vType = ctx.getTypeForName(v.typeName);
					} else {
						vType = new ExpressionFacade(ctx).analyze(v.expression, new HashSet<AnalysationIssue>());
						if (v.forEach) {
							if (vType instanceof ParameterizedType) {
								vType = ((ParameterizedType) vType).getInnerType();
							} else {
								vType = null;
							}
						}
					}
					ctx = (XpandExecutionContext) ctx.cloneWithVariable(new Variable(v.name, vType));
				}
			}
		}
		return ctx;
	}

	public static boolean isInTag(final String str) {
		return IN_TAG_PATTERN.matcher(str).find();
	}

}

Back to the top