def RepN(expr, count): """expr, count -> match the expression 'count' number of time This option is handy for named group repeats since you don't have to use the name twice; for the min_count and max_count fields. """ return Expression.MaxRepeat(expr, count, count)
def MaxRepeat(expr, min_count, max_count=Expression.MAXREPEAT): """expr, min_count, max_count = 65535 -> match between min- and max_count times If max_count == 65535 (which is Expression.MAXREPEAT) then there is no upper limit. """ assert isinstance(expr, Expression.Expression), \ "expecting an Expression, not a %s" % type(expr) return Expression.MaxRepeat(expr, min_count, max_count)
attrs[k] = v[0] return Expression.Group(pattern_name, convert_list(group_names, terms), attrs) # Convert a 'newline' tuple into an AnyEol object def convert_newline(group_names, name, ignore): assert ignore is None, "what does it mean when field is %s?" % ` ignore ` return Expression.AnyEol() # Convert a 'max_repeat' tuple into a MaxRepeat object def convert_max_repeat(group_names, name, (min_count, max_count, terms)): return Expression.MaxRepeat(convert_list(group_names, terms), min_count, max_count) # Convert a 'groupref' tuple into a GroupRef object def convert_groupref(group_names, name, id): assert type(id) != type(0), \ "Martel cannot use numbered group reference: %d" % id # msre_parse returns the list from the GroupNames # Map that back to a number. pattern_name = group_names.reverse_name(id[0]) return Expression.GroupRef(pattern_name) # Map from the tuple typename into the function used to convert the # tuple into an Expression.
def Rep(expr): """expr -> match 'expr' as many times as possible, even 0 time""" assert isinstance(expr, Expression.Expression), \ "expecting an Expression, not a %s" % type(expr) return Expression.MaxRepeat(expr, 0)
def Opt(expr): """expr -> match 'expr' 1 or 0 times""" assert isinstance(expr, Expression.Expression), \ "expecting an Expression, not a %s" % type(expr) return Expression.MaxRepeat(expr, 0, 1)