Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(803)

Side by Side Diff: client/dom/scripts/systembase.py

Issue 9403004: Wrapperless dart:html generator (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Final version to check in. changes generator script but doesn't check in an active version of the … Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « client/dom/scripts/idlnode_test.py ('k') | client/dom/scripts/systemfrog.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
3 # for details. All rights reserved. Use of this source code is governed by a
4 # BSD-style license that can be found in the LICENSE file.
5
6 """This module provides base functionality for systems to generate
7 Dart APIs from the IDL database."""
8
9 import os
10 import re
11 from generator import *
12
13 class System(object):
14 """Generates all the files for one implementation."""
15
16 def __init__(self, templates, database, emitters, output_dir):
17 self._templates = templates
18 self._database = database
19 self._emitters = emitters
20 self._output_dir = output_dir
21 self._dart_callback_file_paths = []
22
23 def InterfaceGenerator(self,
24 interface,
25 common_prefix,
26 super_interface_name,
27 source_filter):
28 """Returns an interface generator for |interface|."""
29 return None
30
31 def ProcessCallback(self, interface, info):
32 pass
33
34 def GenerateLibraries(self, lib_dir):
35 pass
36
37 def Finish(self):
38 pass
39
40
41 def _ProcessCallback(self, interface, info, file_path):
42 """Generates a typedef for the callback interface."""
43 self._dart_callback_file_paths.append(file_path)
44 code = self._emitters.FileEmitter(file_path)
45
46 code.Emit(self._templates.Load('callback.darttemplate'))
47 code.Emit('typedef $TYPE $NAME($PARAMS);\n',
48 NAME=interface.id,
49 TYPE=info.type_name,
50 PARAMS=info.ParametersImplementationDeclaration())
51
52 def _GenerateLibFile(self, lib_template, lib_file_path, file_paths,
53 **template_args):
54 """Generates a lib file from a template and a list of files.
55
56 Additional keyword arguments are passed to the template.
57 """
58 # Load template.
59 template = self._templates.Load(lib_template)
60 # Generate the .lib file.
61 lib_file_contents = self._emitters.FileEmitter(lib_file_path)
62
63 # Emit the list of #source directives.
64 list_emitter = lib_file_contents.Emit(template, **template_args)
65 lib_file_dir = os.path.dirname(lib_file_path)
66 for path in sorted(file_paths):
67 relpath = os.path.relpath(path, lib_file_dir)
68 list_emitter.Emit("#source('$PATH');\n", PATH=relpath)
69
70
71 def _BaseDefines(self, interface):
72 """Returns a set of names (strings) for members defined in a base class.
73 """
74 def WalkParentChain(interface):
75 if interface.parents:
76 # Only consider primary parent, secondary parents are not on the
77 # implementation class inheritance chain.
78 parent = interface.parents[0]
79 if IsDartCollectionType(parent.type.id):
80 return
81 if self._database.HasInterface(parent.type.id):
82 parent_interface = self._database.GetInterface(parent.type.id)
83 for attr in parent_interface.attributes:
84 result.add(attr.id)
85 for op in parent_interface.operations:
86 result.add(op.id)
87 WalkParentChain(parent_interface)
88
89 result = set()
90 WalkParentChain(interface)
91 return result;
92
93
94 # ------------------------------------------------------------------------------
95
96 class InterfacesSystem(System):
97
98 def __init__(self, templates, database, emitters, output_dir):
99 super(InterfacesSystem, self).__init__(
100 templates, database, emitters, output_dir)
101 self._dart_interface_file_paths = []
102
103
104 def InterfaceGenerator(self,
105 interface,
106 common_prefix,
107 super_interface_name,
108 source_filter):
109 """."""
110 interface_name = interface.id
111 dart_interface_file_path = self._FilePathForDartInterface(interface_name)
112
113 self._dart_interface_file_paths.append(dart_interface_file_path)
114
115 dart_interface_code = self._emitters.FileEmitter(dart_interface_file_path)
116
117 template_file = 'interface_%s.darttemplate' % interface_name
118 template = self._templates.TryLoad(template_file)
119 if not template:
120 template = self._templates.Load('interface.darttemplate')
121
122 return DartInterfaceGenerator(
123 interface, dart_interface_code,
124 template,
125 common_prefix, super_interface_name,
126 source_filter)
127
128 def ProcessCallback(self, interface, info):
129 """Generates a typedef for the callback interface."""
130 interface_name = interface.id
131 file_path = self._FilePathForDartInterface(interface_name)
132 self._ProcessCallback(interface, info, file_path)
133
134 def GenerateLibraries(self, lib_dir):
135 pass
136
137
138 def _FilePathForDartInterface(self, interface_name):
139 """Returns the file path of the Dart interface definition."""
140 return os.path.join(self._output_dir, 'src', 'interface',
141 '%s.dart' % interface_name)
142
143 # ------------------------------------------------------------------------------
144
145 class DartInterfaceGenerator(object):
146 """Generates Dart Interface definition for one DOM IDL interface."""
147
148 def __init__(self, interface, emitter, template,
149 common_prefix, super_interface, source_filter):
150 """Generates Dart code for the given interface.
151
152 Args:
153 interface -- an IDLInterface instance. It is assumed that all types have
154 been converted to Dart types (e.g. int, String), unless they are in the
155 same package as the interface.
156 common_prefix -- the prefix for the common library, if any.
157 super_interface -- the name of the common interface that this interface
158 implements, if any.
159 source_filter -- if specified, rewrites the names of any superinterfaces
160 that are not from these sources to use the common prefix.
161 """
162 self._interface = interface
163 self._emitter = emitter
164 self._template = template
165 self._common_prefix = common_prefix
166 self._super_interface = super_interface
167 self._source_filter = source_filter
168
169
170 def StartInterface(self):
171 if self._super_interface:
172 typename = self._super_interface
173 else:
174 typename = self._interface.id
175
176
177 extends = []
178 suppressed_extends = []
179
180 for parent in self._interface.parents:
181 # TODO(vsm): Remove source_filter.
182 if MatchSourceFilter(self._source_filter, parent):
183 # Parent is a DOM type.
184 extends.append(parent.type.id)
185 elif '<' in parent.type.id:
186 # Parent is a Dart collection type.
187 # TODO(vsm): Make this check more robust.
188 extends.append(parent.type.id)
189 else:
190 suppressed_extends.append('%s.%s' %
191 (self._common_prefix, parent.type.id))
192
193 comment = ' extends'
194 extends_str = ''
195 if extends:
196 extends_str += ' extends ' + ', '.join(extends)
197 comment = ','
198 if suppressed_extends:
199 extends_str += ' /*%s %s */' % (comment, ', '.join(suppressed_extends))
200
201 if typename in interface_factories:
202 extends_str += ' default ' + interface_factories[typename]
203
204 # TODO(vsm): Add appropriate package / namespace syntax.
205 (self._members_emitter,
206 self._top_level_emitter) = self._emitter.Emit(
207 self._template + '$!TOP_LEVEL',
208 ID=typename,
209 EXTENDS=extends_str)
210
211 element_type = MaybeTypedArrayElementType(self._interface)
212 if element_type:
213 self._members_emitter.Emit(
214 '\n'
215 ' $CTOR(int length);\n'
216 '\n'
217 ' $CTOR.fromList(List<$TYPE> list);\n'
218 '\n'
219 ' $CTOR.fromBuffer(ArrayBuffer buffer);\n',
220 CTOR=self._interface.id,
221 TYPE=element_type)
222
223
224 def FinishInterface(self):
225 # TODO(vsm): Use typedef if / when that is supported in Dart.
226 # Define variant as subtype.
227 if (self._super_interface and
228 self._interface.id is not self._super_interface):
229 consts_emitter = self._top_level_emitter.Emit(
230 '\n'
231 'interface $NAME extends $BASE {\n'
232 '$!CONSTS'
233 '}\n',
234 NAME=self._interface.id,
235 BASE=self._super_interface)
236 for const in sorted(self._interface.constants, ConstantOutputOrder):
237 self._EmitConstant(consts_emitter, const)
238
239 def AddConstant(self, constant):
240 if (not self._super_interface or
241 self._interface.id is self._super_interface):
242 self._EmitConstant(self._members_emitter, constant)
243
244 def _EmitConstant(self, emitter, constant):
245 emitter.Emit('\n static final $TYPE $NAME = $VALUE;\n',
246 NAME=constant.id,
247 TYPE=constant.type.id,
248 VALUE=constant.value)
249
250 def AddAttribute(self, getter, setter):
251 if getter and setter and getter.type.id == setter.type.id:
252 self._members_emitter.Emit('\n $TYPE $NAME;\n',
253 NAME=getter.id, TYPE=getter.type.id);
254 return
255 if getter and not setter:
256 self._members_emitter.Emit('\n final $TYPE $NAME;\n',
257 NAME=getter.id, TYPE=getter.type.id);
258 return
259 raise Exception('Unexpected getter/setter combination %s %s' %
260 (getter, setter))
261
262 def AddIndexer(self, element_type):
263 # Interface inherits all operations from List<element_type>.
264 pass
265
266 def AddOperation(self, info):
267 """
268 Arguments:
269 operations - contains the overloads, one or more operations with the same
270 name.
271 """
272 self._members_emitter.Emit('\n'
273 ' $TYPE $NAME($PARAMS);\n',
274 TYPE=info.type_name,
275 NAME=info.name,
276 PARAMS=info.ParametersInterfaceDeclaration())
277
278 # Interfaces get secondary members directly via the superinterfaces.
279 def AddSecondaryAttribute(self, interface, getter, setter):
280 pass
281
282 def AddSecondaryOperation(self, interface, attr):
283 pass
284
285 def AddEventAttributes(self, event_attrs):
286 pass
287
288 # Given a sorted sequence of type identifiers, return an appropriate type
289 # name
290 def TypeName(typeIds, interface):
291 # Dynamically type this field for now.
292 return 'var'
293
294 # ------------------------------------------------------------------------------
295
296 class DummyInterfaceGenerator(object):
297 """Generates nothing."""
298
299 def __init__(self, system, interface):
300 pass
301
302 def StartInterface(self):
303 pass
304
305 def FinishInterface(self):
306 pass
307
308 def AddConstant(self, constant):
309 pass
310
311 def AddAttribute(self, getter, setter):
312 pass
313
314 def AddSecondaryAttribute(self, interface, getter, setter):
315 pass
316
317 def AddSecondaryOperation(self, interface, info):
318 pass
319
320 def AddIndexer(self, element_type):
321 pass
322
323 def AddTypedArrayConstructors(self, element_type):
324 pass
325
326 def AddOperation(self, info):
327 pass
328
329 def AddEventAttributes(self, event_attrs):
330 pass
331
OLDNEW
« no previous file with comments | « client/dom/scripts/idlnode_test.py ('k') | client/dom/scripts/systemfrog.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698