Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/yuescript/yue_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -729,13 +729,14 @@ AST_NODE(TableBlockIndent)
ast_ptr<true, Seperator_t> sep;
ast_sel_list<false,
VariablePair_t, NormalPair_t, Exp_t, TableBlockIndent_t,
MetaVariablePair_t, MetaNormalPair_t> values;
MetaVariablePair_t, MetaNormalPair_t,
YueComment_t, EmptyLine_t> values;
AST_MEMBER(TableBlockIndent, &sep, &values)
AST_END(TableBlockIndent)

AST_NODE(TableBlock)
ast_ptr<true, Seperator_t> sep;
ast_sel_list<false, VariablePair_t, NormalPair_t, TableBlockIndent_t, Exp_t, TableBlock_t, SpreadExp_t, MetaVariablePair_t, MetaNormalPair_t> values;
ast_sel_list<false, VariablePair_t, NormalPair_t, TableBlockIndent_t, Exp_t, TableBlock_t, SpreadExp_t, MetaVariablePair_t, MetaNormalPair_t, YueComment_t, EmptyLine_t> values;
AST_MEMBER(TableBlock, &sep, &values)
AST_END(TableBlock)

Expand Down
23 changes: 23 additions & 0 deletions src/yuescript/yue_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,9 @@ class YueCompilerImpl {
auto pair = static_cast<MetaVariablePair_t*>(item);
return isLocal(variableToString(pair->name));
}
case id<YueComment_t>():
case id<EmptyLine_t>():
return true;
case id<MetaNormalPair_t>(): {
auto pair = static_cast<MetaNormalPair_t*>(item);
if (auto str = pair->key.as<String_t>()) {
Expand Down Expand Up @@ -7987,6 +7990,19 @@ class YueCompilerImpl {
transformFor(forNode, temp);
break;
}
case id<YueComment_t>(): {
if (_config.reserveComment) {
auto comment = static_cast<YueComment_t*>(item);
temp.push_back(indent() + comment->to_string(&_config) + nl(item));
}
break;
}
case id<EmptyLine_t>(): {
if (_config.reserveComment) {
temp.push_back(nl(item));
}
break;
}
case id<VariablePair_t>():
case id<VariablePairDef_t>(): {
if (auto pair = ast_cast<VariablePairDef_t>(item)) {
Expand Down Expand Up @@ -8204,6 +8220,8 @@ class YueCompilerImpl {
auto metatable = x->new_ptr<SimpleTable_t>();
ast_sel<false, Exp_t, TableBlock_t> metatableItem;
ast_node* lastValueNode = values.back();
int lastValueLine = x->m_begin.m_line;
bool prevWasEmptyLine = false;
if (!_config.reserveComment) {
for (auto it = values.rbegin(); it != values.rend(); ++it) {
auto node = *it;
Expand Down Expand Up @@ -8265,6 +8283,9 @@ class YueCompilerImpl {
switch (item->get_id()) {
case id<Exp_t>(): transformExp(static_cast<Exp_t*>(item), temp, ExpUsage::Closure); break;
case id<YueComment_t>(): {
if (_config.reserveComment && !prevWasEmptyLine && value->m_begin.m_line > lastValueLine + 1) {
temp.emplace_back(Empty);
}
auto comment = static_cast<YueComment_t*>(item);
temp.emplace_back(comment->to_string(&_config));
skipComma = true;
Expand Down Expand Up @@ -8337,6 +8358,8 @@ class YueCompilerImpl {
temp.back() = indent() + (value == lastValueNode ? temp.back() : temp.back() + ',') + nl(value);
}
}
lastValueLine = value->m_end.m_line;
prevWasEmptyLine = ast_is<EmptyLine_t>(value);
}
if (metatable->pairs.empty() && !metatableItem) {
out.push_back('{' + nl(x) + join(temp));
Expand Down
20 changes: 12 additions & 8 deletions src/yuescript/yue_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,11 +909,12 @@ YueParser::YueParser() {
white >> '}'
);

table_block_inner = Seperator >> key_value_line >> *(+space_break >> key_value_line);
TableBlock = +space_break >> advance_match >> ensure(table_block_inner, pop_indent);
table_block_inner = Seperator >> key_value_line >> *(((plain_space >> line_break) >> key_value_line) | (+space_break >> not_(expr("--")) >> key_value_line));
TableBlock = ((+(plain_space >> line_break)) | (+space_break >> not_(expr("--")))) >> advance_match >> ensure(table_block_inner, pop_indent);
TableBlockIndent = ('*' | '-' >> space_one) >> Seperator >> disable_arg_table_block_rule(
space >> key_value_list >> -(space >> ',') >>
-(+space_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(+space_break >> key_value_line), pop_indent)));
-((plain_space >> line_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(((plain_space >> line_break) >> key_value_line) | (+space_break >> not_(expr("--")) >> key_value_line)), pop_indent)) |
(+space_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(+space_break >> key_value_line), pop_indent))));

ClassMemberList = Seperator >> key_value >> *(space >> ',' >> space >> key_value);
class_line = check_indent_match >> space >> (ClassMemberList | Statement) >> -(space >> ',');
Expand Down Expand Up @@ -1017,11 +1018,14 @@ YueParser::YueParser() {
MetaVariablePair |
MetaNormalPair;
key_value_list = key_value >> *(space >> ',' >> space >> key_value);
key_value_line = check_indent_match >> space >> (
key_value_list >> -(space >> ',') |
TableBlockIndent |
('*' | '-' >> space_one) >> space >> (SpreadExp | Exp | TableBlock)
);
key_value_line =
(EmptyLine >> YueComment) |
YueComment |
check_indent_match >> space >> (
key_value_list >> -(space >> ',') |
TableBlockIndent |
('*' | '-' >> space_one) >> space >> (SpreadExp | Exp | TableBlock)
);

fn_arg_def_list = FnArgDef >> *(space >> ',' >> space >> FnArgDef);

Expand Down
Loading