如何从 Java 项目调用 Xtext 解析器/JvmModelInferrer [英] How to invoke Xtext parser/JvmModelInferrer from a Java project

查看:29
本文介绍了如何从 Java 项目调用 Xtext 解析器/JvmModelInferrer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个简单的小 DSL,它通过使用 JvmModelInferrer 方法从输入文件生成类.我想弄清楚如何在常规 Java 项目的代码中调用解析器和代码生成器.所以在我的非 Xtext 项目的主类中的某个时候,我可以创建一个文件,将它传递给我的 DSL 的 Xtext 解析器/codeGenerator,结果将是类文件生成到我选择的文件夹中(在我的 Java 项目中).此案例未包含在文档中,因此非常感谢您的帮助,谢谢!

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);

推荐答案

基本上,您可以将 DSL 作为 EMF 模型打开:您创建一个 EMF ResourceSet,然后使用 ResourceSet API 打开您的文件(通过创建一个文件或代表文件的平台 uri),然后打开其内容.

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.

但是,这仅在您不使用 Xbase 时才有效.如果您使用 Xbase,则必须使用 Xtext API 提供的 Guice 注入器来实例化 ResourceSet.如果 ResourceSet 被初始化,它的工作方式与之前相同.

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.

要使用注入器,请使用 StandaloneSetup 类实例化它 - 如果您计划将 Eclipse IDE 中的代码与您自己的编辑器一起使用,或者通过使用基于 Xtext 的执行注册 eclipse 扩展,请不要这样做工厂.例如,请参阅我的 Xtext 反射树中的注册编辑器编辑器项目.

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. 您可以使用具有 Xtext 项目性质的 Eclipse 项目.如果设置好了,您只需创建/修改您的 DSL 文件,构建器就会神奇地完成我之前提到的所有内容 - 无需再做任何事情.
  2. 您还可以在代码中注入 IGenerator(当有注入的类时),这样您就可以在打开原始的基于 EMF 的 Xtext 模型后以编程方式调用它.

这篇关于如何从 Java 项目调用 Xtext 解析器/JvmModelInferrer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆