Expand pattern into an (almost) equivalent one, but with single Either. Example: ((-a | -b) (-c | -d)) => (-a -c | -a -d | -b -c | -b -d) Quirks: [-a] => (-a), (-a...) => (-a -a)
(pattern)
| 70 | |
| 71 | |
| 72 | def transform(pattern): |
| 73 | """Expand pattern into an (almost) equivalent one, but with single Either. |
| 74 | |
| 75 | Example: ((-a | -b) (-c | -d)) => (-a -c | -a -d | -b -c | -b -d) |
| 76 | Quirks: [-a] => (-a), (-a...) => (-a -a) |
| 77 | |
| 78 | """ |
| 79 | result = [] |
| 80 | groups = [[pattern]] |
| 81 | while groups: |
| 82 | children = groups.pop(0) |
| 83 | parents = [Required, Optional, OptionsShortcut, Either, OneOrMore] |
| 84 | if any(t in map(type, children) for t in parents): |
| 85 | child = [c for c in children if type(c) in parents][0] |
| 86 | children.remove(child) |
| 87 | if type(child) is Either: |
| 88 | for c in child.children: |
| 89 | groups.append([c] + children) |
| 90 | elif type(child) is OneOrMore: |
| 91 | groups.append(child.children * 2 + children) |
| 92 | else: |
| 93 | groups.append(child.children + children) |
| 94 | else: |
| 95 | result.append(children) |
| 96 | return Either(*[Required(*e) for e in result]) |
| 97 | |
| 98 | |
| 99 | class LeafPattern(Pattern): |
no test coverage detected