Skip to content

fix(lexer): 函数定义/花括号组同行 # 尾注释解析失败(#118) - #119

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/fndef-inline-comment
Sep 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/fndef-inline-comment

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

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 从"报错"变成"静默接受"。

影响面

形态 GNU 5.3.15 修复前 修复后
f() { # note ⏎ echo x ⏎ } x syntax error x
f() { echo x; } # note x syntax error x
f() { echo x; } #note / TAB / # x syntax error x
function f { # note ⏎ … x syntax error x
function f { echo x; } # note x command not found x
{ echo x; } # note x command not found x
f() { echo x; }; # note(对照) x x x
} 独占一行 + 注释(对照) x x x
f() {#note({/# 粘连,对照) 语法错误 语法错误 语法错误
echo {a / echo { a / echo {(对照) 各自原样 各自原样 各自原样

未改动 has_unclosed_brace_group 的 has_group 判定 —— 若把 f() { 也纳入,会让所有多行函数定义的 token 形态从"多 token 拼装"变成"单 token 花括号组",波及 body_start/body_end、declare -f 还原与诊断行号,风险远大于收益。

验证

  • 27 个差分 case(15 + 12)对照 Git Bash GNU bash 5.3.15:修复前 5 处 mismatch → 修复后 0;
  • 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 级。

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] 函数定义/花括号组同行带 # 尾注释解析失败(f() { # note、{ echo x; } # note)

1 participant