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 14 matching lines...) Expand all Loading... |
25 class NodeListBase(NodeBase): | 25 class NodeListBase(NodeBase): |
26 """Represents a list of other nodes, all having the same type. (This is meant | 26 """Represents a list of other nodes, all having the same type. (This is meant |
27 to be subclassed, with subclasses defining _list_item_type to be the class (or | 27 to be subclassed, with subclasses defining _list_item_type to be the class (or |
28 classes, in a tuple) of the members of the list.)""" | 28 classes, in a tuple) of the members of the list.)""" |
29 | 29 |
30 def __init__(self, item_or_items=None, **kwargs): | 30 def __init__(self, item_or_items=None, **kwargs): |
31 super(NodeListBase, self).__init__(**kwargs) | 31 super(NodeListBase, self).__init__(**kwargs) |
32 self.items = [] | 32 self.items = [] |
33 if item_or_items is None: | 33 if item_or_items is None: |
34 pass | 34 pass |
35 self.items = [] | |
36 elif isinstance(item_or_items, list): | 35 elif isinstance(item_or_items, list): |
37 for item in item_or_items: | 36 for item in item_or_items: |
38 assert isinstance(item, self._list_item_type) | 37 assert isinstance(item, self._list_item_type) |
39 self.Append(item) | 38 self.Append(item) |
40 else: | 39 else: |
41 assert isinstance(item_or_items, self._list_item_type) | 40 assert isinstance(item_or_items, self._list_item_type) |
42 self.Append(item_or_items) | 41 self.Append(item_or_items) |
43 | 42 |
44 # Support iteration. For everything else, users should just access |items| | 43 # Support iteration. For everything else, users should just access |items| |
45 # directly. (We intentionally do NOT supply |__len__()| or |__nonzero__()|, so | 44 # directly. (We intentionally do NOT supply |__len__()| or |__nonzero__()|, so |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 self.ordinal == other.ordinal and \ | 342 self.ordinal == other.ordinal and \ |
344 self.typename == other.typename and \ | 343 self.typename == other.typename and \ |
345 self.default_value == other.default_value | 344 self.default_value == other.default_value |
346 | 345 |
347 | 346 |
348 # This needs to be declared after |StructField|. | 347 # This needs to be declared after |StructField|. |
349 class StructBody(NodeListBase): | 348 class StructBody(NodeListBase): |
350 """Represents the body of (i.e., list of definitions inside) a struct.""" | 349 """Represents the body of (i.e., list of definitions inside) a struct.""" |
351 | 350 |
352 _list_item_type = (Const, Enum, StructField) | 351 _list_item_type = (Const, Enum, StructField) |
OLD | NEW |