From b3aba7938aee0d4885d9243dc3f2f3d89d9812b6 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 4 Dec 2025 09:24:55 +0800 Subject: Fixed empty line as block issue. --- spec/inputs/syntax.yue | 8 ++++++++ spec/outputs/syntax.lua | 11 +++++++++++ src/yuescript/yue_ast.cpp | 9 ++++++--- src/yuescript/yue_ast.h | 7 +++++-- src/yuescript/yue_compiler.cpp | 5 ++++- src/yuescript/yue_parser.cpp | 12 +++++++----- src/yuescript/yue_parser.h | 2 ++ src/yuescript/yuescript.cpp | 2 +- 8 files changed, 44 insertions(+), 12 deletions(-) diff --git a/spec/inputs/syntax.yue b/spec/inputs/syntax.yue index a414d6f..4c0c56c 100644 --- a/spec/inputs/syntax.yue +++ b/spec/inputs/syntax.yue @@ -478,5 +478,13 @@ do f2 = -> -- +do + return res if res ~= "" + + +do + return res if res ~= "" + -- + nil diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index 040a325..2df3473 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua @@ -430,4 +430,15 @@ do local f2 f2 = function() end end +do + if res ~= "" then + return res + end +end +do + return res((function() + if res ~= "" then + end + end)()) +end return nil diff --git a/src/yuescript/yue_ast.cpp b/src/yuescript/yue_ast.cpp index 7574b06..a2e3403 100644 --- a/src/yuescript/yue_ast.cpp +++ b/src/yuescript/yue_ast.cpp @@ -1612,6 +1612,9 @@ std::string Statement_t::to_string(void* ud) const { std::string StatementSep_t::to_string(void*) const { return {}; } +std::string EmptyLine_t::to_string(void*) const { + return {}; +} std::string ChainAssign_t::to_string(void* ud) const { str_list temp; for (auto exp : exprs.objects()) { @@ -1635,11 +1638,11 @@ std::string Block_t::to_string(void* ud) const { temp.emplace_back(info->ind() + stmt->to_string(ud)); } } else if (info->reserveComment) { - auto comment = ast_to(stmt_); - if (comment->comment) { + if (auto comment = ast_cast(stmt_)) { temp.emplace_back(info->ind() + comment->to_string(ud)); } else { - temp.emplace_back(comment->to_string(ud)); + auto empty = ast_to(stmt_); + temp.emplace_back(empty->to_string(ud)); } } } diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index df852bd..97901ec 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -933,10 +933,13 @@ AST_LEAF(YueMultilineComment) AST_END(YueMultilineComment) AST_NODE(YueComment) - ast_sel comment; + ast_sel comment; AST_MEMBER(YueComment, &comment) AST_END(YueComment) +AST_LEAF(EmptyLine) +AST_END(EmptyLine) + AST_NODE(ChainAssign) ast_ptr sep; ast_list exprs; @@ -962,7 +965,7 @@ AST_END(Body) AST_NODE(Block) ast_ptr sep; - ast_sel_list statementOrComments; + ast_sel_list statementOrComments; AST_MEMBER(Block, &sep, &statementOrComments) AST_END(Block) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 8d3899f..bc5215c 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -78,7 +78,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.30.1"sv; +const std::string_view version = "0.30.2"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -5290,6 +5290,9 @@ private: transformComment(comment, temp); continue; } + if (!ast_is(node)) { + continue; + } auto transformNode = [&]() { currentScope().lastStatement = (node == lastStmt) && currentScope().mode == GlobalMode::None; transformStatement(static_cast(node), temp); diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index cacfebe..a7feb83 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -66,6 +66,7 @@ YueParser::YueParser() { space = -(and_(set(" \t-\\")) >> *space_one >> -comment); space_break = space >> line_break; white = space >> *(line_break >> space); + plain_white = plain_space >> *(line_break >> plain_space); alpha_num = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; not_alpha_num = not_(alpha_num); Name = (range('a', 'z') | range('A', 'Z') | '_') >> *alpha_num >> not_(larger(255)); @@ -1109,15 +1110,16 @@ YueParser::YueParser() { yue_line_comment; YueComment = check_indent >> comment_line >> and_(stop) | - advance >> ensure(comment_line, pop_indent) >> and_(stop) | - plain_space >> and_(stop); + advance >> ensure(comment_line, pop_indent) >> and_(stop); + + EmptyLine = plain_space >> and_(stop); indentation_error = pl::user(not_(pipe_operator | eof()), [](const item_t& item) { RaiseError("unexpected indent"sv, item); return false; }); - line = ( + line = *(EmptyLine >> line_break) >> ( check_indent_match >> space >> Statement | YueComment | advance_match >> ensure(space >> (indentation_error | Statement), pop_indent) @@ -1128,8 +1130,8 @@ YueParser::YueParser() { }) >> lax_line >> *(line_break >> lax_line) | line >> *(line_break >> line)); shebang = "#!" >> *(not_(stop) >> any_char); - BlockEnd = Block >> stop; - File = -shebang >> -Block >> stop; + BlockEnd = Block >> plain_white >> stop; + File = -shebang >> -Block >> plain_white >> stop; lax_line = advance_match >> ensure(*(not_(stop) >> any()), pop_indent) | line >> and_(stop) | check_indent_match >> *(not_(stop) >> any()); } diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index 3b1cd61..3a726c3 100644 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h @@ -197,6 +197,7 @@ private: NONE_AST_RULE(line_break); NONE_AST_RULE(any_char); NONE_AST_RULE(white); + NONE_AST_RULE(plain_white); NONE_AST_RULE(stop); NONE_AST_RULE(comment); NONE_AST_RULE(multi_line_open); @@ -473,6 +474,7 @@ private: AST_RULE(YueLineComment); AST_RULE(YueMultilineComment); AST_RULE(YueComment); + AST_RULE(EmptyLine); AST_RULE(ChainAssign); AST_RULE(Body); AST_RULE(Block); diff --git a/src/yuescript/yuescript.cpp b/src/yuescript/yuescript.cpp index 6089894..62969fe 100644 --- a/src/yuescript/yuescript.cpp +++ b/src/yuescript/yuescript.cpp @@ -348,7 +348,7 @@ static int yuetoast(lua_State* L) { current.hasSep = true; return false; } - if (!reserveComment && yue::ast_is(child)) { + if (!reserveComment && yue::ast_is(child)) { return false; } if (!current.children) { -- cgit v1.2.3-55-g6feb