确保 ParserRule 中的条目以逗号分隔
我在我的 Xtext 语法文件中编写了以下 ParserRule:
TestSpec:
'{'
......
(('"my"' ':' myValue=(MySpec) (',' | '}'))?) &
......
'}'
MySpec:
'{'
(
(('"suffix"' ':' suffixValue=(STRING) (',')?)?) &
(('"prefix"' ':' prefixValue=(STRING) (',')?)?) &
(('"none"' ':' noneValue=(STRING) (',')?)?) &
)
'}';
现在在我的 Xtext 编辑器中,我想确保“my”中的条目以逗号结尾,除了最后一个如果“后缀”和“前缀”之间缺少逗号,则应给出错误,如以下示例条目所示:
"my": {
"suffix": "testString" ---> I want this to give an error for missing comma (,)
"prefix": "prefixString",
"none": "noneString"
}
解决方案: 我想到的一个解决方案是将语法更改为以下内容:
MySpec:
'{'
(
(('"suffix"' ':' suffixValue=(STRING) (',' | '}'))?) &
(('"prefix"' ':' prefixValue=(STRING) (',' | '}'))?) &
(('"none"' ':' noneValue=(STRING) (',' | '}'))?) &
);
但我不确定这是否是首选方法。是否可以通过验证或其他一些更优雅的方式来实现这一点?
I have written following ParserRule in my Xtext grammar file:
TestSpec:
'{'
......
(('"my"' ':' myValue=(MySpec) (',' | '}'))?) &
......
'}'
MySpec:
'{'
(
(('"suffix"' ':' suffixValue=(STRING) (',')?)?) &
(('"prefix"' ':' prefixValue=(STRING) (',')?)?) &
(('"none"' ':' noneValue=(STRING) (',')?)?) &
)
'}';
Now in my Xtext editor, I want to ensure that the entries in "my" end with comma except the last one and it should give an error if comma is missing between say "suffix" and "prefix" like in following example entries:
"my": {
"suffix": "testString" ---> I want this to give an error for missing comma (,)
"prefix": "prefixString",
"none": "noneString"
}
Solution:
One solution that I thought of is to change the grammar to following:
MySpec:
'{'
(
(('"suffix"' ':' suffixValue=(STRING) (',' | '}'))?) &
(('"prefix"' ':' prefixValue=(STRING) (',' | '}'))?) &
(('"none"' ':' noneValue=(STRING) (',' | '}'))?) &
);
But I am not sure if this the preferred way of doing it. Is it possible to achieve this through Validations or some other more elegant ways?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题在 Xtext Eclipse 社区论坛< /a>.
我选择通过使用以下逗号分隔列表的标准模式来解决它:
This question is answered on Xtext Eclipse Community Forums.
I opted to solve it by using following standard pattern for comma-separated lists: