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 '_'.join(name
.split('-')).lstrip("*")
136 def c_list_type(name
):
137 return '%sList' % name
140 if type(name
) == list:
141 return c_list_type(name
[0])
148 enum_types
.append(name
)
152 return (name
in enum_types
)
161 elif name
== 'number':
163 elif type(name
) == list:
164 return '%s *' % c_list_type(name
[0])
167 elif name
== None or len(name
) == 0:
169 elif name
== name
.upper():
170 return '%sEvent *' % camel_case(name
)
174 def genindent(count
):
176 for i
in range(count
):
182 def push_indent(indent_amount
=4):
184 indent_level
+= indent_amount
186 def pop_indent(indent_amount
=4):
188 indent_level
-= indent_amount
190 def cgen(code
, **kwds
):
191 indent
= genindent(indent_level
)
192 lines
= code
.split('\n')
193 lines
= map(lambda x
: indent
+ x
, lines
)
194 return '\n'.join(lines
) % kwds
+ '\n'
196 def mcgen(code
, **kwds
):
197 return cgen('\n'.join(code
.split('\n')[1:-1]), **kwds
)
199 def basename(filename
):
200 return filename
.split("/")[-1]
202 def guardname(filename
):
203 if filename
.startswith('./'):
204 filename
= filename
[2:]
205 return filename
.replace("/", "_").replace("-", "_").split(".")[0].upper() + '_H'