Repository navigation
fix: allow a trailing separator in table constructors like Lua 5.4 - #34
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
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.
问题
Lua 5.4 允许表构造里有一个尾随分隔符(
{1, 2, 3,}/{1; 2; 3;}),fakelua 的fieldlist规则缺少这个[fieldsep]可选项,导致:原因
不是归约冲突导致的。实测加这个产生式前后 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,所以这两个文件必须一起提交。验证
{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 也拒绝这三处,所以没有顺手放宽)test_redis.integration_*需要真实 Redis + 1 个偶发的test_net.test_websocket_echo_lua),零回归。新增测试
test/lua/syntax/test_constructor_trailing_sep.lua:4 种尾随形态 fixturesyntax_tree.constructor_trailing_separator:AST dump 断言,确保尾随分隔符不产生多余 fieldsyntax_tree.constructor_repeated_separator_rejected:{1,,2}等必须仍报错syntax_tree.trailing_separator_outside_constructor_rejected:调用实参 / 形参表 / return 列表仍拒绝