MCPcopy Create free account
hub / github.com/PlatformLab/NanoLog / LeafPattern

Class LeafPattern

preprocessor/docopt.py:99–130  ·  view source on GitHub ↗

Leaf/terminal node of a pattern tree.

Source from the content-addressed store, hash-verified

97
98
99class 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
133class BranchPattern(Pattern):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected