Xtext 范围和名称提供程序

Xtext Scope and Name provider

如何为语法规则集成作用域提供程序和自定义名称?

语法

Model:
    g+=Greeting+
    (vis+=Visible)*
    ref=Ref;
Ref:
    'ref_greeting' grt=[Greeting];
Visible:
     'vis' make=[Greeting]; 
Greeting:
    'Hallo' name=ID '!';

规则Visible用于定义对其他人可见的Greeting,对于规则Ref的范围。两个输入文件是:

Hallo hallo!
Hallo hallo_other_vis!
ref_greeting hallo

Hallo h!
vis hallo_other_vis
ref_greeting hallo_other_vis   <--- error here

因为第二个文件的范围 h 是可见的,但 hallo_other_vis 不可见,这必须是可见的才能获得所需的结果。

范围提供者是:

override getScope(EObject context, EReference reference) {
    if(context instanceof Ref && reference == MyDslPackage.Literals.REF__GRT){
        var root =  EcoreUtil2.getContainerOfType(context, Model)
        var scope = Scopes.scopeFor(root.g)
        if(root.vis !== null){
            return Scopes.scopeFor(root.vis, scope) /// XXX is this correct
        }
    }
    return super.getScope(context, reference)
}

当您确定问候范围时,您必须收集问候

return Scopes.scopeFor(root.vis, scope)

收集 Visibles 而不是 Greetings 所以你应该解决这个问题,例如

return Scopes.scopeFor(root.vis.map[make], scope)