@@ -203,9 +203,16 @@ std::string YueLineComment_t::to_string(void* ud) const {
203203 auto info = reinterpret_cast <YueFormat*>(ud);
204204 return " --" s + info->convert (this );
205205}
206- std::string MultilineCommentInner_t ::to_string (void * ud) const {
206+ std::string YueMultilineComment_t ::to_string (void * ud) const {
207207 auto info = reinterpret_cast <YueFormat*>(ud);
208- return info->convert (this );
208+ return " --[[" s + info->convert (this ) + " ]]" s;
209+ }
210+ std::string YueComment_t::to_string (void * ud) const {
211+ if (comment) {
212+ return comment->to_string (ud);
213+ } else {
214+ return {};
215+ }
209216}
210217std::string Variable_t::to_string (void * ud) const {
211218 return name->to_string (ud);
@@ -1596,38 +1603,15 @@ std::string StatementAppendix_t::to_string(void* ud) const {
15961603 return item->to_string (ud);
15971604}
15981605std::string Statement_t::to_string (void * ud) const {
1599- std::string line;
1600- if (!comments.empty ()) {
1601- auto info = reinterpret_cast <YueFormat*>(ud);
1602- str_list temp;
1603- for (ast_node* comment : comments.objects ()) {
1604- if (comment == comments.front ()) {
1605- temp.push_back (comment->to_string (ud));
1606- } else {
1607- temp.push_back (info->ind () + comment->to_string (ud));
1608- }
1609- }
1610- if (appendix) {
1611- temp.push_back (info->ind () + content->to_string (ud) + ' ' + appendix->to_string (ud));
1612- return join (temp, " \n " sv);
1613- } else {
1614- temp.push_back (info->ind () + content->to_string (ud));
1615- return join (temp, " \n " sv);
1616- }
1606+ if (appendix) {
1607+ return content->to_string (ud) + ' ' + appendix->to_string (ud);
16171608 } else {
1618- if (appendix) {
1619- return content->to_string (ud) + ' ' + appendix->to_string (ud);
1620- } else {
1621- return content->to_string (ud);
1622- }
1609+ return content->to_string (ud);
16231610 }
16241611}
16251612std::string StatementSep_t::to_string (void *) const {
16261613 return {};
16271614}
1628- std::string YueMultilineComment_t::to_string (void * ud) const {
1629- return " --[[" s + inner->to_string (ud) + " ]]" s;
1630- }
16311615std::string ChainAssign_t::to_string (void * ud) const {
16321616 str_list temp;
16331617 for (auto exp : exprs.objects ()) {
@@ -1641,14 +1625,22 @@ std::string Body_t::to_string(void* ud) const {
16411625std::string Block_t::to_string (void * ud) const {
16421626 auto info = reinterpret_cast <YueFormat*>(ud);
16431627 str_list temp;
1644- for (auto stmt_ : statements.objects ()) {
1645- auto stmt = static_cast <Statement_t*>(stmt_);
1646- if (stmt->content .is <PipeBody_t>()) {
1647- info->pushScope ();
1648- temp.emplace_back (stmt->to_string (ud));
1649- info->popScope ();
1650- } else {
1651- temp.emplace_back (info->ind () + stmt->to_string (ud));
1628+ for (auto stmt_ : statementOrComments.objects ()) {
1629+ if (auto stmt = ast_cast<Statement_t>(stmt_)) {
1630+ if (stmt->content .is <PipeBody_t>()) {
1631+ info->pushScope ();
1632+ temp.emplace_back (stmt->to_string (ud));
1633+ info->popScope ();
1634+ } else {
1635+ temp.emplace_back (info->ind () + stmt->to_string (ud));
1636+ }
1637+ } else if (info->reserveComment ) {
1638+ auto comment = ast_to<YueComment_t>(stmt_);
1639+ if (comment->comment ) {
1640+ temp.emplace_back (info->ind () + comment->to_string (ud));
1641+ } else {
1642+ temp.emplace_back (comment->to_string (ud));
1643+ }
16521644 }
16531645 }
16541646 return join (temp, " \n " sv);
0 commit comments