XText register global variables

1.1k views Asked by At

I want to build tool support for the jape language from gate with the help of XText. Jape is basically a pattern language over annotations; you declare the actions to take when you encounter those annotations. The problem is that the actions can be written in java. After struggling with jdt for a while, I was unable to make it work over parts of the parsed content. So I gave up and decided to use the xbase support with XBlockExpression for that.

The problem is that there are some variables that can be used in the actions - for example there is a variable bindings which allows you to bind and then get annotations from the pattern. So my question is how to register those variables in the xblock scope. After reading the documentation for 3 hours I am still nowhere closer.

Here is a minimal grammar for my problem

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;

I want to parse files with content like this:

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

I started by plugging in my own scope provider but that didn't help me much. Here is the implementation for the provider:

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);
    }
}

Any help will be appreciated

1

There are 1 answers

0
Sebastian Zarnekow On

You should try to implement a JvmModelInferrer for your language where you add the implicitly available variables either as fields or operation arguments in the inferred type. That will do the trick. The approach is nicely documented in the 7 language examples on xtext.org