def next_token(text: bytes, i=0) -> tuple:
    """Find next token in raw string starting at some index."""
    search = "TBD"
    nested = 1
    text_in_array = 0
    h = i
    while i < len(text):
        single = text[i:i+1]
        double = text[i:i+2]
        if search == "TBD":
            if single == b']':
                return (h, i, None)
            elif double == b'>>':
                return (h, i+1, None)
            elif double == b'<<':
                search = "DICT"
            elif single == b'[':
                search = "ARRAY"
            elif single == b"/":
                search = "NAME"
            elif text[i:i+6] == b"stream":
                search = "STREAM"
                #if text[i+6:i+8] == b'\r\n':
                #    offset = 8
                #else:
                #    offset = 7
                offset = 0
                i += offset
            elif single in b"+-.0123456789":
                search = "VALUE"
            elif single in b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
                search = "KEYWORD"
            elif single in b"(":
                search = "LSTRING"
            elif single in b"<":
                search = "HSTRING"
            elif single == b'%':
                search = "COMMENT"
            h = i
        elif search == "DICT":
            if double == b'<<':
                nested += 1
                i += 1
            elif double == b'>>':
                nested -= 1
                i += 1
            if nested == 0:
                return (h, i + 1, 'DICT')
            else:
                pass
        elif search == "NAME":
            if single in (SPACE + DELIMITERS):
                return (h, i, 'NAME')
        elif search == "COMMENT":
            if single in EOL:
                return (h, i, 'COMMENT')
        elif search == "LSTRING":
            if double == b'\\(' or double == b'\\)':
                i += 1
            elif single == b'(':
                nested += 1
            elif single == b')':
                nested -= 1
            if nested == 0:
                return (h, i + 1, 'STRING')
        elif search == "HSTRING":
            if double == b'\\<' or double == b'\\>':
                i += 1
            else:
                if single in b'>':
                    return (h, i + 1, 'STRING')
        elif search == "KEYWORD":
            if single in (SPACE + DELIMITERS): #or i == len(text) - 1:
                if b'true' == text[h:i]:
                    return (h, i, 'TRUE')
                elif b'false' == text[h:i]:
                    return (h, i, 'FALSE')
                else:
                    return (h, i, 'KEYWORD')
        elif search == "VALUE":
            if single in (SPACE + DELIMITERS): #or i == len(text) - 1:
                if b'.' in text[h:i]:
                    return (h, i, 'REAL')
                else:
                    return (h, i, 'INTEGER')
        elif search == "STREAM":
            if text[i:i+11] == b'\r\nendstream':
                return (h, i+11, 'STREAM')
            elif  text[i:i+10] == b'\nendstream':
                return (h, i+10, 'STREAM')
            elif  text[i:i+10] == b'\rendstream':
                return (h, i+10, 'STREAM')
            elif  text[i:i+9] == b'endstream':
                return (h, i+9, 'STREAM')
        elif search == "ARRAY":
            if single == b'(':
                text_in_array = 1
            elif single == b')':
                text_in_array = 0
            elif text_in_array == 0 and single == b'[':
                nested += 1
            elif text_in_array == 0 and single == b']':
                nested -= 1
            if nested == 0:
                return (h, i + 1, 'ARRAY')
        i += 1
    return (h, i, None)
Find next token in raw string starting at some index.
pdfsyntax/objects.py