Home | History | Annotate | Download | only in parser
      1 #!/usr/bin/ruby
      2 # encoding: utf-8
      3 
      4 require 'antlr3/test/functional'
      5 
      6 class TestRulePropertyReference < ANTLR3::Test::Functional
      7 
      8   inline_grammar( <<-'END' )
      9     grammar RuleProperties;
     10     options { language = Ruby; }
     11     
     12     @parser::members {
     13       def emit_error_message(msg)
     14         # do nothing
     15       end
     16       def report_error(error)
     17         raise error
     18       end
     19     }
     20     
     21     @lexer::members {
     22       def emit_error_message(msg)
     23         # do nothing
     24       end
     25       def report_error(error)
     26         raise error
     27       end
     28     }
     29     
     30     a returns [bla]
     31     @after { $bla = [$start, $stop, $text] }
     32         : A+
     33         ;
     34     
     35     A: 'a'..'z';
     36     
     37     WS: ' '+  { $channel = HIDDEN };
     38   END
     39   
     40   example "accessing rule properties" do
     41     lexer = RuleProperties::Lexer.new( '   a a a a  ' )
     42     parser = RuleProperties::Parser.new lexer
     43     start, stop, text = parser.a.bla
     44     
     45     start.index.should == 1
     46     stop.index.should == 7
     47     text.should == 'a a a a'
     48   end
     49 
     50 
     51 end
     52 
     53 class TestLabels < ANTLR3::Test::Functional
     54 
     55   inline_grammar( <<-'END' )
     56     grammar Labels;
     57     options { language = Ruby; }
     58     
     59     @parser::members {
     60       def recover(e)
     61         raise e
     62       end
     63     }
     64     
     65     @lexer::members {
     66       def recover(e)
     67         raise e
     68       end
     69     }
     70     
     71     a returns [l]
     72         : ids+=A ( ',' ids+=(A|B) )* C D w=. ids+=. F EOF
     73             { $l = [$ids, $w] }
     74         ;
     75     
     76     A: 'a'..'z';
     77     B: '0'..'9';
     78     C: a='A' { $a };
     79     D: a='FOOBAR' { $a };
     80     E: 'GNU' a=. { $a };
     81     F: 'BLARZ' a=EOF { $a };
     82     
     83     WS: ' '+  { $channel = HIDDEN };
     84   END
     85   
     86   example "parsing 'a, b, c, 1, 2 A FOOBAR GNU1 A BLARZ'" do
     87     lexer = Labels::Lexer.new 'a, b, c, 1, 2 A FOOBAR GNU1 A BLARZ'
     88     parser = Labels::Parser.new lexer
     89     ids, w = parser.a
     90     
     91     ids.should have( 6 ).things
     92     ids[ 0 ].text.should == 'a'
     93     ids[ 1 ].text.should == 'b'
     94     ids[ 2 ].text.should == 'c'
     95     ids[ 3 ].text.should == '1'
     96     ids[ 4 ].text.should == '2'
     97     ids[ 5 ].text.should == 'A'
     98     
     99     w.text.should == 'GNU1'
    100   end
    101 
    102 
    103 end
    104 
    105 
    106 class TestTokenLabelReference < ANTLR3::Test::Functional
    107 
    108   inline_grammar( <<-'END' )
    109     grammar TokenLabels;
    110     options {
    111       language = Ruby;
    112     }
    113     
    114     @parser::members {
    115       def emit_error_message(msg)
    116         # do nothing
    117       end
    118       def report_error(error)
    119         raise error
    120       end
    121     }
    122     
    123     @lexer::members {
    124       def emit_error_message(msg)
    125         # do nothing
    126       end
    127       def report_error(error)
    128         raise error
    129       end
    130     }
    131     
    132     a returns [$tk]
    133       : t=A
    134           {
    135             $tk = [
    136               $t.text,
    137               $t.type,
    138               $t.name,
    139               $t.line,
    140               $t.pos,
    141               $t.index,
    142               $t.channel
    143             ]
    144           }
    145       ;
    146     
    147     A: 'a'..'z';
    148     
    149     WS  :
    150             (   ' '
    151             |   '\t'
    152             |  ( '\n'
    153                 |	'\r\n'
    154                 |	'\r'
    155                 )
    156             )+
    157             { $channel = HIDDEN }
    158         ;
    159   END
    160   
    161   example "accessing tokens with labels" do
    162     lexer = TokenLabels::Lexer.new( '   a' )
    163     parser = TokenLabels::Parser.new lexer
    164     tk = parser.a
    165     tk.should == [ 
    166       'a', TokenLabels::TokenData::A, 'A',
    167       1, 3, 1, :default
    168     ]
    169   end
    170 
    171 
    172 end
    173 
    174 class TestRuleLabelReference < ANTLR3::Test::Functional
    175 
    176   inline_grammar( <<-'END' )
    177     grammar RuleLabelReference;
    178     options {language = Ruby;}
    179     
    180     @parser::members {
    181       def emit_error_message(msg)
    182         # do nothing
    183       end
    184       def report_error(error)
    185         raise error
    186       end
    187     }
    188     
    189     @lexer::members {
    190       def emit_error_message(msg)
    191         # do nothing
    192       end
    193       def report_error(error)
    194         raise error
    195       end
    196     }
    197     
    198     a returns [bla]: t=b
    199             {
    200                 $bla = [$t.start, $t.stop, $t.text]
    201             }
    202         ;
    203     
    204     b: A+;
    205     
    206     A: 'a'..'z';
    207     
    208     WS: ' '+  { $channel = HIDDEN };
    209   END
    210   
    211   example "referencing rule properties using rule labels" do
    212     lexer = RuleLabelReference::Lexer.new( '   a a a a  ' )
    213     parser = RuleLabelReference::Parser.new lexer
    214     start, stop, text = parser.a
    215     
    216     start.index.should == 1
    217     stop.index.should == 7
    218     text.should == 'a a a a'
    219   end
    220 
    221 end
    222 
    223 
    224 
    225 class TestReferenceDoesntSetChannel < ANTLR3::Test::Functional
    226 
    227   inline_grammar( <<-'END' )
    228     grammar ReferenceSetChannel;
    229     options {language=Ruby;}
    230     a returns [foo]: A EOF { $foo = '\%s, channel=\%p' \% [$A.text, $A.channel]; } ;
    231     A : '-' WS I ;
    232     I : '0'..'9'+ ;
    233     WS: ' ' | '\t';
    234   END
    235 
    236   example 'verifying that a token reference does not set its channel' do
    237     lexer = ReferenceSetChannel::Lexer.new( "- 34" )
    238     parser = ReferenceSetChannel::Parser.new lexer
    239     parser.a.should == "- 34, channel=:default"
    240   end
    241 
    242 end
    243