Ejemplo n.º 1
0
 def match_zero_or_more(self, text, index=0):
     """
     Accumulates zero or more matches into a single Match object.
     This will always return a Match, (0 or 1) = true
     """
     accumulated_match = Match(index, '')
     while True:
         start_index = index + len(accumulated_match.text)
         match = self.match_all_sub_queries(text, start_index)
         if match and match.index == start_index:
             accumulated_match = Match.join_matches([accumulated_match, match])
         else: break
     return accumulated_match
Ejemplo n.º 2
0
    def match_all_sub_queries(self, text, index=0):
        """
            Recursively matches all subqueries in this Query.
            This function searches left to right for matches to subqueries.
            For each subquery, if a match is found, then:
                if it is adjacent to the last match or it is the first match
                    for this query, we add it to the matches array
                otherwise, we empty the matches array and start searching from
                    the first subquery again
            If at any point no match for a subquery is found, we return None
            Once we have found matches for all subqueries, we join all matches
                into a single Match object, which is then returned.
        """
        while index < len(text):
            matches = []
            for i, query in enumerate(self.queries):
                match = None
                if type(query) is str:
                    found_index = text.find(query, index)
                    match = Match(found_index, query)
                else:
                    match = query.match(text, index)

                if match is None or match.index == -1: return None

                if matches == [] or index == match.index:
                    index = match.index + len(match.text)
                    matches.append(match)
                elif index != 0 and index != match.index:
                    curr_match = Match.join_matches(matches)
                    if len(curr_match.text) == 0:
                        index += 1
                    break
                else:
                    return None
            else:
                return Match.join_matches(matches)
        return None