1 #!/usr/bin/ruby 2 # encoding: utf-8 3 4 require 'antlr3/test/functional' 5 6 class TestParameters < ANTLR3::Test::Functional 7 8 inline_grammar( <<-'END' ) 9 grammar Parameters; 10 options { 11 language = Ruby; 12 } 13 14 @parser::members { 15 def emit_error_message(msg) 16 # do nothing 17 end 18 def report_error(error) 19 raise error 20 end 21 } 22 23 @lexer::members { 24 def emit_error_message(msg) 25 # do nothing 26 end 27 def report_error(error) 28 raise error 29 end 30 } 31 32 a[arg1, arg2] returns [l] 33 : A+ EOF 34 { 35 l = [$arg1, $arg2] 36 $arg1 = "gnarz" 37 } 38 ; 39 40 A: 'a'..'z'; 41 42 WS: ' '+ { $channel = HIDDEN }; 43 END 44 45 example "rules with method parameters" do 46 lexer = Parameters::Lexer.new( 'a a a' ) 47 parser = Parameters::Parser.new lexer 48 r = parser.a( 'foo', 'bar' ) 49 r.should == %w(foo bar) 50 end 51 52 end 53 54 55 class TestMultipleReturnValues < ANTLR3::Test::Functional 56 57 inline_grammar( <<-'END' ) 58 grammar MultipleReturnValues; 59 options { language = Ruby; } 60 @parser::members { 61 def emit_error_message(msg) 62 # do nothing 63 end 64 def report_error(error) 65 raise error 66 end 67 } 68 69 @lexer::members { 70 def emit_error_message(msg) 71 # do nothing 72 end 73 def report_error(error) 74 raise error 75 end 76 } 77 78 a returns [foo, bar]: A 79 { 80 $foo = "foo"; 81 $bar = "bar"; 82 } 83 ; 84 85 A: 'a'..'z'; 86 87 WS : 88 ( ' ' 89 | '\t' 90 | ( '\n' 91 | '\r\n' 92 | '\r' 93 ) 94 )+ 95 { $channel = HIDDEN } 96 ; 97 END 98 99 example "multi-valued rule return structures" do 100 lexer = MultipleReturnValues::Lexer.new( ' a' ) 101 parser = MultipleReturnValues::Parser.new lexer 102 ret = parser.a 103 104 ret.foo.should == 'foo' 105 ret.bar.should == 'bar' 106 end 107 108 end 109 110 111 class TestRuleVisibility < ANTLR3::Test::Functional 112 inline_grammar( <<-'END' ) 113 grammar RuleVisibility; 114 options { language=Ruby; } 115 116 public a: ID; 117 private b: DIGIT; 118 protected c: ID DIGIT; 119 120 DIGIT: ('0'..'9')+; 121 ID: ('a'..'z' | 'A'..'Z')+; 122 WS: (' ' | '\t' | '\n' | '\r' | '\f')+ { $channel=HIDDEN; }; 123 END 124 125 example 'using visibility modifiers on rules' do 126 mname = RUBY_VERSION =~ /^1\.9/ ? proc { | n | n.to_sym } : proc { | n | n.to_s } 127 128 RuleVisibility::Parser.public_instance_methods.should include( mname[ 'a' ] ) 129 RuleVisibility::Parser.protected_instance_methods.should include( mname[ 'c' ] ) 130 RuleVisibility::Parser.private_instance_methods.should include( mname[ 'b' ] ) 131 end 132 133 end 134