Xtext scoping with two models [message #1020477] |
Mon, 18 March 2013 09:10 |
Wernke zur Borg Messages: 42 Registered: December 2011 |
Member |
|
|
Hi,
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:
1. zarnekow.blogspot.de/2012/11/xtext-corner-8-libraries-are-key.html
2. dslmeinte.wordpress.com/2010/12/08/getting-alternative-cross-references-to-work-with-existing-epackages/
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:
Main.xtext:
FunctionDefinitionType:
'function' name=ID '(' fpList=FormalArgList ')'
FunctionCall:
functionName=[ecore::EObject|QualifiedName] '(' argList=ActualArgList? ')'
;
QualifiedName:
ID ('.' ID)?
;
Library.xtext:
LibraryFunction:
'function' name=QualifiedName '(' fpList=FormalArgList? ')'
;
QualifiedName:
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,
Wernke
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03393 seconds