Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Scoping across multiple resources(Scope, package import, multiple files)
Scoping across multiple resources [message #885102] Tue, 12 June 2012 13:23 Go to next message
Michael A. is currently offline Michael A.Friend
Messages: 8
Registered: June 2012
Junior Member
Hello

I'm a bit stuck trying to enable my editor to function properly with cross references to other files.
Although there are a number of hints out there in several blogs and articles I still couldn't puzzle together a working solution. I got the point, that there is an index that keeps track of the exported names in the accessible resources which are not necessarily present in memory. In order to maintain semantic integrity I want my editor to filter the available cross reference candidates from other resources, which is fairly simple in a single file.

Let me give an example for the single-file solution.

A simple grammar:
grammar test.scoping.ScopingTest3DSL with org.eclipse.xtext.common.Terminals

generate scopingTest3DSL "ht tp:// w ww.scoping.test/ScopingTest3DSL"

ScopingTestPackageRule returns ScopingTestPackage:
	'package' name=ID ':'
	imports+=ImportRule*
	statements+=StatementRule*;
	
FQN: ID('.'ID)*;
FQNWC: FQN '.*' ;

ImportRule returns Import:
	'import' importedNamespace=FQNWC
;
	
StatementRule returns MyStatement:
	(DeclarationRule | ReferenceRule) ';'	
;

enum EColor : red | blue;

DeclarationRule returns MyDeclaration:
	'declare' name=ID 'is' color=EColor
;

ReferenceRule returns MyReference:
	'referTo' color=EColor declarationRef=[MyDeclaration|FQN]
;


And my scope provider that enforces semantic integrity:
public class ScopingTest3DSLScopeProvider extends AbstractDeclarativeScopeProvider {

	public IScope scope_MyReference_declarationRef(MyReference ctx, EReference ref) { 
		
		System.err.println("scope_MyReference_declarationRef(" + ctx + ")");
		
		List<MyDeclaration> resultList = new ArrayList<MyDeclaration>();
		
		if (ctx.getColor() != null) {
			
			ScopingTestPackage container = EcoreUtil2.getContainerOfType(ctx, ScopingTestPackage.class);
			
			List<MyStatement> statements = EcoreUtil2.getAllContentsOfType(container, MyStatement.class);
			
			int thisPos = statements.indexOf(ctx);
			for(int i = 0; i < thisPos; i++)
				if (statements.get(i) instanceof MyDeclaration) {
					
					MyDeclaration declaration = MyDeclaration.class.cast(statements.get(i));
					if ( ctx.getColor().equals(declaration.getColor()) )
						resultList.add( declaration );
				} //for statements
					
		} //color!= null
		
		return Scopes.scopeFor(resultList);		
	}

}


I'm using this scope provider to enforce that a reference statement may refer only to declarations of the given color plus the declaration-statement has to appear before the reference-statement. Same thing as with declaring and accessing typed variables in a programming language.

My goal is to include declarations from other files via a package-import while preserving my color-check.

Bottom line to me it seems to be a qiet common task. Hints and advises?

What's the best approach to implementing that?
Is there a well documented example I've missed so far?




Re: Scoping across multiple resources [message #885128 is a reply to message #885102] Tue, 12 June 2012 13:56 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Have not read your code in detail - but a tip is that it is possible to
associate data with exported objects (i.e. make it available in the
index) - this way you do not have to instantiate other resources to find
the information you need. I.e. if you can not encode what you want in
the exported name, you can do it with extra data.

I do a lot of processing based on the index in cloudsmith / geppetto @
github as I do not use the default scoping/linking. In particular as I
do a lot of searching in the index, I start by creating a quick
lookup-map based on last part of name (more than 10x speed improvement
in my case).

Don't know if that helps you in any way...
Regards
- henrik

On 2012-12-06 15:23, Michael A. wrote:
> Hello
>
> I'm a bit stuck trying to enable my editor to function properly with
> cross references to other files.
> Although there are a number of hints out there in several blogs and
> articles I still couldn't puzzle together a working solution. I got the
> point, that there is an index that keeps track of the exported names in
> the accessible resources which are not necessarily present in memory. In
> order to maintain semantic integrity I want my editor to filter the
> available cross reference candidates from other resources, which is
> fairly simple in a single file.
>
> Let me give an example for the single-file solution.
> A simple grammar:
>
> grammar test.scoping.ScopingTest3DSL with
> org.eclipse.xtext.common.Terminals
>
> generate scopingTest3DSL "ht tp:// w ww.scoping.test/ScopingTest3DSL"
>
> ScopingTestPackageRule returns ScopingTestPackage:
> 'package' name=ID ':'
> imports+=ImportRule*
> statements+=StatementRule*;
>
> FQN: ID('.'ID)*;
> FQNWC: FQN '.*' ;
>
> ImportRule returns Import:
> 'import' importedNamespace=FQNWC
> ;
>
> StatementRule returns MyStatement:
> (DeclarationRule | ReferenceRule) ';'
> ;
>
> enum EColor : red | blue;
>
> DeclarationRule returns MyDeclaration:
> 'declare' name=ID 'is' color=EColor
> ;
>
> ReferenceRule returns MyReference:
> 'referTo' color=EColor declarationRef=[MyDeclaration|FQN]
> ;
>
>
> And my scope provider that enforces semantic integrity:
>
> public class ScopingTest3DSLScopeProvider extends
> AbstractDeclarativeScopeProvider {
>
> public IScope scope_MyReference_declarationRef(MyReference ctx,
> EReference ref) {
> System.err.println("scope_MyReference_declarationRef(" + ctx +
> ")");
>
> List<MyDeclaration> resultList = new ArrayList<MyDeclaration>();
>
> if (ctx.getColor() != null) {
>
> ScopingTestPackage container =
> EcoreUtil2.getContainerOfType(ctx, ScopingTestPackage.class);
>
> List<MyStatement> statements =
> EcoreUtil2.getAllContentsOfType(container, MyStatement.class);
>
> int thisPos = statements.indexOf(ctx);
> for(int i = 0; i < thisPos; i++)
> if (statements.get(i) instanceof MyDeclaration) {
>
> MyDeclaration declaration =
> MyDeclaration.class.cast(statements.get(i));
> if ( ctx.getColor().equals(declaration.getColor()) )
> resultList.add( declaration );
> } //for statements
>
> } //color!= null
>
> return Scopes.scopeFor(resultList);
> }
>
> }
>
>
> I'm using this scope provider to enforce that a reference statement may
> refer only to declarations of the given color plus the
> declaration-statement has to appear before the reference-statement. Same
> thing as with declaring and accessing typed variables in a programming
> language.
>
> My goal is to include declarations from other files via a package-import
> while preserving my color-check.
> Bottom line to me it seems to be a qiet common task. Hints and advises?
>
> What's the best approach to implementing that?
> Is there a well documented example I've missed so far?
>
>
>
>
>
Previous Topic:[xtext] importUri is default when generating xtext project from existing ecore metamodels
Next Topic:Predicate or Left-factoring with Ecore objects
Goto Forum:
  


Current Time: Sat Oct 24 17:00:11 GMT 2020

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

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

Back to the top