Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » merge scope blocks?
merge scope blocks? [message #767218] Sat, 17 December 2011 10:47 Go to next message
Phil R is currently offline Phil RFriend
Messages: 99
Registered: September 2011
Member
Hi,

I have searched around for merging 2 scope blocks but I couldn't find any solution.
for example:
namespace Root {
...
}

<othernames>
// continue Root:
namespace Root {
..
}


EDIT: SOLVED

-Phil

[Updated on: Sun, 19 February 2012 14:24]

Report message to a moderator

Re: merge scope blocks? [message #767254 is a reply to message #767218] Sat, 17 December 2011 12:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

why do you do such customizations? shouldnt that work out of the box? (i mean with import namespace based scoping)

~Christian


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

[Updated on: Sat, 17 December 2011 12:59]

Report message to a moderator

Re: merge scope blocks? [message #767257 is a reply to message #767254] Sat, 17 December 2011 13:06 Go to previous messageGo to next message
Phil R is currently offline Phil RFriend
Messages: 99
Registered: September 2011
Member
Yes, because both the URI based scoping and the namespace based scoping is needed. Within the URI scope, the elements defined have a limited scope if namespaces are used. That works, but I'm not sure how to merge namespaces with the same qualified name

Regards,
Phil
Re: merge scope blocks? [message #767584 is a reply to message #767257] Sun, 18 December 2011 12:10 Go to previous messageGo to next message
Phil R is currently offline Phil RFriend
Messages: 99
Registered: September 2011
Member
looks like it works indeed out of the box if I bind the scopeproviders correct:

public Class<? extends IQualifiedNameConverter> bindIQualifiedNameConverter() {
		return QualifiedNameConverter.class;
	}
	
	@Override
	public void configureIScopeProviderDelegate(Binder binder) {		
		binder.bind(org.eclipse.xtext.scoping.IScopeProvider.class)
		.annotatedWith(com.google.inject.name.Names.named(org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider.NAMED_DELEGATE))
		.to(FOImportedNamespaceAwareLocalScopeProvider.class);
	}
        
	@Override
	public Class<? extends IGlobalScopeProvider> bindIGlobalScopeProvider() {		
		return ImportUriGlobalScopeProvider.class;
	}

mwe:
// scoping and exporting API
            //fragment = scoping.ImportURIScopingFragment {}
            // fragment = exporting.SimpleNamesFragment {}

            // scoping and exporting API
            fragment = scoping.ImportNamespacesScopingFragment {}

I first didn't see that if I would uncomment the uri fragment, the binding will be overriden by the namespace fragment.
Thereby manually overriding the binding did the trick

Also a namespace with the same qualified name gets its scope merged if the namespace is in the same file, but
when the namespace is extended in an other file, elements in the extended file are not found.

like:
file A:
namespace Root {
  namespace A {
    elements of A...
  }

file B:
namespace Root {
  namespace B {
    using namespace A // not working, it works if Root::A is written, it works with just QFN "A" if this code is in the same file as namespace A
    elements of B using elements of A
  }

Is there an easy solution for this?

Regards,
Phil
Re: merge scope blocks? [message #767592 is a reply to message #767584] Sun, 18 December 2011 12:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i still do not understand what is the behaviour you want to have? that is your expected behaviour when you mix uri and namespace based scoping?
or do you want to mix it at all? if youd share a complete reproducable grammar that would be a lot easier to understand.

id i understand you correct you want to have imported namespace based scoping but only imported stuff visible from global scoping.
but in your sample models you do not any imports of uris

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: merge scope blocks? [message #767595 is a reply to message #767592] Sun, 18 December 2011 12:45 Go to previous messageGo to next message
Phil R is currently offline Phil RFriend
Messages: 99
Registered: September 2011
Member
sorry for the confusing, I forgot to add in the example that in fileB above a import is declared:

#include "fileA"

namespace Root {
  namespace B {
    using namespace A // not working, it works if Root::A is written, it works with just QFN "A" if this code is in the same file as namespace A
    elements of B using elements of A
  }


the scoping should behave like the current files + all included files as the global scope and the namespaces make it easier to manage all elements defined
I hope this clarifies it better Smile

edit: the grammar is getting bigger and bigger, I hope you don't mind I'll place the definition of the namespaces:
Root:
	importedFiles+=IncludeFile*
	contents+=Idp*
;

Idp:  	
	Using	 
	| Namespace 
	| Vocabulary 
	| Theory
	| Instruction				
;

IncludeFile:
	'#include' importURI=STRING 
;

Namespace: 
	'namespace' name=ID 
	'{' contents+=Idp* '}'
;

Using: 
	'using' (UsingVocabulary|UsingNamespace)
;

UsingVocabulary:
	'vocabulary' importedNamespace=FQN
;

UsingNamespace:
	'namespace' importedNamespace=FQN
;

(Vocabulary, Theory and Instruction are "elements" in the namespaces)

[Updated on: Sun, 18 December 2011 12:49]

Report message to a moderator

Re: merge scope blocks? [message #767605 is a reply to message #767595] Sun, 18 December 2011 13:26 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

still do not understand you you need this import uri stuff at all

Model:
	includes+=IncludeFile*
	elements+=Namespace*;
	
IncludeFile:
	'#include' importedNamespace=FQN
;	
	
AbstactElement:
	Element | Namespace | Import
;

Import:
	"using" importedNamespace=FQN
;

Namespace:
	"namespace" name=ID "{"
		elements+=AbstactElement*
	"}"
;

Element:
	"element" name=ID ("uses" uses=[Element|FQN])?
;

FQN: ID ("." ID)*;


public class MyDslQualifiedNameProvider extends
		DefaultDeclarativeQualifiedNameProvider {
	
	QualifiedName qualifiedName(Model m) {
		String name = m.eResource().getURI().trimFileExtension().lastSegment();
		return QualifiedName.create(name);
	}

}


public class MyDslImportedNamespaceAwareLocalScopeProvider extends
		ImportedNamespaceAwareLocalScopeProvider {
	
	protected List<ImportNormalizer> internalGetImportedNamespaceResolvers(final EObject context, boolean ignoreCase) {
		List<ImportNormalizer> importedNamespaceResolvers = Lists.newArrayList();
		SimpleAttributeResolver<EObject, String> importResolver = SimpleAttributeResolver.newResolver(String.class,
				"importedNamespace");
		EList<EObject> eContents = context.eContents();
		for (EObject child : eContents) {
			String value = importResolver.getValue(child);
			ImportNormalizer resolver = createImportedNamespaceResolver(value+".*", ignoreCase);
			if (resolver != null)
				importedNamespaceResolvers.add(resolver);
			
		}
		if (context instanceof Namespace) {
			ImportNormalizer resolver2 = createImportedNamespaceResolver(((Namespace)context).getName()+".*", ignoreCase);
			if (resolver2 != null) {
				importedNamespaceResolvers.add(resolver2);
			}
		}
		return importedNamespaceResolvers;
	}

}


public class MyDslRuntimeModule extends
		org.xtext.example.mydsl.AbstractMyDslRuntimeModule {

	@Override
	public Class<? extends IQualifiedNameProvider> bindIQualifiedNameProvider() {
		return MyDslQualifiedNameProvider.class;
	}

	public void configureIScopeProviderDelegate(com.google.inject.Binder binder) {
		binder.bind(IScopeProvider.class)
				.annotatedWith(
						Names.named(AbstractDeclarativeScopeProvider.NAMED_DELEGATE))
				.to(MyDslImportedNamespaceAwareLocalScopeProvider.class);
	}

}


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: merge scope blocks? [message #767627 is a reply to message #767605] Sun, 18 December 2011 14:20 Go to previous messageGo to next message
Phil R is currently offline Phil RFriend
Messages: 99
Registered: September 2011
Member
I tried to work it out but it has bugs. If a file will also be a sort of namespace, then #include "sample_file.extension" is not possible anymore, because of the FQN. Changing it to STRING would allow it, but the namespace resolver uses "." as delimiter, so I'm worried if that will not cause trouble for the dot between the file and the extension?
Re: merge scope blocks? [message #767630 is a reply to message #767627] Sun, 18 December 2011 14:27 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i just added the semantics of your example model and trimmed the file extension. it was EXTRA work to do so.
i dont think it will cause trouble.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: merge scope blocks? [message #767653 is a reply to message #767630] Sun, 18 December 2011 15:46 Go to previous message
Phil R is currently offline Phil RFriend
Messages: 99
Registered: September 2011
Member
Hi Christian,

I had troubles with my custom delimiters and my grammar has dot syntax to end lines which made it difficult for me to get it all working. But finally it works. Your example works great! Thank you.

Regards,
Phil
Previous Topic:Ensure entries in ParserRule are comma separated
Next Topic:Decision can match input such as "RULE_ID '=' RULE_ID"
Goto Forum:
  


Current Time: Thu Apr 25 07:37:27 GMT 2024

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

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

Back to the top