def _num_tokens_of(rule):
    """Calculate the total number of tokens in a rule."""
    total = len(rule.get("tokens"))
    for _ in ("prev_classes", "prev_tokens", "next_tokens", "next_classes"):
        val = rule.get(_)
        if val:
            total += len(val)
    return total