From 042c393a4ce4454948b69e99f0fc721736403208 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sun, 22 Feb 2026 01:06:44 +0800 Subject: Cleanup. --- spec/inputs/whitespace.yue | 39 ++++++++++++++++++++++++++++++++++++ spec/outputs/whitespace.lua | 33 +++++++++++++++++++++++++++++++ src/yuescript/yue_compiler.cpp | 11 ++++------- src/yuescript/yue_parser.cpp | 45 +++++++++++++++++++----------------------- 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,\ b,\ c +do + tb = + + + -- one + + + -- a + a: 1 -- 1 + + -- two + b: -> -- 2 + + tb2 = { + + + -- one + + + -- a + a: 1 -- 1 + + -- two + b: -> -- 2 + } + + tb3 = class -- dsd + + + + -- one + + -- a + a: 1 -- 1 + + -- two + b: -> -- 2 + + 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 print(i) end local a, b, c +do + local tb = { + a = 1, + b = function() end + } + local tb2 = { + a = 1, + b = function() end + } + local tb3 + local _class_0 + local _base_0 = { + a = 1, + b = function() end + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function() end, + __base = _base_0, + __name = "tb3" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + tb3 = _class_0 +end 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: const std::string nl(ast_node* node) const { if (_config.reserveLineNumber) { - if (ast_is(node)) { - return _newLine; - } return " -- "s + std::to_string(node->m_begin.m_line + _config.lineOffset) + _newLine; } else { return _newLine; @@ -7996,13 +7993,13 @@ private: case id(): { if (_config.reserveComment) { auto comment = static_cast(item); - temp.push_back(indent() + comment->to_string(&_config) + nl(item)); + temp.push_back(indent() + comment->to_string(&_config) + '\n'); } break; } case id(): { if (_config.reserveComment) { - temp.push_back(nl(item)); + temp.push_back("\n"s); } break; } @@ -8356,7 +8353,7 @@ private: } if (!isMetamethod) { if (skipComma) { - temp.back() = indent() + temp.back() + nl(value); + temp.back() = indent() + temp.back() + '\n'; } else { temp.back() = indent() + (value == lastValueNode ? temp.back() : temp.back() + ',') + nl(value); } @@ -9984,7 +9981,7 @@ private: case id(): { if (_config.reserveComment) { auto comment = static_cast(content); - baseEntries.emplace_back(indent(1) + comment->to_string(&_config) + nl(content), false); + baseEntries.emplace_back(indent(1) + comment->to_string(&_config) + '\n', false); } break; } 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: YueParser::YueParser() { plain_space = *set(" \t"); line_break = nl(-expr('\r') >> '\n'); + plain_space_break = plain_space >> line_break; any_char = line_break | any(); stop = line_break | eof(); comment = "--" >> *(not_(set("\r\n")) >> any_char) >> and_(stop); @@ -888,43 +889,37 @@ YueParser::YueParser() { SpreadExp | NormalDef; - table_lit_line = - -EmptyLine >> ( - push_indent_match >> ( - space >> not_(line_break | '}') >> (table_value | expected_expression_error) >> *(space >> ',' >> space >> table_value) >> pop_indent | - YueComment >> pop_indent | - pop_indent - ) - ) | ( - space - ); + table_lit_line = -EmptyLine >> push_indent_match >> ( + space >> not_(line_break | '}') >> (table_value | expected_expression_error) >> *(space >> ',' >> space >> table_value) >> space >> -(',' >> space) >> pop_indent | + YueComment >> pop_indent | + pop_indent + ); - table_lit_lines = space_break >> table_lit_line >> *(-(space >> ',') >> space_break >> table_lit_line) >> -(space >> ','); + table_lit_lines = +plain_space_break >> table_lit_line >> *(line_break >> table_lit_line); TableLit = '{' >> Seperator >> - -(space >> table_value >> *(space >> ',' >> space >> table_value) >> -(space >> ',')) >> + -(space >> table_value >> *(space >> ',' >> space >> table_value) >> -(space >> ',')) >> space >> ( table_lit_lines >> white >> end_braces_expression | white >> '}' ); - 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); + table_block_inner = Seperator >> key_value_line >> *(line_break >> key_value_line); + TableBlock = +plain_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) | (+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)))); + -(plain_space_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(plain_space_break >> key_value_line), pop_indent))); ClassMemberList = Seperator >> key_value >> *(space >> ',' >> space >> key_value); - class_line = - (EmptyLine >> YueComment) | + class_line = -EmptyLine >> ( YueComment | - check_indent_match >> space >> (ClassMemberList | Statement) >> -(space >> ','); + check_indent_match >> space >> (ClassMemberList | Statement) >> -(space >> ',') + ) >> space; ClassBlock = - ((+(plain_space >> line_break)) | (+space_break >> not_(expr("--")))) >> + +plain_space_break >> advance_match >> Seperator >> - class_line >> *(((plain_space >> line_break) >> class_line) | (+space_break >> not_(expr("--")) >> class_line)) >> + class_line >> *(plain_space_break >> class_line) >> pop_indent; ClassDecl = @@ -932,7 +927,7 @@ YueParser::YueParser() { -(space >> Assignable) >> -(space >> key("extends") >> prevent_indent >> space >> ensure(must_exp, pop_indent)) >> -(space >> key("using") >> prevent_indent >> space >> ensure(ExpList | expected_expression_error, pop_indent)) - ) >> -ClassBlock; + ) >> space >> -ClassBlock; GlobalValues = NameList >> -(space >> '=' >> space >> (TableBlock | ExpList | expected_expression_error)); GlobalOp = expr('*') | '^'; @@ -1021,14 +1016,14 @@ YueParser::YueParser() { MetaVariablePair | MetaNormalPair; key_value_list = key_value >> *(space >> ',' >> space >> key_value); - key_value_line = - (EmptyLine >> YueComment) | + key_value_line = -EmptyLine >> ( YueComment | check_indent_match >> space >> ( key_value_list >> -(space >> ',') | TableBlockIndent | ('*' | '-' >> space_one) >> space >> (SpreadExp | Exp | TableBlock) - ); + ) >> space + ); fn_arg_def_list = FnArgDef >> *(space >> ',' >> space >> FnArgDef); 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: NONE_AST_RULE(num_expo_hex); NONE_AST_RULE(lj_num); NONE_AST_RULE(plain_space); + NONE_AST_RULE(plain_space_break); NONE_AST_RULE(line_break); NONE_AST_RULE(any_char); NONE_AST_RULE(white); -- cgit v1.2.3-55-g6feb