#!/usr/bin/env python3 # -*- coding: utf-8 -*- import argparse import collections import textwrap import os import hashlib import struct import io from gen_param_defs import member_defs, ParamDef, IndentWriterBase def _cname_to_fbname(cname): return { "uint32_t": "uint", "uint64_t": "ulong", "int32_t": "int", "float": "float", "double": "double", "DTypeEnum": "DTypeEnum", "bool": "bool", }[cname] def scramble_enum_member_name(name): if name in ("MIN", "MAX"): return name + "_" return name class FlatBuffersWriter(IndentWriterBase): _skip_current_param = False _last_param = None _enums = None _used_enum = None _cur_const_val = {} def __call__(self, fout, defs): param_io = io.StringIO() super().__call__(param_io) self._used_enum = set() self._enums = {} self._process(defs) super().__call__(fout) self._write("// %s", self._get_header()) self._write('include "dtype.fbs";') self._write("namespace mgb.serialization.fbs.param;\n") self._write_enums() self._write(param_io.getvalue()) def _write_enums(self): for (p, e) in sorted(self._used_enum): name = p + e e = self._enums[(p, e)] self._write_doc(e.name) self._write("enum %s%s : uint {", p, e.name, indent=1) for idx, member in enumerate(e.members): self._write_doc(member) if e.combined: self._write("%s=%d,", scramble_enum_member_name(str(member)), 1<