From bdca36433c0ca86e4eab0672f3308dba7a76a871 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 18 Feb 2026 22:56:44 +0800 Subject: Use YueComment node for TableLit comment preservation --- src/yuescript/yue_ast.cpp | 4 ++-- src/yuescript/yue_ast.h | 5 ++--- src/yuescript/yue_compiler.cpp | 18 ++++++------------ src/yuescript/yue_parser.cpp | 4 ++-- 4 files changed, 12 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/yuescript/yue_ast.cpp b/src/yuescript/yue_ast.cpp index 8c19df8..4d62d05 100644 --- a/src/yuescript/yue_ast.cpp +++ b/src/yuescript/yue_ast.cpp @@ -1154,7 +1154,7 @@ std::string TableLit_t::to_string(void* ud) const { bool hasCommentOrEmpty = false; ast_node* lastValueNode = nullptr; for (auto value : values.objects()) { - if (ast_is(value)) { + if (ast_is(value)) { hasCommentOrEmpty = true; continue; } @@ -1173,7 +1173,7 @@ std::string TableLit_t::to_string(void* ud) const { continue; } auto valueStr = value->to_string(ud); - if (!ast_is(value) && value != lastValueNode) { + if (!ast_is(value) && value != lastValueNode) { valueStr += ','; } temp.emplace_back(info->ind() + valueStr); diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index e86d943..9d6d26e 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -87,8 +87,7 @@ class NormalDef_t; class SpreadListExp_t; class Comprehension_t; class Value_t; -class YueLineComment_t; -class YueMultilineComment_t; +class YueComment_t; class EmptyLine_t; } // namespace yue @@ -721,7 +720,7 @@ AST_NODE(TableLit) MetaVariablePairDef_t, MetaNormalPairDef_t, VariablePair_t, NormalPair_t, Exp_t, MetaVariablePair_t, MetaNormalPair_t, - YueLineComment_t, YueMultilineComment_t, EmptyLine_t, + YueComment_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 ff06625..34a03b3 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -8207,14 +8207,14 @@ private: if (!_config.reserveComment) { for (auto it = values.rbegin(); it != values.rend(); ++it) { auto node = *it; - if (!ast_is(node)) { + if (!ast_is(node)) { lastValueNode = node; break; } } } for (auto value : values) { - if (!_config.reserveComment && ast_is(value)) { + if (!_config.reserveComment && ast_is(value)) { continue; } auto item = value; @@ -8264,15 +8264,9 @@ private: 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); + case id(): { + auto comment = static_cast(item); + temp.emplace_back(comment->to_string(&_config)); skipComma = true; break; } @@ -8337,7 +8331,7 @@ private: default: YUEE("AST node mismatch", item); break; } if (!isMetamethod) { - if (skipComma || temp.back().rfind("--"sv, 0) == 0) { + if (skipComma) { temp.back() = indent() + temp.back() + nl(value); } else { temp.back() = indent() + (value == lastValueNode ? temp.back() : temp.back() + ',') + nl(value); diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index ff8b575..746ea29 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -890,7 +890,7 @@ YueParser::YueParser() { 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) + push_indent_match >> (plain_space >> not_(line_break | '}') >> (table_value | YueComment | expected_expression_error) >> *(plain_space >> ',' >> plain_space >> (table_value | YueComment)) >> pop_indent | pop_indent) ) | ( space ); @@ -899,7 +899,7 @@ YueParser::YueParser() { TableLit = '{' >> Seperator >> - -(plain_space >> (table_value | yue_line_comment | yue_multiline_comment) >> *(plain_space >> ',' >> plain_space >> (table_value | yue_line_comment | yue_multiline_comment)) >> -(plain_space >> ',')) >> + -(plain_space >> (table_value | YueComment) >> *(plain_space >> ',' >> plain_space >> (table_value | YueComment)) >> -(plain_space >> ',')) >> ( table_lit_lines >> white >> end_braces_expression | white >> '}' -- cgit v1.2.3-55-g6feb