【问题标题】:Xtext: Use EClass in XExpressionXtext:在 XExpression 中使用 EClass
【发布时间】:2017-08-20 17:30:42
【问题描述】:

我正在编写一个 Xtext 语法,该语法使用 XExpressions 并在 Eclasses 上运行。现在我也希望能够从 XExpression 访问 Eclasses,例如我写一个这样的表达式:

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
}

}

MyImportFeature

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

}

【问题讨论】:

    标签: xtext ecore xbase


    【解决方案1】:

    我不确定我是否得到你的问题。 通常 EMF 会为 EAttributes 生成常量,所以如果你想自己访问属性 所以你可以做 MyDslPackage.Literals.GREETING__NAME 或者 MyDslPackage.eINSTANCE.getGreeting_Name()

    你能提供更多关于你真正想要做什么的提示

    更新:这里是关于如何从对 eclass 的引用中获取 java 类的 sn-p

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

    【讨论】:

    • 这是对 eclass 的引用
    • 我只有operation.leftoperation.right,但他们没有clazz,虽然它们是EClass类型
    • Arrrg 当然你有左右我没有,因为我把它命名为 clazz 我认为这很明显
    • 在知道你的语法之前我做了例子
    • uri 为空,好像在地图上找不到eclazz.EPackage.nsURI
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    相关资源
    最近更新 更多