def tokenize(expr):
    """
    Parse a string expression into a set of tokens that can be used as a path
    into a Python datastructure.
    """
    tokens = []
    escape = False
    cur_token = ''
    for c in expr:
        if escape == True:
            cur_token += c
            escape = False
        elif c == '\\':
            escape = True
            continue
        elif c == '[':
            if len(cur_token) > 0:
                tokens.append(cur_token)
                cur_token = ''
        elif c == ']':
            if len(cur_token) > 0:
                tokens.append(int(cur_token))
                cur_token = ''
        elif c == '.':
            if len(cur_token) > 0:
                tokens.append(cur_token)
                cur_token = ''
        else:
            cur_token += c
    if len(cur_token) > 0:
        tokens.append(cur_token)
    return tokens
