diff options
| author | Li Jin <dragon-fly@qq.com> | 2025-12-04 09:24:55 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2025-12-04 09:24:55 +0800 |
| commit | b3aba7938aee0d4885d9243dc3f2f3d89d9812b6 (patch) | |
| tree | 1aae4362ba535e82d45d335c5f4ff88acd8e5f51 | |
| parent | ec26010fd100bb014584653e0e1370dba816fd1f (diff) | |
| download | yuescript-b3aba7938aee0d4885d9243dc3f2f3d89d9812b6.tar.gz yuescript-b3aba7938aee0d4885d9243dc3f2f3d89d9812b6.tar.bz2 yuescript-b3aba7938aee0d4885d9243dc3f2f3d89d9812b6.zip | |
Fixed empty line as block issue.v0.30.2
| -rw-r--r-- | spec/inputs/syntax.yue | 8 | ||||
| -rw-r--r-- | spec/outputs/syntax.lua | 11 | ||||
| -rw-r--r-- | src/yuescript/yue_ast.cpp | 9 | ||||
| -rw-r--r-- | src/yuescript/yue_ast.h | 7 | ||||
| -rw-r--r-- | src/yuescript/yue_compiler.cpp | 5 | ||||
| -rw-r--r-- | src/yuescript/yue_parser.cpp | 12 | ||||
| -rw-r--r-- | src/yuescript/yue_parser.h | 2 | ||||
| -rw-r--r-- | 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 | |||
| 478 | f2 = -> | 478 | f2 = -> |
| 479 | -- | 479 | -- |
| 480 | 480 | ||
| 481 | do | ||
| 482 | return res if res ~= "" | ||
| 483 | |||
| 484 | |||
| 485 | do | ||
| 486 | return res if res ~= "" | ||
| 487 | -- | ||
| 488 | |||
| 481 | nil | 489 | nil |
| 482 | 490 | ||
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 | |||
| 430 | local f2 | 430 | local f2 |
| 431 | f2 = function() end | 431 | f2 = function() end |
| 432 | end | 432 | end |
| 433 | do | ||
| 434 | if res ~= "" then | ||
| 435 | return res | ||
| 436 | end | ||
| 437 | end | ||
| 438 | do | ||
| 439 | return res((function() | ||
| 440 | if res ~= "" then | ||
| 441 | end | ||
| 442 | end)()) | ||
| 443 | end | ||
| 433 | return nil | 444 | 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 { | |||
| 1612 | std::string StatementSep_t::to_string(void*) const { | 1612 | std::string StatementSep_t::to_string(void*) const { |
| 1613 | return {}; | 1613 | return {}; |
| 1614 | } | 1614 | } |
| 1615 | std::string EmptyLine_t::to_string(void*) const { | ||
| 1616 | return {}; | ||
| 1617 | } | ||
| 1615 | std::string ChainAssign_t::to_string(void* ud) const { | 1618 | std::string ChainAssign_t::to_string(void* ud) const { |
| 1616 | str_list temp; | 1619 | str_list temp; |
| 1617 | for (auto exp : exprs.objects()) { | 1620 | for (auto exp : exprs.objects()) { |
| @@ -1635,11 +1638,11 @@ std::string Block_t::to_string(void* ud) const { | |||
| 1635 | temp.emplace_back(info->ind() + stmt->to_string(ud)); | 1638 | temp.emplace_back(info->ind() + stmt->to_string(ud)); |
| 1636 | } | 1639 | } |
| 1637 | } else if (info->reserveComment) { | 1640 | } else if (info->reserveComment) { |
| 1638 | auto comment = ast_to<YueComment_t>(stmt_); | 1641 | if (auto comment = ast_cast<YueComment_t>(stmt_)) { |
| 1639 | if (comment->comment) { | ||
| 1640 | temp.emplace_back(info->ind() + comment->to_string(ud)); | 1642 | temp.emplace_back(info->ind() + comment->to_string(ud)); |
| 1641 | } else { | 1643 | } else { |
| 1642 | temp.emplace_back(comment->to_string(ud)); | 1644 | auto empty = ast_to<EmptyLine_t>(stmt_); |
| 1645 | temp.emplace_back(empty->to_string(ud)); | ||
| 1643 | } | 1646 | } |
| 1644 | } | 1647 | } |
| 1645 | } | 1648 | } |
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) | |||
| 933 | AST_END(YueMultilineComment) | 933 | AST_END(YueMultilineComment) |
| 934 | 934 | ||
| 935 | AST_NODE(YueComment) | 935 | AST_NODE(YueComment) |
| 936 | ast_sel<false, YueLineComment_t, YueMultilineComment_t> comment; | 936 | ast_sel<true, YueLineComment_t, YueMultilineComment_t> comment; |
| 937 | AST_MEMBER(YueComment, &comment) | 937 | AST_MEMBER(YueComment, &comment) |
| 938 | AST_END(YueComment) | 938 | AST_END(YueComment) |
| 939 | 939 | ||
| 940 | AST_LEAF(EmptyLine) | ||
| 941 | AST_END(EmptyLine) | ||
| 942 | |||
| 940 | AST_NODE(ChainAssign) | 943 | AST_NODE(ChainAssign) |
| 941 | ast_ptr<true, Seperator_t> sep; | 944 | ast_ptr<true, Seperator_t> sep; |
| 942 | ast_list<true, Exp_t> exprs; | 945 | ast_list<true, Exp_t> exprs; |
| @@ -962,7 +965,7 @@ AST_END(Body) | |||
| 962 | 965 | ||
| 963 | AST_NODE(Block) | 966 | AST_NODE(Block) |
| 964 | ast_ptr<true, Seperator_t> sep; | 967 | ast_ptr<true, Seperator_t> sep; |
| 965 | ast_sel_list<false, Statement_t, YueComment_t> statementOrComments; | 968 | ast_sel_list<false, Statement_t, YueComment_t, EmptyLine_t> statementOrComments; |
| 966 | AST_MEMBER(Block, &sep, &statementOrComments) | 969 | AST_MEMBER(Block, &sep, &statementOrComments) |
| 967 | AST_END(Block) | 970 | AST_END(Block) |
| 968 | 971 | ||
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<std::string> Metamethods = { | |||
| 78 | "close"s // Lua 5.4 | 78 | "close"s // Lua 5.4 |
| 79 | }; | 79 | }; |
| 80 | 80 | ||
| 81 | const std::string_view version = "0.30.1"sv; | 81 | const std::string_view version = "0.30.2"sv; |
| 82 | const std::string_view extension = "yue"sv; | 82 | const std::string_view extension = "yue"sv; |
| 83 | 83 | ||
| 84 | class CompileError : public std::logic_error { | 84 | class CompileError : public std::logic_error { |
| @@ -5290,6 +5290,9 @@ private: | |||
| 5290 | transformComment(comment, temp); | 5290 | transformComment(comment, temp); |
| 5291 | continue; | 5291 | continue; |
| 5292 | } | 5292 | } |
| 5293 | if (!ast_is<Statement_t>(node)) { | ||
| 5294 | continue; | ||
| 5295 | } | ||
| 5293 | auto transformNode = [&]() { | 5296 | auto transformNode = [&]() { |
| 5294 | currentScope().lastStatement = (node == lastStmt) && currentScope().mode == GlobalMode::None; | 5297 | currentScope().lastStatement = (node == lastStmt) && currentScope().mode == GlobalMode::None; |
| 5295 | transformStatement(static_cast<Statement_t*>(node), temp); | 5298 | transformStatement(static_cast<Statement_t*>(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() { | |||
| 66 | space = -(and_(set(" \t-\\")) >> *space_one >> -comment); | 66 | space = -(and_(set(" \t-\\")) >> *space_one >> -comment); |
| 67 | space_break = space >> line_break; | 67 | space_break = space >> line_break; |
| 68 | white = space >> *(line_break >> space); | 68 | white = space >> *(line_break >> space); |
| 69 | plain_white = plain_space >> *(line_break >> plain_space); | ||
| 69 | alpha_num = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; | 70 | alpha_num = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; |
| 70 | not_alpha_num = not_(alpha_num); | 71 | not_alpha_num = not_(alpha_num); |
| 71 | Name = (range('a', 'z') | range('A', 'Z') | '_') >> *alpha_num >> not_(larger(255)); | 72 | Name = (range('a', 'z') | range('A', 'Z') | '_') >> *alpha_num >> not_(larger(255)); |
| @@ -1109,15 +1110,16 @@ YueParser::YueParser() { | |||
| 1109 | yue_line_comment; | 1110 | yue_line_comment; |
| 1110 | YueComment = | 1111 | YueComment = |
| 1111 | check_indent >> comment_line >> and_(stop) | | 1112 | check_indent >> comment_line >> and_(stop) | |
| 1112 | advance >> ensure(comment_line, pop_indent) >> and_(stop) | | 1113 | advance >> ensure(comment_line, pop_indent) >> and_(stop); |
| 1113 | plain_space >> and_(stop); | 1114 | |
| 1115 | EmptyLine = plain_space >> and_(stop); | ||
| 1114 | 1116 | ||
| 1115 | indentation_error = pl::user(not_(pipe_operator | eof()), [](const item_t& item) { | 1117 | indentation_error = pl::user(not_(pipe_operator | eof()), [](const item_t& item) { |
| 1116 | RaiseError("unexpected indent"sv, item); | 1118 | RaiseError("unexpected indent"sv, item); |
| 1117 | return false; | 1119 | return false; |
| 1118 | }); | 1120 | }); |
| 1119 | 1121 | ||
| 1120 | line = ( | 1122 | line = *(EmptyLine >> line_break) >> ( |
| 1121 | check_indent_match >> space >> Statement | | 1123 | check_indent_match >> space >> Statement | |
| 1122 | YueComment | | 1124 | YueComment | |
| 1123 | advance_match >> ensure(space >> (indentation_error | Statement), pop_indent) | 1125 | advance_match >> ensure(space >> (indentation_error | Statement), pop_indent) |
| @@ -1128,8 +1130,8 @@ YueParser::YueParser() { | |||
| 1128 | }) >> lax_line >> *(line_break >> lax_line) | line >> *(line_break >> line)); | 1130 | }) >> lax_line >> *(line_break >> lax_line) | line >> *(line_break >> line)); |
| 1129 | 1131 | ||
| 1130 | shebang = "#!" >> *(not_(stop) >> any_char); | 1132 | shebang = "#!" >> *(not_(stop) >> any_char); |
| 1131 | BlockEnd = Block >> stop; | 1133 | BlockEnd = Block >> plain_white >> stop; |
| 1132 | File = -shebang >> -Block >> stop; | 1134 | File = -shebang >> -Block >> plain_white >> stop; |
| 1133 | 1135 | ||
| 1134 | lax_line = advance_match >> ensure(*(not_(stop) >> any()), pop_indent) | line >> and_(stop) | check_indent_match >> *(not_(stop) >> any()); | 1136 | lax_line = advance_match >> ensure(*(not_(stop) >> any()), pop_indent) | line >> and_(stop) | check_indent_match >> *(not_(stop) >> any()); |
| 1135 | } | 1137 | } |
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: | |||
| 197 | NONE_AST_RULE(line_break); | 197 | NONE_AST_RULE(line_break); |
| 198 | NONE_AST_RULE(any_char); | 198 | NONE_AST_RULE(any_char); |
| 199 | NONE_AST_RULE(white); | 199 | NONE_AST_RULE(white); |
| 200 | NONE_AST_RULE(plain_white); | ||
| 200 | NONE_AST_RULE(stop); | 201 | NONE_AST_RULE(stop); |
| 201 | NONE_AST_RULE(comment); | 202 | NONE_AST_RULE(comment); |
| 202 | NONE_AST_RULE(multi_line_open); | 203 | NONE_AST_RULE(multi_line_open); |
| @@ -473,6 +474,7 @@ private: | |||
| 473 | AST_RULE(YueLineComment); | 474 | AST_RULE(YueLineComment); |
| 474 | AST_RULE(YueMultilineComment); | 475 | AST_RULE(YueMultilineComment); |
| 475 | AST_RULE(YueComment); | 476 | AST_RULE(YueComment); |
| 477 | AST_RULE(EmptyLine); | ||
| 476 | AST_RULE(ChainAssign); | 478 | AST_RULE(ChainAssign); |
| 477 | AST_RULE(Body); | 479 | AST_RULE(Body); |
| 478 | AST_RULE(Block); | 480 | 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) { | |||
| 348 | current.hasSep = true; | 348 | current.hasSep = true; |
| 349 | return false; | 349 | return false; |
| 350 | } | 350 | } |
| 351 | if (!reserveComment && yue::ast_is<yue::YueComment_t>(child)) { | 351 | if (!reserveComment && yue::ast_is<yue::YueComment_t, yue::EmptyLine_t>(child)) { |
| 352 | return false; | 352 | return false; |
| 353 | } | 353 | } |
| 354 | if (!current.children) { | 354 | if (!current.children) { |
