1324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverimport antlr3
2324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverimport testbase
3324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverimport unittest
4324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
5324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverclass t001lexer(testbase.ANTLRTest):
6324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    def setUp(self):
7324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        self.compileGrammar()
8324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
9324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
10324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    def lexerClass(self, base):
11324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        class TLexer(base):
12324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            def emitErrorMessage(self, msg):
13324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                # report errors to /dev/null
14324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                pass
15324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
16324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            def reportError(self, re):
17324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                # no error recovery yet, just crash!
18324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver                raise re
19324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
20324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        return TLexer
21324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
22324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
23324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    def testValid(self):
24324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        stream = antlr3.StringStream('0')
25324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        lexer = self.getLexer(stream)
26324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
27324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        token = lexer.nextToken()
28324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        self.failUnlessEqual(token.type, self.lexerModule.ZERO)
29324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
30324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        token = lexer.nextToken()
31324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        self.failUnlessEqual(token.type, self.lexerModule.EOF)
32324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
33324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
34324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    def testIteratorInterface(self):
35324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        stream = antlr3.StringStream('0')
36324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        lexer = self.getLexer(stream)
37324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
38324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        types = [token.type for token in lexer]
39324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
40324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        self.failUnlessEqual(types, [self.lexerModule.ZERO])
41324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
42324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
43324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    def testMalformedInput(self):
44324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        stream = antlr3.StringStream('1')
45324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        lexer = self.getLexer(stream)
46324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
47324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        try:
48324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            token = lexer.nextToken()
49324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            self.fail()
50324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
51324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver        except antlr3.MismatchedTokenException, exc:
52324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            self.failUnlessEqual(exc.expecting, '0')
53324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver            self.failUnlessEqual(exc.unexpectedType, '1')
54324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
55324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver
56324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruverif __name__ == '__main__':
57324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver    unittest.main()
58