diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-02-18 21:07:33 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-02-18 21:07:33 +0800 |
| commit | 208d8f290cd9ed7be0ac8b15f2160e72536d4362 (patch) | |
| tree | f151ad0cc0fc2eee877dd26caaf7af097f36185d /src | |
| parent | 9246c787b4ee652863f165b8826f019d3486d699 (diff) | |
| download | yuescript-208d8f290cd9ed7be0ac8b15f2160e72536d4362.tar.gz yuescript-208d8f290cd9ed7be0ac8b15f2160e72536d4362.tar.bz2 yuescript-208d8f290cd9ed7be0ac8b15f2160e72536d4362.zip | |
Add TableLit AST support for empty lines and multiline comments
Diffstat (limited to 'src')
| -rw-r--r-- | src/yuescript/yue_ast.h | 5 | ||||
| -rw-r--r-- | src/yuescript/yue_compiler.cpp | 14 | ||||
| -rw-r--r-- | src/yuescript/yue_parser.cpp | 13 |
3 files changed, 24 insertions, 8 deletions
diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 8a5bbe8..e86d943 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h | |||
| @@ -88,6 +88,8 @@ 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 YueLineComment_t; |
| 91 | class YueMultilineComment_t; | ||
| 92 | class EmptyLine_t; | ||
| 91 | } // namespace yue | 93 | } // namespace yue |
| 92 | 94 | ||
| 93 | AST_LEAF(Num) | 95 | AST_LEAF(Num) |
| @@ -718,7 +720,8 @@ AST_NODE(TableLit) | |||
| 718 | VariablePairDef_t, NormalPairDef_t, SpreadExp_t, NormalDef_t, | 720 | VariablePairDef_t, NormalPairDef_t, SpreadExp_t, NormalDef_t, |
| 719 | MetaVariablePairDef_t, MetaNormalPairDef_t, | 721 | MetaVariablePairDef_t, MetaNormalPairDef_t, |
| 720 | VariablePair_t, NormalPair_t, Exp_t, | 722 | VariablePair_t, NormalPair_t, Exp_t, |
| 721 | MetaVariablePair_t, MetaNormalPair_t, YueLineComment_t, | 723 | MetaVariablePair_t, MetaNormalPair_t, |
| 724 | YueLineComment_t, YueMultilineComment_t, EmptyLine_t, | ||
| 722 | /*non-syntax-rule*/ TableBlockIndent_t, SpreadListExp_t> values; | 725 | /*non-syntax-rule*/ TableBlockIndent_t, SpreadListExp_t> values; |
| 723 | AST_MEMBER(TableLit, &sep, &values) | 726 | AST_MEMBER(TableLit, &sep, &values) |
| 724 | AST_END(TableLit) | 727 | AST_END(TableLit) |
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 8da6d2f..9226e89 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
| @@ -8248,13 +8248,25 @@ private: | |||
| 8248 | } | 8248 | } |
| 8249 | } | 8249 | } |
| 8250 | bool isMetamethod = false; | 8250 | bool isMetamethod = false; |
| 8251 | bool skipComma = false; | ||
| 8251 | switch (item->get_id()) { | 8252 | switch (item->get_id()) { |
| 8252 | case id<Exp_t>(): transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); break; | 8253 | case id<Exp_t>(): transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); break; |
| 8253 | case id<YueLineComment_t>(): { | 8254 | case id<YueLineComment_t>(): { |
| 8254 | auto comment = static_cast<YueLineComment_t*>(item); | 8255 | auto comment = static_cast<YueLineComment_t*>(item); |
| 8255 | temp.emplace_back("--"s + _parser.toString(comment)); | 8256 | temp.emplace_back("--"s + _parser.toString(comment)); |
| 8257 | skipComma = true; | ||
| 8256 | break; | 8258 | break; |
| 8257 | } | 8259 | } |
| 8260 | case id<YueMultilineComment_t>(): { | ||
| 8261 | auto comment = static_cast<YueMultilineComment_t*>(item); | ||
| 8262 | temp.emplace_back("--[["s + _parser.toString(comment) + "]]"s); | ||
| 8263 | skipComma = true; | ||
| 8264 | break; | ||
| 8265 | } | ||
| 8266 | case id<EmptyLine_t>(): | ||
| 8267 | temp.emplace_back(Empty); | ||
| 8268 | skipComma = true; | ||
| 8269 | break; | ||
| 8258 | case id<VariablePair_t>(): transform_variable_pair(static_cast<VariablePair_t*>(item), temp); break; | 8270 | case id<VariablePair_t>(): transform_variable_pair(static_cast<VariablePair_t*>(item), temp); break; |
| 8259 | case id<NormalPair_t>(): transform_normal_pair(static_cast<NormalPair_t*>(item), temp, false); break; | 8271 | case id<NormalPair_t>(): transform_normal_pair(static_cast<NormalPair_t*>(item), temp, false); break; |
| 8260 | case id<TableBlockIndent_t>(): transformTableBlockIndent(static_cast<TableBlockIndent_t*>(item), temp); break; | 8272 | case id<TableBlockIndent_t>(): transformTableBlockIndent(static_cast<TableBlockIndent_t*>(item), temp); break; |
| @@ -8312,7 +8324,7 @@ private: | |||
| 8312 | default: YUEE("AST node mismatch", item); break; | 8324 | default: YUEE("AST node mismatch", item); break; |
| 8313 | } | 8325 | } |
| 8314 | if (!isMetamethod) { | 8326 | if (!isMetamethod) { |
| 8315 | if (ast_is<YueLineComment_t>(value)) { | 8327 | if (skipComma || temp.back().rfind("--"sv, 0) == 0) { |
| 8316 | temp.back() = indent() + temp.back() + nl(value); | 8328 | temp.back() = indent() + temp.back() + nl(value); |
| 8317 | } else { | 8329 | } else { |
| 8318 | temp.back() = indent() + (value == values.back() ? temp.back() : temp.back() + ',') + nl(value); | 8330 | temp.back() = indent() + (value == values.back() ? temp.back() : temp.back() + ',') + nl(value); |
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 6a2237c..ff8b575 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp | |||
| @@ -888,17 +888,18 @@ YueParser::YueParser() { | |||
| 888 | SpreadExp | | 888 | SpreadExp | |
| 889 | NormalDef; | 889 | NormalDef; |
| 890 | 890 | ||
| 891 | table_lit_line = ( | 891 | table_lit_line = |
| 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) | 892 | -EmptyLine >> ( |
| 893 | ) | ( | 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) |
| 894 | space | 894 | ) | ( |
| 895 | ); | 895 | space |
| 896 | ); | ||
| 896 | 897 | ||
| 897 | table_lit_lines = space_break >> table_lit_line >> *(-(space >> ',') >> space_break >> table_lit_line) >> -(space >> ','); | 898 | table_lit_lines = space_break >> table_lit_line >> *(-(space >> ',') >> space_break >> table_lit_line) >> -(space >> ','); |
| 898 | 899 | ||
| 899 | TableLit = | 900 | TableLit = |
| 900 | '{' >> Seperator >> | 901 | '{' >> Seperator >> |
| 901 | -(plain_space >> (table_value | yue_line_comment) >> *(plain_space >> ',' >> plain_space >> (table_value | yue_line_comment)) >> -(plain_space >> ',')) >> | 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 | ( | 903 | ( |
| 903 | table_lit_lines >> white >> end_braces_expression | | 904 | table_lit_lines >> white >> end_braces_expression | |
| 904 | white >> '}' | 905 | white >> '}' |
