1 import antlr3 2 import testbase 3 import unittest 4 5 class t003lexer(testbase.ANTLRTest): 6 def setUp(self): 7 self.compileGrammar() 8 9 10 def lexerClass(self, base): 11 class TLexer(base): 12 def emitErrorMessage(self, msg): 13 # report errors to /dev/null 14 pass 15 16 def reportError(self, re): 17 # no error recovery yet, just crash! 18 raise re 19 20 return TLexer 21 22 23 def testValid(self): 24 stream = antlr3.StringStream('0fooze1') 25 lexer = self.getLexer(stream) 26 27 token = lexer.nextToken() 28 self.assertEqual(token.type, self.lexerModule.ZERO) 29 30 token = lexer.nextToken() 31 self.assertEqual(token.type, self.lexerModule.FOOZE) 32 33 token = lexer.nextToken() 34 self.assertEqual(token.type, self.lexerModule.ONE) 35 36 token = lexer.nextToken() 37 self.assertEqual(token.type, self.lexerModule.EOF) 38 39 40 def testMalformedInput(self): 41 stream = antlr3.StringStream('2') 42 lexer = self.getLexer(stream) 43 44 try: 45 token = lexer.nextToken() 46 self.fail() 47 48 except antlr3.NoViableAltException as exc: 49 self.assertEqual(exc.unexpectedType, '2') 50 51 52 if __name__ == '__main__': 53 unittest.main() 54