This file is indexed.

/usr/share/pyshared/ometa/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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# -*- test-case-name: ometa.test.test_builder -*-
import ast
from StringIO import StringIO
from types import ModuleType as module
import linecache, sys
from terml.nodes import Term, Tag, coerceToTerm

class TextWriter(object):

    stepSize = 4

    def __init__(self, f, indentSteps=0):
        self.file = f
        self.indentSteps = indentSteps


    def writeln(self, data):
        if data:
            self.file.write(" " * (self.indentSteps * self.stepSize))
            self.file.write(data)
        self.file.write("\n")

    def indent(self):
        return TextWriter(self.file, self.indentSteps + 1)


class PythonWriter(object):
    """
    Converts an OMeta syntax tree into Python source.
    """
    def __init__(self, tree, grammarText):
        self.tree = tree
        self.grammarText = grammarText
        self.gensymCounter = 0
        self.compiledExprCache = None


    def _generate(self, out, expr, retrn=False, debugname=None):
        result = self._generateNode(out, expr, debugname=debugname)
        if retrn:
            out.writeln("return (%s, self.currentError)" % (result,))
        elif result:
            out.writeln(result)

    def output(self, out):
        self._generate(out, self.tree)


    def _generateNode(self, out, node, debugname=None):
        name = node.tag.name
        args = node.args
        if name == 'null':
            return 'None'
        if node.span:
            out.writeln("self._trace(%r, %r, self.input.position)"
                        % (self.grammarText[slice(*node.span)], node.span))
        return getattr(self, "generate_"+name)(out, *args, debugname=debugname)


    def _gensym(self, name):
        """
        Produce a unique name for a variable in generated code.
        """
        self.gensymCounter += 1
        return "_G_%s_%s" % (name, self.gensymCounter)


    def _newThunkFor(self, out, name, expr):
        """
        Define a new function of no arguments.
        @param name: The name of the rule generating this thunk.
        @param expr: A list of lines of Python code.
        """

        fname = self._gensym(name)
        self._writeFunction(out, fname, (),  expr)
        return fname


    def _expr(self, out, typ, e, debugname=None):
        """
        Generate the code needed to execute the expression, and return the
        variable name bound to its value.
        """
        name = self._gensym(typ)
        out.writeln("%s, lastError = %s" % (name, e))
        out.writeln("self.considerError(lastError, %r)" % (debugname and debugname.data,))
        return name


    def _writeFunction(self, out, fname, arglist, expr):
        """
        Generate a function.
        @param out: the TextWriter used for output.
        @param fname: The name of the function generated.
        @param arglist: A list of parameter names.
        @param expr: The term tree to generate the function body from.
        """

        out.writeln("def %s(%s):" % (fname, ", ".join(arglist)))
        self._generate(out.indent(), expr, retrn=True)
        return fname


    def compilePythonExpr(self, out, expr, debugname=None):
        """
        Generate code for running embedded Python expressions.
        """
        try:
            ast.literal_eval(expr)
            return self._expr(out, 'python', '(' + expr + '), None', debugname)
        except ValueError:
            if self.compiledExprCache is None:
                return self._expr(out, 'python',
                                'eval(%r, self.globals, _locals), None' % (expr,),
                                debugname)
            else:
                if expr in self.compiledExprCache:
                    sym = self.compiledExprCache[expr]
                else:
                    sym = self.compiledExprCache[expr] = self._gensym('expr')

                return self._expr(out, 'python',
                                'eval(self.%s, self.globals, _locals), None' % (sym,),
                                debugname)

    def _convertArgs(self, out, rawArgs, debugname):
        return [self._generateNode(out, x, debugname) for x in rawArgs]


    def generate_Apply(self, out, ruleName, codeName, rawArgs, debugname=None):
        """
        Create a call to self.apply(ruleName, *args).
        """
        ruleName = ruleName.data
        args = self._convertArgs(out, rawArgs.args, debugname)
        if ruleName == 'super':
            return self._expr(out, 'apply', 'self.superApply("%s", %s)'
                              % (codeName.data, ', '.join(args)),
                              debugname)
        return self._expr(out, 'apply', 'self._apply(self.rule_%s, "%s", [%s])'
                          % (ruleName, ruleName, ', '.join(args)),
                          debugname)

    def generate_ForeignApply(self, out, grammarName, ruleName, codeName,
            rawArgs, debugname=None):
        """
        Create a call to self.foreignApply(ruleName, *args)
        """
        grammarName = grammarName.data
        ruleName = ruleName.data
        args = self._convertArgs(out, rawArgs.args, debugname)
        call = ('self.foreignApply("%s", "%s", self.globals, _locals, %s)'
                            % (grammarName, ruleName, ', '.join(args)))
        return self._expr(out, 'apply', call, debugname)

    def generate_Exactly(self, out, literal, debugname=None):
        """
        Create a call to self.exactly(expr).
        """
        return self._expr(out, 'exactly', 'self.exactly(%r)' % (literal.data,), debugname)


    def generate_Token(self, out, literal, debugname=None):
        if self.takesTreeInput:
            return self.generate_Exactly(out, literal, debugname)
        else:
            return self._expr(out, 'apply',
                              'self._apply(self.rule_token, "token", ["%s"])'
                              % (literal.data,),
                              debugname)


    def generate_Many(self, out, expr, debugname=None):
        """
        Create a call to self.many(lambda: expr).
        """
        fname = self._newThunkFor(out, "many", expr)
        return self._expr(out, 'many', 'self.many(%s)' % (fname,), debugname)


    def generate_Many1(self, out, expr, debugname=None):
        """
        Create a call to self.many(lambda: expr).
        """
        fname = self._newThunkFor(out, "many1", expr)
        return self._expr(out, 'many1', 'self.many(%s, %s())' % (fname, fname), debugname)


    def generate_Repeat(self, out, min, max, expr, debugname=None):
        """
        Create a call to self.repeat(min, max, lambda: expr).
        """
        fname = self._newThunkFor(out, "repeat", expr)
        if min.tag.name == '.int.':
            min = min.data
        else:
            min = '_locals["%s"]' % min.data
        if max.tag.name == '.int.':
            max = max.data
        else:
            max = '_locals["%s"]' % max.data
        if min == max == 0:
            return "''"
        return self._expr(out, 'repeat', 'self.repeat(%s, %s, %s)'
                          % (min, max, fname))

    def generate_Optional(self, out, expr, debugname=None):
        """
        Try to parse an expr and continue if it fails.
        """
        realf = self._newThunkFor(out, "optional", expr)
        passf = self._gensym("optional")
        out.writeln("def %s():" % (passf,))
        out.indent().writeln("return (None, self.input.nullError())")
        return self._expr(out, 'or', 'self._or([%s])'
                          % (', '.join([realf, passf])),
                          debugname)


    def generate_Or(self, out, exprs, debugname=None):
        """
        Create a call to
        self._or([lambda: expr1, lambda: expr2, ... , lambda: exprN]).
        """
        if len(exprs.args) > 1:
            fnames = [self._newThunkFor(out, "or", expr) for expr in exprs.args]
            return self._expr(out, 'or', 'self._or([%s])' % (', '.join(fnames)), debugname)
        else:
            return self._generateNode(out, exprs.args[0], debugname)


    def generate_Not(self, out, expr, debugname=None):
        """
        Create a call to self._not(lambda: expr).
        """
        fname = self._newThunkFor(out, "not", expr)
        return self._expr(out, "not", "self._not(%s)" % (fname,), debugname)


    def generate_Lookahead(self, out, expr, debugname=None):
        """
        Create a call to self.lookahead(lambda: expr).
        """
        fname = self._newThunkFor(out, "lookahead", expr)
        return self._expr(out, "lookahead", "self.lookahead(%s)" %(fname,), debugname)


    def generate_And(self, out, exprs, debugname=None):
        """
        Generate code for each statement in order.
        """
        v = None
        for ex in exprs.args:
            v = self._generateNode(out, ex, debugname)
        return v


    def generate_Bind(self, out, name, expr, debugname=None):
        """
        Bind the value of 'expr' to a name in the _locals dict.
        """
        v = self._generateNode(out, expr, debugname)
        if name.data:
            ref = "_locals['%s']" % (name.data,)
            out.writeln("%s = %s" % (ref, v))
        else:
            for i, n in enumerate(name.args):
                ref = "_locals['%s']" % (n.data,)
                out.writeln("%s = %s[%i]" %(ref, v, i))
        return v


    def generate_Predicate(self, out, expr, debugname=None):
        """
        Generate a call to self.pred(lambda: expr).
        """

        fname = self._newThunkFor(out, "pred", expr)
        return self._expr(out, "pred", "self.pred(%s)" %(fname,), debugname)


    def generate_Action(self, out, expr, debugname=None):
        """
        Generate this embedded Python expression on its own line.
        """
        return self.compilePythonExpr(out, expr.data, debugname)


    def generate_Python(self, out, expr, debugname=None):
        """
        Generate this embedded Python expression on its own line.
        """
        return self.compilePythonExpr(out, expr.data, debugname)


    def generate_List(self, out, expr, debugname=None):
        """
        Generate a call to self.listpattern(lambda: expr).
        """
        fname = self._newThunkFor(out, "listpattern", expr)
        return  self._expr(out, "listpattern", "self.listpattern(%s)" %(fname,),
                           debugname)

    def generate_Label(self, out, expr, label, debugname=None):
        """
        Generate code for expr, and create label.
        """
        fname = self._newThunkFor(out, "label", expr)
        return self._expr(out, 'label', 'self.label(%s, "%s")' % (fname, label.data, ), debugname)

    def generate_TermPattern(self, out, name, expr, debugname=None):
        fname = self._newThunkFor(out, "termpattern", expr)
        return self._expr(out, 'termpattern', 'self.termpattern(%r, %s)' % (name.data, fname),
                          debugname)

    def generate_StringTemplate(self, out, template, debugname=None):
        out.writeln("from terml.parser import parseTerm as term")
        return self._expr(out, 'stringtemplate', 'self.stringtemplate(%s, _locals)' % (template,))

    def generate_ConsumedBy(self, out, expr, debugname=None):
        """
        Generate a call to self.consumedBy(lambda: expr).
        """
        fname = self._newThunkFor(out, "consumedby", expr)
        return  self._expr(out, "consumedby", "self.consumedby(%s)" %(fname,),
                           debugname)


    def generate_Rule(self, prevOut, name, expr, debugname=None):
        prevOut.writeln("def rule_%s(self):" % (name.data,))
        out = prevOut.indent()
        out.writeln("_locals = {'self': self}")
        out.writeln("self.locals[%r] = _locals" % (name.data,))
        self._generate(prevOut.indent(), expr, retrn=True, debugname=name)

    def generate_Grammar(self, out, name, takesTreeInput, rules,
                         debugname=None):
        self.compiledExprCache = {}
        self.takesTreeInput = takesTreeInput.tag.name == 'true'
        out.writeln("def createParserClass(GrammarBase, ruleGlobals):")
        funcOut = out.indent()
        funcOut.writeln("if ruleGlobals is None:")
        funcOut.indent().writeln("ruleGlobals = {}")
        funcOut.writeln("class %s(GrammarBase):" % (name.data,))
        out = funcOut.indent()
        for rule in rules.args:
            self._generateNode(out, rule, debugname)
            out.writeln("")
            out.writeln("")
        if self.takesTreeInput:
            out.writeln("tree = %s" % self.takesTreeInput)
        for expr, sym in self.compiledExprCache.items():
            out.writeln("%s = compile(%r, '<string>', 'eval')" % (sym, expr))
        funcOut.writeln(
            "if %s.globals is not None:" % (name.data,))
        out.writeln("%s.globals = %s.globals.copy()" % (name.data,
                                                        name.data))
        out.writeln("%s.globals.update(ruleGlobals)" % (name.data,))
        funcOut.writeln(
            "else:")
        out.writeln("%s.globals = ruleGlobals" % (name.data,))
        funcOut.writeln("return " + name.data)
        self.compiledExprCache = None


class _Term2PythonAction(object):
    def leafData(bldr, data, span):
        return repr(data)

    def leafTag(bldr, tag, span):
        return tag.name

    def term(bldr, tag, args):
        if tag == '.tuple.':
            return "[%s]" % (', '.join(args),)
        elif tag == '.attr.':
            return "(%s)" % (', '.join(args),)
        elif tag == '.bag.':
            return "dict(%s)" % (', '.join(args),)
        if not args:
            return tag
        return "%s(%s)" % (tag, ', '.join(args))


class TermActionPythonWriter(PythonWriter):
    builder = _Term2PythonAction

    def _convertArgs(self, out, termArgs, debugname):
        return [self._termAsPython(out, a, debugname) for a in termArgs]


    def generate_Predicate(self, out, term, debugname=None):
        """
        Generate a call to self.pred(lambda: expr).
        """

        fname = self._newThunkFor(out, "pred", Term(Tag("Action"), None,
                                                    [term], None))
        return self._expr(out, "pred", "self.pred(%s)" %(fname,), debugname)

    def generate_Action(self, out, term, debugname=None):
        return self._termAsPython(out, term, debugname)

    generate_Python = generate_Action

    def _termAsPython(self, out, term, debugname):
        if not term.args:
            if term.data is None:
                return self.compilePythonExpr(out, term.tag.name, debugname)
            else:
                name = self._gensym("literal")
                out.writeln("%s = %r" % (name, term.data))
                return name
        else:
            return self.compilePythonExpr(out, term.build(self.builder()), debugname)


def writePython(tree, txt):
    f = StringIO()
    out = TextWriter(f)
    pw = PythonWriter(tree, txt)
    pw.output(out)
    return f.getvalue().strip()


class GeneratedCodeLoader(object):
    """
    Object for use as a module's __loader__, to display generated
    source.
    """
    def __init__(self, source):
        self.source = source
    def get_source(self, name):
        return self.source



def moduleFromGrammar(source, className, modname, filename):
    mod = module(modname)
    mod.__name__ = modname
    mod.__loader__ = GeneratedCodeLoader(source)
    code = compile(source, filename, "exec")
    eval(code, mod.__dict__)
    sys.modules[modname] = mod
    linecache.getlines(filename, mod.__dict__)
    return mod