【问题标题】:Xtext: DSL reference to attributeXtext:对属性的 DSL 引用
【发布时间】:2014-01-24 16:53:48
【问题描述】:

例如

我有这样的语法:

Bundle:
    'Bundle'
    name= ID '{'
    car+=Car+
    service +=Service*
    '}'
    ;

Car:
    'Car'
    name=ID
    extra+=Extra*
    '}'
;   

Extra:
    name= ID '=' type=STRING
;

Service:
    'Service' att=STRING 'for' ref+=Reference*
;

Reference:
    //Ref to car oder Ref to Car.Extra 
;

在我的模型中,我想创建一个Service,例如:

Service "ServiceName" for car1
Service "ServiceName" for car2 (extra1 extra2)

如何解决对CarExtras 的引用?

【问题讨论】:

    标签: reference grammar dsl xtext


    【解决方案1】:

    这可以通过简单的交叉引用来完成

    Service:
        'Service' att=STRING 'for' car=[Car] ('(' extras+=[Extra]+ ')')?
    ;
    

    以及相应的范围提供者

    package org.xtext.example.mydsl.scoping
    
    import org.eclipse.emf.ecore.EReference
    import org.eclipse.xtext.scoping.IScope
    import org.eclipse.xtext.scoping.Scopes
    import org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider
    import org.xtext.example.mydsl.myDsl.Service
    
    class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {
    
        def IScope scope_Service_extras(Service ctx, EReference ref) {
            return Scopes.scopeFor(ctx.car.extra)
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多