From f6aacc59c99483ee8bcc35209d3750b74ab84e9d Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Mon, 28 Aug 2023 23:22:04 -0400 Subject: [PATCH] feat: rework bare dollar --- src/scanner.c | 43 ++++++++++++++++++++++++++++------------ test/corpus/commands.txt | 6 ++++-- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/scanner.c b/src/scanner.c index 18b04b9..1925f72 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -166,6 +166,22 @@ static bool advance_word(TSLexer *lexer, String *unquoted_word) { return !empty; } +static inline bool scan_bare_dollar(TSLexer *lexer) { + while (iswspace(lexer->lookahead) && lexer->lookahead != '\n' && + !lexer->eof(lexer)) { + skip(lexer); + } + + if (lexer->lookahead == '$') { + advance(lexer); + lexer->result_symbol = BARE_DOLLAR; + lexer->mark_end(lexer); + return iswspace(lexer->lookahead) || lexer->eof(lexer); + } + + return false; +} + static bool scan_heredoc_start(Scanner *scanner, TSLexer *lexer) { while (iswspace(lexer->lookahead)) { skip(lexer); @@ -386,17 +402,6 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) { } } - if (valid_symbols[BARE_DOLLAR] && !in_error_recovery(valid_symbols)) { - while (iswspace(lexer->lookahead)) { - skip(lexer); - } - - if (lexer->lookahead == '$') { - advance(lexer); - lexer->result_symbol = BARE_DOLLAR; - return iswspace(lexer->lookahead) || lexer->eof(lexer); - } - } if (valid_symbols[EMPTY_VALUE]) { if (iswspace(lexer->lookahead) || lexer->eof(lexer) || @@ -488,6 +493,11 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) { return true; } } + + if (valid_symbols[BARE_DOLLAR] && !in_error_recovery(valid_symbols) && + scan_bare_dollar(lexer)) { + return true; + } } if ((valid_symbols[VARIABLE_NAME] || valid_symbols[FILE_DESCRIPTOR] || @@ -623,6 +633,11 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) { return false; } + if (valid_symbols[BARE_DOLLAR] && !in_error_recovery(valid_symbols) && + scan_bare_dollar(lexer)) { + return true; + } + if ((valid_symbols[REGEX] || valid_symbols[REGEX_NO_SLASH] || valid_symbols[REGEX_NO_SPACE]) && !in_error_recovery(valid_symbols)) { @@ -727,11 +742,13 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) { } else if (lexer->lookahead == '$') { lexer->mark_end(lexer); advance(lexer); - // do not parse a command substitution + // do not parse a command + // substitution if (lexer->lookahead == '(') { return false; } - // end $ always means regex, e.g. 99999999$ + // end $ always means regex, e.g. + // 99999999$ if (iswspace(lexer->lookahead)) { lexer->result_symbol = REGEX_NO_SPACE; lexer->mark_end(lexer); diff --git a/test/corpus/commands.txt b/test/corpus/commands.txt index e24715f..4e906c8 100644 --- a/test/corpus/commands.txt +++ b/test/corpus/commands.txt @@ -583,13 +583,15 @@ Bare $ ========================================== echo $ +echo "${module}"$ --- (program + (command (command_name (word))) (command - (command_name - (word)))) + (command_name (word)) + (concatenation (string (expansion (variable_name)))))) ==========================================