easyparse.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:easyparse 作者: jayvanderwall 项目源码 文件源码
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))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号