我正在为Lua创建一个序列化库,并使用LPeg解析字符串。我已经让K/V对工作(键被显式命名),但是现在我要添加自动索引。
它的工作方式如下:
@"value"
@"value2"将评估到
{
[1] = "value"
[2] = "value2"
}我已经让值匹配工作(字符串、表、数字和布尔都能很好地工作),所以我不需要帮助;我正在寻找的是索引。对于@value模式的每一次匹配,它都应该捕获找到的@value模式的数量--换句话说,我可以匹配一个值序列(“@value1”@"value2"),但我不知道如何根据匹配的数量为它们分配索引。如果不够清楚,只需注释,我将尝试更好地解释它。
下面是我当前的模式(使用压缩表示法):
local process = {} -- Process a captured value
process.number = tonumber
process.string = function(s) return s:sub(2, -2) end -- Strip of opening and closing tags
process.boolean = function(s) if s == "true" then return true else return false end
number = [decimal number, scientific notation] / process.number
string = [double or single quoted string, supports escaped quotation characters] / process.string
boolean = P("true") + "false" / process.boolean
table = [balanced brackets] / [parse the table]
type = number + string + boolean + table
at_notation = (P("@") * whitespace * type) / [creates a table that includes the key and value]正如您在最后一行代码中所看到的,我有一个函数执行以下操作:
k,v matched in the pattern
-- turns into --
{k, v}
-- which is then added into an "entry table" (I loop through it and add it into the return table)发布于 2013-10-23 23:18:19
根据您到目前为止所描述的内容,您应该能够使用简单的捕获和表捕获来完成这一任务。
下面是我提出的一个简化的示例来说明:
lpeg = require 'lpeg'
l = lpeg.locale(lpeg)
whitesp = l.space ^ 0
bool_val = (l.P "true" + "false") / function (s) return s == "true" end
num_val = l.digit ^ 1 / tonumber
string_val = '"' * l.C(l.alnum ^ 1) * '"'
val = bool_val + num_val + string_val
at_notation = l.Ct( (l.P "@" * whitesp * val * whitesp) ^ 0 )
local testdata = [[
@"value1"
@42
@ "value2"
@true
]]
local res = l.match(at_notation, testdata)匹配返回包含以下内容的表:
{
[1] = "value1",
[2] = 42,
[3] = "value2",
[4] = true
}https://stackoverflow.com/questions/19541065
复制相似问题