alex / rply

An attempt to port David Beazley's PLY to RPython, and give it a cooler API.
BSD 3-Clause "New" or "Revised" License
381 stars 60 forks source link

Unify error handling, add expected tokens to context #23

Closed nvie closed 4 years ago

nvie commented 10 years ago

This does two things:

  1. Change error handler invocation to include a set of expected tokens. This is really useful to create error message to end users;
  2. It unifies error handling by always calling the error handler with 3 arguments, in an order that makes sense: token, expected, state. This means the order, and existence/absense of arguments does not depend on whether or not the parser has been initialized with a state or not. The state is always included, even if it's None.

I realize this is a backward-incompatible change, but it definitely is a lot cleaner, I think. Let me know if you think this should be dealt with differently.

Example:

@pg.error
def error_handler(token, expected, state):
    exp = ', '.join(sorted(expected))
    msg = 'Ran into %s where it was expecting any of %s' % (token, exp)
    raise ValueError(msg)
prologic commented 10 years ago

+1 Can we get this merged in? I'm using this now myself and working quite well.