Hello Henrik,

> I am building a grammar for a script language with parslet but I have
> problems formulating it in such a way that error reporting works well.
> I have tried to reduce the parser grammar, a small-ish example can be
> found here:
> 
> http://pastie.org/private/fa6pufjukwp4x5hkgkueq
> 
> I apologize, it's still kind of lengthy but it illustrates my point
> pretty well. 

Nothing to apologize for, this is probably one of the cleanest parsers
I've seen written in a long time. And I must congratulate you: You've
discovered another error reporting edge case.

To get around the edge case, use a root definition like this one:

  rule(:func)  {
    blank? >>
    (str('create') >> blank >> str('function')).as(:create_func) >>
      blank >> type.as(:type) >> blank? >> identifier.as(:name) >>
      str('(') >> blank? >> parameters.maybe.as(:parameters) >>
      blank? >> str(')') >> blank? >> body.as(:body) >>
      blank? >> str(';') >> blank?
  }

  rule(:scriptfile)   { func.repeat(1).as(:transactions) | blank? }

This makes the first node of the grammar tree be something that error
reporting works on.

Note that this is not really a bug, just something that we could do a
better job on. What happens with the old grammar: Parslet finds that
having no input but just blanks works as a solution - only that in your
blanks, there is obviously some text, so that bothers it.

The above rule changes that to: 'a valid input file is either a series
of functions or a completely blank file'. Specified like that, parslet
can give you error output on both alternatives and tell you where the
function went wrong.

The fact that we discuss error reporting so much means obviously that
all other peg parsers lack a main feature ;) I'll simplify your initial
example down to a regression and work on improving the errors produced.

Hope that helps - best regards,
kaspar



Reply via email to