[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lpeg error handling
- From: Ico <lua@...>
- Date: Wed, 22 Apr 2009 11:16:52 +0200
* On 2009-04-22 Miles Bader <miles@gnu.org> wrote :
> Ico <lua@zevv.nl> writes:
> > My problem is that I'm not sure how to handle parsing errors gracefully.
> > If there's a single syntax error somewhere in the file, the Lpeg pattern
> > does not match at all, so the application is only able to inform the
> > user that "there is a syntax error somewehere", but no details about the
> > exact location. Instead I'd like my app to be able to at least report
> > the line number to the user when the parser finds an error.
>
> I use a function matcher that records the current position, e.g.:
>
> local function update_err_pos (text, pos)
> parse_state.err_pos = pos
> return pos
> end
>
> -- This special lpeg pattern updates the current error position
> --
> ERR_POS = P(update_err_pos)
>
> [where "parse_state" is some global; probably better ways to do that
> now, e.g., the special capture you can use to access the Nth arg of the
> lpeg match call]
>
> Then I stick ERR_POS in various places, for instance, after the
> end-of-line pattern... e.g.:
>
> -- this matches a newline, and also updates the error location
> local WS_NL_SYNC = WS_NL * lu.ERR_POS
>
> The error reporting routines then use the error position to find the
> last line that was ever matched, and include that information along with
> the error message.
Yes, thank you, this seems to be exactly the solution I was looking
for. I think this would make a good and helpful example in the LPEG
documentation!
--
:wq
^X^Cy^K^X^C^C^C^C