~ubuntu-branches/ubuntu/precise/gst0.10-python/precise

« back to all changes in this revision

Viewing changes to codegen/defsparser.py

  • Committer: Bazaar Package Importer
  • Author(s): Loic Minier
  • Date: 2006-06-25 19:37:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060625193745-9yeg0wq56r24n57x
Tags: upstream-0.10.4
ImportĀ upstreamĀ versionĀ 0.10.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; py-indent-offset: 4 -*-
 
2
import os, sys
 
3
import scmexpr
 
4
from definitions import *
 
5
 
 
6
class IncludeParser(scmexpr.Parser):
 
7
    """A simple parser that follows include statements automatically"""
 
8
    def include(self, filename):
 
9
        if not os.path.isabs(filename):
 
10
            filename = os.path.join(os.path.dirname(self.filename), filename)
 
11
 
 
12
        # set self.filename to the include name, to handle recursive includes
 
13
        oldfile = self.filename
 
14
        self.filename = filename
 
15
        self.startParsing()
 
16
        self.filename = oldfile
 
17
 
 
18
class DefsParser(IncludeParser):
 
19
    def __init__(self, arg, defines={}):
 
20
        IncludeParser.__init__(self, arg)
 
21
        self.objects = []
 
22
        self.miniobjects = []
 
23
        self.interfaces = []
 
24
        self.enums = []      # enums and flags
 
25
        self.boxes = []      # boxed types
 
26
        self.pointers = []   # pointer types
 
27
        self.functions = []  # functions and methods
 
28
        self.virtuals = []   # virtual methods
 
29
        self.c_name = {}     # hash of c names of functions
 
30
        self.methods = {}    # hash of methods of particular objects
 
31
        self.defines = defines      # -Dfoo=bar options, as dictionary
 
32
 
 
33
    def define_object(self, *args):
 
34
        odef = apply(ObjectDef, args)
 
35
        self.objects.append(odef)
 
36
        self.c_name[odef.c_name] = odef
 
37
    # TODO: define_mini_object
 
38
    def define_miniobject(self, *args):
 
39
        odef = apply(MiniObjectDef, args)
 
40
        self.miniobjects.append(odef)
 
41
        self.c_name[odef.c_name] = odef
 
42
    def define_interface(self, *args):
 
43
        idef = apply(InterfaceDef, args)
 
44
        self.interfaces.append(idef)
 
45
        self.c_name[idef.c_name] = idef
 
46
    def define_enum(self, *args):
 
47
        edef = apply(EnumDef, args)
 
48
        self.enums.append(edef)
 
49
        self.c_name[edef.c_name] = edef
 
50
    def define_flags(self, *args):
 
51
        fdef = apply(FlagsDef, args)
 
52
        self.enums.append(fdef)
 
53
        self.c_name[fdef.c_name] = fdef
 
54
    def define_boxed(self, *args):
 
55
        bdef = apply(BoxedDef, args)
 
56
        self.boxes.append(bdef)
 
57
        self.c_name[bdef.c_name] = bdef
 
58
    def define_pointer(self, *args):
 
59
        pdef = apply(PointerDef, args)
 
60
        self.pointers.append(pdef)
 
61
        self.c_name[pdef.c_name] = pdef
 
62
    def define_function(self, *args):
 
63
        fdef = apply(FunctionDef, args)
 
64
        self.functions.append(fdef)
 
65
        self.c_name[fdef.c_name] = fdef
 
66
    def define_method(self, *args):
 
67
        mdef = apply(MethodDef, args)
 
68
        self.functions.append(mdef)
 
69
        self.c_name[mdef.c_name] = mdef
 
70
    def define_virtual(self, *args):
 
71
        vdef = apply(VirtualDef, args)
 
72
        self.virtuals.append(vdef)
 
73
    def merge(self, old, parmerge):
 
74
        for obj in self.objects:
 
75
            if old.c_name.has_key(obj.c_name):
 
76
                obj.merge(old.c_name[obj.c_name])
 
77
        for f in self.functions:
 
78
            if old.c_name.has_key(f.c_name):
 
79
                f.merge(old.c_name[f.c_name], parmerge)
 
80
 
 
81
    def printMissing(self, old):
 
82
        for obj in self.objects:
 
83
            if not old.c_name.has_key(obj.c_name):
 
84
                obj.write_defs()
 
85
        for f in self.functions:
 
86
            if not old.c_name.has_key(f.c_name):
 
87
                f.write_defs()
 
88
 
 
89
    def write_defs(self, fp=sys.stdout):
 
90
        for obj in self.objects:
 
91
            obj.write_defs(fp)
 
92
        # TODO: Add miniobject
 
93
        for obj in self.miniobjects:
 
94
            obj.write_defs(fp)
 
95
        for enum in self.enums:
 
96
            enum.write_defs(fp)
 
97
        for boxed in self.boxes:
 
98
            boxed.write_defs(fp)
 
99
        for pointer in self.pointers:
 
100
            pointer.write_defs(fp)
 
101
        for func in self.functions:
 
102
            func.write_defs(fp)
 
103
 
 
104
    def find_object(self, c_name):
 
105
        for obj in self.objects:
 
106
            if obj.c_name == c_name:
 
107
                return obj
 
108
        else:
 
109
            raise ValueError, 'object not found'
 
110
 
 
111
    def find_constructor(self, obj, overrides):
 
112
        for func in self.functions:
 
113
            if isinstance(func, FunctionDef) and \
 
114
               func.is_constructor_of == obj.c_name and \
 
115
               not overrides.is_ignored(func.c_name):
 
116
                return func
 
117
 
 
118
    def find_methods(self, obj):
 
119
        objname = obj.c_name
 
120
        return filter(lambda func, on=objname: isinstance(func, MethodDef) and
 
121
                      func.of_object == on, self.functions)
 
122
 
 
123
    def find_virtuals(self, obj):
 
124
        objname = obj.c_name
 
125
        retval = filter(lambda func, on=objname: isinstance(func, VirtualDef) and
 
126
                        func.of_object == on, self.virtuals)
 
127
        return retval
 
128
 
 
129
    def find_functions(self):
 
130
        return filter(lambda func: isinstance(func, FunctionDef) and
 
131
                      not func.is_constructor_of, self.functions)
 
132
 
 
133
    def ifdef(self, *args):
 
134
        if args[0] in self.defines:
 
135
            for arg in args[1:]:
 
136
                self.handle(arg)
 
137
 
 
138
    def ifndef(self, *args):
 
139
        if args[0] not in self.defines:
 
140
            for arg in args[1:]:
 
141
                self.handle(arg)
 
142