Xtext,多文件Cros-refference

时间:2017-02-01 10:25:56

标签: java xtext xtend

我想调用另一个文件中声明的变量。 文件的包含使用交叉引用完成。以及宣布的召唤。这是语法:

Script:
includes+=(Include)* assignments+=(Assignment)* g=GetLog?  clock=Clock? tests+=Test*
;

Assignment:
    Config |Cosem ;

Include:
    'INCLUDE' includedScript=[Script|STRING];

Cosem:
name=ID '=' 'COSEM' '(' classid=INT ',' version=INT ','  obis=STRING ')' ;

AttributeRef:
     name=[Cosem] "." attributeRef =IDValue

;

声明是Cosem规则。

从文档我明白我必须在IResourceDescription做一些事情,但我不知道到底是什么

修改

public class MyDslQNP extends DefaultDeclarativeQualifiedNameProvider {

    QualifiedName qualifiedName(Script script) {
        return QualifiedName.create(script.eResource().getURI().trimFileExtension().lastSegment(), script.eResource().getURI().fileExtension());
    }

}

1 个答案:

答案 0 :(得分:3)

您正在寻找的是"范围" XTEXT。它在YourDslScopeProvider

中实施

这可能看起来像

class MyDslScopeProvider extends AbstractMyDslScopeProvider {

    override getScope(EObject context, EReference reference) {
        if (reference === MyDslPackage.Literals.ATTRIBUTE_REF__NAME) {
            // we are scoping the AttributeRef.name cross reference
            val script = EcoreUtil2.getContainerOfType(context, Script)
            if (script !== null) {
                val allImportedCosems = script.includes.map[includedScript.assignments.filter(Cosem)].flatten
                // put the imported cosems into scope
                return Scopes.scopeFor(allImportedCosems)
            }
        }
        super.getScope(context, reference)
    }

}