From 208d8f290cd9ed7be0ac8b15f2160e72536d4362 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 18 Feb 2026 21:07:33 +0800 Subject: Add TableLit AST support for empty lines and multiline comments --- src/yuescript/yue_ast.h | 5 ++++- src/yuescript/yue_compiler.cpp | 14 +++++++++++++- src/yuescript/yue_parser.cpp | 13 +++++++------ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 8a5bbe8..e86d943 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -88,6 +88,8 @@ class SpreadListExp_t; class Comprehension_t; class Value_t; class YueLineComment_t; +class YueMultilineComment_t; +class EmptyLine_t; } // namespace yue AST_LEAF(Num) @@ -718,7 +720,8 @@ AST_NODE(TableLit) VariablePairDef_t, NormalPairDef_t, SpreadExp_t, NormalDef_t, MetaVariablePairDef_t, MetaNormalPairDef_t, VariablePair_t, NormalPair_t, Exp_t, - MetaVariablePair_t, MetaNormalPair_t, YueLineComment_t, + MetaVariablePair_t, MetaNormalPair_t, + YueLineComment_t, YueMultilineComment_t, EmptyLine_t, /*non-syntax-rule*/ TableBlockIndent_t, SpreadListExp_t> values; AST_MEMBER(TableLit, &sep, &values) AST_END(TableLit) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 8da6d2f..9226e89 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -8248,13 +8248,25 @@ private: } } bool isMetamethod = false; + bool skipComma = false; switch (item->get_id()) { case id(): transformExp(static_cast(item), temp, ExpUsage::Closure); break; case id(): { auto comment = static_cast(item); temp.emplace_back("--"s + _parser.toString(comment)); + skipComma = true; break; } + case id(): { + auto comment = static_cast(item); + temp.emplace_back("--[["s + _parser.toString(comment) + "]]"s); + skipComma = true; + break; + } + case id(): + temp.emplace_back(Empty); + skipComma = true; + break; case id(): transform_variable_pair(static_cast(item), temp); break; case id(): transform_normal_pair(static_cast(item), temp, false); break; case id(): transformTableBlockIndent(static_cast(item), temp); break; @@ -8312,7 +8324,7 @@ private: default: YUEE("AST node mismatch", item); break; } if (!isMetamethod) { - if (ast_is(value)) { + if (skipComma || temp.back().rfind("--"sv, 0) == 0) { temp.back() = indent() + temp.back() + nl(value); } else { temp.back() = indent() + (value == values.back() ? temp.back() : temp.back() + ',') + nl(value); diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 6a2237c..ff8b575 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -888,17 +888,18 @@ YueParser::YueParser() { SpreadExp | NormalDef; - table_lit_line = ( - push_indent_match >> (plain_space >> not_(line_break | '}') >> (table_value | yue_line_comment | expected_expression_error) >> *(plain_space >> ',' >> plain_space >> (table_value | yue_line_comment)) >> pop_indent | pop_indent) - ) | ( - space - ); + table_lit_line = + -EmptyLine >> ( + push_indent_match >> (plain_space >> not_(line_break | '}') >> (table_value | yue_line_comment | yue_multiline_comment | expected_expression_error) >> *(plain_space >> ',' >> plain_space >> (table_value | yue_line_comment | yue_multiline_comment)) >> pop_indent | pop_indent) + ) | ( + space + ); table_lit_lines = space_break >> table_lit_line >> *(-(space >> ',') >> space_break >> table_lit_line) >> -(space >> ','); TableLit = '{' >> Seperator >> - -(plain_space >> (table_value | yue_line_comment) >> *(plain_space >> ',' >> plain_space >> (table_value | yue_line_comment)) >> -(plain_space >> ',')) >> + -(plain_space >> (table_value | yue_line_comment | yue_multiline_comment) >> *(plain_space >> ',' >> plain_space >> (table_value | yue_line_comment | yue_multiline_comment)) >> -(plain_space >> ',')) >> ( table_lit_lines >> white >> end_braces_expression | white >> '}' -- cgit v1.2.3-55-g6feb