diff options
author | Li Jin <dragon-fly@qq.com> | 2022-09-26 16:13:07 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2022-09-26 16:13:07 +0800 |
commit | 181708953ed4c13b3c0ff03752d50296b3e577ea (patch) | |
tree | 827f2053404cdb9dc63879b9581d66e72ebbd9a7 | |
parent | cb1fbac9223cc62739aa5c7bb97cba6e9dbd87c0 (diff) | |
download | yuescript-181708953ed4c13b3c0ff03752d50296b3e577ea.tar.gz yuescript-181708953ed4c13b3c0ff03752d50296b3e577ea.tar.bz2 yuescript-181708953ed4c13b3c0ff03752d50296b3e577ea.zip |
reserve comment in AST followed by statement.
-rwxr-xr-x | src/yuescript/yue_ast.h | 21 | ||||
-rwxr-xr-x | src/yuescript/yue_compiler.cpp | 6 | ||||
-rwxr-xr-x | src/yuescript/yue_parser.cpp | 33 | ||||
-rwxr-xr-x | src/yuescript/yue_parser.h | 5 | ||||
-rw-r--r-- | src/yuescript/yuescript.cpp | 20 |
5 files changed, 63 insertions, 22 deletions
diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index d8f6c95..1712e8a 100755 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h | |||
@@ -815,14 +815,25 @@ AST_END(statement_appendix, "statement_appendix"sv) | |||
815 | AST_LEAF(statement_sep) | 815 | AST_LEAF(statement_sep) |
816 | AST_END(statement_sep, "statement_sep"sv) | 816 | AST_END(statement_sep, "statement_sep"sv) |
817 | 817 | ||
818 | AST_LEAF(YueLineComment) | ||
819 | AST_END(YueLineComment, "comment"sv) | ||
820 | |||
821 | AST_LEAF(YueMultilineComment) | ||
822 | AST_END(YueMultilineComment, "comment"sv) | ||
823 | |||
818 | AST_NODE(Statement) | 824 | AST_NODE(Statement) |
819 | ast_sel<true, Import_t, While_t, Repeat_t, For_t, ForEach_t, | 825 | ast_ptr<true, Seperator_t> sep; |
820 | Return_t, Local_t, Global_t, Export_t, Macro_t, MacroInPlace_t, | 826 | ast_sel_list<false, YueLineComment_t, YueMultilineComment_t> comments; |
821 | BreakLoop_t, Label_t, Goto_t, ShortTabAppending_t, | 827 | ast_sel<true, |
822 | Backcall_t, LocalAttrib_t, PipeBody_t, ExpListAssign_t> content; | 828 | Import_t, While_t, Repeat_t, For_t, ForEach_t, |
829 | Return_t, Local_t, Global_t, Export_t, Macro_t, MacroInPlace_t, | ||
830 | BreakLoop_t, Label_t, Goto_t, ShortTabAppending_t, | ||
831 | BreakLoop_t, Label_t, Goto_t, ShortTabAppending_t, | ||
832 | Backcall_t, LocalAttrib_t, PipeBody_t, ExpListAssign_t | ||
833 | > content; | ||
823 | ast_ptr<false, statement_appendix_t> appendix; | 834 | ast_ptr<false, statement_appendix_t> appendix; |
824 | ast_ptr<false, statement_sep_t> needSep; | 835 | ast_ptr<false, statement_sep_t> needSep; |
825 | AST_MEMBER(Statement, &content, &appendix, &needSep) | 836 | AST_MEMBER(Statement, &sep, &comments, &content, &appendix, &needSep) |
826 | AST_END(Statement, "statement"sv) | 837 | AST_END(Statement, "statement"sv) |
827 | 838 | ||
828 | class Block_t; | 839 | class Block_t; |
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 9a9e2d6..1d949fe 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -59,7 +59,7 @@ namespace yue { | |||
59 | 59 | ||
60 | typedef std::list<std::string> str_list; | 60 | typedef std::list<std::string> str_list; |
61 | 61 | ||
62 | const std::string_view version = "0.15.2"sv; | 62 | const std::string_view version = "0.15.3"sv; |
63 | const std::string_view extension = "yue"sv; | 63 | const std::string_view extension = "yue"sv; |
64 | 64 | ||
65 | class YueCompilerImpl { | 65 | class YueCompilerImpl { |
@@ -109,8 +109,8 @@ public: | |||
109 | try { | 109 | try { |
110 | auto block = _info.node.to<File_t>()->block.get(); | 110 | auto block = _info.node.to<File_t>()->block.get(); |
111 | if (_info.exportMacro) { | 111 | if (_info.exportMacro) { |
112 | for (auto _stmt : block->statements.objects()) { | 112 | for (auto stmt_ : block->statements.objects()) { |
113 | auto stmt = static_cast<Statement_t*>(_stmt); | 113 | auto stmt = static_cast<Statement_t*>(stmt_); |
114 | switch (stmt->content->getId()) { | 114 | switch (stmt->content->getId()) { |
115 | case id<MacroInPlace_t>(): | 115 | case id<MacroInPlace_t>(): |
116 | case id<Macro_t>(): | 116 | case id<Macro_t>(): |
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 153babe..c5cae0b 100755 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp | |||
@@ -78,8 +78,8 @@ YueParser::YueParser() { | |||
78 | #define disable_chain(patt) (DisableChain >> ((patt) >> EnableChain | EnableChain >> Cut)) | 78 | #define disable_chain(patt) (DisableChain >> ((patt) >> EnableChain | EnableChain >> Cut)) |
79 | #define disable_do_chain_arg_table_block(patt) (DisableDoChainArgTableBlock >> ((patt) >> EnableDoChainArgTableBlock | EnableDoChainArgTableBlock >> Cut)) | 79 | #define disable_do_chain_arg_table_block(patt) (DisableDoChainArgTableBlock >> ((patt) >> EnableDoChainArgTableBlock | EnableDoChainArgTableBlock >> Cut)) |
80 | #define disable_arg_table_block(patt) (DisableArgTableBlock >> ((patt) >> EnableArgTableBlock | EnableArgTableBlock >> Cut)) | 80 | #define disable_arg_table_block(patt) (DisableArgTableBlock >> ((patt) >> EnableArgTableBlock | EnableArgTableBlock >> Cut)) |
81 | #define plain_body_with(str) (-(Space >> key(str)) >> InBlock | Space >> key(str) >> Statement) | 81 | #define plain_body_with(str) (-(Space >> key(str)) >> InBlock | Space >> key(str) >> Space >> Statement) |
82 | #define plain_body (InBlock | Statement) | 82 | #define plain_body (InBlock | Space >> Statement) |
83 | 83 | ||
84 | Variable = pl::user(Name, [](const item_t& item) { | 84 | Variable = pl::user(Name, [](const item_t& item) { |
85 | State* st = reinterpret_cast<State*>(item.user_data); | 85 | State* st = reinterpret_cast<State*>(item.user_data); |
@@ -177,7 +177,7 @@ YueParser::YueParser() { | |||
177 | return true; | 177 | return true; |
178 | }); | 178 | }); |
179 | 179 | ||
180 | InBlock = +SpaceBreak >> Advance >> ensure(Block, PopIndent); | 180 | InBlock = Space >> +(plain_space >> Break) >> Advance >> ensure(Block, PopIndent); |
181 | 181 | ||
182 | local_flag = expr('*') | expr('^'); | 182 | local_flag = expr('*') | expr('^'); |
183 | local_values = NameList >> -(sym('=') >> (TableBlock | ExpListLow)); | 183 | local_values = NameList >> -(sym('=') >> (TableBlock | ExpListLow)); |
@@ -503,7 +503,7 @@ YueParser::YueParser() { | |||
503 | -(+SpaceBreak >> Advance >> ensure(KeyValueList >> -sym(',') >> *(+SpaceBreak >> KeyValueLine), PopIndent))); | 503 | -(+SpaceBreak >> Advance >> ensure(KeyValueList >> -sym(',') >> *(+SpaceBreak >> KeyValueLine), PopIndent))); |
504 | 504 | ||
505 | class_member_list = Seperator >> KeyValue >> *(sym(',') >> KeyValue); | 505 | class_member_list = Seperator >> KeyValue >> *(sym(',') >> KeyValue); |
506 | ClassLine = CheckIndent >> (class_member_list | Statement) >> -sym(','); | 506 | ClassLine = CheckIndent >> (class_member_list | Space >> Statement) >> -sym(','); |
507 | ClassBlock = +SpaceBreak >> Advance >> Seperator >> ClassLine >> *(+SpaceBreak >> ClassLine) >> PopIndent; | 507 | ClassBlock = +SpaceBreak >> Advance >> Seperator >> ClassLine >> *(+SpaceBreak >> ClassLine) >> PopIndent; |
508 | 508 | ||
509 | ClassDecl = | 509 | ClassDecl = |
@@ -641,9 +641,15 @@ YueParser::YueParser() { | |||
641 | 641 | ||
642 | if_line = Space >> IfType >> IfCond; | 642 | if_line = Space >> IfType >> IfCond; |
643 | 643 | ||
644 | YueLineComment = *(not_(set("\r\n")) >> Any); | ||
645 | yue_line_comment = expr("--") >> YueLineComment >> and_(Stop); | ||
646 | YueMultilineComment = multi_line_content; | ||
647 | yue_multiline_comment = multi_line_open >> YueMultilineComment >> multi_line_close; | ||
648 | yue_comment = check_indent >> (yue_multiline_comment >> *(set(" \t") | yue_multiline_comment) >> -yue_line_comment | yue_line_comment) >> and_(Break); | ||
649 | |||
644 | statement_appendix = (if_line | CompInner) >> Space; | 650 | statement_appendix = (if_line | CompInner) >> Space; |
645 | statement_sep = and_(*SpaceBreak >> CheckIndent >> Space >> (set("($'\"") | expr("[[") | expr("[="))); | 651 | statement_sep = and_(*SpaceBreak >> CheckIndent >> Space >> (set("($'\"") | expr("[[") | expr("[="))); |
646 | Statement = Space >> ( | 652 | Statement = Seperator >> -(yue_comment >> *(Break >> yue_comment) >> Break >> CheckIndent) >> Space >> ( |
647 | Import | While | Repeat | For | ForEach | | 653 | Import | While | Repeat | For | ForEach | |
648 | Return | Local | Global | Export | Macro | | 654 | Return | Local | Global | Export | Macro | |
649 | MacroInPlace | BreakLoop | Label | Goto | ShortTabAppending | | 655 | MacroInPlace | BreakLoop | Label | Goto | ShortTabAppending | |
@@ -651,15 +657,22 @@ YueParser::YueParser() { | |||
651 | ) >> Space >> | 657 | ) >> Space >> |
652 | -statement_appendix >> -statement_sep; | 658 | -statement_appendix >> -statement_sep; |
653 | 659 | ||
654 | Body = InBlock | Statement; | 660 | Body = InBlock | Space >> Statement; |
661 | |||
662 | empty_line_stop = ( | ||
663 | check_indent >> (MultiLineComment >> Space | Comment) | | ||
664 | advance >> ensure(MultiLineComment >> Space | Comment, PopIndent) | | ||
665 | plain_space) >> and_(Stop); | ||
655 | 666 | ||
656 | empty_line_stop = Space >> and_(Break); | 667 | Line = |
657 | Line = and_(check_indent >> Space >> not_(PipeOperator)) >> Statement | Advance >> ensure(and_(Space >> PipeOperator) >> Statement, PopIndent) | empty_line_stop; | 668 | CheckIndent >> Statement | |
669 | Advance >> ensure(Space >> and_(PipeOperator) >> Statement, PopIndent) | | ||
670 | empty_line_stop; | ||
658 | Block = Seperator >> Line >> *(+Break >> Line); | 671 | Block = Seperator >> Line >> *(+Break >> Line); |
659 | 672 | ||
660 | Shebang = expr("#!") >> *(not_(Stop) >> Any); | 673 | Shebang = expr("#!") >> *(not_(Stop) >> Any); |
661 | BlockEnd = Block >> -(+Break >> Space >> and_(Stop)) >> Stop; | 674 | BlockEnd = Block >> Stop; |
662 | File = White >> -Shebang >> -Block >> White >> eof(); | 675 | File = -Shebang >> -Block >> Stop; |
663 | } | 676 | } |
664 | // clang-format on | 677 | // clang-format on |
665 | 678 | ||
diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index f83f1b9..7085d04 100755 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h | |||
@@ -196,6 +196,9 @@ private: | |||
196 | rule exp_not_tab; | 196 | rule exp_not_tab; |
197 | rule local_const_item; | 197 | rule local_const_item; |
198 | rule empty_line_stop; | 198 | rule empty_line_stop; |
199 | rule yue_comment; | ||
200 | rule yue_line_comment; | ||
201 | rule yue_multiline_comment; | ||
199 | rule Line; | 202 | rule Line; |
200 | rule Shebang; | 203 | rule Shebang; |
201 | 204 | ||
@@ -334,6 +337,8 @@ private: | |||
334 | AST_RULE(statement_appendix) | 337 | AST_RULE(statement_appendix) |
335 | AST_RULE(statement_sep) | 338 | AST_RULE(statement_sep) |
336 | AST_RULE(Statement) | 339 | AST_RULE(Statement) |
340 | AST_RULE(YueLineComment) | ||
341 | AST_RULE(YueMultilineComment) | ||
337 | AST_RULE(Body) | 342 | AST_RULE(Body) |
338 | AST_RULE(Block) | 343 | AST_RULE(Block) |
339 | AST_RULE(BlockEnd) | 344 | AST_RULE(BlockEnd) |
diff --git a/src/yuescript/yuescript.cpp b/src/yuescript/yuescript.cpp index 08978e5..06feb21 100644 --- a/src/yuescript/yuescript.cpp +++ b/src/yuescript/yuescript.cpp | |||
@@ -175,27 +175,39 @@ static int yuetoast(lua_State* L) { | |||
175 | }); | 175 | }); |
176 | switch (count) { | 176 | switch (count) { |
177 | case 0: { | 177 | case 0: { |
178 | lua_createtable(L, 2, 0); | 178 | lua_createtable(L, 4, 0); |
179 | getName(node); | 179 | getName(node); |
180 | lua_rawseti(L, -2, 1); | 180 | lua_rawseti(L, -2, 1); |
181 | lua_pushinteger(L, node->m_begin.m_line); | ||
182 | lua_rawseti(L, -2, 2); | ||
183 | lua_pushinteger(L, node->m_begin.m_col); | ||
184 | lua_rawseti(L, -2, 3); | ||
181 | auto str = parser.toString(node); | 185 | auto str = parser.toString(node); |
182 | yue::Utils::trim(str); | 186 | yue::Utils::trim(str); |
183 | lua_pushlstring(L, str.c_str(), str.length()); | 187 | lua_pushlstring(L, str.c_str(), str.length()); |
184 | lua_rawseti(L, -2, 2); | 188 | lua_rawseti(L, -2, 4); |
185 | break; | 189 | break; |
186 | } | 190 | } |
187 | case 1: { | 191 | case 1: { |
188 | if (flattenLevel > 1 || (flattenLevel == 1 && !hasSep)) { | 192 | if (flattenLevel > 1 || (flattenLevel == 1 && !hasSep)) { |
189 | getName(node); | 193 | getName(node); |
190 | lua_rawseti(L, -2, 1); | 194 | lua_rawseti(L, -2, 1); |
195 | lua_pushinteger(L, node->m_begin.m_line); | ||
196 | lua_rawseti(L, -2, 2); | ||
197 | lua_pushinteger(L, node->m_begin.m_col); | ||
198 | lua_rawseti(L, -2, 3); | ||
191 | break; | 199 | break; |
192 | } | 200 | } |
193 | } | 201 | } |
194 | default: { | 202 | default: { |
195 | lua_createtable(L, count + 1, 0); | 203 | lua_createtable(L, count + 3, 0); |
196 | getName(node); | 204 | getName(node); |
197 | lua_rawseti(L, -2, 1); | 205 | lua_rawseti(L, -2, 1); |
198 | for (int i = count, j = 2; i >= 1; i--, j++) { | 206 | lua_pushinteger(L, node->m_begin.m_line); |
207 | lua_rawseti(L, -2, 2); | ||
208 | lua_pushinteger(L, node->m_begin.m_col); | ||
209 | lua_rawseti(L, -2, 3); | ||
210 | for (int i = count, j = 4; i >= 1; i--, j++) { | ||
199 | lua_pushvalue(L, -1 - i); | 211 | lua_pushvalue(L, -1 - i); |
200 | lua_rawseti(L, -2, j); | 212 | lua_rawseti(L, -2, j); |
201 | } | 213 | } |