1import antlr3 2import testbase 3import unittest 4 5 6class t026actions(testbase.ANTLRTest): 7 def parserClass(self, base): 8 class TParser(base): 9 def __init__(self, *args, **kwargs): 10 base.__init__(self, *args, **kwargs) 11 12 self._errors = [] 13 self._output = "" 14 15 16 def capture(self, t): 17 self._output += t 18 19 20 def emitErrorMessage(self, msg): 21 self._errors.append(msg) 22 23 24 return TParser 25 26 27 def lexerClass(self, base): 28 class TLexer(base): 29 def __init__(self, *args, **kwargs): 30 base.__init__(self, *args, **kwargs) 31 32 self._errors = [] 33 self._output = "" 34 35 36 def capture(self, t): 37 self._output += t 38 39 40 def emitErrorMessage(self, msg): 41 self._errors.append(msg) 42 43 44 return TLexer 45 46 47 def setUp(self): 48 self.compileGrammar() 49 50 51 def testValid1(self): 52 cStream = antlr3.StringStream('foobar _Ab98 \n A12sdf') 53 lexer = self.getLexer(cStream) 54 tStream = antlr3.CommonTokenStream(lexer) 55 parser = self.getParser(tStream) 56 parser.prog() 57 58 self.assertEqual( 59 parser._output, 60 'init;after;finally;') 61 self.assertEqual( 62 lexer._output, 63 'action;u\'foobar\' 4 1 0 -1 0 0 5;attribute;action;u\'_Ab98\' 4 1 7 -1 0 7 11;attribute;action;u\'A12sdf\' 4 2 1 -1 0 15 20;attribute;') 64 65if __name__ == '__main__': 66 unittest.main() 67