Factory for a convenience function that is used to orphan @string_leaf and then insert multiple new leaves into the same part of the node structure that @string_leaf had originally occupied. Examples: Let `string_leaf = Leaf(token.STRING, '"foo"')` and `N = string_l
(string_leaf: Leaf)
| 2491 | |
| 2492 | |
| 2493 | def insert_str_child_factory(string_leaf: Leaf) -> Callable[[LN], None]: |
| 2494 | """ |
| 2495 | Factory for a convenience function that is used to orphan @string_leaf |
| 2496 | and then insert multiple new leaves into the same part of the node |
| 2497 | structure that @string_leaf had originally occupied. |
| 2498 | |
| 2499 | Examples: |
| 2500 | Let `string_leaf = Leaf(token.STRING, '"foo"')` and `N = |
| 2501 | string_leaf.parent`. Assume the node `N` has the following |
| 2502 | original structure: |
| 2503 | |
| 2504 | Node( |
| 2505 | expr_stmt, [ |
| 2506 | Leaf(NAME, 'x'), |
| 2507 | Leaf(EQUAL, '='), |
| 2508 | Leaf(STRING, '"foo"'), |
| 2509 | ] |
| 2510 | ) |
| 2511 | |
| 2512 | We then run the code snippet shown below. |
| 2513 | ``` |
| 2514 | insert_str_child = insert_str_child_factory(string_leaf) |
| 2515 | |
| 2516 | lpar = Leaf(token.LPAR, '(') |
| 2517 | insert_str_child(lpar) |
| 2518 | |
| 2519 | bar = Leaf(token.STRING, '"bar"') |
| 2520 | insert_str_child(bar) |
| 2521 | |
| 2522 | rpar = Leaf(token.RPAR, ')') |
| 2523 | insert_str_child(rpar) |
| 2524 | ``` |
| 2525 | |
| 2526 | After which point, it follows that `string_leaf.parent is None` and |
| 2527 | the node `N` now has the following structure: |
| 2528 | |
| 2529 | Node( |
| 2530 | expr_stmt, [ |
| 2531 | Leaf(NAME, 'x'), |
| 2532 | Leaf(EQUAL, '='), |
| 2533 | Leaf(LPAR, '('), |
| 2534 | Leaf(STRING, '"bar"'), |
| 2535 | Leaf(RPAR, ')'), |
| 2536 | ] |
| 2537 | ) |
| 2538 | """ |
| 2539 | string_parent = string_leaf.parent |
| 2540 | string_child_idx = string_leaf.remove() |
| 2541 | |
| 2542 | def insert_str_child(child: LN) -> None: |
| 2543 | nonlocal string_child_idx |
| 2544 | |
| 2545 | assert string_parent is not None |
| 2546 | assert string_child_idx is not None |
| 2547 | |
| 2548 | string_parent.insert_child(string_child_idx, child) |
| 2549 | string_child_idx += 1 |
| 2550 |
no test coverage detected