~washort/pymeta/ometa-2

« back to all changes in this revision

Viewing changes to pymeta/builder.py

  • Committer: Allen Short
  • Date: 2008-03-26 05:57:20 UTC
  • mfrom: (25.1.2 PyMeta)
  • Revision ID: washort@divmod.com-20080326055720-693xvlr74hytb3k9
changesĀ fromĀ laptop

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
from compiler import ast, compile as python_compile
4
4
from compiler.pycodegen import ExpressionCodeGenerator
5
5
 
 
6
class TreeBuilder(object):
 
7
    """
 
8
    Produce an abstract syntax tree of OMeta operations.
 
9
    """
 
10
    def __init__(self, name, grammar):
 
11
        self.name = name
 
12
        self.grammar = grammar
 
13
 
 
14
    def makeGrammar(self, rules):
 
15
        return ["Grammar", rules]
 
16
 
 
17
    def apply(self, ruleName, codeName=None, *exprs):
 
18
        return ["Apply", ruleName, codeName or '', exprs]
 
19
 
 
20
    def exactly(self, expr):
 
21
        return ["Exactly", expr]
 
22
 
 
23
    def many(self, expr):
 
24
        return ["Many", expr]
 
25
 
 
26
    def many1(self, expr):
 
27
        return ["Many1", expr]
 
28
 
 
29
    def optional(self, expr):
 
30
        return ["Optional", expr]
 
31
 
 
32
    def _or(self, exprs):
 
33
        return ["Or"] + exprs
 
34
 
 
35
    def _not(self, expr):
 
36
        return ["Not", expr]
 
37
 
 
38
    def lookahead(self, expr):
 
39
        return ["Lookahead", expr]
 
40
 
 
41
    def sequence(self, exprs):
 
42
        return ["And"] + exprs
 
43
 
 
44
    def bind(self, expr, name):
 
45
        return ["Bind", name, expr]
 
46
 
 
47
    def pred(self, expr):
 
48
        return ["Predicate", expr]
 
49
 
 
50
    def action(self, expr):
 
51
        return ["Action", expr]
 
52
 
 
53
    def listpattern(self, exprs):
 
54
        return ["List", exprs]
 
55
 
 
56
    def compilePythonExpr(self, name, expr):
 
57
        return ["Python", name, expr]
 
58
 
6
59
class AstBuilder(object):
7
60
    """
8
61
    Builder of Python code objects via the 'compiler.ast' module.
79
132
        methodDict.update(ruleMethods)
80
133
        return methodDict
81
134
 
82
 
    def apply(self, ruleName, codeName=None, *exprs):
 
135
    def apply(self, ruleName, codeName='', *exprs):
83
136
        """
84
137
        Create a call to self.apply(ruleName, *args).
85
138
        """
225
278
                            None, None)
226
279
 
227
280
 
 
281
 
228
282
class PythonBuilder(object):
229
283
    """
230
284
    Same idea as ASTBuilder but producing literal Python source instead.
301
355
        @param rules: A mapping of names to rule bodies.
302
356
        """
303
357
        lines = list(itertools.chain(*[self._function("def rule_%s(self):"%(name,),
304
 
                                                      ["_locals = {'self': self}", "self.locals[%s] = _locals" % (name,)] + list(body)) + ['\n\n']
 
358
                                                      ["_locals = {'self': self}", "self.locals[%r] = _locals" % (name,)] + list(body)) + ['\n\n']
305
359
                                       for (name, body) in rules]))
306
360
        code = '\n'.join(self._suite("class %s(%s):" %(self.name, self.grammar.__class__.__name__), lines))
307
361
        module = "from %s import %s\n" % (self.grammar.__class__.__module__, self.grammar.__class__.__name__) + code