| 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 objects for the AST for a Mojo IDL file.""" | 5 """Node objects 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). | 9 # and lineno). |
| 10 | 10 |
| 11 | 11 |
| 12 class NodeBase(object): | 12 class NodeBase(object): |
| 13 """Base class for nodes in the AST.""" | 13 """Base class for nodes in the AST.""" |
| 14 | 14 |
| 15 def __init__(self, filename=None, lineno=None): | 15 def __init__(self, filename=None, lineno=None): |
| 16 self.filename = filename | 16 self.filename = filename |
| 17 self.lineno = lineno | 17 self.lineno = lineno |
| 18 | 18 |
| 19 | 19 |
| 20 class Ordinal(NodeBase): | |
| 21 """Represents an ordinal value labeling, e.g., a struct field.""" | |
| 22 | |
| 23 def __init__(self, value, **kwargs): | |
| 24 NodeBase.__init__(self, **kwargs) | |
| 25 self.value = value | |
| 26 | |
| 27 def __eq__(self, other): | |
| 28 return self.value == other.value | |
| 29 | |
| 30 | |
| 31 class Parameter(NodeBase): | |
| 32 """Represents a method request or response parameter.""" | |
| 33 | |
| 34 def __init__(self, typename, name, ordinal, **kwargs): | |
| 35 assert isinstance(ordinal, Ordinal) | |
| 36 NodeBase.__init__(self, **kwargs) | |
| 37 self.typename = typename | |
| 38 self.name = name | |
| 39 self.ordinal = ordinal | |
| 40 | |
| 41 def __eq__(self, other): | |
| 42 return self.typename == other.typename and \ | |
| 43 self.name == other.name and \ | |
| 44 self.ordinal == other.ordinal | |
| 45 | |
| 46 | |
| 47 class NodeListBase(NodeBase): | 20 class NodeListBase(NodeBase): |
| 48 """Represents a list of other nodes, all having the same type. (This is meant | 21 """Represents a list of other nodes, all having the same type. (This is meant |
| 49 to be subclassed, with subclasses defining _list_item_type to be the class of | 22 to be subclassed, with subclasses defining _list_item_type to be the class of |
| 50 the members of the list; _list_item_type should also be a NodeBase.)""" | 23 the members of the list; _list_item_type should also be a NodeBase.)""" |
| 51 | 24 |
| 52 def __init__(self, item_or_items=None, **kwargs): | 25 def __init__(self, item_or_items=None, **kwargs): |
| 53 assert issubclass(self._list_item_type, NodeBase) | 26 assert issubclass(self._list_item_type, NodeBase) |
| 54 NodeBase.__init__(self, **kwargs) | 27 NodeBase.__init__(self, **kwargs) |
| 55 if item_or_items is None: | 28 if item_or_items is None: |
| 56 self.elements = [] | 29 self.elements = [] |
| (...skipping 24 matching lines...) Expand all Loading... |
| 81 assert isinstance(item, self._list_item_type) | 54 assert isinstance(item, self._list_item_type) |
| 82 self.elements.append(item) | 55 self.elements.append(item) |
| 83 self._UpdateFilenameAndLineno() | 56 self._UpdateFilenameAndLineno() |
| 84 | 57 |
| 85 def _UpdateFilenameAndLineno(self): | 58 def _UpdateFilenameAndLineno(self): |
| 86 if self.elements: | 59 if self.elements: |
| 87 self.filename = self.elements[0].filename | 60 self.filename = self.elements[0].filename |
| 88 self.lineno = self.elements[0].lineno | 61 self.lineno = self.elements[0].lineno |
| 89 | 62 |
| 90 | 63 |
| 64 ################################################################################ |
| 65 |
| 66 |
| 67 class Attribute(NodeBase): |
| 68 """Represents an attribute.""" |
| 69 |
| 70 def __init__(self, key, value, **kwargs): |
| 71 assert isinstance(key, str) |
| 72 NodeBase.__init__(self, **kwargs) |
| 73 self.key = key |
| 74 self.value = value |
| 75 |
| 76 def __eq__(self, other): |
| 77 return self.key == other.key and self.value == other.value |
| 78 |
| 79 |
| 80 class AttributeList(NodeListBase): |
| 81 """Represents a list attributes.""" |
| 82 |
| 83 _list_item_type = Attribute |
| 84 |
| 85 |
| 86 class Ordinal(NodeBase): |
| 87 """Represents an ordinal value labeling, e.g., a struct field.""" |
| 88 |
| 89 def __init__(self, value, **kwargs): |
| 90 assert value is None or isinstance(value, int) |
| 91 NodeBase.__init__(self, **kwargs) |
| 92 self.value = value |
| 93 |
| 94 def __eq__(self, other): |
| 95 return self.value == other.value |
| 96 |
| 97 |
| 98 class Parameter(NodeBase): |
| 99 """Represents a method request or response parameter.""" |
| 100 |
| 101 def __init__(self, typename, name, ordinal, **kwargs): |
| 102 assert isinstance(ordinal, Ordinal) |
| 103 NodeBase.__init__(self, **kwargs) |
| 104 self.typename = typename |
| 105 self.name = name |
| 106 self.ordinal = ordinal |
| 107 |
| 108 def __eq__(self, other): |
| 109 return self.typename == other.typename and \ |
| 110 self.name == other.name and \ |
| 111 self.ordinal == other.ordinal |
| 112 |
| 113 |
| 91 class ParameterList(NodeListBase): | 114 class ParameterList(NodeListBase): |
| 92 """Represents a list of (method request or response) parameters.""" | 115 """Represents a list of (method request or response) parameters.""" |
| 93 | 116 |
| 94 _list_item_type = Parameter | 117 _list_item_type = Parameter |
| OLD | NEW |