lua-users home
lua-l archive

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


Am 04.04.19 um 00:28 schröbte nobody:

Would it make sense to add a way to tell LPEG "do not backtrack past this point" – e.g. by 'lpeg.cut( )'? (I'm taking the name from Prolog – maybe there's a better name?)  With cuts, there *would* be hard known information that could be printed:  The position in the input when LPEG attempted to backtrack over the cut.

Going a step further, `lpeg.cut( [name] )` could (maybe) be used to produce something like a stack trace?  (I haven't looked at the LPEG internals, don't know how hard/easy this would be.)

With cut, I could have

   entity = lpeg.P "<entity" * WS^1 * lpeg.cut( "tag:entity" ) * "id=" …

and then LPEG has enough information to tell me that in 'tag:entity' at position 12345 (in 'tag:state' at position 123 in 'tag:savegame' at position 10) no alternative matched, and by using the position I can grab the next couple of lexemes (or bytes) from the file, and then I know that there was 'ref=' instead of 'id=' and debugging would be *so* much easier.


At least that's the dream…  Would that actually work?  And is this sufficiently compatible with LPEG's internals?  Or is that maybe possible already?


I don't know whether I understand your proposal correctly, but I think I have used something similar before to raise parse errors: A custom LPeg operator that, when matched, raises an error with line number information and source code snippet.

    `E( [error-message] )`

You can find the implementation on github[1]. (I hope it still works, I haven't used it in a while ...).


-- nobody



Philipp

  [1]:  http://siffiejoe.github.io/lua-luaepnf/