def _onmatch_rules_lookup(tokens, onmatch_rules):
    """ Creates a dict lookup from current to previous token.

    Returns
    -------
    dict of {str: dict of {str: list of int}}
        Dictionary keyed by current token to previous token containing a list of
        :class:`OnMatchRule` in order that would apply
    """
    onmatch_lookup = {}
    for rule_i, rule in enumerate(onmatch_rules):
        for token_key, token_classes in tokens.items():
            if rule.next_classes[0] in token_classes:
                for prev_token_key, prev_token_classes in tokens.items():
                    if rule.prev_classes[-1] in prev_token_classes:
                        if token_key not in onmatch_lookup:
                            onmatch_lookup[token_key] = {}
                        curr_token = onmatch_lookup[token_key]
                        if prev_token_key not in curr_token:
                            curr_token[prev_token_key] = []
                        rule_list = curr_token[prev_token_key]
                        rule_list.append(rule_i)
    return onmatch_lookup
