"""Render Helpers. Naming and formatting shared by everything that renders C, so the generated code looks the same whichever generator wrote it.""" #################################################################################################################################### from common.string_id import STRING_ID_SEQ_NONE, str_id_from_z, str_id_seq # Width every line of generated code is formatted to, which is the width the project uses everywhere LINE_LENGTH = 132 # Block comment delimiters, e.g. /****...****/, and the separator that divides sections inside a function COMMENT_BLOCK_BEGIN = "/" + "*" * (LINE_LENGTH - 1) COMMENT_BLOCK_END = "*" * (LINE_LENGTH - 2) + "*/" COMMENT_SEPARATOR = " // " + "-" * (LINE_LENGTH - len(" // ")) # Column a #define value is aligned at _DEFINE_COLUMN = 68 #################################################################################################################################### def bld_define(define, value): """Format a #define with the value aligned in a column, e.g. "#define TEST_STRIDSTRID5(...)".""" return "#define %s%*s%s" % (define, _DEFINE_COLUMN - len("#define ") - len(define), "", value) #################################################################################################################################### def bld_comment_block(comment): """Format a block comment, which is how a section of generated code is titled.""" return "%s\n%s\n%s\n" % (COMMENT_BLOCK_BEGIN, comment, COMMENT_BLOCK_END) #################################################################################################################################### def bld_header(module, description): """Format the header every generated file begins with, which says not to edit the file and what to run instead.""" return bld_comment_block("%s\n\nAutomatically generated by 'build.py %s' -- do not modify directly." % (description, module)) #################################################################################################################################### def bld_enum(prefix, value): """Convert a dashed value to an enum name, e.g. "type-convert" becomes "typeConvert". When a prefix is given it is prepended and the first letter of the value is upper-cased, e.g. bld_enum("logLevel", "info") becomes "logLevelInfo".""" result = "" upper = False if prefix is not None: result = prefix upper = True for char in value: # A dash is removed and the letter that follows it is upper-cased if char == "-": upper = True continue result += char.upper() if upper else char upper = False return result #################################################################################################################################### def bld_str_id_seq(value, sequence=STRING_ID_SEQ_NONE): """Render the StringId macro for a value, e.g. STRID5("test", 0x2a7250), which is the form the source is expected to contain.""" str_id = str_id_from_z(value, sequence) if sequence is STRING_ID_SEQ_NONE: return 'STRID%u("%s", 0x%x)' % ((str_id & 1) + 5, value, str_id) return 'STRID%uS("%s", %u, 0x%x)' % ((str_id & 1) + 5, value, str_id_seq(str_id), str_id)