aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-02-18 12:47:32 +0800
committerLi Jin <dragon-fly@qq.com>2026-02-18 12:47:32 +0800
commit9246c787b4ee652863f165b8826f019d3486d699 (patch)
tree082e19884ea641d610777c2c811a888a82f38fd3
parentcf5b1b4a68d762e6e33cac8367611ecea15fa942 (diff)
downloadyuescript-9246c787b4ee652863f165b8826f019d3486d699.tar.gz
yuescript-9246c787b4ee652863f165b8826f019d3486d699.tar.bz2
yuescript-9246c787b4ee652863f165b8826f019d3486d699.zip
Preserve TabLit single-line comments in AST
-rw-r--r--src/yuescript/yue_ast.h3
-rw-r--r--src/yuescript/yue_compiler.cpp11
-rw-r--r--src/yuescript/yue_parser.cpp4
3 files changed, 14 insertions, 4 deletions
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;
87class SpreadListExp_t; 87class SpreadListExp_t;
88class Comprehension_t; 88class Comprehension_t;
89class Value_t; 89class Value_t;
90class YueLineComment_t;
90} // namespace yue 91} // namespace yue
91 92
92AST_LEAF(Num) 93AST_LEAF(Num)
@@ -717,7 +718,7 @@ AST_NODE(TableLit)
717 VariablePairDef_t, NormalPairDef_t, SpreadExp_t, NormalDef_t, 718 VariablePairDef_t, NormalPairDef_t, SpreadExp_t, NormalDef_t,
718 MetaVariablePairDef_t, MetaNormalPairDef_t, 719 MetaVariablePairDef_t, MetaNormalPairDef_t,
719 VariablePair_t, NormalPair_t, Exp_t, 720 VariablePair_t, NormalPair_t, Exp_t,
720 MetaVariablePair_t, MetaNormalPair_t, 721 MetaVariablePair_t, MetaNormalPair_t, YueLineComment_t,
721 /*non-syntax-rule*/ TableBlockIndent_t, SpreadListExp_t> values; 722 /*non-syntax-rule*/ TableBlockIndent_t, SpreadListExp_t> values;
722 AST_MEMBER(TableLit, &sep, &values) 723 AST_MEMBER(TableLit, &sep, &values)
723AST_END(TableLit) 724AST_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:
8250 bool isMetamethod = false; 8250 bool isMetamethod = false;
8251 switch (item->get_id()) { 8251 switch (item->get_id()) {
8252 case id<Exp_t>(): transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); break; 8252 case id<Exp_t>(): transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); break;
8253 case id<YueLineComment_t>(): {
8254 auto comment = static_cast<YueLineComment_t*>(item);
8255 temp.emplace_back("--"s + _parser.toString(comment));
8256 break;
8257 }
8253 case id<VariablePair_t>(): transform_variable_pair(static_cast<VariablePair_t*>(item), temp); break; 8258 case id<VariablePair_t>(): transform_variable_pair(static_cast<VariablePair_t*>(item), temp); break;
8254 case id<NormalPair_t>(): transform_normal_pair(static_cast<NormalPair_t*>(item), temp, false); break; 8259 case id<NormalPair_t>(): transform_normal_pair(static_cast<NormalPair_t*>(item), temp, false); break;
8255 case id<TableBlockIndent_t>(): transformTableBlockIndent(static_cast<TableBlockIndent_t*>(item), temp); break; 8260 case id<TableBlockIndent_t>(): transformTableBlockIndent(static_cast<TableBlockIndent_t*>(item), temp); break;
@@ -8307,7 +8312,11 @@ private:
8307 default: YUEE("AST node mismatch", item); break; 8312 default: YUEE("AST node mismatch", item); break;
8308 } 8313 }
8309 if (!isMetamethod) { 8314 if (!isMetamethod) {
8310 temp.back() = indent() + (value == values.back() ? temp.back() : temp.back() + ',') + nl(value); 8315 if (ast_is<YueLineComment_t>(value)) {
8316 temp.back() = indent() + temp.back() + nl(value);
8317 } else {
8318 temp.back() = indent() + (value == values.back() ? temp.back() : temp.back() + ',') + nl(value);
8319 }
8311 } 8320 }
8312 } 8321 }
8313 if (metatable->pairs.empty() && !metatableItem) { 8322 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() {
889 NormalDef; 889 NormalDef;
890 890
891 table_lit_line = ( 891 table_lit_line = (
892 push_indent_match >> (space >> not_(line_break | '}') >> (table_value | expected_expression_error) >> *(space >> ',' >> space >> table_value) >> pop_indent | pop_indent) 892 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)
893 ) | ( 893 ) | (
894 space 894 space
895 ); 895 );
@@ -898,7 +898,7 @@ YueParser::YueParser() {
898 898
899 TableLit = 899 TableLit =
900 '{' >> Seperator >> 900 '{' >> Seperator >>
901 -(space >> table_value >> *(space >> ',' >> space >> table_value) >> -(space >> ',')) >> 901 -(plain_space >> (table_value | yue_line_comment) >> *(plain_space >> ',' >> plain_space >> (table_value | yue_line_comment)) >> -(plain_space >> ',')) >>
902 ( 902 (
903 table_lit_lines >> white >> end_braces_expression | 903 table_lit_lines >> white >> end_braces_expression |
904 white >> '}' 904 white >> '}'