Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Combining importURI and importedNamespace
Combining importURI and importedNamespace [message #769566] Thu, 22 December 2011 10:47 Go to next message
Torsten Schiefer is currently offline Torsten SchieferFriend
Messages: 26
Registered: December 2011
Junior Member
Hi there,

my Editor should hande two file-types.
The one with the extension .os declares a namespace like this:
using Main.C;
using Main.B;
using Main2.*;

#include "relativePath/morepath/anoNasp.osi"

namespace Main {
    B newFunctionDeclaration(C parameter) {
        ... Statements ...
        parameter = otherFunctionDeclaration(this);
        ... Statements ...
    }
}

The imported namespaces have the same form.
The UsingImport rule look in the grammar:
UsingImport : 'using' importedNamespace=QualifiedNameWithWildCard ';';

The Include rule:
Include : '#include' importURI=STRING;

The included files have the file extension osi and only can declare anonymous namespaces like this:
namespace {
    C otherFunctionDeclaration(Main parameter) {
        ... Statements ...
    }
}

My workflow contains the following lines:
            
// scoping and exporting API
fragment = scoping.ImportURIScopingFragment {}
// fragment = exporting.SimpleNamesFragment {}
fragment = scoping.ImportNamespacesScopingFragment {}
fragment = exporting.QualifiedNamesFragment {}

My question is: Which ScopeProviders I have to use to make this work?
How can I combine the two ImportUriGlobalScope and ImportedNamespaceAwareLocalScope?
Which Providers (e.g. NameProviders) I also have to individualize?
I've also written an ImportUriResolver, so the included recources can be found.
But if I call any Scope in my own OSScopeProvider or OSLinkingService, they are very empty. Why?

I know, anything should work out-of-the-box, but I don't know the right key for the box Wink
Re: Combining importURI and importedNamespace [message #769589 is a reply to message #769566] Thu, 22 December 2011 11:37 Go to previous messageGo to next message
Torsten Schiefer is currently offline Torsten SchieferFriend
Messages: 26
Registered: December 2011
Junior Member
Oh, I forgot:
I have no possibility to change the syntax of this grammar. Only the semantics.

Thank you for your replies, in advance.
Torsten
Re: Combining importURI and importedNamespace [message #769649 is a reply to message #769589] Thu, 22 December 2011 13:23 Go to previous messageGo to next message
Torsten Schiefer is currently offline Torsten SchieferFriend
Messages: 26
Registered: December 2011
Junior Member
An interesting phenomenon:
If I deactivate the binding to the ImportUriGlobalScope, the imported namespaces are in the scope of a Resource plus all objects of all anonymous namespaces.
If I activate the binding to the ImportUriGlobalScope, all objects of the included anonymous namespaces are in the scope but no imported namespaces accept of the own.
Re: Combining importURI and importedNamespace [message #769671 is a reply to message #769649] Thu, 22 December 2011 14:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi,

without knowledge of your metamodel(s) - is it one is it two ....
it is hard to answer such a question. so two reproducable grammars would help.
never the less have a look how global scopeproviders are used:

org.eclipse.xtext.scoping.impl.AbstractGlobalScopeDelegatingScopeProvider.getGlobalScope(Resource, EReference, Predicate<IEObjectDescription>)

maybe you should delegate here to one of both global scopeproviders
depending of the type of the ereference.

(or you combine both and take care that stuff from the imported files is not in the index)

~Christian


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

[Updated on: Thu, 22 December 2011 14:19]

Report message to a moderator

Re: Combining importURI and importedNamespace [message #769681 is a reply to message #769671] Thu, 22 December 2011 14:27 Go to previous messageGo to next message
Torsten Schiefer is currently offline Torsten SchieferFriend
Messages: 26
Registered: December 2011
Junior Member
Thank you,

this is the Beginning of my Skript
Skript :
  imports+=UsingImport* includes+=Include* annotations+=Annotation* (namespace=Namespace | anonymousNamespace=AnonymousNamespace);

// Imports other namespaces
UsingImport : 'using' importedNamespace=QualifiedNameWithWildCard ';';

// Namespace identifier with wild card
QualifiedNameWithWildCard: QualifiedName '.*'?;

// Includes whole Files defined in importURI
Include : '#include' importURI=STRING;

// An unnamed namespace: defines functions for a related namespace
AnonymousNamespace : {AnonymousNamespace}
	'namespace'
	'{' (functionDeclarations+=FunctionDeclaration | textDefinitions+=TextDefinition)* '}';

// A namespace or extension definition
// Has boolean attribute isExtension
Namespace:
	('namespace'|extension?='extension') 
	name = QualifiedName
	'{' (functionDeclarations+=FunctionDeclaration | textDefinitions+=TextDefinition)* '}'
;

FunctionDeclaration :
  returnType=[Namespace|QualifiedName] name=ShortIdentifier
  '(' (paramDefs+=VariableDeclaration (',' paramDefs+=VariableDeclaration)*)? ')'
  statement=Statement;

Statement :
...    (varDef=VariableDeclaration (assignmentWithDeclaration?='=' expression=Expression)? varDecl?=';') ...;

Expression:
... functionName=[FunctionDeclaration|QualifiedName] '(' (expressions+=Expression (',' expressions+=Expression)*)? ')' ... ;


The Reference to FunctionDeclaration can be a imported function referenced by a QualifiedName or a local / included function referenced by a ID.
So unfortunately the type of the Reference is the same.
Re: Combining importURI and importedNamespace [message #769691 is a reply to message #769681] Thu, 22 December 2011 14:39 Go to previous messageGo to next message
Torsten Schiefer is currently offline Torsten SchieferFriend
Messages: 26
Registered: December 2011
Junior Member
How can I combine two globalScopeProviders? And which is the one for Importing Namespaces?
Re: Combining importURI and importedNamespace [message #769692 is a reply to message #769691] Thu, 22 December 2011 14:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi,

this is one grammar where is the second one?

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Combining importURI and importedNamespace [message #769693 is a reply to message #769692] Thu, 22 December 2011 14:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
P.s: use DefaultGlobalScopeProvider for "namespace" behaviour

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Combining importURI and importedNamespace [message #769697 is a reply to message #769693] Thu, 22 December 2011 14:51 Go to previous messageGo to next message
Torsten Schiefer is currently offline Torsten SchieferFriend
Messages: 26
Registered: December 2011
Junior Member
Two grammars? What do you mean? I only have one for my Editor.
Re: Combining importURI and importedNamespace [message #769701 is a reply to message #769697] Thu, 22 December 2011 14:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi,

this is an important fact you didnt mention before.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Combining importURI and importedNamespace [message #769708 is a reply to message #769701] Thu, 22 December 2011 15:10 Go to previous messageGo to next message
Torsten Schiefer is currently offline Torsten SchieferFriend
Messages: 26
Registered: December 2011
Junior Member
Oh, I'm sorry.

Ok now I've tried the following:
public class OSImportUriGlobalScopeProvider extends ImportUriGlobalScopeProvider {
    @Inject
    DefaultGlobalScopeProvider provider;
    //Is bound in the RuntimeModule

    @Override
    public IScope getScope(final Resource resource, final EReference reference,
        final Predicate < IEObjectDescription > filter) {
        final IScope scope = this.provider.getScope(resource, reference, filter);
        return getScope(scope, resource, isIgnoreCase(reference), reference.getEReferenceType(), filter);
    }

    protected IScope getScope(final IScope parent, final Resource resource, final boolean ignoreCase,
        final EClass type, final Predicate < IEObjectDescription > filter) {
        final LinkedHashSet < URI > uniqueImportURIs = getImportedUris(resource);
        final IResourceDescriptions descriptions = getResourceDescriptions(resource, uniqueImportURIs);
        final List < URI > urisAsList = Lists.newArrayList(uniqueImportURIs);
        Collections.reverse(urisAsList);
        IScope scope = parent;
        for (final URI uri : urisAsList) {
            scope = createLazyResourceScope(scope, uri, descriptions, type, filter, ignoreCase);
        }
        return scope;
    }
}

and it works.
But the scope contains all objects of the anonymousNamespaces.
So it's the same result like I would get without using the ImportUriGlobalScope.
Re: Combining importURI and importedNamespace [message #769717 is a reply to message #769708] Thu, 22 December 2011 15:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Hi,

if you store the anon stuff to the index it is in the index.
so this is what could work
(quick and dirty try with your grammar)

public class StrangeResourceDescriptionStrategy extends
		DefaultResourceDescriptionStrategy {
	
	private final static Logger LOG = Logger.getLogger(StrangeResourceDescriptionStrategy.class);
	
	@Override
	public boolean createEObjectDescriptions(EObject eObject, IAcceptor<IEObjectDescription> acceptor) {
		if (getQualifiedNameProvider() == null)
			return false;
		try {
			QualifiedName qualifiedName = getQualifiedNameProvider().getFullyQualifiedName(eObject);
			if (qualifiedName != null) {
				Map<String, String> userData = new HashMap<String, String>();
				if (EcoreUtil2.getContainerOfType(eObject, AnonymousNamespace.class)!=null) {
					userData.put("anon", Boolean.TRUE.toString());
				}
				IEObjectDescription create = EObjectDescription.create(qualifiedName, eObject, userData);
				acceptor.accept(create);
			}
		} catch (Exception exc) {
			LOG.error(exc.getMessage());
		}
		return true;
	}

}


public class OSImportUriGlobalScopeProvider extends ImportUriGlobalScopeProvider {
    @Inject
    DefaultGlobalScopeProvider provider;
    //Is bound in the RuntimeModule

    @Override
    public IScope getScope(final Resource resource, final EReference reference,
        final Predicate < IEObjectDescription > filter) {
    	Predicate<IEObjectDescription> filter2 =new Predicate<IEObjectDescription>() {

			@Override
			public boolean apply(IEObjectDescription d) {
				return ! Boolean.TRUE.toString().equals(d.getUserData("anon"));
			}
        	
        };
    	
        final IScope scope = this.provider.getScope(resource, reference, filter != null ? Predicates.and(filter, filter2) : filter2);
        return getScope(scope, resource, isIgnoreCase(reference), reference.getEReferenceType(), filter);
    }

    protected IScope getScope(final IScope parent, final Resource resource, final boolean ignoreCase,
        final EClass type, final Predicate < IEObjectDescription > filter) {
        final LinkedHashSet < URI > uniqueImportURIs = getImportedUris(resource);
        final IResourceDescriptions descriptions = getResourceDescriptions(resource, uniqueImportURIs);
        final List < URI > urisAsList = Lists.newArrayList(uniqueImportURIs);
        Collections.reverse(urisAsList);
        IScope scope = parent;
        for (final URI uri : urisAsList) {
            scope = createLazyResourceScope(scope, uri, descriptions, type, filter, ignoreCase);
        }
        return scope;
    }
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Combining importURI and importedNamespace [message #769730 is a reply to message #769717] Thu, 22 December 2011 15:49 Go to previous message
Torsten Schiefer is currently offline Torsten SchieferFriend
Messages: 26
Registered: December 2011
Junior Member
It works like magic!
Thanks a million, Christian! Smile
Previous Topic:Type Inference with XBase
Next Topic:Debugging model post processing
Goto Forum:
  


Current Time: Tue Apr 16 11:22:08 GMT 2024

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

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

Back to the top