lua-users home
lua-l archive

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


> I have a main call to lpeg.match in the form:
> 	return lpeg.match(lpeg.Ct([pattern]), input)
> 
> but in [pattern], I would like to be able to have patterns that call 
> lpeg.match for a subset of text. For example:
> 	lpeg.P(function(sub_input, index)
> 		local matches = lpeg.match(lpeg.Ct([sub-pattern]), sub_input)
> 		-- NEEDED: additional code
> 		return some_valid_number
> 	end)
> 
> but I want to append the contents of 'matches' to the main call's table 
> captures where the 'additional code' comment is. Is there a way to do this?

Not directly. You will have to duplicate the sub-match in a function
capture. Something more or less like this:

  lpeg.P(function (sub_input, index)
    return lpeg.match([sub-pattern-with-no-captures], sub_input))
  / function(sub_match)
      return lpeg.match(lpeg.Ct([sub-pattern-with-captures]), sub_input)
    end

The first function simply limits the submatch; this submatch is sent
to the second function that returns the values to be added to the
enclosing table.

-- Roberto