1*16467b97STreehugger Robotimport antlr3 2*16467b97STreehugger Robotimport testbase 3*16467b97STreehugger Robotimport unittest 4*16467b97STreehugger Robot 5*16467b97STreehugger Robotclass t007lexer(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('fofababbooabb') 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 == 1, token.stop 31*16467b97STreehugger Robot assert token.text == 'fo', 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 == 2, token.start 36*16467b97STreehugger Robot assert token.stop == 12, token.stop 37*16467b97STreehugger Robot assert token.text == 'fababbooabb', token.text 38*16467b97STreehugger Robot 39*16467b97STreehugger Robot token = lexer.nextToken() 40*16467b97STreehugger Robot assert token.type == self.lexerModule.EOF 41*16467b97STreehugger Robot 42*16467b97STreehugger Robot 43*16467b97STreehugger Robot def testMalformedInput(self): 44*16467b97STreehugger Robot stream = antlr3.StringStream('foaboao') 45*16467b97STreehugger Robot lexer = self.getLexer(stream) 46*16467b97STreehugger Robot 47*16467b97STreehugger Robot try: 48*16467b97STreehugger Robot token = lexer.nextToken() 49*16467b97STreehugger Robot raise AssertionError, token 50*16467b97STreehugger Robot 51*16467b97STreehugger Robot except antlr3.EarlyExitException, exc: 52*16467b97STreehugger Robot assert exc.unexpectedType == 'o', repr(exc.unexpectedType) 53*16467b97STreehugger Robot assert exc.charPositionInLine == 6, repr(exc.charPositionInLine) 54*16467b97STreehugger Robot assert exc.line == 1, repr(exc.line) 55*16467b97STreehugger Robot 56*16467b97STreehugger Robot 57*16467b97STreehugger Robotif __name__ == '__main__': 58*16467b97STreehugger Robot unittest.main() 59*16467b97STreehugger Robot 60