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