• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import antlr3
2import testbase
3import unittest
4
5class t002lexer(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('01')
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.ONE)
32
33        token = lexer.nextToken()
34        self.assertEqual(token.type, self.lexerModule.EOF)
35
36
37    def testMalformedInput(self):
38        stream = antlr3.StringStream('2')
39        lexer = self.getLexer(stream)
40
41        try:
42            token = lexer.nextToken()
43            self.fail()
44
45        except antlr3.NoViableAltException as exc:
46            self.assertEqual(exc.unexpectedType, '2')
47
48
49if __name__ == '__main__':
50    unittest.main()
51