lua-users home
lua-l archive

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


On Jan 25, 2018, at 8:03 PM, Sean Conner <sean@conman.org> wrote:

>  I didn't find this lpeg.pcode() function.  There is an lpeg.ptree()
> function, and the re expression above generates:
>    
>    [1 = g  ]
>    capture kind: 'simple'  key: 0
>      grammar 1
>        rule n: 0  key: 1
>          choice
>            seq
>              any
>              call key: 1  (rule: 0)
>            and
>              seq
>                char 'a'
>                seq
>                  char 'n'
>                  char 'd'
>  I'm not sure what you mean by "behind 3" instruction.

i am using lpeg 1.0.1, and it has lpeg.code, which call lp_printcode in lptree.c

pat = re.compile "{g <- .g / &'and'}"
= lpeg.pcode(pat)

[1 = g ]
00 opencapture simple (idx = 0)
01 call -> 5
03 jmp -> 19
05 testany -> 14
07 choice -> 14
09 any
10 call -> 5
12 commit -> 18
14 char 'a'
15 char 'n'
16 char 'd'
17 behind 3          -- undo consumed 'and'
18 ret
19 closecapture
20 end

behind == codebehind() in lpcode.c (line 659 - 663)

>  If you are looking for a final "and" (which ends the input), then this
> works:
> 
>    last_and = P"and" * P(-1)
>    char     = R("\0\96","b\255")^1
>                 + -last_and * P"a"
>    pat      = C((char)^0) * last_and

the code is just an example of the usefulness of undo

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 ...