简体   繁体   English

Parboiled2:引用AST中源文本中的位置

[英]Parboiled2: reference to position in source text from AST

I am writing a DSL, and learning parboiled2, at the same time. 我正在编写DSL,并同时学习parboiled2。 Once my AST is built, I would like to run some semantic checks and, if there are any errors, output error messages that reference the offending positions in the source text. 建立AST之后,我想进行一些语义检查,如果有任何错误,请输出引用源文本中有问题的位置的错误消息。

I am writing things like the following, which, so far, do work: 我正在编写类似以下内容的代码,到目前为止,这些代码可以正常工作:

case class CtxElem[A](start:Int, end:Int, elem:A)

def Identifier = rule {
  push(cursor) ~
  capture(Alpha ~ zeroOrMore(AlphaNum)) ~
  push(cursor) ~
  WhiteSpace
  ~> ((start, identifier, finish) => CtxElem(start, finish, identifier))
}

Is there a better or simpler way? 有没有更好或更简单的方法?

Parboiled 2 (for now) doesn't support parser recovery strategies . Parboiled 2(目前) 不支持解析器恢复策略 It means that if parser will fail - it will stop. 这意味着如果解析器将失败-它将停止。 As far as I remember it should print the symbol where it failed, or at least you could get the cursor 据我记得,它应该在失败的地方打印符号,否则至少您可以得到光标

So if you're trying to build your own DSL and you need that kind of functionality, I would propose you to use a different tool like ANTLR. 因此,如果您尝试构建自己的DSL,并且需要这种功能,那么我建议您使用其他工具,例如ANTLR。 Parboiled1 supports parser recovery techniques, but for now it's dead in buried if favor of support for the second version. Parboiled1支持解析器恢复技术,但如果现在支持第二个版本,那么它现在已经死了。 Parboiled 2 is good in parsing of log files or configuration files (that should correct by default), but it's not good for building DSLs. Parboiled 2可以很好地解析日志文件或配置文件(默认情况下应更正),但不适用于构建DSL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM