import re


def parse_event_fields(lines, idx, event_dict):
    """
        Parses lines from a proto file that contain an event definition and stores it in event_dict
    """
    fields = []
    end_of_event = False

    # record all fields in event definition.
    # note: we don't check if there's a leading brace.
    while not end_of_event and idx < len(lines):
        line = lines[idx].rstrip()
        idx += 1

        # ex 1: uint32_t    numSampleCLZExecuted; // number of sample_cl_z instructions executed
        # ex 2: char        reason[256]; // size of reason
        match = re.match(r'^(\s*)([\w\*]+)(\s+)([\w]+)(\[\d+\])*;\s*(\/\/.*)*$', line)
        # group 1 -
        # group 2 type
        # group 3 -
        # group 4 name
        # group 5 [array size]
        # group 6 //comment

        if match:
            field = {
                "type": match.group(2),
                "name": match.group(4),
                "size": int(match.group(5)[1:-1]) if match.group(5) else 1,
                "desc": match.group(6)[2:].strip() if match.group(6) else "",
            }
            fields.append(field)

        end_of_event = re.match(r'(\s*)};', line)

    event_dict['fields'] = fields
    event_dict['num_fields'] = len(fields)

    return idx