def handle_fields(definitions, fields, prefix, offset, multiplier):
items = []<EOL>for f in fields:<EOL><INDENT>if f.type_id == "<STR_LIT>" and f.options['<STR_LIT>'].value in CONSTRUCT_CODE:<EOL><INDENT>prefix_name = '<STR_LIT:.>'.join([prefix, f.identifier]) if prefix else f.identifier<EOL>n_with_values = f.options['<STR_LIT>'].value<EOL>bitfields = f.options['<STR_LIT>'].value if n_with_values > <NUM_LIT:0> else None<EOL>if '<STR_LIT:size>' in f.options:<EOL><INDENT>name = "<STR_LIT>" % (f.options['<STR_LIT>'].value, str(f.options['<STR_LIT:size>'].value))<EOL>size = field_sizes[f.options['<STR_LIT>'].value] * f.options['<STR_LIT:size>'].value<EOL>item = FieldItem(prefix_name, name, offset, size,<EOL>str(f.units), f.desc, n_with_values, bitfields)<EOL>items.append(item)<EOL>offset += size<EOL><DEDENT>else:<EOL><INDENT>name = "<STR_LIT>" % (f.options['<STR_LIT>'].value, "<STR_LIT:N>")<EOL>multiplier = field_sizes[f.options['<STR_LIT>'].value]<EOL>size = field_sizes[f.options['<STR_LIT>'].value] * <NUM_LIT:1><EOL>item = FieldItem(prefix_name, name, offset, "<STR_LIT:N>",<EOL>str(f.units), f.desc, n_with_values, bitfields)<EOL>items.append(item)<EOL>offset += size<EOL><DEDENT><DEDENT>elif f.type_id == "<STR_LIT:string>":<EOL><INDENT>prefix_name = '<STR_LIT:.>'.join([prefix, f.identifier]) if prefix else f.identifier<EOL>n_with_values = f.options['<STR_LIT>'].value<EOL>bitfields = f.options['<STR_LIT>'].value if n_with_values > <NUM_LIT:0> else None<EOL>if '<STR_LIT:size>' in f.options:<EOL><INDENT>name = "<STR_LIT:string>"<EOL>size = field_sizes['<STR_LIT>'] * f.options['<STR_LIT:size>'].value<EOL>item = FieldItem(prefix_name, name, offset, size,<EOL>str(f.units), f.desc, n_with_values, bitfields)<EOL>items.append(item)<EOL>offset += size<EOL><DEDENT>else:<EOL><INDENT>name = "<STR_LIT:string>"<EOL>size = field_sizes['<STR_LIT>']<EOL>multiplier = <NUM_LIT:1><EOL>item = FieldItem(prefix_name, name, offset, "<STR_LIT:N>",<EOL>str(f.units), f.desc, n_with_values, bitfields)<EOL>items.append(item)<EOL>offset += size<EOL><DEDENT><DEDENT>elif f.type_id == "<STR_LIT>":<EOL><INDENT>name = f.options['<STR_LIT>'].value<EOL>definition = next(d for d in definitions if name == d.identifier)<EOL>prefix_name = '<STR_LIT:.>'.join([prefix, f.identifier]) if prefix else f.identifier<EOL>(new_items, new_offset, new_multiplier)= handle_fields(definitions,<EOL>definition.fields,<EOL>prefix_name + "<STR_LIT>",<EOL>offset,<EOL>multiplier)<EOL>multiplier = new_offset - offset<EOL>(newer_items, newer_offset, newer_multiplier)= handle_fields(definitions,<EOL>definition.fields,<EOL>prefix_name + "<STR_LIT>", offset,<EOL>multiplier)<EOL>items += newer_items<EOL>offset = newer_offset<EOL><DEDENT>elif f.type_id not in CONSTRUCT_CODE:<EOL><INDENT>name = f.type_id<EOL>definition = next(d for d in definitions if name == d.identifier)<EOL>prefix_name = '<STR_LIT:.>'.join([prefix, f.identifier]) if prefix else f.identifier<EOL>(new_items, new_offset, new_multiplier)= handle_fields(definitions,<EOL>definition.fields,<EOL>prefix_name,<EOL>offset,<EOL>multiplier)<EOL>items += new_items<EOL>offset = new_offset<EOL>multiplier = new_multiplier<EOL><DEDENT>else:<EOL><INDENT>size = field_sizes[f.type_id]<EOL>name = f.type_id<EOL>adj_offset = "<STR_LIT>" % (multiplier, offset) if multiplier else offset<EOL>prefix_name = '<STR_LIT:.>'.join([prefix, f.identifier]) if prefix else f.identifier<EOL>n_with_values = f.options['<STR_LIT>'].value<EOL>bitfields = f.options['<STR_LIT>'].value if n_with_values > <NUM_LIT:0> else None<EOL>item = FieldItem(prefix_name, name, adj_offset, size, str(f.units), f.desc, n_with_values, bitfields)<EOL>items.append(item)<EOL>offset += size<EOL><DEDENT><DEDENT>return (items, offset, multiplier)<EOL>
Helper for handling naming and sizing of fields. It's terrible.