Xtext语法错误"cannot find type for [State]"即使有这样的类型也会抛出

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

我有这个语法:

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' )* ;

在 Transition 规则中,当我尝试使用 ref to State 类型错误时,总是会抛出 "cannot find type for [State]"。当我在没有 [] 的情况下使用它时,所以不作为交叉引用一切正常。我该如何解决这种情况?这个语法有什么问题吗?

错误在这一行:

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

在 Xtext 中 [Foo] 表示 "a crossreference to an instance of type Foo"。是并不意味着 "a reference to a grammar rule"。由于这一行,Xtext 不会生成 State 类型:

State returns Declaration:

其中 returns Declaration 表示 "The rule State will return a type Declaration",因此不需要类型 State

以下语法将修复它:

StateMachine:
    declarations+=Declaration*;

Declaration:
    Transition |
    State;

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

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

此处 Xtext 将为 DeclarationTransitionState 生成类型,其中 TransitionState 派生自 Declaration