diff --git a/corpus/literals.txt b/corpus/literals.txt index b64fbc91a..6f12dc712 100644 --- a/corpus/literals.txt +++ b/corpus/literals.txt @@ -685,3 +685,66 @@ nix build nixpkgs#hello -v # comment with space (word) (word)) (comment)) + +================================================================================ +Words containing # that are not comments +================================================================================ + +echo 'word'#not-comment # a legit comment +echo $(uname -a)#not-comment # a legit comment +echo `uname -a`#not-comment # a legit comment +echo $hey#not-comment # a legit comment +var=#not-comment # a legit comment +echo "'$var'" # -> '#not-comment' + +-------------------------------------------------------------------------------- + +(program + (command + (command_name + (word)) + (concatenation + (raw_string) + (word))) + (comment) + (command + (command_name + (word)) + (concatenation + (command_substitution + (command + (command_name + (word)) + (word))) + (word))) + (comment) + (command + (command_name + (word)) + (concatenation + (command_substitution + (command + (command_name + (word)) + (word))) + (word))) + (comment) + (command + (command_name + (word)) + (concatenation + (simple_expansion + (variable_name)) + (word))) + (comment) + (variable_assignment + (variable_name) + (word)) + (comment) + (command + (command_name + (word)) + (string + (simple_expansion + (variable_name)))) + (comment)) diff --git a/grammar.js b/grammar.js index 3ae925c22..1fad7283f 100644 --- a/grammar.js +++ b/grammar.js @@ -300,7 +300,7 @@ module.exports = grammar({ choice('=~', '=='), choice($._literal, $.regex) ) - ))) + ))), )), command_name: $ => $._literal, @@ -317,7 +317,8 @@ module.exports = grammar({ field('value', choice( $._literal, $.array, - $._empty_value + $._empty_value, + alias($._comment_word, $.word), )) ), @@ -334,6 +335,7 @@ module.exports = grammar({ field('descriptor', optional($.file_descriptor)), choice('<', '>', '>>', '&>', '&>>', '<&', '>&', '>|'), field('destination', $._literal) + field('destination', $._literal), )), heredoc_redirect: $ => seq( @@ -449,6 +451,7 @@ module.exports = grammar({ choice( $._primary_expression, $._special_character, + alias($._comment_word, $.word), ) ))), optional(seq($._concat, '$')) @@ -538,6 +541,17 @@ module.exports = grammar({ ), comment: $ => token(prec(-10, /#.*/)), + _comment_word: $ => token(prec(-9, seq( + choice( + noneOf(...SPECIAL_CHARACTERS), + seq('\\', noneOf('\\s')) + ), + repeat(choice( + noneOf(...SPECIAL_CHARACTERS), + seq('\\', noneOf('\\s')), + "\\ ", + )) + ))), _simple_variable_name: $ => alias(/\w+/, $.variable_name), diff --git a/src/scanner.c b/src/scanner.c index 539c53adc..5dad873e7 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -264,7 +264,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) { lexer->lookahead == '<' || lexer->lookahead == ')' || lexer->lookahead == '(' || lexer->lookahead == ';' || lexer->lookahead == '&' || lexer->lookahead == '|' || - lexer->lookahead == '`' || lexer->lookahead == '#' || + lexer->lookahead == '`' || (lexer->lookahead == '}' && valid_symbols[CLOSING_BRACE]) || (lexer->lookahead == ']' && valid_symbols[CLOSING_BRACKET]))) { lexer->result_symbol = CONCAT;