Skip to content

Commit ec26010

Browse files
committed
Updated reserved comments function.
1 parent 1fea444 commit ec26010

10 files changed

Lines changed: 635 additions & 509 deletions

File tree

doc/docs/doc/README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4729,7 +4729,7 @@ Converts the code to the AST.
47294729

47304730
**Signature:**
47314731
```lua
4732-
to_ast: function(code: string, flattenLevel?: number, astName?: string):
4732+
to_ast: function(code: string, flattenLevel?: number, astName?: string, reserveComment?: boolean):
47334733
--[[AST]] AST | nil,
47344734
--[[error]] nil | string
47354735
```
@@ -4741,6 +4741,7 @@ to_ast: function(code: string, flattenLevel?: number, astName?: string):
47414741
| code | string | The code. |
47424742
| flattenLevel | integer | [Optional] The flatten level. Higher level means more flattening. Default is 0. Maximum is 2. |
47434743
| astName | string | [Optional] The AST name. Default is "File". |
4744+
| reserveComment | boolean | [Optional] Whether to reserve the original comments. Default is false. |
47444745

47454746
**Returns:**
47464747

@@ -4749,6 +4750,33 @@ to_ast: function(code: string, flattenLevel?: number, astName?: string):
47494750
| AST \| nil | The AST, or nil if the conversion failed. |
47504751
| string \| nil | The error message, or nil if the conversion succeeded. |
47514752

4753+
#### format
4754+
4755+
**Type:** Function.
4756+
4757+
**Description:**
4758+
4759+
Formats the YueScript code.
4760+
4761+
**Signature:**
4762+
```lua
4763+
format: function(code: string, tabSize?: number, reserveComment?: boolean): string
4764+
```
4765+
4766+
**Parameters:**
4767+
4768+
| Parameter | Type | Description |
4769+
| --- | --- | --- |
4770+
| code | string | The code. |
4771+
| tabSize | integer | [Optional] The tab size. Default is 4. |
4772+
| reserveComment | boolean | [Optional] Whether to reserve the original comments. Default is true. |
4773+
4774+
**Returns:**
4775+
4776+
| Return Type | Description |
4777+
| --- | --- |
4778+
| string | The formatted code. |
4779+
47524780
#### __call
47534781

47544782
**Type:** Metamethod.
@@ -4820,6 +4848,19 @@ Whether the compiler should reserve the original line number in the compiled cod
48204848
reserve_line_number: boolean
48214849
```
48224850

4851+
#### reserve_comment
4852+
4853+
**Type:** Field.
4854+
4855+
**Description:**
4856+
4857+
Whether the compiler should reserve the original comments in the compiled code.
4858+
4859+
**Signature:**
4860+
```lua
4861+
reserve_comment: boolean
4862+
```
4863+
48234864
#### space_over_tab
48244865

48254866
**Type:** Field.

doc/docs/zh/doc/README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4686,7 +4686,7 @@ type AST = {string, integer, integer, any}
46864686

46874687
**签名:**
46884688
```lua
4689-
to_ast: function(code: string, flattenLevel?: number, astName?: string):
4689+
to_ast: function(code: string, flattenLevel?: number, astName?: string, reserveComment?: boolean):
46904690
--[[AST]] AST | nil,
46914691
--[[error]] nil | string
46924692
```
@@ -4697,6 +4697,42 @@ to_ast: function(code: string, flattenLevel?: number, astName?: string):
46974697
| --- | --- | --- |
46984698
| code | string | 代码。 |
46994699
| flattenLevel | integer | [可选] 扁平化级别。级别越高,会消除更多的 AST 结构的嵌套。默认为 0。最大为 2。 |
4700+
| astName | string | [可选] AST 名称。默认为 "File"。 |
4701+
| reserveComment | boolean | [可选] 是否保留原始注释。默认为 false。 |
4702+
4703+
**返回值:**
4704+
4705+
| 返回类型 | 描述 |
4706+
| --- | --- |
4707+
| AST \| nil | AST,如果转换失败则为 nil。 |
4708+
| string \| nil | 错误消息,如果转换成功则为 nil。 |
4709+
4710+
#### format
4711+
4712+
**类型:** 函数。
4713+
4714+
**描述:**
4715+
4716+
格式化 YueScript 代码。
4717+
4718+
**签名:**
4719+
```lua
4720+
format: function(code: string, tabSize?: number, reserveComment?: boolean): string
4721+
```
4722+
4723+
**参数:**
4724+
4725+
| 参数名 | 类型 | 描述 |
4726+
| --- | --- | --- |
4727+
| code | string | 代码。 |
4728+
| tabSize | integer | [可选] 制表符大小。默认为 4。 |
4729+
| reserveComment | boolean | [可选] 是否保留原始注释。默认为 true。 |
4730+
4731+
**返回值:**
4732+
4733+
| 返回类型 | 描述 |
4734+
| --- | --- |
4735+
| string | 格式化后的代码。 |
47004736

47014737
#### __call
47024738

@@ -4769,6 +4805,19 @@ implicit_return_root: boolean
47694805
reserve_line_number: boolean
47704806
```
47714807

4808+
#### reserve_comment
4809+
4810+
**类型:** 成员变量。
4811+
4812+
**描述:**
4813+
4814+
编译器是否应该在编译后的代码中保留原始注释。
4815+
4816+
**签名:**
4817+
```lua
4818+
reserve_comment: boolean
4819+
```
4820+
47724821
#### space_over_tab
47734822

47744823
**类型:** 成员变量。

spec/inputs/test/format_spec.yue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ for file in *files
119119
original_ast = yue.to_ast code
120120
assert.is_not_nil original_ast
121121
rewriteLineCol original_ast
122-
formated = yue.format code
122+
formated = yue.format code, 0, false
123123
ast = yue.to_ast formated
124124
assert.is_not_nil ast
125125
rewriteLineCol ast

spec/outputs/test/format_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ return describe("format", function()
122122
local original_ast = yue.to_ast(code)
123123
assert.is_not_nil(original_ast)
124124
rewriteLineCol(original_ast)
125-
local formated = yue.format(code)
125+
local formated = yue.format(code, 0, false)
126126
local ast = yue.to_ast(formated)
127127
assert.is_not_nil(ast)
128128
rewriteLineCol(ast)

src/yuescript/yue_ast.cpp

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
210217
std::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
}
15981605
std::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
}
16251612
std::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-
}
16311615
std::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 {
16411625
std::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);

src/yuescript/yue_ast.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -929,14 +929,14 @@ AST_END(StatementSep)
929929
AST_LEAF(YueLineComment)
930930
AST_END(YueLineComment)
931931

932-
AST_LEAF(MultilineCommentInner)
933-
AST_END(MultilineCommentInner)
934-
935-
AST_NODE(YueMultilineComment)
936-
ast_ptr<true, MultilineCommentInner_t> inner;
937-
AST_MEMBER(YueMultilineComment, &inner)
932+
AST_LEAF(YueMultilineComment)
938933
AST_END(YueMultilineComment)
939934

935+
AST_NODE(YueComment)
936+
ast_sel<false, YueLineComment_t, YueMultilineComment_t> comment;
937+
AST_MEMBER(YueComment, &comment)
938+
AST_END(YueComment)
939+
940940
AST_NODE(ChainAssign)
941941
ast_ptr<true, Seperator_t> sep;
942942
ast_list<true, Exp_t> exprs;
@@ -945,16 +945,14 @@ AST_NODE(ChainAssign)
945945
AST_END(ChainAssign)
946946

947947
AST_NODE(Statement)
948-
ast_ptr<true, Seperator_t> sep;
949-
ast_sel_list<false, YueLineComment_t, YueMultilineComment_t> comments;
950948
ast_sel<true,
951949
Import_t, While_t, Repeat_t, For_t, ForEach_t,
952950
Return_t, Local_t, Global_t, Export_t, Macro_t, MacroInPlace_t,
953951
BreakLoop_t, Label_t, Goto_t, ShortTabAppending_t,
954952
Backcall_t, LocalAttrib_t, PipeBody_t, ExpListAssign_t, ChainAssign_t
955953
> content;
956954
ast_ptr<false, StatementAppendix_t> appendix;
957-
AST_MEMBER(Statement, &sep, &comments, &content, &appendix)
955+
AST_MEMBER(Statement, &content, &appendix)
958956
AST_END(Statement)
959957

960958
AST_NODE(Body)
@@ -964,8 +962,8 @@ AST_END(Body)
964962

965963
AST_NODE(Block)
966964
ast_ptr<true, Seperator_t> sep;
967-
ast_list<false, Statement_t> statements;
968-
AST_MEMBER(Block, &sep, &statements)
965+
ast_sel_list<false, Statement_t, YueComment_t> statementOrComments;
966+
AST_MEMBER(Block, &sep, &statementOrComments)
969967
AST_END(Block)
970968

971969
AST_NODE(BlockEnd)
@@ -984,6 +982,7 @@ struct YueFormat {
984982
int indent = 0;
985983
bool spaceOverTab = false;
986984
int tabSpaces = 4;
985+
bool reserveComment = true;
987986
std::string toString(ast_node* node);
988987

989988
void pushScope();

0 commit comments

Comments
 (0)