def match(tokens, rule):
    """Checks if a token stream matches a rule.

    Expects the rule part of the rule tuple (rule[1]).
    Returns 0 if it doesn't match, 1 if it matches the begin, and 2 if it
    matches entirely.
    """
    for r in rule:
        if len(tokens) > len(r):
            continue
        for i in range(len(tokens)):
            if not tokens[i] or tokens[i].bnf() != r[i]:
                break
        else:   # executed if the loop ends without break
            return 2 if len(tokens) == len(r) else 1
    return 0