Skip to content

Commit

Permalink
feat: rework bare dollar
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Aug 29, 2023
1 parent da06a31 commit f6aacc5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
43 changes: 30 additions & 13 deletions src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) ||
Expand Down Expand Up @@ -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] ||
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions test/corpus/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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))))))


==========================================
Expand Down

0 comments on commit f6aacc5

Please sign in to comment.