Skip to content

Commit

Permalink
fix: arrays in expansion concats, _word_in_replacement should be _exp…
Browse files Browse the repository at this point in the history
…ansion_word
  • Loading branch information
amaanq committed Aug 29, 2023
1 parent d35dd56 commit e225c58
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
24 changes: 6 additions & 18 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module.exports = grammar({
$.regex,
$._regex_no_slash,
$._regex_no_space,
$._word_in_replacement,
$._expansion_word,
$.extglob_pattern,
$._bare_dollar,
$._brace_start,
Expand Down Expand Up @@ -618,6 +618,7 @@ module.exports = grammar({
$.expansion,
$._simple_variable_name,
$.variable_name,
$.string,
)),

_arithmetic_binary_expression: $ => prec.left(2, choice(
Expand Down Expand Up @@ -806,7 +807,6 @@ module.exports = grammar({
$.raw_string,
$.ansi_c_string,
alias($._expansion_word, $.word),
alias($._word_in_replacement, $.word),
),
)),
)),
Expand All @@ -827,9 +827,9 @@ module.exports = grammar({
$._literal,
seq(
$.command_substitution,
alias($._word_in_replacement, $.word),
alias($._expansion_word, $.word),
),
alias($._word_in_replacement, $.word),
alias($._expansion_word, $.word),
),
field('operator', optional('/')),
)),
Expand Down Expand Up @@ -879,7 +879,7 @@ module.exports = grammar({
$.raw_string,
$.command_substitution,
alias($._expansion_word, $.word),
alias($._word_in_replacement, $.word),
$.array,
),
repeat1(seq(
choice($._concat, alias(/`\s*`/, '``')),
Expand All @@ -892,7 +892,7 @@ module.exports = grammar({
$.raw_string,
$.command_substitution,
alias($._expansion_word, $.word),
alias($._word_in_replacement, $.word),
$.array,
),
)),
)),
Expand Down Expand Up @@ -921,18 +921,6 @@ module.exports = grammar({

comment: _ => token(prec(-10, /#.*/)),

_expansion_word: _ => token(prec(-9, seq(
choice(
noneOf(...['$', '"', '{', '}', '(', ')', '\'', '\\s']),
seq('\\', noneOf('\\s')),
),
repeat(choice(
noneOf(...['$', '"', '{', '}', '(', ')', '\'', '\\s']),
seq('\\', noneOf('\\s')),
'\\ ',
)),
))),

_comment_word: _ => token(prec(-8, seq(
choice(
noneOf(...SPECIAL_CHARACTERS),
Expand Down
17 changes: 9 additions & 8 deletions src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ enum TokenType {
REGEX,
REGEX_NO_SLASH,
REGEX_NO_SPACE,
WORD_IN_REPLACEMENT,
EXPANSION_WORD,
EXTGLOB_PATTERN,
BARE_DOLLAR,
BRACE_START,
Expand Down Expand Up @@ -89,6 +89,7 @@ static inline void advance(TSLexer *lexer) { lexer->advance(lexer, false); }

static inline void skip(TSLexer *lexer) { lexer->advance(lexer, true); }


static inline bool in_error_recovery(const bool *valid_symbols) {
return valid_symbols[HEREDOC_START] && valid_symbols[HEREDOC_END] &&
valid_symbols[FILE_DESCRIPTOR] && valid_symbols[EMPTY_VALUE] &&
Expand Down Expand Up @@ -338,7 +339,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
}
}
if (iswspace(lexer->lookahead) && valid_symbols[CLOSING_BRACE] &&
!valid_symbols[WORD_IN_REPLACEMENT]) {
!valid_symbols[EXPANSION_WORD]) {
lexer->result_symbol = CONCAT;
return true;
}
Expand Down Expand Up @@ -476,9 +477,9 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
lexer->mark_end(lexer);
advance(lexer);
if (lexer->lookahead == '}' && valid_symbols[CLOSING_BRACE]) {
if (valid_symbols[WORD_IN_REPLACEMENT]) {
if (valid_symbols[EXPANSION_WORD]) {
lexer->mark_end(lexer);
lexer->result_symbol = WORD_IN_REPLACEMENT;
lexer->result_symbol = EXPANSION_WORD;
return true;
}
return false;
Expand All @@ -496,7 +497,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
if ((lexer->lookahead == ' ' || lexer->lookahead == '\t' ||
lexer->lookahead == '\r' ||
(lexer->lookahead == '\n' && !valid_symbols[NEWLINE])) &&
!valid_symbols[WORD_IN_REPLACEMENT]) {
!valid_symbols[EXPANSION_WORD]) {
skip(lexer);
} else if (lexer->lookahead == '\\') {
skip(lexer);
Expand Down Expand Up @@ -561,7 +562,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
if (lexer->lookahead == '{') {
goto brace_start;
}
if (valid_symbols[WORD_IN_REPLACEMENT]) {
if (valid_symbols[EXPANSION_WORD]) {
goto word_in_replacement;
}
if (valid_symbols[EXTGLOB_PATTERN]) {
Expand Down Expand Up @@ -882,7 +883,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
}

word_in_replacement:
if (valid_symbols[WORD_IN_REPLACEMENT]) {
if (valid_symbols[EXPANSION_WORD]) {
bool advanced_once = false;
for (;;) {
if (lexer->lookahead == '\"') {
Expand All @@ -900,7 +901,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {

if (lexer->lookahead == '}') {
lexer->mark_end(lexer);
lexer->result_symbol = WORD_IN_REPLACEMENT;
lexer->result_symbol = EXPANSION_WORD;
return advanced_once;
}

Expand Down
22 changes: 21 additions & 1 deletion test/corpus/literals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,8 @@ echo $((bar=1))
echo $((-1, 1))
echo $((! -a || ~ +b || ++c || --d))
echo $((foo-- || bar++))
(("${MULTIBUILD_VARIANTS}" > 1))
$(("$(stat --printf '%05a' "${save_file}")" & 07177))

--------------------------------------------------------------------------------

Expand Down Expand Up @@ -1211,7 +1213,25 @@ echo $((foo-- || bar++))
(binary_expression
(postfix_expression
(variable_name))
(variable_name))))))
(variable_name)))))
(command
(command_name
(arithmetic_expansion
(binary_expression
(string (expansion (variable_name)))
(number)))))
(command
(command_name
(arithmetic_expansion
(binary_expression
(string
(command_substitution
(command
(command_name (word))
(word)
(raw_string)
(string (expansion (variable_name))))))
(number))))))

================================================================================
Concatenation with double backticks
Expand Down
4 changes: 0 additions & 4 deletions test/corpus/statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,6 @@ fi
[[ -f "${EROOT}/usr/share/php/.packagexml/${MY_P}.xml" && \
-x "${EROOT}/usr/bin/peardev" ]]

(("${MULTIBUILD_VARIANTS}" > 1))

[[ ${test} == @($(IFS='|'; echo "${skip[*]}")) ]]

[[ ${SRC_URI} == */${a}* ]]
Expand Down Expand Up @@ -1007,8 +1005,6 @@ fi
(unary_expression
(test_operator)
(string (expansion (variable_name)))))))
(test_command
(binary_expression (string (expansion (variable_name))) (number)))
(test_command
(binary_expression
(expansion (variable_name))
Expand Down

0 comments on commit e225c58

Please sign in to comment.