Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Custom Scoping( Different Scope in UnitTest and Eclipse-Instance)
Custom Scoping [message #1473074] Fri, 14 November 2014 10:13 Go to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Hi all,

right now I'm trying to customize my scope. I test all my changes in JUnit-Tests and then run an Eclipse instance to try it out in "real-life" mode.

But I notice different behavior in the Unittests and the generated Xtext-Editor.
The Unittests show no errors after the parsing but the Xtext-Editor throws an Error:
"Couldn't resolve reference to.."

How could this happen?

The tests looks like this:

	@Test
	def void testDerivedPropertyAccess() {
		val model = '''
			class  Car {
				price:Double;
			}
			class Person {	
				public car:Car = new Car();	
				public doStuff(){
					car.price;
			}	
			}
		'''.parse
		
		 model.assertNoErrors
}


And exactly the same model throws an error in the Xtext-Editor:
https://dl.dropboxusercontent.com/u/18688378/eclipseCoding/errors/couldnt_resolve_ref.jpg


I thought UnitTests were equal to the parsed model in the Xtext-Editor.

Any help is welcome!

~Alex
Re: Custom Scoping [message #1473156 is a reply to message #1473074] Fri, 14 November 2014 11:32 Go to previous messageGo to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Hi,

the weird thing about this is: The member "price" is in the context of the "car.reference"
(cause the auto-quickfix is availiable) but the linker may not find the right element to reference to.

Any suggestions?

Re: Custom Scoping [message #1473158 is a reply to message #1473156] Fri, 14 November 2014 11:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi

xtetx behaves differently in ui and standalone. but if you do it right this should not matter.

can you share

(a) a reproducable grammar
(b) all relevant customizations
(3) the complete test


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 14 November 2014 11:38]

Report message to a moderator

Re: Custom Scoping [message #1473264 is a reply to message #1473158] Fri, 14 November 2014 13:26 Go to previous messageGo to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Hi,

I configured a new grammar + costum scope but I was not able to reproduce the error in the new project....

The important part of the grammar looks like this:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
	classes+=Class*;
	
NamedElement returns NamedElement:
	Class | Field | Method ;

Class:
	'class' name=ID '{'
	members+=Member*
	'}';

Member:
	Field | Method;
//
Field:
	type=[Class|QualifiedName] name=ID ';';

QualifiedName:
	ID ('.' ID)*;
//
Method:
	type=[Class|QualifiedName] name=ID
	'(' ')'  
	body=MethodBody;
//
MethodBody:
	{MethodBody} '{' statements+=Statement* '}';
//
Statement returns Expression:
	Expression ';';
//
Expression returns Expression:
	PrimaryExpression
	
;
//
PrimaryExpression returns Expression:
	PropertyAccessExpression;

PropertyAccessExpression:
	{PropertyAccessExpression} featureReference=FeatureReference;

FeatureReference:
	{FeatureReference}
	(expression=NameExpression) | (expression=NameExpression '.' nameBinding=[Member])
	;
NameExpression:
	{NameExpression}
	nameBinding=[NamedElement|ID];


And the ScopeProvider looks like this:

class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {

	def IScope scope_Member(FeatureReference featureRefContext, EReference eReference) {
		var parentScope = IScope::NULLSCOPE
		val expr = (featureRefContext.expression as NameExpression)

		val field = expr.nameBinding as Field
		val type = field.type

		if (type instanceof Class) { 

			 
			val innerScope = type.selectMemberInRightOrder(featureRefContext)
			return Scopes::scopeFor(innerScope,parentScope)
		}

	}

	def selectMemberInRightOrder(Class type, FeatureReference ref) {

		 
		switch (type) {
			Class: type.members.filter(typeof(Field)) + type.members.filter(typeof(Method))
		}

	}

}


But some words to the original problem. I debugged the my old dsl and watched into my ScopeProvider. Here I noticed that the refereces

FeatureReference:
{FeatureReference}

(expression=NameExpression) | (expression=NameExpression '.' nameBinding=[Member])
;


Were still null! But when I runt the UnitTest they are NOT null!
What may cause this problem??

Thanks a lot for your help,

Alex
Re: Custom Scoping [message #1473306 is a reply to message #1473264] Fri, 14 November 2014 14:12 Go to previous messageGo to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
I found the problem!
I hooked into the linking phase and overrided some methods...( a year ago or so..) to set some default values to rootelements.
but somehow this crashes the whole linking...

Here my linking method:

	@Override
	public void ensureLinked(EObject obj, IDiagnosticProducer producer) {
		super.ensureLinked(obj, producer);

		if (obj instanceof Parameter && checkNode(obj)) {
			Parameter par = (Parameter) obj;

			// set the uppervalue of an param to 1
			if (par.getUpper() == 0) {
				par.setUpper(1);

			}

		}
		if (obj instanceof Model && checkNode(obj)) {
			((Model) obj).setName("model");
		}

	}

	private boolean checkNode(EObject obj) {

		return NodeModelUtils.getNode(obj) != null;
	}


But I'm still don't know what is going wrong here. If you have some time maybe you could comment my ensureLinked-method.

~Alex
Re: Custom Scoping [message #1473456 is a reply to message #1473306] Fri, 14 November 2014 16:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

just one remark: since xtext creates objects lazyly the context object in the content assist scoping might be a parent


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Custom Scoping [message #1476685 is a reply to message #1473456] Mon, 17 November 2014 09:04 Go to previous messageGo to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Hi,

thanks for the hint!
Is there a way to show the contentassist for the children without starting to type
one of their names?

~Alex
Re: Custom Scoping [message #1476694 is a reply to message #1476685] Mon, 17 November 2014 09:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi i dont get that question

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Custom Scoping [message #1476707 is a reply to message #1476694] Mon, 17 November 2014 09:28 Go to previous messageGo to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Hi,

here an example of what I wanted to say:

On the left side, the contentassits doesn't show the props of the class Car ( even though the scope is right).
On the right side it works fine, but I have to start typing one of the properties name.

https://dl.dropboxusercontent.com/u/18688378/eclipseCoding/question/contentAssist.jpg
Re: Custom Scoping [message #1476717 is a reply to message #1476707] Mon, 17 November 2014 09:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,


with the grammar you posted i cannot reproduce that.
but as said before: it depends on the grammar and how you implement the scoping


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Custom Scoping [message #1499825 is a reply to message #1476717] Fri, 05 December 2014 12:47 Go to previous messageGo to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Hi Christian,

I created a whole xtext project that produces this bug. The bug itself looks like this:

https://dl.dropboxusercontent.com/u/18688378/eclipseCoding/question/contentAssistBug.jpg

You can find all the projects: ..mydsl, ..ui, ..test here:
https://dl.dropboxusercontent.com/u/18688378/eclipseCoding/question/autocompleteBug.7z

To achieve this bug I also customized the scope. It would be great if you find the time to look at this problem..

Thanks for your help!

~Alx
Re: Custom Scoping [message #1499852 is a reply to message #1499825] Fri, 05 December 2014 13:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
the problem is in your grammar. the ui parser treats the "." as extranous input.
thus the input model is not as expected.

i have no idea how to workaround that. (besides moving the ; to dev null or removing the predicate

FeatureReference returns Expression:
	NameOrThisExpression ({FeatureReference.expression=current} '.' nameBinding=[Member])* ';';


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Custom Scoping [message #1499862 is a reply to message #1499852] Fri, 05 December 2014 13:26 Go to previous messageGo to next message
Alexander R is currently offline Alexander RFriend
Messages: 211
Registered: July 2013
Senior Member
Hi,

hmm thanks for the hint! I found the following post of you: https://www.eclipse.org/forums/index.php/t/457539/
Here you said, that one may do the following:
Quote:

P.S: i think the problem is often xtexts lazy object creation
(affects you if the cross reference i the first "real" thing (first assignment) in the rule for the object

XXX: "keyword" yyy=[KKK]

then you have 2 possibilities to workaround this
(1) use scope_XXX_yyy(ParentObject ctx, Ereference ref) instead of scope_XXX_yyy(XXX ctx, Ereference ref)
(2) try to use a unassigned action to force xtext to create the object eager
XXX: {XXX}"keyword" yyy=[KKK]


Is this a real solution for me, what do you think?

~Alex
Re: Custom Scoping [message #1499863 is a reply to message #1499862] Fri, 05 December 2014 13:29 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

as i said.

the following grammar and scoping works for me

FeatureReference returns Expression:
	NameOrThisExpression ({FeatureReference.expression=current} '.' nameBinding=[Member])* ';';
	def IScope scope_FeatureReference_nameBinding(FeatureReference ctx, EReference ref) {
		val target = ctx.expression
		val type = getType(target) as Class
		var parentScope = IScope::NULLSCOPE
		if (type == null || !(type instanceof Class)) {
			return parentScope
		}

		return Scopes::scopeFor(type.selectMemberInRightOrder(), parentScope)
	}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Referencing classes not yet generated
Next Topic:refreshing outline on mouse hover (with Ctrl pressed)
Goto Forum:
  


Current Time: Fri Apr 26 08:52:23 GMT 2024

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

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

Back to the top