${...}: a plain variable, an element 0, or a default (:- - := =). */
| 677 | |
| 678 | /* ${...}: a plain variable, an element 0, or a default (:- - := =). */ |
| 679 | static const char *sh_eval_expansion(sh_t *s, TSNode n) { |
| 680 | TSNode name = {0}; |
| 681 | const char *op = NULL; |
| 682 | uint32_t op_end = 0; |
| 683 | uint32_t close = ts_node_end_byte(n); |
| 684 | uint32_t cc = ts_node_child_count(n); |
| 685 | for (uint32_t i = 0; i < cc; i++) { |
| 686 | TSNode c = ts_node_child(n, i); |
| 687 | const char *t = ts_node_type(c); |
| 688 | if (ts_node_is_named(c)) { |
| 689 | if (ts_node_is_null(name)) { |
| 690 | name = c; |
| 691 | } else if (!op) { |
| 692 | return NULL; /* ${X%pattern} and the like: no plain value */ |
| 693 | } |
| 694 | continue; |
| 695 | } |
| 696 | if (strcmp(t, "${") == 0) { |
| 697 | continue; |
| 698 | } |
| 699 | if (strcmp(t, "}") == 0) { |
| 700 | close = ts_node_start_byte(c); |
| 701 | continue; |
| 702 | } |
| 703 | if (op) { |
| 704 | continue; /* inside the default */ |
| 705 | } |
| 706 | if (ts_node_is_null(name)) { |
| 707 | return NULL; /* ${#X}, ${!X}: not the value */ |
| 708 | } |
| 709 | if (strcmp(t, ":-") != 0 && strcmp(t, "-") != 0 && strcmp(t, ":=") != 0 && |
| 710 | strcmp(t, "=") != 0) { |
| 711 | return NULL; /* ${X%..}, ${X/..}, ${X:+..}, ... */ |
| 712 | } |
| 713 | op = t; |
| 714 | op_end = ts_node_end_byte(c); |
| 715 | } |
| 716 | if (ts_node_is_null(name)) { |
| 717 | return NULL; |
| 718 | } |
| 719 | const char *nm = NULL; |
| 720 | int index = SH_NO_DEPTH; |
| 721 | if (sh_type(name, "subscript")) { |
| 722 | TSNode base = ts_node_child_by_field_name(name, TS_FIELD("name")); |
| 723 | TSNode idx = ts_node_child_by_field_name(name, TS_FIELD("index")); |
| 724 | if (ts_node_is_null(base) || ts_node_is_null(idx)) { |
| 725 | return NULL; |
| 726 | } |
| 727 | const char *it = sh_text(s, idx); |
| 728 | if (!it || strcmp(it, "0") != 0) { |
| 729 | return NULL; |
| 730 | } |
| 731 | nm = sh_text(s, base); |
| 732 | index = 0; |
| 733 | } else if (sh_type(name, "variable_name") || sh_type(name, "special_variable_name")) { |
| 734 | nm = sh_text(s, name); |
| 735 | } else { |
| 736 | return NULL; |
no test coverage detected