Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Couldn't resolve reference to Field 'x'(xText Custom Scoping)
Couldn't resolve reference to Field 'x' [message #1811345] Sat, 31 August 2019 12:37 Go to next message
Eclipse UserFriend
Here is my 'Types' xText grammar:
grammar sample.types.Types with org.eclipse.xtext.common.Terminals
    
generate types "http://www.types.sample/Types"
    
Model:
 	structs     += Struct*
  	data        += Data*
  	assignments += Assignment*
;
Struct:
  	'struct' name=ID '{'
 		fields += Field*
  	'}'
;
Field:
 	type=Type name=ID
;
Type:
  	  'number'
  	| 'string'
;
Data:
  	type=[Struct|ID] name=ID
;
Assignment:
 	qname=QName '=' value=Value
;
QName:
 	data=[Data|ID] '.' path=[Field|ID]
;
Value:
	  INT
 	| STRING
;


Here is an instance of the 'Types' grammar:

struct Sample {
   number n
   string s
}
Sample sample
sample.n = 12
sample.s = "Hello"


The two last lines, which reference fields 'n' and 's', generates the error:

Quote:
Couldn't resolve reference to Field 'x'.'


I've coded the following custom scope provider without success:
class TypesScopeProvider extends AbstractTypesScopeProvider {

   override getScope( EObject context, EReference reference ) {
      if( reference === TypesPackage.Literals.QNAME__PATH ) {
         val model = EcoreUtil2.getContainerOfType(context, Model)
         if( model !== null ) {
            val result = newArrayList
            for( data : model.data ) {
               result.add(
                  EObjectDescription.create(
                     QualifiedName.create( data.name ),
                     data ))
               for( field : data.type.fields ) {
                  result.add(
                     EObjectDescription.create(
                        QualifiedName.create( data.name, field.name ),
                        field ))
               }
            }
            return new SimpleScope(IScope.NULLSCOPE, result)
         }
      }
      super.getScope( context, reference )
   }
}


How can I improve TypesScopeProvider to resolve 'n' and 's' as field of stuct 'Sample'?
Re: Couldn't resolve reference to Field 'x' [message #1811346 is a reply to message #1811345] Sat, 31 August 2019 12:44 Go to previous messageGo to next message
Eclipse UserFriend
Neither your scope provider nor your grammar make sense in that combination

- if you want to have two references you need to give the field references a simple name, not a qualified name (yurt use fields name, not combine it with data name
-
Re: Couldn't resolve reference to Field 'x' [message #1811347 is a reply to message #1811346] Sat, 31 August 2019 12:45 Go to previous messageGo to next message
Eclipse UserFriend
P.s.it is Xtext not xText
Re: Couldn't resolve reference to Field 'x' [message #1811348 is a reply to message #1811347] Sat, 31 August 2019 13:13 Go to previous messageGo to next message
Eclipse UserFriend
Do you know the difference between a type and an instance, sir?
Who are you, to be so rude?
A king? a god?
Re: Couldn't resolve reference to Field 'x' [message #1811579 is a reply to message #1811348] Sat, 31 August 2019 14:41 Go to previous messageGo to next message
Eclipse UserFriend
if you took my answer rude i am sorry.

let me rephrase my answer

in your grammar you have

QName:
data=[Data|ID] '.' path=[Field|ID]
;

and have sample.n

the you scope sample and n separately cause you have two references


if you scope path=[Field|ID]

you are just scoping the n so have to put the field n to the scope with the name n

=> QualifiedName.create(field.name);

alternatively you could have adapted the grammar to your scoping

QName:
dataAndPath=[Field|FQN]
;

with

FQN: ID "." ID;

[Updated on: Sun, 01 September 2019 03:55] by Moderator

Re: Couldn't resolve reference to Field 'x' [message #1814063 is a reply to message #1811579] Mon, 02 September 2019 12:04 Go to previous message
Eclipse UserFriend
Here is the solution, thanks to Christian Dietrich on StackOverflow.

class TypesScopeProvider extends AbstractTypesScopeProvider {

   override getScope( EObject context, EReference reference ) {
      if( reference === TypesPackage.Literals.QNAME__PATH ) {
         if( context instanceof QName ) {
            val result = newArrayList
            for( field : context.data.type.fields ) {
               result.add( EObjectDescription.create( QualifiedName.create( field.name ), field ))
            }
            System.err.println(result)
            return new SimpleScope(IScope.NULLSCOPE, result)
         }
      }
      super.getScope(context, reference)
   }
}
Previous Topic:Rule has non-LL(*) decision even tho its copied from a lecture
Next Topic:Reference an object from parent model
Goto Forum:
  


Current Time: Wed May 14 19:50:48 EDT 2025

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

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

Back to the top