lua-users home
lua-l archive

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


lpeg.P always match at beginning of string, maybe an efficient matching anywhere function ?

Example:
keywords = P'while' + 'repeat' + 'for'

to search keywords anywhere efficiently, skip all bad head chars

skip = 1 - S"wrf"    -- set of skipped chars

--> lpeg.M(keywords) == P{ keywords + 1 * skip ^ 0 * V(1) }

lpeg.M(Cp() * keywords * Cp()) == position of first keyword

lpeg.M(C(keywords))^1 == capture all the keywords

it will be convenient to have lpeg go thru the lpeg object tree to build skip set,
and produce an efficient matching object.

If we do it by hand, and more keywords were added, skip have to be updated too.
with lpeg.M (if available), skip set is built on the fly, and always up-to-date