lua-users home
lua-l archive

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


Igor Medeiros <igorosbergster@gmail.com> writes:
> i tried print(lpeg.match(lpeg.P(lua), code)) but it prints just the index
> of first character after the match or nil if the match does not occur

Of course a grammar without any actions is just a "matcher".

So you need to add some actions to the grammar to actually do
something more.  In LPEG, every pattern has a sort of "return value",
called its "captures", and you can use special pattern types (see the
LPEG homepage[*] for a concise summary) to modify/filter capture
values.

In cases where you really want lpeg.match to return the value your
looking for, you can just add a capture around the particular sub-rule
you're interested in, and then let that percolate up.

But in more complicated parsing tasks, often  you want to record the
result via side-effect, which can be easily accomplished using
function captures, e.g. "rule / function".

[*] http://www.inf.puc-rio.br/~roberto/lpeg#captures

-miles

-- 
`There are more things in heaven and earth, Horatio,
 Than are dreamt of in your philosophy.'