| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 3 # for details. All rights reserved. Use of this source code is governed by a | 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. | 4 # BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 import sys | 6 import sys |
| 7 | 7 |
| 8 | 8 |
| 9 class IDLNode(object): | 9 class IDLNode(object): |
| 10 """Base class for all IDL elements. | 10 """Base class for all IDL elements. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 """Returns a list containing this node and all it child nodes | 50 """Returns a list containing this node and all it child nodes |
| 51 (recursive). | 51 (recursive). |
| 52 | 52 |
| 53 Args: | 53 Args: |
| 54 type_filter -- can be used to limit the results to a specific | 54 type_filter -- can be used to limit the results to a specific |
| 55 node type (e.g. IDLOperation). | 55 node type (e.g. IDLOperation). |
| 56 """ | 56 """ |
| 57 res = [] | 57 res = [] |
| 58 if type_filter is None or isinstance(self, type_filter): | 58 if type_filter is None or isinstance(self, type_filter): |
| 59 res.append(self) | 59 res.append(self) |
| 60 for (k, v) in self.__dict__.items(): | 60 for v in self._all_subnodes(): |
| 61 if isinstance(v, IDLNode): | 61 if isinstance(v, IDLNode): |
| 62 res.extend(v.all(type_filter)) | 62 res.extend(v.all(type_filter)) |
| 63 elif isinstance(v, list): | 63 elif isinstance(v, list): |
| 64 for item in v: | 64 for item in v: |
| 65 if isinstance(item, IDLNode): | 65 if isinstance(item, IDLNode): |
| 66 res.extend(item.all(type_filter)) | 66 res.extend(item.all(type_filter)) |
| 67 return res | 67 return res |
| 68 | 68 |
| 69 def _all_subnodes(self): |
| 70 """Accessor used by all() to find subnodes.""" |
| 71 return self.__dict__.values() |
| 72 |
| 69 def to_dict(self): | 73 def to_dict(self): |
| 70 """Converts the IDLNode and its children into a dictionary. | 74 """Converts the IDLNode and its children into a dictionary. |
| 71 This method is useful mostly for debugging and pretty printing. | 75 This method is useful mostly for debugging and pretty printing. |
| 72 """ | 76 """ |
| 73 res = {} | 77 res = {} |
| 74 for (k, v) in self.__dict__.items(): | 78 for (k, v) in self.__dict__.items(): |
| 75 if v == None or v == False or v == [] or v == {}: | 79 if v == None or v == False or v == [] or v == {}: |
| 76 # Skip empty/false members. | 80 # Skip empty/false members. |
| 77 continue | 81 continue |
| 78 elif isinstance(v, IDLDictNode) and not len(v): | 82 elif isinstance(v, IDLDictNode) and not len(v): |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 The IDLDictNode members are copied into a new dictionary, and | 227 The IDLDictNode members are copied into a new dictionary, and |
| 224 IDLNode members are recursively converted into dicts as well. | 228 IDLNode members are recursively converted into dicts as well. |
| 225 """ | 229 """ |
| 226 res = {} | 230 res = {} |
| 227 for (k, v) in self.__map.items(): | 231 for (k, v) in self.__map.items(): |
| 228 if isinstance(v, IDLNode): | 232 if isinstance(v, IDLNode): |
| 229 v = v.to_dict() | 233 v = v.to_dict() |
| 230 res[k] = v | 234 res[k] = v |
| 231 return res | 235 return res |
| 232 | 236 |
| 233 def all(self, node_filter): | 237 def _all_subnodes(self): |
| 234 """Overrides the default IDLNode.all recursive operator.""" | 238 # Usually an IDLDictNode does not contain further IDLNodes. |
| 235 if node_filter is None or isinstance(self, node_filter): | |
| 236 return [self] | |
| 237 return [] | 239 return [] |
| 238 | 240 |
| 239 | 241 |
| 240 class IDLFile(IDLNode): | 242 class IDLFile(IDLNode): |
| 241 """IDLFile is the top-level node in each IDL file. It may contain | 243 """IDLFile is the top-level node in each IDL file. It may contain |
| 242 modules or interfaces.""" | 244 modules or interfaces.""" |
| 243 | 245 |
| 244 def __init__(self, ast, filename=None): | 246 def __init__(self, ast, filename=None): |
| 245 IDLNode.__init__(self, ast) | 247 IDLNode.__init__(self, ast) |
| 246 self.filename = filename | 248 self.filename = filename |
| (...skipping 20 matching lines...) Expand all Loading... |
| 267 def __init__(self, ast=None): | 269 def __init__(self, ast=None): |
| 268 IDLDictNode.__init__(self, None) | 270 IDLDictNode.__init__(self, None) |
| 269 if not ast: | 271 if not ast: |
| 270 return | 272 return |
| 271 ext_attrs_ast = self._find_first(ast, 'ExtAttrs') | 273 ext_attrs_ast = self._find_first(ast, 'ExtAttrs') |
| 272 if not ext_attrs_ast: | 274 if not ext_attrs_ast: |
| 273 return | 275 return |
| 274 for ext_attr in self._find_all(ext_attrs_ast, 'ExtAttr'): | 276 for ext_attr in self._find_all(ext_attrs_ast, 'ExtAttr'): |
| 275 name = self._find_first(ext_attr, 'Id') | 277 name = self._find_first(ext_attr, 'Id') |
| 276 value = self._find_first(ext_attr, 'ExtAttrValue') | 278 value = self._find_first(ext_attr, 'ExtAttrValue') |
| 279 |
| 277 func_value = self._find_first(value, 'ExtAttrFunctionValue') | 280 func_value = self._find_first(value, 'ExtAttrFunctionValue') |
| 278 if func_value: | 281 if func_value: |
| 279 # TODO: Parse ExtAttrFunctionValue. It is used only for | 282 # E.g. NamedConstructor=Audio(in [Optional] DOMString src) |
| 280 # NamedConstructor which is ignored. | 283 self[name] = IDLExtAttrFunctionValue( |
| 281 pass | 284 func_value, |
| 282 else: | 285 self._find_first(func_value, 'ExtAttrArgList')) |
| 283 self[name] = value | 286 continue |
| 287 |
| 288 ctor_args = not value and self._find_first(ext_attr, 'ExtAttrArgList') |
| 289 if ctor_args: |
| 290 # E.g. Constructor(Element host) |
| 291 self[name] = IDLExtAttrFunctionValue(None, ctor_args) |
| 292 continue |
| 293 |
| 294 self[name] = value |
| 295 |
| 296 def _all_subnodes(self): |
| 297 # Extended attributes may contain IDLNodes, e.g. IDLExtAttrFunctionValue |
| 298 return self.values() |
| 299 |
| 300 |
| 301 class IDLExtAttrFunctionValue(IDLNode): |
| 302 """IDLExtAttrFunctionValue.""" |
| 303 def __init__(self, func_value_ast, arg_list_ast): |
| 304 IDLNode.__init__(self, func_value_ast) |
| 305 self.arguments = self._convert_all(arg_list_ast, 'Argument', IDLArgument) |
| 284 | 306 |
| 285 | 307 |
| 286 class IDLType(IDLNode): | 308 class IDLType(IDLNode): |
| 287 """IDLType is used to describe constants, attributes and operations' | 309 """IDLType is used to describe constants, attributes and operations' |
| 288 return and input types. IDLType matches AST labels such as ScopedName, | 310 return and input types. IDLType matches AST labels such as ScopedName, |
| 289 StringType, VoidType, IntegerType, etc.""" | 311 StringType, VoidType, IntegerType, etc.""" |
| 290 | 312 |
| 291 def __init__(self, ast): | 313 def __init__(self, ast): |
| 292 IDLNode.__init__(self, ast) | 314 IDLNode.__init__(self, ast) |
| 293 # Search for a 'ScopedName' or any label ending with 'Type'. | 315 # Search for a 'ScopedName' or any label ending with 'Type'. |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 """IDLDictNode specialization for one annotation.""" | 483 """IDLDictNode specialization for one annotation.""" |
| 462 def __init__(self, ast=None): | 484 def __init__(self, ast=None): |
| 463 IDLDictNode.__init__(self, ast) | 485 IDLDictNode.__init__(self, ast) |
| 464 self.id = None | 486 self.id = None |
| 465 if not ast: | 487 if not ast: |
| 466 return | 488 return |
| 467 for arg in self._find_all(ast, 'AnnotationArg'): | 489 for arg in self._find_all(ast, 'AnnotationArg'): |
| 468 name = self._find_first(arg, 'Id') | 490 name = self._find_first(arg, 'Id') |
| 469 value = self._find_first(arg, 'AnnotationArgValue') | 491 value = self._find_first(arg, 'AnnotationArgValue') |
| 470 self[name] = value | 492 self[name] = value |
| OLD | NEW |