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
18 if ch in ['{', '}', ':', ',', '[', ']']:
27 raise Exception("Mismatched quotes")
45 while tokens[0] != '}':
49 tokens = tokens[1:] # :
51 value, tokens = parse(tokens)
59 elif tokens[0] == '[':
62 while tokens[0] != ']':
63 value, tokens = parse(tokens)
70 return tokens[0], tokens[1:]
73 return parse(map(lambda x: x, tokenize(string)))[0]
81 if line.startswith('#') or line == '\n':
84 if line.startswith(' '):
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)
98 expr_eval = evaluate(expr)
99 if expr_eval.has_key('enum'):
100 add_enum(expr_eval['enum'])
101 elif expr_eval.has_key('union'):
102 add_enum('%sKind' % expr_eval['union'])
103 exprs.append(expr_eval)
107 def parse_args(typeinfo):
108 for member in typeinfo:
110 argentry = typeinfo[member]
113 if member.startswith('*'):
116 if isinstance(argentry, OrderedDict):
118 yield (argname, argentry, optional, structured)
120 def de_camel_case(name):
123 if ch.isupper() and new_name:
128 new_name += ch.lower()
131 def camel_case(name):
138 new_name += ch.upper()
141 new_name += ch.lower()
144 def c_var(name, protect=True):
145 # ANSI X3J11/88-090, 3.1.1
146 c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
147 'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
148 'for', 'goto', 'if', 'int', 'long', 'register', 'return',
149 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
150 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
151 # ISO/IEC 9899:1999, 6.4.1
152 c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
153 # ISO/IEC 9899:2011, 6.4.1
154 c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
155 '_Static_assert', '_Thread_local'])
156 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
158 gcc_words = set(['asm', 'typeof'])
159 # namespace pollution:
160 polluted_words = set(['unix'])
161 if protect and (name in c89_words | c99_words | c11_words | gcc_words | polluted_words):
163 return name.replace('-', '_').lstrip("*")
165 def c_fun(name, protect=True):
166 return c_var(name, protect).replace('.', '_')
168 def c_list_type(name):
169 return '%sList' % name
172 if type(name) == list:
173 return c_list_type(name[0])
180 enum_types.append(name)
184 return (name in enum_types)
191 elif (name == 'int8' or name == 'int16' or name == 'int32' or
192 name == 'int64' or name == 'uint8' or name == 'uint16' or
193 name == 'uint32' or name == 'uint64'):
199 elif name == 'number':
201 elif type(name) == list:
202 return '%s *' % c_list_type(name[0])
205 elif name == None or len(name) == 0:
207 elif name == name.upper():
208 return '%sEvent *' % camel_case(name)
212 def genindent(count):
214 for i in range(count):
220 def push_indent(indent_amount=4):
222 indent_level += indent_amount
224 def pop_indent(indent_amount=4):
226 indent_level -= indent_amount
228 def cgen(code, **kwds):
229 indent = genindent(indent_level)
230 lines = code.split('\n')
231 lines = map(lambda x: indent + x, lines)
232 return '\n'.join(lines) % kwds + '\n'
234 def mcgen(code, **kwds):
235 return cgen('\n'.join(code.split('\n')[1:-1]), **kwds)
237 def basename(filename):
238 return filename.split("/")[-1]
240 def guardname(filename):
241 guard = basename(filename).rsplit(".", 1)[0]
242 for substr in [".", " ", "-"]:
243 guard = guard.replace(substr, "_")
244 return guard.upper() + '_H'