reference - Xtext:对属性的 DSL 引用

标签 reference grammar dsl xtext

例如

我有一个这样的语法:

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)

我如何解决对 Extras 的引用的 Car ?

最佳答案

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

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

}

关于reference - Xtext:对属性的 DSL 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21306822/

相关文章:

regex - 什么是上下文无关语法?

python - 对 int 文字的属性访问

ocaml - 从 OCaml 语法中删除无法访问的规则

java - Apache Camel - GSON JsonSerializer 在路线上使用

haskell - 如何将自由的一元 DSL 与状态交错,但在程序中解释状态?

Pearl:嵌套数据结构中的交叉引用?

c++ - 带/不带指针的函数原型(prototype)使用区别

elasticsearch - 在 bool 值上具有一个值和一个查询字符串的IS DSL查询在语义上是相同的

c++ - 对指向 const 对象的非常量指针的非常量引用

c++ - 按值返回与右值引用