使用xtext-xbase-xtend工具链创建一个简单的特定于域的语言

时间:2013-11-03 21:00:12

标签: java xtext eclipse-emf xtend xbase

这是用于创建特定于域的语言(DSL)的类项目。没什么好考虑的。对于我自己,我将这个项目设置为纯粹的练习,以学习所涉及的步骤。

涉及的文件(附带代码文件)::

1)Xtext中的语法(entities.xtext)

grammar org.example.xbase.entities.Entities with org.eclipse.xtext.xbase.Xbase  

generate entities "http://www.example.org/xbase/entities/Entities"


Model:
    entities+=Entity*;


Entity:
    'entity' name=ID ('extends'
        superType=JvmParameterizedTypeReference)?
        '{'
            attributes+=Attribute*
            operations+=Operation*
        '}';

Attribute:
    'attr'(type=JvmTypeReference)? name=ID
    ('=' initexpression=XExpression)? ';';

Operation:
    'op'(type=JvmTypeReference)? name=ID
    '(' (params+=FullJvmFormalParameter (',' 
        params+=FullJvmFormalParameter)*)? ')'
    body=XBlockExpression;

2)xtend中的JvmModelInferrer(entitiesJvmModelInferrer.xtend)

package org.example.xbase.entities.jvmmodel

import com.google.inject.Inject
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.example.xbase.entities.entities.Entity

class EntitiesJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
@Inject extension IQualifiedNameProvider

    def dispatch void infer(Entity entity, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
        acceptor.accept(entity.toClass("Animals."+entity.name)).
            initializeLater[
                documentation= entity.documentation
                if(entity.superType !=null)
                    superTypes += entity.superType.cloneWithProxies

                    //Create fields for each attribute in the entity
                    entity.attributes.forEach[
                        a | val type = a.type ?: a.initexpression?.inferredType
                        members += a.toField(a.name,type) [
                            documentation=a.documentation
                            if(a.initexpression !=null)
                                initializer = a.initexpression
                        ]
                        //create getter method
                        members +=a.toGetter(a.name,type)
                        //create setter method
                        members +=a.toSetter(a.name,type)
                    ]   
                    //Create method for each operation in the entity
                    entity.operations.forEach[
                        op|
                        members+= op.toMethod(op.name,op.type?:inferredType)[
                            documentation=op.documentation
                            for (p:op.params){
                                parameters +=p.toParameter(p.name,p.parameterType)
                            }
                            //create a main method in one of the classes, when called. Make sure it is also static!!!
                            if(op.name.equals('main')){
                                static=true
                            }
                            body=op.body
                        ]
                    ]

            ]
}
}

3)新语言的源文件(Animal.xentities和Main.xentities)。 .xentities扩展特定于此DSL项目的文件。    - Main.xentities

entity Main{
    //define an instance of Animal
    attr me=new Animal;
    op main(){
        println('main-func')
    }
}

- 生成Main.java

package Animals;

import Animals.Animal;
import org.eclipse.xtext.xbase.lib.Functions.Function0;
import org.eclipse.xtext.xbase.lib.InputOutput;

@SuppressWarnings("all")
public class Main {
  private Animal me = new Function0<Animal>() {
    public Animal apply() {
      Animal _animal = new Animal();
      return _animal;
    }
  }.apply();

  public Animal getMe() {
    return this.me;
  }

  public void setMe(final Animal me) {
    this.me = me;
  }

  public static String main() {
    //This is the main function I created to
    //have an entry point for program execution
    String _println = InputOutput.<String>println("main-func");
    return _println;
  }
}

-Animal.xentities

entity Animal{
//define an equivalent class in my domain-specific language
attr int nlegs;
op printAnimalSound(){
    println('I grunt and sniff')
}
}

-Generated Animal.java代码

package Animals;

import org.eclipse.xtext.xbase.lib.InputOutput;

@SuppressWarnings("all")
public class Animal {
  private int nlegs;

  public int getNlegs() {
    return this.nlegs;
  }

  public void setNlegs(final int nlegs) {
    this.nlegs = nlegs;
  }

  public String printAnimalSound() {
    String _println = InputOutput.<String>println("I grunt and sniff");
    return _println;
  }
}

我的目标:: 我想开发一个基本的工作流基础结构,以便我可以定义具有相关功能和属性的类。然后我希望能够执行它们。

我的问题:: 继续收到一条消息,指出 '选择不包含主要类型'

我的尝试:在实体(类)'Main.xentities'中,我创建了一个'op main()'函数。在生成的.java代码中,这将显示为公共静态String main()函数。我认为这会奏效。但事实并非如此。我不知道遗失了什么。欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

当然,这不会起作用!!该 main方法必须返回void并获取String [] args类型的参数;或

public static void main(String[] args){ ...}

我需要修改JvmModelInferrer文件才能实现这一点。