即使存在这样的类型,也会抛出Xtext语法错误“找不到[状态]的类型”

时间:2019-01-26 10:40:23

标签: dsl xtext

我有这个语法:

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

在“转换规则”中,当我尝试使用ref表示状态类型错误时,始终抛出“找不到[State]的类型”。当我不带[]使用它时,则不能作为交叉引用,但一切正常。我该如何解决这种情况?该语法可能有什么问题?

1 个答案:

答案 0 :(得分:1)

此行中的错误:

def check_availability(meetings, proposed_time):
    # Loop over the list of meetings; "meeting"
    # is the meeting that you are currently inspecting.
    for meeting in meetings:
        # if proposed_time is between meeting.start_time
        # and meeting.end_time, return False
    # If you get through the list without returning False
    # then the proposed time must be ok, so return True.

在Xtext "trans" label=ID ":" source=[State] "->" target=[State] ";" ; 中表示“对类型为[Foo]的实例的交叉引用”。 Is并不表示“对语法规则的引用”。由于以下这一行,Xtext不会生成Foo类型:

State

其中State returns Declaration: 的意思是“规则returns Declaration将返回类型State”,因此不需要类型Declaration

以下语法将对其进行修复:

State

此处Xtext将为StateMachine: declarations+=Declaration*; Declaration: Transition | State; Transition: "trans" label=ID ":" source=[State] "->" target=[State] ";" ; State: "state" id=ID ";" ; DeclarationTransition生成类型,其中StateTransitionState派生。