Handle words containing bare '#' (#109)

* Handle words containing bare '#'

Only a word beginning with a '#' starts a comment. A word can contain
'#' character without escaping as long as it is not the first character.

See: Bash Reference Manual section '3.1.3 Comments'
https://www.gnu.org/software/bash/manual/bash.html#Comments

* Regenerate
This commit is contained in:
oxalica 2021-11-02 00:59:27 +08:00 committed by GitHub
parent 7fb8506cbe
commit 4094e3a040
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 998 additions and 875 deletions

View File

@ -355,3 +355,18 @@ echo -ne "\033k$1\033\\" > /dev/stderr
(redirected_statement
(command (command_name (word)) (word) (string (simple_expansion (variable_name))))
(file_redirect (word))))
================================================================================
Words containing bare '#'
================================================================================
curl -# localhost #comment without space
nix build nixpkgs#hello -v # comment with space
--------------------------------------------------------------------------------
(program
(command (command_name (word)) (word) (word))
(comment)
(command (command_name (word)) (word) (word) (word))
(comment))

View File

@ -8,7 +8,6 @@ const SPECIAL_CHARACTERS = [
'|', '&', ';',
'\\',
'\\s',
'#',
];
module.exports = grammar({
@ -537,10 +536,16 @@ module.exports = grammar({
_special_variable_name: $ => alias(choice('*', '@', '?', '-', '$', '0', '_'), $.special_variable_name),
word: $ => token(repeat1(choice(
noneOf(...SPECIAL_CHARACTERS),
seq('\\', noneOf('\\s'))
))),
word: $ => token(seq(
choice(
noneOf('#', ...SPECIAL_CHARACTERS),
seq('\\', noneOf('\\s'))
),
repeat(choice(
noneOf(...SPECIAL_CHARACTERS),
seq('\\', noneOf('\\s'))
))
)),
test_operator: $ => token(prec(1, seq('-', /[a-zA-Z]+/))),

59
src/grammar.json vendored
View File

@ -2470,29 +2470,56 @@
"word": {
"type": "TOKEN",
"content": {
"type": "REPEAT1",
"content": {
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[^'\"<>{}\\[\\]()`$|&;\\\\\\s#]"
},
{
"type": "SEQ",
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[^#'\"<>{}\\[\\]()`$|&;\\\\\\s]"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\\"
},
{
"type": "PATTERN",
"value": "[^\\s]"
}
]
}
]
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "\\"
"type": "PATTERN",
"value": "[^'\"<>{}\\[\\]()`$|&;\\\\\\s]"
},
{
"type": "PATTERN",
"value": "[^\\s]"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\\"
},
{
"type": "PATTERN",
"value": "[^\\s]"
}
]
}
]
}
]
}
}
]
}
},
"test_operator": {

1784
src/parser.c vendored

File diff suppressed because it is too large Load Diff