Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext scoping with two models(How to create a scope with qualified names?)
Xtext scoping with two models [message #1020477] Mon, 18 March 2013 09:10 Go to next message
Wernke zur Borg is currently offline Wernke zur BorgFriend
Messages: 39
Registered: December 2011

I have a main language model and a second one for library functions, which is imported into the main one. I went along the lines of Sebastian Zarnekow's Xtext Corner #8 - Libraries Are Key and Meinte Boersma's Getting alternative cross-references to work with existing EPackages. Sorry I am not allowed to post the links as I have fewer than five posts in this forum. You have to copy and paste them:


I need the latter because the main language allows to call both local functions and library functions. Local functions are referred to by the simple name function(), library functions by a qualified name library.function().

Here are a few simplified snippets:

    'function' name=ID '(' fpList=FormalArgList ')'

    functionName=[ecore::EObject|QualifiedName] '(' argList=ActualArgList? ')'

    ID ('.' ID)?	

    'function' name=QualifiedName '(' fpList=FormalArgList? ')' 

	ID '.' ID

With my custom scope provider I am able to make the library functions visible, so that both local functions and library functions are proposed as code completion options:

	IScope scope_FunctionCallType_functionName(MyModel model, EReference ref) {
		// get all local function definitions
		List<EObject> crossRefTargets = Lists.newArrayList();
		Iterables.addAll(crossRefTargets, EcoreUtil2.getAllContentsOfType(model, FunctionDefinitionType.class));
		// Add library functions to the scope
		LibraryModel lm = getLibraryModel();
		List<LibraryFunction> libFuncs = EcoreUtil2.getAllContentsOfType(lm, LibraryFunction.class);
		Iterables.addAll(crossRefTargets, libFuncs);
		return Scopes.scopeFor(crossRefTargets);

But when I then select a library function from the list the code is flagged as an error "Couldn't resolve reference to EObject lib.function".

I have debugged into the code and found that it has to do with qualified names. Apparently the library function names get created as simple names with one segment [lib.function], whereas the function call is looking for a two-segment name [lib].[function].

One problem here I guess is the fact that there is no EObject "Library" that could carry the name of the library - the library name is implicitly defined as the first part in the function definition (see 'QualifiedName' in the Library.xtext). Therefore I do not have a parent scope of which the scope of library functions could be a child.

Is there a possibility to create an artificial parent scope with the library name? Or how could I avoid qualified names at this point and use flat names on both sides? Note I need the QualifiedNamesFragment in my workflow for other language parts.

Thanks for any advice,
Re: Xtext scoping with two models [message #1020776 is a reply to message #1020477] Mon, 18 March 2013 22:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13613
Registered: July 2009
Senior Member

the method org.eclipse.xtext.scoping.Scopes.scopeFor(Iterable<? extends T>, Function<T, QualifiedName>, IScope)
is your friend

Need professional support for Xtext, Xpand, EMF?
Go to:
Twitter : @chrdietrich
Blog :
Re: Xtext scoping with two models [message #1020961 is a reply to message #1020776] Tue, 19 March 2013 08:56 Go to previous message
Wernke zur Borg is currently offline Wernke zur BorgFriend
Messages: 39
Registered: December 2011
Yes, I found that function myself and it is working. I was not sure though whether it's a clean way of doing it. But as you confirm the approach I will be happy. Smile

Thank you!
Previous Topic:How to implement a code generators workflow nowadays (MWE2 or Xtend2)?
Next Topic:org.junit.ComparisonFailure when unittesting my language
Goto Forum:

Current Time: Mon Sep 28 07:04:45 GMT 2020

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

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

Back to the top