Search code examples
eclipsextextresolve

Xtext - Couldn't resolve reference to Object


I'm new to Xtext and having a problem with a simple Xtext program:

my grammar looks like this:

grammar org.xtext.example.domainmodel.Domainmodel with org.eclipse.xtext.common.Terminals

generate domainmodel "http://www.xtext.org/example/domainmodel/Domainmodel"

Library:
    (books+=Book)*;

Book:
    'Book' 
    isbn=ID 
    title=STRING 
    (subtitle=STRING)? 
    pages=INT 
    ('sequelof' sequelof=[Book])? 
    ('hardcover'|'softcover');

and my script looks like this

Book J123 "LotR1" "The Fellowship" 608 hardcover
Book J124 "LotR2" "Two Towers" 510 sequelof J123 hardcover

but the "J123" in the second line is red underlined and it says "Couldn't resolve reference to Book 'J123'"

Everthing else works fine, like Content Assist (Strg+Space)

Maybe you can help me :)


Solution

  • By default, references work on name properties. I.e. you need to change your grammar to the following:

    grammar org.xtext.example.domainmodel.Domainmodel with 
    org.eclipse.xtext.common.Terminals
    
    generate domainmodel "http://www.xtext.org/example/domainmodel/Domainmodel"
    
    Library:
      (books+=Book)*;
    
    Book:
      'Book' 
      name=ID 
      title=STRING 
      (subtitle=STRING)? 
      pages=INT 
      ('sequelof' sequelof=[Book])? 
      ('hardcover'|'softcover');
    

    If you don't want to do that, you can implement the IQualifiedName provider so that it computes the name using the isbn property.