Xtext交叉引用:遵循功能参数名称

问题描述 投票:0回答:1

我正在努力通过函数定义中函数参数的名称交叉引用,并与Google争夺解决方案。考虑以下示例。

def helloWorld() {
    return "Hello World!"
} 

def combine(Person person, Place place) {
    return person.name + place.code  // ❎ Couldn't resolve reference to Feature 'name'. 
}          


entity Person {
    name: String
    title : String
    occupation : String
}

entity Place  {
    name: String
    code:String
}

datatype String

语法如下,它通过简单的表达式语言扩展了标准示例,其中包含一个非常简单的函数定义概念。

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

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

Domainmodel :
    (
        elements+=Type |
        functions+=Function // Note Functions
    )*;

/********************  Functions ********************/

Function : 'def' name=ID '('
    (parameters+=Parameter (',' parameters+=Parameter)*)? 
    ')' '{' 'return' exp=Exp    '}'
;

Parameter: type=[Entity] name=ID;   

Exp:
   TerminalExp 
   ({Exp.left=current} 
     '+' 
     right=TerminalExp)*;

TerminalExp : value=STRING | dotExpression = DotExpression;

/******************** PROBLEM AREA ********************/

DotExpression : parameterRef=[Parameter] '.' featureRef=FeatureRef;

FeatureRef  :   featureRef=[Feature];

/********************  THE USUAL ********************/

Type:
    DataType | Entity;

DataType:
    'datatype' name=ID;

Entity:
    'entity' name=ID  '{'
        (features+=Feature)*
    '}';

Feature:
     name=ID ':' type=[Type];

此语法完美解析,但函数参数名称的虚线使用未正确链接。我的范围提供者如下,后者异常抛出方法涉及次要问题。

/*
 * generated by Xtext 2.14.0
 */
package org.xtext.example.mydsl.scoping

import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EReference
import org.eclipse.xtext.scoping.IScope
import org.eclipse.xtext.scoping.Scopes
import org.xtext.example.mydsl.myDsl.DotExpression
import org.xtext.example.mydsl.myDsl.FeatureRef

class MyDslScopeProvider extends AbstractMyDslScopeProvider  {

    override getScope(EObject context, EReference reference) {
        if (context instanceof FeatureRef) {
            val myDotExpression = (context as EObject/*?*/).eContainer as DotExpression 
            val features = myDotExpression.parameterRef.type.features
            println("### " + features.stream.map["[" + name + "]"].reduce("", [$0 + $1]))
            Scopes::scopeFor(features)
        }
        super.getScope(context, reference)
    }

    def IScope scope_FeatureRef(FeatureRef context, EReference ref) {
        println("### I have been called")
        throw new RuntimeException("I HAVE BEEN CALLED!");
    }

}

以下输出演示了(1)找到了正确的对象,并且它们具有预期的名称,以及(2)从不调用后一种方法。

### [name][title][occupation]
### [name][code]
### [name][title][occupation]
### [name][code]
  1. 我在第一个范围方法中犯了什么错误?
  2. 为什么后一种方法从未被调用过?

我读过Xtext and Dot/Path-ExpressionsRuntime Concepts:Scoping。我之前也看过这个解决方案,但是几天来谷歌尝试都没有成功。

xtext
1个回答
2
投票

scope_方法只有从AbstractDeclarativeScopeProvider继承才有效,并且应该命名为scope_FeatureRef_featureRef

别忘了回来

return Scopes::scopeFor(features)

重要的是回归

© www.soinside.com 2019 - 2024. All rights reserved.