在确定范围时获取当前多值特征的索引

时间:2016-11-27 22:49:28

标签: xtext scoping

让我们说我希望能够声明一堆节点,可能包含前一个节点的链接。然后在链接之后通过这些节点声明路径。我希望DSL看起来像这样:

node A
node B1 -> A
node B2 -> A
node C1 -> B1
node C2 -> B1
path C2 -> B1 -> A
path B2 -> A

这是我想要使用的XText语法。请注意,我将节点链接放在路径的多值功能nodes中。

Model:(
      nodes += Node
    | paths += Path
    )*
;

Node: 
    'node' name=ID ('->' from=[Node])? ;

Path:
    'path' nodes+=[Node] ('->' nodes+=[Node])* ;

现在,我需要约束路径中的[Node]个链接,以便它们只遵循声明的链接。我想,我必须通过范围界定来实现这个目标:

override getScope(EObject ctx, EReference ref){
    if(ref == MyDslPackage.Literals.PATH__NODES ){
        if(ctx instanceof Path) 
        return Scopes::scopeFor(/* Node link, previous to the current one */);
    }
    return super.getScope(ctx,ref)
}

为了进入上一个链接,我需要一个当前链接的索引,但我找不到获取它的方法。有没有办法获得这样的指数?我应该通过直接访问ctx.nodes以某种方式这样做吗? (这似乎导致链接解决周期错误,但是,也许,我正确地做到了。)

编辑。更改添加{Path.parent=current}的语法并将其添加到作用域中可以解决问题:

 return Scopes::scopeFor(#[ctx.parent.node.from])

1 个答案:

答案 0 :(得分:1)

不应将其解析为引用列表,而应将其解析为具有父pathes的不同Path对象,如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.package1</groupId>
  <artifactId>spring-test-app</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.3.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.3.3.RELEASE</version>
    </dependency>
  </dependencies>
</project>

它可以更轻松地处理各个路径元素。

相关问题