Skip to content

fix: allow a trailing separator in table constructors like Lua 5.4 - #34

Merged
esrrhs merged 1 commit into
masterfrom
fix/lua54-table-trailing-separator
Oct 16, 2026
Merged

esrrhs merged 1 commit into
masterfrom
fix/lua54-table-trailing-separator

Conversation

@esrrhs

@esrrhs esrrhs commented Oct 16, 2026

Copy link
Copy Markdown
Owner

问题

Lua 5.4 允许表构造里有一个尾随分隔符({1, 2, 3,} / {1; 2; 3;}),fakelua 的 fieldlist 规则缺少这个 [fieldsep] 可选项,导致:

$ ./flua t.lua
flua: fakelua error: t.lua:2.25: syntax error, unexpected }

原因

不是归约冲突导致的。实测加这个产生式前后 bison 冲突数完全一致(5 shift/reduce + 1 reduce/reduce,均在 state 13/25/40/163,都是仓库既有的)—— fieldlist 的 FOLLOW 集只有 RCURLY,而 RCURLY 不可能开启一个 field,LALR(1) 在此无歧义。真正原因只是这条产生式当初没写。

改动

新增 tableconstructor: LCURLY fieldlist fieldsep RCURLY。

⚠️ 尾随分隔符刻意放在 tableconstructor 上,而不是写成 fieldlist: fieldlist fieldsep。后者会踩 bison 默认归约(default reduction) 的坑:在 { fieldlist fieldsep . 状态遇到非法 lookahead(第二个 ,)时仍会归约,使 {1,,2}、{1,,,}、{1;,} 被静默接受(Lua 5.4 均拒绝)。这一点在 PR 里加了回归测试锁住。

parser.cpp / parser.h 用 bison 3.8.2 重新生成(与已提交文件的生成版本一致)。注意 src/CMakeLists.txt 是 aux_source_directory(compile/bison ...),直接编译已提交的 parser.cpp,构建时不会跑 bison,所以这两个文件必须一起提交。

验证

  • 用真实 Lua 5.4.9 作 oracle,同一段代码分别喂给两个引擎,23 个用例 × TCC(0)/GCC(1)/interp(2) 三后端全部一致。其中 15 个应当被接受(含 {a=1,}、{[1]=1,}、嵌套 {1, {2,3,}, }、多行写法、函数字段),8 个应当被拒绝:
    • {1,,2}、{,}、{,1}、{1;,}(重复/孤立分隔符)
    • f(1,2,)、function g(a,b,)、return 1,2,(尾随分隔符只在表构造内合法,Lua 5.4 也拒绝这三处,所以没有顺手放宽)
  • 全量单测:1591 ran / 1586 passed。5 个失败均为仓库已知基线(4 个 test_redis.integration_* 需要真实 Redis + 1 个偶发的 test_net.test_websocket_echo_lua),零回归。

新增测试

  • test/lua/syntax/test_constructor_trailing_sep.lua:4 种尾随形态 fixture
  • syntax_tree.constructor_trailing_separator:AST dump 断言,确保尾随分隔符不产生多余 field
  • syntax_tree.constructor_repeated_separator_rejected:{1,,2} 等必须仍报错
  • syntax_tree.trailing_separator_outside_constructor_rejected:调用实参 / 形参表 / return 列表仍拒绝

Lua 5.4 accepts a single trailing field separator in a table
constructor ({1, 2, 3,} / {1; 2; 3;}), but fakelua's fieldlist rule
had no [fieldsep] term, so those failed with
"syntax error, unexpected }".

Add `tableconstructor: LCURLY fieldlist fieldsep RCURLY`. The trailing
separator deliberately lives on the tableconstructor rule rather than
on `fieldlist: fieldlist fieldsep`: in the latter form bison's default
reduction fires on an invalid lookahead, which makes {1,,2}, {1,,,}
and {1;,} parse successfully even though Lua 5.4 rejects them.

The grammar conflict count is unchanged (5 shift/reduce +
1 reduce/reduce, states 13/25/40/163).

parser.cpp/parser.h are regenerated with bison 3.8.2, matching the
version that produced the previously committed files (the build
compiles the committed parser.cpp directly, it does not run bison).

Verified against real Lua 5.4.9 on the TCC/GCC/interp backends: 23
accept/reject cases all match, including the ones Lua rejects
({1,,2}, {,}, {,1}, f(1,2,), function g(a,b,), return 1,2,).

Full unit test suite: 1591 ran / 1586 passed; the 5 failures are the
known baseline (4 redis integration tests needing a real Redis, plus
the flaky test_net.test_websocket_echo_lua).
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.07%. Comparing base (4e5aef0) to head (572463b).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #34   +/-   ##
=======================================
  Coverage   88.07%   88.07%           
=======================================
  Files         124      124           
  Lines       25486    25486           
=======================================
  Hits        22446    22446           
  Misses       3040     3040           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@esrrhs
esrrhs merged commit e4582bd into master Oct 16, 2026
9 checks passed
@esrrhs
esrrhs deleted the fix/lua54-table-trailing-separator branch October 16, 2026 13:57
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.

2 participants