| 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 from idlnode import * | 6 from idlnode import * |
| 7 | 7 |
| 8 | 8 |
| 9 def render(idl_node, indent_str=' '): | 9 def render(idl_node, indent_str=' '): |
| 10 output = [] | 10 output = [] |
| 11 indent_stack = [] | 11 indent_stack = [] |
| 12 | 12 |
| 13 def begin_indent(): | 13 def begin_indent(): |
| 14 indent_stack.append(indent_str) | 14 indent_stack.append(indent_str) |
| 15 | |
| 16 def end_indent(): | 15 def end_indent(): |
| 17 indent_stack.pop() | 16 indent_stack.pop() |
| 18 | 17 |
| 19 def sort(nodes): | 18 def sort(nodes): |
| 20 return sorted(nodes, key=lambda node: node.id) | 19 return sorted(nodes, key=lambda node: node.id) |
| 21 | 20 |
| 22 def wln(node=None): | 21 def wln(node=None): |
| 23 """Writes the given node and adds a new line.""" | 22 """Writes the given node and adds a new line.""" |
| 24 w(node) | 23 w(node) |
| 25 output.append('\n') | 24 output.append('\n') |
| 26 | 25 |
| 26 def wsp(node): |
| 27 """Writes the given node and adds a space if there was output.""" |
| 28 mark = len(output) |
| 29 w(node) |
| 30 if mark != len(output): |
| 31 w(' ') |
| 32 |
| 27 def w(node, list_separator=None): | 33 def w(node, list_separator=None): |
| 28 """Writes the given node. | 34 """Writes the given node. |
| 29 | 35 |
| 30 Args: | 36 Args: |
| 31 node -- a string, IDLNode instance or a list of such. | 37 node -- a string, IDLNode instance or a list of such. |
| 32 list_separator -- if provided, and node is a list, | 38 list_separator -- if provided, and node is a list, |
| 33 list_separator will be written between the list items. | 39 list_separator will be written between the list items. |
| 34 """ | 40 """ |
| 35 if node is None: | 41 if node is None: |
| 36 return | 42 return |
| 37 elif isinstance(node, str): | 43 elif isinstance(node, str): |
| 38 if output and output[-1].endswith('\n'): | 44 if output and output[-1].endswith('\n'): |
| 39 # Auto-indent. | 45 # Auto-indent. |
| 40 output.extend(indent_stack) | 46 output.extend(indent_stack) |
| 41 output.append(node) | 47 output.append(node) |
| 42 elif isinstance(node, list): | 48 elif isinstance(node, list): |
| 43 for i in range(0, len(node)): | 49 for i in range(0, len(node)): |
| 44 if i > 0: | 50 if i > 0: |
| 45 w(list_separator) | 51 w(list_separator) |
| 46 w(node[i]) | 52 w(node[i]) |
| 47 elif isinstance(node, IDLFile): | 53 elif isinstance(node, IDLFile): |
| 48 w(node.modules) | 54 w(node.modules) |
| 49 w(node.interfaces) | 55 w(node.interfaces) |
| 50 elif isinstance(node, IDLModule): | 56 elif isinstance(node, IDLModule): |
| 51 w(node.annotations) | 57 wsp(node.annotations) |
| 52 w(node.ext_attrs) | 58 wsp(node.ext_attrs) |
| 53 wln('module %s {' % node.id) | 59 wln('module %s {' % node.id) |
| 54 begin_indent() | 60 begin_indent() |
| 55 w(node.interfaces) | 61 w(node.interfaces) |
| 56 w(node.typeDefs) | 62 w(node.typeDefs) |
| 57 end_indent() | 63 end_indent() |
| 58 wln('};') | 64 wln('};') |
| 59 elif isinstance(node, IDLInterface): | 65 elif isinstance(node, IDLInterface): |
| 60 wln(node.annotations) | 66 if node.annotations: |
| 61 wln(node.ext_attrs) | 67 wln(node.annotations) |
| 68 if node.ext_attrs: |
| 69 wln(node.ext_attrs) |
| 62 w('interface %s' % node.id) | 70 w('interface %s' % node.id) |
| 63 begin_indent() | 71 begin_indent() |
| 64 begin_indent() | 72 begin_indent() |
| 65 if node.parents: | 73 if node.parents: |
| 66 wln(' :') | 74 wln(' :') |
| 67 w(node.parents, ',\n') | 75 w(node.parents, ',\n') |
| 68 wln(' {') | 76 wln(' {') |
| 69 end_indent() | 77 end_indent() |
| 70 if node.constants: | 78 if node.constants: |
| 71 wln() | 79 wln() |
| 72 wln('/* Constants */') | 80 wln('/* Constants */') |
| 73 w(sort(node.constants)) | 81 w(sort(node.constants)) |
| 74 if node.attributes: | 82 if node.attributes: |
| 75 wln() | 83 wln() |
| 76 wln('/* Attributes */') | 84 wln('/* Attributes */') |
| 77 w(sort(node.attributes)) | 85 w(sort(node.attributes)) |
| 78 if node.operations: | 86 if node.operations: |
| 79 wln() | 87 wln() |
| 80 wln('/* Operations */') | 88 wln('/* Operations */') |
| 81 w(sort(node.operations)) | 89 w(sort(node.operations)) |
| 82 end_indent() | 90 end_indent() |
| 83 wln('};') | 91 wln('};') |
| 84 elif isinstance(node, IDLParentInterface): | 92 elif isinstance(node, IDLParentInterface): |
| 85 w(node.annotations) | 93 wsp(node.annotations) |
| 86 w(node.type.id) | 94 w(node.type.id) |
| 87 elif isinstance(node, IDLAnnotations): | 95 elif isinstance(node, IDLAnnotations): |
| 96 sep = '' |
| 88 for (name, annotation) in sorted(node.items()): | 97 for (name, annotation) in sorted(node.items()): |
| 98 w(sep) |
| 99 sep = ' ' |
| 89 if annotation and len(annotation): | 100 if annotation and len(annotation): |
| 90 subRes = [] | 101 subRes = [] |
| 91 for (argName, argValue) in sorted(annotation.items()): | 102 for (argName, argValue) in sorted(annotation.items()): |
| 92 if argValue is None: | 103 if argValue is None: |
| 93 subRes.append(argName) | 104 subRes.append(argName) |
| 94 else: | 105 else: |
| 95 subRes.append('%s=%s' % (argName, argValue)) | 106 subRes.append('%s=%s' % (argName, argValue)) |
| 96 w('@%s(%s)' % (name, ', '.join(subRes))) | 107 w('@%s(%s)' % (name, ', '.join(subRes))) |
| 97 else: | 108 else: |
| 98 w('@%s' % name) | 109 w('@%s' % name) |
| 99 w(' ') | |
| 100 elif isinstance(node, IDLExtAttrs): | 110 elif isinstance(node, IDLExtAttrs): |
| 101 if len(node): | 111 if len(node): |
| 102 w('[') | 112 w('[') |
| 103 i = 0 | 113 i = 0 |
| 104 for k in sorted(node): | 114 for k in sorted(node): |
| 105 if i > 0: | 115 if i > 0: |
| 106 w(', ') | 116 w(', ') |
| 107 w(k) | 117 w(k) |
| 108 v = node[k] | 118 v = node[k] |
| 109 if v is not None: | 119 if v is not None: |
| 110 if isinstance(v, IDLExtAttrFunctionValue): | 120 if isinstance(v, IDLExtAttrFunctionValue): |
| 111 if v.id: | 121 if v.id: |
| 112 w('=') | 122 w('=') |
| 113 w(v) | 123 w(v) |
| 114 else: | 124 else: |
| 115 w('=%s' % v.__str__()) | 125 w('=%s' % v.__str__()) |
| 116 i += 1 | 126 i += 1 |
| 117 w('] ') | 127 w(']') |
| 118 elif isinstance(node, IDLExtAttrFunctionValue): | 128 elif isinstance(node, IDLExtAttrFunctionValue): |
| 119 if node.id: | 129 if node.id: |
| 120 w(node.id) | 130 w(node.id) |
| 121 w('(') | 131 w('(') |
| 122 w(node.arguments, ', ') | 132 w(node.arguments, ', ') |
| 123 w(')') | 133 w(')') |
| 124 elif isinstance(node, IDLAttribute): | 134 elif isinstance(node, IDLAttribute): |
| 125 w(node.annotations) | 135 wsp(node.annotations) |
| 126 w(node.ext_attrs) | 136 wsp(node.ext_attrs) |
| 127 if node.is_fc_getter: | 137 if node.is_fc_getter: |
| 128 w('getter ') | 138 w('getter ') |
| 129 if node.is_fc_setter: | 139 if node.is_fc_setter: |
| 130 w('setter ') | 140 w('setter ') |
| 131 w('attribute %s %s' % (node.type.id, node.id)) | 141 w('attribute %s %s' % (node.type.id, node.id)) |
| 132 if node.raises: | 142 if node.raises: |
| 133 w(' raises (%s)' % node.raises.id) | 143 w(' raises (%s)' % node.raises.id) |
| 134 elif node.is_fc_getter and node.get_raises: | 144 elif node.is_fc_getter and node.get_raises: |
| 135 w(' getraises (%s)' % node.get_raises.id) | 145 w(' getraises (%s)' % node.get_raises.id) |
| 136 elif node.is_fc_setter and node.set_raises: | 146 elif node.is_fc_setter and node.set_raises: |
| 137 w(' setraises (%s)' % node.set_raises.id) | 147 w(' setraises (%s)' % node.set_raises.id) |
| 138 wln(';') | 148 wln(';') |
| 139 elif isinstance(node, IDLConstant): | 149 elif isinstance(node, IDLConstant): |
| 140 w(node.annotations) | 150 wsp(node.annotations) |
| 141 w(node.ext_attrs) | 151 wsp(node.ext_attrs) |
| 142 wln('const %s %s = %s;' % (node.type.id, node.id, node.value)) | 152 wln('const %s %s = %s;' % (node.type.id, node.id, node.value)) |
| 143 elif isinstance(node, IDLOperation): | 153 elif isinstance(node, IDLOperation): |
| 144 w(node.annotations) | 154 wsp(node.annotations) |
| 145 w(node.ext_attrs) | 155 wsp(node.ext_attrs) |
| 146 if node.is_static: | 156 if node.is_static: |
| 147 w('static ') | 157 w('static ') |
| 148 if node.specials: | 158 if node.specials: |
| 149 w(node.specials, ' ') | 159 w(node.specials, ' ') |
| 150 w(' ') | 160 w(' ') |
| 151 w('%s ' % node.type.id) | 161 w('%s ' % node.type.id) |
| 152 w(node.id) | 162 w(node.id) |
| 153 w('(') | 163 w('(') |
| 154 w(node.arguments, ', ') | 164 w(node.arguments, ', ') |
| 155 w(')') | 165 w(')') |
| 156 if node.raises: | 166 if node.raises: |
| 157 w(' raises (%s)' % node.raises.id) | 167 w(' raises (%s)' % node.raises.id) |
| 158 wln(';') | 168 wln(';') |
| 159 elif isinstance(node, IDLArgument): | 169 elif isinstance(node, IDLArgument): |
| 160 w(node.ext_attrs) | 170 wsp(node.ext_attrs) |
| 161 w('in ') | 171 w('in ') |
| 162 if node.is_optional: | 172 if node.is_optional: |
| 163 w('optional ') | 173 w('optional ') |
| 164 w('%s %s' % (node.type.id, node.id)) | 174 w('%s %s' % (node.type.id, node.id)) |
| 165 else: | 175 else: |
| 166 raise TypeError("Expected str or IDLNode but %s found" % | 176 raise TypeError("Expected str or IDLNode but %s found" % |
| 167 type(node)) | 177 type(node)) |
| 168 | 178 |
| 169 w(idl_node) | 179 w(idl_node) |
| 170 return ''.join(output) | 180 return ''.join(output) |
| OLD | NEW |