From 81d306ef60b145ccd118ac9809905eac7e1f7d15 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 19 Feb 2026 13:38:22 +0800 Subject: [PATCH 1/5] feat(parser): preserve TableBlock comment lines in AST --- src/yuescript/yue_ast.h | 5 +++-- src/yuescript/yue_compiler.cpp | 2 ++ src/yuescript/yue_parser.cpp | 16 +++++++++------- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 9d6d26e..d44815e 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -729,13 +729,14 @@ AST_NODE(TableBlockIndent) ast_ptr sep; ast_sel_list values; + MetaVariablePair_t, MetaNormalPair_t, + YueComment_t> values; AST_MEMBER(TableBlockIndent, &sep, &values) AST_END(TableBlockIndent) AST_NODE(TableBlock) ast_ptr sep; - ast_sel_list values; + ast_sel_list values; AST_MEMBER(TableBlock, &sep, &values) AST_END(TableBlock) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index d85c92b..9891e79 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -733,6 +733,8 @@ class YueCompilerImpl { auto pair = static_cast(item); return isLocal(variableToString(pair->name)); } + case id(): + return true; case id(): { auto pair = static_cast(item); if (auto str = pair->key.as()) { diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index e3dc598..a43cdd7 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -909,11 +909,11 @@ YueParser::YueParser() { white >> '}' ); - table_block_inner = Seperator >> key_value_line >> *(+space_break >> key_value_line); + table_block_inner = Seperator >> key_value_line >> *(+(plain_space >> line_break) >> key_value_line); TableBlock = +space_break >> advance_match >> ensure(table_block_inner, pop_indent); TableBlockIndent = ('*' | '-' >> space_one) >> Seperator >> disable_arg_table_block_rule( space >> key_value_list >> -(space >> ',') >> - -(+space_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(+space_break >> key_value_line), pop_indent))); + -(+(plain_space >> line_break) >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(+(plain_space >> line_break) >> key_value_line), pop_indent))); ClassMemberList = Seperator >> key_value >> *(space >> ',' >> space >> key_value); class_line = check_indent_match >> space >> (ClassMemberList | Statement) >> -(space >> ','); @@ -1017,11 +1017,13 @@ YueParser::YueParser() { MetaVariablePair | MetaNormalPair; key_value_list = key_value >> *(space >> ',' >> space >> key_value); - key_value_line = check_indent_match >> space >> ( - key_value_list >> -(space >> ',') | - TableBlockIndent | - ('*' | '-' >> space_one) >> space >> (SpreadExp | Exp | TableBlock) - ); + key_value_line = + YueComment | + check_indent_match >> space >> ( + key_value_list >> -(space >> ',') | + TableBlockIndent | + ('*' | '-' >> space_one) >> space >> (SpreadExp | Exp | TableBlock) + ); fn_arg_def_list = FnArgDef >> *(space >> ',' >> space >> FnArgDef); From 36d9c36b53f5ce2b389be2637d35fab0aa5d65f0 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 19 Feb 2026 14:34:12 +0800 Subject: [PATCH 2/5] Fix TableBlock indentation parsing after make gen --- src/yuescript/yue_parser.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index a43cdd7..e445945 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -909,11 +909,11 @@ YueParser::YueParser() { white >> '}' ); - table_block_inner = Seperator >> key_value_line >> *(+(plain_space >> line_break) >> key_value_line); + table_block_inner = Seperator >> key_value_line >> *(+space_break >> key_value_line); TableBlock = +space_break >> advance_match >> ensure(table_block_inner, pop_indent); TableBlockIndent = ('*' | '-' >> space_one) >> Seperator >> disable_arg_table_block_rule( space >> key_value_list >> -(space >> ',') >> - -(+(plain_space >> line_break) >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(+(plain_space >> line_break) >> key_value_line), pop_indent))); + -(+space_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(+space_break >> key_value_line), pop_indent))); ClassMemberList = Seperator >> key_value >> *(space >> ',' >> space >> key_value); class_line = check_indent_match >> space >> (ClassMemberList | Statement) >> -(space >> ','); From 6437b10cd35a55a5a223f98973209cb48f6cfb02 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 19 Feb 2026 14:55:56 +0800 Subject: [PATCH 3/5] Respect reserve comments in spread table/tableblock compilation --- src/yuescript/yue_compiler.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 9891e79..f67e921 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -7989,6 +7989,19 @@ class YueCompilerImpl { transformFor(forNode, temp); break; } + case id(): { + if (_config.reserveComment) { + auto comment = static_cast(item); + temp.push_back(indent() + comment->to_string(&_config) + nl(item)); + } + break; + } + case id(): { + if (_config.reserveComment) { + temp.push_back(nl(item)); + } + break; + } case id(): case id(): { if (auto pair = ast_cast(item)) { From 0ada9d56ae41d07860d61a5ee46bbe67d1424d6f Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 19 Feb 2026 15:49:30 +0800 Subject: [PATCH 4/5] Fix reserve comment parsing for table block comment lines --- src/yuescript/yue_parser.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index e445945..21f19a3 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -909,11 +909,12 @@ YueParser::YueParser() { white >> '}' ); - table_block_inner = Seperator >> key_value_line >> *(+space_break >> key_value_line); - TableBlock = +space_break >> advance_match >> ensure(table_block_inner, pop_indent); + table_block_inner = Seperator >> key_value_line >> *((+(plain_space >> line_break) >> key_value_line) | (+space_break >> not_(expr("--")) >> key_value_line)); + TableBlock = ((+(plain_space >> line_break)) | (+space_break >> not_(expr("--")))) >> advance_match >> ensure(table_block_inner, pop_indent); TableBlockIndent = ('*' | '-' >> space_one) >> Seperator >> disable_arg_table_block_rule( space >> key_value_list >> -(space >> ',') >> - -(+space_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(+space_break >> key_value_line), pop_indent))); + -((plain_space >> line_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *((+(plain_space >> line_break) >> key_value_line) | (+space_break >> not_(expr("--")) >> key_value_line)), pop_indent)) | + (+space_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(+space_break >> key_value_line), pop_indent)))); ClassMemberList = Seperator >> key_value >> *(space >> ',' >> space >> key_value); class_line = check_indent_match >> space >> (ClassMemberList | Statement) >> -(space >> ','); From 9ed05ef421bdc19f2f931544a24298e080a5ed30 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 19 Feb 2026 19:03:07 +0800 Subject: [PATCH 5/5] Preserve blank lines between table comments with reserve-comments --- src/yuescript/yue_ast.h | 4 ++-- src/yuescript/yue_compiler.cpp | 8 ++++++++ src/yuescript/yue_parser.cpp | 5 +++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index d44815e..3236b5a 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -730,13 +730,13 @@ AST_NODE(TableBlockIndent) ast_sel_list values; + YueComment_t, EmptyLine_t> values; AST_MEMBER(TableBlockIndent, &sep, &values) AST_END(TableBlockIndent) AST_NODE(TableBlock) ast_ptr sep; - ast_sel_list values; + ast_sel_list values; AST_MEMBER(TableBlock, &sep, &values) AST_END(TableBlock) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index f67e921..38e9fbb 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -734,6 +734,7 @@ class YueCompilerImpl { return isLocal(variableToString(pair->name)); } case id(): + case id(): return true; case id(): { auto pair = static_cast(item); @@ -8219,6 +8220,8 @@ class YueCompilerImpl { auto metatable = x->new_ptr(); ast_sel metatableItem; ast_node* lastValueNode = values.back(); + int lastValueLine = x->m_begin.m_line; + bool prevWasEmptyLine = false; if (!_config.reserveComment) { for (auto it = values.rbegin(); it != values.rend(); ++it) { auto node = *it; @@ -8280,6 +8283,9 @@ class YueCompilerImpl { switch (item->get_id()) { case id(): transformExp(static_cast(item), temp, ExpUsage::Closure); break; case id(): { + if (_config.reserveComment && !prevWasEmptyLine && value->m_begin.m_line > lastValueLine + 1) { + temp.emplace_back(Empty); + } auto comment = static_cast(item); temp.emplace_back(comment->to_string(&_config)); skipComma = true; @@ -8352,6 +8358,8 @@ class YueCompilerImpl { temp.back() = indent() + (value == lastValueNode ? temp.back() : temp.back() + ',') + nl(value); } } + lastValueLine = value->m_end.m_line; + prevWasEmptyLine = ast_is(value); } if (metatable->pairs.empty() && !metatableItem) { out.push_back('{' + nl(x) + join(temp)); diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 21f19a3..ffe3ad2 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -909,11 +909,11 @@ YueParser::YueParser() { white >> '}' ); - table_block_inner = Seperator >> key_value_line >> *((+(plain_space >> line_break) >> key_value_line) | (+space_break >> not_(expr("--")) >> key_value_line)); + table_block_inner = Seperator >> key_value_line >> *(((plain_space >> line_break) >> key_value_line) | (+space_break >> not_(expr("--")) >> key_value_line)); TableBlock = ((+(plain_space >> line_break)) | (+space_break >> not_(expr("--")))) >> advance_match >> ensure(table_block_inner, pop_indent); TableBlockIndent = ('*' | '-' >> space_one) >> Seperator >> disable_arg_table_block_rule( space >> key_value_list >> -(space >> ',') >> - -((plain_space >> line_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *((+(plain_space >> line_break) >> key_value_line) | (+space_break >> not_(expr("--")) >> key_value_line)), pop_indent)) | + -((plain_space >> line_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(((plain_space >> line_break) >> key_value_line) | (+space_break >> not_(expr("--")) >> key_value_line)), pop_indent)) | (+space_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(+space_break >> key_value_line), pop_indent)))); ClassMemberList = Seperator >> key_value >> *(space >> ',' >> space >> key_value); @@ -1019,6 +1019,7 @@ YueParser::YueParser() { MetaNormalPair; key_value_list = key_value >> *(space >> ',' >> space >> key_value); key_value_line = + (EmptyLine >> YueComment) | YueComment | check_indent_match >> space >> ( key_value_list >> -(space >> ',') |