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_fc_getter: | |
138 w('getter ') | |
139 if node.is_fc_setter: | |
140 w('setter ') | |
141 w('attribute %s %s' % (node.type.id, node.id)) | |
142 if node.raises: | |
143 w(' raises (%s)' % node.raises.id) | |
144 elif node.is_fc_getter and node.get_raises: | |
145 w(' getraises (%s)' % node.get_raises.id) | |
146 elif node.is_fc_setter and node.set_raises: | |
147 w(' setraises (%s)' % node.set_raises.id) | |
148 wln(';') | |
149 elif isinstance(node, IDLConstant): | |
150 wsp(node.annotations) | |
151 wsp(node.ext_attrs) | |
152 wln('const %s %s = %s;' % (node.type.id, node.id, node.value)) | |
153 elif isinstance(node, IDLOperation): | |
154 wsp(node.annotations) | |
155 wsp(node.ext_attrs) | |
156 if node.is_static: | |
157 w('static ') | |
158 if node.specials: | |
159 w(node.specials, ' ') | |
160 w(' ') | |
161 w('%s ' % node.type.id) | |
162 w(node.id) | |
163 w('(') | |
164 w(node.arguments, ', ') | |
165 w(')') | |
166 if node.raises: | |
167 w(' raises (%s)' % node.raises.id) | |
168 wln(';') | |
169 elif isinstance(node, IDLArgument): | |
170 wsp(node.ext_attrs) | |
171 w('in ') | |
172 if node.is_optional: | |
173 w('optional ') | |
174 w('%s %s' % (node.type.id, node.id)) | |
175 else: | |
176 raise TypeError("Expected str or IDLNode but %s found" % | |
177 type(node)) | |
178 | |
179 w(idl_node) | |
180 return ''.join(output) | |
OLD | NEW |