4 # Copyright IBM, Corp. 2011
5 # Copyright (c) 2013-2015 Red Hat Inc.
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Markus Armbruster <armbru@redhat.com>
11 # This work is licensed under the terms of the GNU GPL, version 2.
12 # See the COPYING file in the top-level directory.
15 from ordereddict
import OrderedDict
20 'str': 'QTYPE_QSTRING',
22 'number': 'QTYPE_QFLOAT',
23 'bool': 'QTYPE_QBOOL',
25 'int16': 'QTYPE_QINT',
26 'int32': 'QTYPE_QINT',
27 'int64': 'QTYPE_QINT',
28 'uint8': 'QTYPE_QINT',
29 'uint16': 'QTYPE_QINT',
30 'uint32': 'QTYPE_QINT',
31 'uint64': 'QTYPE_QINT',
35 # Whitelist of commands allowed to return a non-dictionary
38 'human-monitor-command',
39 'query-migrate-cache-size',
46 'guest-fsfreeze-freeze',
47 'guest-fsfreeze-freeze-list',
48 'guest-fsfreeze-status',
49 'guest-fsfreeze-thaw',
53 'guest-sync-delimited',
55 # From qapi-schema-test:
65 def error_path(parent
):
68 res
= ("In file included from %s:%d:\n" % (parent
['file'],
69 parent
['line'])) + res
70 parent
= parent
['parent']
73 class QAPISchemaError(Exception):
74 def __init__(self
, schema
, msg
):
75 self
.input_file
= schema
.input_file
78 self
.line
= schema
.line
79 for ch
in schema
.src
[schema
.line_pos
:schema
.pos
]:
81 self
.col
= (self
.col
+ 7) % 8 + 1
84 self
.info
= schema
.parent_info
87 return error_path(self
.info
) + \
88 "%s:%d:%d: %s" % (self
.input_file
, self
.line
, self
.col
, self
.msg
)
90 class QAPIExprError(Exception):
91 def __init__(self
, expr_info
, msg
):
96 return error_path(self
.info
['parent']) + \
97 "%s:%d: %s" % (self
.info
['file'], self
.info
['line'], self
.msg
)
101 def __init__(self
, fp
, input_relname
=None, include_hist
=[],
102 previously_included
=[], parent_info
=None):
103 """ include_hist is a stack used to detect inclusion cycles
104 previously_included is a global state used to avoid multiple
105 inclusions of the same file"""
106 input_fname
= os
.path
.abspath(fp
.name
)
107 if input_relname
is None:
108 input_relname
= fp
.name
109 self
.input_dir
= os
.path
.dirname(input_fname
)
110 self
.input_file
= input_relname
111 self
.include_hist
= include_hist
+ [(input_relname
, input_fname
)]
112 previously_included
.append(input_fname
)
113 self
.parent_info
= parent_info
115 if self
.src
== '' or self
.src
[-1] != '\n':
123 while self
.tok
!= None:
124 expr_info
= {'file': input_relname
, 'line': self
.line
, 'parent': self
.parent_info
}
125 expr
= self
.get_expr(False)
126 if isinstance(expr
, dict) and "include" in expr
:
128 raise QAPIExprError(expr_info
, "Invalid 'include' directive")
129 include
= expr
["include"]
130 if not isinstance(include
, str):
131 raise QAPIExprError(expr_info
,
132 'Expected a file name (string), got: %s'
134 include_path
= os
.path
.join(self
.input_dir
, include
)
135 for elem
in self
.include_hist
:
136 if include_path
== elem
[1]:
137 raise QAPIExprError(expr_info
, "Inclusion loop for %s"
139 # skip multiple include of the same file
140 if include_path
in previously_included
:
143 fobj
= open(include_path
, 'r')
145 raise QAPIExprError(expr_info
,
146 '%s: %s' % (e
.strerror
, include
))
147 exprs_include
= QAPISchema(fobj
, include
, self
.include_hist
,
148 previously_included
, expr_info
)
149 self
.exprs
.extend(exprs_include
.exprs
)
151 expr_elem
= {'expr': expr
,
153 self
.exprs
.append(expr_elem
)
157 self
.tok
= self
.src
[self
.cursor
]
158 self
.pos
= self
.cursor
163 self
.cursor
= self
.src
.find('\n', self
.cursor
)
164 elif self
.tok
in ['{', '}', ':', ',', '[', ']']:
166 elif self
.tok
== "'":
170 ch
= self
.src
[self
.cursor
]
173 raise QAPISchemaError(self
,
174 'Missing terminating "\'"')
188 for x
in range(0, 4):
189 ch
= self
.src
[self
.cursor
]
191 if ch
not in "0123456789abcdefABCDEF":
192 raise QAPISchemaError(self
,
193 '\\u escape needs 4 '
195 value
= (value
<< 4) + int(ch
, 16)
196 # If Python 2 and 3 didn't disagree so much on
197 # how to handle Unicode, then we could allow
198 # Unicode string defaults. But most of QAPI is
199 # ASCII-only, so we aren't losing much for now.
200 if not value
or value
> 0x7f:
201 raise QAPISchemaError(self
,
202 'For now, \\u escape '
203 'only supports non-zero '
204 'values up to \\u007f')
209 raise QAPISchemaError(self
,
210 "Unknown escape \\%s" %ch
)
219 elif self
.tok
in "tfn":
220 val
= self
.src
[self
.cursor
- 1:]
221 if val
.startswith("true"):
225 elif val
.startswith("false"):
229 elif val
.startswith("null"):
233 elif self
.tok
== '\n':
234 if self
.cursor
== len(self
.src
):
238 self
.line_pos
= self
.cursor
239 elif not self
.tok
.isspace():
240 raise QAPISchemaError(self
, 'Stray "%s"' % self
.tok
)
242 def get_members(self
):
248 raise QAPISchemaError(self
, 'Expected string or "}"')
253 raise QAPISchemaError(self
, 'Expected ":"')
256 raise QAPISchemaError(self
, 'Duplicate key "%s"' % key
)
257 expr
[key
] = self
.get_expr(True)
262 raise QAPISchemaError(self
, 'Expected "," or "}"')
265 raise QAPISchemaError(self
, 'Expected string')
267 def get_values(self
):
272 if not self
.tok
in "{['tfn":
273 raise QAPISchemaError(self
, 'Expected "{", "[", "]", string, '
276 expr
.append(self
.get_expr(True))
281 raise QAPISchemaError(self
, 'Expected "," or "]"')
284 def get_expr(self
, nested
):
285 if self
.tok
!= '{' and not nested
:
286 raise QAPISchemaError(self
, 'Expected "{"')
289 expr
= self
.get_members()
290 elif self
.tok
== '[':
292 expr
= self
.get_values()
293 elif self
.tok
in "'tfn":
297 raise QAPISchemaError(self
, 'Expected "{", "[" or string')
300 def find_base_fields(base
):
301 base_struct_define
= find_struct(base
)
302 if not base_struct_define
:
304 return base_struct_define
['data']
306 # Return the qtype of an alternate branch, or None on error.
307 def find_alternate_member_qtype(qapi_type
):
308 if builtin_types
.has_key(qapi_type
):
309 return builtin_types
[qapi_type
]
310 elif find_struct(qapi_type
):
312 elif find_enum(qapi_type
):
313 return "QTYPE_QSTRING"
314 elif find_union(qapi_type
):
318 # Return the discriminator enum define if discriminator is specified as an
319 # enum type, otherwise return None.
320 def discriminator_find_enum_define(expr
):
321 base
= expr
.get('base')
322 discriminator
= expr
.get('discriminator')
324 if not (discriminator
and base
):
327 base_fields
= find_base_fields(base
)
331 discriminator_type
= base_fields
.get(discriminator
)
332 if not discriminator_type
:
335 return find_enum(discriminator_type
)
337 valid_name
= re
.compile('^[a-zA-Z_][a-zA-Z0-9_.-]*$')
338 def check_name(expr_info
, source
, name
, allow_optional
= False,
339 enum_member
= False):
343 if not isinstance(name
, str):
344 raise QAPIExprError(expr_info
,
345 "%s requires a string name" % source
)
346 if name
.startswith('*'):
347 membername
= name
[1:]
348 if not allow_optional
:
349 raise QAPIExprError(expr_info
,
350 "%s does not allow optional name '%s'"
352 # Enum members can start with a digit, because the generated C
353 # code always prefixes it with the enum name
355 membername
= '_' + membername
356 if not valid_name
.match(membername
):
357 raise QAPIExprError(expr_info
,
358 "%s uses invalid name '%s'" % (source
, name
))
360 def check_type(expr_info
, source
, value
, allow_array
= False,
361 allow_dict
= False, allow_optional
= False,
362 allow_star
= False, allow_metas
= []):
369 if allow_star
and value
== '**':
372 # Check if array type for value is okay
373 if isinstance(value
, list):
375 raise QAPIExprError(expr_info
,
376 "%s cannot be an array" % source
)
377 if len(value
) != 1 or not isinstance(value
[0], str):
378 raise QAPIExprError(expr_info
,
379 "%s: array type must contain single type name"
382 orig_value
= "array of %s" %value
384 # Check if type name for value is okay
385 if isinstance(value
, str):
387 raise QAPIExprError(expr_info
,
388 "%s uses '**' but did not request 'gen':false"
390 if not value
in all_names
:
391 raise QAPIExprError(expr_info
,
392 "%s uses unknown type '%s'"
393 % (source
, orig_value
))
394 if not all_names
[value
] in allow_metas
:
395 raise QAPIExprError(expr_info
,
396 "%s cannot use %s type '%s'"
397 % (source
, all_names
[value
], orig_value
))
400 # value is a dictionary, check that each member is okay
401 if not isinstance(value
, OrderedDict
):
402 raise QAPIExprError(expr_info
,
403 "%s should be a dictionary" % source
)
405 raise QAPIExprError(expr_info
,
406 "%s should be a type name" % source
)
407 for (key
, arg
) in value
.items():
408 check_name(expr_info
, "Member of %s" % source
, key
,
409 allow_optional
=allow_optional
)
410 # Todo: allow dictionaries to represent default values of
411 # an optional argument.
412 check_type(expr_info
, "Member '%s' of %s" % (key
, source
), arg
,
413 allow_array
=True, allow_star
=allow_star
,
414 allow_metas
=['built-in', 'union', 'alternate', 'struct',
417 def check_member_clash(expr_info
, base_name
, data
, source
= ""):
418 base
= find_struct(base_name
)
420 base_members
= base
['data']
421 for key
in data
.keys():
422 if key
.startswith('*'):
424 if key
in base_members
or "*" + key
in base_members
:
425 raise QAPIExprError(expr_info
,
426 "Member name '%s'%s clashes with base '%s'"
427 % (key
, source
, base_name
))
429 check_member_clash(expr_info
, base
['base'], data
, source
)
431 def check_command(expr
, expr_info
):
432 name
= expr
['command']
433 allow_star
= expr
.has_key('gen')
435 check_type(expr_info
, "'data' for command '%s'" % name
,
436 expr
.get('data'), allow_dict
=True, allow_optional
=True,
437 allow_metas
=['union', 'struct'], allow_star
=allow_star
)
438 returns_meta
= ['union', 'struct']
439 if name
in returns_whitelist
:
440 returns_meta
+= ['built-in', 'alternate', 'enum']
441 check_type(expr_info
, "'returns' for command '%s'" % name
,
442 expr
.get('returns'), allow_array
=True, allow_dict
=True,
443 allow_optional
=True, allow_metas
=returns_meta
,
444 allow_star
=allow_star
)
446 def check_event(expr
, expr_info
):
449 params
= expr
.get('data')
451 if name
.upper() == 'MAX':
452 raise QAPIExprError(expr_info
, "Event name 'MAX' cannot be created")
454 check_type(expr_info
, "'data' for event '%s'" % name
,
455 expr
.get('data'), allow_dict
=True, allow_optional
=True,
456 allow_metas
=['union', 'struct'])
458 def check_union(expr
, expr_info
):
460 base
= expr
.get('base')
461 discriminator
= expr
.get('discriminator')
462 members
= expr
['data']
463 values
= { 'MAX': '(automatic)' }
465 # If the object has a member 'base', its value must name a struct,
466 # and there must be a discriminator.
468 if discriminator
is None:
469 raise QAPIExprError(expr_info
,
470 "Union '%s' requires a discriminator to go "
471 "along with base" %name
)
473 # Two types of unions, determined by discriminator.
475 # With no discriminator it is a simple union.
476 if discriminator
is None:
478 allow_metas
=['built-in', 'union', 'alternate', 'struct', 'enum']
480 raise QAPIExprError(expr_info
,
481 "Simple union '%s' must not have a base"
484 # Else, it's a flat union.
486 # The object must have a string member 'base'.
487 if not isinstance(base
, str):
488 raise QAPIExprError(expr_info
,
489 "Flat union '%s' must have a string base field"
491 base_fields
= find_base_fields(base
)
493 raise QAPIExprError(expr_info
,
494 "Base '%s' is not a valid struct"
497 # The value of member 'discriminator' must name a non-optional
498 # member of the base struct.
499 check_name(expr_info
, "Discriminator of flat union '%s'" % name
,
501 discriminator_type
= base_fields
.get(discriminator
)
502 if not discriminator_type
:
503 raise QAPIExprError(expr_info
,
504 "Discriminator '%s' is not a member of base "
506 % (discriminator
, base
))
507 enum_define
= find_enum(discriminator_type
)
508 allow_metas
=['struct']
509 # Do not allow string discriminator
511 raise QAPIExprError(expr_info
,
512 "Discriminator '%s' must be of enumeration "
513 "type" % discriminator
)
516 for (key
, value
) in members
.items():
517 check_name(expr_info
, "Member of union '%s'" % name
, key
)
519 # Each value must name a known type; furthermore, in flat unions,
520 # branches must be a struct with no overlapping member names
521 check_type(expr_info
, "Member '%s' of union '%s'" % (key
, name
),
522 value
, allow_array
=True, allow_metas
=allow_metas
)
524 branch_struct
= find_struct(value
)
526 check_member_clash(expr_info
, base
, branch_struct
['data'],
527 " of branch '%s'" % key
)
529 # If the discriminator names an enum type, then all members
530 # of 'data' must also be members of the enum type.
532 if not key
in enum_define
['enum_values']:
533 raise QAPIExprError(expr_info
,
534 "Discriminator value '%s' is not found in "
536 (key
, enum_define
["enum_name"]))
538 # Otherwise, check for conflicts in the generated enum
540 c_key
= _generate_enum_string(key
)
542 raise QAPIExprError(expr_info
,
543 "Union '%s' member '%s' clashes with '%s'"
544 % (name
, key
, values
[c_key
]))
547 def check_alternate(expr
, expr_info
):
548 name
= expr
['alternate']
549 members
= expr
['data']
550 values
= { 'MAX': '(automatic)' }
554 for (key
, value
) in members
.items():
555 check_name(expr_info
, "Member of alternate '%s'" % name
, key
)
557 # Check for conflicts in the generated enum
558 c_key
= _generate_enum_string(key
)
560 raise QAPIExprError(expr_info
,
561 "Alternate '%s' member '%s' clashes with '%s'"
562 % (name
, key
, values
[c_key
]))
565 # Ensure alternates have no type conflicts.
566 check_type(expr_info
, "Member '%s' of alternate '%s'" % (key
, name
),
568 allow_metas
=['built-in', 'union', 'struct', 'enum'])
569 qtype
= find_alternate_member_qtype(value
)
571 if qtype
in types_seen
:
572 raise QAPIExprError(expr_info
,
573 "Alternate '%s' member '%s' can't "
574 "be distinguished from member '%s'"
575 % (name
, key
, types_seen
[qtype
]))
576 types_seen
[qtype
] = key
578 def check_enum(expr
, expr_info
):
580 members
= expr
.get('data')
581 values
= { 'MAX': '(automatic)' }
583 if not isinstance(members
, list):
584 raise QAPIExprError(expr_info
,
585 "Enum '%s' requires an array for 'data'" % name
)
586 for member
in members
:
587 check_name(expr_info
, "Member of enum '%s'" %name
, member
,
589 key
= _generate_enum_string(member
)
591 raise QAPIExprError(expr_info
,
592 "Enum '%s' member '%s' clashes with '%s'"
593 % (name
, member
, values
[key
]))
596 def check_struct(expr
, expr_info
):
597 name
= expr
['struct']
598 members
= expr
['data']
600 check_type(expr_info
, "'data' for struct '%s'" % name
, members
,
601 allow_dict
=True, allow_optional
=True)
602 check_type(expr_info
, "'base' for struct '%s'" % name
, expr
.get('base'),
603 allow_metas
=['struct'])
605 check_member_clash(expr_info
, expr
['base'], expr
['data'])
607 def check_exprs(schema
):
608 for expr_elem
in schema
.exprs
:
609 expr
= expr_elem
['expr']
610 info
= expr_elem
['info']
612 if expr
.has_key('enum'):
613 check_enum(expr
, info
)
614 elif expr
.has_key('union'):
615 check_union(expr
, info
)
616 elif expr
.has_key('alternate'):
617 check_alternate(expr
, info
)
618 elif expr
.has_key('struct'):
619 check_struct(expr
, info
)
620 elif expr
.has_key('command'):
621 check_command(expr
, info
)
622 elif expr
.has_key('event'):
623 check_event(expr
, info
)
625 assert False, 'unexpected meta type'
627 def check_keys(expr_elem
, meta
, required
, optional
=[]):
628 expr
= expr_elem
['expr']
629 info
= expr_elem
['info']
631 if not isinstance(name
, str):
632 raise QAPIExprError(info
,
633 "'%s' key must have a string value" % meta
)
634 required
= required
+ [ meta
]
635 for (key
, value
) in expr
.items():
636 if not key
in required
and not key
in optional
:
637 raise QAPIExprError(info
,
638 "Unknown key '%s' in %s '%s'"
640 if (key
== 'gen' or key
== 'success-response') and value
!= False:
641 raise QAPIExprError(info
,
642 "'%s' of %s '%s' should only use false value"
645 if not expr
.has_key(key
):
646 raise QAPIExprError(info
,
647 "Key '%s' is missing from %s '%s'"
651 def parse_schema(input_file
):
655 # First pass: read entire file into memory
657 schema
= QAPISchema(open(input_file
, "r"))
658 except (QAPISchemaError
, QAPIExprError
), e
:
659 print >>sys
.stderr
, e
663 # Next pass: learn the types and check for valid expression keys. At
664 # this point, top-level 'include' has already been flattened.
665 for builtin
in builtin_types
.keys():
666 all_names
[builtin
] = 'built-in'
667 for expr_elem
in schema
.exprs
:
668 expr
= expr_elem
['expr']
669 info
= expr_elem
['info']
670 if expr
.has_key('enum'):
671 check_keys(expr_elem
, 'enum', ['data'])
672 add_enum(expr
['enum'], info
, expr
['data'])
673 elif expr
.has_key('union'):
674 check_keys(expr_elem
, 'union', ['data'],
675 ['base', 'discriminator'])
676 add_union(expr
, info
)
677 elif expr
.has_key('alternate'):
678 check_keys(expr_elem
, 'alternate', ['data'])
679 add_name(expr
['alternate'], info
, 'alternate')
680 elif expr
.has_key('struct'):
681 check_keys(expr_elem
, 'struct', ['data'], ['base'])
682 add_struct(expr
, info
)
683 elif expr
.has_key('command'):
684 check_keys(expr_elem
, 'command', [],
685 ['data', 'returns', 'gen', 'success-response'])
686 add_name(expr
['command'], info
, 'command')
687 elif expr
.has_key('event'):
688 check_keys(expr_elem
, 'event', [], ['data'])
689 add_name(expr
['event'], info
, 'event')
691 raise QAPIExprError(expr_elem
['info'],
692 "Expression is missing metatype")
695 # Try again for hidden UnionKind enum
696 for expr_elem
in schema
.exprs
:
697 expr
= expr_elem
['expr']
698 if expr
.has_key('union'):
699 if not discriminator_find_enum_define(expr
):
700 add_enum('%sKind' % expr
['union'], expr_elem
['info'],
702 elif expr
.has_key('alternate'):
703 add_enum('%sKind' % expr
['alternate'], expr_elem
['info'],
706 # Final pass - validate that exprs make sense
708 except QAPIExprError
, e
:
709 print >>sys
.stderr
, e
714 def parse_args(typeinfo
):
715 if isinstance(typeinfo
, str):
716 struct
= find_struct(typeinfo
)
717 assert struct
!= None
718 typeinfo
= struct
['data']
720 for member
in typeinfo
:
722 argentry
= typeinfo
[member
]
724 if member
.startswith('*'):
727 # Todo: allow argentry to be OrderedDict, for providing the
728 # value of an optional argument.
729 yield (argname
, argentry
, optional
)
731 def de_camel_case(name
):
734 if ch
.isupper() and new_name
:
739 new_name
+= ch
.lower()
742 def camel_case(name
):
749 new_name
+= ch
.upper()
752 new_name
+= ch
.lower()
755 def c_var(name
, protect
=True):
756 # ANSI X3J11/88-090, 3.1.1
757 c89_words
= set(['auto', 'break', 'case', 'char', 'const', 'continue',
758 'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
759 'for', 'goto', 'if', 'int', 'long', 'register', 'return',
760 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
761 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
762 # ISO/IEC 9899:1999, 6.4.1
763 c99_words
= set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
764 # ISO/IEC 9899:2011, 6.4.1
765 c11_words
= set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
766 '_Static_assert', '_Thread_local'])
767 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
769 gcc_words
= set(['asm', 'typeof'])
770 # C++ ISO/IEC 14882:2003 2.11
771 cpp_words
= set(['bool', 'catch', 'class', 'const_cast', 'delete',
772 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
773 'namespace', 'new', 'operator', 'private', 'protected',
774 'public', 'reinterpret_cast', 'static_cast', 'template',
775 'this', 'throw', 'true', 'try', 'typeid', 'typename',
776 'using', 'virtual', 'wchar_t',
777 # alternative representations
778 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
779 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
780 # namespace pollution:
781 polluted_words
= set(['unix', 'errno'])
782 if protect
and (name
in c89_words | c99_words | c11_words | gcc_words | cpp_words | polluted_words
):
784 return name
.replace('-', '_').lstrip("*")
786 def c_fun(name
, protect
=True):
787 return c_var(name
, protect
).replace('.', '_')
789 def c_list_type(name
):
790 return '%sList' % name
793 if type(name
) == list:
794 return c_list_type(name
[0])
797 def add_name(name
, info
, meta
, implicit
= False):
799 check_name(info
, "'%s'" % meta
, name
)
800 if name
in all_names
:
801 raise QAPIExprError(info
,
802 "%s '%s' is already defined"
803 % (all_names
[name
], name
))
804 if not implicit
and name
[-4:] == 'Kind':
805 raise QAPIExprError(info
,
806 "%s '%s' should not end in 'Kind'"
808 all_names
[name
] = meta
810 def add_struct(definition
, info
):
812 name
= definition
['struct']
813 add_name(name
, info
, 'struct')
814 struct_types
.append(definition
)
816 def find_struct(name
):
818 for struct
in struct_types
:
819 if struct
['struct'] == name
:
823 def add_union(definition
, info
):
825 name
= definition
['union']
826 add_name(name
, info
, 'union')
827 union_types
.append(definition
)
829 def find_union(name
):
831 for union
in union_types
:
832 if union
['union'] == name
:
836 def add_enum(name
, info
, enum_values
= None, implicit
= False):
838 add_name(name
, info
, 'enum', implicit
)
839 enum_types
.append({"enum_name": name
, "enum_values": enum_values
})
843 for enum
in enum_types
:
844 if enum
['enum_name'] == name
:
849 return find_enum(name
) != None
851 eatspace
= '\033EATSPACE.'
853 # A special suffix is added in c_type() for pointer types, and it's
854 # stripped in mcgen(). So please notice this when you check the return
855 # value of c_type() outside mcgen().
856 def c_type(name
, is_param
=False):
859 return 'const char *' + eatspace
860 return 'char *' + eatspace
864 elif (name
== 'int8' or name
== 'int16' or name
== 'int32' or
865 name
== 'int64' or name
== 'uint8' or name
== 'uint16' or
866 name
== 'uint32' or name
== 'uint64'):
872 elif name
== 'number':
874 elif type(name
) == list:
875 return '%s *%s' % (c_list_type(name
[0]), eatspace
)
878 elif name
== None or len(name
) == 0:
881 return '%sEvent *%s' % (camel_case(name
), eatspace
)
883 return '%s *%s' % (name
, eatspace
)
886 suffix
= "*" + eatspace
887 return c_type(name
).endswith(suffix
)
889 def genindent(count
):
891 for i
in range(count
):
897 def push_indent(indent_amount
=4):
899 indent_level
+= indent_amount
901 def pop_indent(indent_amount
=4):
903 indent_level
-= indent_amount
905 def cgen(code
, **kwds
):
906 indent
= genindent(indent_level
)
907 lines
= code
.split('\n')
908 lines
= map(lambda x
: indent
+ x
, lines
)
909 return '\n'.join(lines
) % kwds
+ '\n'
911 def mcgen(code
, **kwds
):
912 raw
= cgen('\n'.join(code
.split('\n')[1:-1]), **kwds
)
913 return re
.sub(re
.escape(eatspace
) + ' *', '', raw
)
915 def basename(filename
):
916 return filename
.split("/")[-1]
918 def guardname(filename
):
919 guard
= basename(filename
).rsplit(".", 1)[0]
920 for substr
in [".", " ", "-"]:
921 guard
= guard
.replace(substr
, "_")
922 return guard
.upper() + '_H'
924 def guardstart(name
):
931 name
=guardname(name
))
936 #endif /* %(name)s */
939 name
=guardname(name
))
941 # ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
942 # ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
943 # ENUM24_Name -> ENUM24_NAME
944 def _generate_enum_string(value
):
945 c_fun_str
= c_fun(value
, False)
953 # When c is upper and no "_" appears before, do more checks
954 if c
.isupper() and (i
> 0) and c_fun_str
[i
- 1] != "_":
955 # Case 1: next string is lower
956 # Case 2: previous string is digit
957 if (i
< (l
- 1) and c_fun_str
[i
+ 1].islower()) or \
958 c_fun_str
[i
- 1].isdigit():
961 return new_name
.lstrip('_').upper()
963 def generate_enum_full_value(enum_name
, enum_value
):
964 abbrev_string
= _generate_enum_string(enum_name
)
965 value_string
= _generate_enum_string(enum_value
)
966 return "%s_%s" % (abbrev_string
, value_string
)