2

I am writing a DSL, and learning parboiled2, at the same time. 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.

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?

Eduardo
  • 8,362
  • 6
  • 38
  • 72
  • Looks fine to me. I would consider avoiding "finish". You can probably replace it with a direct call to cursor at that position, without going through the stack. – Andrzej Wąsowski Jan 06 '18 at 17:09

1 Answers1

0

Parboiled 2 (for now) doesn't support parser recovery strategies. 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. Parboiled1 supports parser recovery techniques, but for now it's dead in buried if favor of support for the second version. 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.

ppopoff
  • 658
  • 7
  • 17
  • I needed this for detecting semantic errors in syntactically correct input, and it worked. Syntax error recovery was my next hurdle, so I did try ANTLR (the JS version) but the null pointer exceptions I got were disappointing. I ended up hand-coding a recursive descent parser, with error-recovery, and am very happy with the outcome. – Eduardo Aug 22 '16 at 23:54