-
Notifications
You must be signed in to change notification settings - Fork 3
/
AcceleoDebugger.java
547 lines (483 loc) · 17.9 KB
/
AcceleoDebugger.java
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
/*******************************************************************************
* Copyright (c) 2020, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.aql.ls.debug;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import org.eclipse.acceleo.AcceleoASTNode;
import org.eclipse.acceleo.Expression;
import org.eclipse.acceleo.Module;
import org.eclipse.acceleo.Query;
import org.eclipse.acceleo.Statement;
import org.eclipse.acceleo.Template;
import org.eclipse.acceleo.aql.AcceleoUtil;
import org.eclipse.acceleo.aql.evaluation.AcceleoEvaluator;
import org.eclipse.acceleo.aql.evaluation.writer.DefaultGenerationStrategy;
import org.eclipse.acceleo.aql.evaluation.writer.IAcceleoGenerationStrategy;
import org.eclipse.acceleo.aql.ide.AcceleoPlugin;
import org.eclipse.acceleo.aql.parser.AcceleoAstResult;
import org.eclipse.acceleo.aql.parser.AcceleoParser;
import org.eclipse.acceleo.aql.parser.ModuleLoader;
import org.eclipse.acceleo.aql.profiler.IProfiler;
import org.eclipse.acceleo.aql.profiler.ProfilerPackage;
import org.eclipse.acceleo.aql.profiler.ProfilerUtils;
import org.eclipse.acceleo.aql.profiler.ProfilerUtils.Representation;
import org.eclipse.acceleo.debug.AbstractDSLDebugger;
import org.eclipse.acceleo.debug.DSLSource;
import org.eclipse.acceleo.debug.event.IDSLDebugEventProcessor;
import org.eclipse.acceleo.debug.util.StackFrame;
import org.eclipse.acceleo.query.AQLUtils;
import org.eclipse.acceleo.query.ast.VariableDeclaration;
import org.eclipse.acceleo.query.ide.QueryPlugin;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameQueryEnvironment;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameResolver;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.URIUtil;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.swt.widgets.Display;
public class AcceleoDebugger extends AbstractDSLDebugger {
/**
* The generate {@link Runnable}.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
private final class GenerateRunnable implements Runnable {
@Override
public void run() {
final Module module = astResult.getModule();
final long threadID = Thread.currentThread().getId();
spawnRunningThread(threadID, Thread.currentThread().getName(), module);
try {
if (isNoDebug()) {
generateNoDebug(queryEnvironment, module, model);
} else {
evaluator = new AcceleoDebugEvaluator(queryEnvironment, newLine);
final IQualifiedNameResolver resolver = queryEnvironment.getLookupEngine().getResolver();
resolver.clearLoaders();
resolver.addLoader(new ModuleLoader(new AcceleoParser(), evaluator));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(
AcceleoParser.QUALIFIER_SEPARATOR, false));
final IAcceleoGenerationStrategy strategy = new DefaultGenerationStrategy(model
.getResourceSet().getURIConverter());
AcceleoUtil.generate(evaluator, queryEnvironment, module, model, strategy,
getDestination(), logURI);
}
} finally {
// FIXME workaround: UI jobs are coming from core.debug even if the gen has finished,
// which cause makes the JobManager to stall and prevents the termination of the LSP
// process. By launching the termination in the UI thread in sync we allow the jobs to
// finish first.
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
terminate(threadID);
terminated(threadID);
terminated();
}
});
}
IWorkspace workspace = ResourcesPlugin.getWorkspace();
if (workspace != null) {
IContainer container = workspace.getRoot().getContainerForLocation(new Path(destination
.toFileString()));
if (container != null) {
try {
container.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
/**
* Acceleo Debugger.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
private final class AcceleoDebugEvaluator extends AcceleoEvaluator {
/**
* Constructor.
*
* @param queryEnvironment
* the {@link IQualifiedNameQueryEnvironment}
* @param newLine
* the new line {@link String}
*/
AcceleoDebugEvaluator(IQualifiedNameQueryEnvironment queryEnvironment, String newLine) {
super(queryEnvironment.getLookupEngine(), newLine);
}
@Override
public Object doSwitch(EObject eObject) {
if (isTerminated()) {
return null;
}
if (eObject instanceof Template || eObject instanceof Query) {
pushStackFrame(Thread.currentThread().getId(), eObject);
}
try {
if (isAcceleoInstruction(eObject)) {
final StackFrame currentFrame = peekStackFrame(Thread.currentThread().getId());
currentFrame.setInstruction(eObject);
currentFrame.setVariables(peekVariables());
if (!AcceleoDebugger.this.control(Thread.currentThread().getId(), eObject)) {
Thread.currentThread().interrupt();
}
}
return super.doSwitch(eObject);
} finally {
if (eObject instanceof Template || eObject instanceof Query) {
popStackFrame(Thread.currentThread().getId());
}
}
}
}
/**
* The module.
*/
public static final String MODULE = "module";
/**
* The model.
*/
public static final String MODEL = "model";
/**
* The destination.
*/
public static final String DESTINATION = "destination";
/**
* The JSON map of options.
*/
public static final String OPTIONS = "options";
/**
* The option map type.
*/
public static final Type OPTION_MAP_TYPE = new TypeToken<LinkedHashMap<String, String>>() {
}.getType();
/**
* The profile model
*/
public static final String PROFILE_MODEL = "profileModel";
/**
* The profile model {@link Representation}.
*/
public static final String PROFILE_MODEL_REPRESENTATION = "modelRepresentation";
/**
* The {@link IQualifiedNameQueryEnvironment}.
*/
private IQualifiedNameQueryEnvironment queryEnvironment;
/**
* The {@link ResourceSet} for models.
*/
private ResourceSet resourceSetForModels;
/**
* The destination {@link URI}.
*/
private URI destination;
private Map<String, String> options;
/**
* The profile model {@link URI}.
*/
private URI profileModelURI;
/**
* The profiler model {@link Representation}.
*/
private Representation profilerModelRepresentation;
/**
* The {@link AcceleoAstResult}.
*/
private AcceleoAstResult astResult;
/**
* The {@link Resource} containing the model.
*/
private Resource model;
/**
* The new line {@link String}.
*/
private String newLine;
/**
* The log {@link URI} if any, <code>null</code> otherwise.
*/
private URI logURI;
/**
* The {@link AcceleoDebugEvaluator}.
*/
private AcceleoDebugEvaluator evaluator;
/**
* Constructor.
*
* @param target
* the {@link org.eclipse.acceleo.debug.event.DSLDebugEventDispatcher dispatcher} for
* asynchronous communication or the {@link org.eclipse.acceleo.debug.ide.DSLDebugTargetAdapter
* target} for synchronous communication
*/
public AcceleoDebugger(IDSLDebugEventProcessor target) {
super(target);
}
@Override
public void initialize(boolean noDebug, Map<String, Object> arguments) {
setNoDebug(noDebug);
final URI moduleURI = URI.createURI((String)arguments.get(MODULE));
final URI modelURI = URI.createURI((String)arguments.get(MODEL));
destination = URI.createURI((String)arguments.get(DESTINATION));
final String optionString = (String)arguments.get(OPTIONS);
if (optionString != null) {
options = new Gson().fromJson(optionString, OPTION_MAP_TYPE);
} else {
options = new LinkedHashMap<>();
}
final String profileModel = (String)arguments.get(PROFILE_MODEL);
if (profileModel != null) {
profileModelURI = URI.createFileURI(URI.decode(profileModel));
}
final String profilerModelRepresentationString = (String)arguments.get(PROFILE_MODEL_REPRESENTATION);
if (profilerModelRepresentationString != null) {
profilerModelRepresentation = Representation.valueOf(profilerModelRepresentationString);
}
final IProject project = LSPEclipseUtils.findResourceFor((String)arguments.get(MODULE)).getProject();
final IQualifiedNameResolver resolver = QueryPlugin.getPlugin().createQualifiedNameResolver(
AcceleoPlugin.getPlugin().getClass().getClassLoader(), project,
AcceleoParser.QUALIFIER_SEPARATOR, false);
newLine = options.getOrDefault(AcceleoUtil.NEW_LINE_OPTION, System.lineSeparator());
logURI = AcceleoUtil.getlogURI(destination, options.get(AcceleoUtil.LOG_URI_OPTION));
final ArrayList<Exception> exceptions = new ArrayList<>();
resourceSetForModels = AQLUtils.createResourceSetForModels(exceptions, this, new ResourceSetImpl(),
options);
// TODO report exceptions
model = resourceSetForModels.getResource(modelURI, true);
queryEnvironment = AcceleoUtil.newAcceleoQueryEnvironment(options, resolver, resourceSetForModels,
false);
for (String nsURI : new ArrayList<String>(EPackage.Registry.INSTANCE.keySet())) {
registerEPackage(queryEnvironment, EPackage.Registry.INSTANCE.getEPackage(nsURI));
}
resolver.addLoader(new ModuleLoader(new AcceleoParser(), evaluator));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR,
false));
final java.net.URI moduleBinaryURI = resolver.getBinaryURI(java.net.URI.create(moduleURI.toString()));
final String moduleQualifiedName = resolver.getQualifiedName(moduleBinaryURI);
final Object resolved = resolver.resolve(moduleQualifiedName);
if (resolved instanceof Module) {
astResult = ((Module)resolved).getAst();
}
}
/**
* Registers the given {@link EPackage} in the given {@link IQualifiedNameQueryEnvironment} recursively.
*
* @param environment
* the {@link IQualifiedNameQueryEnvironment}
* @param ePackage
* the {@link EPackage}
*/
private void registerEPackage(IQualifiedNameQueryEnvironment environment, EPackage ePackage) {
environment.registerEPackage(ePackage);
for (EPackage child : ePackage.getESubpackages()) {
registerEPackage(environment, child);
}
}
@Override
public void start() {
new Thread(new GenerateRunnable(), "Acceleo Debug Thread").start();
}
/**
* Generates without debug.
*
* @param environment
* the {@link IQualifiedNameQueryEnvironment}
* @param module
* the {@link Module} to generate
* @param modelResource
* the model {@link Resource}
*/
protected void generateNoDebug(IQualifiedNameQueryEnvironment environment, Module module,
Resource modelResource) {
final AcceleoEvaluator noDebugEvaluator;
final IProfiler profiler;
if (profileModelURI != null && profilerModelRepresentation != null) {
profiler = ProfilerUtils.getProfiler(profilerModelRepresentation, ProfilerPackage.eINSTANCE
.getProfilerFactory());
noDebugEvaluator = new AcceleoProfilerEvaluator(queryEnvironment, newLine, profiler);
} else {
noDebugEvaluator = new AcceleoEvaluator(environment.getLookupEngine(), newLine);
profiler = null;
}
final IQualifiedNameResolver resolver = environment.getLookupEngine().getResolver();
resolver.clearLoaders();
resolver.addLoader(new ModuleLoader(new AcceleoParser(), noDebugEvaluator));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR,
false));
final IAcceleoGenerationStrategy strategy = new DefaultGenerationStrategy(modelResource
.getResourceSet().getURIConverter());
AcceleoUtil.generate(noDebugEvaluator, environment, module, modelResource, strategy, getDestination(),
logURI);
if (profiler != null) {
try {
profiler.save(profileModelURI);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void disconnect() {
if (queryEnvironment != null && resourceSetForModels != null) {
AQLUtils.cleanResourceSetForModels(this, resourceSetForModels);
AcceleoUtil.cleanServices(queryEnvironment, resourceSetForModels);
}
}
@Override
public boolean canStepInto(Long threadID, EObject instruction) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean validateVariableValue(Long threadID, String variableName, String value) {
// TODO Auto-generated method stub
return false;
}
@Override
public Object getVariableValue(Long threadID, String stackName, String variableName, String value) {
// TODO Auto-generated method stub
return null;
}
@Override
public void setVariableValue(Long threadID, String stackName, String variableName, Object value) {
// TODO Auto-generated method stub
}
@Override
public EObject getInstruction(String path, long line, long column) {
AcceleoAstResult moduleAstResult = null;
try {
final IQualifiedNameResolver resolver = queryEnvironment.getLookupEngine().getResolver();
final String moduleQualifiedName = resolver.getQualifiedName(new java.net.URI("file://" + path));
if (moduleQualifiedName != null) {
final Object resolved = resolver.resolve(moduleQualifiedName);
if (resolved instanceof Module) {
moduleAstResult = ((Module)resolved).getAst();
}
}
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final EObject res;
if (moduleAstResult != null) {
EObject instruction = moduleAstResult.getAstNode((int)line, (int)column);
if (column == 0) {
// The breakpoint has been set on the line so we select the containing Expression statement if
// needed
while (instruction != null) {
if (isAcceleoInstruction(instruction)) {
break;
}
instruction = instruction.eContainer();
}
res = instruction;
} else {
res = instruction;
}
} else {
res = null;
}
return res;
}
/**
* Tells if the given {@link EObject} is an Acceleo instruction.
*
* @param eObject
* the {@link EObject}
* @return <code>true</code> if the given {@link EObject} is an Acceleo instruction, <code>false</code>
* otherwise
*/
private boolean isAcceleoInstruction(EObject eObject) {
return eObject instanceof Statement;
}
@Override
public DSLSource getSource(EObject instruction) {
DSLSource res = null;
String path = null;
Module module = getModule(instruction);
if (module != null) {
final AcceleoAstResult moduleAstResult = module.getAst();
final IQualifiedNameResolver resolver = queryEnvironment.getLookupEngine().getResolver();
final String moduleQualifiedName = resolver.getQualifiedName(moduleAstResult.getModule());
java.net.URI moduleSourceURI = resolver.getSourceURI(moduleQualifiedName);
path = URIUtil.toFile(moduleSourceURI).toString();
if (instruction instanceof AcceleoASTNode) {
final int startLine = moduleAstResult.getStartLine((AcceleoASTNode)instruction);
final int startColumn = moduleAstResult.getStartColumn((AcceleoASTNode)instruction);
final int endLine = moduleAstResult.getEndLine((AcceleoASTNode)instruction);
final int endColumn = moduleAstResult.getEndColumn((AcceleoASTNode)instruction);
res = new DSLSource(path, startLine + 1, startColumn + 1, endLine + 1, endColumn + 1);
} else if (instruction instanceof Expression) {
final int startLine = moduleAstResult.getStartLine((Expression)instruction);
final int startColumn = moduleAstResult.getStartColumn((Expression)instruction);
final int endLine = moduleAstResult.getEndLine((Expression)instruction);
final int endColumn = moduleAstResult.getEndColumn((Expression)instruction);
res = new DSLSource(path, startLine + 1, startColumn + 1, endLine + 1, endColumn + 1);
} else if (instruction instanceof VariableDeclaration) {
final int startLine = moduleAstResult.getStartLine((VariableDeclaration)instruction);
final int startColumn = moduleAstResult.getStartColumn((VariableDeclaration)instruction);
final int endLine = moduleAstResult.getEndLine((VariableDeclaration)instruction);
final int endColumn = moduleAstResult.getEndColumn((VariableDeclaration)instruction);
res = new DSLSource(path, startLine + 1, startColumn + 1, endLine + 1, endColumn + 1);
} else {
res = null;
}
}
return res;
}
/**
* Gets the {@link Module} of the given {@link EObject instruction}.
*
* @param instruction
* the {@link EObject instruction}
* @return the {@link Module} of the given {@link EObject instruction} if nay, <code>null</code> otherwise
*/
private Module getModule(EObject instruction) {
Module res = null;
EObject current = instruction;
while (current != null) {
if (current instanceof Module) {
res = (Module)current;
break;
}
current = current.eContainer();
}
return res;
}
/**
* Gets the destination {@link URI}.
*
* @return the destination {@link URI}
*/
protected URI getDestination() {
return destination;
}
}