繁体   English   中英

Xtext:在XExpression中使用EClass

[英]Xtext: Use EClass in XExpression

我正在使用XExpressions编写Xtext语法,并且还在Eclasses上进行操作 现在,我想也可以从XExpression访问的eclass,比如我写这样的表达式:

Eclass1.attribute1 = Eclass2.attribute1

我想知道如何在XExpression中使用Eclass

语法

grammar org.xtext.example.mydsl.Mydsl with 
org.eclipse.xtext.xbase.Xbase

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

generate mydsl "http://www.xtext.org/example/mydsl/Mydsl"

Model:
(operations += Operation)*;

terminal ATTR : ID ('.' ID)+;

Operation:
'operation' left=[ecore::EClass|ATTR] 'and' right=
 [ecore::EClass|ATTR] 'defined' 'as' condition=XExpression
;

推断者/推断方法

def dispatch void infer(Model element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
      acceptor.accept(element.toClass("example.mydsl")) [
        for (operation : element.operations) {
            left = operation.left
            right = operation.right
            if (left.eIsProxy()) {
                left = EcoreUtil.resolve(left, operation) as EClass
            }   
            if (right.eIsProxy()) {
                right = EcoreUtil.resolve(right, operation) as EClass
            }
            //field for right class left out, but works the same
            members += left.toField(left.name,typeRef(left.EPackage.name+"."+left.name))    
            members += operation.toMethod("conditionExpr", 
            typeRef(Void.TYPE)) [
                body = operation.condition
            ]       
        }
    ]

}

运行时模块

    class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {

       def Class<? extends ImplicitlyImportedFeatures> bindImplicitlyImportedTypes() {
       return MyImportFeature
}

}

我的导入功能

class MyImportFeature extends ImplicitlyImportedFeatures{
override protected getStaticImportClasses() {
    (super.getStaticImportClasses() + #[PackageFromWorkSpace]).toList
}

}

我不确定是否能收到您的问题。 通常,EMF会为EAttributes生成常数,因此,如果您想自己访问属性,则可以执行MyDslPackage.Literals.GREETING__NAMEMyDslPackage.eINSTANCE.getGreeting_Name()

您能否进一步暗示您实际想要做什么

更新:这是有关如何从对eclass的引用中获取Java类的代码段

Thingy:{
    val EClass eclazz = f.clazz
    val uri = EcorePlugin.getEPackageNsURIToGenModelLocationMap(true).get(eclazz.EPackage.nsURI)
    val rs = new ResourceSetImpl
    val r = rs.getResource(uri, true)
    r.load(null)
    val p = r.contents.head
    if (p instanceof GenModel) {
        val genClass = p.findGenClassifier(eclazz)
        if (genClass instanceof GenClass) {
            println(genClass.qualifiedInterfaceName)
            members+=f.toField(eclazz.name, genClass.qualifiedInterfaceName.typeRef)
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM