Balanced annotation arguments/types, with strings, chars, quoted identifiers, nested/NUL comments and interpolation. An explicit stack keeps lookahead nesting independent of the C call stack and the scanner's serialized strings.
| 723 | // nested/NUL comments and interpolation. An explicit stack keeps lookahead |
| 724 | // nesting independent of the C call stack and the scanner's serialized strings. |
| 725 | static bool constructor_balanced(TSLexer *lexer, char close) { |
| 726 | ConstructorProbeStack stack; |
| 727 | array_init(&stack); |
| 728 | array_push(&stack, ((ConstructorProbeFrame){close, 0, 0})); |
| 729 | advance(lexer); |
| 730 | bool ok = false; |
| 731 | while (stack.size && !lexer->eof(lexer)) { |
| 732 | ConstructorProbeFrame frame = *array_back(&stack); |
| 733 | int32_t c = lexer->lookahead; |
| 734 | if (frame.kind) { |
| 735 | if (frame.kind == 1 && c == '\\') { advance(lexer); if (!lexer->eof(lexer)) advance(lexer); continue; } |
| 736 | if (c == frame.end) { |
| 737 | advance(lexer); |
| 738 | if (frame.kind != 2) { (void)array_pop(&stack); continue; } |
| 739 | if (lexer->lookahead == '"') { |
| 740 | advance(lexer); |
| 741 | if (lexer->lookahead == '"') { advance(lexer); (void)array_pop(&stack); } |
| 742 | } |
| 743 | continue; |
| 744 | } |
| 745 | if (frame.end == '"' && c == '$') { |
| 746 | unsigned run = 0; |
| 747 | while (lexer->lookahead == '$') { advance(lexer); run++; } |
| 748 | if (run >= frame.dollars && lexer->lookahead == '{') { |
| 749 | advance(lexer); |
| 750 | array_push(&stack, ((ConstructorProbeFrame){'}', 0, 0})); |
| 751 | } |
| 752 | continue; |
| 753 | } |
| 754 | advance(lexer); continue; |
| 755 | } |
| 756 | if (c == frame.end) { advance(lexer); (void)array_pop(&stack); continue; } |
| 757 | if (c == '(' || c == '[' || c == '{' || (c == '<' && frame.end == '>')) { |
| 758 | array_push(&stack, ((ConstructorProbeFrame){c == '(' ? ')' : c == '[' ? ']' : c == '{' ? '}' : '>', 0, 0})); |
| 759 | advance(lexer); continue; |
| 760 | } |
| 761 | if (c == '/') { bool newline = false; if (!constructor_trivia(lexer, &newline)) continue; continue; } |
| 762 | unsigned dollars = 1; |
| 763 | if (c == '$') { |
| 764 | dollars = 0; |
| 765 | while (lexer->lookahead == '$') { advance(lexer); dollars++; } |
| 766 | c = lexer->lookahead; |
| 767 | } |
| 768 | if (c == '"' || c == '\'' || c == '`') { |
| 769 | char kind = c == '`' ? 3 : 1; |
| 770 | advance(lexer); |
| 771 | if (c == '"' && lexer->lookahead == '"') { |
| 772 | advance(lexer); |
| 773 | if (lexer->lookahead == '"') { advance(lexer); kind = 2; } |
| 774 | else continue; // Empty ordinary string. |
| 775 | } |
| 776 | array_push(&stack, ((ConstructorProbeFrame){(char)c, kind, dollars})); |
| 777 | continue; |
| 778 | } |
| 779 | advance(lexer); |
| 780 | } |
| 781 | ok = stack.size == 0; |
| 782 | array_delete(&stack); |
no test coverage detected