forked from pcanz/pPEGjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpPEG.mjs
1254 lines (1162 loc) · 30.4 KB
/
pPEG.mjs
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
pPEG in JavaScript
See: <https://github.com/pcanz/pPEG>
No dependencies, this is the only file you need.
*/
const pPEG_grammar = `
Peg = _ rule+
rule = id _ '=' _ alt
alt = seq ('/' _ seq)*
seq = rep+
rep = pre sfx? _
pre = pfx? term
term = call / sq / chs / group / extn
id = [a-zA-Z_] [a-zA-Z0-9_]*
pfx = [&!~]
sfx = [+?] / '*' range?
range = num (dots num?)?
num = [0-9]+
dots = '..'
call = id _ !'='
sq = ['] ~[']* [']
chs = '[' ~']'* ']'
group = '(' _ alt ')'
extn = '<' ~'>'* '>'
_ = ('#' ~[\n\r]* / [ \t\n\r]*)*
`;
// biome-ignore format:
const pPEG_rules =
[["rule",[["id","Peg"],
["seq",[["id","_"],["rep",[["id","rule"],["sfx","+"]]],["id","_"]]]]],
["rule",[["id","rule"],
["seq",[["id","id"],["id","_"],["sq","'='"],["id","_"],["id","alt"]]]]],
["rule",[["id","alt"],
["seq",[["id","seq"],["rep",[["seq",[["sq","'/'"],["id","_"],["id","seq"]]],["sfx","*"]]]]]]],
["rule",[["id","seq"],
["rep",[["id","rep"],["sfx","+"]]]]],
["rule",[["id","rep"],
["seq",[["id","pre"],["rep",[["id","sfx"],["sfx","?"]]],["id","_"]]]]],
["rule",[["id","pre"],
["seq",[["rep",[["id","pfx"],["sfx","?"]]],["id","term"]]]]],
["rule",[["id","term"],
["alt",[["id","call"],["id","sq"],["id","chs"],["id","group"],["id","extn"]]]]],
["rule",[["id","id"],
["seq",[["chs","[a-zA-Z_]"],
["rep",[["chs","[a-zA-Z0-9_]"],["sfx","*"]]]]]]],
["rule",[["id","pfx"],
["chs","[&!~]"]]],
["rule",[["id","sfx"],
["alt",[["chs","[+?]"],["seq",[["sq","'*'"],["rep",[["id","range"],["sfx","?"]]]]]]]]],
["rule",[["id","range"],
["seq",[["id","num"],["rep",[["seq",[["id","dots"],["rep",[["id","num"],["sfx","?"]]]]],
["sfx","?"]]]]]]],
["rule",[["id","num"],
["rep",[["chs","[0-9]"],["sfx","+"]]]]],
["rule",[["id","dots"],
["sq","'..'"]]],
["rule",[["id","call"],
["seq",[["id","id"],["id","_"],["pre",[["pfx","!"],["sq","'='"]]]]]]],
["rule",[["id","sq"],
["seq",[["chs","[']"],["rep",[["pre",[["pfx","~"],["chs","[']"]]],
["sfx","*"]]],["chs","[']"],["rep",[["sq","'i'"],["sfx","?"]]]]]]],
["rule",[["id","chs"],
["seq",[["sq","'['"],["rep",[["pre",[["pfx","~"],["sq","']'"]]],
["sfx","*"]]],["sq","']'"]]]]],
["rule",[["id","group"],
["seq",[["sq","'('"],["id","_"],["id","alt"],["sq","')'"]]]]],
["rule",[["id","extn"],
["seq",[["sq","'<'"],["rep",[["pre",[["pfx","~"],["sq","'>'"]]],
["sfx","*"]]],["sq","'>'"]]]]],
["rule",[["id","_"],
["rep",[["alt",[["seq",[["sq","'#'"],["rep",[["pre",[["pfx","~"],["chs","[\n\r]"]]],
["sfx","*"]]]]],["rep",[["chs","[ \t\n\r]"],["sfx","*"]]]]],["sfx","*"]]]]]];
const pPEG_codex = compiler(pPEG_rules);
// pPEG machine instructions --------------------------------------------------
/**
*
* @param exp
* @param env
* @returns {boolean}
*/
function ID(exp, env) {
// [ID, idx, name]
const start = env.pos;
const stack = env.tree.length;
const name = exp[2];
const expr = env.code[exp[1]];
if (env.panic || env.depth > env.max_depth) {
if (!env.panic)
env.panic = `max depth of recursion exceeded in rules:\n ... ${env.rule_names.slice(-6).join(" ")}\n`;
return false;
}
if (env.trace) trace_enter(exp, env);
env.depth += 1;
env.rule_names[env.depth] = name;
env.start[env.depth] = start;
env.stack[env.depth] = stack;
const result = expr[0](expr, env);
env.depth -= 1;
if (result === false) {
if (env.trace) trace_result(exp, env, false);
env.pos = start;
env.tree.length = stack;
return false;
}
if (name[0] === "_") {
// no results required..
if (env.tree.length > stack) {
// nested rule results...
env.tree = env.tree.slice(0, stack); // deleted
}
if (env.trace) trace_result(exp, env, true, start);
return true;
}
if (env.tree.length - stack > 1 || name[0] <= "Z") {
const result = [name, env.tree.slice(stack)]; // stack..top
env.tree = env.tree.slice(0, stack); // delete stack..
env.tree.push(result);
if (env.trace) trace_result(exp, env, result);
return true;
}
if (env.tree.length === stack) {
// terminal string value..
const result = [name, env.input.slice(start, env.pos)];
env.tree.push(result);
if (env.trace) trace_result(exp, env, result);
return true;
}
if (env.trace) trace_result(exp, env, env.tree[env.tree.length - 1]);
return true; // elide this rule label
} // ID
function ALT(exp, env) {
// [ALT, [...exp], [...guards]]
if (env.trace) trace_rep(exp, env);
const start = env.pos;
const stack = env.tree.length;
const ch = env.input[start];
for (let i = 0; i < exp[1].length; i += 1) {
if (!env.trace && exp.length > 2) {
const x = exp[2][i]; // guard ch
if (x && ch !== x) continue; // forget this alt
}
const arg = exp[1][i];
const result = arg[0](arg, env);
if (result) return true;
if (env.tree.length > stack) {
env.tree = env.tree.slice(0, stack);
}
env.pos = start; // reset pos and try the next alt
}
return false;
} // ALT
function SEQ(exp, env) {
// [SEQ, min, max, [...exp]]
if (env.trace) trace_rep(exp, env);
const [_, min, max, args] = exp;
let count = 0;
while (true) {
// min..max
let i = 0;
let start = env.pos;
const stack = env.tree.length;
for (i = 0; i < args.length; i += 1) {
const arg = args[i];
const result = arg[0](arg, env);
if (result === false) {
if (count >= min) {
env.pos = start;
if (env.tree.length > stack) {
env.tree = env.tree.slice(0, stack);
}
return true;
}
if (env.pos > start && env.pos > env.fault_pos) {
env.fault_pos = env.pos;
env.fault_tree = env.tree.slice(0);
env.fault_rule = env.rule_names[env.depth];
env.fault_exp = exp[3][i];
}
return false;
}
}
count += 1;
if (count === max) break; // max 0 means any`
if (env.pos === start) break; // no progress
start = env.pos; // next start in min..max
} // min..max
return count >= min;
}
function REP(exp, env) {
// [REP, min, max, exp]
if (env.trace) trace_rep(exp, env);
const [_rep, min, max, expr] = exp;
const stack = env.tree.length;
let count = 0;
let pos = env.pos;
while (true) {
// min..max
const result = expr[0](expr, env);
if (result === false) break;
count += 1;
if (pos === env.pos) break; // no progress
if (count === max) break; // max 0 means any`
pos = env.pos;
}
if (count < min) {
if (env.tree.length > stack) {
env.tree = env.tree.slice(0, stack);
}
return false;
}
return true;
}
function PRE(exp, env) {
// [PRE, sign, term]
const [_pre, sign, term] = exp;
const start = env.pos;
const stack = env.tree.length;
const trace = env.trace;
const peak = env.peak;
env.trace = false;
const result = term[0](term, env);
env.peak = peak;
env.trace = trace;
if (trace) trace_pre(exp, env, result);
if (env.tree.length > stack) {
env.tree = env.tree.slice(0, stack);
}
env.pos = start;
if (sign === "~") {
if (result === true || env.pos >= env.input.length) return false;
env.pos = start + 1;
if (env.peak < env.pos) env.peak = env.pos;
return true;
}
if (sign === "!") return !result;
return result;
}
function SQ(exp, env) {
// [SQ, icase, "..."]
const start = env.pos;
const input = env.input;
const icase = exp[1]; // case insensitive
const str = exp[2];
const len = str.length;
if (len === 0) return true; // '' empty str
let pos = env.pos;
for (let i = 0; i < len; i += 1) {
let char = input[pos]; // undefined if pos >= input.length
if (icase && pos < input.length) char = char.toUpperCase();
if (str[i] !== char) {
env.pos = start;
if (env.trace) trace_chars_fail(exp, env);
return false;
}
pos += 1;
}
env.pos = pos;
if (pos > env.peak) env.peak = pos;
if (env.trace) trace_chars_match(exp, env, start);
return true;
}
function CHS(exp, env) {
// [CHS, neg, min, max, str]
const input = env.input;
const start = env.pos;
const [_, neg, min, max, str] = exp;
let pos = env.pos;
let count = 0;
while (pos < input.length) {
// min..max
let hit = false;
const ch = env.input[pos];
for (let i = 0; i < str.length; i += 1) {
if (i + 2 < str.length && str[i + 1] === "-") {
if (ch < str[i] || ch > str[i + 2]) {
i += 2;
continue;
}
} else {
if (ch !== str[i]) continue;
}
hit = true;
break;
}
if (neg) hit = !hit;
if (!hit) break;
count += 1;
pos += 1;
if (count === max) break;
} // min..max loop
if (count < min) {
if (env.trace) trace_chars_fail(exp, env);
return false;
}
env.pos = pos;
if (pos > env.peak) env.peak = pos;
if (env.trace) trace_chars_match(exp, env, start);
return true;
}
function EXTN(exp, env) {
// [EXTN, "<xxx>"]
const ext = exp[1].slice(1, -1).split(" ");
const key = ext[0]; //, sigil = '';
// for (let i=0; i<key.length; i+=1) {
// if (key[i] >= 'A') break; // 0..@
// sigil += key[i];
// }
// key = sigil || key;
const fn = env.extend[key] || builtin(key);
if (!fn) {
if (env.pos > env.fault_pos) {
env.fault_pos = env.pos;
env.fault_tree = env.tree.slice(0);
env.fault_rule = env.rule_names[env.depth];
env.fault_exp = `missing extension: ${exp[1]}`;
}
return false;
}
try {
const result = fn(exp, env);
if (env.trace) trace_extn(exp, env, result);
if (result) return true; // allow any JS truthy return
return false;
} catch (err) {
// treat extn exceptions as panic failures
env.panic = err;
return false;
}
}
// bultins -- predefined extension functions -------------------------------
const builtins = {
"?": dump_trace,
trace: trace_trigger, // deprecate trace...?
"@": same_match, // deprecate?
eq: same_match, // deprecate
at: same_match, // TODO -- chk undefined extn working?
infix, // pratt op exp
quote,
quoter,
indent: ext_indent,
inset: ext_inset,
undent: ext_undent,
};
function builtin(key) {
return builtins[key] || undefined;
}
// <?> dump trace -----------------------------------------
function dump_trace(exp, env) {
let report = `<?> at line: ${line_number(env.input, env.pos)}\n`;
for (let i = 0; i < env.tree.length; i += 1) {
const tree = show_tree(env.tree[i]);
report += `${tree}\n`;
}
report += line_report(env.input, env.pos);
console.log(report);
return true;
}
// <infix> -------------------------------------------------
function infix(exp, env) {
const stack = env.stack[env.depth];
if (env.tree.length - stack < 3) return true;
let next = stack - 1; // tree stack index
env.tree[stack] = pratt(0);
env.tree.length = stack + 1;
return true;
function pratt(lbp) {
next += 1;
let result = env.tree[next];
while (true) {
next += 1;
const op = env.tree[next];
let rbp = op ? 0 : -1;
const sfx = op ? op[0].slice(-3) : undefined;
if (sfx && sfx[0] === "_") {
// _xL or _xR
const x = sfx.charCodeAt(1);
if (sfx[2] === "L") rbp = (x << 1) + 1;
if (sfx[2] === "R") rbp = (x + 1) << 1;
}
if (rbp < lbp) {
next -= 1; // restore op
break;
}
rbp = rbp % 2 === 0 ? rbp - 1 : rbp + 1;
result = [op[1], [result, pratt(rbp)]];
}
return result;
}
}
// <@name> -------------------------------------------------
function same_match(exp, env) {
// <eq name>, deprecate <@name>
const ext = exp[1].slice(1, -1).split(" ");
const name = ext[1];
const idx = env.codex.names[name];
const code = env.code[idx];
if (!code) throw `${exp[1]} undefined rule: ${name}`;
let prior = ""; // previous name rule result
for (let i = env.tree.length - 1; i >= 0; i -= 1) {
const [rule, value] = env.tree[i];
if (rule === name) {
prior = value;
break;
}
}
if (prior === "") return true; // '' empty match deafult (no prior value)
if (env.input.startsWith(prior, env.pos)) {
env.pos += prior.length;
return true;
}
return false;
}
// <quote> and <quopter> --------------------------------------
function quote(exp, env) {
// marks <quote>
const input = env.input;
const start = env.start[env.depth];
const sot = env.pos;
const marks = input.slice(start, sot);
const eot = input.indexOf(marks, sot);
if (eot === -1) return false;
env.tree.push(["quote", input.slice(sot, eot)]);
env.pos = eot + marks.length;
if (env.peak < env.pos) env.peak = env.pos;
return true;
}
function quoter(exp, env) {
// marks <quoter>
const input = env.input;
const start = env.start[env.depth];
const sot = env.pos;
let marks = ""; // reverse marks
for (let i = sot - 1; i >= start; i -= 1) marks += input[i];
const eot = input.indexOf(marks, sot);
if (eot === -1) return false;
env.tree.push(["quoter", input.slice(sot, eot)]);
console.log(start, sot, eot, marks);
env.pos = eot + marks.length;
if (env.peak < env.pos) env.peak = env.pos;
return true;
}
// <indent, <inset>, <undent> ---------------------------------
function ext_indent(exp, env) {
const input = env.input;
const start = env.pos;
let i = start;
while (input[i] === " ") i += 1;
if (i === start) while (input[i] === "\t") i += 1;
if (i === start) return false;
const current = env.indent.at(-1);
if (current && i - start <= current.length) return false;
if (current && current[0] !== input[start]) throw "indent: mixed space & tab";
env.indent.push(env.input.slice(start, i));
return true;
}
function ext_inset(exp, env) {
const input = env.input;
const start = env.pos;
let i = start;
while (input[i] === " ") i += 1;
if (i === start) while (input[i] === "\t") i += 1;
if (i === start) return false;
return input.slice(start, i) === env.indent.at(-1);
}
function ext_undent(exp, env) {
env.indent.pop();
return true;
}
// fault reporting -------------------------------------------------------
function line_report(str, pos, note = "") {
let i = pos; // start of line..
while (i > 0 && str[i - 1] !== "\n" && str[i - 1] !== "\r") i -= 1;
let j = pos; // end of line..
while (j < str.length && str[j] !== "\n" && str[j] !== "\r") j += 1;
const { row } = line_info(str, pos); // {row, col}
let inset = line_label(-1); // ...inset...^
for (let n = i; n < pos; n += 1) inset += " ";
const before = lines_before(str, row, i, j, 3);
const after = lines_after(str, row, j, 2);
return `${before}${inset}^${after}${note}`;
}
function line_number(str, pos) {
return line_info(str, pos).at;
}
function line_info(str, pos) {
let i = 0;
let sol = 0;
let cr = 1;
let lf = 1;
while (i < pos) {
if (str[i] === "\r") {
cr += 1;
i += 1;
}
if (str[i] === "\n") {
lf += 1;
i += 1;
}
sol = i;
while (i < pos && str[i] !== "\n" && str[i] !== "\r") i += 1;
}
const row = lf >= cr ? lf : cr;
const col = pos - sol + 1;
const at = `${row}.${col}`;
return { row, col, at };
}
function lines_before(str, ln, i, j, n) {
let before = "";
while (true) {
const label = line_label(ln);
before = `${label}${str.slice(i, j)}\n${before}`;
n -= 1;
if (i === 0 || n === 0) return before;
ln -= 1;
if (i > 0 && str[i - 1] === "\n") i -= 1;
if (i > 0 && str[i - 1] === "\r") i -= 1;
j = i;
while (i > 0 && str[i - 1] !== "\n" && str[i - 1] !== "\r") i -= 1;
}
}
function lines_after(str, ln, i, n) {
let after = "";
while (n > 0 && i < str.length) {
ln += 1;
let j = i;
if (str[j] === "\n") j += 1;
if (j < str.length && str[j] === "\r") j += 1;
let k = j;
while (k < str.length && str[k] !== "\n" && str[k] !== "\r") k += 1;
after += `\n${line_label(ln)}${str.slice(j, k)}`;
i = k;
n -= 1;
}
return after;
}
/**
*
* @param {number} n
* @returns {string}
*/
function line_label(n) {
const blank = " ";
if (n < 0) return blank;
let ln = ` ${n} | `;
while (ln.length < blank.length) ln = ` ${ln}`;
return ln;
}
// trace ----------------------------------------------------------
function trace_trigger(exp, env) {
// <?> extension
if (env.trace) return true; // nested <?>
env.trace = true;
env.trace_depth = env.depth; // active enter/exit current rule
trace_report(` ${env.rule_names[env.trace_depth]}`);
return true;
}
function trace_enter(exp, env) {
if (env.trace_depth === -1) {
// not active
if (env.trace !== exp[2]) return;
env.trace_depth = env.depth; // active enter/exit current rule
}
trace_report(indent(env) + exp[2]);
}
function trace_result(exp, env, result, start) {
if (env.trace_depth === -1) return; // not active
if (env.depth + 1 < env.trace_depth) return;
if (result === false) {
trace_report(`${indent(env)}${exp_show(exp)} != ${show_line(env)}`);
} else if (result === true) {
trace_report(
`${indent(env)}${exp[2]} == ${show_input(env, start, env.pos)}`,
);
} else {
let show = JSON.stringify(result);
if (show.length > 70) show = `${show.slice(0, 60)} ... ]`;
trace_report(`${indent(env)}${exp[2]} => ${show}`);
}
}
function trace_rep(exp, env) {
if (env.trace_depth === -1) return; // not active
trace_report(indent(env) + exp_show(exp));
}
function trace_chars_match(exp, env, start) {
if (env.trace_depth === -1) return; // not active
trace_report(
`${indent(env)}${exp_show(exp)} == ${show_input(env, start, env.pos)}`,
);
}
function trace_chars_fail(exp, env) {
if (env.trace_depth === -1) return; // not active
trace_report(`${indent(env)}${exp_show(exp)} != ${show_line(env)}`);
}
function trace_pre(exp, env, result) {
if (env.trace_depth === -1) return; // not active
const [_pre, sign, term] = exp;
let result2 = result;
if (sign === "!") result2 = !result2;
const flag = result2 ? " == " : " != ";
trace_report(indent(env) + exp_show(term) + flag);
}
function trace_extn(exp, env) {
if (env.trace_depth === -1) return; // not active
trace_report(indent(env) + exp[1]);
}
function show_line(env) {
let eol = env.pos;
while (env.input[eol] >= " ") eol += 1;
if (eol - env.pos > 2) return show_input(env, env.pos, eol);
return show_input(env, env.pos, env.input.length);
}
function show_input(env, i, j) {
let s = env.input.slice(i, j);
if (s.length > 40) s = `${s.slice(0, 30)} ...`;
return str_esc(s);
}
/**
*
* @param {Env} env
* @returns {string}
*/
function indent(env) {
let s = line_number(env.input, env.pos);
while (s.length < 8) s += " ";
for (let i = env.trace_depth; i < env.depth; i += 1) s += "| ";
return s;
}
// exp decode display ------------------------------------------------
function exp_show(exp) {
if (typeof exp === "string") return exp;
switch (exp[0]) {
case ID:
return exp[2];
case SQ:
return `'${str_esc(exp[2])}'`;
// case DQ: return '"'+str_esc(exp[2])+'"';
case CHS: {
const [_, neg, min, max, str] = exp;
const sign = neg ? "~" : "";
return `${sign}[${str_esc(str)}]${sfx_show(min, max)}`;
}
case PRE: {
const [_pre, sign, term] = exp;
return sign + exp_show(term);
}
case REP: {
const [_rep, min, max, expr] = exp;
return `${exp_show(expr)}${sfx_show(min, max)}`;
}
case ALT: {
const xs = exp[1].map(exp_show);
return `(${xs.join("/")})`;
}
case SEQ: {
const [_, min, max, args] = exp;
const xs = args.map(exp_show);
return `(${xs.join(" ")})${sfx_show(min, max)}`;
}
case EXTN:
return exp[1];
default:
return "(...)";
}
}
function sfx_show(n, m) {
if (n === 0 && m === 0) return "*";
if (n === 0 && m === 1) return "?";
if (n === 1 && m === 0) return "+";
if (n === 1 && m === 1) return "";
if (m === 0) return `*${n}..`;
return `*${n}..${m}`;
}
function str_esc(s) {
let r = "";
for (const c of s) {
if (c >= " ") {
r += c;
} else if (c === "\n") {
r += "\\n";
} else if (c === "\r") {
r += "\\r";
} else if (c === "\t") {
r += "\\t";
} else {
const n = c.charCodeAt(0);
let xxxx = n.toString(16);
while (xxxx.length < 4) xxxx = `0${xxxx}`;
r += `\\u${xxxx}`;
}
}
return r;
}
// compiler -- ptree rules => instruction code ----------------------------
/**
* @param {Array} rules
* @returns {Codex}
*/
function compiler(rules) {
const names = {};
let first;
for (let i = 0; i < rules.length; i += 1) {
const [_rule, [[_id, name], _exp]] = rules[i];
if (i === 0) first = name;
names[name] = i;
}
const start = [ID, 0, first]; // start rule
const code = [];
for (const rule of rules) {
const [_rule, [[_id, name], exp]] = rule;
code.push(emit(exp));
}
for (let i = 0; i < code.length; i += 1) {
optimize(code[i], code);
}
// let space, sp = names["_space_"];
// if (sp) space = code[sp];
return { rules, names, code, start };
function emit(exp) {
// ptree -> [Op, args..]
switch (exp[0]) {
case "id": {
const name = exp[1];
const index = names[name];
if (index === undefined) throw `Undefined rule: ${name}`;
return [ID, index, name];
}
case "alt":
return [ALT, exp[1].map(emit)];
case "seq":
return [SEQ, 1, 1, exp[1].map(emit)];
case "rep": {
// ['rep',[[expn],['sfx', sfx]]]
const [expn, [suffix, sfx]] = exp[1];
const [min, max] = min_max(suffix, sfx);
const expr = emit(expn);
if (expr[0] === SEQ) {
const [_SEQ, n, m, ex] = expr;
if (n === 1 && m === 1) {
return [SEQ, min, max, ex];
}
}
if (expr[0] === CHS) {
const [_CHS, neg, n, m, str] = expr;
if (n === 1 && m === 1) {
return [CHS, neg, min, max, str];
}
}
if (expr[0] === SQ) {
const [_SQ, icase, str] = expr;
if (str.length === 1) {
return [CHS, false, min, max, str];
}
}
return [REP, min, max, expr];
}
case "pre": {
const [[_pfx, pfx], term] = exp[1];
const expr = emit(term);
if (pfx === "~") {
if (expr[0] === SQ) {
const [_SQ, icase, str] = expr;
if (!icase) return [CHS, true, 1, 1, str];
} else if (expr[0] === CHS) {
const [_CHS, neg, min, max, str] = expr;
if (!neg) return [CHS, true, min, max, str];
}
}
return [PRE, pfx, expr];
}
// case "sq": return sq_dq(SQ, exp[1]);
// case "dq": return sq_dq(DQ, exp[1]);
case "sq": {
const txt = exp[1];
const icase = txt.slice(-1) === "i";
let str = icase ? txt.slice(1, -2) : txt.slice(1, -1);
str = escape_codes(str);
if (icase) str = str.toUpperCase();
return [SQ, icase, str];
}
case "chs": {
let str = exp[1].slice(1, -1);
str = escape_codes(str);
return [CHS, false, 1, 1, str];
}
case "extn":
return [EXTN, exp[1]];
default:
throw `Undefined ptree node: ${exp}`;
}
}
function min_max(suffix, sfx) {
// -> [min, max]
// sfx = [+?] / '*' range?
// range = num (dots num?)?
let min = 0;
let max = 0; // for sfx == "*""
if (suffix === "sfx") {
if (sfx === "+") {
min = 1;
} else if (sfx === "?") {
max = 1;
}
} else if (suffix === "num") {
min = Number.parseInt(sfx, 10);
max = min;
} else if (suffix === "range") {
// *N..M
// ["range", [[num, min],["dots", ".."]]]
if (sfx.length === 2) {
// *N..
min = Number.parseInt(sfx[0][1], 10);
} else {
// *N..M
// ["range", [[num, min],["dots", ".."],["num", max]]]
min = Number.parseInt(sfx[0][1], 10);
max = Number.parseInt(sfx[2][1], 10);
}
} else throw `unknown suffix: ${suffix}`;
return [min, max];
}
function optimize(exp, code) {
switch (exp[0]) {
case SEQ: {
// [SEQ, n, m, exps]
for (const ex of exp[3]) optimize(ex, code);
return;
}
case ALT: {
// [ALT, exps]
const exprs = exp[1];
const guards = [];
for (let i = 0; i < exprs.length; i += 1) {
guards.push(first_char(exprs[i], code));
}
exp.push(guards);
// console.log("ALT-guards",exp);
return;
}
default:
return;
}
}
function first_char(exp, code) {
switch (
exp[0] // TODO empty sq return undefined...
) {
case ID: {
return first_char(code[exp[1]], code);
}
case SEQ:
return first_char(exp[3][0], code);
case SQ:
return exp[2][0];
default:
return null;
}
}
} // compiler
const escape_code1 = {
t: "\t",
n: "\n",
r: "\r",
};
function escape_codes(str) {
let s = "";
for (let i = 0; i < str.length; i += 1) {
const c = str[i];
if (c !== "\\") {
s += c;
continue;
}
const x = str[i + 1];
const code = escape_code1[x];
if (code) {
i += 1;
s += code;
continue;
}
if (x === "u" && i + 5 < str.length) {
const hex = str.slice(i + 2, i + 6);
s += String.fromCharCode(Number.parseInt(hex, 16));
i += 5;
continue;
}
if (x === "U" && i + 9 < str.length) {
const hex = str.slice(i + 2, i + 10);
s += String.fromCharCode(Number.parseInt(hex, 16));
i += 9;
continue;
}
s += "\\"; // literal back-slash
}
return s;
}
// -- pretty print ptree ----------------------------------------------
/**
* @param {Array} ptree
* @param {boolean=false} json
* @returns {string}
*/
function show_tree(ptree, json = false) {
if (!ptree) return "";
if (json) return show_json(ptree);
return show_ptree(ptree, 0, 0);
}
function show_ptree(ptree, inset, last) {
// last = int bit map, 1 if after last kid
const res = ptree[0]; // rule name
if (typeof ptree[1] === "string") {
return `${res} "${str_esc(ptree[1])}"`;
}
let bars = "\n"; // indent bar markers
for (let i = 0; i < inset; i += 1) {
bars += ((last >> i) & 1) === 1 ? " " : "\u2502 "; // "| "
}
const kids = ptree[1];
let bod = "";
let tip = "\u251C\u2500"; // |-
let last2 = last;
for (let i = 0; i < kids.length; i += 1) {
if (i === kids.length - 1) {
tip = "\u2514\u2500"; // `-
last2 |= 1 << inset; // bit flag last kid
}
bod += bars + tip + show_ptree(kids[i], inset + 1, last2);
}
return res + bod;
}