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
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."""
25 output.append('\n')
26
27 def wsp(node):
28 """Writes the given node and adds a space if there was output."""
31 if mark !=
len(output):
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
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:
54 elif isinstance(node, IDLFile):
58 elif isinstance(node, IDLModule):
59 wsp(node.annotations)
60 wsp(node.ext_attrs)
61 wln('module %s {' % node.id)
62 begin_indent()
66 end_indent()
67 wln('};')
68 elif isinstance(node, IDLEnum):
69 w(
'enum %s {}' % node.id)
70
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)
101 elif isinstance(node, IDLAnnotations):
102 sep = ''
103 for (name, annotation) in sorted(node.items()):
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:
116 elif isinstance(node, IDLExtAttrs):
119 i = 0
120 for k in sorted(node):
121 if i > 0:
124 v = node[k]
125 if v is not None:
126 if isinstance(v, IDLExtAttrFunctionValue):
127 if v.id:
130 elif isinstance(v, list):
131 assert k == 'Constructor'
133 for c in v[1:]:
137 else:
138 w(
'=%s' % v.__str__())
139 i += 1
141 elif isinstance(node, IDLExtAttrFunctionValue):
142 if node.id:
145 w(node.arguments,
', ')
147 elif isinstance(node, IDLAttribute):
148 wsp(node.annotations)
149 wsp(node.ext_attrs)
150 if node.is_read_only:
154 if (node.type.nullable):
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:
168 if node.specials:
169 w(node.specials,
' ')
172 if (node.type.nullable):
177 w(node.arguments,
', ')
179 wln(';')
180 elif isinstance(node, IDLArgument):
181 wsp(node.ext_attrs)
182 if (node.optional):
185 if node.type.nullable:
188 else:
189 raise TypeError(
"Expected str or IDLNode but %s found" %
type(node))
190
192 return ''.
join(output)
static std::vector< SkPDFIndirectReference > sort(const THashSet< SkPDFIndirectReference > &src)
def render(idl_node, indent_str=' ')
static SkString join(const CommandLineFlags::StringArray &)