def repeat(rule, from_count, to_count=None, allow_whitespace=False):
"""Allow between from_count and to_count repetitions of a rule.
If to_count is not given, then allow as many repetitions as can be
parsed.
"""
if to_count == 0:
return null
if from_count == 0 and to_count is None:
return many(rule, allow_whitespace=allow_whitespace)
op = operator.__add__ if allow_whitespace else operator.__xor__
next_to_count = to_count - 1 if to_count is not None else None
next_from_count = from_count - 1 if from_count > 0 else 0
first_part = optional(rule) if from_count == 0 else rule
return op(first_part, repeat(rule, next_from_count, next_to_count,
allow_whitespace))
评论列表
文章目录