Search code examples
scopextext

Scoping only previous elements with qualified name


I am trying to understand the Xtext scoping using the home automation example. The grammar is the following:

grammar org.xtext.homeautomation.Rules with org.eclipse.xtext.common.Terminals

generate rules "http://www.xtext.org/homeautomation/Rules"

Model:
    declarations+=Declaration*
;

Declaration : 
    Device | Rule
;

Device :
    'Device' name=ID 'can' 'be'
        (states+=State (',' states+=State)*)?
;

State :
    name=ID
;

Rule:
    'Rule' description=STRING
        'when' when=[State|QualifiedName]
        'then' then=[State|QualifiedName]
;

QualifiedName :
    ID ('.' ID)*
;

I am using the following ScopeProvider to prevent forward references, but by this I loose the qualified name.

class RulesScopeProvider extends AbstractRulesScopeProvider {
    override getScope(EObject context, EReference reference) {
        if (context instanceof Rule) {
            scope_Rule(context, reference)
        }
    }

    def scope_Rule(Rule rule, EReference r) {
        var list = (rule.eContainer as Model).declarations as List<Declaration>
        var i = list.subList(0, list.indexOf(rule)).filter(typeof(Device)).map[states].flatten
        Scopes::scopeFor(i)
    }
}

How do I bring back the qualified names for the States?


Solution

  • Have a look at the other methods in the Scopes class.

    @Inject IQualifiedNameProvider qnp 
    ....
    Scopes.scopeFor(list, qnp, IScope.NULLSCOPE)