• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import antlr3
2import testbase
3import unittest
4import textwrap
5
6
7class t022scopes(testbase.ANTLRTest):
8    def setUp(self):
9        self.compileGrammar()
10
11
12    def parserClass(self, base):
13        class TParser(base):
14            def emitErrorMessage(self, msg):
15                # report errors to /dev/null
16                pass
17
18            def reportError(self, re):
19                # no error recovery yet, just crash!
20                raise re
21
22        return TParser
23
24
25    def testa1(self):
26        cStream = antlr3.StringStream('foobar')
27        lexer = self.getLexer(cStream)
28        tStream = antlr3.CommonTokenStream(lexer)
29        parser = self.getParser(tStream)
30        parser.a()
31
32
33    def testb1(self):
34        cStream = antlr3.StringStream('foobar')
35        lexer = self.getLexer(cStream)
36        tStream = antlr3.CommonTokenStream(lexer)
37        parser = self.getParser(tStream)
38
39        self.assertRaises(antlr3.RecognitionException, parser.b, False)
40
41
42    def testb2(self):
43        cStream = antlr3.StringStream('foobar')
44        lexer = self.getLexer(cStream)
45        tStream = antlr3.CommonTokenStream(lexer)
46        parser = self.getParser(tStream)
47        parser.b(True)
48
49
50    def testc1(self):
51        cStream = antlr3.StringStream(
52            textwrap.dedent('''\
53            {
54                int i;
55                int j;
56                i = 0;
57            }
58            '''))
59
60        lexer = self.getLexer(cStream)
61        tStream = antlr3.CommonTokenStream(lexer)
62        parser = self.getParser(tStream)
63        symbols = parser.c()
64
65        self.assertEqual(
66            symbols,
67            set(['i', 'j'])
68            )
69
70
71    def testc2(self):
72        cStream = antlr3.StringStream(
73            textwrap.dedent('''\
74            {
75                int i;
76                int j;
77                i = 0;
78                x = 4;
79            }
80            '''))
81
82        lexer = self.getLexer(cStream)
83        tStream = antlr3.CommonTokenStream(lexer)
84        parser = self.getParser(tStream)
85
86        self.assertRaisesRegex(RuntimeError, r'x', parser.c)
87
88
89    def testd1(self):
90        cStream = antlr3.StringStream(
91            textwrap.dedent('''\
92            {
93                int i;
94                int j;
95                i = 0;
96                {
97                    int i;
98                    int x;
99                    x = 5;
100                }
101            }
102            '''))
103
104        lexer = self.getLexer(cStream)
105        tStream = antlr3.CommonTokenStream(lexer)
106        parser = self.getParser(tStream)
107        symbols = parser.d()
108
109        self.assertEqual(
110            symbols,
111            set(['i', 'j'])
112            )
113
114
115    def teste1(self):
116        cStream = antlr3.StringStream(
117            textwrap.dedent('''\
118            { { { { 12 } } } }
119            '''))
120
121        lexer = self.getLexer(cStream)
122        tStream = antlr3.CommonTokenStream(lexer)
123        parser = self.getParser(tStream)
124        res = parser.e()
125
126        self.assertEqual(res, 12)
127
128
129    def testf1(self):
130        cStream = antlr3.StringStream(
131            textwrap.dedent('''\
132            { { { { 12 } } } }
133            '''))
134
135        lexer = self.getLexer(cStream)
136        tStream = antlr3.CommonTokenStream(lexer)
137        parser = self.getParser(tStream)
138        res = parser.f()
139
140        self.assertIsNone(res)
141
142
143    def testf2(self):
144        cStream = antlr3.StringStream(
145            textwrap.dedent('''\
146            { { 12 } }
147            '''))
148
149        lexer = self.getLexer(cStream)
150        tStream = antlr3.CommonTokenStream(lexer)
151        parser = self.getParser(tStream)
152        res = parser.f()
153
154        self.assertIsNone(res)
155
156
157
158if __name__ == '__main__':
159    unittest.main()
160