4 # Copyright IBM, Corp. 2011
7 # Anthony Liguori <aliguori@us.ibm.com>
9 # This work is licensed under the terms of the GNU GPLv2.
10 # See the COPYING.LIB file in the top-level directory.
12 from ordereddict
import OrderedDict
16 if data
[0] in ['{', '}', ':', ',', '[', ']']:
19 elif data
[0] in ' \n':
34 while tokens
[0] != '}':
38 tokens
= tokens
[1:] # :
40 value
, tokens
= parse(tokens
)
48 elif tokens
[0] == '[':
51 while tokens
[0] != ']':
52 value
, tokens
= parse(tokens
)
59 return tokens
[0], tokens
[1:]
62 return parse(map(lambda x
: x
, tokenize(string
)))[0]
70 if line
.startswith('#') or line
== '\n':
73 if line
.startswith(' '):
76 expr_eval
= evaluate(expr
)
77 if expr_eval
.has_key('enum'):
78 add_enum(expr_eval
['enum'])
79 elif expr_eval
.has_key('union'):
80 add_enum('%sKind' % expr_eval
['union'])
81 exprs
.append(expr_eval
)
87 expr_eval
= evaluate(expr
)
88 if expr_eval
.has_key('enum'):
89 add_enum(expr_eval
['enum'])
90 elif expr_eval
.has_key('union'):
91 add_enum('%sKind' % expr_eval
['union'])
92 exprs
.append(expr_eval
)
96 def parse_args(typeinfo
):
97 for member
in typeinfo
:
99 argentry
= typeinfo
[member
]
102 if member
.startswith('*'):
105 if isinstance(argentry
, OrderedDict
):
107 yield (argname
, argentry
, optional
, structured
)
109 def de_camel_case(name
):
112 if ch
.isupper() and new_name
:
117 new_name
+= ch
.lower()
120 def camel_case(name
):
127 new_name
+= ch
.upper()
130 new_name
+= ch
.lower()
134 return name
.replace('-', '_').lstrip("*")
137 return c_var(name
).replace('.', '_')
139 def c_list_type(name
):
140 return '%sList' % name
143 if type(name
) == list:
144 return c_list_type(name
[0])
151 enum_types
.append(name
)
155 return (name
in enum_types
)
162 elif (name
== 'int8' or name
== 'int16' or name
== 'int32' or
163 name
== 'int64' or name
== 'uint8' or name
== 'uint16' or
164 name
== 'uint32' or name
== 'uint64'):
170 elif name
== 'number':
172 elif type(name
) == list:
173 return '%s *' % c_list_type(name
[0])
176 elif name
== None or len(name
) == 0:
178 elif name
== name
.upper():
179 return '%sEvent *' % camel_case(name
)
183 def genindent(count
):
185 for i
in range(count
):
191 def push_indent(indent_amount
=4):
193 indent_level
+= indent_amount
195 def pop_indent(indent_amount
=4):
197 indent_level
-= indent_amount
199 def cgen(code
, **kwds
):
200 indent
= genindent(indent_level
)
201 lines
= code
.split('\n')
202 lines
= map(lambda x
: indent
+ x
, lines
)
203 return '\n'.join(lines
) % kwds
+ '\n'
205 def mcgen(code
, **kwds
):
206 return cgen('\n'.join(code
.split('\n')[1:-1]), **kwds
)
208 def basename(filename
):
209 return filename
.split("/")[-1]
211 def guardname(filename
):
212 guard
= basename(filename
).rsplit(".", 1)[0]
213 for substr
in [".", " ", "-"]:
214 guard
= guard
.replace(substr
, "_")
215 return guard
.upper() + '_H'