1

这是一个用于创建领域特定语言 (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')
}
}

- 生成的 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;
  }
}

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

我的问题:: 不断收到一条消息,上面写着'selection does not contain a main type'

我的尝试:在实体(类)'Main.xentities'中,我创建了一个'op main()'函数。在生成的 .java 代码中,这将显示为公共静态 String main() 函数。我以为这会奏效。然而它没有。我不知道缺少什么。欢迎任何帮助。

4

1 回答 1

0

当然,这是行不通的!!main 方法必须返回 void 并接受 String[] args 类型的参数;或者

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

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

于 2013-11-03T22:38:33.290 回答