Xtext, multi-file Cross-reference

谁都会走 提交于 2019-12-25 18:39:40

问题


I want to call variables declared in another file. The include of the file is done using cross referencing. and the call of the declared too. this is the grammar:

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

;

the declaration is the Cosem rule.

from the documentation I understand that I must do something in the IResourceDescription but I don't know what exactly

EDIT

public class MyDslQNP extends DefaultDeclarativeQualifiedNameProvider {

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

}

回答1:


what you are looking for is called "scoping" xtext. it is implemented in YourDslScopeProvider

this could look like

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

}


来源:https://stackoverflow.com/questions/41977296/xtext-multi-file-cross-reference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!