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)
164 elif name == 'number':
166 elif type(name) == list:
167 return '%s *' % c_list_type(name[0])
170 elif name == None or len(name) == 0:
172 elif name == name.upper():
173 return '%sEvent *' % camel_case(name)
177 def genindent(count):
179 for i in range(count):
185 def push_indent(indent_amount=4):
187 indent_level += indent_amount
189 def pop_indent(indent_amount=4):
191 indent_level -= indent_amount
193 def cgen(code, **kwds):
194 indent = genindent(indent_level)
195 lines = code.split('\n')
196 lines = map(lambda x: indent + x, lines)
197 return '\n'.join(lines) % kwds + '\n'
199 def mcgen(code, **kwds):
200 return cgen('\n'.join(code.split('\n')[1:-1]), **kwds)
202 def basename(filename):
203 return filename.split("/")[-1]
205 def guardname(filename):
206 guard = basename(filename).rsplit(".", 1)[0]
207 for substr in [".", " ", "-"]:
208 guard = guard.replace(substr, "_")
209 return guard.upper() + '_H'