From 181708953ed4c13b3c0ff03752d50296b3e577ea Mon Sep 17 00:00:00 2001 From: Li Jin Date: Mon, 26 Sep 2022 16:13:07 +0800 Subject: reserve comment in AST followed by statement. --- src/yuescript/yue_ast.h | 21 ++++++++++++++++----- src/yuescript/yue_compiler.cpp | 6 +++--- src/yuescript/yue_parser.cpp | 33 +++++++++++++++++++++++---------- src/yuescript/yue_parser.h | 5 +++++ src/yuescript/yuescript.cpp | 20 ++++++++++++++++---- 5 files changed, 63 insertions(+), 22 deletions(-) (limited to 'src') 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) AST_LEAF(statement_sep) AST_END(statement_sep, "statement_sep"sv) +AST_LEAF(YueLineComment) +AST_END(YueLineComment, "comment"sv) + +AST_LEAF(YueMultilineComment) +AST_END(YueMultilineComment, "comment"sv) + AST_NODE(Statement) - ast_sel content; + ast_ptr sep; + ast_sel_list comments; + ast_sel content; ast_ptr appendix; ast_ptr needSep; - AST_MEMBER(Statement, &content, &appendix, &needSep) + AST_MEMBER(Statement, &sep, &comments, &content, &appendix, &needSep) AST_END(Statement, "statement"sv) 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 { typedef std::list str_list; -const std::string_view version = "0.15.2"sv; +const std::string_view version = "0.15.3"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -109,8 +109,8 @@ public: try { auto block = _info.node.to()->block.get(); if (_info.exportMacro) { - for (auto _stmt : block->statements.objects()) { - auto stmt = static_cast(_stmt); + for (auto stmt_ : block->statements.objects()) { + auto stmt = static_cast(stmt_); switch (stmt->content->getId()) { case id(): case id(): 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() { #define disable_chain(patt) (DisableChain >> ((patt) >> EnableChain | EnableChain >> Cut)) #define disable_do_chain_arg_table_block(patt) (DisableDoChainArgTableBlock >> ((patt) >> EnableDoChainArgTableBlock | EnableDoChainArgTableBlock >> Cut)) #define disable_arg_table_block(patt) (DisableArgTableBlock >> ((patt) >> EnableArgTableBlock | EnableArgTableBlock >> Cut)) - #define plain_body_with(str) (-(Space >> key(str)) >> InBlock | Space >> key(str) >> Statement) - #define plain_body (InBlock | Statement) + #define plain_body_with(str) (-(Space >> key(str)) >> InBlock | Space >> key(str) >> Space >> Statement) + #define plain_body (InBlock | Space >> Statement) Variable = pl::user(Name, [](const item_t& item) { State* st = reinterpret_cast(item.user_data); @@ -177,7 +177,7 @@ YueParser::YueParser() { return true; }); - InBlock = +SpaceBreak >> Advance >> ensure(Block, PopIndent); + InBlock = Space >> +(plain_space >> Break) >> Advance >> ensure(Block, PopIndent); local_flag = expr('*') | expr('^'); local_values = NameList >> -(sym('=') >> (TableBlock | ExpListLow)); @@ -503,7 +503,7 @@ YueParser::YueParser() { -(+SpaceBreak >> Advance >> ensure(KeyValueList >> -sym(',') >> *(+SpaceBreak >> KeyValueLine), PopIndent))); class_member_list = Seperator >> KeyValue >> *(sym(',') >> KeyValue); - ClassLine = CheckIndent >> (class_member_list | Statement) >> -sym(','); + ClassLine = CheckIndent >> (class_member_list | Space >> Statement) >> -sym(','); ClassBlock = +SpaceBreak >> Advance >> Seperator >> ClassLine >> *(+SpaceBreak >> ClassLine) >> PopIndent; ClassDecl = @@ -641,9 +641,15 @@ YueParser::YueParser() { if_line = Space >> IfType >> IfCond; + YueLineComment = *(not_(set("\r\n")) >> Any); + yue_line_comment = expr("--") >> YueLineComment >> and_(Stop); + YueMultilineComment = multi_line_content; + yue_multiline_comment = multi_line_open >> YueMultilineComment >> multi_line_close; + yue_comment = check_indent >> (yue_multiline_comment >> *(set(" \t") | yue_multiline_comment) >> -yue_line_comment | yue_line_comment) >> and_(Break); + statement_appendix = (if_line | CompInner) >> Space; statement_sep = and_(*SpaceBreak >> CheckIndent >> Space >> (set("($'\"") | expr("[[") | expr("[="))); - Statement = Space >> ( + Statement = Seperator >> -(yue_comment >> *(Break >> yue_comment) >> Break >> CheckIndent) >> Space >> ( Import | While | Repeat | For | ForEach | Return | Local | Global | Export | Macro | MacroInPlace | BreakLoop | Label | Goto | ShortTabAppending | @@ -651,15 +657,22 @@ YueParser::YueParser() { ) >> Space >> -statement_appendix >> -statement_sep; - Body = InBlock | Statement; + Body = InBlock | Space >> Statement; + + empty_line_stop = ( + check_indent >> (MultiLineComment >> Space | Comment) | + advance >> ensure(MultiLineComment >> Space | Comment, PopIndent) | + plain_space) >> and_(Stop); - empty_line_stop = Space >> and_(Break); - Line = and_(check_indent >> Space >> not_(PipeOperator)) >> Statement | Advance >> ensure(and_(Space >> PipeOperator) >> Statement, PopIndent) | empty_line_stop; + Line = + CheckIndent >> Statement | + Advance >> ensure(Space >> and_(PipeOperator) >> Statement, PopIndent) | + empty_line_stop; Block = Seperator >> Line >> *(+Break >> Line); Shebang = expr("#!") >> *(not_(Stop) >> Any); - BlockEnd = Block >> -(+Break >> Space >> and_(Stop)) >> Stop; - File = White >> -Shebang >> -Block >> White >> eof(); + BlockEnd = Block >> Stop; + File = -Shebang >> -Block >> Stop; } // clang-format on 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: rule exp_not_tab; rule local_const_item; rule empty_line_stop; + rule yue_comment; + rule yue_line_comment; + rule yue_multiline_comment; rule Line; rule Shebang; @@ -334,6 +337,8 @@ private: AST_RULE(statement_appendix) AST_RULE(statement_sep) AST_RULE(Statement) + AST_RULE(YueLineComment) + AST_RULE(YueMultilineComment) AST_RULE(Body) AST_RULE(Block) 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) { }); switch (count) { case 0: { - lua_createtable(L, 2, 0); + lua_createtable(L, 4, 0); getName(node); lua_rawseti(L, -2, 1); + lua_pushinteger(L, node->m_begin.m_line); + lua_rawseti(L, -2, 2); + lua_pushinteger(L, node->m_begin.m_col); + lua_rawseti(L, -2, 3); auto str = parser.toString(node); yue::Utils::trim(str); lua_pushlstring(L, str.c_str(), str.length()); - lua_rawseti(L, -2, 2); + lua_rawseti(L, -2, 4); break; } case 1: { if (flattenLevel > 1 || (flattenLevel == 1 && !hasSep)) { getName(node); lua_rawseti(L, -2, 1); + lua_pushinteger(L, node->m_begin.m_line); + lua_rawseti(L, -2, 2); + lua_pushinteger(L, node->m_begin.m_col); + lua_rawseti(L, -2, 3); break; } } default: { - lua_createtable(L, count + 1, 0); + lua_createtable(L, count + 3, 0); getName(node); lua_rawseti(L, -2, 1); - for (int i = count, j = 2; i >= 1; i--, j++) { + lua_pushinteger(L, node->m_begin.m_line); + lua_rawseti(L, -2, 2); + lua_pushinteger(L, node->m_begin.m_col); + lua_rawseti(L, -2, 3); + for (int i = count, j = 4; i >= 1; i--, j++) { lua_pushvalue(L, -1 - i); lua_rawseti(L, -2, j); } -- cgit v1.2.3-55-g6feb