Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Only allow explicit imports
Only allow explicit imports [message #1856568] Thu, 15 December 2022 16:36 Go to next message
Horacio Hoyos is currently offline Horacio HoyosFriend
Messages: 227
Registered: October 2009
Location: Mexico
Senior Member

Hello,

I have a grammar that uses the importedNamespace mechanism.
QualifiedID:
	ID (QID_SEPARATOR ID)*
;
Import:
	'import' (alias=ID EQUALS)? importedNamespace=QualifiedID SEMI
;


So I can write
package main;
import second;

class A extends B { 
}


The problem is that out of the box B can be referenced EVEN without the import because XText adds all resources in the project to the local scope. To limit this, I created my own GlobalScopeProvider that filters out all resources in the project:
@Override
	protected IScope getScope(
		final Resource context, 
		boolean ignoreCase, 
		EClass type, 
		Predicate<IEObjectDescription> filter) {
		...
		Predicate<IEObjectDescription> resFilter = new Predicate<IEObjectDescription>() {
            @Override
            public boolean apply(IEObjectDescription input) {
            	// Filter out if not in the same resource
            	URI inputBase = input.getEObjectURI().trimFragment();
            	return inputBase.hashCode() == context.getURI().hashCode();
            }
        };
        if (filter != null) {
        	resFilter = (Predicate<IEObjectDescription>) filter.and(resFilter);
        }
		...
	}


But after adding the import, B can't be found. I think the issue is that the "ImportedNamespaceAwareLocalScopeProvider" would eventually use the GlobalScope, which is filtered.

Is there a way to make this work?


Horacio Hoyos Rodriguez
Kinori Tech
Need professional support for Epsilon, EMF?
Go to: https://kinori.tech

[Updated on: Thu, 15 December 2022 16:38]

Report message to a moderator

Re: Only allow explicit imports [message #1856571 is a reply to message #1856568] Thu, 15 December 2022 18:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14434
Registered: July 2009
Senior Member
Assuming

You import will be semantically a

Import second.*

(Your sample does not show what part of the ImportedNamespaceAwareLocalScopeProvideR customization)

You can use a filtering scope. Then if the uri differes, and the element is imported, the imported IEObjectDescription will be an AliasedEObjectDescription


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Only allow explicit imports [message #1856669 is a reply to message #1856571] Wed, 21 December 2022 21:44 Go to previous messageGo to next message
Horacio Hoyos is currently offline Horacio HoyosFriend
Messages: 227
Registered: October 2009
Location: Mexico
Senior Member

Thanks for the reply Christian.

I have not modified the ImportedNamespaceAwareLocalScopeProvider (yet).

I see the AliasedEObjectDescription being created in the ImportScope. However, the getImportFrom() in that class uses the parent scope which is filtered so I never see the elements from the second file in order to import them. Do I need to add them manually in my ImportedNamespaceAwareLocalScopeProvider?

Thanks,


Horacio Hoyos Rodriguez
Kinori Tech
Need professional support for Epsilon, EMF?
Go to: https://kinori.tech
Re: Only allow explicit imports [message #1856697 is a reply to message #1856669] Fri, 23 December 2022 08:51 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14434
Registered: July 2009
Senior Member
No all files are visible everywhere by default
And the importednamespaceawarelocslc scopeprovider can be used to add explicit imports
This makes long names to short names as in java.
About the visibility of non. Local names you need to care about yourself

But with no details
( the example model younshared does not fit to ‚did not adapt anything')
It is hard to tell


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Parser rule for floating points with scientific notation
Next Topic:Problem while adding libraries to xtext Project
Goto Forum:
  


Current Time: Sat Feb 04 16:38:25 GMT 2023

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

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

Back to the top