lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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.

[The following is a repeat of an old reply I gave to a similar query.]

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.

-Miles

-- 
80% of success is just showing up.  --Woody Allen