Leaf/terminal node of a pattern tree.
| 97 | |
| 98 | |
| 99 | class LeafPattern(Pattern): |
| 100 | |
| 101 | """Leaf/terminal node of a pattern tree.""" |
| 102 | |
| 103 | def __init__(self, name, value=None): |
| 104 | self.name, self.value = name, value |
| 105 | |
| 106 | def __repr__(self): |
| 107 | return '%s(%r, %r)' % (self.__class__.__name__, self.name, self.value) |
| 108 | |
| 109 | def flat(self, *types): |
| 110 | return [self] if not types or type(self) in types else [] |
| 111 | |
| 112 | def match(self, left, collected=None): |
| 113 | collected = [] if collected is None else collected |
| 114 | pos, match = self.single_match(left) |
| 115 | if match is None: |
| 116 | return False, left, collected |
| 117 | left_ = left[:pos] + left[pos + 1:] |
| 118 | same_name = [a for a in collected if a.name == self.name] |
| 119 | if type(self.value) in (int, list): |
| 120 | if type(self.value) is int: |
| 121 | increment = 1 |
| 122 | else: |
| 123 | increment = ([match.value] if type(match.value) is str |
| 124 | else match.value) |
| 125 | if not same_name: |
| 126 | match.value = increment |
| 127 | return True, left_, collected + [match] |
| 128 | same_name[0].value += increment |
| 129 | return True, left_, collected |
| 130 | return True, left_, collected + [match] |
| 131 | |
| 132 | |
| 133 | class BranchPattern(Pattern): |
nothing calls this directly
no outgoing calls
no test coverage detected