1import antlr3 2import testbase 3import unittest 4import textwrap 5 6 7class t022scopes(testbase.ANTLRTest): 8 def setUp(self): 9 self.compileGrammar() 10 11 12 def parserClass(self, base): 13 class TParser(base): 14 def emitErrorMessage(self, msg): 15 # report errors to /dev/null 16 pass 17 18 def reportError(self, re): 19 # no error recovery yet, just crash! 20 raise re 21 22 return TParser 23 24 25 def testa1(self): 26 cStream = antlr3.StringStream('foobar') 27 lexer = self.getLexer(cStream) 28 tStream = antlr3.CommonTokenStream(lexer) 29 parser = self.getParser(tStream) 30 parser.a() 31 32 33 def testb1(self): 34 cStream = antlr3.StringStream('foobar') 35 lexer = self.getLexer(cStream) 36 tStream = antlr3.CommonTokenStream(lexer) 37 parser = self.getParser(tStream) 38 39 try: 40 parser.b(False) 41 self.fail() 42 except antlr3.RecognitionException: 43 pass 44 45 46 def testb2(self): 47 cStream = antlr3.StringStream('foobar') 48 lexer = self.getLexer(cStream) 49 tStream = antlr3.CommonTokenStream(lexer) 50 parser = self.getParser(tStream) 51 parser.b(True) 52 53 54 def testc1(self): 55 cStream = antlr3.StringStream( 56 textwrap.dedent('''\ 57 { 58 int i; 59 int j; 60 i = 0; 61 } 62 ''')) 63 64 lexer = self.getLexer(cStream) 65 tStream = antlr3.CommonTokenStream(lexer) 66 parser = self.getParser(tStream) 67 symbols = parser.c() 68 69 self.failUnlessEqual( 70 symbols, 71 set(['i', 'j']) 72 ) 73 74 75 def testc2(self): 76 cStream = antlr3.StringStream( 77 textwrap.dedent('''\ 78 { 79 int i; 80 int j; 81 i = 0; 82 x = 4; 83 } 84 ''')) 85 86 lexer = self.getLexer(cStream) 87 tStream = antlr3.CommonTokenStream(lexer) 88 parser = self.getParser(tStream) 89 90 try: 91 parser.c() 92 self.fail() 93 except RuntimeError, exc: 94 self.failUnlessEqual(exc.args[0], 'x') 95 96 97 def testd1(self): 98 cStream = antlr3.StringStream( 99 textwrap.dedent('''\ 100 { 101 int i; 102 int j; 103 i = 0; 104 { 105 int i; 106 int x; 107 x = 5; 108 } 109 } 110 ''')) 111 112 lexer = self.getLexer(cStream) 113 tStream = antlr3.CommonTokenStream(lexer) 114 parser = self.getParser(tStream) 115 symbols = parser.d() 116 117 self.failUnlessEqual( 118 symbols, 119 set(['i', 'j']) 120 ) 121 122 123 def teste1(self): 124 cStream = antlr3.StringStream( 125 textwrap.dedent('''\ 126 { { { { 12 } } } } 127 ''')) 128 129 lexer = self.getLexer(cStream) 130 tStream = antlr3.CommonTokenStream(lexer) 131 parser = self.getParser(tStream) 132 res = parser.e() 133 134 self.failUnlessEqual(res, 12) 135 136 137 def testf1(self): 138 cStream = antlr3.StringStream( 139 textwrap.dedent('''\ 140 { { { { 12 } } } } 141 ''')) 142 143 lexer = self.getLexer(cStream) 144 tStream = antlr3.CommonTokenStream(lexer) 145 parser = self.getParser(tStream) 146 res = parser.f() 147 148 self.failUnlessEqual(res, None) 149 150 151 def testf2(self): 152 cStream = antlr3.StringStream( 153 textwrap.dedent('''\ 154 { { 12 } } 155 ''')) 156 157 lexer = self.getLexer(cStream) 158 tStream = antlr3.CommonTokenStream(lexer) 159 parser = self.getParser(tStream) 160 res = parser.f() 161 162 self.failUnlessEqual(res, None) 163 164 165 166if __name__ == '__main__': 167 unittest.main() 168