确保 ParserRule 中的条目以逗号分隔

发布于 2024-12-20 23:28:39 字数 950 浏览 0 评论 0原文

我在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风夜微凉 2024-12-27 23:28:39

This question is answered on Xtext Eclipse Community Forums.
I opted to solve it by using following standard pattern for comma-separated lists:

List:
    listItems+=ListItem (',' listItems+=ListItem)*;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文