OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Node classes for the AST for a Mojo IDL file.""" | 5 """Node classes for the AST for a Mojo IDL file.""" |
6 | 6 |
7 # Note: For convenience of testing, you probably want to define __eq__() methods | 7 # Note: For convenience of testing, you probably want to define __eq__() methods |
8 # for all node types; it's okay to be slightly lax (e.g., not compare filename | 8 # for all node types; it's okay to be slightly lax (e.g., not compare filename |
9 # and lineno). You may also define __repr__() to help with analyzing test | 9 # and lineno). You may also define __repr__() to help with analyzing test |
10 # failures, especially for more complex types. | 10 # failures, especially for more complex types. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 if self.items: | 77 if self.items: |
78 self.filename = self.items[0].filename | 78 self.filename = self.items[0].filename |
79 self.lineno = self.items[0].lineno | 79 self.lineno = self.items[0].lineno |
80 | 80 |
81 | 81 |
82 class Definition(NodeBase): | 82 class Definition(NodeBase): |
83 """Represents a definition of anything that has a global name (e.g., enums, | 83 """Represents a definition of anything that has a global name (e.g., enums, |
84 enum values, consts, structs, struct fields, interfaces). (This does not | 84 enum values, consts, structs, struct fields, interfaces). (This does not |
85 include parameter definitions.) This class is meant to be subclassed.""" | 85 include parameter definitions.) This class is meant to be subclassed.""" |
86 | 86 |
87 def __init__(self, name, **kwargs): | 87 def __init__(self, mojom_name, **kwargs): |
88 assert isinstance(name, str) | 88 assert isinstance(mojom_name, str) |
89 NodeBase.__init__(self, **kwargs) | 89 NodeBase.__init__(self, **kwargs) |
90 self.name = name | 90 self.mojom_name = mojom_name |
91 | 91 |
92 | 92 |
93 ################################################################################ | 93 ################################################################################ |
94 | 94 |
95 | 95 |
96 class Attribute(NodeBase): | 96 class Attribute(NodeBase): |
97 """Represents an attribute.""" | 97 """Represents an attribute.""" |
98 | 98 |
99 def __init__(self, key, value, **kwargs): | 99 def __init__(self, key, value, **kwargs): |
100 assert isinstance(key, str) | 100 assert isinstance(key, str) |
101 super(Attribute, self).__init__(**kwargs) | 101 super(Attribute, self).__init__(**kwargs) |
102 self.key = key | 102 self.key = key |
103 self.value = value | 103 self.value = value |
104 | 104 |
105 def __eq__(self, other): | 105 def __eq__(self, other): |
106 return super(Attribute, self).__eq__(other) and \ | 106 return super(Attribute, self).__eq__(other) and \ |
107 self.key == other.key and \ | 107 self.key == other.key and \ |
108 self.value == other.value | 108 self.value == other.value |
109 | 109 |
110 | 110 |
111 class AttributeList(NodeListBase): | 111 class AttributeList(NodeListBase): |
112 """Represents a list attributes.""" | 112 """Represents a list attributes.""" |
113 | 113 |
114 _list_item_type = Attribute | 114 _list_item_type = Attribute |
115 | 115 |
116 | 116 |
117 class Const(Definition): | 117 class Const(Definition): |
118 """Represents a const definition.""" | 118 """Represents a const definition.""" |
119 | 119 |
120 def __init__(self, name, typename, value, **kwargs): | 120 def __init__(self, mojom_name, typename, value, **kwargs): |
121 # The typename is currently passed through as a string. | 121 # The typename is currently passed through as a string. |
122 assert isinstance(typename, str) | 122 assert isinstance(typename, str) |
123 # The value is either a literal (currently passed through as a string) or a | 123 # The value is either a literal (currently passed through as a string) or a |
124 # "wrapped identifier". | 124 # "wrapped identifier". |
125 assert isinstance(value, str) or isinstance(value, tuple) | 125 assert isinstance(value, str) or isinstance(value, tuple) |
126 super(Const, self).__init__(name, **kwargs) | 126 super(Const, self).__init__(mojom_name, **kwargs) |
127 self.typename = typename | 127 self.typename = typename |
128 self.value = value | 128 self.value = value |
129 | 129 |
130 def __eq__(self, other): | 130 def __eq__(self, other): |
131 return super(Const, self).__eq__(other) and \ | 131 return super(Const, self).__eq__(other) and \ |
132 self.typename == other.typename and \ | 132 self.typename == other.typename and \ |
133 self.value == other.value | 133 self.value == other.value |
134 | 134 |
135 | 135 |
136 class Enum(Definition): | 136 class Enum(Definition): |
137 """Represents an enum definition.""" | 137 """Represents an enum definition.""" |
138 | 138 |
139 def __init__(self, name, attribute_list, enum_value_list, **kwargs): | 139 def __init__(self, mojom_name, attribute_list, enum_value_list, **kwargs): |
140 assert attribute_list is None or isinstance(attribute_list, AttributeList) | 140 assert attribute_list is None or isinstance(attribute_list, AttributeList) |
141 assert enum_value_list is None or isinstance(enum_value_list, EnumValueList) | 141 assert enum_value_list is None or isinstance(enum_value_list, EnumValueList) |
142 super(Enum, self).__init__(name, **kwargs) | 142 super(Enum, self).__init__(mojom_name, **kwargs) |
143 self.attribute_list = attribute_list | 143 self.attribute_list = attribute_list |
144 self.enum_value_list = enum_value_list | 144 self.enum_value_list = enum_value_list |
145 | 145 |
146 def __eq__(self, other): | 146 def __eq__(self, other): |
147 return super(Enum, self).__eq__(other) and \ | 147 return super(Enum, self).__eq__(other) and \ |
148 self.attribute_list == other.attribute_list and \ | 148 self.attribute_list == other.attribute_list and \ |
149 self.enum_value_list == other.enum_value_list | 149 self.enum_value_list == other.enum_value_list |
150 | 150 |
151 | 151 |
152 class EnumValue(Definition): | 152 class EnumValue(Definition): |
153 """Represents a definition of an enum value.""" | 153 """Represents a definition of an enum value.""" |
154 | 154 |
155 def __init__(self, name, attribute_list, value, **kwargs): | 155 def __init__(self, mojom_name, attribute_list, value, **kwargs): |
156 # The optional value is either an int (which is current a string) or a | 156 # The optional value is either an int (which is current a string) or a |
157 # "wrapped identifier". | 157 # "wrapped identifier". |
158 assert attribute_list is None or isinstance(attribute_list, AttributeList) | 158 assert attribute_list is None or isinstance(attribute_list, AttributeList) |
159 assert value is None or isinstance(value, (str, tuple)) | 159 assert value is None or isinstance(value, (str, tuple)) |
160 super(EnumValue, self).__init__(name, **kwargs) | 160 super(EnumValue, self).__init__(mojom_name, **kwargs) |
161 self.attribute_list = attribute_list | 161 self.attribute_list = attribute_list |
162 self.value = value | 162 self.value = value |
163 | 163 |
164 def __eq__(self, other): | 164 def __eq__(self, other): |
165 return super(EnumValue, self).__eq__(other) and \ | 165 return super(EnumValue, self).__eq__(other) and \ |
166 self.attribute_list == other.attribute_list and \ | 166 self.attribute_list == other.attribute_list and \ |
167 self.value == other.value | 167 self.value == other.value |
168 | 168 |
169 | 169 |
170 class EnumValueList(NodeListBase): | 170 class EnumValueList(NodeListBase): |
(...skipping 18 matching lines...) Expand all Loading... |
189 | 189 |
190 class ImportList(NodeListBase): | 190 class ImportList(NodeListBase): |
191 """Represents a list (i.e., sequence) of import statements.""" | 191 """Represents a list (i.e., sequence) of import statements.""" |
192 | 192 |
193 _list_item_type = Import | 193 _list_item_type = Import |
194 | 194 |
195 | 195 |
196 class Interface(Definition): | 196 class Interface(Definition): |
197 """Represents an interface definition.""" | 197 """Represents an interface definition.""" |
198 | 198 |
199 def __init__(self, name, attribute_list, body, **kwargs): | 199 def __init__(self, mojom_name, attribute_list, body, **kwargs): |
200 assert attribute_list is None or isinstance(attribute_list, AttributeList) | 200 assert attribute_list is None or isinstance(attribute_list, AttributeList) |
201 assert isinstance(body, InterfaceBody) | 201 assert isinstance(body, InterfaceBody) |
202 super(Interface, self).__init__(name, **kwargs) | 202 super(Interface, self).__init__(mojom_name, **kwargs) |
203 self.attribute_list = attribute_list | 203 self.attribute_list = attribute_list |
204 self.body = body | 204 self.body = body |
205 | 205 |
206 def __eq__(self, other): | 206 def __eq__(self, other): |
207 return super(Interface, self).__eq__(other) and \ | 207 return super(Interface, self).__eq__(other) and \ |
208 self.attribute_list == other.attribute_list and \ | 208 self.attribute_list == other.attribute_list and \ |
209 self.body == other.body | 209 self.body == other.body |
210 | 210 |
211 | 211 |
212 class Method(Definition): | 212 class Method(Definition): |
213 """Represents a method definition.""" | 213 """Represents a method definition.""" |
214 | 214 |
215 def __init__(self, name, attribute_list, ordinal, parameter_list, | 215 def __init__(self, mojom_name, attribute_list, ordinal, parameter_list, |
216 response_parameter_list, **kwargs): | 216 response_parameter_list, **kwargs): |
217 assert attribute_list is None or isinstance(attribute_list, AttributeList) | 217 assert attribute_list is None or isinstance(attribute_list, AttributeList) |
218 assert ordinal is None or isinstance(ordinal, Ordinal) | 218 assert ordinal is None or isinstance(ordinal, Ordinal) |
219 assert isinstance(parameter_list, ParameterList) | 219 assert isinstance(parameter_list, ParameterList) |
220 assert response_parameter_list is None or \ | 220 assert response_parameter_list is None or \ |
221 isinstance(response_parameter_list, ParameterList) | 221 isinstance(response_parameter_list, ParameterList) |
222 super(Method, self).__init__(name, **kwargs) | 222 super(Method, self).__init__(mojom_name, **kwargs) |
223 self.attribute_list = attribute_list | 223 self.attribute_list = attribute_list |
224 self.ordinal = ordinal | 224 self.ordinal = ordinal |
225 self.parameter_list = parameter_list | 225 self.parameter_list = parameter_list |
226 self.response_parameter_list = response_parameter_list | 226 self.response_parameter_list = response_parameter_list |
227 | 227 |
228 def __eq__(self, other): | 228 def __eq__(self, other): |
229 return super(Method, self).__eq__(other) and \ | 229 return super(Method, self).__eq__(other) and \ |
230 self.attribute_list == other.attribute_list and \ | 230 self.attribute_list == other.attribute_list and \ |
231 self.ordinal == other.ordinal and \ | 231 self.ordinal == other.ordinal and \ |
232 self.parameter_list == other.parameter_list and \ | 232 self.parameter_list == other.parameter_list and \ |
233 self.response_parameter_list == other.response_parameter_list | 233 self.response_parameter_list == other.response_parameter_list |
234 | 234 |
235 | 235 |
236 # This needs to be declared after |Method|. | 236 # This needs to be declared after |Method|. |
237 class InterfaceBody(NodeListBase): | 237 class InterfaceBody(NodeListBase): |
238 """Represents the body of (i.e., list of definitions inside) an interface.""" | 238 """Represents the body of (i.e., list of definitions inside) an interface.""" |
239 | 239 |
240 _list_item_type = (Const, Enum, Method) | 240 _list_item_type = (Const, Enum, Method) |
241 | 241 |
242 | 242 |
243 class Module(NodeBase): | 243 class Module(NodeBase): |
244 """Represents a module statement.""" | 244 """Represents a module statement.""" |
245 | 245 |
246 def __init__(self, name, attribute_list, **kwargs): | 246 def __init__(self, mojom_namespace, attribute_list, **kwargs): |
247 # |name| is either none or a "wrapped identifier". | 247 # |mojom_namespace| is either none or a "wrapped identifier". |
248 assert name is None or isinstance(name, tuple) | 248 assert mojom_namespace is None or isinstance(mojom_namespace, tuple) |
249 assert attribute_list is None or isinstance(attribute_list, AttributeList) | 249 assert attribute_list is None or isinstance(attribute_list, AttributeList) |
250 super(Module, self).__init__(**kwargs) | 250 super(Module, self).__init__(**kwargs) |
251 self.name = name | 251 self.mojom_namespace = mojom_namespace |
252 self.attribute_list = attribute_list | 252 self.attribute_list = attribute_list |
253 | 253 |
254 def __eq__(self, other): | 254 def __eq__(self, other): |
255 return super(Module, self).__eq__(other) and \ | 255 return super(Module, self).__eq__(other) and \ |
256 self.name == other.name and \ | 256 self.mojom_namespace == other.mojom_namespace and \ |
257 self.attribute_list == other.attribute_list | 257 self.attribute_list == other.attribute_list |
258 | 258 |
259 | 259 |
260 class Mojom(NodeBase): | 260 class Mojom(NodeBase): |
261 """Represents an entire .mojom file. (This is the root node.)""" | 261 """Represents an entire .mojom file. (This is the root node.)""" |
262 | 262 |
263 def __init__(self, module, import_list, definition_list, **kwargs): | 263 def __init__(self, module, import_list, definition_list, **kwargs): |
264 assert module is None or isinstance(module, Module) | 264 assert module is None or isinstance(module, Module) |
265 assert isinstance(import_list, ImportList) | 265 assert isinstance(import_list, ImportList) |
266 assert isinstance(definition_list, list) | 266 assert isinstance(definition_list, list) |
(...skipping 22 matching lines...) Expand all Loading... |
289 self.value = value | 289 self.value = value |
290 | 290 |
291 def __eq__(self, other): | 291 def __eq__(self, other): |
292 return super(Ordinal, self).__eq__(other) and \ | 292 return super(Ordinal, self).__eq__(other) and \ |
293 self.value == other.value | 293 self.value == other.value |
294 | 294 |
295 | 295 |
296 class Parameter(NodeBase): | 296 class Parameter(NodeBase): |
297 """Represents a method request or response parameter.""" | 297 """Represents a method request or response parameter.""" |
298 | 298 |
299 def __init__(self, name, attribute_list, ordinal, typename, **kwargs): | 299 def __init__(self, mojom_name, attribute_list, ordinal, typename, **kwargs): |
300 assert isinstance(name, str) | 300 assert isinstance(mojom_name, str) |
301 assert attribute_list is None or isinstance(attribute_list, AttributeList) | 301 assert attribute_list is None or isinstance(attribute_list, AttributeList) |
302 assert ordinal is None or isinstance(ordinal, Ordinal) | 302 assert ordinal is None or isinstance(ordinal, Ordinal) |
303 assert isinstance(typename, str) | 303 assert isinstance(typename, str) |
304 super(Parameter, self).__init__(**kwargs) | 304 super(Parameter, self).__init__(**kwargs) |
305 self.name = name | 305 self.mojom_name = mojom_name |
306 self.attribute_list = attribute_list | 306 self.attribute_list = attribute_list |
307 self.ordinal = ordinal | 307 self.ordinal = ordinal |
308 self.typename = typename | 308 self.typename = typename |
309 | 309 |
310 def __eq__(self, other): | 310 def __eq__(self, other): |
311 return super(Parameter, self).__eq__(other) and \ | 311 return super(Parameter, self).__eq__(other) and \ |
312 self.name == other.name and \ | 312 self.mojom_name == other.mojom_name and \ |
313 self.attribute_list == other.attribute_list and \ | 313 self.attribute_list == other.attribute_list and \ |
314 self.ordinal == other.ordinal and \ | 314 self.ordinal == other.ordinal and \ |
315 self.typename == other.typename | 315 self.typename == other.typename |
316 | 316 |
317 | 317 |
318 class ParameterList(NodeListBase): | 318 class ParameterList(NodeListBase): |
319 """Represents a list of (method request or response) parameters.""" | 319 """Represents a list of (method request or response) parameters.""" |
320 | 320 |
321 _list_item_type = Parameter | 321 _list_item_type = Parameter |
322 | 322 |
323 | 323 |
324 class Struct(Definition): | 324 class Struct(Definition): |
325 """Represents a struct definition.""" | 325 """Represents a struct definition.""" |
326 | 326 |
327 def __init__(self, name, attribute_list, body, **kwargs): | 327 def __init__(self, mojom_name, attribute_list, body, **kwargs): |
328 assert attribute_list is None or isinstance(attribute_list, AttributeList) | 328 assert attribute_list is None or isinstance(attribute_list, AttributeList) |
329 assert isinstance(body, StructBody) or body is None | 329 assert isinstance(body, StructBody) or body is None |
330 super(Struct, self).__init__(name, **kwargs) | 330 super(Struct, self).__init__(mojom_name, **kwargs) |
331 self.attribute_list = attribute_list | 331 self.attribute_list = attribute_list |
332 self.body = body | 332 self.body = body |
333 | 333 |
334 def __eq__(self, other): | 334 def __eq__(self, other): |
335 return super(Struct, self).__eq__(other) and \ | 335 return super(Struct, self).__eq__(other) and \ |
336 self.attribute_list == other.attribute_list and \ | 336 self.attribute_list == other.attribute_list and \ |
337 self.body == other.body | 337 self.body == other.body |
338 | 338 |
339 | 339 |
340 class StructField(Definition): | 340 class StructField(Definition): |
341 """Represents a struct field definition.""" | 341 """Represents a struct field definition.""" |
342 | 342 |
343 def __init__(self, name, attribute_list, ordinal, typename, default_value, | 343 def __init__(self, mojom_name, attribute_list, ordinal, typename, |
344 **kwargs): | 344 default_value, **kwargs): |
345 assert isinstance(name, str) | 345 assert isinstance(mojom_name, str) |
346 assert attribute_list is None or isinstance(attribute_list, AttributeList) | 346 assert attribute_list is None or isinstance(attribute_list, AttributeList) |
347 assert ordinal is None or isinstance(ordinal, Ordinal) | 347 assert ordinal is None or isinstance(ordinal, Ordinal) |
348 assert isinstance(typename, str) | 348 assert isinstance(typename, str) |
349 # The optional default value is currently either a value as a string or a | 349 # The optional default value is currently either a value as a string or a |
350 # "wrapped identifier". | 350 # "wrapped identifier". |
351 assert default_value is None or isinstance(default_value, (str, tuple)) | 351 assert default_value is None or isinstance(default_value, (str, tuple)) |
352 super(StructField, self).__init__(name, **kwargs) | 352 super(StructField, self).__init__(mojom_name, **kwargs) |
353 self.attribute_list = attribute_list | 353 self.attribute_list = attribute_list |
354 self.ordinal = ordinal | 354 self.ordinal = ordinal |
355 self.typename = typename | 355 self.typename = typename |
356 self.default_value = default_value | 356 self.default_value = default_value |
357 | 357 |
358 def __eq__(self, other): | 358 def __eq__(self, other): |
359 return super(StructField, self).__eq__(other) and \ | 359 return super(StructField, self).__eq__(other) and \ |
360 self.attribute_list == other.attribute_list and \ | 360 self.attribute_list == other.attribute_list and \ |
361 self.ordinal == other.ordinal and \ | 361 self.ordinal == other.ordinal and \ |
362 self.typename == other.typename and \ | 362 self.typename == other.typename and \ |
363 self.default_value == other.default_value | 363 self.default_value == other.default_value |
364 | 364 |
365 | 365 |
366 # This needs to be declared after |StructField|. | 366 # This needs to be declared after |StructField|. |
367 class StructBody(NodeListBase): | 367 class StructBody(NodeListBase): |
368 """Represents the body of (i.e., list of definitions inside) a struct.""" | 368 """Represents the body of (i.e., list of definitions inside) a struct.""" |
369 | 369 |
370 _list_item_type = (Const, Enum, StructField) | 370 _list_item_type = (Const, Enum, StructField) |
371 | 371 |
372 | 372 |
373 class Union(Definition): | 373 class Union(Definition): |
374 """Represents a union definition.""" | 374 """Represents a union definition.""" |
375 | 375 |
376 def __init__(self, name, attribute_list, body, **kwargs): | 376 def __init__(self, mojom_name, attribute_list, body, **kwargs): |
377 assert attribute_list is None or isinstance(attribute_list, AttributeList) | 377 assert attribute_list is None or isinstance(attribute_list, AttributeList) |
378 assert isinstance(body, UnionBody) | 378 assert isinstance(body, UnionBody) |
379 super(Union, self).__init__(name, **kwargs) | 379 super(Union, self).__init__(mojom_name, **kwargs) |
380 self.attribute_list = attribute_list | 380 self.attribute_list = attribute_list |
381 self.body = body | 381 self.body = body |
382 | 382 |
383 def __eq__(self, other): | 383 def __eq__(self, other): |
384 return super(Union, self).__eq__(other) and \ | 384 return super(Union, self).__eq__(other) and \ |
385 self.attribute_list == other.attribute_list and \ | 385 self.attribute_list == other.attribute_list and \ |
386 self.body == other.body | 386 self.body == other.body |
387 | 387 |
388 | 388 |
389 class UnionField(Definition): | 389 class UnionField(Definition): |
390 | 390 |
391 def __init__(self, name, attribute_list, ordinal, typename, **kwargs): | 391 def __init__(self, mojom_name, attribute_list, ordinal, typename, **kwargs): |
392 assert isinstance(name, str) | 392 assert isinstance(mojom_name, str) |
393 assert attribute_list is None or isinstance(attribute_list, AttributeList) | 393 assert attribute_list is None or isinstance(attribute_list, AttributeList) |
394 assert ordinal is None or isinstance(ordinal, Ordinal) | 394 assert ordinal is None or isinstance(ordinal, Ordinal) |
395 assert isinstance(typename, str) | 395 assert isinstance(typename, str) |
396 super(UnionField, self).__init__(name, **kwargs) | 396 super(UnionField, self).__init__(mojom_name, **kwargs) |
397 self.attribute_list = attribute_list | 397 self.attribute_list = attribute_list |
398 self.ordinal = ordinal | 398 self.ordinal = ordinal |
399 self.typename = typename | 399 self.typename = typename |
400 | 400 |
401 def __eq__(self, other): | 401 def __eq__(self, other): |
402 return super(UnionField, self).__eq__(other) and \ | 402 return super(UnionField, self).__eq__(other) and \ |
403 self.attribute_list == other.attribute_list and \ | 403 self.attribute_list == other.attribute_list and \ |
404 self.ordinal == other.ordinal and \ | 404 self.ordinal == other.ordinal and \ |
405 self.typename == other.typename | 405 self.typename == other.typename |
406 | 406 |
407 | 407 |
408 class UnionBody(NodeListBase): | 408 class UnionBody(NodeListBase): |
409 | 409 |
410 _list_item_type = UnionField | 410 _list_item_type = UnionField |
OLD | NEW |