1 import antlr3 2 import testbase 3 import unittest 4 5 class t007lexer(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('fofababbooabb') 25 lexer = self.getLexer(stream) 26 27 token = lexer.nextToken() 28 self.assertEqual(token.type, self.lexerModule.FOO) 29 self.assertEqual(token.start, 0) 30 self.assertEqual(token.stop, 1) 31 self.assertEqual(token.text, 'fo') 32 33 token = lexer.nextToken() 34 self.assertEqual(token.type, self.lexerModule.FOO) 35 self.assertEqual(token.start, 2) 36 self.assertEqual(token.stop, 12) 37 self.assertEqual(token.text, 'fababbooabb') 38 39 token = lexer.nextToken() 40 self.assertEqual(token.type, self.lexerModule.EOF) 41 42 43 def testMalformedInput(self): 44 stream = antlr3.StringStream('foaboao') 45 lexer = self.getLexer(stream) 46 47 try: 48 token = lexer.nextToken() 49 self.fail(token) 50 51 except antlr3.EarlyExitException as exc: 52 self.assertEqual(exc.unexpectedType, 'o') 53 self.assertEqual(exc.charPositionInLine, 6) 54 self.assertEqual(exc.line, 1) 55 56 57 if __name__ == '__main__': 58 unittest.main() 59 60