-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynth.2.st
445 lines (403 loc) · 10.6 KB
/
synth.2.st
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
parse:
ignore "space newline tab"
entry "Template"
[
Template { template:Segment* }
Segment
[
Parse { name:'parse' ':' config:Config* '[' body:ParseRule* ']' }
Script { name:'script' ':' config:Config* '[' body:ScriptRule* ']' }
Output { name:'output' ':' config:Config* '[' body:OutputRule* ']' }
]
Config { name:Name item:RawString }
ParseRule { name:Name body:ParseVariant }
ParseVariant
[
Array { '[' children:ParseVariant* ']' }
Named { name:Name pattern:ParseVariant }
Anonymous { pattern:ParsePattern }
]
ParsePattern { '{' chunks:ParseChunk* '}' }
ParseChunk
[
Aliased { name:Name ':' element:ParseElement }
Plain { element:ParseElement }
]
ParseElement
[
Type { type:TokenType operators:Operator* }
Rule { rule:RulePath operators:Operator* }
Tokens { tokens:String operators:Operator* }
]
RulePath { rule:Name variant:VariantPart* }
VariantPart { '.' variant:Name }
TokenType [
{ t:"WORD" }
{ t:"NUMBER" }
{ t:"SYMBOL" }
{ t:"SPACE" }
{ t:"TAB" }
{ t:"NEWLINE" }
{ t:"BYTE" }
]
ScriptRule { name:Name body:ScriptVariant }
ScriptVariant
[
Array { '[' children:ScriptVariant* ']' }
Code { code:Method }
Named { name:Name children:ScriptVariant }
]
OutputRule { name:Name body:OutputVariant }
OutputVariant
[
Array { '[' children:OutputVariant* ']' }
Pattern { pattern:OutputPattern }
Named { name:Name pattern:OutputVariant }
]
OutputPattern { '{' Padding? chunks:OutputChunk* Padding? '}' }
OutputChunk { element:OutputElement }
OutputElement
[
Whitespace{ w:Whitespace }
Newline { Padding }
Indent { '>' Padding }
Dedent { '<' Padding }
Interline { '^' }
Child { name:Interpolate }
Chunk { chunk:LinePart }
]
Interpolate { '${' name:Name '}' }
LinePart [
Nested { s:Bits }
Any { s:AnyPart }
]
AnyPart [
{ p:Whitespace }
{ p:'}'! }
]
Bits { '{' Padding? bits:OutputElement* Padding? '}' }
Whitespace [
{ s:SPACE }
{ s:TAB }
]
Padding { NEWLINE Whitespace* }
Operator
[
NoneOrMore { op:'*' }
OneOrMore { op:'+' }
NoneOrOne { op:'?' }
AllExcept { op:'!' }
]
String
[
A { '"' inner:AString* '"' }
B { "'" inner:BString* "'" }
]
RawString
[
{ '"' inner:AString* '"' }
{ "'" inner:BString* "'" }
]
AString [ { e:'\\\\' } { e:'\\"' } { e:'"'! } ]
BString [ { e:'\\\\' } { e:"\\'" } { e:"'"! } ]
Method { '---' inner:MethodString* '---' }
MethodString [ { e:'\\\\' } { e:'\\-' } { e:'---'! } ]
Name { head:NameHead tail:NamePart* }
NameHead [
{ head:WORD }
{ head:"_" }
]
NamePart
[
{ head:'_' tail:WORD }
{ head:'_' tail:NUMBER }
]
]
script:
[
Name ---
self.string = collapse(self)
---
LinePart [
Any ---
self.s = collapse(self.s):gsub('\\', '\\\\'):gsub('"', '\\"')
---
]
]
script:
direction "down"
[
Segment [
Parse ---
for _,conf in pairs(self.config) do
if conf.name.string == 'entry' then
assert(not self.entry, 'entry point already specified')
self.entry = collapse(conf.item)
end
end
assert(self.entry, 'Parse segment must have an entry point')
---
]
ParseElement ---
local opends = ''
if #self.operators == 0 then self.operators = {'tokens:match('} end
for i=1, #self.operators do
opends = opends..')'
end
self.opends = opends
---
ParseRule ---
self.body.pname = self.name.string
---
ParseVariant
[
Array ---
for _,child in ipairs(self.children) do
child.pname = self.pname
end
---
Named ---
self.pattern.pname = (self.pname and self.pname..'.' or '')..self.name.string
---
Anonymous ---
self.pattern.pname = self.pname
---
]
String ---
self.inner = collapse(self.inner):gsub(']\\=]', ']=]')
---
]
output:
target 'bootc.lua'
[
Template
{
require "newtokenizer"
require "lib.iter"
require "lib.dump"
require "lib.stringext"
require "errfmt"
require "common"
global = {}
dent = " "
source = fetch(...)
assert(source, "a source file must be specified")
tokens = tokenize(source)
assert(tokens, "tokenization failure")
function indent() >
local d = 0
return function(offset) >
d = d + offset
return "\n" .. dent*d <
end <
end
function inline(interline) >
interline[1] = true return "" <
end
${template}
}
Segment [
Parse {
local parse_conf = { ${config}}
local ruleiter = function(self) >
for n,rule in ipairs(self) do >
if type(rule) == "table" then n,rule = next(rule) end
local v = rule()
if v then return v end <
end <
end
tokens.ignore = >
iter(parse_conf.ignore:trim():split(" "))
:map(function(e) return T(e) end) <
R = { >
^${body}<
}
AST = R.${entry}()
if not AST and tokens:peek() then >
error("unexpected token:"..errfmt(tokens:getLast())) <
end
}
Script {
local script_conf = { ${config}}
script = function(scripts, ast) >
local direction = script_conf.direction
for alias, child in pairs(copy(ast)) do >
if type(child) == "table" and direction ~= "down" then >
script(scripts, child) <
end
if meta(child) and meta(child).type then >
local func = scripts
for _, t in ipairs(meta(child).type) do >
if type(func) == "table" then >
func = func[t] or findOrderedKey(func, t) <
end <
end
if type(func) == "function" then >
if type(child) == "table" and #child > 0 then >
for _, item in ipairs(child) do >
func(item, alias) <
end <
else >
func(child, alias) <
end <
end <
end
if type(child) == "table" and direction == "down" then >
script(scripts, child) <
end <
end <
end
S = { >
^${body} <
}
script(S, AST)
}
Output {
local output_conf = { ${config}}
local assert_node = function(node, alias) >
if not node[alias] then >
error("node doesn't exist: "..alias.." in "..dump(node)..":"..dump(getmetatable(node).type), 2) <
end
return node[alias] <
end
local function output(node, interline, dent) >
if type(node) == "string" or node.meta and node.meta.__token__ then >
return tostring(node) <
else >
if #node > 0 or not getmetatable(node).type then >
local out = {}
for _, node in ipairs(node) do >
out[#out+1] = output(node, false, dent)
if interline[1] then out[#out+1] = dent(0) end <
end
if interline[1] then out[#out] = nil end
return table.concat(out) <
else >
local fn = O
for _, t in ipairs(getmetatable(node).type) do >
fn = assert(fn[t], "type "..t.." has no output")
if type(fn) == "function" then break end <
end
return fn(node, false, dent) <
end <
end <
end
O = { >
^${body} <
}
print(output(AST, false, indent()))
}
]
Config {${name} = [[${item}]], }
ParseRule {
${name} = setmetatable({>
^${body}<
}, {__call = ruleiter});
}
ParseVariant
[
Array {^${children}}
Anonymous {${pattern};}
Named {{${name} = ${pattern}};}
]
ParsePattern {
function() >
local __reset__, node = tokens.curr, {}
repeat >
^${chunks}
return setmetatable(node, {type=("${pname}"):split(".")}) <
until true
tokens.curr = __reset__ <
end}
ParseChunk
[
Aliased {
node.${name} = ${element}
if not node.${name} then break end
}
Plain {
if not ${element} then break end
}
]
ParseElement
[
Type {${operators}${type}${opends}}
Rule {${operators}R.${rule}${opends}}
Tokens {${operators}${tokens}${opends}}
]
RulePath {${rule} ${variant}}
VariantPart {"${variant}"}
TokenType {T"${t}"}
ScriptRule {{${name} = ${body}};}
ScriptVariant
[
Array {
{>
^${children}<
}}
Code {${code}}
Named {${name} = ${children};}
]
OutputRule {${name} = ${body}}
OutputVariant
[
Array {{ >
^${children}<
},}
Pattern {${pattern},}
Named {${name} = ${pattern}}
]
OutputPattern {
function(self, interline, dent) >
local interline = interline or {false}
local dent = dent or indent()
return table.concat({ >
${chunks} <
}) <
end
}
OutputChunk {${element}}
OutputElement
[
Whitespace {"${w}",}
Indent {dent(1), >
}
Dedent {dent(-1), <
}
Newline {dent(0),
}
Interline {inline(interline),}
Child {output(assert_node(self, "${name}"), interline, dent),
}
Chunk {${chunk}}
]
Interpolate {${name}}
LinePart [
Nested {"{", ${s}"}", }
Any {"${s}",}
]
AnyPart {${p}}
Whitespace {${s}}
Bits {${bits}}
Operator
[
NoneOrMore {tokens:noneOrMore(}
OneOrMore {tokens:oneOrMore(}
NoneOrOne {tokens:noneOrOne(}
AllExcept {tokens:allExcept(}
]
String [
A {"${inner}"}
B {'${inner}'}
]
RawString {${inner}}
AString {${e}}
BString {${e}}
Method {
function (self, alias, segment) ${inner} end
}
MethodString {${e}}
Name {${head}${tail}}
NameHead {${head}}
NamePart {${head}${tail}}
]