我最近开始使用XText。 到目前为止,我已经能够定义一个简单的语法,完成JvmModelInferrer并生成相应的java类和.java文件。
是否可以从一组自定义Java类自动生成DSL文件(考虑其语法)?
让我举一个简单的例子。
我有以下语法:
MODEL:
entities+=ENTITY*
;
ENTITY:
'entity' name=ValidID 'as'
(elements+=PROPERTY)*
'end'
;
PROPERTY:
(many?='many')? 'property' name=ID 'of' type=JvmTypeReference
;
如果我有以下 sample.myDsl
entity Book as
property title of String
property numPages of Integer
end
entity Author as
property name of String
property surname of String
end
我得到了Book.java和Author.java文件。在我的项目中,我有一个分析java文件并从中创建对象的处理器,所以如果我在之前的Book.java和Author.java上运行处理器,我会得到两个自定义实体java类型的实例。每个Entity实例都有一组Property实例。因此,Java模型与xtext语法非常相似。
是否有可能"饲料"这两个对象到XText,可能会定义一个Inferrer来指定翻译,并考虑到相同的.xtext语法文件,自动生成一个.myDsl文件?
答案 0 :(得分:0)
使用xtext通常没问题
如果使用xbase和jvmmodelinferrrer构建ast,如果从模型引用推断的jvm元素,则可能会有一种痛苦 或者尝试将xbase表达式构建为ast 这是一个使用domainmodel示例的简单复杂示例
package org.eclipse.xtext.example.domainmodel.tests
import com.google.inject.Injector
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.common.types.JvmParameterizedTypeReference
import org.eclipse.xtext.common.types.util.TypeReferences
import org.eclipse.xtext.example.domainmodel.DomainmodelStandaloneSetup
import org.eclipse.xtext.example.domainmodel.domainmodel.DomainmodelFactory
import org.eclipse.xtext.resource.DerivedStateAwareResource
import org.eclipse.xtext.resource.SaveOptions
import org.eclipse.xtext.xbase.jvmmodel.JvmTypeReferenceBuilder
class Main {
var static extension DomainmodelFactory factory = DomainmodelFactory.eINSTANCE
def static void main(String[] args) {
var Injector injector = new DomainmodelStandaloneSetup().createInjectorAndDoEMFRegistration()
val ResourceSet resourceSet = injector.getInstance(ResourceSet)
val Resource r0 = resourceSet.createResource(URI.createURI("base/Base.dmodel"))
val Resource r1 = resourceSet.createResource(URI.createURI("model/Person.dmodel"))
val typeReferenceBuilder = injector.getInstance(JvmTypeReferenceBuilder.Factory).create(resourceSet)
val typeReferences = injector.getInstance(TypeReferences)
val model = createDomainModel
r1.contents += model
val model0 = createDomainModel
r0.contents += model0
// build the ast using xtends with clause
model0 => [
elements += createPackageDeclaration => [
name = "base"
elements += createEntity => [
name = "Base"
features+= createProperty => [
name = "id"
type = typeReferenceBuilder.typeRef("java.lang.String")
println(type)
]
]
]
]
//trigger the inferrer on resource 0
(r0 as DerivedStateAwareResource) => [
fullyInitialized = false
installDerivedState(false)
]
// build the ast of the second resource
model => [
elements += createPackageDeclaration => [
name = "model"
elements += createEntity => [
val base = typeReferences.findDeclaredType("base.Base", resourceSet)
println(base)
superType = typeReferenceBuilder.typeRef(base) as JvmParameterizedTypeReference
println(superType)
name = "Person"
features+= createProperty => [
name = "name"
type = typeReferenceBuilder.typeRef("java.lang.String")
println(type)
]
]
]
]
//save the resources
r0.save(SaveOptions.defaultOptions.toOptionsMap)
r1.save(SaveOptions.defaultOptions.toOptionsMap)
}
}