解析 Xtext 中的字符串

发布于 2024-11-03 04:07:08 字数 733 浏览 3 评论 0原文

我目前正在尝试为公司使用的内部语言编写一个基于 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 技术交流群。

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

发布评论

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

评论(1

﹉夏雨初晴づ 2024-11-10 04:07:08

您可以尝试使用数据类型规则和一组精简的终端规则。
像这样的事情可以工作:

Banner
  'Banner:' name=Value;
Value hidden(): 
  (ID | WS | INT | <any keyword from your grammar>)* LineBreak;
terminal LineBreak: '\r' '\n'? | '\n';
termianl WS: (' '|'\t') *

You could try to use a data type rule and a reduced set of terminal rules.
Something like this could work:

Banner
  'Banner:' name=Value;
Value hidden(): 
  (ID | WS | INT | <any keyword from your grammar>)* LineBreak;
terminal LineBreak: '\r' '\n'? | '\n';
termianl WS: (' '|'\t') *
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文