(self, pattern, start)
Recursively adds a linear pattern to the AC automaton
Recursively adds a linear pattern to the AC automaton
Recursively adds a linear pattern to the AC automaton
def add(self, pattern, start):
        "Recursively adds a linear pattern to the AC automaton"
        #print("adding pattern", pattern, "to", start)
        if not pattern:
            #print("empty pattern")
            return [start]
        if isinstance(pattern[0], tuple):
            #alternatives
            #print("alternatives")
            match_nodes = []
            for alternative in pattern[0]:
                #add all alternatives, and add the rest of the pattern
                #to each end node
                end_nodes = self.add(alternative, start=start)
                for end in end_nodes:
                    match_nodes.extend(self.add(pattern[1:], end))
            return match_nodes
        else:
            #single token
            #not last
            if pattern[0] not in start.transition_table:
                #transition did not exist, create new
                next_node = BMNode()
                start.transition_table[pattern[0]] = next_node
            else:
                #transition exists already, follow
                next_node = start.transition_table[pattern[0]]

            if pattern[1:]:
                end_nodes = self.add(pattern[1:], start=next_node)
            else:
                end_nodes = [next_node]
            return end_nodes
def add ( self , pattern , start ) : #print("adding pattern", pattern, "to", start) if not pattern : #print("empty pattern") return [ start ] if isinstance ( pattern [ 0 ] , tuple ) : #alternatives #print("alternatives") match_nodes = [ ] for alternative in pattern [ 0 ] : #add all alternatives, and add the rest of the pattern #to each end node end_nodes = self . add ( alternative , start = start ) for end in end_nodes : match_nodes . extend ( self . add ( pattern [ 1 : ] , end ) ) return match_nodes else : #single token #not last if pattern [ 0 ] not in start . transition_table : #transition did not exist, create new next_node = BMNode ( ) start . transition_table [ pattern [ 0 ] ] = next_node else : #transition exists already, follow next_node = start . transition_table [ pattern [ 0 ] ] if pattern [ 1 : ] : end_nodes = self . add ( pattern [ 1 : ] , start = next_node ) else : end_nodes = [ next_node ] return end_nodes