XText注册全局变量

时间:2012-08-08 21:37:39

标签: java eclipse-emf xtext xtend xbase

我希望在jape language from gate的帮助下为XText构建工具支持。 Jape基本上是注释的模式语言;您声明在遇到这些注释时要采取的操作。问题是这些动作可以用java编写。在与jdt争吵了一段时间之后,我无法让它在部分解析内容上运行。所以我放弃了,决定使用xbase支持和XBlockExpression。

问题在于可以在动作中使用一些变量 - 例如,有一个变量 bindings ,它允许您绑定然后从模式中获取注释。所以我的问题是如何在xblock范围内注册这些变量。阅读文档3小时后,我仍然离我更近了。

这是我的问题的最小语法

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.Xbase

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

Model:
    greetings=Greeting;


Greeting:
    block=XBlockExpression;

我想解析包含以下内容的文件:

{
    val testAS = bindings.get("test") as AnnotationSet
}

我从插入我自己的范围提供商开始,但这对我没什么帮助。以下是提供者的实现:

package org.xtext.example.mydsl;

import java.util.List;

public class MyScopeProvider extends XbaseScopeProvider {

    XbaseFactory factory = new XbaseFactoryImpl();

    @Override
    public IScope getScope(EObject context, EReference reference) {
        //System.err.println(context);
        //System.err.println(reference);
        List<IValidatedEObjectDescription> descriptions = Lists.newArrayList();
        XVariableDeclaration variableDeclaration = factory
                .createXVariableDeclaration();
        variableDeclaration.setName("bindings");
        IValidatedEObjectDescription variableDescription = createLocalVarDescription(variableDeclaration);

        System.err.println(variableDescription);

        IScope scope = super.getScope(context, reference);
        System.err.println(variableDeclaration);
        return new JvmFeatureScope(scope, "test", descriptions);
    }
}

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

您应该尝试为您的语言实现JvmModelInferrer,您可以将隐式可用变量添加为推断类型中的字段或操作参数。那就行了。在xtext.org

的7个语言示例中很好地记录了该方法