Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Shadowing with ImportedNamespaceAwareSP(Shadowing without different QualifiedNames)
Shadowing with ImportedNamespaceAwareSP [message #669278] Mon, 09 May 2011 17:32 Go to next message
Simon Stratmann is currently offline Simon StratmannFriend
Messages: 27
Registered: February 2011
Junior Member
Hello,

I've been trying to adjust the ImportedNamespaceAwareScopeProvider.

Say this is part of my language:
entity e
 var v;
 block b
 var v;
end entity;


Here the two different "v" variables aren't a problem because their QN are e.v and e.b.v respectively.

But look at this:
entity e
 var v;
 for v = 1 to 5
 refTo v; //needs to refer to the loop counter
 refTo e.v; //needs to refer to the "upper" v
 end for;
end entity;


Here the second v has the same QN as the first one, so the validator shows an error, saying there's a duplicate variable. But the second should shadow the first one.

Any ideas on how I would achieve that? There are several cases / surrounding elements where this would need to be done.

Thanks,

Simon

[Updated on: Mon, 09 May 2011 17:52]

Report message to a moderator

Re: Shadowing with ImportedNamespaceAwareSP [message #669376 is a reply to message #669278] Tue, 10 May 2011 06:33 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

my first idea would be to adapt (or deactivate) the duplicate name validation. Then you have to implement the scoping such that the loop variable is known under its simple name in the loop scope and the regular variable with its qualified name.

Another possibility would be to calculate the name of the loop variable differently (only simple name rather than e.v, as it should not be visible to the outside world anyway).

Alex
Re: Shadowing with ImportedNamespaceAwareSP [message #670506 is a reply to message #669376] Sat, 14 May 2011 14:56 Go to previous messageGo to next message
Simon Stratmann is currently offline Simon StratmannFriend
Messages: 27
Registered: February 2011
Junior Member
Alex,

as always thanks for your help. I followed your advice and it worked like planned, but now I have the same problem one level higher. Now say this is my code:

ENTITY e
 VAR v; -- name is e.v
PROCESS
 VAR v; --name is v
  BEGIN
   REFTO v; //should refer to the v in the block
   FOR v = 1 to 5
    REFTO v; //should refer to the loop counter
   END FOR;
 END PROCESS:
END PROCESS;


So in this case the two inner v variables should both have simple names, but the one in the FOR loop should shadow the one declared in the PROCESS. The PROCESS can optionally have a name; in this case I don't have a problem because then the variable in it would have the FQN e.NAME.v and the v in the LOOP statement would be simply v.
So I don't think that just changing the name calculation will help, because even if I disable the duplicate name validation the "Jump to declaration" function would jump from the "REFTO v;" to the variable declaration in the PROCESS. That's why I tried shadowing the scope, but it doesn't seem to work the way I thought:

if (context instanceof loop_statement) {
			log.debug("Creating new scope for loop statement {}", context);
			result = Scopes.scopeFor(EcoreUtil2.eAllContents(context), result);
		}


at the end of getLocalDescriptions().

If anything else fails I will probably change the calculation of the FQN that it uses a generic name for the loop so that its child variable will be called "e.process.loop1.v" or something.
Re: Shadowing with ImportedNamespaceAwareSP [message #670516 is a reply to message #670506] Sat, 14 May 2011 16:05 Go to previous message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

please make your scoping smart! The snippet you provide is not pointing in that direction.
a) Use EcoreUtil2.eAllContents only as a last resource. You know your model structure and thus can navigate to the elements you want in the scope
b) Use the inner/outer-scope possibilities (see blog posts by Sven Efftinge on the concept itself, the Scopes-method allow defining scopes with an inner and an outer one). Your language has a block structure, so the variables in the current block would go to the inner scope, those of containing block to the outer one (note, the outer scope has the same layer structure again). That way you automatically achieve the shadowing you want. Variables in the inner scope automatically hide variables with the same name in the outer scope as they are found first. Note that if an inner scope contains two variables with the same name, they hide each other.
c) use polymorphic dispatch in the scope provider. That way you don't have to do instance-of-cascades

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Previous Topic:Unassigned Rule calls
Next Topic:XTEXT - Problems with my grammar
Goto Forum:
  


Current Time: Fri Apr 26 19:14:46 GMT 2024

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

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

Back to the top