BR.Hamza
BR.Hamza

Reputation: 199

Xtext, multi-file Cross-reference

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

}

Upvotes: 0

Views: 265

Answers (1)

Christian Dietrich
Christian Dietrich

Reputation: 11868

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

}

Upvotes: 3

Related Questions