• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import antlr3
2import testbase
3import unittest
4
5class t014parser(testbase.ANTLRTest):
6    def setUp(self):
7        self.compileGrammar()
8
9
10    def testValid(self):
11        cStream = antlr3.StringStream('var foobar; gnarz(); var blupp; flupp ( ) ;')
12        lexer = self.getLexer(cStream)
13        tStream = antlr3.CommonTokenStream(lexer)
14        parser = self.getParser(tStream)
15        parser.document()
16
17        self.assertEqual(parser.reportedErrors, [])
18        self.assertEqual(parser.events,
19                         [('decl', 'foobar'), ('call', 'gnarz'),
20                          ('decl', 'blupp'), ('call', 'flupp')])
21
22
23    def testMalformedInput1(self):
24        cStream = antlr3.StringStream('var; foo();')
25        lexer = self.getLexer(cStream)
26        tStream = antlr3.CommonTokenStream(lexer)
27        parser = self.getParser(tStream)
28
29        parser.document()
30
31        # FIXME: currently strings with formatted errors are collected
32        # can't check error locations yet
33        self.assertEqual(len(parser.reportedErrors), 1, parser.reportedErrors)
34        self.assertEqual(parser.events, [])
35
36
37    def testMalformedInput2(self):
38        cStream = antlr3.StringStream('var foobar(); gnarz();')
39        lexer = self.getLexer(cStream)
40        tStream = antlr3.CommonTokenStream(lexer)
41        parser = self.getParser(tStream)
42
43        parser.document()
44
45        # FIXME: currently strings with formatted errors are collected
46        # can't check error locations yet
47        self.assertEqual(len(parser.reportedErrors), 1, parser.reportedErrors)
48        self.assertEqual(parser.events, [('call', 'gnarz')])
49
50
51    def testMalformedInput3(self):
52        cStream = antlr3.StringStream('gnarz(; flupp();')
53        lexer = self.getLexer(cStream)
54        tStream = antlr3.CommonTokenStream(lexer)
55        parser = self.getParser(tStream)
56
57        parser.document()
58
59        # FIXME: currently strings with formatted errors are collected
60        # can't check error locations yet
61        self.assertEqual(len(parser.reportedErrors), 1, parser.reportedErrors)
62        self.assertEqual(parser.events, [('call', 'gnarz'), ('call', 'flupp')])
63
64
65if __name__ == '__main__':
66    unittest.main()
67