Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8ec346090344b23dacd4bedd721f6ce128ba6390 (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
/*******************************************************************************
 * 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.xtend.xtend.codeassist;

import java.util.ArrayList;
import java.util.Collections;
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.xtend.expression.codeassist.LazyVar;
import org.eclipse.internal.xtend.xtend.XtendFile;
import org.eclipse.internal.xtend.xtend.ast.Around;
import org.eclipse.internal.xtend.xtend.ast.Extension;
import org.eclipse.internal.xtend.xtend.types.AdviceContextType;
import org.eclipse.xtend.expression.AnalysationIssue;
import org.eclipse.xtend.expression.ExecutionContext;
import org.eclipse.xtend.expression.ExpressionFacade;
import org.eclipse.xtend.expression.ResourceManager;
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("import\\s+([\\w\\:]+)\\s*;");

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

	private final static Pattern EXTENSION_PATTERN = Pattern
			.compile("extension\\s+([\\w\\:]+)\\s*(reexport)?\\s*;");

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

	private final static Pattern FUNCTION_PATTERN = Pattern
			.compile("((\\w+)\\s+)?(create\\s+([\\w:]+)(\\s+(\\w+))?\\s+)?([\\w:]+)\\s*\\(\\s*([\\[\\]:\\w\\s\\,]+)?\\s*\\)\\s*:\\s*[^;]*\\z");

	private final static Pattern AROUND_PATTERN = Pattern
			.compile("around\\s+([\\w:*]+)\\s*\\(\\s*([\\[\\]:\\w\\s\\,]+)?[\\s,*]*\\)\\s*:\\s*[^;]*\\z");

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

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

	private FastAnalyzer() {
	}

	public static boolean isInsideTypeDeclaration(final String s) {
		final Matcher m = TYPEDECL_PATTERN.matcher(s);
		if (m.find())
			return TYPEDECL_PARAM_PATTERN.matcher(m.group(2)).find();
		return false;
	}

	public static boolean isInsideExtensionImport(final String s) {
		final Matcher m = INCOMPLETE_EXTENSION_PATTERN.matcher(s);
		return m.find();
	}

	public static boolean isInsideImport(final String s) {
		final Matcher m = INCOMPLETE_IMPORT_PATTERN.matcher(s);
		return m.find();
	}

	public static boolean isInsideExpression(final String s) {
		final Matcher m1 = AROUND_PATTERN.matcher(s);
		if (!m1.find()) {
			final Matcher m = FUNCTION_PATTERN.matcher(s);
			return m.find();
		}
		return true;
	}

	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<Set<LazyVar>> computeStack(String toAnalyze) {
		Matcher m = AROUND_PATTERN.matcher(toAnalyze);
		Pattern p = AROUND_PATTERN;
		final Set<LazyVar> vars = new HashSet<LazyVar>();
		if (!m.find()) {
			m = FUNCTION_PATTERN.matcher(toAnalyze);
			p = FUNCTION_PATTERN;
			if (m.find()) {
				final int start = m.start();
				toAnalyze = toAnalyze.substring(start);
				m = p.matcher(toAnalyze);
				m.find();
				if (m.group(4) != null) {
					final LazyVar v = new LazyVar();
					v.typeName = m.group(4);
					v.name = m.group(6);
					if (v.name == null)
						v.name = "this";
					vars.add(v);
				}
				fillParams(vars, m.group(8));
			}
		} else {
			fillParams(vars, m.group(2));
			final LazyVar v = new LazyVar();
			v.typeName = AdviceContextType.TYPE_NAME;
			v.name = Around.CONTEXT_PARAM_NAME;
			vars.add(v);
		}
		final Stack<Set<LazyVar>> stack = new Stack<Set<LazyVar>>();
		stack.push(vars);

		return stack;
	}

	private static void fillParams(final Set<LazyVar> vars, final String params) {
		Matcher m;
		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);
				if (m.find()) {
					final LazyVar v = new LazyVar();
					v.typeName = m.group(1);
					v.name = m.group(2);
					vars.add(v);
				}
			}
		}
	}

	public final static Partition computePartition(final String str) {
		if (isInsideImport(str))
			return Partition.NAMESPACE_IMPORT;

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

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

		if (isInsideExpression(str))
			return Partition.EXPRESSION;

		return Partition.DEFAULT;
	}

	public final static ExecutionContext computeExecutionContext(
			final String str, ExecutionContext ctx,
			final List<Extension> extensions) {
		final Partition p = computePartition(str);
		if (p == Partition.EXPRESSION || p == Partition.TYPE_DECLARATION) {

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

				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 extensionImports.toArray(new String[extensionImports
							.size()]);
				}

				public List<Extension> getExtensions() {
					return extensions;
				}

				public void analyze(ExecutionContext ctx,
						Set<AnalysationIssue> issues) {
					// TODO Auto-generated method stub

				}

				public List<Extension> getPublicExtensions(ResourceManager rm, ExecutionContext ctx) {
					return extensions;
				}

				public List<Extension> getPublicExtensions(
						ResourceManager resourceManager,ExecutionContext ctx,
						Set<String> flowoverCache) {
					return extensions;
				}

				public List<Around> getArounds() {
					return Collections.emptyList();
				}

			};

			ctx = ctx.cloneWithResource(tpl);
			final Stack<Set<LazyVar>> s = computeStack(str);

			for (final Iterator<Set<LazyVar>> iter = s.iterator(); iter
					.hasNext();) {
				final Set<LazyVar> vars = iter.next();
				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 = ctx.cloneWithVariable(new Variable(v.name, vType));
				}
			}
		}
		return ctx;

	}

}

Back to the top