diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-02-22 01:06:44 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-02-22 01:07:06 +0800 |
| commit | 042c393a4ce4454948b69e99f0fc721736403208 (patch) | |
| tree | 657043c6e3c899cd86c6b30fc304fc4e9228804a | |
| parent | 5368d22d4bbb36682766688a7387622953a0069c (diff) | |
| download | yuescript-042c393a4ce4454948b69e99f0fc721736403208.tar.gz yuescript-042c393a4ce4454948b69e99f0fc721736403208.tar.bz2 yuescript-042c393a4ce4454948b69e99f0fc721736403208.zip | |
Cleanup.
| -rw-r--r-- | spec/inputs/whitespace.yue | 39 | ||||
| -rw-r--r-- | spec/outputs/whitespace.lua | 33 | ||||
| -rw-r--r-- | src/yuescript/yue_compiler.cpp | 11 | ||||
| -rw-r--r-- | src/yuescript/yue_parser.cpp | 45 | ||||
| -rw-r--r-- | src/yuescript/yue_parser.h | 1 |
5 files changed, 97 insertions, 32 deletions
diff --git a/spec/inputs/whitespace.yue b/spec/inputs/whitespace.yue index e501d3d..a999143 100644 --- a/spec/inputs/whitespace.yue +++ b/spec/inputs/whitespace.yue | |||
| @@ -160,4 +160,43 @@ local a,\ | |||
| 160 | b,\ | 160 | b,\ |
| 161 | c | 161 | c |
| 162 | 162 | ||
| 163 | do | ||
| 164 | tb = | ||
| 165 | |||
| 166 | |||
| 167 | -- one | ||
| 168 | |||
| 169 | |||
| 170 | -- a | ||
| 171 | a: 1 -- 1 | ||
| 172 | |||
| 173 | -- two | ||
| 174 | b: -> -- 2 | ||
| 175 | |||
| 176 | tb2 = { | ||
| 177 | |||
| 178 | |||
| 179 | -- one | ||
| 180 | |||
| 181 | |||
| 182 | -- a | ||
| 183 | a: 1 -- 1 | ||
| 184 | |||
| 185 | -- two | ||
| 186 | b: -> -- 2 | ||
| 187 | } | ||
| 188 | |||
| 189 | tb3 = class -- dsd | ||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | -- one | ||
| 194 | |||
| 195 | -- a | ||
| 196 | a: 1 -- 1 | ||
| 197 | |||
| 198 | -- two | ||
| 199 | b: -> -- 2 | ||
| 200 | |||
| 201 | |||
| 163 | nil | 202 | nil |
diff --git a/spec/outputs/whitespace.lua b/spec/outputs/whitespace.lua index 864f085..60e98bb 100644 --- a/spec/outputs/whitespace.lua +++ b/spec/outputs/whitespace.lua | |||
| @@ -107,4 +107,37 @@ for i = 1, 10, -1 do | |||
| 107 | print(i) | 107 | print(i) |
| 108 | end | 108 | end |
| 109 | local a, b, c | 109 | local a, b, c |
| 110 | do | ||
| 111 | local tb = { | ||
| 112 | a = 1, | ||
| 113 | b = function() end | ||
| 114 | } | ||
| 115 | local tb2 = { | ||
| 116 | a = 1, | ||
| 117 | b = function() end | ||
| 118 | } | ||
| 119 | local tb3 | ||
| 120 | local _class_0 | ||
| 121 | local _base_0 = { | ||
| 122 | a = 1, | ||
| 123 | b = function() end | ||
| 124 | } | ||
| 125 | if _base_0.__index == nil then | ||
| 126 | _base_0.__index = _base_0 | ||
| 127 | end | ||
| 128 | _class_0 = setmetatable({ | ||
| 129 | __init = function() end, | ||
| 130 | __base = _base_0, | ||
| 131 | __name = "tb3" | ||
| 132 | }, { | ||
| 133 | __index = _base_0, | ||
| 134 | __call = function(cls, ...) | ||
| 135 | local _self_0 = setmetatable({ }, _base_0) | ||
| 136 | cls.__init(_self_0, ...) | ||
| 137 | return _self_0 | ||
| 138 | end | ||
| 139 | }) | ||
| 140 | _base_0.__class = _class_0 | ||
| 141 | tb3 = _class_0 | ||
| 142 | end | ||
| 110 | return nil | 143 | return nil |
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index a4ce15c..6cf0fc7 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
| @@ -1033,9 +1033,6 @@ private: | |||
| 1033 | 1033 | ||
| 1034 | const std::string nl(ast_node* node) const { | 1034 | const std::string nl(ast_node* node) const { |
| 1035 | if (_config.reserveLineNumber) { | 1035 | if (_config.reserveLineNumber) { |
| 1036 | if (ast_is<EmptyLine_t>(node)) { | ||
| 1037 | return _newLine; | ||
| 1038 | } | ||
| 1039 | return " -- "s + std::to_string(node->m_begin.m_line + _config.lineOffset) + _newLine; | 1036 | return " -- "s + std::to_string(node->m_begin.m_line + _config.lineOffset) + _newLine; |
| 1040 | } else { | 1037 | } else { |
| 1041 | return _newLine; | 1038 | return _newLine; |
| @@ -7996,13 +7993,13 @@ private: | |||
| 7996 | case id<YueComment_t>(): { | 7993 | case id<YueComment_t>(): { |
| 7997 | if (_config.reserveComment) { | 7994 | if (_config.reserveComment) { |
| 7998 | auto comment = static_cast<YueComment_t*>(item); | 7995 | auto comment = static_cast<YueComment_t*>(item); |
| 7999 | temp.push_back(indent() + comment->to_string(&_config) + nl(item)); | 7996 | temp.push_back(indent() + comment->to_string(&_config) + '\n'); |
| 8000 | } | 7997 | } |
| 8001 | break; | 7998 | break; |
| 8002 | } | 7999 | } |
| 8003 | case id<EmptyLine_t>(): { | 8000 | case id<EmptyLine_t>(): { |
| 8004 | if (_config.reserveComment) { | 8001 | if (_config.reserveComment) { |
| 8005 | temp.push_back(nl(item)); | 8002 | temp.push_back("\n"s); |
| 8006 | } | 8003 | } |
| 8007 | break; | 8004 | break; |
| 8008 | } | 8005 | } |
| @@ -8356,7 +8353,7 @@ private: | |||
| 8356 | } | 8353 | } |
| 8357 | if (!isMetamethod) { | 8354 | if (!isMetamethod) { |
| 8358 | if (skipComma) { | 8355 | if (skipComma) { |
| 8359 | temp.back() = indent() + temp.back() + nl(value); | 8356 | temp.back() = indent() + temp.back() + '\n'; |
| 8360 | } else { | 8357 | } else { |
| 8361 | temp.back() = indent() + (value == lastValueNode ? temp.back() : temp.back() + ',') + nl(value); | 8358 | temp.back() = indent() + (value == lastValueNode ? temp.back() : temp.back() + ',') + nl(value); |
| 8362 | } | 8359 | } |
| @@ -9984,7 +9981,7 @@ private: | |||
| 9984 | case id<YueComment_t>(): { | 9981 | case id<YueComment_t>(): { |
| 9985 | if (_config.reserveComment) { | 9982 | if (_config.reserveComment) { |
| 9986 | auto comment = static_cast<YueComment_t*>(content); | 9983 | auto comment = static_cast<YueComment_t*>(content); |
| 9987 | baseEntries.emplace_back(indent(1) + comment->to_string(&_config) + nl(content), false); | 9984 | baseEntries.emplace_back(indent(1) + comment->to_string(&_config) + '\n', false); |
| 9988 | } | 9985 | } |
| 9989 | break; | 9986 | break; |
| 9990 | } | 9987 | } |
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index f712db7..c5c3ef7 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp | |||
| @@ -63,6 +63,7 @@ public: | |||
| 63 | YueParser::YueParser() { | 63 | YueParser::YueParser() { |
| 64 | plain_space = *set(" \t"); | 64 | plain_space = *set(" \t"); |
| 65 | line_break = nl(-expr('\r') >> '\n'); | 65 | line_break = nl(-expr('\r') >> '\n'); |
| 66 | plain_space_break = plain_space >> line_break; | ||
| 66 | any_char = line_break | any(); | 67 | any_char = line_break | any(); |
| 67 | stop = line_break | eof(); | 68 | stop = line_break | eof(); |
| 68 | comment = "--" >> *(not_(set("\r\n")) >> any_char) >> and_(stop); | 69 | comment = "--" >> *(not_(set("\r\n")) >> any_char) >> and_(stop); |
| @@ -888,43 +889,37 @@ YueParser::YueParser() { | |||
| 888 | SpreadExp | | 889 | SpreadExp | |
| 889 | NormalDef; | 890 | NormalDef; |
| 890 | 891 | ||
| 891 | table_lit_line = | 892 | table_lit_line = -EmptyLine >> push_indent_match >> ( |
| 892 | -EmptyLine >> ( | 893 | space >> not_(line_break | '}') >> (table_value | expected_expression_error) >> *(space >> ',' >> space >> table_value) >> space >> -(',' >> space) >> pop_indent | |
| 893 | push_indent_match >> ( | 894 | YueComment >> pop_indent | |
| 894 | space >> not_(line_break | '}') >> (table_value | expected_expression_error) >> *(space >> ',' >> space >> table_value) >> pop_indent | | 895 | pop_indent |
| 895 | YueComment >> pop_indent | | 896 | ); |
| 896 | pop_indent | ||
| 897 | ) | ||
| 898 | ) | ( | ||
| 899 | space | ||
| 900 | ); | ||
| 901 | 897 | ||
| 902 | table_lit_lines = space_break >> table_lit_line >> *(-(space >> ',') >> space_break >> table_lit_line) >> -(space >> ','); | 898 | table_lit_lines = +plain_space_break >> table_lit_line >> *(line_break >> table_lit_line); |
| 903 | 899 | ||
| 904 | TableLit = | 900 | TableLit = |
| 905 | '{' >> Seperator >> | 901 | '{' >> Seperator >> |
| 906 | -(space >> table_value >> *(space >> ',' >> space >> table_value) >> -(space >> ',')) >> | 902 | -(space >> table_value >> *(space >> ',' >> space >> table_value) >> -(space >> ',')) >> space >> |
| 907 | ( | 903 | ( |
| 908 | table_lit_lines >> white >> end_braces_expression | | 904 | table_lit_lines >> white >> end_braces_expression | |
| 909 | white >> '}' | 905 | white >> '}' |
| 910 | ); | 906 | ); |
| 911 | 907 | ||
| 912 | table_block_inner = Seperator >> key_value_line >> *(((plain_space >> line_break) >> key_value_line) | (+space_break >> not_(expr("--")) >> key_value_line)); | 908 | table_block_inner = Seperator >> key_value_line >> *(line_break >> key_value_line); |
| 913 | TableBlock = ((+(plain_space >> line_break)) | (+space_break >> not_(expr("--")))) >> advance_match >> ensure(table_block_inner, pop_indent); | 909 | TableBlock = +plain_space_break >> advance_match >> ensure(table_block_inner, pop_indent); |
| 914 | TableBlockIndent = ('*' | '-' >> space_one) >> Seperator >> disable_arg_table_block_rule( | 910 | TableBlockIndent = ('*' | '-' >> space_one) >> Seperator >> disable_arg_table_block_rule( |
| 915 | space >> key_value_list >> -(space >> ',') >> | 911 | space >> key_value_list >> -(space >> ',') >> |
| 916 | -((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)) | | 912 | -(plain_space_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(plain_space_break >> key_value_line), pop_indent))); |
| 917 | (+space_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(+space_break >> key_value_line), pop_indent)))); | ||
| 918 | 913 | ||
| 919 | ClassMemberList = Seperator >> key_value >> *(space >> ',' >> space >> key_value); | 914 | ClassMemberList = Seperator >> key_value >> *(space >> ',' >> space >> key_value); |
| 920 | class_line = | 915 | class_line = -EmptyLine >> ( |
| 921 | (EmptyLine >> YueComment) | | ||
| 922 | YueComment | | 916 | YueComment | |
| 923 | check_indent_match >> space >> (ClassMemberList | Statement) >> -(space >> ','); | 917 | check_indent_match >> space >> (ClassMemberList | Statement) >> -(space >> ',') |
| 918 | ) >> space; | ||
| 924 | ClassBlock = | 919 | ClassBlock = |
| 925 | ((+(plain_space >> line_break)) | (+space_break >> not_(expr("--")))) >> | 920 | +plain_space_break >> |
| 926 | advance_match >> Seperator >> | 921 | advance_match >> Seperator >> |
| 927 | class_line >> *(((plain_space >> line_break) >> class_line) | (+space_break >> not_(expr("--")) >> class_line)) >> | 922 | class_line >> *(plain_space_break >> class_line) >> |
| 928 | pop_indent; | 923 | pop_indent; |
| 929 | 924 | ||
| 930 | ClassDecl = | 925 | ClassDecl = |
| @@ -932,7 +927,7 @@ YueParser::YueParser() { | |||
| 932 | -(space >> Assignable) >> | 927 | -(space >> Assignable) >> |
| 933 | -(space >> key("extends") >> prevent_indent >> space >> ensure(must_exp, pop_indent)) >> | 928 | -(space >> key("extends") >> prevent_indent >> space >> ensure(must_exp, pop_indent)) >> |
| 934 | -(space >> key("using") >> prevent_indent >> space >> ensure(ExpList | expected_expression_error, pop_indent)) | 929 | -(space >> key("using") >> prevent_indent >> space >> ensure(ExpList | expected_expression_error, pop_indent)) |
| 935 | ) >> -ClassBlock; | 930 | ) >> space >> -ClassBlock; |
| 936 | 931 | ||
| 937 | GlobalValues = NameList >> -(space >> '=' >> space >> (TableBlock | ExpList | expected_expression_error)); | 932 | GlobalValues = NameList >> -(space >> '=' >> space >> (TableBlock | ExpList | expected_expression_error)); |
| 938 | GlobalOp = expr('*') | '^'; | 933 | GlobalOp = expr('*') | '^'; |
| @@ -1021,14 +1016,14 @@ YueParser::YueParser() { | |||
| 1021 | MetaVariablePair | | 1016 | MetaVariablePair | |
| 1022 | MetaNormalPair; | 1017 | MetaNormalPair; |
| 1023 | key_value_list = key_value >> *(space >> ',' >> space >> key_value); | 1018 | key_value_list = key_value >> *(space >> ',' >> space >> key_value); |
| 1024 | key_value_line = | 1019 | key_value_line = -EmptyLine >> ( |
| 1025 | (EmptyLine >> YueComment) | | ||
| 1026 | YueComment | | 1020 | YueComment | |
| 1027 | check_indent_match >> space >> ( | 1021 | check_indent_match >> space >> ( |
| 1028 | key_value_list >> -(space >> ',') | | 1022 | key_value_list >> -(space >> ',') | |
| 1029 | TableBlockIndent | | 1023 | TableBlockIndent | |
| 1030 | ('*' | '-' >> space_one) >> space >> (SpreadExp | Exp | TableBlock) | 1024 | ('*' | '-' >> space_one) >> space >> (SpreadExp | Exp | TableBlock) |
| 1031 | ); | 1025 | ) >> space |
| 1026 | ); | ||
| 1032 | 1027 | ||
| 1033 | fn_arg_def_list = FnArgDef >> *(space >> ',' >> space >> FnArgDef); | 1028 | fn_arg_def_list = FnArgDef >> *(space >> ',' >> space >> FnArgDef); |
| 1034 | 1029 | ||
diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index df9f39c..12b19ac 100644 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h | |||
| @@ -200,6 +200,7 @@ private: | |||
| 200 | NONE_AST_RULE(num_expo_hex); | 200 | NONE_AST_RULE(num_expo_hex); |
| 201 | NONE_AST_RULE(lj_num); | 201 | NONE_AST_RULE(lj_num); |
| 202 | NONE_AST_RULE(plain_space); | 202 | NONE_AST_RULE(plain_space); |
| 203 | NONE_AST_RULE(plain_space_break); | ||
| 203 | NONE_AST_RULE(line_break); | 204 | NONE_AST_RULE(line_break); |
| 204 | NONE_AST_RULE(any_char); | 205 | NONE_AST_RULE(any_char); |
| 205 | NONE_AST_RULE(white); | 206 | NONE_AST_RULE(white); |
