aboutsummaryrefslogtreecommitdiff
path: root/antlr-3.4/runtime/Ruby/test/functional/tree-parser/basic.rb
blob: 285ab6d97806c26fd42495cee7894ab116aa49f7 (plain)
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
#!/usr/bin/ruby
# encoding: utf-8

require 'antlr3/test/functional'

class TestTreeParser1 < ANTLR3::Test::Functional
  
  example "flat list" do
    compile_and_load inline_grammar( <<-'END' )
      grammar FlatList;
      options {
          language=Ruby;
          output=AST;
      }
      a : ID INT;
      ID : 'a'..'z'+ ;
      INT : '0'..'9'+;
      WS : (' '|'\n') {$channel=HIDDEN;} ;
    END
    compile_and_load inline_grammar( <<-'END' )
      tree grammar FlatListWalker;
      options {
          language=Ruby;
          ASTLabelType=CommonTree;
      }
      @members { include ANTLR3::Test::CaptureOutput }
      a : ID INT
          {self.capture("\%s, \%s" \% [$ID, $INT])}
        ;
    END
    
    lexer  = FlatList::Lexer.new( "abc 34" )
    tokens = ANTLR3::CommonTokenStream.new( lexer )
    parser = FlatList::Parser.new( tokens )
    
    result = parser.a
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = tokens
    walker = FlatListWalker::TreeParser.new( nodes )
    walker.a
    walker.output.should == "abc, 34"
  end
  
  example "simple tree" do
    compile_and_load inline_grammar( <<-'END' )
      grammar SimpleTree;
      options {
          language=Ruby;
          output=AST;
      }
      a : ID INT -> ^(ID INT);
      ID : 'a'..'z'+ ;
      INT : '0'..'9'+;
      WS : (' '|'\\n') {$channel=HIDDEN;} ;
    END
    compile_and_load inline_grammar( <<-'END' )
      tree grammar SimpleTreeWalker;
      options {
          language=Ruby;
          ASTLabelType=CommonTree;
      }
      @members { include ANTLR3::Test::CaptureOutput }
      
      a : ^(ID INT)
          {capture('\%s, \%s' \% [$ID, $INT])}
        ;
    END
    lexer  = SimpleTree::Lexer.new( "abc 34" )
    tokens = ANTLR3::CommonTokenStream.new( lexer )
    parser = SimpleTree::Parser.new( tokens )
    
    result = parser.a
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = tokens
    walker = SimpleTreeWalker::TreeParser.new( nodes )
    walker.a
    walker.output.should == "abc, 34"
  end
  
  example "flat vs tree decision" do
    compile_and_load inline_grammar( <<-'END' )
      grammar FlatVsTreeDecision;
      options {
          language=Ruby;
          output=AST;
      }
      a : b c ;
      b : ID INT -> ^(ID INT);
      c : ID INT;
      ID : 'a'..'z'+ ;
      INT : '0'..'9'+;
      WS : (' '|'\\n') {$channel=HIDDEN;} ;
    END
    compile_and_load inline_grammar( <<-'END' )
      tree grammar FlatVsTreeDecisionWalker;
      options {
          language=Ruby;
          ASTLabelType=CommonTree;
      }
      @members { include ANTLR3::Test::CaptureOutput }
      
      a : b b ;
      b : ID INT    {capture("\%s \%s\n" \% [$ID, $INT])}
        | ^(ID INT) {capture("^(\%s \%s)" \% [$ID, $INT])}
        ;
    END
    lexer  = FlatVsTreeDecision::Lexer.new( "a 1 b 2" )
    tokens = ANTLR3::CommonTokenStream.new( lexer )
    parser = FlatVsTreeDecision::Parser.new( tokens )
    
    result = parser.a
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = tokens
    walker = FlatVsTreeDecisionWalker::TreeParser.new( nodes )
    walker.a
    walker.output.should == "^(a 1)b 2\n"
  end
  
  example "flat vs tree decision2" do
    compile_and_load inline_grammar( <<-'END' )
      grammar FlatVsTreeDecision2;
      options {
          language=Ruby;
          output=AST;
      }
      a : b c ;
      b : ID INT+ -> ^(ID INT+);
      c : ID INT+;
      ID : 'a'..'z'+ ;
      INT : '0'..'9'+;
      WS : (' '|'\n') {$channel=HIDDEN;} ;
    END
    compile_and_load inline_grammar( <<-'END' )
      tree grammar FlatVsTreeDecision2Walker;
      options {
          language=Ruby;
          ASTLabelType=CommonTree;
      }
      @members { include ANTLR3::Test::CaptureOutput }
      a : b b ;
      b : ID INT+    {say("#{$ID} #{$INT}")}
        | ^(x=ID (y=INT)+) {capture("^(#{$x} #{$y})")}
        ;
    END
    lexer  = FlatVsTreeDecision2::Lexer.new( "a 1 2 3 b 4 5" )
    tokens = ANTLR3::CommonTokenStream.new( lexer )
    parser = FlatVsTreeDecision2::Parser.new( tokens )
    
    result = parser.a
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = tokens
    walker = FlatVsTreeDecision2Walker::TreeParser.new( nodes )
    walker.a
    walker.output.should == "^(a 3)b 5\n"
  end
  
  example "cyclic dfa lookahead" do
    compile_and_load inline_grammar( <<-'END' )
      grammar CyclicDFALookahead;
      options {
          language=Ruby;
          output=AST;
      }
      a : ID INT+ PERIOD;
      ID : 'a'..'z'+ ;
      INT : '0'..'9'+;
      SEMI : ';' ;
      PERIOD : '.' ;
      WS : (' '|'\n') {$channel=HIDDEN;} ;
    END
    compile_and_load inline_grammar( <<-'END' )
      tree grammar CyclicDFALookaheadWalker;
      options {
          language=Ruby;
          ASTLabelType=CommonTree;
      }
      @members { include ANTLR3::Test::CaptureOutput }
      a : ID INT+ PERIOD {capture("alt 1")}
        | ID INT+ SEMI   {capture("alt 2")}
        ;
    END
    lexer  = CyclicDFALookahead::Lexer.new( "a 1 2 3." )
    tokens = ANTLR3::CommonTokenStream.new( lexer )
    parser = CyclicDFALookahead::Parser.new( tokens )
    
    result = parser.a
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = tokens
    walker = CyclicDFALookaheadWalker::TreeParser.new( nodes )
    walker.a
    walker.output.should == "alt 1"
  end
  
  example "nullable child list" do
    compile_and_load inline_grammar( <<-'END' )
      grammar NullableChildList;
      options {
          language=Ruby;
          output=AST;
      }
      a : ID INT? -> ^(ID INT?);
      ID : 'a'..'z'+ ;
      INT : '0'..'9'+;
      WS : (' '|'\\n') {$channel=HIDDEN;} ;
    END
    compile_and_load inline_grammar( <<-'END' )
      tree grammar NullableChildListWalker;
      options {
          language=Ruby;
          ASTLabelType=CommonTree;
      }
      @members { include ANTLR3::Test::CaptureOutput }
      a : ^(ID INT?)
          {capture($ID.to_s)}
        ;
    END
    lexer  = NullableChildList::Lexer.new( "abc" )
    tokens = ANTLR3::CommonTokenStream.new( lexer )
    parser = NullableChildList::Parser.new( tokens )
    
    result = parser.a
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = tokens
    walker = NullableChildListWalker::TreeParser.new( nodes )
    walker.a
    walker.output.should == "abc"
  end
  
  example "nullable child list2" do
    compile_and_load inline_grammar( <<-'END' )
      grammar NullableChildList2;
      options {
          language=Ruby;
          output=AST;
      }
      a : ID INT? SEMI -> ^(ID INT?) SEMI ;
      ID : 'a'..'z'+ ;
      INT : '0'..'9'+;
      SEMI : ';' ;
      WS : (' '|'\n') {$channel=HIDDEN;} ;
    END
    compile_and_load inline_grammar( <<-'END' )
      tree grammar NullableChildList2Walker;
      options {
          language=Ruby;
          ASTLabelType=CommonTree;
      }
      @members { include ANTLR3::Test::CaptureOutput }
      a : ^(ID INT?) SEMI
          {capture($ID.to_s)}
        ;
    END
    lexer  = NullableChildList2::Lexer.new( "abc;" )
    tokens = ANTLR3::CommonTokenStream.new( lexer )
    parser = NullableChildList2::Parser.new( tokens )
    
    result = parser.a
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = tokens
    walker = NullableChildList2Walker::TreeParser.new( nodes )
    walker.a
    walker.output.should == "abc"
  end
  
  example "nullable child list3" do
    compile_and_load inline_grammar( <<-'END' )
      grammar NullableChildList3;
      options {
          language=Ruby;
          output=AST;
      }
      a : x=ID INT? (y=ID)? SEMI -> ^($x INT? $y?) SEMI ;
      ID : 'a'..'z'+ ;
      INT : '0'..'9'+;
      SEMI : ';' ;
      WS : (' '|'\\n') {$channel=HIDDEN;} ;
    END
    compile_and_load inline_grammar( <<-'END' )
      tree grammar NullableChildList3Walker;
      options {
          language=Ruby;
          ASTLabelType=CommonTree;
      }
      @members { include ANTLR3::Test::CaptureOutput }
      a : ^(ID INT? b) SEMI
          {self.capture($ID.to_s + ", " + $b.text.to_s)}
        ;
      b : ID? ;
    END
    lexer  = NullableChildList3::Lexer.new( "abc def;" )
    tokens = ANTLR3::CommonTokenStream.new( lexer )
    parser = NullableChildList3::Parser.new( tokens )
    
    result = parser.a
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = tokens
    walker = NullableChildList3Walker::TreeParser.new( nodes )
    walker.a
    walker.output.should == "abc, def"
  end
  
  example "actions after root" do
    compile_and_load inline_grammar( <<-'END' )
      grammar ActionsAfterRoot;
      options {
          language=Ruby;
          output=AST;
      }
      a : x=ID INT? SEMI -> ^($x INT?) ;
      ID : 'a'..'z'+ ;
      INT : '0'..'9'+;
      SEMI : ';' ;
      WS : (' '|'\n') {$channel=HIDDEN;} ;
    END
    compile_and_load inline_grammar( <<-'END' )
      tree grammar ActionsAfterRootWalker;
      options {
          language=Ruby;
          ASTLabelType=CommonTree;
      }
      @members { include ANTLR3::Test::CaptureOutput }
      a @init {x=0} : ^(ID {x=1} {x=2} INT?)
          {say( $ID.to_s + ", " + x.to_s )}
        ;
    END
    lexer  = ActionsAfterRoot::Lexer.new( "abc;" )
    tokens = ANTLR3::CommonTokenStream.new( lexer )
    parser = ActionsAfterRoot::Parser.new( tokens )
    
    result = parser.a
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = tokens
    walker = ActionsAfterRootWalker::TreeParser.new( nodes )
    walker.a
    walker.output.should == "abc, 2\n"
  end
  
  example "wildcard lookahead" do
    compile_and_load inline_grammar( <<-'END' )
      grammar WildcardLookahead;
      options {language=Ruby; output=AST;}
      a : ID '+'^ INT;
      ID : 'a'..'z'+ ;
      INT : '0'..'9'+;
      SEMI : ';' ;
      PERIOD : '.' ;
      WS : (' '|'\n') {$channel=HIDDEN;} ;
    END
    compile_and_load inline_grammar( <<-'END' )
      tree grammar WildcardLookaheadWalker;
      options {language=Ruby; tokenVocab=WildcardLookahead; ASTLabelType=CommonTree;}
      @members { include ANTLR3::Test::CaptureOutput }
      a : ^('+' . INT) { capture("alt 1") }
        ;
    END
    lexer  = WildcardLookahead::Lexer.new( "a + 2" )
    tokens = ANTLR3::CommonTokenStream.new( lexer )
    parser = WildcardLookahead::Parser.new( tokens )
    
    result = parser.a
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = tokens
    walker = WildcardLookaheadWalker::TreeParser.new( nodes )
    walker.a
    walker.output.should == "alt 1"
  end
  
  example "wildcard lookahead2" do
    compile_and_load inline_grammar( <<-'END' )
      grammar WildcardLookahead2;
      options {language=Ruby; output=AST;}
      a : ID '+'^ INT;
      ID : 'a'..'z'+ ;
      INT : '0'..'9'+;
      SEMI : ';' ;
      PERIOD : '.' ;
      WS : (' '|'\n') {$channel=HIDDEN;} ;
    END
    compile_and_load inline_grammar( <<-'END' )
      tree grammar WildcardLookahead2Walker;
      options {language=Ruby; tokenVocab=WildcardLookahead2; ASTLabelType=CommonTree;}
      @members { include ANTLR3::Test::CaptureOutput }
      a : ^('+' . INT) { capture("alt 1") }
        | ^('+' . .)   { capture("alt 2") }
        ;
    END
    lexer  = WildcardLookahead2::Lexer.new( "a + 2" )
    tokens = ANTLR3::CommonTokenStream.new( lexer )
    parser = WildcardLookahead2::Parser.new( tokens )
    
    result = parser.a
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = tokens
    walker = WildcardLookahead2Walker::TreeParser.new( nodes )
    walker.a
    walker.output.should == "alt 1"
  end
  
  example "wildcard lookahead3" do
    compile_and_load inline_grammar( <<-'END' )
      grammar WildcardLookahead3;
      options {language=Ruby; output=AST;}
      a : ID '+'^ INT;
      ID : 'a'..'z'+ ;
      INT : '0'..'9'+;
      SEMI : ';' ;
      PERIOD : '.' ;
      WS : (' '|'\n') {$channel=HIDDEN;} ;
    END
    compile_and_load inline_grammar( <<-'END' )
      tree grammar WildcardLookahead3Walker;
      options {language=Ruby; tokenVocab=WildcardLookahead3; ASTLabelType=CommonTree;}
      @members { include ANTLR3::Test::CaptureOutput }
      a : ^('+' ID INT) { capture("alt 1") }
        | ^('+' . .)   { capture("alt 2") }
        ;
    END
    lexer  = WildcardLookahead3::Lexer.new( "a + 2" )
    tokens = ANTLR3::CommonTokenStream.new( lexer )
    parser = WildcardLookahead3::Parser.new( tokens )
    
    result = parser.a
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = tokens
    walker = WildcardLookahead3Walker::TreeParser.new( nodes )
    walker.a
    walker.output.should == "alt 1"
  end
  
  example "wildcard plus lookahead" do
    compile_and_load inline_grammar( <<-'END' )
      grammar WildcardPlusLookahead;
      options {language=Ruby; output=AST;}
      a : ID '+'^ INT;
      ID : 'a'..'z'+ ;
      INT : '0'..'9'+;
      SEMI : ';' ;
      PERIOD : '.' ;
      WS : (' '|'\n') {$channel=HIDDEN;} ;
    END
    compile_and_load inline_grammar( <<-'END' )
      tree grammar WildcardPlusLookaheadWalker;
      options {language=Ruby; tokenVocab=WildcardPlusLookahead; ASTLabelType=CommonTree;}
      @members { include ANTLR3::Test::CaptureOutput }
      a : ^('+' INT INT ) { capture("alt 1") }
        | ^('+' .+)   { capture("alt 2") }
        ;
    END
    lexer  = WildcardPlusLookahead::Lexer.new( "a + 2" )
    tokens = ANTLR3::CommonTokenStream.new( lexer )
    parser = WildcardPlusLookahead::Parser.new( tokens )
    
    result = parser.a
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = tokens
    walker = WildcardPlusLookaheadWalker::TreeParser.new( nodes )
    walker.a
    walker.output.should == "alt 2"
  end
  
end


class TestTreeParser2 < ANTLR3::Test::Functional
  inline_grammar( <<-'END' )
    grammar GenericLanguage;
    options {
        language = Ruby;
        output=AST;
    }
    
    tokens {
        VAR_DEF;
        ARG_DEF;
        FUNC_HDR;
        FUNC_DECL;
        FUNC_DEF;
        BLOCK;
    }
    
    program
        :   declaration+
        ;
    
    declaration
        :   variable
        |   functionHeader ';' -> ^(FUNC_DECL functionHeader)
        |   functionHeader block -> ^(FUNC_DEF functionHeader block)
        ;
    
    variable
        :   type declarator ';' -> ^(VAR_DEF type declarator)
        ;
    
    declarator
        :   ID 
        ;
    
    functionHeader
        :   type ID '(' ( formalParameter ( ',' formalParameter )* )? ')'
            -> ^(FUNC_HDR type ID formalParameter+)
        ;
    
    formalParameter
        :   type declarator -> ^(ARG_DEF type declarator)
        ;
    
    type
        :   'int'   
        |   'char'  
        |   'void'
        |   ID        
        ;
    
    block
        :   lc='{'
                variable*
                stat*
            '}'
            -> ^(BLOCK[$lc,"BLOCK"] variable* stat*)
        ;
    
    stat: forStat
        | expr ';'!
        | block
        | assignStat ';'!
        | ';'!
        ;
    
    forStat
        :   'for' '(' start=assignStat ';' expr ';' next=assignStat ')' block
            -> ^('for' $start expr $next block)
        ;
    
    assignStat
        :   ID EQ expr -> ^(EQ ID expr)
        ;
    
    expr:   condExpr
        ;
    
    condExpr
        :   aexpr ( ('=='^ | '<'^) aexpr )?
        ;
    
    aexpr
        :   atom ( '+'^ atom )*
        ;
    
    atom
        : ID      
        | INT      
        | '(' expr ')' -> expr
        ; 
    
    FOR : 'for' ;
    INT_TYPE : 'int' ;
    CHAR: 'char';
    VOID: 'void';
    
    ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
        ;
    
    INT :	('0'..'9')+
        ;
    
    EQ   : '=' ;
    EQEQ : '==' ;
    LT   : '<' ;
    PLUS : '+' ;
    
    WS  :   (   ' '
            |   '\t'
            |   '\r'
            |   '\n'
            )+
            { $channel=HIDDEN }
        ;
  END

  inline_grammar( <<-'END' )
    tree grammar GenericLanguageWalker;
    options {
        language = Ruby;
        tokenVocab = GenericLanguage;
        ASTLabelType = CommonTree;
    }
    
    @init { @traces = [] }
    @members {
      attr_reader :traces
      
      def trace_in(rule_name, rule_index)
        @traces << ">#{rule_name}"
      end
      
      def trace_out(rule_name, rule_index)
        @traces << "<#{rule_name}"
      end
    }
    
    program
        :   declaration+
        ;
    
    declaration
        :   variable
        |   ^(FUNC_DECL functionHeader)
        |   ^(FUNC_DEF functionHeader block)
        ;
    
    variable returns [res]
        :   ^(VAR_DEF type declarator)
            { 
                $res = $declarator.text; 
            }
        ;
    
    declarator
        :   ID 
        ;
    
    functionHeader
        :   ^(FUNC_HDR type ID formalParameter+)
        ;
    
    formalParameter
        :   ^(ARG_DEF type declarator)
        ;
    
    type
        :   'int'
        |   'char'
        |   'void'
        |   ID        
        ;
    
    block
        :   ^(BLOCK variable* stat*)
        ;
    
    stat: forStat
        | expr
        | block
        ;
    
    forStat
        :   ^('for' expr expr expr block)
        ;
    
    expr:   ^(EQEQ expr expr)
        |   ^(LT expr expr)
        |   ^(PLUS expr expr)
        |   ^(EQ ID expr)
        |   atom
        ;
    
    atom
        : ID      
        | INT      
        ;
  END
  
  compile_options :trace => true
  
  example "processing AST output from a parser with a tree parser" do
    input_source = <<-END.fixed_indent( 0 )
      char c;
      int x;
      
      void bar(int x);
      
      int foo(int y, char d) {
        int i;
        for (i=0; i<3; i=i+1) {
          x=3;
          y=5;
        }
      }
    END
    
    lexer = GenericLanguage::Lexer.new( input_source )
    parser = GenericLanguage::Parser.new( lexer )
    
    expected_tree = <<-END.strip!.gsub!( /\s+/, ' ' )
      (VAR_DEF char c)
      (VAR_DEF int x)
      (FUNC_DECL (FUNC_HDR void bar (ARG_DEF int x)))
      (FUNC_DEF
        (FUNC_HDR int foo (ARG_DEF int y) (ARG_DEF char d))
        (BLOCK
          (VAR_DEF int i)
          (for (= i 0) (< i 3) (= i (+ i 1))
            (BLOCK (= x 3) (= y 5)))))
    END
    
    result = parser.program
    result.tree.inspect.should == expected_tree
    
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = parser.input
    tree_parser = GenericLanguageWalker::TreeParser.new( nodes )
    
    tree_parser.program
    tree_parser.traces.should == %w(
      >program          >declaration      >variable         >type           
      <type             >declarator       <declarator       <variable       
      <declaration      >declaration      >variable         >type           
      <type             >declarator       <declarator       <variable       
      <declaration      >declaration      >functionHeader   >type           
      <type             >formalParameter  >type             <type           
      >declarator       <declarator       <formalParameter  <functionHeader 
      <declaration      >declaration      >functionHeader   >type           
      <type             >formalParameter  >type             <type           
      >declarator       <declarator       <formalParameter  >formalParameter
      >type             <type             >declarator       <declarator     
      <formalParameter  <functionHeader   >block            >variable       
      >type             <type             >declarator       <declarator     
      <variable         >stat             >forStat          >expr           
      >expr             >atom             <atom             <expr           
      <expr             >expr             >expr             >atom           
      <atom             <expr             >expr             >atom           
      <atom             <expr             <expr             >expr           
      >expr             >expr             >atom             <atom           
      <expr             >expr             >atom             <atom           
      <expr             <expr             <expr             >block          
      >stat             >expr             >expr             >atom           
      <atom             <expr             <expr             <stat           
      >stat             >expr             >expr             >atom           
      <atom             <expr             <expr             <stat           
      <block            <forStat          <stat             <block          
      <declaration      <program        
    )
  end
  
  example 'tree parser rule label property references' do
    input = "char c;\n"
    lexer  = GenericLanguage::Lexer.new( "char c;\n" )
    parser = GenericLanguage::Parser.new( lexer )
    
    result = parser.variable
    nodes = ANTLR3::AST::CommonTreeNodeStream.new( result.tree )
    nodes.token_stream = parser.input
    
    tree_parser = GenericLanguageWalker::TreeParser.new( nodes )
    tree_parser.variable.should == 'c'
  end
  
end