Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Migrating Name Provider Function to Xtext 2.0(Scoping)
Migrating Name Provider Function to Xtext 2.0 [message #683480] Mon, 13 June 2011 20:37 Go to next message
Rafael Angarita is currently offline Rafael AngaritaFriend
Messages: 94
Registered: November 2010
Member
Hello,

I have this method that works very well for Xtext 1.0:

 private IScope getColumnsQualifiedName(EList<Column> list) {

                com.google.common.base.Function<Column, String> nameProvider = new com.google.common.base.Function<Column, String>() {
                        @Override
                        public String apply(Column from) {
                                CompositeNode cNode = NodeUtil.getNode(from);
                                EList<AbstractNode> anList = cNode.getChildren();
                                if (anList.size() > 1){
                                        AbstractNode aNode = anList.get(1);
                                                LeafNode lNode = (LeafNode) aNode;
                                                        return lNode.getText();
                                }
                                return null;
                        }
                };
                return Scopes.scopeFor(list, nameProvider, IScope.NULLSCOPE);
        }



I use it to make columns of my table referencable inside the current table (i.e: as table indexes).
Those columns doesn't have a qualified name because they are cross-references to other type of object of my language.

Now, I'm migrating to Xtext 2.0 and I can't find (debugging) the value I should return here: "return lNode.getText()".

I'm following the migration guide, so I node I have to use the org.eclipse.xtext.nodemodel interfaces. Here is what I have:

private IScope getColumnsQualifiedName(Iterable<Column> list) {

		com.google.common.base.Function<Column, QualifiedName> nameProvider = new com.google.common.base.Function<Column, QualifiedName>() {
			@Override
			public QualifiedName apply(Column from) {
				
				ICompositeNode cNode = NodeModelUtils.getNode(from);
				BidiIterable<INode> anList = cNode.getChildren();
	
				
				 Iterator<INode> i = anList.iterator();
				 if(i.hasNext()){
				      INode node = i.next();
				   
				      ILeafNode lNode = (ILeafNode)node;
				      
				      QualifiedName qn = QualifiedName.create(lNode.getText());
				      return qn;
					 
				 }
				return null;
			}
		};
		return Scopes.scopeFor(list, nameProvider, IScope.NULLSCOPE);
		//return null;
	}


, but the QualifiedName qn is always "null". Sincerely, I don't understand much about how this tree model is supposed to be created, so my way of discovering where was the "lNode.getText()" i had to return was debugging, but this time, it seems I can't find it!


Maybe I missing some methods or utilities I should use.

How can I traverse correcty this tree?

Here is my table grammar:

TableDefinition:
	table=Table;
	
Table:
	'Name:' name=ID
	table=TableStructure
;

TableStructure:
	'Columns:' '{' columns+=Column (',' columns+=Column)* '}'
	'Indexes:' '{' indexes+=Index+ '}'
	('References:' '{' 
		references+=Reference (',' references+=Reference)* '}')?
;

Reference:
	
	reference=[Table] ('as' name=ID '{' keymaps+=KeyMap (',' keymaps+=KeyMap)*'}')?
;

KeyMap:
	fromAttribute=[Column] toAttribute=[attributeAG::Attribute]
;

Column:
	attribute=[attributeAG::Attribute]  (nullableColumn=NullableColumn)?
;

NullableColumn:
	('null' | 'not null')
;

Index: {Index}
	'Index:'   indexName=IndexName (indextype = 'PrimaryKey' | indextype = 'Duplicate' | indextype = 'Unique')?
	'Columns:'  '{' indexcolumns+=IndexColumn (',' indexcolumns+=IndexColumn)* '}'
;

IndexName:
	ID
;

IndexColumn:
	indexColumn=[Column] (order=('Ascending'|'Descending'))?
;


Thank you very much for your help and great work!
Re: Migrating Name Provider Function to Xtext 2.0 [message #683489 is a reply to message #683480] Mon, 13 June 2011 21:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

your code works for me. i am using the latest version of Xtext 2.0.0 from the nightly builds.
of course you should check for non whitespace stuff when searching the leaf nodes.
(isHidden != true)

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Migrating Name Provider Function to Xtext 2.0 [message #683508 is a reply to message #683489] Mon, 13 June 2011 22:17 Go to previous messageGo to next message
Rafael Angarita is currently offline Rafael AngaritaFriend
Messages: 94
Registered: November 2010
Member
Thank you Christian!

I did the check for non whitespace stuff. If the LeafNode is hidden, I get its next INode sibling, cast it to ILeafNode and them, the getText() method returns what I want.

I don't know if it is a general solution, but it seems to be working fine.

Thank you very much!
Re: Migrating Name Provider Function to Xtext 2.0 [message #683647 is a reply to message #683508] Tue, 14 June 2011 07:10 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Rafael,

you should have use NodeModelUtils#findNodesForFeature instead of
traversing the model on your own.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 14.06.11 00:17, schrieb Rafael Angarita:
> Thank you Christian!
>
> I did the check for non whitespace stuff. If the LeafNode is hidden, I
> get its next INode sibling, cast it to ILeafNode and them, the getText()
> method returns what I want.
>
> I don't know if it is a general solution, but it seems to be working fine.
>
> Thank you very much!
Re: Migrating Name Provider Function to Xtext 2.0 [message #683903 is a reply to message #683647] Tue, 14 June 2011 16:13 Go to previous message
Rafael Angarita is currently offline Rafael AngaritaFriend
Messages: 94
Registered: November 2010
Member
Thank you Sebastian! I'll check that! Smile
Previous Topic:How to parser 2 file extension with one grammar
Next Topic:[Xtend2] anonymous inner classes
Goto Forum:
  


Current Time: Fri Apr 19 06:40:16 GMT 2024

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

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

Back to the top