This file is indexed.

/usr/share/pyshared/ometa/test/test_builder.py is in python-parsley 1.2-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
from textwrap import dedent
import unittest

from ometa.builder import writePython
from terml.nodes import termMaker as t
from terml.parser import parseTerm as term

def dd(txt):
    return dedent(txt).strip()

class PythonWriterTests(unittest.TestCase):
    """
    Tests for generating Python source from an AST.
    """

    def test_exactly(self):
        """
        Test generation of code for the 'exactly' pattern.
        """

        x = t.Exactly("x")
        self.assertEqual(writePython(x ,""),
                         dd("""
                            _G_exactly_1, lastError = self.exactly('x')
                            self.considerError(lastError, None)
                            _G_exactly_1
                            """))



    def test_apply(self):
        """
        Test generation of code for rule application.
        """

        one = t.Action("1")
        x = t.Action("x")
        a = t.Apply("foo", "main", [one, x])
        self.assertEqual(writePython(a, ""),
                         dd("""
                            _G_python_1, lastError = (1), None
                            self.considerError(lastError, None)
                            _G_python_2, lastError = eval('x', self.globals, _locals), None
                            self.considerError(lastError, None)
                            _G_apply_3, lastError = self._apply(self.rule_foo, "foo", [_G_python_1, _G_python_2])
                            self.considerError(lastError, None)
                            _G_apply_3
                            """))

    def test_foreignApply(self):
        """
        Test generation of code for calling foreign grammar's rules.
        """
        one = t.Action("1")
        x = t.Action("x")
        a = t.ForeignApply("thegrammar", "foo", "main", [one, x])
        self.assertEqual(writePython(a, ""),
                         dd("""
                            _G_python_1, lastError = (1), None
                            self.considerError(lastError, None)
                            _G_python_2, lastError = eval('x', self.globals, _locals), None
                            self.considerError(lastError, None)
                            _G_apply_3, lastError = self.foreignApply("thegrammar", "foo", self.globals, _locals, _G_python_1, _G_python_2)
                            self.considerError(lastError, None)
                            _G_apply_3
                            """))

    def test_superApply(self):
        """
        Test generation of code for calling the superclass' implementation of
        the current rule.
        """
        one = t.Action("1")
        x = t.Action("x")
        a = t.Apply("super", "main", [one, x])
        self.assertEqual(writePython(a, ""),
                         dd("""
                            _G_python_1, lastError = (1), None
                            self.considerError(lastError, None)
                            _G_python_2, lastError = eval('x', self.globals, _locals), None
                            self.considerError(lastError, None)
                            _G_apply_3, lastError = self.superApply("main", _G_python_1, _G_python_2)
                            self.considerError(lastError, None)
                            _G_apply_3
                            """))


    def test_many(self):
        """
        Test generation of code for matching zero or more instances of
        a pattern.
        """

        xs = t.Many(t.Exactly("x"))
        self.assertEqual(writePython(xs, ""),
                         dd("""
                            def _G_many_1():
                                _G_exactly_2, lastError = self.exactly('x')
                                self.considerError(lastError, None)
                                return (_G_exactly_2, self.currentError)
                            _G_many_3, lastError = self.many(_G_many_1)
                            self.considerError(lastError, None)
                            _G_many_3
                            """))


    def test_many1(self):
        """
        Test generation of code for matching one or more instances of
        a pattern.
        """

        xs = t.Many1(t.Exactly("x"))
        self.assertEqual(writePython(xs, ""),
                         dd("""
                            def _G_many1_1():
                                _G_exactly_2, lastError = self.exactly('x')
                                self.considerError(lastError, None)
                                return (_G_exactly_2, self.currentError)
                            _G_many1_3, lastError = self.many(_G_many1_1, _G_many1_1())
                            self.considerError(lastError, None)
                            _G_many1_3
                            """))



    def test_or(self):
        """
        Test code generation for a sequence of alternatives.
        """

        xy = t.Or([t.Exactly("x"),
                               t.Exactly("y")])
        self.assertEqual(writePython(xy, ""),
                         dd("""
                            def _G_or_1():
                                _G_exactly_2, lastError = self.exactly('x')
                                self.considerError(lastError, None)
                                return (_G_exactly_2, self.currentError)
                            def _G_or_3():
                                _G_exactly_4, lastError = self.exactly('y')
                                self.considerError(lastError, None)
                                return (_G_exactly_4, self.currentError)
                            _G_or_5, lastError = self._or([_G_or_1, _G_or_3])
                            self.considerError(lastError, None)
                            _G_or_5
                            """))

    def test_singleOr(self):
        """
        Test code generation for a sequence of alternatives.
        """

        x1 = t.Or([t.Exactly("x")])
        x = t.Exactly("x")
        self.assertEqual(writePython(x, ""), writePython(x1, ""))


    def test_optional(self):
        """
        Test code generation for optional terms.
        """
        x = t.Optional(t.Exactly("x"))
        self.assertEqual(writePython(x, ""),
                         dd("""
                            def _G_optional_1():
                                _G_exactly_2, lastError = self.exactly('x')
                                self.considerError(lastError, None)
                                return (_G_exactly_2, self.currentError)
                            def _G_optional_3():
                                return (None, self.input.nullError())
                            _G_or_4, lastError = self._or([_G_optional_1, _G_optional_3])
                            self.considerError(lastError, None)
                            _G_or_4
                            """))


    def test_not(self):
        """
        Test code generation for negated terms.
        """
        x = t.Not(t.Exactly("x"))
        self.assertEqual(writePython(x ,""),
                         dd("""
                            def _G_not_1():
                                _G_exactly_2, lastError = self.exactly('x')
                                self.considerError(lastError, None)
                                return (_G_exactly_2, self.currentError)
                            _G_not_3, lastError = self._not(_G_not_1)
                            self.considerError(lastError, None)
                            _G_not_3
                            """))


    def test_lookahead(self):
        """
        Test code generation for lookahead expressions.
        """
        x = t.Lookahead(t.Exactly("x"))
        self.assertEqual(writePython(x, ""),
                         dd("""
                            def _G_lookahead_1():
                                _G_exactly_2, lastError = self.exactly('x')
                                self.considerError(lastError, None)
                                return (_G_exactly_2, self.currentError)
                            _G_lookahead_3, lastError = self.lookahead(_G_lookahead_1)
                            self.considerError(lastError, None)
                            _G_lookahead_3
                            """))



    def test_sequence(self):
        """
        Test generation of code for sequence patterns.
        """
        x = t.Exactly("x")
        y = t.Exactly("y")
        z = t.And([x, y])
        self.assertEqual(writePython(z, ""),
                         dd("""
                            _G_exactly_1, lastError = self.exactly('x')
                            self.considerError(lastError, None)
                            _G_exactly_2, lastError = self.exactly('y')
                            self.considerError(lastError, None)
                            _G_exactly_2
                            """))


    def test_bind(self):
        """
        Test code generation for variable assignment.
        """
        x = t.Exactly("x")
        b = t.Bind("var", x)
        self.assertEqual(writePython(b, ""),
                         dd("""
                            _G_exactly_1, lastError = self.exactly('x')
                            self.considerError(lastError, None)
                            _locals['var'] = _G_exactly_1
                            _G_exactly_1
                            """))


    def test_pred(self):
        """
        Test code generation for predicate expressions.
        """
        x = t.Predicate(t.Exactly("x"))
        self.assertEqual(writePython(x, ""),
                         dd("""
                            def _G_pred_1():
                                _G_exactly_2, lastError = self.exactly('x')
                                self.considerError(lastError, None)
                                return (_G_exactly_2, self.currentError)
                            _G_pred_3, lastError = self.pred(_G_pred_1)
                            self.considerError(lastError, None)
                            _G_pred_3
                            """))


    def test_action(self):
        """
        Test code generation for semantic actions.
        """
        x = t.Action("doStuff()")
        self.assertEqual(writePython(x, ""),
                         dd("""
                            _G_python_1, lastError = eval('doStuff()', self.globals, _locals), None
                            self.considerError(lastError, None)
                            _G_python_1
                            """))


    def test_expr(self):
        """
        Test code generation for semantic predicates.
        """
        x = t.Action("returnStuff()")
        self.assertEqual(writePython(x, ""),
                         dd("""
                            _G_python_1, lastError = eval('returnStuff()', self.globals, _locals), None
                            self.considerError(lastError, None)
                            _G_python_1
                            """))

    def test_label(self):
        """
        Test code generation for custom labels.
        """
        xs = t.Label(t.Exactly("x"), 'CustomLabel')
        self.assertEqual(writePython(xs, ""),
                         dd("""
                                def _G_label_1():
                                    _G_exactly_2, lastError = self.exactly('x')
                                    self.considerError(lastError, None)
                                    return (_G_exactly_2, self.currentError)
                                _G_label_3, lastError = self.label(_G_label_1, "CustomLabel")
                                self.considerError(lastError, None)
                                _G_label_3
                                """))

    def test_listpattern(self):
        """
        Test code generation for list patterns.
        """
        x = t.List(t.Exactly("x"))
        self.assertEqual(writePython(x, ""),
                         dd("""
                            def _G_listpattern_1():
                                _G_exactly_2, lastError = self.exactly('x')
                                self.considerError(lastError, None)
                                return (_G_exactly_2, self.currentError)
                            _G_listpattern_3, lastError = self.listpattern(_G_listpattern_1)
                            self.considerError(lastError, None)
                            _G_listpattern_3
                            """))


    def test_markAsTree(self):
        """
        Grammars containing list patterns are marked as taking
        tree-shaped input rather than character streams.
        """
        x = t.Rule("foo", t.List(
                t.Exactly("x")))
        g = t.Grammar("TestGrammar", True, [x])
        self.assertIn("\n        tree = True\n", writePython(g, ""))


    def test_rule(self):
        """
        Test generation of entire rules.
        """

        x = t.Rule("foo", t.Exactly("x"))
        self.assertEqual(writePython(x, ""),
                         dd("""
                            def rule_foo(self):
                                _locals = {'self': self}
                                self.locals['foo'] = _locals
                                _G_exactly_1, lastError = self.exactly('x')
                                self.considerError(lastError, 'foo')
                                return (_G_exactly_1, self.currentError)
                            """))


    def test_grammar(self):
        """
        Test generation of an entire grammar.
        """
        r1 = t.Rule("foo", t.Exactly("x"))
        r2 = t.Rule("baz", t.Exactly("y"))
        x = t.Grammar("BuilderTest", False, [r1, r2])
        self.assertEqual(
            writePython(x, ""),
            dd("""
               def createParserClass(GrammarBase, ruleGlobals):
                   if ruleGlobals is None:
                       ruleGlobals = {}
                   class BuilderTest(GrammarBase):
                       def rule_foo(self):
                           _locals = {'self': self}
                           self.locals['foo'] = _locals
                           _G_exactly_1, lastError = self.exactly('x')
                           self.considerError(lastError, 'foo')
                           return (_G_exactly_1, self.currentError)


                       def rule_baz(self):
                           _locals = {'self': self}
                           self.locals['baz'] = _locals
                           _G_exactly_2, lastError = self.exactly('y')
                           self.considerError(lastError, 'baz')
                           return (_G_exactly_2, self.currentError)


                   if BuilderTest.globals is not None:
                       BuilderTest.globals = BuilderTest.globals.copy()
                       BuilderTest.globals.update(ruleGlobals)
                   else:
                       BuilderTest.globals = ruleGlobals
                   return BuilderTest
                            """))