From 9246c787b4ee652863f165b8826f019d3486d699 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 18 Feb 2026 12:47:32 +0800 Subject: Preserve TabLit single-line comments in AST --- src/yuescript/yue_ast.h | 3 ++- src/yuescript/yue_compiler.cpp | 11 ++++++++++- src/yuescript/yue_parser.cpp | 4 ++-- 3 files changed, 14 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 91800b0..8a5bbe8 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -87,6 +87,7 @@ class NormalDef_t; class SpreadListExp_t; class Comprehension_t; class Value_t; +class YueLineComment_t; } // namespace yue AST_LEAF(Num) @@ -717,7 +718,7 @@ 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, + MetaVariablePair_t, MetaNormalPair_t, YueLineComment_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 2615a2b..8da6d2f 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -8250,6 +8250,11 @@ private: bool isMetamethod = 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)); + 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; @@ -8307,7 +8312,11 @@ private: default: YUEE("AST node mismatch", item); break; } if (!isMetamethod) { - temp.back() = indent() + (value == values.back() ? temp.back() : temp.back() + ',') + nl(value); + if (ast_is(value)) { + temp.back() = indent() + temp.back() + nl(value); + } else { + temp.back() = indent() + (value == values.back() ? temp.back() : temp.back() + ',') + nl(value); + } } } if (metatable->pairs.empty() && !metatableItem) { diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index b52a452..6a2237c 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -889,7 +889,7 @@ YueParser::YueParser() { NormalDef; table_lit_line = ( - push_indent_match >> (space >> not_(line_break | '}') >> (table_value | expected_expression_error) >> *(space >> ',' >> space >> table_value) >> pop_indent | pop_indent) + 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 ); @@ -898,7 +898,7 @@ YueParser::YueParser() { TableLit = '{' >> Seperator >> - -(space >> table_value >> *(space >> ',' >> space >> table_value) >> -(space >> ',')) >> + -(plain_space >> (table_value | yue_line_comment) >> *(plain_space >> ',' >> plain_space >> (table_value | yue_line_comment)) >> -(plain_space >> ',')) >> ( table_lit_lines >> white >> end_braces_expression | white >> '}' -- cgit v1.2.3-55-g6feb