• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import antlr3
2import testbase
3import unittest
4
5class t004lexer(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('ffofoofooo')
25        lexer = self.getLexer(stream)
26
27        token = lexer.nextToken()
28        self.assertEqual(token.type, self.lexerModule.FOO)
29        self.assertEqual(token.start, 0)
30        self.assertEqual(token.stop, 0)
31        self.assertEqual(token.text, 'f')
32
33        token = lexer.nextToken()
34        self.assertEqual(token.type, self.lexerModule.FOO)
35        self.assertEqual(token.start, 1)
36        self.assertEqual(token.stop, 2)
37        self.assertEqual(token.text, 'fo')
38
39        token = lexer.nextToken()
40        self.assertEqual(token.type, self.lexerModule.FOO)
41        self.assertEqual(token.start, 3)
42        self.assertEqual(token.stop, 5)
43        self.assertEqual(token.text, 'foo')
44
45        token = lexer.nextToken()
46        self.assertEqual(token.type, self.lexerModule.FOO)
47        self.assertEqual(token.start, 6)
48        self.assertEqual(token.stop, 9)
49        self.assertEqual(token.text, 'fooo')
50
51        token = lexer.nextToken()
52        self.assertEqual(token.type, self.lexerModule.EOF)
53
54
55    def testMalformedInput(self):
56        stream = antlr3.StringStream('2')
57        lexer = self.getLexer(stream)
58
59        try:
60            token = lexer.nextToken()
61            self.fail()
62
63        except antlr3.MismatchedTokenException as exc:
64            self.assertEqual(exc.expecting, 'f')
65            self.assertEqual(exc.unexpectedType, '2')
66
67
68if __name__ == '__main__':
69    unittest.main()
70
71