Skip to content

Line continuation directly before # starts a comment that bash reads as part of the word #354

Open

Description

@OmerFarukOruc

A backslash-newline (line continuation) directly before # is parsed as a separator, so the # starts a comment node. Bash removes the backslash-newline pair before it splits words, so the # is inside a word and is not a comment.

Input (a backslash at the end of line 1):

echo x\
#literal

Bash 5.3.9 prints x#literal:

$ printf 'echo x\\\n#literal\n' | bash
x#literal

tree-sitter-bash 0.25.1 (web-tree-sitter 0.27.0) parses it as a command plus a comment, with no error:

(program (command name: (command_name (word)) argument: (word)) (comment))

The comment node's text is #literal.

Expected: no comment node. The argument should cover x\ and #literal as one word or concatenation, the same way #100 made echo a\ + newline + b one concatenation.

Reproducer:

import { Parser, Language } from "web-tree-sitter";
await Parser.init();
const parser = new Parser();
parser.setLanguage(await Language.load("node_modules/tree-sitter-bash/tree-sitter-bash.wasm"));
const tree = parser.parse("echo x\\\n#literal\n");
console.log(tree.rootNode.toString());
console.log(tree.rootNode.descendantsOfType("comment").map((n) => n.text));

Related: #68 (a # that does not start a word should not begin a comment) and #100 (a newline escaped without whitespace before the backslash). This case is the continuation form of #68. We hit it in a tool that uses the comment nodes to find comments in shell scripts.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions