-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathLlpgBugsAndSlugs.cs
More file actions
807 lines (781 loc) · 19.6 KB
/
Copy pathLlpgBugsAndSlugs.cs
File metadata and controls
807 lines (781 loc) · 19.6 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Loyc.MiniTest;
namespace Loyc.LLParserGenerator
{
/// <summary>Tests for known slugs (slowness bugs) and fixed bugs (regressions)</summary>
class LlpgBugsAndSlugs : LlpgGeneralTestsBase
{
[Test]
public void Bug_2020_11_SynPredInTokenProblem()
{
Test(@"
@[LL(2), FullLLk(true), Verbosity(1), AddCsLineDirectives(@false)]
LLLPG parser(laType(TokenType), terminalType(Token), matchType(int),
allowSwitch(@true), setType(HashSet!int), castLA(@false));
token TypeSuffixOpt(afterAsOrIs::bool, out dimensionBrack::opt!Token, ref e::LNode)::bool
@{
( t:=TT.QuestionMark
(&!{afterIsOrAs} | &!(TT.Id | etc))
{$result=true;}
)*
};
", @"
bool TypeSuffixOpt(bool afterAsOrIs, out Token? dimensionBrack, ref LNode e)
{
TokenType la0;
bool result = default(bool);
for (;;) {
la0 = LA0;
if (la0 == TT.QuestionMark) {
if (!afterIsOrAs || !Try_TypeSuffixOpt_Test0(1)) {
var t = MatchAny();
// This shouldn't really be here, but it doesn't break correctness
if (!afterIsOrAs) { } else
Check(!Try_TypeSuffixOpt_Test0(0), ""Did not expect(etc | TT.Id)"");
result = true;
}
else
break;
} else
break;
}
return result;
}
private bool Try_TypeSuffixOpt_Test0(int lookaheadAmt)
{
using (new SavePosition(this, lookaheadAmt))
return TypeSuffixOpt_Test0();
}
private bool TypeSuffixOpt_Test0()
{
if (!TryMatch((int)etc, (int)TT.Id))
return false;
return true;
}
");
}
[Test]
public void Bug_2017_08_UnexpectedWarning()
{
// Spurious "It's poor style to put a code block {..} before an and-predicate"
// warning was caused by a synthetic ActionPred inserted by AutoValueSaverVisitor
Test(@"
LLLPG parser(terminalType:Token) {
@[private] rule Rule()::Token @{ &{cond} result:1234 };
}",
@"private Token Rule()
{
Token result = default(Token);
Check(cond, ""Expected cond"");
result = Match(1234);
return result;
}", new SeverityMessageFilter(ConsoleMessageSink.Value, Severity.Warning));
}
[Test(Fails = "TODO: figure out how to implement this properly")]
public void Bug_2017_01_CannotSharePrematchData()
{
// This test checks that the recognizer version of a rule doesn't
// use the same prematch information as the main rule (if it does,
// Scan_XAbc() will incorrectly begin with Skip()).
Test(@"
LLLPG parser(LAType: char, AllowSwitch: @true, SetType: HashSet!int)
{
rule Start @{ XAbc / 'x' &XAbc ('a'|'x')* / 'y' };
@[private] rule XAbc @{ 'x' ('a'|'b'|'c')* };
};",
@"
void Start()
{
char la0;
la0 = (char) LA0;
if (la0 == 'x') {
switch ((char) LA(1)) {
case 'a': case 'b': case 'c': case EOF:
XAbc();
break;
default:
{
Skip();
Check(Try_Scan_XAbc(0), ""Expected XAbc"");
for (;;) {
la0 = (char) LA0;
if (la0 == 'a' || la0 == 'x')
Skip();
else
break;
}
}
break;
}
} else
Match('y');
}
private void XAbc()
{
char la0;
Skip();
for (;;) {
la0 = (char) LA0;
if (la0 == 'a' || la0 == 'b' || la0 == 'c')
Skip();
else
break;
}
}
private bool Try_Scan_XAbc(int lookaheadAmt) {
using (new SavePosition(this, lookaheadAmt))
return Scan_XAbc();
}
private bool Scan_XAbc()
{
char la0;
if (!TryMatch('x'))
return false;
for (;;) {
la0 = (char) LA0;
if (la0 == 'a' || la0 == 'b' || la0 == 'c')
Skip();
else
break;
}
return true;
}");
}
[Test]
public void Bug_2017_01_ErrorBranchCausesIncorrectRecognizer()
{
Test(@"
LLLPG lexer {
rule Digit(x::int) @{ &OddDigit '0'..'9' };
rule OddDigit(x::int) @{ '1'|'3'|'5'|'7'|'9' | '-' '1'| error {Error();} };
};",
@"
void Digit(int x)
{
Check(Try_Scan_OddDigit(0), ""Expected OddDigit"");
MatchRange('0', '9');
}
void OddDigit(int x)
{
switch (LA0) {
case '1': case '3': case '5': case '7':
case '9':
Skip();
break;
case '-':
{Skip();
Match('1');}
break;
default:
{Error();}
break;
}
}
bool Try_Scan_OddDigit(int lookaheadAmt, int x) {
using (new SavePosition(this, lookaheadAmt))
return Scan_OddDigit(x);
}
bool Scan_OddDigit(int x)
{
switch (LA0) {
case '1': case '3': case '5': case '7':
case '9':
Skip();
break;
case '-':
{
Skip();
if (!TryMatch('1'))
return false;
}
break;
default:
return false;
}
return true;
}");
Test(@"
LLLPG lexer {
rule Digit(x::int) @{ &OddDigit '0'..'9' };
rule OddDigit(x::int) @{ '1'|'3'|'5'|'7'|'9'|error {Error();} };
};",
@"
void Digit(int x)
{
Check(Try_Scan_OddDigit(0), ""Expected OddDigit"");
MatchRange('0', '9');
}
void OddDigit(int x)
{
switch (LA0) {
case '1': case '3': case '5': case '7':
case '9':
Skip();
break;
default:
{Error();}
break;
}
}
bool Try_Scan_OddDigit(int lookaheadAmt, int x) {
using (new SavePosition(this, lookaheadAmt))
return Scan_OddDigit(x);
}
bool Scan_OddDigit(int x)
{
switch (LA0) {
case '1': case '3': case '5': case '7':
case '9':
Skip();
break;
default:
return false;
}
return true;
}");
// NOTE: This is the Scan_OddDigit that we would prefer to get from the
// previous test - this would be the result of eliminating the error branch
// when generating the recognizer. But that's not how LLLPG is implemented.
/*
static readonly HashSet<int> Scan_OddDigit_set0 = NewSet('1', '3', '5', '7', '9');
bool Try_Scan_OddDigit(int lookaheadAmt, int x) {
using (new SavePosition(this, lookaheadAmt))
return Scan_OddDigit(x);
}
bool Scan_OddDigit(int x)
{
if (!TryMatch(Scan_OddDigit_set0))
return false;
return true;
}
*/
}
public void Bug_2016_11()
{
// Originally SLComment would do `if (la1 == -1 || la1 == '\\') goto stop;`,
// needlessly checking for EOF. Fixed by editing `ComputeNextSets()` to call
// `ComputeNextSet(..., addEOF: false)` not `addEOF: previous[i].Alt == ExitAlt`.
// This caused a few other EOF checks to disappear from the test suite and
// elsewhere, but it seems like a reasonable change.
Test(@"
[FullLLk, AddCsLineDirectives(false)]
LLLPG (lexer) @{
private token SLComment returns[object result] :
'/' '/' nongreedy(_)* ('\\' '\\' | ('\r'|'\n'|EOF) =>);
};", @"
private object SLComment()
{
int la0, la1;
Match('/');
Match('/');
for (;;) {
switch (LA0) {
case '\\':
{
la1 = LA(1);
if (la1 == '\\')
goto stop;
else
Skip();
}
break;
case -1: case '\n': case '\r':
goto stop;
default:
Skip();
break;
}
}
stop:;
la0 = LA0;
if (la0 == '\\') {
Skip();
Match('\\');
} else { }
}", null, Ecs.EcsLanguageService.Value);
}
[Test]
public void Regression_2016_10()
{
// Regression: while turning off hoisting by default for semantic &{...}
// predicates, hoisting was accidentally turned off for syntactic &(...)
// predicates too (even though the latter doesn't currently expose any way
// to do that in code)
Test(@"
@[AddCsLineDirectives(false)] LLLPG(lexer);
@[private] rule Letter @{ 'a'..'z' };
@[private] rule Word @{ Letter+ };
@[private] rule XCode @{ &('x' Letter Letter) Letter+ };
@[LL(1)]
rule Choice @{ XCode / Word };",
@"
private void Letter() {
MatchRange('a', 'z');
}
private bool Scan_Letter() {
if (!TryMatchRange('a', 'z'))
return false;
return true;
}
private void Word() {
int la0;
Letter();
for (;;) {
la0 = LA0;
if (la0 >= 'a' && la0 <= 'z')
Letter();
else
break;
}
}
private void XCode() {
int la0;
Letter();
for (;;) {
la0 = LA0;
if (la0 >= 'a' && la0 <= 'z')
Letter();
else
break;
}
}
void Choice() {
if (Try_XCode_Test0(0))
XCode();
else
Word();
}
private bool Try_XCode_Test0(int lookaheadAmt) {
using (new SavePosition(this, lookaheadAmt))
return XCode_Test0();
}
private bool XCode_Test0() {
if (!TryMatch('x'))
return false;
if (!Scan_Letter())
return false;
if (!Scan_Letter())
return false;
return true;
}");
}
[Test]
public void Regression_2016_05()
{
// The bug here was thought to relate to `error`, but it turned out
// that in EC#, 'token' was treated as 'rule' if there's a return value.
Test(@"
[AddCsLineDirectives(false)]
LLLPG (lexer);
public override token TT NextToken() @{
( '=' '=' {$result = TT.Eq;}
/ '=' {$result = TT.Assign;}
| error _? {$result = TT.Invalid;}
)
}
", @"
public override TT NextToken()
{
int la0, la1;
TT result = default(TT);
la0 = LA0;
if (la0 == '=') {
la1 = LA(1);
if (la1 == '=') {
Skip();
Skip();
result = TT.Eq;
} else {
Skip();
result = TT.Assign;
}
} else {
la0 = LA0;
if (la0 != -1)
Skip();
result = TT.Invalid;
}
return result;
}", null, Ecs.EcsLanguageService.Value);
}
[Test(Fails = "Haven't decided how to fix this yet")]
public void Regression_2016_03_InappropriateSkip()
{
Test(@"LLLPG (parser(terminalType: Token, laType: TT, matchType: int, castLa: @false)) {
rule X()::LNode @{
D | TT.Tilde TT.Literal
};
@[private] rule D()::LNode @{
tilde:=TT.Tilde
( n=(TT.Id|TT.ContextualKeyword)
| n=TT.This
)
};
};", @"
LNode X()
{
TT la1;
la1 = LA(1);
if (la1 == TT.ContextualKeyword || la1 == TT.Id || la1 == TT.This)
D();
else {
Match((int) TT.Tilde);
Match((int) TT.Literal);
}
}
LNode D()
{
var tilde = Match((int) TT.Tilde);
TODO; // bug: Skip() here, should be... if-else? MatchAny? not Skip anyway
}
");
}
[Test(Fails = "Unsure how to fix. Considering a major rewrite to make core engine easier to reason about.")]
public void Regression_2016_01_AndPredBug()
{
Test(@"LLLPG (parser(terminalType: Token, laType: TT)) {
token AndPredBug() @{
[ t:TT.QuestionMark (&{flagA} | &{flagB}) ]?
};
}", @"
void AndPredBug()
{
TT la0;
Token t = default(Token);
la0 = (TT) LA0;
if (la0 == TT.QuestionMark) {
if (flagA || flagB)
t = MatchAny();
}
}");
Test(@"LLLPG (parser) {
token AndPredBug()
@{
[ t:=TT.QuestionMark (&{flagA} | &{flagB})
{Action();}
]*
};
}", @"
void AndPredBug()
{
TokenType la0, la1;
for (;;) {
la0 = LA0;
if (la0 == TT.QuestionMark) {
la1 = LA(1);
if (la1 == TT.QuestionMark) {
if (flagA || flagB)
goto match1;
else
break;
} else {
if (flagA || flagB)
goto match1;
else
break;
}
} else
break;
match1:
{
var t = MatchAny();
Action();
}
}
}");
}
[Test] public void Regression_2012_02()
{
// 2014-2-05: Weird threading bug while adding --timeout option
// The bug was that StageOneParser.ReclassifyTokens was called twice on
// the same tokens. Mysteriously, this caused parsing errors only when
// StageOneParser ran on an a worker thread (i.e. for --timeout=i, i != 0)
Test(@"LLLPG lexer { rule Foo @{ _ greedy('g')* _ }; }",
@"void Foo() {
int la0, la1;
MatchExcept();
for(;;) {
la0 = LA0;
if (la0=='g') {
la1 = LA(1);
if (la1 != -1)
Skip();
else
break;
} else
break;
}
MatchExcept();
}");
}
[Test] public void Regression_2013_12_LI_LA()
{
// 2013-12-01: Regression test: $LI and $LA were not replaced inside call targets or attributes
Test(@"LLLPG parser {
rule Foo() @{ &!{LA($LI) == $LI} &{$LI() && Bar($LA())} &{@[Foo($LA)] $LI} _ };
}", @"void Foo()
{
Check(!(LA(0) == 0), ""Did not expect LA($LI) == $LI"");
Check(0() && Bar(((int)LA0)()), ""Expected $LI() && Bar($LA())"");
Check($LI, ""Expected $LI"");
MatchExcept();
}");
}
[Test]
public void Regression_2013_12_Misc()
{
// 2013-12-22: I really thought by now that I had found most of the
// bugs, but this example exposed two separate bugs:
// 1. {Money();} was dropped by Alts constructor, as the inner and
// outer Alts were merged and should not have been.
// 2. GenerateExtraMatchingCode() didn't add "break;" before "match1:"
Test(@"
LLLPG lexer {
rule Test @{
({Money();} ('$' {Dollar();} | '#' {Pound();}))?
'$'
};
}", @"
void Test()
{
int la0, la1;
do {
la0 = LA0;
if (la0 == '$') {
la1 = LA(1);
if (la1 == '$')
goto match1;
} else if (la0 == '#')
goto match1;
break;
match1: {
Money();
la0 = LA0;
if (la0 == '$') {
Skip();
Dollar();
} else {
Match('#');
Pound();
}
}
} while(false);
Match('$');
}");
// 2013-12-22: A variation on the same bug in GenerateExtraMatchingCode
Test(@"
LLLPG lexer {
rule Test @{
( 'a'..'b' 'c'
| 'b'..'c' 'a'
)? '$'
};
}",
@" void Test()
{
int la0, la1;
do {
la0 = LA0;
if (la0 == 'b') {
la1 = LA(1);
if (la1 == 'c')
goto match1;
else
goto match2;
} else if (la0 == 'a')
goto match1;
else if (la0 == 'c')
goto match2;
break;
match1:
{
Skip();
Match('c');
}
break;
match2:
{
Skip();
Match('a');
}
} while (false);
Match('$');
}");
}
[Test] public void Regression_2013_12_NullReferenceException()
{
// This grammar used to crash LLLPG with a NullReferenceException.
// The output doesn't seem quite right; probably because of the left recursion.
Test(@"@[FullLLk] LLLPG parser(laType(TT), matchType(int), allowSwitch(@true), castLA(@false)) {
private rule Atom @{
TT.Id (TT.LParen TT.RParen)?
};
token Expr @{
greedy(
Atom
| &{foo}
greedy(Expr)+
)*
};
}", // Output changed 2013-12-21; doesn't matter because grammar is invalid.
@" private void Atom()
{
TT la0;
Skip();
la0 = LA0;
if (la0 == TT.LParen) {
Skip();
Match((int) TT.RParen);
}
}
void Expr()
{
TT la0, la1;
for (;;) {
la0 = LA0;
if (la0 == TT.Id) {
la1 = LA(1);
if (la1 == TT.Id || la1 == TT.LParen)
Atom();
else
break;
} else
break;
}
}
",
TraceMessageSink.Value); // Suppress warnings caused by this test
}
[Test] public void SlugTest1()
{
// [2013-12-25]
// It became very clear while writing the EC# grammar that large,
// complex, ambiguous grammars could make LLLPG run very slowly, but
// this was the first SMALL grammar I could find that would make LLLPG
// run very slowly (25 seconds to analyze 'Start' with k=2!).
Test(@"@[DefaultK(2), FullLLk(false)]
LLLPG lexer {
rule PositiveDigit @{ '1'..'9' {""Think positive!""} };
rule WeirdDigit @{ '0' | &{@[Hoist] a} '1' | &{@[Hoist] b} '2' | &{@[Hoist] c} '3'
| &{@[Hoist] d} '4' | &{@[Hoist] e} '5' | &{@[Hoist] f} '6' | &{@[Hoist] g} '7'
| &{@[Hoist] h} '8' | &{@[Hoist] i} '9' };
rule Start @{ (WeirdDigit / PositiveDigit)* };
}",
@"void PositiveDigit()
{
MatchRange('1', '9');
""Think positive!"";
}
void WeirdDigit()
{
switch (LA0) {
case '0': Skip(); break;
case '1': { Check(a, ""Expected a""); Skip(); } break;
case '2': { Check(b, ""Expected b""); Skip(); } break;
case '3': { Check(c, ""Expected c""); Skip(); } break;
case '4': { Check(d, ""Expected d""); Skip(); } break;
case '5': { Check(e, ""Expected e""); Skip(); } break;
case '6': { Check(f, ""Expected f""); Skip(); } break;
case '7': { Check(g, ""Expected g""); Skip(); } break;
case '8': { Check(h, ""Expected h""); Skip(); } break;
default: { Check(i, ""Expected i""); Match('9'); } break;
}
}
void Start()
{
int la0;
for (;;) {
la0 = LA0;
if (la0 >= '1' && la0 <= '9') {
if (a || b || c || d || e || f || g || h || i)
WeirdDigit();
else
PositiveDigit();
} else if (la0 == '0')
WeirdDigit();
else
break;
}
}");
}
[Test] public void SlugTest2()
{
// This example is just a slight variation on the first, but it is
// more difficult to fix.
//
// This takes over 15 seconds to analyze 'Start' with k=2, and
// 8 minutes and 20 seconds (and almost 2 GB memory) for k=3!).
//
// Each branch of WeirdDigit that overlaps PositiveDigit and has an
// &and-predicate increases CPU time and memory by a factor of four.
// FullLLk(false) is required to cause the slug; with FullLLk(true),
// processing time drops dramatically but the output is nearly seven
// times larger.
Test(@"@[DefaultK(2), FullLLk(false)] //@[Verbosity(3)]
LLLPG lexer {
rule PositiveDigit @{ '1'..'9' {""Think positive!""} };
rule WeirdDigit @{ '0' | &{@[Hoist] a} '1' | &{@[Hoist] b} '2' | &{@[Hoist] c} '3'
| &{@[Hoist] d} '4' | &{@[Hoist] e} '5' | &{@[Hoist] f} '6' | &{@[Hoist] g} '7'
| &{@[Hoist] h} '8' | &{@[Hoist] i} '9' };
rule Start @{ WeirdDigit+ / PositiveDigit+ };
};",
@"void PositiveDigit()
{
MatchRange('1', '9');
""Think positive!"";
}
void WeirdDigit()
{
switch (LA0) {
case '0': Skip(); break;
case '1': { Check(a, ""Expected a""); Skip(); } break;
case '2': { Check(b, ""Expected b""); Skip(); } break;
case '3': { Check(c, ""Expected c""); Skip(); } break;
case '4': { Check(d, ""Expected d""); Skip(); } break;
case '5': { Check(e, ""Expected e""); Skip(); } break;
case '6': { Check(f, ""Expected f""); Skip(); } break;
case '7': { Check(g, ""Expected g""); Skip(); } break;
case '8': { Check(h, ""Expected h""); Skip(); } break;
default: { Check(i, ""Expected i""); Match('9'); } break;
}
}
void Start()
{
int la0;
do {
la0 = LA0;
if (la0 >= '1' && la0 <= '9') {
if (a || b || c || d || e || f || g || h || i)
goto matchWeirdDigit;
else {
PositiveDigit();
for (;;) {
la0 = LA0;
if (la0 >= '1' && la0 <= '9')
PositiveDigit();
else
break;
}
}
} else
goto matchWeirdDigit;
break;
matchWeirdDigit:
{
WeirdDigit();
for (;;) {
la0 = LA0;
if (la0 >= '0' && la0 <= '9')
WeirdDigit();
else
break;
}
}
} while (false);
}");
}
}
}