scanyboss
scanyboss

Reputation: 45

Xtext grammar error "cannot find type for [State]" is thrown even if there is such a type

I have this grammar:

StateMachine:
    declarations+=Declaration*;

Declaration:
    Transition |
    State;

Transition returns Declaration:
     "trans" label=ID ":" source=[State] "->" target=[State] ";" ;

State returns Declaration:
     "state" id=ID ";" ;

@Override
terminal WS: 
    (' ' | '\t' | '\n' | '\r')+;

@Override
terminal ID: 
    ( 'a' .. 'z'  |  'A' .. 'Z' ) ( 'a' .. 'z'  |  'A' .. 'Z'  |  '0' .. '9' )* ;

In Transition rule when I'm trying to use ref to State type error "cannot find type for [State]" is always thrown. When I use it without [], so not as a cross reference everything works fine. How could I solve this situation ? What could be wrong with this grammar ?

Upvotes: 0

Views: 87

Answers (1)

A.H.
A.H.

Reputation: 66283

The error is in this line:

"trans" label=ID ":" source=[State] "->" target=[State] ";" ;

In Xtext [Foo] means "a crossreference to an instance of type Foo". Is does NOT mean "a reference to a grammar rule". Xtext does NOT generate a State type because of this line:

State returns Declaration:

where returns Declaration means "The rule State will return a type Declaration" and hence no type State is necessary.

The following grammar will fix it:

StateMachine:
    declarations+=Declaration*;

Declaration:
    Transition |
    State;

Transition:
     "trans" label=ID ":" source=[State] "->" target=[State] ";" ;

State:
     "state" id=ID ";" ;

Here Xtext will generate types for Declaration, Transition and State where Transition and State derive from Declaration.

Upvotes: 1

Related Questions