-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec2.lua
More file actions
61 lines (53 loc) · 1.45 KB
/
spec2.lua
File metadata and controls
61 lines (53 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
local lpeg = require "lpeg"
local re = require "re"
local gram = re.compile[=[
gram <- {| lines {:_trailing: {.*} :} |}
lines <- line*
line <- {| keyvalue |} %nl*
varname <- [a-zA-Z] [_a-zA-Z0-9]*
value <- string
keyvalue <- {:varname: {varname} :} anyspace '=' anyspace anyvalue
anyvalue <- stringvalue / table
stringvalue <- {:value: string :}
string <- string1 / string2 / string3
string1 <- {| '"' { [^"]* } '"' {:_type: '' -> "string1" :} |}
string2 <- {| "'" { [^']* } "'" {:_type: '' -> "string2" :} |}
string3 <- {| "[[" %nl { [^]]* } "]]" %nl {:_type: '' -> "string3" :} |}
table <- {| "{" anyspace tablekeyvalue "}" {:_type: '' -> "table" :} |}
tablekeyvalue <- {| ( {:tkey: anytablekeys :} anyspace "=" anyspace {:tvalue: anyvalue :} anyspace) |}
anytablekeys <- {| ("[" anyspace ( string1 / string2 ) anyspace "]") / { varname } |}
anyspace <- (' '/ %nl )*
name <- [^ <>()=,]+
]=]
local function parse(data)
return gram:match(data)
end
local data
--data = [[package = "luafilesystem"]]
--data = [[package = 'luafilesystem']]
--data = "package = [[luafilesystem]]"
data = [[
package = "luafilesystem"
version = "scm-1"
source = {
url = "git://github.com/keplerproject/luafilesystem"
}
]]
data = [=[
package = "luafilesystem"
version = "scm-1"
foo = 'F o o'
bar = [[
BAR
blah blah
]]
tab1 = {
["foo"] = "FOO"
}
tab2 = {
bar = "BAR"
}
]=]
local result = parse(data)
local tprint = require"tprint"
print(tprint(result, {inline=false}))