Home » Modeling » TMF (Xtext) » Qualified name relative to a namespace import?
Qualified name relative to a namespace import? [message #1424189] |
Mon, 15 September 2014 12:06  |
Eclipse User |
|
|
|
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 12:39   |
Eclipse User |
|
|
|
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 #1424263 is a reply to message #1424217] |
Mon, 15 September 2014 14:36   |
Eclipse User |
|
|
|
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 03:35  |
Eclipse User |
|
|
|
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
>
>
|
|
|
Goto Forum:
Current Time: Wed Jul 23 09:56:35 EDT 2025
Powered by FUDForum. Page generated in 0.08059 seconds
|