【问题标题】:Ecore EClass inheritance in XtextXtext 中的 Ecore EClass 继承
【发布时间】:2015-03-12 23:37:55
【问题描述】:

考虑以下 Ecore 模型(以 Xcore 表示法):

class Foo {
    contains Element[] elements
}
class Bar extends Foo {
    int n
}
class Element {
    String name
}

以及以下 Xtext 规则:

FooBar: Foo | Bar;
Foo: {Foo} 'foo' '{' elements+=Element (',' elements+=Element)* '}';
Foo: {Bar} 'bar' n=INT '{' elements+=Element (',' elements+=Element)* '}';
Element: {Element} name=ID;

使得文本模型是这样的:

foo {one, two}
bar 2 {three, four}

有没有办法从规则中重构重复?我知道我可以引入一个新的 EClass ElementContain 并为 FooBar 制定规则参考它的规则,但是,这会使元模型混乱。

【问题讨论】:

    标签: inheritance xtext ecore


    【解决方案1】:

    一般来说:没有。但如果唯一的区别是第一个关键字:

    FooBar:
     ({Foo} 'foo' | {Bar}'bar') '{' elements+=Element (',' elements+=Element)* '}';
    Element: {Element} name=ID;
    

    【讨论】:

    • 因此,如果在某些情况下 Bar 有意义但 Foo 没有意义,那么我几乎需要使用组合来代替继承或在继承之外使用?
    • 是的。语法规则没有模板/片段。随时向 Eclipse Bugzilla 提交增强请求
    猜你喜欢
    • 1970-01-01
    • 2013-03-05
    • 2018-04-12
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多