lua-users home
lua-l archive

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


It was thus said that the Great albertmcchan once stated:
> i am using lpeg 1.0.1, and it has lpeg.code, which call lp_printcode in lptree.c

  Ah, I did not see it when I scanned the source code.  

> the code is just an example of the usefulness of undo

  I still think you approaching LPeg as a replacement for patterns instead
of a tool for semantically parsing text.  

> FYI, the last 'and' may not be anchored in end-of-string.
> If you remove the anchor, you got the first 'and', not the last
> 
> i was just curious if undo is possible ...

  "Undo" what?  If you have an expression:

	A * ( B + C )

A is matched.  Then B is tried.  If that fails, LPeg will "undo" B and try
C.  That's an "undo".  You also have look-ahead with the '#' operator (and
to some degree with the '-' operator).

I know, you are trying to do:

	(.*)and(.*)

and have it replicate a Lua pattern.  Yes, you can do that.  There have been
several versions doing so.  But (and this is something I can't repeat
enough) you need to think differently with LPeg---think "structure" and not
just "pattern".  Compare this:

	https://github.com/spc476/LPeg-talk/blob/master/email-addr.lua

with this:

	http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

to parse email addresses as defined in RFC-822 (the LPeg one can handle
comments; the regex one can't).

  -spc (So I ask again, what exactly are you trying to do?)