Repository navigation
fix(lexer): 函数定义/花括号组同行 # 尾注释解析失败(#118) - #119
Merged
Merged
Conversation
A function definition or bare brace group sharing its line with a `#`
comment failed to parse:
f() { # note
echo x
}
f
# bash: -c: line 1: syntax error near unexpected token `('
and the one-line forms (`f() { echo x; } # note', `{ echo x; } # note')
fused the comment into the brace token.
Two lexer faults combined:
1. `has_unclosed_brace_group' does not recognise `f() {' / `function f {'
as opening a group, so the logical line was not continued. The first
line reached the scanner alone, `skip_brace' found no matching `}' and
the slice to end of line became the token `{ # note'. The parser only
accepts a function-body token whose trimmed value is exactly `{', so
it returned None and fell through to the unexpected-token diagnostic.
2. `brace_close_can_end_compact_group' did not treat a `#' after the
closing brace as ending the group, so `{ echo x; } # note' kept
scanning for a later `}' and swallowed the comment text.
`skip_brace' now reports whether the matching `}' was consumed and, when
not, the offset of the first word-initial `#' comment seen at the group's
top level. The scanner emits only the group text before that comment and
resumes from the comment, which the dispatcher drops like any other
comment; the parser then assembles the body from the following lines --
the path multiline definitions already take.
`brace_close_can_end_compact_group' also returns true on an inline `#'
after the brace, the way it already does for `;', `|' and newline.
`comment_start' started as true inside `skip_brace', but the scan begins
just past the opening brace, which is itself a word start; `{#note' is one
word in GNU, so that form has to stay a syntax error instead of quietly
becoming a comment.
Verified against GNU bash 5.3.15 with 27 differential cases (15 + 12):
0 mismatches after the change, 5 before. `cargo test --lib' 398 passed;
`cargo test --test cli_tests' failure count unchanged from the master
baseline; `tests/run_all_tests.sh' 6/6. Pinned by
tests/issue120_fndef_inline_comment.rs and tests/lexer_comment_tests.rs.
Refs unixwin/niubash#120
This was referenced Sep 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #118(来源:#120 的后续复验,@Pius-x)。
改动
skip_brace()现在返回BraceScan { closed, comment_start }:closed—— 是否吃到了配对的};comment_start—— 组内顶层出现的首个词首#注释的字节偏移。scanner 在"组未闭合 + 遇到顶层注释"时,只把注释之前的文本作为 token,并把读取位置回退到注释处,由 dispatcher 按普通注释丢弃。于是
f() { # note得到干净的{,parser 走f() {\n…\n}已有的matching_brace_group_end多 token 拼装路径。另外两处:
brace_close_can_end_compact_group在}后遇行内注释起始#时判定组结束(#起注释,本节剩余部分不属于组)。修掉{ echo x; } # note的 token 融合。skip_brace的comment_start初值由true改为false。它从{之后开始扫描,而{本身是词首,所以{#note的#处于词中,GNU 里不构成注释({#note是一个词,整个定义应报语法错误)。不改这里的话,修完上一处会把f() {#note从"报错"变成"静默接受"。影响面
f() { # note⏎echo x⏎}xxf() { echo x; } # notexxf() { echo x; } #note/ TAB /#xxfunction f { # note⏎ …xxfunction f { echo x; } # notexcommand not foundx{ echo x; } # notexcommand not foundxf() { echo x; }; # note(对照)xxx}独占一行 + 注释(对照)xxxf() {#note({/#粘连,对照)echo {a/echo { a/echo {(对照)未改动
has_unclosed_brace_group的has_group判定 —— 若把f() {也纳入,会让所有多行函数定义的 token 形态从"多 token 拼装"变成"单 token 花括号组",波及body_start/body_end、declare -f还原与诊断行号,风险远大于收益。验证
cargo test --lib:398 passed / 0 failed(CI 严格门);tests/run_all_tests.sh:6/6(CI 第二道门);cargo test --test cli_tests:349 passed / 68 failed,与本机 master 基线逐项一致(Windows 下$0为完整路径、缺 bashdb fixture 导致的既有失败;CI 该 job 为continue-on-error);tests/issue120_fndef_inline_comment.rs(13 项行为级)、tests/lexer_comment_tests.rs+4 项 token 级。