简体   繁体   中英

Ecore EClass inheritance in Xtext

Consider the following Ecore model (in Xcore notation):

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

and the following Xtext rules:

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

such that textual models are like this:

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

Is there a way to refactor the repetition away from the rules? I know I could introduce a new EClass ElementContain and make rules for Foo and Bar refer to its rule, however, that would clutter the metamodel.

generally: no. but if the only difference is the first keyword:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM