• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import antlr3
2import testbase
3import unittest
4
5class t001lexer(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('0')
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.EOF)
32
33
34    def testIteratorInterface(self):
35        stream = antlr3.StringStream('0')
36        lexer = self.getLexer(stream)
37
38        types = [token.type for token in lexer]
39
40        self.assertEqual(types, [self.lexerModule.ZERO])
41
42
43    def testMalformedInput(self):
44        stream = antlr3.StringStream('1')
45        lexer = self.getLexer(stream)
46
47        try:
48            token = lexer.nextToken()
49            self.fail()
50
51        except antlr3.MismatchedTokenException as exc:
52            self.assertEqual(exc.expecting, '0')
53            self.assertEqual(exc.unexpectedType, '1')
54
55
56if __name__ == '__main__':
57    unittest.main()
58