• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import antlr3
2import testbase
3import unittest
4
5class t009lexer(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('085')
25        lexer = self.getLexer(stream)
26
27        token = lexer.nextToken()
28        assert token.type == self.lexerModule.DIGIT
29        assert token.start == 0, token.start
30        assert token.stop == 0, token.stop
31        assert token.text == '0', token.text
32
33        token = lexer.nextToken()
34        assert token.type == self.lexerModule.DIGIT
35        assert token.start == 1, token.start
36        assert token.stop == 1, token.stop
37        assert token.text == '8', token.text
38
39        token = lexer.nextToken()
40        assert token.type == self.lexerModule.DIGIT
41        assert token.start == 2, token.start
42        assert token.stop == 2, token.stop
43        assert token.text == '5', token.text
44
45        token = lexer.nextToken()
46        assert token.type == self.lexerModule.EOF
47
48
49    def testMalformedInput(self):
50        stream = antlr3.StringStream('2a')
51        lexer = self.getLexer(stream)
52
53        lexer.nextToken()
54        try:
55            token = lexer.nextToken()
56            raise AssertionError, token
57
58        except antlr3.MismatchedSetException, exc:
59            # TODO: This should provide more useful information
60            assert exc.expecting is None
61            assert exc.unexpectedType == 'a', repr(exc.unexpectedType)
62            assert exc.charPositionInLine == 1, repr(exc.charPositionInLine)
63            assert exc.line == 1, repr(exc.line)
64
65
66if __name__ == '__main__':
67    unittest.main()
68