xref: /aosp_15_r20/external/antlr/runtime/Python/tests/t005lexer.py (revision 16467b971bd3e2009fad32dd79016f2c7e421deb)
1*16467b97STreehugger Robotimport antlr3
2*16467b97STreehugger Robotimport testbase
3*16467b97STreehugger Robotimport unittest
4*16467b97STreehugger Robot
5*16467b97STreehugger Robotclass t005lexer(testbase.ANTLRTest):
6*16467b97STreehugger Robot    def setUp(self):
7*16467b97STreehugger Robot        self.compileGrammar()
8*16467b97STreehugger Robot
9*16467b97STreehugger Robot
10*16467b97STreehugger Robot    def lexerClass(self, base):
11*16467b97STreehugger Robot        class TLexer(base):
12*16467b97STreehugger Robot            def emitErrorMessage(self, msg):
13*16467b97STreehugger Robot                # report errors to /dev/null
14*16467b97STreehugger Robot                pass
15*16467b97STreehugger Robot
16*16467b97STreehugger Robot            def reportError(self, re):
17*16467b97STreehugger Robot                # no error recovery yet, just crash!
18*16467b97STreehugger Robot                raise re
19*16467b97STreehugger Robot
20*16467b97STreehugger Robot        return TLexer
21*16467b97STreehugger Robot
22*16467b97STreehugger Robot
23*16467b97STreehugger Robot    def testValid(self):
24*16467b97STreehugger Robot        stream = antlr3.StringStream('fofoofooo')
25*16467b97STreehugger Robot        lexer = self.getLexer(stream)
26*16467b97STreehugger Robot
27*16467b97STreehugger Robot        token = lexer.nextToken()
28*16467b97STreehugger Robot        assert token.type == self.lexerModule.FOO
29*16467b97STreehugger Robot        assert token.start == 0, token.start
30*16467b97STreehugger Robot        assert token.stop == 1, token.stop
31*16467b97STreehugger Robot        assert token.text == 'fo', token.text
32*16467b97STreehugger Robot
33*16467b97STreehugger Robot        token = lexer.nextToken()
34*16467b97STreehugger Robot        assert token.type == self.lexerModule.FOO
35*16467b97STreehugger Robot        assert token.start == 2, token.start
36*16467b97STreehugger Robot        assert token.stop == 4, token.stop
37*16467b97STreehugger Robot        assert token.text == 'foo', token.text
38*16467b97STreehugger Robot
39*16467b97STreehugger Robot        token = lexer.nextToken()
40*16467b97STreehugger Robot        assert token.type == self.lexerModule.FOO
41*16467b97STreehugger Robot        assert token.start == 5, token.start
42*16467b97STreehugger Robot        assert token.stop == 8, token.stop
43*16467b97STreehugger Robot        assert token.text == 'fooo', token.text
44*16467b97STreehugger Robot
45*16467b97STreehugger Robot        token = lexer.nextToken()
46*16467b97STreehugger Robot        assert token.type == self.lexerModule.EOF
47*16467b97STreehugger Robot
48*16467b97STreehugger Robot
49*16467b97STreehugger Robot    def testMalformedInput1(self):
50*16467b97STreehugger Robot        stream = antlr3.StringStream('2')
51*16467b97STreehugger Robot        lexer = self.getLexer(stream)
52*16467b97STreehugger Robot
53*16467b97STreehugger Robot        try:
54*16467b97STreehugger Robot            token = lexer.nextToken()
55*16467b97STreehugger Robot            raise AssertionError
56*16467b97STreehugger Robot
57*16467b97STreehugger Robot        except antlr3.MismatchedTokenException, exc:
58*16467b97STreehugger Robot            assert exc.expecting == 'f', repr(exc.expecting)
59*16467b97STreehugger Robot            assert exc.unexpectedType == '2', repr(exc.unexpectedType)
60*16467b97STreehugger Robot
61*16467b97STreehugger Robot
62*16467b97STreehugger Robot    def testMalformedInput2(self):
63*16467b97STreehugger Robot        stream = antlr3.StringStream('f')
64*16467b97STreehugger Robot        lexer = self.getLexer(stream)
65*16467b97STreehugger Robot
66*16467b97STreehugger Robot        try:
67*16467b97STreehugger Robot            token = lexer.nextToken()
68*16467b97STreehugger Robot            raise AssertionError
69*16467b97STreehugger Robot
70*16467b97STreehugger Robot        except antlr3.EarlyExitException, exc:
71*16467b97STreehugger Robot            assert exc.unexpectedType == antlr3.EOF, repr(exc.unexpectedType)
72*16467b97STreehugger Robot
73*16467b97STreehugger Robot
74*16467b97STreehugger Robotif __name__ == '__main__':
75*16467b97STreehugger Robot    unittest.main()
76