forked from pygy/LuLPeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinters.lua
340 lines (295 loc) · 10.6 KB
/
printers.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
return function(Builder, LL)
-- Print
local ipairs, pairs, print, tostring, type
= ipairs, pairs, print, tostring, type
local s, t, u = require"string", require"table", require"util"
local S_tostring = Builder.set.tostring
local _ENV = u.noglobals() ----------------------------------------------------
local s_char, s_sub, t_concat
= s.char, s.sub, t.concat
local expose, load, map
= u.expose, u.load, u.map
local escape_index = {
["\f"] = "\\f",
["\n"] = "\\n",
["\r"] = "\\r",
["\t"] = "\\t",
["\v"] = "\\v",
["\127"] = "\\ESC"
}
local function flatten(kind, list)
if list[2].pkind == kind then
return list[1], flatten(kind, list[2])
else
return list[1], list[2]
end
end
for i = 0, 8 do escape_index[s_char(i)] = "\\"..i end
for i = 14, 31 do escape_index[s_char(i)] = "\\"..i end
local
function escape( str )
return str:gsub("%c", escape_index)
end
local
function set_repr (set)
return s_char(load("return "..S_tostring(set))())
end
local printers = {}
local
function LL_pprint (pt, offset, prefix)
-- [[DBG]] print("PRINT -", pt)
-- [[DBG]] print("PRINT +", pt.pkind)
-- [[DBG]] expose(pt)
-- [[DBG]] expose(LL.proxycache[pt])
return printers[pt.pkind](pt, offset, prefix)
end
function LL.pprint (pt0)
local pt = LL.P(pt0)
print"\nPrint pattern"
LL_pprint(pt, "", "")
print"--- /pprint\n"
return pt0
end
for k, v in pairs{
string = [[ "P( \""..escape(pt.as_is).."\" )" ]],
char = [[ "P( \""..escape(to_char(pt.aux)).."\" )"]],
["true"] = [[ "P( true )" ]],
["false"] = [[ "P( false )" ]],
eos = [[ "~EOS~" ]],
one = [[ "P( one )" ]],
any = [[ "P( "..pt.aux.." )" ]],
set = [[ "S( "..'"'..escape(set_repr(pt.aux))..'"'.." )" ]],
["function"] = [[ "P( "..pt.aux.." )" ]],
ref = [[
"V( ",
(type(pt.aux) == "string" and "\""..pt.aux.."\"")
or tostring(pt.aux)
, " )"
]],
range = [[
"R( ",
escape(t_concat(map(
pt.as_is,
function(e) return '"'..e..'"' end)
, ", "))
," )"
]]
} do
printers[k] = load(([==[
local k, map, t_concat, to_char, escape, set_repr = ...
return function (pt, offset, prefix)
print(t_concat{offset,prefix,XXXX})
end
]==]):gsub("XXXX", v), k.." printer")(k, map, t_concat, s_char, escape, set_repr)
end
for k, v in pairs{
["behind"] = [[ LL_pprint(pt.pattern, offset, "B ") ]],
["at least"] = [[ LL_pprint(pt.pattern, offset, pt.aux.." ^ ") ]],
["at most"] = [[ LL_pprint(pt.pattern, offset, pt.aux.." ^ ") ]],
unm = [[LL_pprint(pt.pattern, offset, "- ")]],
lookahead = [[LL_pprint(pt.pattern, offset, "# ")]],
choice = [[
print(offset..prefix.."+")
-- dprint"Printer for choice"
local ch, i = {}, 1
while pt.pkind == "choice" do
ch[i], pt, i = pt[1], pt[2], i + 1
end
ch[i] = pt
map(ch, LL_pprint, offset.." :", "")
]],
sequence = [=[
-- print("Seq printer", s, u)
-- u.expose(pt)
print(offset..prefix.."*")
local acc, p2 = {}
offset = offset .. " |"
while true do
if pt.pkind ~= "sequence" then -- last element
if pt.pkind == "char" then
acc[#acc + 1] = pt.aux
print(offset..'P( "'..s.char(u.unpack(acc))..'" )')
else
if #acc ~= 0 then
print(offset..'P( "'..s.char(u.unpack(acc))..'" )')
end
LL_pprint(pt, offset, "")
end
break
elseif pt[1].pkind == "char" then
acc[#acc + 1] = pt[1].aux
elseif #acc ~= 0 then
print(offset..'P( "'..s.char(u.unpack(acc))..'" )')
acc = {}
LL_pprint(pt[1], offset, "")
else
LL_pprint(pt[1], offset, "")
end
pt = pt[2]
end
]=],
grammar = [[
print(offset..prefix.."Grammar")
-- dprint"Printer for Grammar"
for k, pt in pairs(pt.aux) do
local prefix = ( type(k)~="string"
and tostring(k)
or "\""..k.."\"" )
LL_pprint(pt, offset.." ", prefix .. " = ")
end
]]
} do
printers[k] = load(([[
local map, LL_pprint, pkind, s, u, flatten = ...
return function (pt, offset, prefix)
XXXX
end
]]):gsub("XXXX", v), k.." printer")(map, LL_pprint, type, s, u, flatten)
end
-------------------------------------------------------------------------------
--- Captures patterns
--
-- for _, cap in pairs{"C", "Cs", "Ct"} do
-- for _, cap in pairs{"Carg", "Cb", "Cp"} do
-- function LL_Cc (...)
-- for _, cap in pairs{"Cf", "Cmt"} do
-- function LL_Cg (pt, tag)
-- local valid_slash_type = newset{"string", "number", "table", "function"}
for _, cap in pairs{"C", "Cs", "Ct"} do
printers[cap] = function (pt, offset, prefix)
print(offset..prefix..cap)
LL_pprint(pt.pattern, offset.." ", "")
end
end
for _, cap in pairs{"Cg", "Clb", "Cf", "Cmt", "div_number", "/zero", "div_function", "div_table"} do
printers[cap] = function (pt, offset, prefix)
print(offset..prefix..cap.." "..tostring(pt.aux or ""))
LL_pprint(pt.pattern, offset.." ", "")
end
end
printers["div_string"] = function (pt, offset, prefix)
print(offset..prefix..'/string "'..tostring(pt.aux or "")..'"')
LL_pprint(pt.pattern, offset.." ", "")
end
for _, cap in pairs{"Carg", "Cp"} do
printers[cap] = function (pt, offset, prefix)
print(offset..prefix..cap.."( "..tostring(pt.aux).." )")
end
end
printers["Cb"] = function (pt, offset, prefix)
print(offset..prefix.."Cb( \""..pt.aux.."\" )")
end
printers["Cc"] = function (pt, offset, prefix)
print(offset..prefix.."Cc(" ..t_concat(map(pt.aux, tostring),", ").." )")
end
-------------------------------------------------------------------------------
--- Capture objects
--
local cprinters = {}
local padding = " "
local function padnum(n)
n = tostring(n)
n = n .."."..((" "):rep(4 - #n))
return n
end
local function _cprint(caps, ci, indent, sbj, n)
local openclose, kind = caps.openclose, caps.kind
indent = indent or 0
while kind[ci] and openclose[ci] >= 0 do
if caps.openclose[ci] > 0 then
print(t_concat({
padnum(n),
padding:rep(indent),
caps.kind[ci],
": start = ", tostring(caps.bounds[ci]),
" finish = ", tostring(caps.openclose[ci]),
caps.aux[ci] and " aux = " or "",
caps.aux[ci] and (
type(caps.aux[ci]) == "string"
and '"'..tostring(caps.aux[ci])..'"'
or tostring(caps.aux[ci])
) or "",
" \t", s_sub(sbj, caps.bounds[ci], caps.openclose[ci] - 1)
}))
if type(caps.aux[ci]) == "table" then expose(caps.aux[ci]) end
else
local kind = caps.kind[ci]
local start = caps.bounds[ci]
print(t_concat({
padnum(n),
padding:rep(indent), kind,
": start = ", start,
caps.aux[ci] and " aux = " or "",
caps.aux[ci] and (
type(caps.aux[ci]) == "string"
and '"'..tostring(caps.aux[ci])..'"'
or tostring(caps.aux[ci])
) or ""
}))
ci, n = _cprint(caps, ci + 1, indent + 1, sbj, n + 1)
print(t_concat({
padnum(n),
padding:rep(indent),
"/", kind,
" finish = ", tostring(caps.bounds[ci]),
" \t", s_sub(sbj, start, (caps.bounds[ci] or 1) - 1)
}))
end
n = n + 1
ci = ci + 1
end
return ci, n
end
function LL.cprint (caps, ci, sbj)
ci = ci or 1
print"\nCapture Printer:\n================"
-- print(capture)
-- [[DBG]] expose(caps)
_cprint(caps, ci, 0, sbj, 1)
print"================\n/Cprinter\n"
end
return { pprint = LL.pprint,cprint = LL.cprint }
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.