Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Context-dependent Name Resolution
Context-dependent Name Resolution [message #1732505] Tue, 17 May 2016 18:58 Go to next message
Charles Baker is currently offline Charles BakerFriend
Messages: 9
Registered: May 2016
Junior Member
Hi,

Is there any way to make XText resolve names depending on the scope they're in? For instance, let's say I want to parse a programming language that supports C-like structures and a "with" keyword (just like the one in Visual Basic). That means this:

struct MyStructure { int a, b; }
MyStructure myStruct;
with (myStruct)
{
a = 5;
b = 7;
}

... should be equivalent to this:

MyStructure myStruct;
myStruct.a = 5;
myStruct.b = 7;

So, inside the "with" block, "a" and "b" need to become "myStruct.a" and "myStruct.b", respectively. I tried to implement this using scope providers, but I kept getting an error about xtext not being able to resolve the proxy objects for variables inside "with" blocks, because the fully qualified name of "a" is still "myStruct.a" and not just "a".
Is there some way to override the default proxy resolution behavior and make it scope-dependent? Or should this be implemented in some entirely different way?
Re: Context-dependent Name Resolution [message #1732508 is a reply to message #1732505] Tue, 17 May 2016 19:09 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13192
Registered: July 2009
Senior Member
This is usually implemented inside yourdsl scopeprovider

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Context-dependent Name Resolution [message #1732511 is a reply to message #1732508] Tue, 17 May 2016 19:25 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13192
Registered: July 2009
Senior Member
e.g. (implemented with Xtext >= 2.9.x)

Model:
	elements+=Element*;
	
Element:
	Struct | StructInstance | WithClause
;

Struct:
	"struct" name=ID "{"
		properties += Property*
	"}"
;

Property:
	name=ID ";"
;

StructInstance:
	struct=[Struct] name=ID ";"
;

WithClause:
	"with" "(" structInstance=[StructInstance] ")" "{"
		assignments+=Assignment*
	"}"
;

Assignment:
	property=[Property] "=" value=INT
;



class MyDslScopeProvider extends AbstractMyDslScopeProvider {
	
	override getScope(EObject context, EReference reference) {
		if (reference == MyDslPackage.Literals.ASSIGNMENT__PROPERTY) {
			val withClause = EcoreUtil2.getContainerOfType(context, WithClause)
			if (withClause != null) {
				return Scopes.scopeFor(withClause.structInstance.struct.properties)
			}
		}
		super.getScope(context, reference)
	}

}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Context-dependent Name Resolution [message #1732512 is a reply to message #1732508] Tue, 17 May 2016 19:20 Go to previous messageGo to next message
Charles Baker is currently offline Charles BakerFriend
Messages: 9
Registered: May 2016
Junior Member
If I understand correctly, a scope provider returns a list of qualified names for a certain context and reference. But how could it change the way in which the qualified name is built from the string? For example, if I return "myStruct.a" (as a QualifiedName), then that's what appears when I use code completion in Eclipse, and that gets resolved properly. But if I return "a", then an "a" appears instead, and it can't figure out that "a" is supposed to behave just like "myStruct.a" in this case. Do I need to use a QualifiedNameConverter for that? If so, where could I find some examples that show how QualifiedNameConverters work?
Re: Context-dependent Name Resolution [message #1732513 is a reply to message #1732511] Tue, 17 May 2016 19:34 Go to previous messageGo to next message
Charles Baker is currently offline Charles BakerFriend
Messages: 9
Registered: May 2016
Junior Member
Hmm... In your example, what is the fully qualified name of a property? "StructureName.PropertyName" or just "PropertyName"?
Re: Context-dependent Name Resolution [message #1732514 is a reply to message #1732511] Tue, 17 May 2016 19:37 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13192
Registered: July 2009
Senior Member
A scope is a collection of ieobjectdescriptions (basically a pair of name and eobject(uri))


Well - it depends.

How the names for the xtext index are built is the IQualifiedNameProvider which is usually implemented by DefaultDeclarativeQualifiedNameProvider

For the scope provider: there are other methods inside the scopes class e.g. One that takes the list of objects (the scope) the parent scope (I scope.nullscope) and a function that calculates the qualified name for a given eobject


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Context-dependent Name Resolution [message #1732518 is a reply to message #1732514] Tue, 17 May 2016 19:53 Go to previous messageGo to next message
Charles Baker is currently offline Charles BakerFriend
Messages: 9
Registered: May 2016
Junior Member
Is there any alternative to Scopes.scopeFor that lets me provide my own way of mapping names to URIs? I need "a" to resolve to "#MyStruct/a" (if that's the correct syntax) instead of just "#a". By printing the URIs, I've discovered that it just appends the name of the property, without the name of the structure, and that can't possibly work because I need to be able to have different structures that share a property name. as in:

struct MyStructA { int a; }
struct MyStructB { int a; }

Perhaps I can somehow create the IScope manually?

[Updated on: Tue, 17 May 2016 20:01]

Report message to a moderator

Re: Context-dependent Name Resolution [message #1732519 is a reply to message #1732518] Tue, 17 May 2016 19:54 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13192
Registered: July 2009
Senior Member
As I said. Have a look at the class there is a method that takes a name call function.

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Context-dependent Name Resolution [message #1732520 is a reply to message #1732519] Tue, 17 May 2016 19:58 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13192
Registered: July 2009
Senior Member
Dane yes you can create the simple scope manually ( the scopes classes methods do the same)

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Context-dependent Name Resolution [message #1732521 is a reply to message #1732520] Tue, 17 May 2016 20:11 Go to previous messageGo to next message
Charles Baker is currently offline Charles BakerFriend
Messages: 9
Registered: May 2016
Junior Member
No matter what I try, I keep getting a "Couldn't resolve reference" error in Eclipse. Could the URIs of my EObjects be wrong somehow?
I tried this:

var descriptions = new ArrayList<IEObjectDescription>();
for (Attribute attribute : context.type.attributes) {
descriptions.add(EObjectDescription.create(QualifiedName.create(attribute.name), attribute);
}
return new SimpleScope(descriptions);

I guess I should also mention it accepts this:
with (myStruct) { "StructureName.a" = 5; }
but not this:
with (myStruct) { a = 5; }
The latter gets a "Couldn't resolve reference to Attribute 'a'" error.
According to my QualifiedNameProvider, the fully qualified name of "a" is "program.MyStructure.a".

[Updated on: Tue, 17 May 2016 20:15]

Report message to a moderator

Re: Context-dependent Name Resolution [message #1732522 is a reply to message #1732521] Tue, 17 May 2016 20:25 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13192
Registered: July 2009
Senior Member
Can you share a grammar and the complete class as well

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Context-dependent Name Resolution [message #1732524 is a reply to message #1732522] Tue, 17 May 2016 20:36 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13192
Registered: July 2009
Senior Member
.ps make sure your code gets actually called (debugger)

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Context-dependent Name Resolution [message #1732530 is a reply to message #1732524] Tue, 17 May 2016 22:28 Go to previous message
Charles Baker is currently offline Charles BakerFriend
Messages: 9
Registered: May 2016
Junior Member
OK. I just tried to write the smallest possible example of what I want to do that doesn't work and... it worked.
So I decided to recheck everything, and I realized I was applying my scope at the wrong time.
In other words, my code wasn't actually called. Smile
Thanks for all the help!
Previous Topic:Create Custom Project
Next Topic:Support two different grammars in eclipse
Goto Forum:
  


Current Time: Fri Nov 15 09:55:06 GMT 2019

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

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

Back to the top