import logging
from lib import constant_pool
from lib import instruction
from lib import read_bytes


SAME = 0
SAME_LOCALS_1_STACK_ITEM = 64
SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247
CHOP = 248
SAME_FRAME_EXTENDED = 251
APPEND = 252
FULL_FRAME = 255


def parse(fd, class_file):
    '''Parse attributes
    '''
    count = read_bytes.read_u2_int(fd)
    attributes = []
    for _ in range(count):
        attr = parse_attr(fd, class_file)
        attributes.append(attr)
    return (count, attributes)


def parse_attr(fd, class_file):
    name_index = read_bytes.read_u2_int(fd)
    length = read_bytes.read_u4_int(fd)
    name_constant = class_file.constant_pool[name_index]
    assert type(name_constant) == constant_pool.ConstantUtf8, 'Attribute name constant is not CONSTANT_Utf8_info.'
    attribute_type = {
        'ConstantValue': ConstantValueAttribute,
        'Code': CodeAttribute,
        'StackMapTable': StackMapTableAttribute,