XText Validator在错误的行中显示Parse Error

时间:2016-10-02 21:58:47

标签: validation parsing dsl xtext

我目前正在开发一个带有以下(shortend)语法的小型dsl:

grammar mydsl with org.eclipse.xtext.common.Terminals hidden(WS, SL_COMMENT)
generate mydsl "uri::mydsl"

CommandSet:
    (commands+=Command)*
;

Command:
    (commandName=CommandName LBRACKET (args=ArgumentList)? RBRACKET EOL ) |
;
terminal LBRACKET:
    '('
;
terminal RBRACKET:
    ')'
;
terminal EOL:
    ';'
;

正如您所看到的,我使用分号作为EOL分隔符,它对我来说效果很好。在eclipse中使用dsl时,内置语法验证器会出现问题。当我错过分号时,验证器会在错误的行中抛出语法错误:

Error marker is shown on the next line

我的语法有错误吗?谢谢;)

1 个答案:

答案 0 :(得分:1)

这是一个基于你的例子松散的小型DSL。基本上,我不认为换行符是"隐藏"任何更长的时间(即解析器不再忽略它们),只有空格。注意语法标题中的新终端MY_WSMY_NL以及修改后的hidden语句(我还在相关位置添加了一些注释)。这种方法只是给你一些一般性的想法,你可以试验它来实现你想要的。请注意,如果不再隐藏换行符,则需要在语法规则中考虑它们。

grammar org.xtext.example.mydsl.MyDsl
    with org.eclipse.xtext.common.Terminals
    hidden( MY_WS, SL_COMMENT )   // ---> hide whitespaces and comments only, not linebreaks!
generate mydsl "uri::mydsl"

CommandSet:
     (commands+=Command)*
;

CommandName:
    name=ID
;

ArgumentList:
   arguments += STRING (',' STRING)*
;

Command:
     (commandName=CommandName LBRACKET (args=ArgumentList)? RBRACKET EOL);

terminal LBRACKET:
   '('
;
terminal RBRACKET:
   ')'
;
terminal EOL:
   ';' MY_NL?    // ---> now an optional linebreak at the end!
;

terminal MY_WS: (' '|'\t')+;    // ---> whitespace characters (formerly part of WS)
terminal MY_NL: ('\r'|'\n')+;   // ---> linebreak characters (no longer hidden)

这是一张展示最终行为的图片。

enter image description here