1*16467b97STreehugger Robotimport antlr3 2*16467b97STreehugger Robotimport testbase 3*16467b97STreehugger Robotimport unittest 4*16467b97STreehugger Robot 5*16467b97STreehugger Robotclass t008lexer(testbase.ANTLRTest): 6*16467b97STreehugger Robot def setUp(self): 7*16467b97STreehugger Robot self.compileGrammar() 8*16467b97STreehugger Robot 9*16467b97STreehugger Robot 10*16467b97STreehugger Robot def lexerClass(self, base): 11*16467b97STreehugger Robot class TLexer(base): 12*16467b97STreehugger Robot def emitErrorMessage(self, msg): 13*16467b97STreehugger Robot # report errors to /dev/null 14*16467b97STreehugger Robot pass 15*16467b97STreehugger Robot 16*16467b97STreehugger Robot def reportError(self, re): 17*16467b97STreehugger Robot # no error recovery yet, just crash! 18*16467b97STreehugger Robot raise re 19*16467b97STreehugger Robot 20*16467b97STreehugger Robot return TLexer 21*16467b97STreehugger Robot 22*16467b97STreehugger Robot 23*16467b97STreehugger Robot def testValid(self): 24*16467b97STreehugger Robot stream = antlr3.StringStream('ffaf') 25*16467b97STreehugger Robot lexer = self.getLexer(stream) 26*16467b97STreehugger Robot 27*16467b97STreehugger Robot token = lexer.nextToken() 28*16467b97STreehugger Robot assert token.type == self.lexerModule.FOO 29*16467b97STreehugger Robot assert token.start == 0, token.start 30*16467b97STreehugger Robot assert token.stop == 0, token.stop 31*16467b97STreehugger Robot assert token.text == 'f', token.text 32*16467b97STreehugger Robot 33*16467b97STreehugger Robot token = lexer.nextToken() 34*16467b97STreehugger Robot assert token.type == self.lexerModule.FOO 35*16467b97STreehugger Robot assert token.start == 1, token.start 36*16467b97STreehugger Robot assert token.stop == 2, token.stop 37*16467b97STreehugger Robot assert token.text == 'fa', token.text 38*16467b97STreehugger Robot 39*16467b97STreehugger Robot token = lexer.nextToken() 40*16467b97STreehugger Robot assert token.type == self.lexerModule.FOO 41*16467b97STreehugger Robot assert token.start == 3, token.start 42*16467b97STreehugger Robot assert token.stop == 3, token.stop 43*16467b97STreehugger Robot assert token.text == 'f', token.text 44*16467b97STreehugger Robot 45*16467b97STreehugger Robot token = lexer.nextToken() 46*16467b97STreehugger Robot assert token.type == self.lexerModule.EOF 47*16467b97STreehugger Robot 48*16467b97STreehugger Robot 49*16467b97STreehugger Robot def testMalformedInput(self): 50*16467b97STreehugger Robot stream = antlr3.StringStream('fafb') 51*16467b97STreehugger Robot lexer = self.getLexer(stream) 52*16467b97STreehugger Robot 53*16467b97STreehugger Robot lexer.nextToken() 54*16467b97STreehugger Robot lexer.nextToken() 55*16467b97STreehugger Robot try: 56*16467b97STreehugger Robot token = lexer.nextToken() 57*16467b97STreehugger Robot raise AssertionError, token 58*16467b97STreehugger Robot 59*16467b97STreehugger Robot except antlr3.MismatchedTokenException, exc: 60*16467b97STreehugger Robot assert exc.unexpectedType == 'b', repr(exc.unexpectedType) 61*16467b97STreehugger Robot assert exc.charPositionInLine == 3, repr(exc.charPositionInLine) 62*16467b97STreehugger Robot assert exc.line == 1, repr(exc.line) 63*16467b97STreehugger Robot 64*16467b97STreehugger Robot 65*16467b97STreehugger Robotif __name__ == '__main__': 66*16467b97STreehugger Robot unittest.main() 67