diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-02-18 22:56:44 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-02-18 22:56:44 +0800 |
| commit | bdca36433c0ca86e4eab0672f3308dba7a76a871 (patch) | |
| tree | 7c82dab7de5a1f5e5e987346729979149866ec8c /src | |
| parent | af0047eb9b21921013fe7b3b578851ec963e24cc (diff) | |
| download | yuescript-bdca36433c0ca86e4eab0672f3308dba7a76a871.tar.gz yuescript-bdca36433c0ca86e4eab0672f3308dba7a76a871.tar.bz2 yuescript-bdca36433c0ca86e4eab0672f3308dba7a76a871.zip | |
Use YueComment node for TableLit comment preservation
Diffstat (limited to 'src')
| -rw-r--r-- | src/yuescript/yue_ast.cpp | 4 | ||||
| -rw-r--r-- | src/yuescript/yue_ast.h | 5 | ||||
| -rw-r--r-- | src/yuescript/yue_compiler.cpp | 18 | ||||
| -rw-r--r-- | src/yuescript/yue_parser.cpp | 4 |
4 files changed, 12 insertions, 19 deletions
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 { | |||
| 1154 | bool hasCommentOrEmpty = false; | 1154 | bool hasCommentOrEmpty = false; |
| 1155 | ast_node* lastValueNode = nullptr; | 1155 | ast_node* lastValueNode = nullptr; |
| 1156 | for (auto value : values.objects()) { | 1156 | for (auto value : values.objects()) { |
| 1157 | if (ast_is<YueLineComment_t, YueMultilineComment_t, EmptyLine_t>(value)) { | 1157 | if (ast_is<YueComment_t, EmptyLine_t>(value)) { |
| 1158 | hasCommentOrEmpty = true; | 1158 | hasCommentOrEmpty = true; |
| 1159 | continue; | 1159 | continue; |
| 1160 | } | 1160 | } |
| @@ -1173,7 +1173,7 @@ std::string TableLit_t::to_string(void* ud) const { | |||
| 1173 | continue; | 1173 | continue; |
| 1174 | } | 1174 | } |
| 1175 | auto valueStr = value->to_string(ud); | 1175 | auto valueStr = value->to_string(ud); |
| 1176 | if (!ast_is<YueLineComment_t, YueMultilineComment_t>(value) && value != lastValueNode) { | 1176 | if (!ast_is<YueComment_t>(value) && value != lastValueNode) { |
| 1177 | valueStr += ','; | 1177 | valueStr += ','; |
| 1178 | } | 1178 | } |
| 1179 | temp.emplace_back(info->ind() + valueStr); | 1179 | 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; | |||
| 87 | class SpreadListExp_t; | 87 | class SpreadListExp_t; |
| 88 | class Comprehension_t; | 88 | class Comprehension_t; |
| 89 | class Value_t; | 89 | class Value_t; |
| 90 | class YueLineComment_t; | 90 | class YueComment_t; |
| 91 | class YueMultilineComment_t; | ||
| 92 | class EmptyLine_t; | 91 | class EmptyLine_t; |
| 93 | } // namespace yue | 92 | } // namespace yue |
| 94 | 93 | ||
| @@ -721,7 +720,7 @@ AST_NODE(TableLit) | |||
| 721 | MetaVariablePairDef_t, MetaNormalPairDef_t, | 720 | MetaVariablePairDef_t, MetaNormalPairDef_t, |
| 722 | VariablePair_t, NormalPair_t, Exp_t, | 721 | VariablePair_t, NormalPair_t, Exp_t, |
| 723 | MetaVariablePair_t, MetaNormalPair_t, | 722 | MetaVariablePair_t, MetaNormalPair_t, |
| 724 | YueLineComment_t, YueMultilineComment_t, EmptyLine_t, | 723 | YueComment_t, EmptyLine_t, |
| 725 | /*non-syntax-rule*/ TableBlockIndent_t, SpreadListExp_t> values; | 724 | /*non-syntax-rule*/ TableBlockIndent_t, SpreadListExp_t> values; |
| 726 | AST_MEMBER(TableLit, &sep, &values) | 725 | AST_MEMBER(TableLit, &sep, &values) |
| 727 | AST_END(TableLit) | 726 | 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: | |||
| 8207 | if (!_config.reserveComment) { | 8207 | if (!_config.reserveComment) { |
| 8208 | for (auto it = values.rbegin(); it != values.rend(); ++it) { | 8208 | for (auto it = values.rbegin(); it != values.rend(); ++it) { |
| 8209 | auto node = *it; | 8209 | auto node = *it; |
| 8210 | if (!ast_is<YueLineComment_t, YueMultilineComment_t, EmptyLine_t>(node)) { | 8210 | if (!ast_is<YueComment_t, EmptyLine_t>(node)) { |
| 8211 | lastValueNode = node; | 8211 | lastValueNode = node; |
| 8212 | break; | 8212 | break; |
| 8213 | } | 8213 | } |
| 8214 | } | 8214 | } |
| 8215 | } | 8215 | } |
| 8216 | for (auto value : values) { | 8216 | for (auto value : values) { |
| 8217 | if (!_config.reserveComment && ast_is<YueLineComment_t, YueMultilineComment_t, EmptyLine_t>(value)) { | 8217 | if (!_config.reserveComment && ast_is<YueComment_t, EmptyLine_t>(value)) { |
| 8218 | continue; | 8218 | continue; |
| 8219 | } | 8219 | } |
| 8220 | auto item = value; | 8220 | auto item = value; |
| @@ -8264,15 +8264,9 @@ private: | |||
| 8264 | bool skipComma = false; | 8264 | bool skipComma = false; |
| 8265 | switch (item->get_id()) { | 8265 | switch (item->get_id()) { |
| 8266 | case id<Exp_t>(): transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); break; | 8266 | case id<Exp_t>(): transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); break; |
| 8267 | case id<YueLineComment_t>(): { | 8267 | case id<YueComment_t>(): { |
| 8268 | auto comment = static_cast<YueLineComment_t*>(item); | 8268 | auto comment = static_cast<YueComment_t*>(item); |
| 8269 | temp.emplace_back("--"s + _parser.toString(comment)); | 8269 | temp.emplace_back(comment->to_string(&_config)); |
| 8270 | skipComma = true; | ||
| 8271 | break; | ||
| 8272 | } | ||
| 8273 | case id<YueMultilineComment_t>(): { | ||
| 8274 | auto comment = static_cast<YueMultilineComment_t*>(item); | ||
| 8275 | temp.emplace_back("--[["s + _parser.toString(comment) + "]]"s); | ||
| 8276 | skipComma = true; | 8270 | skipComma = true; |
| 8277 | break; | 8271 | break; |
| 8278 | } | 8272 | } |
| @@ -8337,7 +8331,7 @@ private: | |||
| 8337 | default: YUEE("AST node mismatch", item); break; | 8331 | default: YUEE("AST node mismatch", item); break; |
| 8338 | } | 8332 | } |
| 8339 | if (!isMetamethod) { | 8333 | if (!isMetamethod) { |
| 8340 | if (skipComma || temp.back().rfind("--"sv, 0) == 0) { | 8334 | if (skipComma) { |
| 8341 | temp.back() = indent() + temp.back() + nl(value); | 8335 | temp.back() = indent() + temp.back() + nl(value); |
| 8342 | } else { | 8336 | } else { |
| 8343 | temp.back() = indent() + (value == lastValueNode ? temp.back() : temp.back() + ',') + nl(value); | 8337 | 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() { | |||
| 890 | 890 | ||
| 891 | table_lit_line = | 891 | table_lit_line = |
| 892 | -EmptyLine >> ( | 892 | -EmptyLine >> ( |
| 893 | 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) | 893 | 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) |
| 894 | ) | ( | 894 | ) | ( |
| 895 | space | 895 | space |
| 896 | ); | 896 | ); |
| @@ -899,7 +899,7 @@ YueParser::YueParser() { | |||
| 899 | 899 | ||
| 900 | TableLit = | 900 | TableLit = |
| 901 | '{' >> Seperator >> | 901 | '{' >> Seperator >> |
| 902 | -(plain_space >> (table_value | yue_line_comment | yue_multiline_comment) >> *(plain_space >> ',' >> plain_space >> (table_value | yue_line_comment | yue_multiline_comment)) >> -(plain_space >> ',')) >> | 902 | -(plain_space >> (table_value | YueComment) >> *(plain_space >> ',' >> plain_space >> (table_value | YueComment)) >> -(plain_space >> ',')) >> |
| 903 | ( | 903 | ( |
| 904 | table_lit_lines >> white >> end_braces_expression | | 904 | table_lit_lines >> white >> end_braces_expression | |
| 905 | white >> '}' | 905 | white >> '}' |
