gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:12:45 24 4
gpt4 key购买 nike

这是一个用于创建特定领域语言 (DSL) 的类(class)项目。没什么值得考虑的。对于我自己来说,我将这个项目纯粹作为学习所涉及步骤的练习。

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

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

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

我的问题::不断收到一条消息:“选择不包含主类型”

我的尝试:在实体(类)“Main.xentities”中,我创建了一个“op main()”函数。在生成的 .java 代码中,这将显示为公共(public)静态 String main() 函数。我认为这会起作用。然而事实并非如此。我不知道缺少什么。欢迎任何帮助。

最佳答案

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

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

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

关于java - 使用 xtext-xbase-xtend 工具链创建简单的特定于领域的语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19758058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com