Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Register variables in xtext block of code(How to register a varible when entering a block of code)
Register variables in xtext block of code [message #900963] Thu, 09 August 2012 08:54 Go to next message
Nikola Petrov is currently offline Nikola PetrovFriend
Messages: 4
Registered: August 2012
Junior Member
Hi, I want to be able to register variables as if they were defined in the scope of an xbase block. The Idea is that I want to write tool support for a language that has implicit variables defined in a block of code. For example one of the variables bindings is just a HashMap<String, String>. I want to be able to use that in the block of code as if it was already defined. After reading the whole documentation I am not able to find a solution Sad

Here is a minimal grammar for my problem

(Sorry but the forum recognizes the target namespace as a link and doesn't let me write as I don't have enough posts here)

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

generate myDsl "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, thanks in advance!

[Updated on: Thu, 09 August 2012 08:55]

Report message to a moderator

Re: Register variables in xtext block of code [message #900973 is a reply to message #900963] Thu, 09 August 2012 09:21 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi this is usually done in the jvmmodelinferrer

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Register variables in xtext block of code [message #901219 is a reply to message #900973] Fri, 10 August 2012 11:56 Go to previous message
Nikola Petrov is currently offline Nikola PetrovFriend
Messages: 4
Registered: August 2012
Junior Member
Thanks a lot. I was indeed trying to solve the problem in the wrong place.

Adding a member(members) in a dummy class and then plugging in my own implementation of the generator solved it for me.



I am now having some strange problem with the formatting of the xbase expression. I will be glad if someone can help me with that

I have the following formatter:
public class MyDslFormatter extends XbaseFormatter {
	
	@Override
	protected void configureFormatting(FormattingConfig c) {
		MyDslGrammarAccess grammar = (MyDslGrammarAccess) getGrammarAccess();
		super.configure(c, grammar.getXbaseGrammarAccess());
        }
}


If I have the same grammar as above and I try to format

{
	val test = "Greetings";
	test.toCharArray 
	test.toCharArray
	test.toCharArray
}


I get

{
	val test = "Greetings";
	test.toCharArray test.toCharArray test.toCharArray
}


I can see that this was asked before here but my feature calls are in a XBlockExpression

Do you know what is causing this and how am I supposed to format the xbase properly
Previous Topic:problem with if expression
Next Topic:Change Default size of hover
Goto Forum:
  


Current Time: Fri Mar 29 01:25:01 GMT 2024

Powered by FUDForum. Page generated in 0.02404 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top