| 193 | static tp_chain_t tp_parse_sequence(tp_expander_t *exp, unsigned depth, bool in_group); |
| 194 | |
| 195 | static tp_chain_t tp_parse_group(tp_expander_t *exp, unsigned depth) { |
| 196 | tp_chain_t result = {0}; |
| 197 | size_t open = exp->pos++; |
| 198 | if (depth >= TP_MAX_BRACE_DEPTH) { |
| 199 | exp->valid = false; |
| 200 | return result; |
| 201 | } |
| 202 | tp_expand_node_t *first = NULL, *last = NULL; |
| 203 | tp_expand_node_t *join = tp_expand_node(exp, NULL, 0); |
| 204 | size_t count = 0; |
| 205 | while (exp->valid) { |
| 206 | tp_chain_t branch = tp_parse_sequence(exp, depth + 1, true); |
| 207 | if (!exp->valid) |
| 208 | return result; |
| 209 | if (!branch.head) |
| 210 | tp_chain_literal(exp, &branch, NULL, 0); |
| 211 | if (!exp->valid) |
| 212 | return result; |
| 213 | branch.tail->next = join; |
| 214 | if (last) |
| 215 | last->sibling = branch.head; |
| 216 | else |
| 217 | first = branch.head; |
| 218 | last = branch.head; |
| 219 | count++; |
| 220 | if (exp->pos >= exp->len) { |
| 221 | exp->valid = false; |
| 222 | return result; |
| 223 | } |
| 224 | if (exp->text[exp->pos] == '}') { |
| 225 | exp->pos++; |
| 226 | break; |
| 227 | } |
| 228 | /* parse_sequence stops at either a comma or a closing brace. */ |
| 229 | exp->pos++; |
| 230 | } |
| 231 | if (!exp->valid) |
| 232 | return result; |
| 233 | if (count == 1) { |
| 234 | /* A group with no comma preserves its braces, while nested groups |
| 235 | * still expand: {a{b,c}} yields {ab} and {ac}. */ |
| 236 | tp_chain_literal(exp, &result, exp->text + open, 1); |
| 237 | if (!exp->valid) |
| 238 | return (tp_chain_t){0}; |
| 239 | result.tail->next = first; |
| 240 | result.tail = join; |
| 241 | tp_chain_literal(exp, &result, exp->text + exp->pos - 1, 1); |
| 242 | } else { |
| 243 | tp_expand_node_t *choice = tp_expand_node(exp, NULL, 0); |
| 244 | if (!choice) |
| 245 | return (tp_chain_t){0}; |
| 246 | choice->alternatives = first; |
| 247 | result.head = choice; |
| 248 | result.tail = join; |
| 249 | exp->alternative_count += count; |
| 250 | } |
| 251 | return result; |
| 252 | } |
no test coverage detected