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