-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathintrospection.d
620 lines (565 loc) · 21.9 KB
/
introspection.d
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
/**
This module contains function to inspect a Pegged grammar.
*/
module pegged.introspection;
import std.algorithm : equal;
import std.typecons;
import pegged.parser;
@safe:
/**
The different kinds of recursion for a rule.
'direct' means the rule name appears in its own definition. 'indirect' means the rule calls itself through another rule (the call chain can be long).
*/
enum Recursive { no, direct, indirect }
/**
Left-recursion diagnostic for a rule. A rule is left-recursive when its own name appears at the beginning of its definition or behind possibly-null-matching rules (see below for null matches).
For example A <- A 'a' is left-recursive, whereas A <- 'a' A is not. *But* A <- 'a'? A is left-recursive, since if the input does not begin
with 'a', then the parsing will continue by invoking A again, at the same position.
'direct' means the rule invokes itself in the first position of its definition (A <- A ...). 'hidden' means the rule names appears after a possibly-null other rule (A <- 'a'? A ...). 'indirect' means the rule calls itself trough another rule.
*/
enum LeftRecursive { no, direct, hidden, indirect }
/**
NullMatch.yes means a rule can succeed while consuming no input. For example e? or e*, for all expressions e.
Nullmatch.no means a rule will always consume at least a token while succeeding.
Nullmatch.indeterminate means the algorithm could not converge.
*/
enum NullMatch { no, yes, indeterminate }
/**
InfiniteLoop.yes means a rule can loop indefinitely while consuming nothing.
InfiniteLoop.no means a rule cannot loop indefinitely.
InfiniteLoop.indeterminate means the algorithm could not converge.
*/
enum InfiniteLoop { no, yes, indeterminate }
/**
Struct holding the introspection info on a rule.
*/
struct RuleInfo
{
Recursive recursion; /// Is the rule recursive?
LeftRecursive leftRecursion; /// Is the rule left-recursive?
NullMatch nullMatch; /// Can the rule succeed while consuming nothing?
InfiniteLoop infiniteLoop; /// Can the rule loop indefinitely, while consuming nothing?
string[] leftRecursiveCycle; /// The path of rules traversed before indirect left-recursion recurses.
}
/**
Struct holding the introspection info on a grammar.
*/
struct GrammarInfo
{
RuleInfo[string] ruleInfo;
immutable(string[])[] leftRecursiveCycles;
}
pure void appendCycleIfUnique(ref immutable(string[])[] cycles, const string[] cycle)
{
bool exists()
{
import std.algorithm : countUntil;
foreach (c; cycles)
{
if (c.length != cycle.length)
continue;
auto pos = countUntil(c, cycle[0]);
if (pos < 0)
continue;
if (equal!equal(c[pos .. $] ~ c[0 .. pos], cycle))
return true;
}
return false;
}
if (!exists())
cycles ~= cycle.idup;
}
/**
Returns for all grammar rules:
- the recursion type (no recursion, direct or indirect recursion).
- the left-recursion type (no left-recursion, direct left-recursion, hidden, or indirect)
- the null-match for a grammar's rules: whether the rule can succeed while consuming nothing.
- the possibility of an infinite loop (if 'e' can null-match, then 'e*' can enter an infinite loop).
This kind of potential problem can be detected statically and should be transmitted to the grammar designer.
*/
pure GrammarInfo grammarInfo(ParseTree p)
{
if (p.name == "Pegged")
return grammarInfo(p.children[0]);
assert(p.name == "Pegged.Grammar");
GrammarInfo result;
ParseTree[string] rules;
/**
Returns the call graph of a grammar: the list of rules directly called by each rule of the grammar.
The graph is represented as a bool[string][string] associative array, the string holding
the rules names. graph["ruleName"] contains all rules called by ruleName, as a set (a bool[string] AA).
graph.keys thus gives the grammar's rules names.
If a rule calls itself, its own name will appear in the called set. If a rule calls an external rule, it will
also appear in the call graph when the rule has a name: hence, calls to predefined rules like 'identifier' or
'digit' will appear, but not a call to '[0-9]+', considered here as an anonymous rule.
*/
bool[string][string] callGraph(ParseTree p) @safe
{
bool[string] findIdentifiers(ParseTree p) @safe
{
bool[string] idList;
if (p.name == "Pegged.Identifier")
idList[p.matches[0]] = true;
else
foreach(child; p.children)
{
auto ids = findIdentifiers(child);
static if (hasSystemAAKeys)
auto keys = () @trusted { return ids.keys; } ();
else
auto keys = ids.keys;
foreach(name; keys)
idList[name] = true;
}
return idList;
}
bool[string][string] graph;
foreach(definition; p.children)
if (definition.name == "Pegged.Definition")
{
auto ids = findIdentifiers(definition.children[2]);
graph[definition.matches[0]] = ids;
foreach(id, _; ids) // getting possible external calls
if (id !in graph)
graph[id] = (bool[string]).init;
}
return graph;
}
/**
The transitive closure of a call graph.
It will propagate the calls to find all rules called by a given rule,
directly (already in the call graph) or indirectly (through another rule).
*/
bool[string][string] closure(bool[string][string] graph) @safe
{
bool[string][string] path;
foreach(rule, children; graph) // deep-dupping, to avoid children aliasing
path[rule] = children.dup;
bool changed = true;
while(changed)
{
changed = false;
static if (hasSystemAAKeys)
auto keys = () @trusted { return graph.keys; } ();
else
auto keys = graph.keys;
foreach(rule1; keys)
foreach(rule2; keys)
if (rule2 in path[rule1])
foreach(rule3; keys)
if (rule3 in path[rule2] && rule3 !in path[rule1])
{
path[rule1][rule3] = true;
changed = true;
}
}
return path;
}
Recursive[string] recursions(bool[string][string] graph) @safe
{
bool[string][string] path = closure(graph);
Recursive[string] result;
foreach(rule, children; path)
{
result[rule] = Recursive.no;
if (rule in children)
{
if (rule in graph[rule])
result[rule] = Recursive.direct;
else
result[rule] = Recursive.indirect;
}
}
return result;
}
NullMatch nullMatching(ParseTree p) @safe
{
switch (p.name)
{
case "Pegged.Expression":
return nullMatching(p.children[0]);
case "Pegged.FirstExpression",
"Pegged.LongestExpression": // choice expressions null-match whenever one of their components can null-match
foreach(seq; p.children)
if (nullMatching(seq) == NullMatch.yes)
return NullMatch.yes;
foreach(seq; p.children)
if (nullMatching(seq) == NullMatch.indeterminate)
return NullMatch.indeterminate;
return NullMatch.no;
case "Pegged.Sequence": // sequence expressions can null-match when all their components can null-match
foreach(pref; p.children)
if (nullMatching(pref) == NullMatch.no)
return NullMatch.no;
foreach(pref; p.children)
if (nullMatching(pref) == NullMatch.indeterminate)
return NullMatch.indeterminate;
return NullMatch.yes;
case "Pegged.Prefix":
foreach(pref; p.children[0..$-1])
if (pref.name == "Pegged.POS" || pref.name == "Pegged.NEG")
return NullMatch.yes;
return nullMatching(p.children[$-1]);
case "Pegged.Suffix":
foreach(suff; p.children[1..$])
if (suff.name == "Pegged.ZEROORMORE" || suff.name == "Pegged.OPTION")
return NullMatch.yes;
return nullMatching(p.children[0]);
case "Pegged.Primary":
return nullMatching(p.children[0]);
case "Pegged.RhsName":
if (p.matches[0] in result.ruleInfo)
if (result.ruleInfo[p.matches[0]].nullMatch != NullMatch.indeterminate)
return result.ruleInfo[p.matches[0]].nullMatch;
return nullMatching(p.children[0]);
case "Pegged.Identifier":
if (p.matches[0] == "eps" ||
p.matches[0] == "eoi")
return NullMatch.yes;
return NullMatch.indeterminate;
case "Pegged.Literal":
if (p.matches[0].length == 0) // Empty literal, '' or ""
return NullMatch.yes;
else
return NullMatch.no;
case "Pegged.CharClass":
case "Pegged.ANY":
return NullMatch.no;
case "eps":
case "eoi":
return NullMatch.yes;
default:
return NullMatch.indeterminate;
}
}
InfiniteLoop infiniteLooping(ParseTree p) @safe
{
switch (p.name)
{
case "Pegged.Expression": // choice expressions loop whenever one of their components can loop
case "Pegged.Sequence": // sequence expressions can loop when one of their components can loop
foreach(seq; p.children)
{
auto nm = infiniteLooping(seq);
if (nm == InfiniteLoop.yes)
return InfiniteLoop.yes;
if (nm == InfiniteLoop.indeterminate)
return InfiniteLoop.indeterminate;
}
return InfiniteLoop.no;
case "Pegged.Prefix":
return infiniteLooping(p.children[$-1]);
case "Pegged.Suffix":
foreach(pref; p.children[1..$])
if (( pref.name == "Pegged.ZEROORMORE" || pref.name == "Pegged.ONEORMORE")
&& p.matches[0] in result.ruleInfo
&& result.ruleInfo[p.matches[0]].nullMatch == NullMatch.yes)
return InfiniteLoop.yes;
return infiniteLooping(p.children[0]);
case "Pegged.Primary":
return infiniteLooping(p.children[0]);
case "Pegged.RhsName":
if (p.matches[0] in result.ruleInfo)
return result.ruleInfo[p.matches[0]].infiniteLoop;
else
return infiniteLooping(p.children[0]);
case "Pegged.Literal":
case "Pegged.CharClass":
case "Pegged.ANY":
case "eps":
case "eoi":
return InfiniteLoop.no;
default:
return InfiniteLoop.indeterminate;
}
}
LeftRecursive leftRecursion(ParseTree p, ref string[] cycle) @safe
{
import std.algorithm.searching: countUntil;
switch (p.name)
{
case "Pegged.Expression":
return leftRecursion(p.children[0], cycle);
case "Pegged.FirstExpression",
"Pegged.LongestExpression": // Choices are left-recursive if any choice is left-recursive
// Because memoized left-recursion handling needs to know about all left-recursive cycles,
// we consider all choices, not just one.
auto any_lr = LeftRecursive.no;
size_t current = cycle.length;
foreach(seq; p.children)
{
cycle = cycle[0..current];
auto lr = leftRecursion(seq, cycle);
if (lr != LeftRecursive.no && any_lr == LeftRecursive.no)
any_lr = lr;
}
cycle = cycle[0..current];
return any_lr;
case "Pegged.Sequence": // Sequences are left-recursive when the leftmost member is left-recursive
// or behind null-matching members
size_t current = cycle.length;
foreach(i, seq; p.children)
{
auto lr = leftRecursion(seq, cycle);
if (lr == LeftRecursive.direct)
return (i == 0 ? LeftRecursive.direct : LeftRecursive.hidden);
if (lr == LeftRecursive.hidden || lr == LeftRecursive.indirect)
return lr;
if (nullMatching(seq) == NullMatch.yes)
continue;
else
{
cycle = cycle[0..current];
return LeftRecursive.no;
}
}
cycle = cycle[0..current];
return LeftRecursive.no; // found only null-matching rules!
case "Pegged.Prefix":
return leftRecursion(p.children[$-1], cycle);
case "Pegged.Suffix":
case "Pegged.Primary":
return leftRecursion(p.children[0], cycle);
case "Pegged.RhsName":
auto dejavu = cycle.countUntil(p.matches[0]);
if (dejavu >= 0)
{
if (cycle.length == 1)
{
result.leftRecursiveCycles.appendCycleIfUnique(cycle);
return LeftRecursive.direct;
}
// Strictly spoken, the rules before dejavu are not actively left-recursive, but since they
// can call into a left-recursive cycle, they would still left-recurse if left-recursion
// wasn't handled.
result.leftRecursiveCycles.appendCycleIfUnique(cycle[dejavu..$]);
return LeftRecursive.indirect;
}
size_t current = cycle.length;
cycle ~= p.matches[0];
if ((p.matches[0] in rules) && (leftRecursion(rules[p.matches[0]], cycle) != LeftRecursive.no))
return LeftRecursive.indirect;
cycle = cycle[0..current];
return LeftRecursive.no;
default:
return LeftRecursive.no;
}
}
// Initialize rules and result.
foreach(definition; p.children)
if (definition.name == "Pegged.Definition")
{
rules[definition.matches[0]] = definition.children[2];
result.ruleInfo[definition.matches[0]] = RuleInfo(Recursive.no, LeftRecursive.no,
NullMatch.indeterminate, InfiniteLoop.indeterminate);
}
// Detect recursions.
foreach(rule, recursionType; recursions(callGraph(p)))
if (rule in result.ruleInfo) // external rules are in recursions, but not in result
result.ruleInfo[rule].recursion = recursionType;
// Detect left-recursions.
foreach(name, tree; rules)
if (result.ruleInfo[name].recursion != Recursive.no)
{
result.ruleInfo[name].leftRecursiveCycle ~= name;
result.ruleInfo[name].leftRecursion = leftRecursion(tree, result.ruleInfo[name].leftRecursiveCycle);
}
// Detect null matches.
bool changed = true;
while(changed) // while something new happened, the process is not over
{
changed = false;
foreach(name, tree; rules)
if (result.ruleInfo[name].nullMatch == NullMatch.indeterminate) // not done yet
{
result.ruleInfo[name].nullMatch = nullMatching(tree); // try to find if it's null-matching
if (result.ruleInfo[name].nullMatch != NullMatch.indeterminate)
changed = true;
}
}
// Detect infinite loops.
changed = true;
while(changed) // while something new happened, the process is not over
{
changed = false;
foreach(name, tree; rules)
if (result.ruleInfo[name].infiniteLoop == InfiniteLoop.indeterminate) // not done yet
{
result.ruleInfo[name].infiniteLoop = infiniteLooping(tree); // try to find if it's looping
if (result.ruleInfo[name].infiniteLoop != InfiniteLoop.indeterminate)
changed = true;
}
}
return result;
}
/**
Returns for all grammar rules:
- the recursion type (no recursion, direct or indirect recursion).
- the left-recursion type (no left-recursion, direct left-recursion, hidden, or indirect)
- the null-match for a grammar's rules: whether the rule can succeed while consuming nothing.
- the possibility of an infinite loop (if 'e' can null-match, then 'e*' can enter an infinite loop).
This kind of potential problem can be detected statically and should be transmitted to the grammar designer.
*/
pure RuleInfo[string] ruleInfo(ParseTree p)
{
return grammarInfo(p).ruleInfo;
}
/** ditto */
RuleInfo[string] ruleInfo(string grammar)
{
return ruleInfo(Pegged(grammar).children[0]);
}
unittest
{
auto info = ruleInfo(`
Test:
A <- A 'a'
`);
assert(info["A"].leftRecursion == LeftRecursive.direct);
info = ruleInfo(`
Test:
A <- B? A 'a'
B <- 'b'
`);
assert(info["A"].leftRecursion == LeftRecursive.hidden);
info = ruleInfo(`
Test:
A <- B 'a'
B <- A
`);
assert(info["A"].leftRecursion == LeftRecursive.indirect);
}
// Test against infinite recursion in detection of indirect left-recursion.
unittest
{
auto info = ruleInfo(`
Test:
A <- B / C 'a'
B <- A
C <- A
`);
assert(info["A"].leftRecursion == LeftRecursive.indirect);
}
// Test against compile-time infinite recursion.
unittest // Mutual left-recursion
{
enum ct = ruleInfo(`
Left:
A <- L
L <- P
P <- P / L
`);
static assert(ct["A"].leftRecursion == LeftRecursive.no);
static assert(ct["L"].leftRecursion == LeftRecursive.indirect);
static assert(ct["P"].leftRecursion == LeftRecursive.direct);
auto rt = ruleInfo(`
Left:
A <- L
L <- P
P <- P / L
`);
assert(rt["A"].leftRecursion == LeftRecursive.no);
assert(rt["L"].leftRecursion == LeftRecursive.indirect);
assert(rt["P"].leftRecursion == LeftRecursive.direct);
}
unittest // Intersecting cycles of left-recursion
{
enum ct = ruleInfo(`
Left:
C <- A
A <- B* C
B <- A
`);
static assert(ct["C"].leftRecursion == LeftRecursive.indirect);
static assert(ct["A"].leftRecursion == LeftRecursive.indirect);
static assert(ct["B"].leftRecursion == LeftRecursive.indirect);
auto rt = ruleInfo(`
Left:
C <- A
A <- B* C
B <- A
`);
assert(rt["C"].leftRecursion == LeftRecursive.indirect);
assert(rt["A"].leftRecursion == LeftRecursive.indirect);
assert(rt["B"].leftRecursion == LeftRecursive.indirect);
}
unittest // Null-matching
{
enum ct = ruleInfo(`
NM:
NMM <- NML eoi
NML <- 'x'?
`);
static assert(ct["NML"].nullMatch == NullMatch.yes);
static assert(ct["NMM"].nullMatch == NullMatch.yes);
auto rt = ruleInfo(`
NM:
NMM <- NML eoi
NML <- 'x'?
`);
assert(rt["NML"].nullMatch == NullMatch.yes);
assert(rt["NMM"].nullMatch == NullMatch.yes);
}
unittest // Not null-matching
{
enum ct = ruleInfo(`
Left:
M <- L eoi
L <- P '.x' / 'x'
P <- P '(n)' / L
`);
static assert(ct["M"].nullMatch == NullMatch.no);
static assert(ct["L"].nullMatch == NullMatch.no);
static assert(ct["P"].nullMatch == NullMatch.no);
auto rt = ruleInfo(`
Left:
M <- L eoi
L <- P '.x' / 'x'
P <- P '(n)' / L
`);
assert(rt["M"].nullMatch == NullMatch.no);
assert(rt["L"].nullMatch == NullMatch.no);
assert(rt["P"].nullMatch == NullMatch.no);
}
unittest // Left-recursive null-matching
{
enum ct = ruleInfo(`
Left:
M <- L eoi
L <- P? '.x'? / 'x'
P <- P '(n)' / L
`);
static assert(ct["M"].nullMatch == NullMatch.yes);
static assert(ct["L"].nullMatch == NullMatch.yes);
static assert(ct["P"].nullMatch == NullMatch.yes);
auto rt = ruleInfo(`
Left:
M <- L eoi
L <- P? '.x'? / 'x'
P <- P '(n)' / L
`);
assert(rt["M"].nullMatch == NullMatch.yes);
assert(rt["L"].nullMatch == NullMatch.yes);
assert(rt["P"].nullMatch == NullMatch.yes);
}
/**
Act on rules parse tree as produced by pegged.parser.
Replace every occurence of child in parent by child's parse tree
*/
ParseTree replaceInto(ParseTree parent, ParseTree child)
{
if (parent.name == "Pegged.RhsName" && parent.matches[0] == child.matches[0])
return ParseTree("Pegged.Named", true, child.matches[0..1], "",0,0,
[child.children[2],
ParseTree("Pegged.Identifier", true, child.matches[0..1])]);
else
foreach(ref branch; parent.children)
branch = replaceInto(branch, child);
return parent;
}
/* .keys is @safe after compiler version >= 2.098.0.
*
* See:
* https://dlang.org/changelog/2.098.0.html#bugfix-list and
* https://issues.dlang.org/show_bug.cgi?id=14439 */
enum bool hasSystemAAKeys = __VERSION__ < 2098;