[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: LPEG pattern question
- From: "Wesley Smith" <wesley.hoke@...>
- Date: Fri, 12 Sep 2008 02:30:35 -0700
I've been doing some exercises with LPEG to trying and figure this
thing out and have a question about a pattern I concocted and don't
understand. I was trying to extract function names from C/C++ header
files and came up the the code below based on the wiki and docs. The
problem I was trying to solve was how to model a series of of
alphanumeric characters potentially followed by whitespace and
balanced parentheses. I'm not taking into account things like
underscores in the function name etc. This just really basic. The
thing that I had the most trouble with was how to chop off the closed
parentheses from the end of a matched string and ended up using a
function. Is there a pattern oriented way to do this without
functions? Also, this thing:
local FUNCTION_SIGNATURE_STRINGS = (lpeg.Cs(FUNCTION_AND_SIGNATURE) + 1)^0
I have no idea what's going on there, but it works. If I muck with it
it breaks. For example, any positive exponent works, but any negative
exponent doesn't. In the docs for lpeg.Cs it says:
"Creates a substitution capture, which captures the substring of the
subject that matches patt, with substitutions. For any capture inside
patt, the substring that matched the capture is replaced by the
capture value (which should be a string). The capture values from patt
are not returned independently (only as substrings in the resulting
string). "
My current understanding of what this is saying is "For a given
substring of the input string that a pattern matches, it will be
replaced by a value (of some kind, not sure what 'capture value' is
referring to)." Also, what does it mean for the substituted value to
be returned as substrings in the resulting string?
thanks for any clues. I'm a a bit lost in the language.
wes
src = [[
/*! Binds a texture to a texture unit
@param unit - texture unit number [0, MAX_UNITS]
*/
static int bind(lua_State * L);
static int unbind(lua_State * L); ///< Unbinds a texture from a texture unit
static int startcapture(lua_State * L);
static int endcapture(lua_State * L); ///< Other comment
///< Other commentdasds
///< Other commentasdasdas
/*! blah
*/
]]
local WHITESPACE = lpeg.S(" \t\v\n\f")
local DIGIT = lpeg.R("09")
local LETTER = lpeg.R("az", "AZ") + lpeg.P("_")
local ALPHANUM = LETTER + DIGIT
local BALANCED_PARENTHESES = lpeg.P{ "(" * ((1 - lpeg.S"()") +
lpeg.V(1))^0 * ")" }
local FUNCTION_AND_SIGNATURE = ALPHANUM^1*WHITESPACE^0*BALANCED_PARENTHESES
local FUNCTION_SIGNATURE_STRINGS = (lpeg.Cs(FUNCTION_AND_SIGNATURE) + 1)^0
FUNCTION_NAMES = FUNCTION_SIGNATURE_STRINGS /
function(...)
local v = {}
for i, s in ipairs({...}) do
print(s)
v[i] = string.sub(s, 1, (ALPHANUM^1):match(s)-1)
end
return unpack(v)
end
local res = { FUNCTION_NAMES:match(src) }
print("RESULT:");
for i, v in ipairs(res) do
print(i, v)
print("---------------------------------------")
end