• 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        assert len(parser.reportedErrors) == 0, parser.reportedErrors
18        assert parser.events == [
19            ('decl', 'foobar'),
20            ('call', 'gnarz'),
21            ('decl', 'blupp'),
22            ('call', 'flupp')
23            ], parser.events
24
25
26    def testMalformedInput1(self):
27        cStream = antlr3.StringStream('var; foo();')
28        lexer = self.getLexer(cStream)
29        tStream = antlr3.CommonTokenStream(lexer)
30        parser = self.getParser(tStream)
31
32        parser.document()
33
34        # FIXME: currently strings with formatted errors are collected
35        # can't check error locations yet
36        assert len(parser.reportedErrors) == 1, parser.reportedErrors
37        assert parser.events == [], parser.events
38
39
40    def testMalformedInput2(self):
41        cStream = antlr3.StringStream('var foobar(); gnarz();')
42        lexer = self.getLexer(cStream)
43        tStream = antlr3.CommonTokenStream(lexer)
44        parser = self.getParser(tStream)
45
46        parser.document()
47
48        # FIXME: currently strings with formatted errors are collected
49        # can't check error locations yet
50        assert len(parser.reportedErrors) == 1, parser.reportedErrors
51        assert parser.events == [
52            ('call', 'gnarz'),
53            ], parser.events
54
55
56    def testMalformedInput3(self):
57        cStream = antlr3.StringStream('gnarz(; flupp();')
58        lexer = self.getLexer(cStream)
59        tStream = antlr3.CommonTokenStream(lexer)
60        parser = self.getParser(tStream)
61
62        parser.document()
63
64        # FIXME: currently strings with formatted errors are collected
65        # can't check error locations yet
66        assert len(parser.reportedErrors) == 1, parser.reportedErrors
67        assert parser.events == [
68            ('call', 'gnarz'),
69            ('call', 'flupp'),
70            ], parser.events
71
72
73if __name__ == '__main__':
74    unittest.main()
75