• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2import textwrap
3import antlr3
4import antlr3.tree
5import testbase
6
7class T(testbase.ANTLRTest):
8    def walkerClass(self, base):
9        class TWalker(base):
10            def __init__(self, *args, **kwargs):
11                super().__init__(*args, **kwargs)
12                self.buf = ""
13
14            def traceIn(self, ruleName, ruleIndex):
15                self.traces.append('>'+ruleName)
16
17
18            def traceOut(self, ruleName, ruleIndex):
19                self.traces.append('<'+ruleName)
20
21
22            def recover(self, input, re):
23                # no error recovery yet, just crash!
24                raise
25
26        return TWalker
27
28
29    def execTreeParser(self, grammar, grammarEntry, treeGrammar, treeEntry, input):
30        lexerCls, parserCls = self.compileInlineGrammar(grammar)
31        walkerCls = self.compileInlineGrammar(treeGrammar)
32
33        cStream = antlr3.StringStream(input)
34        lexer = lexerCls(cStream)
35        tStream = antlr3.CommonTokenStream(lexer)
36        parser = parserCls(tStream)
37        r = getattr(parser, grammarEntry)()
38        nodes = antlr3.tree.CommonTreeNodeStream(r.tree)
39        nodes.setTokenStream(tStream)
40        walker = walkerCls(nodes)
41        r = getattr(walker, treeEntry)()
42
43        if r.tree:
44            return r.tree.toStringTree()
45
46        return ""
47
48
49    def testFlatList(self):
50        grammar = textwrap.dedent(
51        r'''
52        grammar T1;
53        options {
54            language=Python3;
55            output=AST;
56        }
57        a : ID INT;
58        ID : 'a'..'z'+ ;
59        INT : '0'..'9'+;
60        WS : (' '|'\\n') {$channel=HIDDEN;} ;
61        ''')
62
63        treeGrammar = textwrap.dedent(
64        r'''
65        tree grammar TP1;
66        options {
67            language=Python3;
68            output=AST;
69            ASTLabelType=CommonTree;
70            tokenVocab=T1;
71        }
72
73        a : ID INT -> INT ID;
74        ''')
75
76        found = self.execTreeParser(
77            grammar, 'a',
78            treeGrammar, 'a',
79            "abc 34"
80            )
81
82        self.assertEqual("34 abc", found)
83
84
85    def testSimpleTree(self):
86        grammar = textwrap.dedent(
87        r'''
88        grammar T2;
89        options {
90            language=Python3;
91            output=AST;
92        }
93        a : ID INT -> ^(ID INT);
94        ID : 'a'..'z'+ ;
95        INT : '0'..'9'+;
96        WS : (' '|'\\n') {$channel=HIDDEN;} ;
97        ''')
98
99        treeGrammar = textwrap.dedent(
100        r'''
101        tree grammar TP2;
102        options {
103            language=Python3;
104            output=AST;
105            ASTLabelType=CommonTree;
106            tokenVocab=T2;
107        }
108        a : ^(ID INT) -> ^(INT ID);
109        ''')
110
111        found = self.execTreeParser(
112            grammar, 'a',
113            treeGrammar, 'a',
114            "abc 34"
115            )
116
117        self.assertEqual("(34 abc)", found)
118
119
120    def testCombinedRewriteAndAuto(self):
121        grammar = textwrap.dedent(
122        r'''
123        grammar T3;
124        options {
125            language=Python3;
126            output=AST;
127        }
128        a : ID INT -> ^(ID INT) | INT ;
129        ID : 'a'..'z'+ ;
130        INT : '0'..'9'+;
131        WS : (' '|'\\n') {$channel=HIDDEN;} ;
132        ''')
133
134        treeGrammar = textwrap.dedent(
135        r'''
136        tree grammar TP3;
137        options {
138            language=Python3;
139            output=AST;
140            ASTLabelType=CommonTree;
141            tokenVocab=T3;
142        }
143        a : ^(ID INT) -> ^(INT ID) | INT;
144        ''')
145
146        found = self.execTreeParser(
147            grammar, 'a',
148            treeGrammar, 'a',
149            "abc 34"
150            )
151
152        self.assertEqual("(34 abc)", found)
153
154
155        found = self.execTreeParser(
156            grammar, 'a',
157            treeGrammar, 'a',
158            "34"
159            )
160
161        self.assertEqual("34", found)
162
163
164    def testAvoidDup(self):
165        grammar = textwrap.dedent(
166        r'''
167        grammar T4;
168        options {
169            language=Python3;
170            output=AST;
171        }
172        a : ID ;
173        ID : 'a'..'z'+ ;
174        INT : '0'..'9'+;
175        WS : (' '|'\\n') {$channel=HIDDEN;} ;
176        ''')
177
178        treeGrammar = textwrap.dedent(
179        r'''
180        tree grammar TP4;
181        options {
182            language=Python3;
183            output=AST;
184            ASTLabelType=CommonTree;
185            tokenVocab=T4;
186        }
187        a : ID -> ^(ID ID);
188        ''')
189
190        found = self.execTreeParser(
191            grammar, 'a',
192            treeGrammar, 'a',
193            "abc"
194            )
195
196        self.assertEqual("(abc abc)", found)
197
198
199    def testLoop(self):
200        grammar = textwrap.dedent(
201        r'''
202        grammar T5;
203        options {
204            language=Python3;
205            output=AST;
206        }
207        a : ID+ INT+ -> (^(ID INT))+ ;
208        ID : 'a'..'z'+ ;
209        INT : '0'..'9'+;
210        WS : (' '|'\\n') {$channel=HIDDEN;} ;
211        ''')
212
213        treeGrammar = textwrap.dedent(
214        r'''
215        tree grammar TP5;
216        options {
217            language=Python3;
218            output=AST;
219            ASTLabelType=CommonTree;
220            tokenVocab=T5;
221        }
222        a : (^(ID INT))+ -> INT+ ID+;
223        ''')
224
225        found = self.execTreeParser(
226            grammar, 'a',
227            treeGrammar, 'a',
228            "a b c 3 4 5"
229            )
230
231        self.assertEqual("3 4 5 a b c", found)
232
233
234    def testAutoDup(self):
235        grammar = textwrap.dedent(
236        r'''
237        grammar T6;
238        options {
239            language=Python3;
240            output=AST;
241        }
242        a : ID ;
243        ID : 'a'..'z'+ ;
244        INT : '0'..'9'+;
245        WS : (' '|'\\n') {$channel=HIDDEN;} ;
246        ''')
247
248        treeGrammar = textwrap.dedent(
249        r'''
250        tree grammar TP6;
251        options {
252            language=Python3;
253            output=AST;
254            ASTLabelType=CommonTree;
255            tokenVocab=T6;
256        }
257        a : ID;
258        ''')
259
260        found = self.execTreeParser(
261            grammar, 'a',
262            treeGrammar, 'a',
263            "abc"
264            )
265
266        self.assertEqual("abc", found)
267
268
269    def testAutoDupRule(self):
270        grammar = textwrap.dedent(
271        r'''
272        grammar T7;
273        options {
274            language=Python3;
275            output=AST;
276        }
277        a : ID INT ;
278        ID : 'a'..'z'+ ;
279        INT : '0'..'9'+;
280        WS : (' '|'\\n') {$channel=HIDDEN;} ;
281        ''')
282
283        treeGrammar = textwrap.dedent(
284        r'''
285        tree grammar TP7;
286        options {
287            language=Python3;
288            output=AST;
289            ASTLabelType=CommonTree;
290            tokenVocab=T7;
291        }
292        a : b c ;
293        b : ID ;
294        c : INT ;
295        ''')
296
297        found = self.execTreeParser(
298            grammar, 'a',
299            treeGrammar, 'a',
300            "a 1"
301            )
302
303        self.assertEqual("a 1", found)
304
305
306    def testAutoWildcard(self):
307        grammar = textwrap.dedent(
308            r'''
309            grammar T;
310            options {language=Python3;output=AST;}
311            a : ID INT ;
312            ID : 'a'..'z'+ ;
313            INT : '0'..'9'+;
314            WS : (' '|'\n') {$channel=HIDDEN;} ;
315            ''')
316
317        treeGrammar = textwrap.dedent(
318            r'''
319            tree grammar TP;
320            options {language=Python3;output=AST; ASTLabelType=CommonTree; tokenVocab=T;}
321            a : ID .
322              ;
323            ''')
324
325        found = self.execTreeParser(
326            grammar, 'a',
327            treeGrammar, 'a',
328            "abc 34")
329        self.assertEqual("abc 34", found)
330
331
332    def testAutoWildcard2(self):
333        grammar = textwrap.dedent(
334            r'''
335            grammar T;
336            options {language=Python3;output=AST;}
337            a : ID INT -> ^(ID INT);
338            ID : 'a'..'z'+ ;
339            INT : '0'..'9'+;
340            WS : (' '|'\n') {$channel=HIDDEN;} ;
341            ''')
342
343        treeGrammar = textwrap.dedent(
344            r'''
345            tree grammar TP;
346            options {language=Python3;output=AST; ASTLabelType=CommonTree; tokenVocab=T;}
347            a : ^(ID .)
348              ;
349            ''')
350
351        found = self.execTreeParser(
352            grammar, 'a',
353            treeGrammar, 'a',
354            "abc 34")
355        self.assertEqual("(abc 34)", found)
356
357
358    def testAutoWildcardWithLabel(self):
359        grammar = textwrap.dedent(
360            r'''
361            grammar T;
362            options {language=Python3;output=AST;}
363            a : ID INT ;
364            ID : 'a'..'z'+ ;
365            INT : '0'..'9'+;
366            WS : (' '|'\n') {$channel=HIDDEN;} ;
367            ''')
368
369        treeGrammar = textwrap.dedent(
370            r'''
371            tree grammar TP;
372            options {language=Python3;output=AST; ASTLabelType=CommonTree; tokenVocab=T;}
373            a : ID c=.
374              ;
375            ''')
376
377        found = self.execTreeParser(
378            grammar, 'a',
379            treeGrammar, 'a',
380            "abc 34")
381        self.assertEqual("abc 34", found)
382
383
384    def testAutoWildcardWithListLabel(self):
385        grammar = textwrap.dedent(
386            r'''
387            grammar T;
388            options {language=Python3;output=AST;}
389            a : ID INT ;
390            ID : 'a'..'z'+ ;
391            INT : '0'..'9'+;
392            WS : (' '|'\n') {$channel=HIDDEN;} ;
393            ''')
394
395        treeGrammar = textwrap.dedent(
396            r'''
397            tree grammar TP;
398            options {language=Python3;output=AST; ASTLabelType=CommonTree; tokenVocab=T;}
399            a : ID c+=.
400              ;
401            ''')
402
403        found = self.execTreeParser(
404            grammar, 'a',
405            treeGrammar, 'a',
406            "abc 34")
407        self.assertEqual("abc 34", found)
408
409
410    def testAutoDupMultiple(self):
411        grammar = textwrap.dedent(
412        r'''
413        grammar T8;
414        options {
415            language=Python3;
416            output=AST;
417        }
418        a : ID ID INT;
419        ID : 'a'..'z'+ ;
420        INT : '0'..'9'+;
421        WS : (' '|'\\n') {$channel=HIDDEN;} ;
422        ''')
423
424        treeGrammar = textwrap.dedent(
425        r'''
426        tree grammar TP8;
427        options {
428            language=Python3;
429            output=AST;
430            ASTLabelType=CommonTree;
431            tokenVocab=T8;
432        }
433        a : ID ID INT
434          ;
435        ''')
436
437        found = self.execTreeParser(
438            grammar, 'a',
439            treeGrammar, 'a',
440            "a b 3"
441            )
442
443        self.assertEqual("a b 3", found)
444
445
446    def testAutoDupTree(self):
447        grammar = textwrap.dedent(
448        r'''
449        grammar T9;
450        options {
451            language=Python3;
452            output=AST;
453        }
454        a : ID INT -> ^(ID INT);
455        ID : 'a'..'z'+ ;
456        INT : '0'..'9'+;
457        WS : (' '|'\\n') {$channel=HIDDEN;} ;
458        ''')
459
460        treeGrammar = textwrap.dedent(
461        r'''
462        tree grammar TP9;
463        options {
464            language=Python3;
465            output=AST;
466            ASTLabelType=CommonTree;
467            tokenVocab=T9;
468        }
469        a : ^(ID INT)
470          ;
471        ''')
472
473        found = self.execTreeParser(
474            grammar, 'a',
475            treeGrammar, 'a',
476            "a 3"
477            )
478
479        self.assertEqual("(a 3)", found)
480
481
482    def testAutoDupTreeWithLabels(self):
483        grammar = textwrap.dedent(
484        r'''
485        grammar T10;
486        options {
487            language=Python3;
488            output=AST;
489        }
490        a : ID INT -> ^(ID INT);
491        ID : 'a'..'z'+ ;
492        INT : '0'..'9'+;
493        WS : (' '|'\\n') {$channel=HIDDEN;} ;
494        ''')
495
496        treeGrammar = textwrap.dedent(
497        r'''
498        tree grammar TP10;
499        options {
500            language=Python3;
501            output=AST;
502            ASTLabelType=CommonTree;
503            tokenVocab=T10;
504        }
505        a : ^(x=ID y=INT)
506          ;
507        ''')
508
509        found = self.execTreeParser(
510            grammar, 'a',
511            treeGrammar, 'a',
512            "a 3"
513            )
514
515        self.assertEqual("(a 3)", found)
516
517
518    def testAutoDupTreeWithListLabels(self):
519        grammar = textwrap.dedent(
520        r'''
521        grammar T11;
522        options {
523            language=Python3;
524            output=AST;
525        }
526        a : ID INT -> ^(ID INT);
527        ID : 'a'..'z'+ ;
528        INT : '0'..'9'+;
529        WS : (' '|'\\n') {$channel=HIDDEN;} ;
530        ''')
531
532        treeGrammar = textwrap.dedent(
533        r'''
534        tree grammar TP11;
535        options {
536            language=Python3;
537            output=AST;
538            ASTLabelType=CommonTree;
539            tokenVocab=T11;
540        }
541        a : ^(x+=ID y+=INT)
542          ;
543        ''')
544
545        found = self.execTreeParser(
546            grammar, 'a',
547            treeGrammar, 'a',
548            "a 3"
549            )
550
551        self.assertEqual("(a 3)", found)
552
553
554    def testAutoDupTreeWithRuleRoot(self):
555        grammar = textwrap.dedent(
556        r'''
557        grammar T12;
558        options {
559            language=Python3;
560            output=AST;
561        }
562        a : ID INT -> ^(ID INT);
563        ID : 'a'..'z'+ ;
564        INT : '0'..'9'+;
565        WS : (' '|'\\n') {$channel=HIDDEN;} ;
566        ''')
567
568        treeGrammar = textwrap.dedent(
569        r'''
570        tree grammar TP12;
571        options {
572            language=Python3;
573            output=AST;
574            ASTLabelType=CommonTree;
575            tokenVocab=T12;
576        }
577        a : ^(b INT) ;
578        b : ID ;
579        ''')
580
581        found = self.execTreeParser(
582            grammar, 'a',
583            treeGrammar, 'a',
584            "a 3"
585            )
586
587        self.assertEqual("(a 3)", found)
588
589
590    def testAutoDupTreeWithRuleRootAndLabels(self):
591        grammar = textwrap.dedent(
592        r'''
593        grammar T13;
594        options {
595            language=Python3;
596            output=AST;
597        }
598        a : ID INT -> ^(ID INT);
599        ID : 'a'..'z'+ ;
600        INT : '0'..'9'+;
601        WS : (' '|'\\n') {$channel=HIDDEN;} ;
602        ''')
603
604        treeGrammar = textwrap.dedent(
605        r'''
606        tree grammar TP13;
607        options {
608            language=Python3;
609            output=AST;
610            ASTLabelType=CommonTree;
611            tokenVocab=T13;
612        }
613        a : ^(x=b INT) ;
614        b : ID ;
615        ''')
616
617        found = self.execTreeParser(
618            grammar, 'a',
619            treeGrammar, 'a',
620            "a 3"
621            )
622
623        self.assertEqual("(a 3)", found)
624
625
626    def testAutoDupTreeWithRuleRootAndListLabels(self):
627        grammar = textwrap.dedent(
628        r'''
629        grammar T14;
630        options {
631            language=Python3;
632            output=AST;
633        }
634        a : ID INT -> ^(ID INT);
635        ID : 'a'..'z'+ ;
636        INT : '0'..'9'+;
637        WS : (' '|'\\n') {$channel=HIDDEN;} ;
638        ''')
639
640        treeGrammar = textwrap.dedent(
641        r'''
642        tree grammar TP14;
643        options {
644            language=Python3;
645            output=AST;
646            ASTLabelType=CommonTree;
647            tokenVocab=T14;
648        }
649        a : ^(x+=b y+=c) ;
650        b : ID ;
651        c : INT ;
652        ''')
653
654        found = self.execTreeParser(
655            grammar, 'a',
656            treeGrammar, 'a',
657            "a 3"
658            )
659
660        self.assertEqual("(a 3)", found)
661
662
663    def testAutoDupNestedTree(self):
664        grammar = textwrap.dedent(
665        r'''
666        grammar T15;
667        options {
668            language=Python3;
669            output=AST;
670        }
671        a : x=ID y=ID INT -> ^($x ^($y INT));
672        ID : 'a'..'z'+ ;
673        INT : '0'..'9'+;
674        WS : (' '|'\\n') {$channel=HIDDEN;} ;
675        ''')
676
677        treeGrammar = textwrap.dedent(
678        r'''
679        tree grammar TP15;
680        options {
681            language=Python3;
682            output=AST;
683            ASTLabelType=CommonTree;
684            tokenVocab=T15;
685        }
686        a : ^(ID ^(ID INT))
687          ;
688        ''')
689
690        found = self.execTreeParser(
691            grammar, 'a',
692            treeGrammar, 'a',
693            "a b 3"
694            )
695
696        self.assertEqual("(a (b 3))", found)
697
698
699    def testDelete(self):
700        grammar = textwrap.dedent(
701        r'''
702        grammar T16;
703        options {
704            language=Python3;
705            output=AST;
706        }
707        a : ID ;
708        ID : 'a'..'z'+ ;
709        INT : '0'..'9'+;
710        WS : (' '|'\\n') {$channel=HIDDEN;} ;
711        ''')
712
713        treeGrammar = textwrap.dedent(
714        r'''
715        tree grammar TP16;
716        options {
717            language=Python3;
718            output=AST;
719            ASTLabelType=CommonTree;
720            tokenVocab=T16;
721        }
722        a : ID ->
723          ;
724        ''')
725
726        found = self.execTreeParser(
727            grammar, 'a',
728            treeGrammar, 'a',
729            "abc"
730            )
731
732        self.assertEqual("", found)
733
734    def testSetMatchNoRewrite(self):
735        grammar = textwrap.dedent(
736            r'''
737            grammar T;
738            options {
739                language=Python3;
740                output=AST;
741            }
742            a : ID INT ;
743            ID : 'a'..'z'+ ;
744            INT : '0'..'9'+;
745            WS : (' '|'\n') {$channel=HIDDEN;} ;
746            ''')
747
748        treeGrammar = textwrap.dedent(
749            r'''
750            tree grammar TP;
751            options {
752                language=Python3;
753                output=AST;
754                ASTLabelType=CommonTree;
755                tokenVocab=T;
756            }
757            a : b INT;
758            b : ID | INT;
759            ''')
760
761        found = self.execTreeParser(
762            grammar, 'a',
763            treeGrammar, 'a',
764            "abc 34"
765            )
766
767        self.assertEqual("abc 34", found)
768
769
770    def testSetOptionalMatchNoRewrite(self):
771        grammar = textwrap.dedent(
772            r'''
773            grammar T;
774            options {
775                language=Python3;
776                output=AST;
777            }
778            a : ID INT ;
779            ID : 'a'..'z'+ ;
780            INT : '0'..'9'+;
781            WS : (' '|'\n') {$channel=HIDDEN;} ;
782            ''')
783
784        treeGrammar = textwrap.dedent(
785            r'''
786            tree grammar TP;
787            options {
788                language=Python3;
789                output=AST;
790                ASTLabelType=CommonTree;
791                tokenVocab=T;
792            }
793            a : (ID|INT)? INT ;
794            ''')
795
796        found = self.execTreeParser(
797            grammar, 'a',
798            treeGrammar, 'a',
799            "abc 34")
800
801        self.assertEqual("abc 34", found)
802
803
804    def testSetMatchNoRewriteLevel2(self):
805        grammar = textwrap.dedent(
806            r'''
807            grammar T;
808            options {
809                language=Python3;
810                output=AST;
811            }
812            a : x=ID INT -> ^($x INT);
813            ID : 'a'..'z'+ ;
814            INT : '0'..'9'+;
815            WS : (' '|'\n') {$channel=HIDDEN;} ;
816            ''')
817
818        treeGrammar = textwrap.dedent(
819            r'''
820            tree grammar TP;
821            options {
822                language=Python3;
823                output=AST;
824                ASTLabelType=CommonTree;
825                tokenVocab=T;
826            }
827            a : ^(ID (ID | INT) ) ;
828            ''')
829
830        found = self.execTreeParser(
831            grammar, 'a',
832            treeGrammar, 'a',
833            "abc 34"
834            )
835
836        self.assertEqual("(abc 34)", found)
837
838
839    def testSetMatchNoRewriteLevel2Root(self):
840        grammar = textwrap.dedent(
841            r'''
842            grammar T;
843            options {
844                language=Python3;
845                output=AST;
846            }
847            a : x=ID INT -> ^($x INT);
848            ID : 'a'..'z'+ ;
849            INT : '0'..'9'+;
850            WS : (' '|'\n') {$channel=HIDDEN;} ;
851            ''')
852
853        treeGrammar = textwrap.dedent(
854            r'''
855            tree grammar TP;
856            options {
857                language=Python3;
858                output=AST;
859                ASTLabelType=CommonTree;
860                tokenVocab=T;
861            }
862            a : ^((ID | INT) INT) ;
863            ''')
864
865        found = self.execTreeParser(
866            grammar, 'a',
867            treeGrammar, 'a',
868            "abc 34"
869            )
870
871        self.assertEqual("(abc 34)", found)
872
873
874    ## REWRITE MODE
875
876    def testRewriteModeCombinedRewriteAndAuto(self):
877        grammar = textwrap.dedent(
878        r'''
879        grammar T17;
880        options {
881            language=Python3;
882            output=AST;
883        }
884        a : ID INT -> ^(ID INT) | INT ;
885        ID : 'a'..'z'+ ;
886        INT : '0'..'9'+;
887        WS : (' '|'\\n') {$channel=HIDDEN;} ;
888        ''')
889
890        treeGrammar = textwrap.dedent(
891        r'''
892        tree grammar TP17;
893        options {
894            language=Python3;
895            output=AST;
896            ASTLabelType=CommonTree;
897            tokenVocab=T17;
898            rewrite=true;
899        }
900        a : ^(ID INT) -> ^(ID["ick"] INT)
901          | INT // leaves it alone, returning $a.start
902          ;
903        ''')
904
905        found = self.execTreeParser(
906            grammar, 'a',
907            treeGrammar, 'a',
908            "abc 34"
909            )
910
911        self.assertEqual("(ick 34)", found)
912
913
914        found = self.execTreeParser(
915            grammar, 'a',
916            treeGrammar, 'a',
917            "34"
918            )
919
920        self.assertEqual("34", found)
921
922
923    def testRewriteModeFlatTree(self):
924        grammar = textwrap.dedent(
925            r'''
926            grammar T18;
927            options {
928              language=Python3;
929              output=AST;
930            }
931            a : ID INT -> ID INT | INT ;
932            ID : 'a'..'z'+ ;
933            INT : '0'..'9'+;
934            WS : (' '|'\n') {$channel=HIDDEN;} ;
935            ''')
936
937        treeGrammar = textwrap.dedent(
938            r'''
939            tree grammar TP18;
940            options {
941              language=Python3;
942              output=AST;
943              ASTLabelType=CommonTree;
944              tokenVocab=T18;
945              rewrite=true;
946            }
947            s : ID a ;
948            a : INT -> INT["1"]
949              ;
950            ''')
951
952        found = self.execTreeParser(
953            grammar, 'a',
954            treeGrammar, 's',
955            "abc 34"
956            )
957        self.assertEqual("abc 1", found)
958
959
960    def testRewriteModeChainRuleFlatTree(self):
961        grammar = textwrap.dedent(
962            r'''
963            grammar T;
964            options {language=Python3; output=AST;}
965            a : ID INT -> ID INT | INT ;
966            ID : 'a'..'z'+ ;
967            INT : '0'..'9'+;
968            WS : (' '|'\n') {$channel=HIDDEN;} ;
969            ''')
970
971        treeGrammar = textwrap.dedent(
972            r'''
973            tree grammar TP;
974            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T; rewrite=true;}
975            s : a ;
976            a : b ;
977            b : ID INT -> INT ID
978              ;
979            ''')
980
981        found = self.execTreeParser(
982            grammar, 'a',
983            treeGrammar, 's',
984            "abc 34")
985        self.assertEqual("34 abc", found)
986
987
988    def testRewriteModeChainRuleTree(self):
989        grammar = textwrap.dedent(
990            r'''
991            grammar T;
992            options {language=Python3; output=AST;}
993            a : ID INT -> ^(ID INT) ;
994            ID : 'a'..'z'+ ;
995            INT : '0'..'9'+;
996            WS : (' '|'\n') {$channel=HIDDEN;} ;
997            ''')
998
999        treeGrammar = textwrap.dedent(
1000            r'''
1001            tree grammar TP;
1002            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T; rewrite=true;}
1003            s : a ;
1004            a : b ; // a.tree must become b.tree
1005            b : ^(ID INT) -> INT
1006              ;
1007            ''')
1008
1009        found = self.execTreeParser(
1010            grammar, 'a',
1011            treeGrammar, 's',
1012            "abc 34")
1013        self.assertEqual("34", found)
1014
1015
1016    def testRewriteModeChainRuleTree2(self):
1017        grammar = textwrap.dedent(
1018            r'''
1019            grammar T;
1020            options {language=Python3; output=AST;}
1021            a : ID INT -> ^(ID INT) ;
1022            ID : 'a'..'z'+ ;
1023            INT : '0'..'9'+;
1024            WS : (' '|'\n') {$channel=HIDDEN;} ;
1025            ''')
1026
1027        treeGrammar = textwrap.dedent(
1028            r"""
1029            tree grammar TP;
1030            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T; rewrite=true;}
1031            tokens { X; }
1032            s : a* b ; // only b contributes to tree, but it's after a*; s.tree = b.tree
1033            a : X ;
1034            b : ^(ID INT) -> INT
1035              ;
1036            """)
1037
1038        found = self.execTreeParser(
1039            grammar, 'a',
1040            treeGrammar, 's',
1041            "abc 34")
1042        self.assertEqual("34", found)
1043
1044
1045    def testRewriteModeChainRuleTree3(self):
1046        grammar = textwrap.dedent(
1047            r'''
1048            grammar T;
1049            options {language=Python3; output=AST;}
1050            a : 'boo' ID INT -> 'boo' ^(ID INT) ;
1051            ID : 'a'..'z'+ ;
1052            INT : '0'..'9'+;
1053            WS : (' '|'\n') {$channel=HIDDEN;} ;
1054            ''')
1055
1056        treeGrammar = textwrap.dedent(
1057            r"""
1058            tree grammar TP;
1059            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T; rewrite=true;}
1060            tokens { X; }
1061            s : 'boo' a* b ; // don't reset s.tree to b.tree due to 'boo'
1062            a : X ;
1063            b : ^(ID INT) -> INT
1064              ;
1065            """)
1066
1067        found = self.execTreeParser(
1068            grammar, 'a',
1069            treeGrammar, 's',
1070            "boo abc 34")
1071        self.assertEqual("boo 34", found)
1072
1073
1074    def testRewriteModeChainRuleTree4(self):
1075        grammar = textwrap.dedent(
1076            r"""
1077            grammar T;
1078            options {language=Python3; output=AST;}
1079            a : 'boo' ID INT -> ^('boo' ^(ID INT)) ;
1080            ID : 'a'..'z'+ ;
1081            INT : '0'..'9'+;
1082            WS : (' '|'\n') {$channel=HIDDEN;} ;
1083            """)
1084
1085        treeGrammar = textwrap.dedent(
1086            r"""
1087            tree grammar TP;
1088            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T; rewrite=true;}
1089            tokens { X; }
1090            s : ^('boo' a* b) ; // don't reset s.tree to b.tree due to 'boo'
1091            a : X ;
1092            b : ^(ID INT) -> INT
1093              ;
1094            """)
1095
1096        found = self.execTreeParser(
1097            grammar, 'a',
1098            treeGrammar, 's',
1099            "boo abc 34")
1100        self.assertEqual("(boo 34)", found)
1101
1102
1103    def testRewriteModeChainRuleTree5(self):
1104        grammar = textwrap.dedent(
1105            r"""
1106            grammar T;
1107            options {language=Python3; output=AST;}
1108            a : 'boo' ID INT -> ^('boo' ^(ID INT)) ;
1109            ID : 'a'..'z'+ ;
1110            INT : '0'..'9'+;
1111            WS : (' '|'\n') {$channel=HIDDEN;} ;
1112            """)
1113
1114        treeGrammar = textwrap.dedent(
1115            r"""
1116            tree grammar TP;
1117            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T; rewrite=true;}
1118            tokens { X; }
1119            s : ^(a b) ; // s.tree is a.tree
1120            a : 'boo' ;
1121            b : ^(ID INT) -> INT
1122              ;
1123            """)
1124
1125        found = self.execTreeParser(
1126            grammar, 'a',
1127            treeGrammar, 's',
1128            "boo abc 34")
1129        self.assertEqual("(boo 34)", found)
1130
1131
1132    def testRewriteOfRuleRef(self):
1133        grammar = textwrap.dedent(
1134            r"""
1135            grammar T;
1136            options {language=Python3; output=AST;}
1137            a : ID INT -> ID INT | INT ;
1138            ID : 'a'..'z'+ ;
1139            INT : '0'..'9'+;
1140            WS : (' '|'\n') {$channel=HIDDEN;} ;
1141            """)
1142
1143        treeGrammar = textwrap.dedent(
1144            r"""
1145            tree grammar TP;
1146            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T; rewrite=true;}
1147            s : a -> a ;
1148            a : ID INT -> ID INT ;
1149            """)
1150
1151        found = self.execTreeParser(
1152            grammar, 'a',
1153            treeGrammar, 's',
1154            "abc 34")
1155        self.assertEqual("abc 34", found)
1156
1157
1158    def testRewriteOfRuleRefRoot(self):
1159        grammar = textwrap.dedent(
1160            r"""
1161            grammar T;
1162            options {language=Python3; output=AST;}
1163            a : ID INT INT -> ^(INT ^(ID INT));
1164            ID : 'a'..'z'+ ;
1165            INT : '0'..'9'+;
1166            WS : (' '|'\n') {$channel=HIDDEN;} ;
1167            """)
1168
1169        treeGrammar = textwrap.dedent(
1170            r"""
1171            tree grammar TP;
1172            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T; rewrite=true;}
1173            s : ^(a ^(ID INT)) -> a ;
1174            a : INT ;
1175            """)
1176
1177        found = self.execTreeParser(
1178            grammar, 'a',
1179            treeGrammar, 's',
1180            "abc 12 34")
1181        # emits whole tree when you ref the root since I can't know whether
1182        # you want the children or not.  You might be returning a whole new
1183        # tree.  Hmm...still seems weird.  oh well.
1184        self.assertEqual("(12 (abc 34))", found)
1185
1186
1187    def testRewriteOfRuleRefRootLabeled(self):
1188        grammar = textwrap.dedent(
1189            r"""
1190            grammar T;
1191            options {language=Python3; output=AST;}
1192            a : ID INT INT -> ^(INT ^(ID INT));
1193            ID : 'a'..'z'+ ;
1194            INT : '0'..'9'+;
1195            WS : (' '|'\n') {$channel=HIDDEN;} ;
1196            """)
1197
1198        treeGrammar = textwrap.dedent(
1199            r"""
1200            tree grammar TP;
1201            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T; rewrite=true;}
1202            s : ^(label=a ^(ID INT)) -> a ;
1203            a : INT ;
1204            """)
1205
1206        found = self.execTreeParser(
1207            grammar, 'a',
1208            treeGrammar, 's',
1209            "abc 12 34")
1210        # emits whole tree when you ref the root since I can't know whether
1211        # you want the children or not.  You might be returning a whole new
1212        # tree.  Hmm...still seems weird.  oh well.
1213        self.assertEqual("(12 (abc 34))", found)
1214
1215
1216    def testRewriteOfRuleRefRootListLabeled(self):
1217        grammar = textwrap.dedent(
1218            r"""
1219            grammar T;
1220            options {language=Python3; output=AST;}
1221            a : ID INT INT -> ^(INT ^(ID INT));
1222            ID : 'a'..'z'+ ;
1223            INT : '0'..'9'+;
1224            WS : (' '|'\n') {$channel=HIDDEN;} ;
1225            """)
1226
1227        treeGrammar = textwrap.dedent(
1228            r"""
1229            tree grammar TP;
1230            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T; rewrite=true;}
1231            s : ^(label+=a ^(ID INT)) -> a ;
1232            a : INT ;
1233            """)
1234
1235        found = self.execTreeParser(
1236            grammar, 'a',
1237            treeGrammar, 's',
1238            "abc 12 34")
1239        # emits whole tree when you ref the root since I can't know whether
1240        # you want the children or not.  You might be returning a whole new
1241        # tree.  Hmm...still seems weird.  oh well.
1242        self.assertEqual("(12 (abc 34))", found)
1243
1244
1245    def testRewriteOfRuleRefChild(self):
1246        grammar = textwrap.dedent(
1247            r"""
1248            grammar T;
1249            options {language=Python3; output=AST;}
1250            a : ID INT -> ^(ID ^(INT INT));
1251            ID : 'a'..'z'+ ;
1252            INT : '0'..'9'+;
1253            WS : (' '|'\n') {$channel=HIDDEN;} ;
1254            """)
1255
1256        treeGrammar = textwrap.dedent(
1257            r"""
1258            tree grammar TP;
1259            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T; rewrite=true;}
1260            s : ^(ID a) -> a ;
1261            a : ^(INT INT) ;
1262            """)
1263
1264        found = self.execTreeParser(
1265            grammar, 'a',
1266            treeGrammar, 's',
1267            "abc 34")
1268        self.assertEqual("(34 34)", found)
1269
1270
1271    def testRewriteOfRuleRefLabel(self):
1272        grammar = textwrap.dedent(
1273            r"""
1274            grammar T;
1275            options {language=Python3; output=AST;}
1276            a : ID INT -> ^(ID ^(INT INT));
1277            ID : 'a'..'z'+ ;
1278            INT : '0'..'9'+;
1279            WS : (' '|'\n') {$channel=HIDDEN;} ;
1280            """)
1281
1282        treeGrammar = textwrap.dedent(
1283            r"""
1284            tree grammar TP;
1285            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T; rewrite=true;}
1286            s : ^(ID label=a) -> a ;
1287            a : ^(INT INT) ;
1288            """)
1289
1290        found = self.execTreeParser(
1291            grammar, 'a',
1292            treeGrammar, 's',
1293            "abc 34")
1294        self.assertEqual("(34 34)", found)
1295
1296
1297    def testRewriteOfRuleRefListLabel(self):
1298        grammar = textwrap.dedent(
1299            r"""
1300            grammar T;
1301            options {language=Python3; output=AST;}
1302            a : ID INT -> ^(ID ^(INT INT));
1303            ID : 'a'..'z'+ ;
1304            INT : '0'..'9'+;
1305            WS : (' '|'\n') {$channel=HIDDEN;} ;
1306            """)
1307
1308        treeGrammar = textwrap.dedent(
1309            r"""
1310            tree grammar TP;
1311            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T; rewrite=true;}
1312            s : ^(ID label+=a) -> a ;
1313            a : ^(INT INT) ;
1314            """)
1315
1316        found = self.execTreeParser(
1317            grammar, 'a',
1318            treeGrammar, 's',
1319            "abc 34")
1320        self.assertEqual("(34 34)", found)
1321
1322
1323
1324    def testRewriteModeWithPredicatedRewrites(self):
1325        grammar = textwrap.dedent(
1326            r'''
1327            grammar T19;
1328            options {
1329              language=Python3;
1330              output=AST;
1331            }
1332            a : ID INT -> ^(ID["root"] ^(ID INT)) | INT -> ^(ID["root"] INT) ;
1333            ID : 'a'..'z'+ ;
1334            INT : '0'..'9'+;
1335            WS : (' '|'\n') {$channel=HIDDEN;} ;
1336            ''')
1337
1338        treeGrammar = textwrap.dedent(
1339            r'''
1340            tree grammar TP19;
1341            options {
1342              language=Python3;
1343              output=AST;
1344              ASTLabelType=CommonTree;
1345              tokenVocab=T19;
1346              rewrite=true;
1347            }
1348            s : ^(ID a) { self.buf += $s.start.toStringTree() };
1349            a : ^(ID INT) -> {True}? ^(ID["ick"] INT)
1350                          -> INT
1351              ;
1352            ''')
1353
1354        found = self.execTreeParser(
1355            grammar, 'a',
1356            treeGrammar, 's',
1357            "abc 34"
1358            )
1359
1360        self.assertEqual("(root (ick 34))", found)
1361
1362
1363    def testWildcardSingleNode(self):
1364        grammar = textwrap.dedent(
1365            r'''
1366            grammar T;
1367            options {
1368                language=Python3;
1369                output=AST;
1370            }
1371            a : ID INT -> ^(ID["root"] INT);
1372            ID : 'a'..'z'+ ;
1373            INT : '0'..'9'+;
1374            WS : (' '|'\n') {$channel=HIDDEN;} ;
1375            ''')
1376
1377        treeGrammar = textwrap.dedent(
1378            r'''
1379            tree grammar TP;
1380            options {
1381                language=Python3;
1382                output=AST;
1383                ASTLabelType=CommonTree;
1384                tokenVocab=T;
1385            }
1386            s : ^(ID c=.) -> $c
1387            ;
1388            ''')
1389
1390        found = self.execTreeParser(
1391            grammar, 'a',
1392            treeGrammar, 's',
1393            "abc 34"
1394            )
1395
1396        self.assertEqual("34", found)
1397
1398    def testWildcardUnlabeledSingleNode(self):
1399        grammar = textwrap.dedent(
1400            r'''
1401            grammar T;
1402            options {language=Python3; output=AST;}
1403            a : ID INT -> ^(ID INT);
1404            ID : 'a'..'z'+ ;
1405            INT : '0'..'9'+;
1406            WS : (' '|'\n') {$channel=HIDDEN;} ;
1407            ''')
1408
1409        treeGrammar = textwrap.dedent(
1410            r'''
1411            tree grammar TP;
1412            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T;}
1413            s : ^(ID .) -> ID
1414              ;
1415            ''')
1416
1417        found = self.execTreeParser(
1418            grammar, 'a',
1419            treeGrammar, 's',
1420            "abc 34")
1421        self.assertEqual("abc", found)
1422
1423
1424    def testWildcardGrabsSubtree(self):
1425        grammar = textwrap.dedent(
1426            r'''
1427            grammar T;
1428            options {language=Python3; output=AST;}
1429            a : ID x=INT y=INT z=INT -> ^(ID[\"root\"] ^($x $y $z));
1430            ID : 'a'..'z'+ ;
1431            INT : '0'..'9'+;
1432            WS : (' '|'\n') {$channel=HIDDEN;} ;
1433            ''')
1434
1435        treeGrammar = textwrap.dedent(
1436            r'''
1437            tree grammar TP;
1438            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T;}
1439            s : ^(ID c=.) -> $c
1440              ;
1441            ''')
1442
1443        found = self.execTreeParser(
1444            grammar, 'a',
1445            treeGrammar, 's',
1446            "abc 1 2 3")
1447        self.assertEqual("(1 2 3)", found)
1448
1449
1450    def testWildcardGrabsSubtree2(self):
1451        grammar = textwrap.dedent(
1452            r'''
1453            grammar T;
1454            options {language=Python3; output=AST;}
1455            a : ID x=INT y=INT z=INT -> ID ^($x $y $z);
1456            ID : 'a'..'z'+ ;
1457            INT : '0'..'9'+;
1458            WS : (' '|'\n') {$channel=HIDDEN;} ;
1459            ''')
1460
1461        treeGrammar = textwrap.dedent(
1462            r'''
1463            tree grammar TP;
1464            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T;}
1465            s : ID c=. -> $c
1466              ;
1467            ''')
1468
1469        found = self.execTreeParser(
1470            grammar, 'a',
1471            treeGrammar, 's',
1472            "abc 1 2 3")
1473        self.assertEqual("(1 2 3)", found)
1474
1475
1476    def testWildcardListLabel(self):
1477        grammar = textwrap.dedent(
1478            r'''
1479            grammar T;
1480            options {language=Python3; output=AST;}
1481            a : INT INT INT ;
1482            ID : 'a'..'z'+ ;
1483            INT : '0'..'9'+;
1484            WS : (' '|'\n') {$channel=HIDDEN;} ;
1485            ''')
1486
1487        treeGrammar = textwrap.dedent(
1488            r'''
1489            tree grammar TP;
1490            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T;}
1491            s : (c+=.)+ -> $c+
1492              ;
1493            ''')
1494
1495        found = self.execTreeParser(
1496            grammar, 'a',
1497            treeGrammar, 's',
1498            "1 2 3")
1499        self.assertEqual("1 2 3", found)
1500
1501
1502    def testWildcardListLabel2(self):
1503        grammar = textwrap.dedent(
1504            r'''
1505            grammar T;
1506            options {language=Python3; output=AST; ASTLabelType=CommonTree;}
1507            a  : x=INT y=INT z=INT -> ^($x ^($y $z) ^($y $z));
1508            ID : 'a'..'z'+ ;
1509            INT : '0'..'9'+;
1510            WS : (' '|'\n') {$channel=HIDDEN;} ;
1511            ''')
1512
1513        treeGrammar = textwrap.dedent(
1514            r'''
1515            tree grammar TP;
1516            options {language=Python3; output=AST; ASTLabelType=CommonTree; tokenVocab=T; rewrite=true;}
1517            s : ^(INT (c+=.)+) -> $c+
1518              ;
1519            ''')
1520
1521        found = self.execTreeParser(
1522            grammar, 'a',
1523            treeGrammar, 's',
1524            "1 2 3")
1525        self.assertEqual("(2 3) (2 3)", found)
1526
1527
1528    def testRuleResultAsRoot(self):
1529        grammar = textwrap.dedent(
1530            r'''
1531            grammar T;
1532            options {
1533                language=Python3;
1534                output=AST;
1535            }
1536            a : ID '=' INT -> ^('=' ID INT);
1537            ID : 'a'..'z'+ ;
1538            INT : '0'..'9'+;
1539            COLON : ':' ;
1540            WS : (' '|'\n') {$channel=HIDDEN;} ;
1541            ''')
1542
1543        treeGrammar = textwrap.dedent(
1544            r'''
1545            tree grammar TP;
1546            options {
1547                language=Python3;
1548                output=AST;
1549                rewrite=true;
1550                ASTLabelType=CommonTree;
1551                tokenVocab=T;
1552            }
1553            a : ^(eq e1=ID e2=.) -> ^(eq $e2 $e1) ;
1554            eq : '=' | ':' {pass} ;  // bug in set match, doesn't add to tree!! booh. force nonset.
1555            ''')
1556
1557        found = self.execTreeParser(
1558            grammar, 'a',
1559            treeGrammar, 'a',
1560            "abc = 34")
1561        self.assertEqual("(= 34 abc)", found)
1562
1563
1564if __name__ == '__main__':
1565    unittest.main()
1566