diff options
author | Li Jin <dragon-fly@qq.com> | 2023-02-27 12:01:38 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2023-02-27 12:01:38 +0800 |
commit | 618b2be86e2d44a9151d708cf590dc3713410022 (patch) | |
tree | 786417df432c3b44a53f25f2226c4ad713b4cd00 | |
parent | 78f07cc7bf7e310cd559c45c6c7a4977341c107e (diff) | |
download | yuescript-618b2be86e2d44a9151d708cf590dc3713410022.tar.gz yuescript-618b2be86e2d44a9151d708cf590dc3713410022.tar.bz2 yuescript-618b2be86e2d44a9151d708cf590dc3713410022.zip |
fix issue #126 by reserving comments before statements
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | src/yue.cpp | 3 | ||||
-rw-r--r-- | src/yuescript/yue_ast.h | 7 | ||||
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 18 | ||||
-rw-r--r-- | src/yuescript/yue_compiler.h | 2 | ||||
-rw-r--r-- | src/yuescript/yue_parser.cpp | 8 | ||||
-rw-r--r-- | src/yuescript/yue_parser.h | 2 | ||||
-rw-r--r-- | src/yuescript/yuescript.cpp | 3 |
8 files changed, 37 insertions, 7 deletions
@@ -95,6 +95,7 @@ Usage: yue [options|files|directories] ... | |||
95 | -b Dump compile time (doesn't write output) | 95 | -b Dump compile time (doesn't write output) |
96 | -g Dump global variables used in NAME LINE COLUMN | 96 | -g Dump global variables used in NAME LINE COLUMN |
97 | -l Write line numbers from source codes | 97 | -l Write line numbers from source codes |
98 | -c Reserve comments before statement from source codes | ||
98 | -w path Watch changes and compile every file under directory | 99 | -w path Watch changes and compile every file under directory |
99 | -v Print version | 100 | -v Print version |
100 | -- Read from standard in, print to standard out | 101 | -- Read from standard in, print to standard out |
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) { | |||
254 | " -b Dump compile time (doesn't write output)\n" | 254 | " -b Dump compile time (doesn't write output)\n" |
255 | " -g Dump global variables used in NAME LINE COLUMN\n" | 255 | " -g Dump global variables used in NAME LINE COLUMN\n" |
256 | " -l Write line numbers from source codes\n" | 256 | " -l Write line numbers from source codes\n" |
257 | " -c Reserve comments before statement from source codes\n" | ||
257 | #ifndef YUE_NO_WATCHER | 258 | #ifndef YUE_NO_WATCHER |
258 | " -w path Watch changes and compile every file under directory\n" | 259 | " -w path Watch changes and compile every file under directory\n" |
259 | #endif // YUE_NO_WATCHER | 260 | #endif // YUE_NO_WATCHER |
@@ -526,6 +527,8 @@ int main(int narg, const char** args) { | |||
526 | config.useSpaceOverTab = true; | 527 | config.useSpaceOverTab = true; |
527 | } else if (arg == "-l"sv) { | 528 | } else if (arg == "-l"sv) { |
528 | config.reserveLineNumber = true; | 529 | config.reserveLineNumber = true; |
530 | } else if (arg == "-c"sv) { | ||
531 | config.reserveComment = true; | ||
529 | } else if (arg == "-p"sv) { | 532 | } else if (arg == "-p"sv) { |
530 | writeToFile = false; | 533 | writeToFile = false; |
531 | } else if (arg == "-g"sv) { | 534 | } 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) | |||
840 | AST_LEAF(YueLineComment) | 840 | AST_LEAF(YueLineComment) |
841 | AST_END(YueLineComment, "comment"sv) | 841 | AST_END(YueLineComment, "comment"sv) |
842 | 842 | ||
843 | AST_LEAF(YueMultilineComment) | 843 | AST_LEAF(MultilineCommentInner) |
844 | AST_END(MultilineCommentInner, "comment"sv) | ||
845 | |||
846 | AST_NODE(YueMultilineComment) | ||
847 | ast_ptr<true, MultilineCommentInner_t> inner; | ||
848 | AST_MEMBER(YueMultilineComment, &inner) | ||
844 | AST_END(YueMultilineComment, "comment"sv) | 849 | AST_END(YueMultilineComment, "comment"sv) |
845 | 850 | ||
846 | AST_NODE(ChainAssign) | 851 | 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<std::string> Metamethods = { | |||
73 | "close"s // Lua 5.4 | 73 | "close"s // Lua 5.4 |
74 | }; | 74 | }; |
75 | 75 | ||
76 | const std::string_view version = "0.15.26"sv; | 76 | const std::string_view version = "0.15.27"sv; |
77 | const std::string_view extension = "yue"sv; | 77 | const std::string_view extension = "yue"sv; |
78 | 78 | ||
79 | class YueCompilerImpl { | 79 | class YueCompilerImpl { |
@@ -1160,6 +1160,22 @@ private: | |||
1160 | 1160 | ||
1161 | void transformStatement(Statement_t* statement, str_list& out) { | 1161 | void transformStatement(Statement_t* statement, str_list& out) { |
1162 | auto x = statement; | 1162 | auto x = statement; |
1163 | if (_config.reserveComment && !x->comments.empty()) { | ||
1164 | for (ast_node* node : x->comments.objects()) { | ||
1165 | switch (node->getId()) { | ||
1166 | case id<YueLineComment_t>(): { | ||
1167 | auto comment = ast_cast<YueLineComment_t>(node); | ||
1168 | out.push_back(indent() + "--"s + _parser.toString(comment) + '\n'); | ||
1169 | break; | ||
1170 | } | ||
1171 | case id<YueMultilineComment_t>(): { | ||
1172 | auto comment = ast_cast<YueMultilineComment_t>(node); | ||
1173 | out.push_back(indent() + _parser.toString(comment) + '\n'); | ||
1174 | break; | ||
1175 | } | ||
1176 | } | ||
1177 | } | ||
1178 | } | ||
1163 | if (statement->appendix) { | 1179 | if (statement->appendix) { |
1164 | if (auto assignment = assignmentFrom(statement)) { | 1180 | if (auto assignment = assignmentFrom(statement)) { |
1165 | auto preDefine = getPreDefineLine(assignment); | 1181 | 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 { | |||
29 | bool implicitReturnRoot = true; | 29 | bool implicitReturnRoot = true; |
30 | bool reserveLineNumber = true; | 30 | bool reserveLineNumber = true; |
31 | bool useSpaceOverTab = false; | 31 | bool useSpaceOverTab = false; |
32 | bool reserveComment = false; | ||
33 | |||
32 | bool exporting = false; | 34 | bool exporting = false; |
33 | bool profiling = false; | 35 | bool profiling = false; |
34 | int lineOffset = 0; | 36 | 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() { | |||
798 | 798 | ||
799 | YueLineComment = *(not_(set("\r\n")) >> any_char); | 799 | YueLineComment = *(not_(set("\r\n")) >> any_char); |
800 | yue_line_comment = "--" >> YueLineComment >> and_(stop); | 800 | yue_line_comment = "--" >> YueLineComment >> and_(stop); |
801 | YueMultilineComment = multi_line_content; | 801 | MultilineCommentInner = multi_line_content; |
802 | yue_multiline_comment = multi_line_open >> YueMultilineComment >> multi_line_close; | 802 | YueMultilineComment = multi_line_open >> MultilineCommentInner >> multi_line_close; |
803 | yue_comment = check_indent >> ( | 803 | yue_comment = check_indent >> ( |
804 | ( | 804 | ( |
805 | yue_multiline_comment >> | 805 | YueMultilineComment >> |
806 | *(set(" \t") | yue_multiline_comment) >> | 806 | *(set(" \t") | YueMultilineComment) >> |
807 | -yue_line_comment | 807 | -yue_line_comment |
808 | ) | yue_line_comment | 808 | ) | yue_line_comment |
809 | ) >> and_(line_break); | 809 | ) >> 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: | |||
245 | NONE_AST_RULE(empty_line_break); | 245 | NONE_AST_RULE(empty_line_break); |
246 | NONE_AST_RULE(yue_comment); | 246 | NONE_AST_RULE(yue_comment); |
247 | NONE_AST_RULE(yue_line_comment); | 247 | NONE_AST_RULE(yue_line_comment); |
248 | NONE_AST_RULE(yue_multiline_comment); | ||
249 | NONE_AST_RULE(line); | 248 | NONE_AST_RULE(line); |
250 | NONE_AST_RULE(shebang); | 249 | NONE_AST_RULE(shebang); |
251 | 250 | ||
@@ -389,6 +388,7 @@ private: | |||
389 | AST_RULE(StatementSep) | 388 | AST_RULE(StatementSep) |
390 | AST_RULE(Statement) | 389 | AST_RULE(Statement) |
391 | AST_RULE(YueLineComment) | 390 | AST_RULE(YueLineComment) |
391 | AST_RULE(MultilineCommentInner) | ||
392 | AST_RULE(YueMultilineComment) | 392 | AST_RULE(YueMultilineComment) |
393 | AST_RULE(ChainAssign) | 393 | AST_RULE(ChainAssign) |
394 | AST_RULE(Body) | 394 | 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) { | |||
196 | auto& current = stack.top(); | 196 | auto& current = stack.top(); |
197 | int continuation = current.continuation; | 197 | int continuation = current.continuation; |
198 | auto node = current.node; | 198 | auto node = current.node; |
199 | if (auto comment = yue::ast_cast<yue::YueMultilineComment_t>(node)) { | ||
200 | node = comment->inner.get(); | ||
201 | } | ||
199 | switch (continuation) { | 202 | switch (continuation) { |
200 | case 0: { | 203 | case 0: { |
201 | if (!current.children) { | 204 | if (!current.children) { |