Xtext
Xtext is a tool used to build DSL in eclipse. It is very convenient to use. Besides developing DSL, it also supports eclipse syntax highlighting, error prompting, and other functions. Http://www.eclipse.org/Xtext/index.html
It is recommended that you read the first several tutorial in the official document to have a rough understanding of the use of xtext. Http://www.eclipse.org/Xtext/documentation.html#FirstFiveMinutes
Code Generation In headless Mode
Generally, xtext-based DSL projects are written in the IDE environment. In this way, although it is convenient for our editing, IDE is clumsy and not easy to automate in project construction and other application scenarios. I suppose you have read the example in the xtext document. Let's take a look at how to generate the corresponding Java code of DSL through command line.
1. Create an xtext Project
2. Write the project name and the corresponding DSL suffix name
3. Run mwe2 to generate a bunch of messy things
4. Download anlr. At this time, you will be prompted to download anlr on the console, enter y, and press Enter.
5. Compile DSL generator. Because it is convenient to use it here, you can directly use the built-in templates.
6.write mainnames, which are used to receive a DSL file and generate greetings.txt
Package org. Kiwi. Text. Hello. generator;
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. igenerator;
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 org. Kiwi. Text. Hello. mydslstandalonesetupgenerated;
Import com. Google. Inject. Inject;
Import com. Google. Inject. injector;
Import com. Google. Inject. provider;
Publicclass main {
Publicstatic void main (string [] ARGs ){
If (ARGs. Length = 0 ){
System. Err. println ("aborting: no path to EMF resource provided! ");
Return;
}
Injector injector = new mydslstandalonesetupgenerated (). createinjectoranddoemfregistration ();
Main main = injector. getinstance (main. Class );
Main. rungenerator (ARGs [0]);
}
@ Inject
Private provider <resourceset> resourcesetprovider;
@ Inject
Private iresourcevalidatorvalidator;
@ Inject
Private igeneratorgenerator;
@ Inject
Private javaiofilesystemaccessfileaccess;
Protectedvoid rungenerator (string ){
// Load the resource
Resourceset set = resourcesetprovider. Get ();
Resource resource = set. getresource (URI. createuri (string), true );
// Validate the resource
List <issue> List = validator. Validate (resource, checkmode. All, cancelindicator. nullimpl );
If (! List. isempty ()){
For (issue: List ){
System. Err. println (issue );
}
Return;
}
// Configure and start the generator
Fileaccess. setoutputpath (".");
Generator. dogenerate (resource, fileaccess );
System. Out. println ("code generation finished .");
}
}
7. Configure Java headless
8. Export runnable jar
Note: select "package required libraries into generted jar"
8. Verify
Create the file hello. mydsl with the following content:
Hello Jack!
Hello Lucy!
Hello Nigel!
Execute Code Generation:
The Java-jar hello-dsl.jar hello. mydsl
To greetings.txt:
People to greet: Jack, Lucy, Nigel
More
Here we only show how to use the headless mode, but not how to generate Java code. Refer to the xtext official document on how to generate the JVM language.
References
Http://xtextcasts.org/episodes/12-building-with-ant? View = comments # comment_32
Xtext headless mode code generation