解析 Xtext 中的字符串
我目前正在尝试为公司使用的内部语言编写一个基于 Eclipse 的编辑器。该语言是以下形式的语句的集合:
{action}: {arguments}
...在其自己的行上。 {arguments} 的格式取决于所执行的 {action} 的类型。脚本片段的示例可能如下所示:
banner: Some string with numbers and punctuation (23) in it!
# some comment
timeout: 42
我的问题是解析这样的片段。我有评论和超时声明有效,但我似乎无法创建规则来覆盖横幅声明。我的所有尝试都导致 Antlr“无法访问令牌定义”警告,或者编辑器无法匹配输入。我已经尝试了以下横幅声明规则:
Banner:
'banner:' name=ANY_OTHER*;
and
Banner:
'banner:' name=FF_STRING;
terminal FF_STRING : ('a'..'z'|'A'..'Z'|'0'..'9'|'.'|':'|' ')+;
...这给了我 antlr 警告。我想出的一个技巧是简单地创建一个与 SL_COMMENT 相同的终端,并在开头使用“banner:”而不是“#”。缺点是我没有得到语法着色,“横幅”也没有出现在自动完成列表中。
欢迎任何建议。
I'm currently attempting to write an Eclipse-based editor for an in-house language we use in the Company. The language is a collection of statements of the form:
{action}: {arguments}
...on its own line. The format of {arguments} depends on the type of {action} being performed. An example of a script fragment could look like:
banner: Some string with numbers and punctuation (23) in it!
# some comment
timeout: 42
My problem is parsing such a fragment. I've got comments and the timeout statement working, but I can't seem to create a rule to cover the banner statement. All of my attempts have resulted in Antlr "token definition unreachable" warnings, or the editor not being able to match input. I've tried the following rules for the banner statement:
Banner:
'banner:' name=ANY_OTHER*;
and
Banner:
'banner:' name=FF_STRING;
terminal FF_STRING : ('a'..'z'|'A'..'Z'|'0'..'9'|'.'|':'|' ')+;
...which gives me the antlr warnings. A hack I've come up with is to simple create a terminal which is identical to an SL_COMMENT, with 'banner:' instead of '#' at the beginning. The disadvantage is that I do not get syntax colouring, nor does 'banner' show up in the auto-completion list.
Any advice is welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用数据类型规则和一组精简的终端规则。
像这样的事情可以工作:
You could try to use a data type rule and a reduced set of terminal rules.
Something like this could work: