简体   繁体   中英

How to invoke Xtext parser/JvmModelInferrer from a Java project

I have implemented a simple little DSL that generates classes from input files, by using the JvmModelInferrer approach. I am trying to figure out how I can invoke the parser and the code genarator within the code of a regular java project. So at some point in the main class of my non-Xtext project I can just create a file, pass it to the Xtext parser/codeGenerator for my DSL, and the result will be that class files are generated to a folder of my choice (within my java project). This case is not covered in the documentation, so any help would be much appreciated, Thanks!

UPDATE: My code so far looks like this:

    private  static IGenerator generator;

    /**
     * @param args
     */
    public static void main(String[] args) {

        // this line registers the EMF for our DSL
        Injector injector = new HyRuleStandaloneSetup().createInjectorAndDoEMFRegistration();
        ResourceSet rs = new ResourceSetImpl();
        File file = new File(
                "C:/Documents and Settings/chmamat2/runtime-EclipseXtext/hyrule.project/src/hyrule/project/main.hrule");
        Resource resource = rs.getResource(URI.createURI(file.toURI().toString()), true);

        //setup the generator
        generator = injector.getInstance(IGenerator.class);

        //setup the file system access
        final EclipseResourceFileSystemAccess2 fsa = new EclipseResourceFileSystemAccess2();


        generator.doGenerate(resource, fsa);

Basically, you can open your DSL as an EMF model: you create an EMF ResourceSet, then use the ResourceSet API to open your file (by creating either a file or platform uri representing the file), and open its contents.

However, this only works if you do not use Xbase. If you use Xbase, you have to use the Guice injector the Xtext API provides to instantiate the ResourceSet. If the ResourceSet is initialized, it works the same way as before.

To use the injector, either instantiate it using the StandaloneSetup class - do not do this, if you plan to use the code in the Eclipse IDE together with your own editor, or by registering an eclipse extension with an Xtext-based execution factory. For example on this see the registered editor in my Xtext Reflective Tree editor project .

To also call the code generator, you have several possibilities:

  1. You can use an Eclipse project with the Xtext project nature. If it is set up, you simply create/modify your DSL file, and the builder magically finishes everything I mentioned before - nothing to do anymore.
  2. You can also inject the IGenerator in your code (when having an injected class), so you can programmatically call it after opening the original EMF-based Xtext model.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM