DSL为什么显示“无法解析对“的引用??

时间:2019-03-29 05:15:54

标签: grammar xtext

我正在实施一个包含三个部分的语法。在第一部分中,我声明了带有接口的组件,例如带有接口interface_1,interface_2的组件A。在第三部分中,我声明了一些限制,例如,组件A可以通过接口XXXX访问组件B。当我尝试交叉引用组件的接口时,出现错误“无法解析对ProbeInterface'interface_1'的引用”吗?。

我从互联网上尝试了几个示例,但没有一个适合我的情况。

这是我语法的一部分:


ArchitectureDefinition:
    'Abstractions' '{' abstractions += DSLAbstraction+ '}'  
    'Compositions' '{' compositions += DSLComposition* '}'  
    'Restrictions' '{' restrictions += DSLRestriction* '}'  
;


DSLComposition:

   DSLProbe|DSLSensor

;


DSLRestriction:

'sensor' t=[DSLSensor] 'must-access-probe' type = [DSLProbe] 'through-interface' probeinterface=[ProbeInterface] ';'

;

DSLSensor: 

  'Sensor' name=ID ';'
;



DSLProbe:

'Probe' name=ID  ('with-interface' probeinterface=ProbeInterface)? ';'

;  


ProbeInterface :

    name+=ID (',' name+=ID)* 
;


执行:

Abstractions
{

   Sensor sensor_1 ;
   Probe probe_1 with-interface interface_1, interface_2;
}

Compositions{}

Restrictions
{
   sensor sensor_1 must-access-probe probe_1 through-interface 
   interface_1;
}


我希望文法可以引用interface_1或interface_2。

谢谢。

1 个答案:

答案 0 :(得分:1)

您发布的语法不完整

定义接口的方式确实很糟糕。 默认命名仅适用于单值名称属性

ProbeInterface :

    name+=ID (',' name+=ID)* 
;

更好

DSLProbe:

'Probe' name=ID  ('with-interface' probeinterfaces+=ProbeInterface ("," probeinterfaces+=ProbeInterface)*)? ';'

;  


ProbeInterface :

    name=ID
;

它看起来像一个接口的合格名称是

<probename>.<interfacename>
  • 您要么必须改编名称提供程序
  • 或语法和模型以将质量名ref=[Thing|FQN]FQN: ID ("." ID)*;一起使用
  • 或者您适当地实现了作用域设置,这是您想针对您的情况进行的操作,因为您想限制特定探针的接口

这是一个样本

    override getScope(EObject context, EReference reference) {
        if (reference === MyDslPackage.Literals.DSL_RESTRICTION__PROBEINTERFACE) {
            if (context instanceof DSLRestriction) {
                val probe = context.type
                return Scopes.scopeFor(probe.probeinterfaces)
            }
        }
        super.getScope(context, reference)
    }
相关问题