| 1084 | } |
| 1085 | |
| 1086 | func (p *pattern) transform() *pattern { |
| 1087 | /* |
| 1088 | Expand pattern into an (almost) equivalent one, but with single Either. |
| 1089 | |
| 1090 | Example: ((-a | -b) (-c | -d)) => (-a -c | -a -d | -b -c | -b -d) |
| 1091 | Quirks: [-a] => (-a), (-a...) => (-a -a) |
| 1092 | */ |
| 1093 | result := []patternList{} |
| 1094 | groups := []patternList{patternList{p}} |
| 1095 | parents := patternRequired + |
| 1096 | patternOptionAL + |
| 1097 | patternOptionSSHORTCUT + |
| 1098 | patternEither + |
| 1099 | patternOneOrMore |
| 1100 | for len(groups) > 0 { |
| 1101 | children := groups[0] |
| 1102 | groups = groups[1:] |
| 1103 | var child *pattern |
| 1104 | for _, c := range children { |
| 1105 | if c.t&parents != 0 { |
| 1106 | child = c |
| 1107 | break |
| 1108 | } |
| 1109 | } |
| 1110 | if child != nil { |
| 1111 | children.remove(child) |
| 1112 | if child.t&patternEither != 0 { |
| 1113 | for _, c := range child.children { |
| 1114 | r := patternList{} |
| 1115 | r = append(r, c) |
| 1116 | r = append(r, children...) |
| 1117 | groups = append(groups, r) |
| 1118 | } |
| 1119 | } else if child.t&patternOneOrMore != 0 { |
| 1120 | r := patternList{} |
| 1121 | r = append(r, child.children.double()...) |
| 1122 | r = append(r, children...) |
| 1123 | groups = append(groups, r) |
| 1124 | } else { |
| 1125 | r := patternList{} |
| 1126 | r = append(r, child.children...) |
| 1127 | r = append(r, children...) |
| 1128 | groups = append(groups, r) |
| 1129 | } |
| 1130 | } else { |
| 1131 | result = append(result, children) |
| 1132 | } |
| 1133 | } |
| 1134 | either := patternList{} |
| 1135 | for _, e := range result { |
| 1136 | either = append(either, newRequired(e...)) |
| 1137 | } |
| 1138 | return newEither(either...) |
| 1139 | } |
| 1140 | |
| 1141 | func (p *pattern) eq(other *pattern) bool { |
| 1142 | return reflect.DeepEqual(p, other) |