def _print_def_enum(name, enum): params = "" for v in enum["values"]: params += ENUM_DEF_PARAMS % { "enum": name, "params": enum["values"][v] } utils.out_text(ENUM_DEF_CODE % { "name": name, "params": params, "size": len(enum["values"]) })
def start_gen(file_name): try: f = open(file_name, "r") except: utils.error("Cannot open %s" % file_name) utils.out_code(SET_OUT_CODE % file_name) for l in f: start_index = l.find(COMMENT_START) if start_index >= 0: if start_index > 0: utils.out_text(l[:start_index]) l = _exec_comment(f, _parse_comment(f, l[start_index:]), 0) utils.out_text(l)
def _print_enum(name, enum): fields = "" for s in enum["fields"]: fields += ENUM_VALUE % {"value": s} values = "" for v in enum["values"]: values += ENUM_FIELD % { "enum": name, "name": v } utils.out_text(ENUM_CODE % { "name": name, "fields": fields, "values": values }) if ENUM_OPT_MACRO_LEN[0] in enum["options"]: utils.out_text(ENUM_OPT_MACRO_LEN[1] % { "macro": enum["options"][ENUM_OPT_MACRO_LEN[0]], "length": len(enum["values"]) })
# Trim comment line start def _trim_comment(comment, (markup_index, markup_line), markup_length): comment[markup_line] = comment[markup_line][markup_index + markup_length:] if comment[markup_line].startswith(' '): comment[markup_line] = comment[markup_line][1:] for i in range(markup_line + 1, len(comment)): comment[i] = comment[i][markup_index:] if comment[i].startswith(MARKUP_END): return (comment[:i], True) return (comment, False) # def _exec_comment_util(f, (l, comment), (markup_index, markup_line), generator): while True: for c in comment: utils.out_text(c) comment, end = _trim_comment(comment, (markup_index, markup_line), len(generator['markup'])) generator['generator'](comment[markup_line:]) if end: return l try: while True: start_index = l.find(COMMENT_START) if start_index >= 0: l, comment = _parse_comment(f, l[start_index:]) markup_line = 0 for c in comment: if c.find(MARKUP_END) >= 0: return _exec_comment(f, (l, comment), markup_line) markup_line += 1 break