• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import antlr3
2import testbase
3import unittest
4
5class t006lexer(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('fofaaooa')
25        lexer = self.getLexer(stream)
26
27        token = lexer.nextToken()
28        assert token.type == self.lexerModule.FOO
29        assert token.start == 0, token.start
30        assert token.stop == 1, token.stop
31        assert token.text == 'fo', token.text
32
33        token = lexer.nextToken()
34        assert token.type == self.lexerModule.FOO
35        assert token.start == 2, token.start
36        assert token.stop == 7, token.stop
37        assert token.text == 'faaooa', token.text
38
39        token = lexer.nextToken()
40        assert token.type == self.lexerModule.EOF
41
42
43    def testMalformedInput(self):
44        stream = antlr3.StringStream('fofoaooaoa2')
45        lexer = self.getLexer(stream)
46
47        lexer.nextToken()
48        lexer.nextToken()
49        try:
50            token = lexer.nextToken()
51            raise AssertionError, token
52
53        except antlr3.MismatchedTokenException, exc:
54            assert exc.expecting == 'f', repr(exc.expecting)
55            assert exc.unexpectedType == '2', repr(exc.unexpectedType)
56            assert exc.charPositionInLine == 10, repr(exc.charPositionInLine)
57            assert exc.line == 1, repr(exc.line)
58
59
60if __name__ == '__main__':
61    unittest.main()
62