def __init__(self, *args, **kwargs): """ Construct a ParsedPattern object. Since ParsedPattern objects can contain other ParsedPattern objects, this can be called recursively, therefore it needs to do different things depending on how it is called. The external entry point, to create a new ParsedPattern from a pattern string: pp = ParsedPattern(pattern) Or if you want to limit it to the simple tokens: pp = ParsedPattern(pattern, simple=True) The following variations are used during the recursion process: To create an empty ParsedPattern: pp = ParsedPattern() To create a new ParsedPattern and fill with a list of tokens: pp = ParsedPattern(token_list) To create a new ParsedPattern from a token generator expression pp = ParsedPattern(token_gen, just_one=False, terminator=None) In this last version, setting just_one to True causes it to parse just one token and then return. Setting terminator to ")" or "]" causes it to return when it encounters that character. """ self.contents = [] if len(args) > 1: raise TypeError("Expected 1 argument, {0} given".format(len(args))) if not args: pass elif isinstance(args[0], list): self.contents.extend(args[0]) else: if isinstance(args[0], text_type): simple = kwargs.pop("simple", False) pattern = args[0] pattern = pattern.lower() if simple: tokens = self.pp_simple.tokens(pattern) else: tokens = self.pp.tokens(pattern) terminator = None just_one = False elif inspect.isgenerator(args[0]): tokens = args[0] terminator = kwargs.pop("terminator", None) just_one = kwargs.pop("just_one", False) else: raise TypeError("Expected unicode string") try: while True: token_class, text = next(tokens) token = token_class(tokens, text, terminator) token.add_to_parsetree(self) if just_one: break except StopScanLoop: pass except StopIteration: if terminator is not None: raise PatternError("Missing a closing parenthesis " "or square bracket") self.remove_trailing_space() if not self.contents: raise PatternError("Pattern string is empty") if kwargs: raise TypeError("Unexpected **kwargs: {0}".format(repr(kwargs)))
def regex(self, variables, counter): return "(?P<match{0}>{1})".format(next(counter), self.item.regex(variables, counter))