Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Xbase] Configuring scoping when extending Xbase for use of variables and expressions
[Xbase] Configuring scoping when extending Xbase for use of variables and expressions [message #1690133] Wed, 25 March 2015 12:53
Florian König is currently offline Florian KönigFriend
Messages: 30
Registered: June 2014
Hello there,

I am working on a project for which I have to add an expression language to our DSL. What we want is something (the syntax can be different) like this:

Ecore variable definitions:
define RailCab rc

Variable declaration:
set int maxSpeed = 5

condition < maxSpeed == 5 >
condition < maxSpeed >= rc.getSpeed() >

Since we already failed extending OCL in the past, we want to use Xbase for this. So my first question: Is this a good idea? We don't want to implement our own expression language because that would consume to much time.

My approach (an example):

grammar org.xtext.example.usexbase.UseXbase with org.eclipse.xtext.xbase.Xbase

import ""
generate useXbase ""

	('package' packages+=[EPackage])*

	'import' importURI=STRING;

	ID ('.' ID)*;

	'define' c=[EClass] name=ID;

	'set' var=XVariableDeclaration;

	'condition' '[' expression+=XExpressionOrVarDeclaration ']';

We use importURI to import an Ecore model (RailCab for example). This already works fine. But when we extend Xbase instead of Terminals the scoping does not work anymore.

class UseXbaseScopeProvider extends AbstractDeclarativeScopeProvider {

	def IScope scope_Greeting_c(Greeting greeting, EReference ref) {
		val list = new BasicEList<EClass>()
		(greeting.eContainer as Model).packages.forEach [ p |
		return Scopes::scopeFor(list, getNameComputationForEClass, IScope.NULLSCOPE)

	var Function<EClass, QualifiedName> getNameComputationForEClass = [QualifiedName::create(]


Now we can use variables and expressions from Xbase like:
set var maxSpeed = 5 as int
condition [ maxSpeed == 5 ]
condition [ maxSpeed >= rc.getSpeed() ]

This looks pretty good, BUT: The maxSpeed value in the condition is not the value from the variable, since there is a warning saying 'The value of the local variable maxSpeed is not used'. Also the rc.getSpeed() is not the defined object rc, but only some random value. I can also write
'condition [ xyz>= something.something() ]' which is also correct for the editor.

I guess that is because I don't extend the XbaseScopeProvider. I looked for some other projects that use Xbase, like XCore. There they extend the XbaseScopeProvider. But when I try that it says the class does not exist. Do I have to fix something in my project setup to get that working or is there another way to make the Xbase scoping work? Also how can I tell Xbase that rc is a valid value? I need it to be an EClass and no Xbase variable.

So my questions are:
1. Is this a good idea? Or is there a better way to get an expression language?
2. How can I fix the scoping for the EClasses?
3. How can I fix the Xbase scoping?
4. How can I add rc to the Xbase scope?

I hope you can help me with this since I am working on that for quite a few days now...

I attached my project and an example to this post in case you need to see some more of my code.

Thanks in advance,

[Updated on: Wed, 25 March 2015 12:58]

Report message to a moderator

Previous Topic:Xtext 2.8.1 available
Next Topic:Extending existing files with Xtend
Goto Forum:

Current Time: Mon Apr 19 13:56:50 GMT 2021

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

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

Back to the top