forked from pygy/LuLPeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstructors.lua
312 lines (258 loc) · 8.32 KB
/
constructors.lua
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
-- Constructors
-- Patterns have the following, optional fields:
--
-- - type: the pattern type. ~1 to 1 correspondance with the pattern constructors
-- described in the LPeg documentation.
-- - pattern: the one subpattern held by the pattern, like most captures, or
-- `#pt`, `-pt` and `pt^n`.
-- - aux: any other type of data associated to the pattern. Like the string of a
-- `P"string"`, the range of an `R`, or the list of subpatterns of a `+` or
-- `*` pattern. In some cases, the data is pre-processed. in that case,
-- the `as_is` field holds the data as passed to the constructor.
-- - as_is: see aux.
-- - meta: A table holding meta information about patterns, like their
-- minimal and maximal width, the form they can take when compiled,
-- whether they are terminal or not (no V patterns), and so on.
local getmetatable, ipairs, newproxy, print, setmetatable
= getmetatable, ipairs, newproxy, print, setmetatable
local t, u, compat
= require"table", require"util", require"compat"
--[[DBG]] local debug = require"debug"
local t_concat = t.concat
local copy, getuniqueid, id, map
, weakkey, weakval
= u.copy, u.getuniqueid, u.id, u.map
, u.weakkey, u.weakval
local _ENV = u.noglobals() ----------------------------------------------------
--- The type of cache for each kind of pattern:
--
-- Patterns are memoized using different strategies, depending on what kind of
-- data is associated with them.
local patternwith = {
constant = {
"Cp", "true", "false"
},
-- only aux
aux = {
"string", "any",
"char", "range", "set",
"ref", "sequence", "choice",
"Carg", "Cb"
},
-- only sub pattern
subpt = {
"unm", "lookahead", "C", "Cf",
"Cg", "Cs", "Ct", "/zero"
},
-- both
both = {
"behind", "at least", "at most", "Clb", "Cmt",
"div_string", "div_number", "div_table", "div_function"
},
none = "grammar", "Cc"
}
-------------------------------------------------------------------------------
return function(Builder, LL) --- module wrapper.
--
local S_tostring = Builder.set.tostring
-------------------------------------------------------------------------------
--- Base pattern constructor
--
local newpattern, pattmt
-- This deals with the Lua 5.1/5.2 compatibility, and restricted
-- environements without access to newproxy and/or debug.setmetatable.
if compat.proxies and not compat.lua52_len then
-- Lua 5.1 / LuaJIT without compat.
local proxycache = weakkey{}
local __index_LL = {__index = LL}
local baseproxy = newproxy(true)
pattmt = getmetatable(baseproxy)
Builder.proxymt = pattmt
function pattmt:__index(k)
return proxycache[self][k]
end
function pattmt:__newindex(k, v)
proxycache[self][k] = v
end
function LL.getdirect(p) return proxycache[p] end
function newpattern(cons)
local pt = newproxy(baseproxy)
setmetatable(cons, __index_LL)
proxycache[pt]=cons
return pt
end
else
-- Fallback if neither __len(table) nor newproxy work
-- for example in restricted sandboxes.
if LL.warnings and not compat.lua52_len then
print("Warning: The `__len` metamethod won't work with patterns, "
.."use `LL.L(pattern)` for lookaheads.")
end
pattmt = LL
function LL.getdirect (p) return p end
function newpattern(pt)
return setmetatable(pt,LL)
end
end
Builder.newpattern = newpattern
local
function LL_ispattern(pt) return getmetatable(pt) == pattmt end
LL.ispattern = LL_ispattern
function LL.type(pt)
if LL_ispattern(pt) then
return "pattern"
else
return nil
end
end
-------------------------------------------------------------------------------
--- The caches
--
local ptcache, meta
local
function resetcache()
ptcache, meta = {}, weakkey{}
Builder.ptcache = ptcache
-- Patterns with aux only.
for _, p in ipairs(patternwith.aux) do
ptcache[p] = weakval{}
end
-- Patterns with only one sub-pattern.
for _, p in ipairs(patternwith.subpt) do
ptcache[p] = weakval{}
end
-- Patterns with both
for _, p in ipairs(patternwith.both) do
ptcache[p] = {}
end
return ptcache
end
LL.resetptcache = resetcache
resetcache()
-------------------------------------------------------------------------------
--- Individual pattern constructor
--
local constructors = {}
Builder.constructors = constructors
constructors["constant"] = {
truept = newpattern{ pkind = "true" },
falsept = newpattern{ pkind = "false" },
Cppt = newpattern{ pkind = "Cp" }
}
-- data manglers that produce cache keys for each aux type.
-- `id()` for unspecified cases.
local getauxkey = {
string = function(aux, as_is) return as_is end,
table = copy,
set = function(aux, as_is)
return S_tostring(aux)
end,
range = function(aux, as_is)
return t_concat(as_is, "|")
end,
sequence = function(aux, as_is)
return t_concat(map(getuniqueid, aux),"|")
end
}
getauxkey.choice = getauxkey.sequence
constructors["aux"] = function(typ, aux, as_is)
-- dprint("CONS: ", typ, pt, aux, as_is)
local cache = ptcache[typ]
local key = (getauxkey[typ] or id)(aux, as_is)
if not cache[key] then
cache[key] = newpattern{
pkind = typ,
aux = aux,
as_is = as_is
}
end
return cache[key]
end
-- no cache for grammars
constructors["none"] = function(typ, aux)
-- [[DBG]] print("CONS: ", typ, _, aux)
-- [[DBG]] print(debug.traceback(1))
return newpattern{
pkind = typ,
aux = aux
}
end
constructors["subpt"] = function(typ, pt)
-- [[DP]]print("CONS: ", typ, pt, aux)
local cache = ptcache[typ]
if not cache[pt] then
cache[pt] = newpattern{
pkind = typ,
pattern = pt
}
end
return cache[pt]
end
constructors["both"] = function(typ, pt, aux)
-- [[DBG]] print("CONS: ", typ, pt, aux)
local cache = ptcache[typ][aux]
if not cache then
ptcache[typ][aux] = weakval{}
cache = ptcache[typ][aux]
end
if not cache[pt] then
cache[pt] = newpattern{
pkind = typ,
pattern = pt,
aux = aux,
cache = cache -- needed to keep the cache as long as the pattern exists.
}
end
return cache[pt]
end
constructors["binary"] = function(typ, a, b)
-- [[DBG]] print("CONS: ", typ, pt, aux)
return newpattern{
a, b;
pkind = typ,
}
end
end -- module wrapper
-- The Romantic WTF public license.
-- --------------------------------
-- a.k.a. version "<3" or simply v3
--
--
-- Dear user,
--
-- The LuLPeg library
--
-- \
-- '.,__
-- \ /
-- '/,__
-- /
-- /
-- /
-- has been / released
-- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
-- under the Romantic WTF Public License.
-- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~`,´ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
-- I hereby grant you an irrevocable license to
-- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
-- do what the gentle caress you want to
-- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
-- with this lovely
-- ~ ~ ~ ~ ~ ~ ~ ~
-- / thing...
-- / ~ ~ ~ ~
-- / Love,
-- # / '.'
-- ####### ·
-- #####
-- ###
-- #
--
-- -- Pierre-Yves
--
--
-- P.S.: Even though I poured my heart into this work,
-- I _cannot_ provide any warranty regarding
-- its fitness for _any_ purpose. You
-- acknowledge that I will not be held liable
-- for any damage its use could incur.