Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Qualified name relative to a namespace import?
Qualified name relative to a namespace import? [message #1424189] Mon, 15 September 2014 16:06 Go to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1746
Registered: July 2009
Senior Member
I'm in the process of migrating a pile of DSLs from importURI to importedNamespace.

At the point where I believe my qualified name providers and scope providers are in a fairly good shape, I'm trying to achieve that an element with name x.y.z can be referenced by
- importing x.y and
- using y.z as the reference (just as you would do for static members in Java).
Since y is in scope, I want to interpret y.z as a qualified name relative to that y.

Is this supposed to work out of the box (i.e., I broke it by some customization)?

Is possible by some additional tweak?

Does this need to be implemented manually?

I've seen solutions with an explicit sub-grammar for dot expressions, but this isn't so attractive, for two reasons: (a) it seems to require to define this for every reference in the grammar that should make use of this concept, and (b) it seems to require knowledge about the intermediate types, but if z can be reached via x.y.z and x.a.z those 'y' and 'a' could possibly be of unrelated types.

cheers,
Stephan

PS: this is at Xtext 2.4.3. Migration to 2.7.1 has been deferred until after the import change is complete ... not sure if this matters for the issue at hand.


Re: Qualified name relative to a namespace import? [message #1424205 is a reply to message #1424189] Mon, 15 September 2014 16:39 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3104
Registered: July 2009
Senior Member
Hi Stephan,

no, this doesn't work out of the box. You'd have to adjust
ImportedNamespaceAwareLocalScopeProvider.doCreateImportNormalizer(QualifiedName,
boolean, boolean) to use that feature together with the
importedNamespace convenience.

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 15.09.14 18:06, schrieb Stephan Herrmann:
> I'm in the process of migrating a pile of DSLs from importURI to
> importedNamespace.
>
> At the point where I believe my qualified name providers and scope
> providers are in a fairly good shape, I'm trying to achieve that an
> element with name x.y.z can be referenced by
> - importing x.y and - using y.z as the reference (just as you would do
> for static members in Java).
> Since y is in scope, I want to interpret y.z as a qualified name
> relative to that y.
>
> Is this supposed to work out of the box (i.e., I broke it by some
> customization)?
>
> Is possible by some additional tweak?
>
> Does this need to be implemented manually?
>
> I've seen solutions with an explicit sub-grammar for dot expressions,
> but this isn't so attractive, for two reasons: (a) it seems to require
> to define this for every reference in the grammar that should make use
> of this concept, and (b) it seems to require knowledge about the
> intermediate types, but if z can be reached via x.y.z and x.a.z those
> 'y' and 'a' could possibly be of unrelated types.
>
> cheers,
> Stephan
>
> PS: this is at Xtext 2.4.3. Migration to 2.7.1 has been deferred until
> after the import change is complete ... not sure if this matters for the
> issue at hand.
>
>
>
Re: Qualified name relative to a namespace import? [message #1424217 is a reply to message #1424205] Mon, 15 September 2014 17:00 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1746
Registered: July 2009
Senior Member
Hi Sebastian,

Thanks for the quick response!
Good to know I didn't break it Smile
I'll try to figure out if this can be done in that hook you point out --
hopefully in a re-usable way ...

cheers,
Stephan
Re: Qualified name relative to a namespace import? [message #1424263 is a reply to message #1424217] Mon, 15 September 2014 18:36 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1746
Registered: July 2009
Senior Member
FWIW: this is what I came up with:
public class MyImportedNamespaceAwareLocalScopeProvider extends ImportedNamespaceAwareLocalScopeProvider {
	
	@Override
	protected ImportNormalizer doCreateImportNormalizer(QualifiedName importedNamespace, boolean wildcard, boolean ignoreCase) {
		return new JoiningImportNormalizer(importedNamespace, wildcard, ignoreCase);
	}

	static class JoiningImportNormalizer extends ImportNormalizer {

		public JoiningImportNormalizer(QualifiedName importedNamespace, boolean wildCard, boolean ignoreCase) {
			super(importedNamespace, wildCard, ignoreCase);
		}
		@Override
		public QualifiedName resolve(QualifiedName relativeName) {
			if (relativeName.getSegmentCount() > 1 && !hasWildCard()) {
				String pivot = relativeName.getFirstSegment();
				if (isIgnoreCase())
					pivot = pivot.toLowerCase();
				QualifiedName importPrefix = getImportedNamespacePrefix();
				if (pivot.equals(importPrefix.getLastSegment()))
					return importPrefix.append(relativeName.skipFirst(1));
			}
			return super.resolve(relativeName);
		}
		@Override
		public QualifiedName deresolve(QualifiedName fullyQualifiedName) {
			if (!hasWildCard()) {
				QualifiedName importPrefix = getImportedNamespacePrefix();
				if (fullyQualifiedName.startsWith(importPrefix))
					return fullyQualifiedName.skipFirst(importPrefix.getSegmentCount()-1);
			}
			return super.deresolve(fullyQualifiedName);
		}
	}
}


No rocket science in fact, but looks indeed re-usable. Would Xtext like to adopt s.t. like this?
I wonder what use cases this might break, but since normal ImportNormalizers only do their work for segment count == 1 we seem to handle disjoint use cases.
Or did you have specific reasons, *not* to implement such behavior?

best,
Stephan

Re: Qualified name relative to a namespace import? [message #1424626 is a reply to message #1424263] Tue, 16 September 2014 07:35 Go to previous message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3104
Registered: July 2009
Senior Member
Hi Stephan,

from the top of my head I don't remember if explicitly opted out from
that behavior. So a ticket + a patch would be nice.

Best regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 15.09.14 20:36, schrieb Stephan Herrmann:
> FWIW: this is what I came up with:
>
> public class MyImportedNamespaceAwareLocalScopeProvider extends
> ImportedNamespaceAwareLocalScopeProvider {
>
> @Override
> protected ImportNormalizer doCreateImportNormalizer(QualifiedName
> importedNamespace, boolean wildcard, boolean ignoreCase) {
> return new JoiningImportNormalizer(importedNamespace, wildcard,
> ignoreCase);
> }
>
> static class JoiningImportNormalizer extends ImportNormalizer {
>
> public JoiningImportNormalizer(QualifiedName importedNamespace,
> boolean wildCard, boolean ignoreCase) {
> super(importedNamespace, wildCard, ignoreCase);
> }
> @Override
> public QualifiedName resolve(QualifiedName relativeName) {
> if (relativeName.getSegmentCount() > 1 && !hasWildCard()) {
> String pivot = relativeName.getFirstSegment();
> if (isIgnoreCase())
> pivot = pivot.toLowerCase();
> QualifiedName importPrefix = getImportedNamespacePrefix();
> if (pivot.equals(importPrefix.getLastSegment()))
> return importPrefix.append(relativeName.skipFirst(1));
> }
> return super.resolve(relativeName);
> }
> @Override
> public QualifiedName deresolve(QualifiedName fullyQualifiedName) {
> if (!hasWildCard()) {
> QualifiedName importPrefix = getImportedNamespacePrefix();
> if (fullyQualifiedName.startsWith(importPrefix))
> return
> fullyQualifiedName.skipFirst(importPrefix.getSegmentCount()-1);
> }
> return super.deresolve(fullyQualifiedName);
> }
> }
> }
>
>
> No rocket science in fact, but looks indeed re-usable. Would Xtext like
> to adopt s.t. like this?
> I wonder what use cases this might break, but since normal
> ImportNormalizers only do their work for segment count == 1 we seem to
> handle disjoint use cases.
> Or did you have specific reasons, *not* to implement such behavior?
>
> best,
> Stephan
>
>
Previous Topic:Xtext example how to configure validation via preferences
Next Topic:Custom validation in xtext
Goto Forum:
  


Current Time: Wed Dec 11 20:41:45 GMT 2019

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

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

Back to the top