Last active
April 13, 2019 10:28
-
-
Save sourcevault/42ed54ceecb60e56ac25fc3cbfd14bab to your computer and use it in GitHub Desktop.
understanding lpeg.Cmt recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local success = function(all, pos) print('success') return pos end | |
local fail = function(all, pos) print('fail') return pos end | |
local common = Cmt(P('['), success)*(V('STMP')^0)*P(']') | |
local main_cmt = common + Cmt(P(']'),fail) | |
local main_normal = common + P(']')/fail | |
local grammer_cmt = P({'STMP', STMP = main_cmt }) | |
local grammer_normal = P({'STMP', STMP = main_normal }) | |
local str = '[[]]' | |
--------------------------------------------------------------- | |
grammer_cmt:match(str) | |
-- success | |
-- success | |
-- fail | |
-- fail | |
-- QUESTION: why does the second pattern ( printing fail ) match ?? | |
--------------------------------------------------------------- | |
grammer_normal:match(str) | |
-- success | |
-- success | |
-- This seems to work as expected |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@spc476 do you know why
Cmt
match all innerCmt
when used recursively ?Thanks !