xtext:自动导入和验证当前​​包

xtext: autoimport and -validate current package

我有两个相关的问题,我知道如何解决 "by foot"(使用自定义验证器和范围提供程序)。但令我惊讶的是它们在 xtext 中并没有开箱即用,所以无论如何我都会问(以防我遗漏了什么)。

我有这个 DSL:

Model:
    'package' name=QualifiedName
    imports+=Import*
    entities+=Entity*;

Import:
    'import' importedNamespace=QualifiedName;

Entity:
    name=ID '{'
    references += Reference*
    '}';

Reference:
    name=ID':'entitiy=[Entity]
;

QualifiedName:
    ID('.'ID)*
;

除了在同一文件中定义多个实体之外,我希望能够在同一包中定义 "live" 的多个文件。我这里有 2 个问题:

1) 我发现没有明显的方法可以轻松地自动导入当前包中的所有元素。所以如果我有一个文件

package com.test

EntityA {}

还有第二个

package com.test

import com.test.EntityA // fails if I remove this line

EntityB{
  refToA:EntityA        // I could make qualified names work here.
                        // But that is not the goal.
}

无法解析对 EntityA 的引用,除非我明确导入它(这是我想避免的事情),尽管它们在同一个命名空间中。

那么:有没有办法轻松地为 "current" 包启用 outimports?

2) 我启用了 org.eclipse.xtext.validation.NamesAreUniqueValidator,它对同一文件中定义的实体工作正常。但是如果我像这样重新定义一个导入的实体

package com.test

import com.test.EntityA

EntityB{
  refToA:EntityA
}

EntityA {}

我没有收到预期的验证错误。有什么我可以做的而不必编写自己的验证器吗?

谢谢。

要使自动导入工作,您需要使用 Xbase 和

Model:
    'package' name=QualifiedName
    importSection=XImportSection?

要使用 Xbase,请将此附加到您的语法中:with org.eclipse.xtext.xbase.Xbase

详情见http://www.lorenzobettini.it/2013/01/the_ximportsection_in_xbase_2_4/

第二个只能用你自己的验证器 AFAIK 来解决。原因是您可以使用允许的语法:

import com.test.EntityA as X

private EntityA extends X {}