Xtext - 如何在自己的语法中使用“import”

时间:2014-11-17 13:48:16

标签: dsl xtext

我有自己的Xtext语法定义:

grammar org.xtext.example.mydsl.MyOtherDsl with org.eclipse.xtext.common.Terminals

generate myOtherDsl "http://www.xtext.org/example/mydsl/MyOtherDsl"

OtherModel:
    Foo | Bar;

Foo: 'foo' name=ID;

Bar: 'bar' name=ID;

现在,我想通过“导入”在(标准示例)Xtext语法中使用此语法:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

import "http://www.xtext.org/example/mydsl/MyOtherDsl" as other

Model:
    greetings+=Greeting*
    foobars+=FooBar*;

Greeting: 'Hello' name=ID '!' foo=[other::Foo];

FooBar:  other::Foo | other::Bar;

Greeting: 'Hello' name=ID '!' foo=[other::Foo];正在运行时,FooBar: other::Foo | other::Bar;会抛出错误:在输入'other'时没有可行的选择。在Greeting规则中,我使用属性引用(foo)。在FooBar规则中,我只想将其用作类型。我该怎么办?

任何帮助表示感谢。

谢谢, 安德烈亚斯

1 个答案:

答案 0 :(得分:2)

Xtext仅支持单个Inhertiance层次结构

grammar org.xtext.example.mydsl.MyDslBase with org.eclipse.xtext.common.Terminals
grammar org.xtext.example.mydsl.MyDslGrandParent with org.xtext.example.mydsl.MyDslBase
grammar org.xtext.example.mydsl.MyDslParent with org.xtext.example.mydsl.MyDslGrandParent
grammar org.xtext.example.mydsl.MyDslChild with org.xtext.example.mydsl.MyDslParent

因此不可能将它们分成几个并将它们分组到一个dsl