def parse_obj(text: bytes, start=0) -> Any:
    """Recursively parse bytes into PDF objects."""
    h1, j1, t1 = next_token(text, start)
    obj = text[h1:j1]
    if t1 == 'DICT':
        res = {}
        res_array = []
        h2, j2, t2 = next_token(text, j1)
        following_obj = text[h2:j2]
        if t2 == 'STREAM': 
            stream_def =  parse_obj(obj)
            stream_encoded =  parse_obj(following_obj)
            stream_content = decode_stream(stream_encoded, stream_def)
            res = Stream(stream_def, stream_content, stream_encoded)
            return res

        i = start + 2
        while i < j1 - 1:
            h, i, t = next_token(text, i)
            if t and t != 'COMMENT':
                if i < j1 and t:
                    if t == 'DICT' or t == 'ARRAY':
                        obj = parse_obj(text[h:i])
                    else:
                        obj = dedicated_type(text[h:i], t)
                    res_array.append(obj)
        toggle = True
        res_array = replace_ref(res_array)
        j = len(res_array) - 1
        while j >= 0:
            if toggle:
                val = res_array[j]
                toggle = False
            else:
                key = res_array[j]
                res[key] = val
                toggle = True
            j -= 1
        return res
    elif t1 == 'ARRAY':
        res = []
        i = start + 1
        while i < j1 - 1:
            h, i, t = next_token(text, i)
            if t and t != 'COMMENT':
                if t == 'DICT' or t == 'ARRAY':
                    obj = parse_obj(text[h:i])
                else:
                    obj = dedicated_type(text[h:i], t)
                res.append(obj)
        res = replace_ref(res)
        return res
    elif t1 == 'STREAM':
        b1 = b'stream' + b'\r\n'
        b2 = b'stream' + b'\n'
        e1 = b'\r\n' + b'endstream'
        e2 = b'\n' + b'endstream'
        e3 = b'\r' + b'endstream'
        e4 = b'endstream'
        if obj[:len(b1)] == b1:
            b = len(b1)
        elif obj[:len(b2)] == b2:
            b = len(b2)
        else:
            return None
        if obj[-len(e1):] == e1:
            e = len(e1)
        elif obj[-len(e2):] == e2:
            e = len(e2)
        elif obj[-len(e3):] == e3:
            e = len(e3)
        elif obj[-len(e4):] == e4:
            e = len(e4)
        else:
            return None
        s = obj[b:-e]
        return s
    elif t1 == 'COMMENT':
        return ''
    else:
        return dedicated_type(text[h1:j1], t1)
Recursively parse bytes into PDF objects.
pdfsyntax/objects.py