码迷,mamicode.com
首页 > 其他好文 > 详细

Xtext Headless模式代码生成

时间:2014-07-21 22:45:07      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:dsl   xtext   headless   eclipse   antlr   

Xtext

Xtext是eclipse下用于构建DSL的工具。其使用非常方便,除了支持开发DSL以外,还能得到eclipse语法高亮、错误提示等功能。http://www.eclipse.org/Xtext/index.html

建议先读读官方文档里面的最开始的几个tutorial,对Xtext的使用有个大概的了解。http://www.eclipse.org/Xtext/documentation.html#FirstFiveMinutes


Headless模式下代码生成

一般情况下,我们编写Xtext生成的DSL项目的时候,更多的时候使用在IDE环境下编写。这样虽然方便了我们的编辑,但是在项目构建等使用场景,IDE就显得有些笨拙,不容易自动化。我假设大家已经读完了Xtext文档中的例子,我们来看看如何通过命令行的方式生成DSL对应的java代码。


1.创建Xtext工程

bubuko.com,布布扣

2.写入工程名称,以及对应的DSL后缀名

bubuko.com,布布扣

3.运行mwe2,生成一堆乱七八糟的东西

bubuko.com,布布扣

4.download antlr,此时在console中会提示你下载download antlr,输入y,回车

bubuko.com,布布扣

5.编写DSL Generator,因为我们这里涂个方便,就直接使用模板中自带的,去掉注释部分就可以用

bubuko.com,布布扣

6.写main函数,用于接收一个DSL文件,生成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;


public class 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 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 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.配置java headless

bubuko.com,布布扣

8.Export runnable jar

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

注意:要选择“Package required libraries into generted JAR”

8.验证

创建文件hello.mydsl,内容如下:

Hello Jack!
Hello Lucy!
Hello Nigel!


执行代码生成:

java -jar hello-dsl.jar hello.mydsl


得到greetings.txt:

People to greet: Jack, Lucy, Nigel


更多

这里只是展示了如何使用headless模式,没有展示如何生成java代码。可以参考Xtext官方文档如何生成JVM语言。


参考资料

http://xtextcasts.org/episodes/12-building-with-ant?view=comments#comment_32

Xtext Headless模式代码生成

标签:dsl   xtext   headless   eclipse   antlr   

原文地址:http://blog.csdn.net/kiwi_coder/article/details/38024349

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!