Skip to content

Commit b3aba79

Browse files
committed
Fixed empty line as block issue.
1 parent ec26010 commit b3aba79

8 files changed

Lines changed: 44 additions & 12 deletions

File tree

spec/inputs/syntax.yue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,5 +478,13 @@ do
478478
f2 = ->
479479
--
480480

481+
do
482+
return res if res ~= ""
483+
484+
485+
do
486+
return res if res ~= ""
487+
--
488+
481489
nil
482490

spec/outputs/syntax.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,15 @@ do
430430
local f2
431431
f2 = function() end
432432
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
433444
return nil

src/yuescript/yue_ast.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,9 @@ std::string Statement_t::to_string(void* ud) const {
16121612
std::string StatementSep_t::to_string(void*) const {
16131613
return {};
16141614
}
1615+
std::string EmptyLine_t::to_string(void*) const {
1616+
return {};
1617+
}
16151618
std::string ChainAssign_t::to_string(void* ud) const {
16161619
str_list temp;
16171620
for (auto exp : exprs.objects()) {
@@ -1635,11 +1638,11 @@ std::string Block_t::to_string(void* ud) const {
16351638
temp.emplace_back(info->ind() + stmt->to_string(ud));
16361639
}
16371640
} else if (info->reserveComment) {
1638-
auto comment = ast_to<YueComment_t>(stmt_);
1639-
if (comment->comment) {
1641+
if (auto comment = ast_cast<YueComment_t>(stmt_)) {
16401642
temp.emplace_back(info->ind() + comment->to_string(ud));
16411643
} 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));
16431646
}
16441647
}
16451648
}

src/yuescript/yue_ast.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,10 +933,13 @@ AST_LEAF(YueMultilineComment)
933933
AST_END(YueMultilineComment)
934934

935935
AST_NODE(YueComment)
936-
ast_sel<false, YueLineComment_t, YueMultilineComment_t> comment;
936+
ast_sel<true, YueLineComment_t, YueMultilineComment_t> comment;
937937
AST_MEMBER(YueComment, &comment)
938938
AST_END(YueComment)
939939

940+
AST_LEAF(EmptyLine)
941+
AST_END(EmptyLine)
942+
940943
AST_NODE(ChainAssign)
941944
ast_ptr<true, Seperator_t> sep;
942945
ast_list<true, Exp_t> exprs;
@@ -962,7 +965,7 @@ AST_END(Body)
962965

963966
AST_NODE(Block)
964967
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;
966969
AST_MEMBER(Block, &sep, &statementOrComments)
967970
AST_END(Block)
968971

src/yuescript/yue_compiler.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static std::unordered_set<std::string> Metamethods = {
7878
"close"s // Lua 5.4
7979
};
8080

81-
const std::string_view version = "0.30.1"sv;
81+
const std::string_view version = "0.30.2"sv;
8282
const std::string_view extension = "yue"sv;
8383

8484
class CompileError : public std::logic_error {
@@ -5290,6 +5290,9 @@ class YueCompilerImpl {
52905290
transformComment(comment, temp);
52915291
continue;
52925292
}
5293+
if (!ast_is<Statement_t>(node)) {
5294+
continue;
5295+
}
52935296
auto transformNode = [&]() {
52945297
currentScope().lastStatement = (node == lastStmt) && currentScope().mode == GlobalMode::None;
52955298
transformStatement(static_cast<Statement_t*>(node), temp);

src/yuescript/yue_parser.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ YueParser::YueParser() {
6666
space = -(and_(set(" \t-\\")) >> *space_one >> -comment);
6767
space_break = space >> line_break;
6868
white = space >> *(line_break >> space);
69+
plain_white = plain_space >> *(line_break >> plain_space);
6970
alpha_num = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_';
7071
not_alpha_num = not_(alpha_num);
7172
Name = (range('a', 'z') | range('A', 'Z') | '_') >> *alpha_num >> not_(larger(255));
@@ -1109,15 +1110,16 @@ YueParser::YueParser() {
11091110
yue_line_comment;
11101111
YueComment =
11111112
check_indent >> comment_line >> and_(stop) |
1112-
advance >> ensure(comment_line, pop_indent) >> and_(stop) |
1113-
plain_space >> and_(stop);
1113+
advance >> ensure(comment_line, pop_indent) >> and_(stop);
1114+
1115+
EmptyLine = plain_space >> and_(stop);
11141116

11151117
indentation_error = pl::user(not_(pipe_operator | eof()), [](const item_t& item) {
11161118
RaiseError("unexpected indent"sv, item);
11171119
return false;
11181120
});
11191121

1120-
line = (
1122+
line = *(EmptyLine >> line_break) >> (
11211123
check_indent_match >> space >> Statement |
11221124
YueComment |
11231125
advance_match >> ensure(space >> (indentation_error | Statement), pop_indent)
@@ -1128,8 +1130,8 @@ YueParser::YueParser() {
11281130
}) >> lax_line >> *(line_break >> lax_line) | line >> *(line_break >> line));
11291131

11301132
shebang = "#!" >> *(not_(stop) >> any_char);
1131-
BlockEnd = Block >> stop;
1132-
File = -shebang >> -Block >> stop;
1133+
BlockEnd = Block >> plain_white >> stop;
1134+
File = -shebang >> -Block >> plain_white >> stop;
11331135

11341136
lax_line = advance_match >> ensure(*(not_(stop) >> any()), pop_indent) | line >> and_(stop) | check_indent_match >> *(not_(stop) >> any());
11351137
}

src/yuescript/yue_parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ class YueParser {
197197
NONE_AST_RULE(line_break);
198198
NONE_AST_RULE(any_char);
199199
NONE_AST_RULE(white);
200+
NONE_AST_RULE(plain_white);
200201
NONE_AST_RULE(stop);
201202
NONE_AST_RULE(comment);
202203
NONE_AST_RULE(multi_line_open);
@@ -473,6 +474,7 @@ class YueParser {
473474
AST_RULE(YueLineComment);
474475
AST_RULE(YueMultilineComment);
475476
AST_RULE(YueComment);
477+
AST_RULE(EmptyLine);
476478
AST_RULE(ChainAssign);
477479
AST_RULE(Body);
478480
AST_RULE(Block);

src/yuescript/yuescript.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ static int yuetoast(lua_State* L) {
348348
current.hasSep = true;
349349
return false;
350350
}
351-
if (!reserveComment && yue::ast_is<yue::YueComment_t>(child)) {
351+
if (!reserveComment && yue::ast_is<yue::YueComment_t, yue::EmptyLine_t>(child)) {
352352
return false;
353353
}
354354
if (!current.children) {

0 commit comments

Comments
 (0)