def Alt(*args): """exp1, exp2, ... -> match exp1 or (if that fails) match exp2 or ...""" # Do some type checking for arg in args: assert isinstance(arg, Expression.Expression), \ "expecting an Expression, not a %s" % type(arg) return Expression.Alt(args)
# Convert an 'assert_not' tuple into a Assert object, as a negative assertion def convert_assert_not(group_names, name, (direction, terms)): assert direction == 1, "does not support lookbehind" return Expression.Assert(convert_list(group_names, terms), 1) # Convert a 'branch' tuple into an Alt object def convert_branch(group_names, name, (ignore, branches)): assert ignore is None, "what is %s?" % repr(ignore) results = [] for branch in branches: results.append(convert_list(group_names, branch)) if len(results) == 1: return results[0] return Expression.Alt(tuple(results)) # I know, it's only good for ASCII... def invert(s): """s -> a string containing all the characters not present in s""" letters = [] if not (isinstance(s, type(""))): s = str(s) for c in map(chr, range(256)): if c not in s: letters.append(c) return string.join(letters, "") # Map from the msre_parse category names into actual characters.
def Str(*args): """(s1, s2, ...) -> match s1 or s2 or ...""" if len(args) == 1: return Str1(args[0]) return Expression.Alt(tuple(map(Str, args)))