The question may seem obvious, but I'm just beginning with Xtext
. So after creating the xtext project, and running the mydsl.xtext
file.
it launches a new iteration of eclipse. I create a new .mydsl file in a new project. But I don't know what to do next! How do I run the .mydsl
file?? How do i use my DSL
??
All Xtext tutorial stops after creating the DSL
and don't show how to use it. I was following the 15 minutes Xtext tutorial
My code is the hello word code given by eclipse, nothing really special at this point.
if you want a java main to read the model and execute the generator you may have a look at this snippet
package org.eclipse.xtext.example.domainmodel;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.generator.GeneratorContext;
import org.eclipse.xtext.generator.GeneratorDelegate;
import org.eclipse.xtext.generator.IGeneratorContext;
import org.eclipse.xtext.generator.JavaIoFileSystemAccess;
import org.eclipse.xtext.util.CancelIndicator;
import org.eclipse.xtext.validation.CheckMode;
import org.eclipse.xtext.validation.IResourceValidator;
import org.eclipse.xtext.validation.Issue;
import com.google.common.collect.Lists;
import com.google.inject.Injector;
/**
* @author dietrich - Initial contribution and API
*/
public class Main {
public static void main(String[] args) {
// TODO traverse directory
List<String> files = Lists.newArrayList("model/a.dmodel", "model/b.dmodel");
Injector injector = new DomainmodelStandaloneSetup().createInjectorAndDoEMFRegistration();
ResourceSet rs = injector.getInstance(ResourceSet.class);
ArrayList<Resource> resources = Lists.newArrayList();
for (String file : files) {
Resource r = rs.getResource(URI.createFileURI(file), true);
resources.add(r);
}
IResourceValidator validator = injector.getInstance(IResourceValidator.class);
for (Resource r : resources) {
List<Issue> issues = validator.validate(r, CheckMode.ALL, CancelIndicator.NullImpl);
for (Issue i : issues) {
System.out.println(i);
}
}
GeneratorDelegate generator = injector.getInstance(GeneratorDelegate.class);
JavaIoFileSystemAccess fsa = injector.getInstance(JavaIoFileSystemAccess.class);
fsa.setOutputPath("src-gen-code/");
GeneratorContext context = new GeneratorContext();
context.setCancelIndicator(CancelIndicator.NullImpl);
for (Resource r : resources) {
generator.generate(r, fsa, context);
}
}
}
When you save your . yourdsl file on the editor you temporarily opened, it will automatically build it. Once it builds successfully without errors, you will have a directory called src-gen inside your project, on your workspace. There you'll have the files your dsl just generated based on your yourdsl Generator.xtend
The result of an Xtext project is a Domain Specific Language designed by your own, that can be parsed, validated, linked and used for code generation inside an IDE (Eclipse or Intellij as far as I know) and headless.
Depending on the features you wish to have in your DSL, you have to alter and create different classes inside your XText project.
Usually the first steps include:
.xtext
file), to specify how your DSL should be parsed and look syntactically. .xtext
file -> run as --> generate Xtext artifacts)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.