From 618b2be86e2d44a9151d708cf590dc3713410022 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Mon, 27 Feb 2023 12:01:38 +0800 Subject: fix issue #126 by reserving comments before statements --- src/yue.cpp | 3 +++ src/yuescript/yue_ast.h | 7 ++++++- src/yuescript/yue_compiler.cpp | 18 +++++++++++++++++- src/yuescript/yue_compiler.h | 2 ++ src/yuescript/yue_parser.cpp | 8 ++++---- src/yuescript/yue_parser.h | 2 +- src/yuescript/yuescript.cpp | 3 +++ 7 files changed, 36 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/yue.cpp b/src/yue.cpp index 7adde70..0b13688 100644 --- a/src/yue.cpp +++ b/src/yue.cpp @@ -254,6 +254,7 @@ int main(int narg, const char** args) { " -b Dump compile time (doesn't write output)\n" " -g Dump global variables used in NAME LINE COLUMN\n" " -l Write line numbers from source codes\n" + " -c Reserve comments before statement from source codes\n" #ifndef YUE_NO_WATCHER " -w path Watch changes and compile every file under directory\n" #endif // YUE_NO_WATCHER @@ -526,6 +527,8 @@ int main(int narg, const char** args) { config.useSpaceOverTab = true; } else if (arg == "-l"sv) { config.reserveLineNumber = true; + } else if (arg == "-c"sv) { + config.reserveComment = true; } else if (arg == "-p"sv) { writeToFile = false; } else if (arg == "-g"sv) { diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index b265ebc..47ddb0b 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -840,7 +840,12 @@ AST_END(StatementSep, "statement_sep"sv) AST_LEAF(YueLineComment) AST_END(YueLineComment, "comment"sv) -AST_LEAF(YueMultilineComment) +AST_LEAF(MultilineCommentInner) +AST_END(MultilineCommentInner, "comment"sv) + +AST_NODE(YueMultilineComment) + ast_ptr inner; + AST_MEMBER(YueMultilineComment, &inner) AST_END(YueMultilineComment, "comment"sv) AST_NODE(ChainAssign) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 0cf643f..52ed005 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -73,7 +73,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.15.26"sv; +const std::string_view version = "0.15.27"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -1160,6 +1160,22 @@ private: void transformStatement(Statement_t* statement, str_list& out) { auto x = statement; + if (_config.reserveComment && !x->comments.empty()) { + for (ast_node* node : x->comments.objects()) { + switch (node->getId()) { + case id(): { + auto comment = ast_cast(node); + out.push_back(indent() + "--"s + _parser.toString(comment) + '\n'); + break; + } + case id(): { + auto comment = ast_cast(node); + out.push_back(indent() + _parser.toString(comment) + '\n'); + break; + } + } + } + } if (statement->appendix) { if (auto assignment = assignmentFrom(statement)) { auto preDefine = getPreDefineLine(assignment); diff --git a/src/yuescript/yue_compiler.h b/src/yuescript/yue_compiler.h index c7a0e1b..a5d33cc 100644 --- a/src/yuescript/yue_compiler.h +++ b/src/yuescript/yue_compiler.h @@ -29,6 +29,8 @@ struct YueConfig { bool implicitReturnRoot = true; bool reserveLineNumber = true; bool useSpaceOverTab = false; + bool reserveComment = false; + bool exporting = false; bool profiling = false; int lineOffset = 0; diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index e70c4a0..78e6e52 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -798,12 +798,12 @@ YueParser::YueParser() { YueLineComment = *(not_(set("\r\n")) >> any_char); yue_line_comment = "--" >> YueLineComment >> and_(stop); - YueMultilineComment = multi_line_content; - yue_multiline_comment = multi_line_open >> YueMultilineComment >> multi_line_close; + MultilineCommentInner = multi_line_content; + YueMultilineComment = multi_line_open >> MultilineCommentInner >> multi_line_close; yue_comment = check_indent >> ( ( - yue_multiline_comment >> - *(set(" \t") | yue_multiline_comment) >> + YueMultilineComment >> + *(set(" \t") | YueMultilineComment) >> -yue_line_comment ) | yue_line_comment ) >> and_(line_break); diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index 8e7f555..cce6741 100644 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h @@ -245,7 +245,6 @@ private: NONE_AST_RULE(empty_line_break); NONE_AST_RULE(yue_comment); NONE_AST_RULE(yue_line_comment); - NONE_AST_RULE(yue_multiline_comment); NONE_AST_RULE(line); NONE_AST_RULE(shebang); @@ -389,6 +388,7 @@ private: AST_RULE(StatementSep) AST_RULE(Statement) AST_RULE(YueLineComment) + AST_RULE(MultilineCommentInner) AST_RULE(YueMultilineComment) AST_RULE(ChainAssign) AST_RULE(Body) diff --git a/src/yuescript/yuescript.cpp b/src/yuescript/yuescript.cpp index ed09aaa..3a17fa8 100644 --- a/src/yuescript/yuescript.cpp +++ b/src/yuescript/yuescript.cpp @@ -196,6 +196,9 @@ static int yuetoast(lua_State* L) { auto& current = stack.top(); int continuation = current.continuation; auto node = current.node; + if (auto comment = yue::ast_cast(node)) { + node = comment->inner.get(); + } switch (continuation) { case 0: { if (!current.children) { -- cgit v1.2.3-55-g6feb