def mk_first_match_rule(c): if c == empty: cls = FirstEqualRule elif is_iterable(c): if len(c) == 1: cls = FirstEqualRule else: cls = FirstEqualAnyRule elif callable(c): cls = FirstEqualPredRule c = match_first_predicate(c) else: raise Err("Don't know how to make match from {}", c) return cls(c)
def match_iterable(name, pat, conv, options): if not cor.is_iterable(pat): raise Err("Don't know what to do with {}", seq) seq = [x for x in pat] if isinstance(pat, str) else pat @rule(name, options) def fn(src, pos): try: v = src[pos] except IndexError as e: v = empty if v in seq: v = conv(v) return (1, v) if v != nomatch else _nomatch_res else: return _nomatch_res return fn
def mk_rule(src): if isinstance(src, Rule): return src if is_iterable(src): return mk_first_match_rule(src) raise Err("Can't make rule from {}", src)
def __radd__(self, other): if not is_str(other) and is_iterable(other): return other.__add__((self,)) return mk_rule_seq(other, self, self._next_child_name)