Skip to content

Instantly share code, notes, and snippets.

@sourcevault
Last active April 13, 2019 10:28
Show Gist options
  • Save sourcevault/42ed54ceecb60e56ac25fc3cbfd14bab to your computer and use it in GitHub Desktop.
Save sourcevault/42ed54ceecb60e56ac25fc3cbfd14bab to your computer and use it in GitHub Desktop.
understanding lpeg.Cmt recursively
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
@sourcevault
Copy link
Author

sourcevault commented Apr 13, 2019

@spc476 do you know why Cmt match all inner Cmt when used recursively ?

Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment