1#!/usr/bin/ruby 2# encoding: utf-8 3 4require 'antlr3/test/functional' 5 6class TestEmptyAlternative < ANTLR3::Test::Functional 7 8 inline_grammar( <<-'END' ) 9 grammar EmptyAlt; 10 options { 11 language = Ruby; 12 } 13 14 r 15 : NAME 16 ( WS+ NAME 17 | 18 ) 19 EOF 20 ; 21 22 NAME: ('a'..'z') ('a'..'z' | '0'..'9')+; 23 NUMBER: ('0'..'9')+; 24 WS: ' '+; 25 END 26 27 example "rule with empty alternative" do 28 lexer = EmptyAlt::Lexer.new( 'foo' ) 29 parser = EmptyAlt::Parser.new lexer 30 events = parser.r 31 end 32 33end 34 35class TestSubrulePrediction < ANTLR3::Test::Functional 36 37 inline_grammar( <<-'END' ) 38 grammar Subrule; 39 options { 40 language = Ruby; 41 } 42 43 @parser::members { 44 def emit_error_message(msg) 45 # do nothing 46 end 47 def report_error(error) 48 raise error 49 end 50 } 51 52 a: 'BEGIN' b WS+ 'END'; 53 b: ( WS+ 'A' )+; 54 WS: ' '; 55 END 56 57 example "make correct predictions involving subrules" do 58 lexer = Subrule::Lexer.new( 'BEGIN A END' ) 59 parser = Subrule::Parser.new lexer 60 events = parser.a 61 end 62 63end 64 65 66class TestSpecialStates < ANTLR3::Test::Functional 67 68 inline_grammar( <<-'END' ) 69 grammar SpecialStates; 70 options { language = Ruby; } 71 72 @init { @cond = true } 73 74 @members { 75 attr_accessor :cond 76 def recover(re) 77 raise re 78 end 79 } 80 81 r 82 : ( { @cond }? NAME 83 | {!@cond }? NAME WS+ NAME 84 ) 85 ( WS+ NAME )? 86 EOF 87 ; 88 89 NAME: ('a'..'z') ('a'..'z' | '0'..'9')+; 90 NUMBER: ('0'..'9')+; 91 WS: ' '+; 92 END 93 94 example "parsing 'foo'" do 95 lexer = SpecialStates::Lexer.new 'foo' 96 parser = SpecialStates::Parser.new lexer 97 parser.r 98 end 99 100 example "parsing 'foo name1'" do 101 lexer = SpecialStates::Lexer.new 'foo name1' 102 parser = SpecialStates::Parser.new lexer 103 parser.r 104 end 105 106 example "parsing 'bar name1'" do 107 lexer = SpecialStates::Lexer.new 'bar name1' 108 parser = SpecialStates::Parser.new lexer 109 parser.cond = false 110 parser.r 111 end 112 113 example "parsing 'bar name1 name2'" do 114 lexer = SpecialStates::Lexer.new 'bar name1 name2' 115 parser = SpecialStates::Parser.new lexer 116 parser.cond = false 117 parser.r 118 end 119end 120 121 122class TestDFABug < ANTLR3::Test::Functional 123 124 inline_grammar( <<-'END' ) 125 grammar DFABug; 126 options { 127 language = Ruby; 128 output = AST; 129 } 130 131 132 // this rule used to generate an infinite loop in DFA.predict 133 r 134 options { backtrack=true; } 135 : (modifier+ INT)=> modifier+ expression 136 | modifier+ statement 137 ; 138 139 expression 140 : INT '+' INT 141 ; 142 143 statement 144 : 'fooze' 145 | 'fooze2' 146 ; 147 148 modifier 149 : 'public' 150 | 'private' 151 ; 152 153 ID : 'a'..'z' + ; 154 INT : '0'..'9' +; 155 WS: (' ' | '\n' | '\t')+ {$channel = HIDDEN;}; 156 END 157 158 example "testing for DFA-based decision bug" do 159 lexer = DFABug::Lexer.new 'public fooze' 160 parser = DFABug::Parser.new lexer 161 parser.r 162 end 163 164end 165