-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathFormatter.cpp
More file actions
587 lines (508 loc) · 19.7 KB
/
Formatter.cpp
File metadata and controls
587 lines (508 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#include <Ark/Constants.hpp>
#include <CLI/Formatter.hpp>
#include <fmt/core.h>
#include <fmt/color.h>
#include <Ark/Utils/Files.hpp>
#include <Ark/Error/Exceptions.hpp>
#include <Ark/Error/Diagnostics.hpp>
#include <Ark/Compiler/Common.hpp>
#include <Ark/Utils/Literals.hpp>
using namespace Ark;
using namespace Ark::internal;
using namespace Ark::literals;
Formatter::Formatter(const bool dry_run) :
m_dry_run(dry_run), m_parser(/* debug= */ 0, ParserMode::Raw), m_updated(false)
{}
Formatter::Formatter(std::string filename, const bool dry_run) :
m_filename(std::move(filename)), m_dry_run(dry_run), m_parser(/* debug= */ 0, ParserMode::Raw), m_updated(false)
{}
void Formatter::run()
{
try
{
const std::string code = Utils::readFile(m_filename);
m_parser.process(m_filename, code);
processAst(m_parser.ast());
warnIfCommentsWereRemoved(code, ARK_NO_NAME_FILE);
m_updated = code != m_output;
}
catch (const CodeError& e)
{
Diagnostics::generate(e);
}
}
void Formatter::runWithString(const std::string& code)
{
try
{
m_parser.process(ARK_NO_NAME_FILE, code);
processAst(m_parser.ast());
warnIfCommentsWereRemoved(code, ARK_NO_NAME_FILE);
m_updated = code != m_output;
}
catch (const CodeError& e)
{
Diagnostics::generate(e);
}
}
const std::string& Formatter::output() const
{
return m_output;
}
bool Formatter::codeModified() const
{
return m_updated;
}
void Formatter::processAst(const Node& ast)
{
// remove useless surrounding begin (generated by the parser)
if (isBeginBlock(ast))
{
for (std::size_t i = 1, end = ast.constList().size(); i < end; ++i)
{
const Node node = ast.constList()[i];
if (shouldAddNewLineBetweenNodes(ast, i) && !m_output.empty())
m_output += "\n";
m_output += format(node, 0, false) + "\n";
}
}
else
m_output = format(ast, 0, false);
if (!m_dry_run)
{
std::ofstream stream(m_filename);
stream << m_output;
}
}
void Formatter::warnIfCommentsWereRemoved(const std::string& original_code, const std::string& filename)
{
const std::size_t before_count = std::ranges::count(original_code, '#');
const std::size_t after_count = std::ranges::count(m_output, '#');
if (before_count != after_count)
{
fmt::println(
"{}: one or more comments from the original source code seem to have been {} by mistake while formatting {}",
fmt::styled("Warning", fmt::fg(fmt::color::dark_orange)),
before_count > after_count ? "removed" : "duplicated",
filename != ARK_NO_NAME_FILE ? filename : "file");
fmt::println("Please fill an issue on GitHub: https://github.com/ArkScript-lang/Ark");
}
}
bool Formatter::isListStartingWithKeyword(const Node& node, const Keyword keyword)
{
return node.isListLike() && !node.constList().empty() && node.constList()[0].nodeType() == NodeType::Keyword && node.constList()[0].keyword() == keyword;
}
bool Formatter::isBeginBlock(const Node& node)
{
return isListStartingWithKeyword(node, Keyword::Begin);
}
bool Formatter::isFuncDef(const Node& node)
{
return isListStartingWithKeyword(node, Keyword::Fun);
}
bool Formatter::isFuncCall(const Node& node)
{
return node.isListLike() && !node.constList().empty() && node.constList()[0].nodeType() == NodeType::Symbol;
}
std::size_t Formatter::lineOfLastNodeIn(const Node& node)
{
if (node.isListLike() && !node.constList().empty())
{
const std::size_t child_line = lineOfLastNodeIn(node.constList().back());
if (child_line < node.position().start.line)
return node.position().start.line;
return child_line;
}
return node.position().start.line;
}
bool Formatter::isLongLine(const Node& node)
{
const std::string formatted = format(node, 0, false);
const std::size_t max_len =
std::ranges::max(
Utils::splitString(formatted, '\n'),
[](const std::string& lhs, const std::string& rhs) {
return lhs.size() < rhs.size();
})
.size();
const std::size_t newlines = std::ranges::count(formatted, '\n');
// split on multiple lines if we have a very long node,
// or if we added many line breaks while doing dumb formatting
return max_len >= FormatterConfig::LongLineLength || (newlines > 0 && node.isListLike() && newlines + 1 >= node.constList().size());
}
bool Formatter::shouldSplitOnNewline(const Node& node)
{
if (node.comment().empty() && (isBeginBlock(node) || isFuncCall(node)))
return false;
if (isLongLine(node) || (node.isListLike() && node.constList().size() > 1) || !node.comment().empty())
return true;
return false;
}
bool Formatter::shouldAddNewLineBetweenNodes(const Node& node, const std::size_t at)
{
if (at <= 1)
return false;
const auto& list = node.constList();
const std::size_t previous_line = lineOfLastNodeIn(list[at - 1]);
const auto& child = list[at];
// If we have a node before the current one,
// and the line count between the two nodes is more than 1,
// maybe we should add a new line to preserve user spacing.
// However, if the current node has a comment, do not add a new line, this is causing the spacing.
if (child.position().start.line - previous_line > 1 && child.comment().empty())
return true;
// If we do have a comment but the spacing is more than 2,
// then add a newline to preserve user spacing.
if (child.position().start.line - previous_line > 2 && !child.comment().empty())
return true;
return false;
}
std::string Formatter::format(const Node& node, std::size_t indent, bool after_newline)
{
std::string result;
if (!node.comment().empty())
{
result += formatComment(node.comment(), indent);
after_newline = true;
}
if (after_newline)
result += prefix(indent);
switch (node.nodeType())
{
case NodeType::Symbol:
result += node.string();
break;
case NodeType::MutArg:
result += fmt::format("(mut {})", node.string());
break;
case NodeType::RefArg:
result += fmt::format("(ref {})", node.string());
break;
case NodeType::Capture:
result += "&" + node.string();
break;
case NodeType::Keyword:
result += std::string(keywords[static_cast<std::size_t>(node.keyword())]);
break;
case NodeType::String:
result += fmt::format("\"{}\"", node.string());
break;
case NodeType::Number:
result += fmt::format("{}", node.number());
break;
case NodeType::List:
result += formatBlock(node, indent, after_newline);
break;
case NodeType::Spread:
result += fmt::format("...{}", node.string());
break;
case NodeType::Field:
{
std::string field = format(node.constList()[0], indent, false);
for (std::size_t i = 1, end = node.constList().size(); i < end; ++i)
field += "." + format(node.constList()[i], indent, false);
result += field;
break;
}
case NodeType::Macro:
result += formatMacro(node, indent);
break;
// not handling Namespace nor Unused node types as those can not be generated by the parser
case NodeType::Namespace:
[[fallthrough]];
case NodeType::Unused:
break;
}
if (!node.commentAfter().empty())
result += " " + formatComment(node.commentAfter(), /* indent= */ 0);
return result;
}
std::string Formatter::formatComment(const std::string& comment, const std::size_t indent) const
{
std::string result = prefix(indent);
for (std::size_t i = 0, end = comment.size(); i < end; ++i)
{
result += comment[i];
if (comment[i] == '\n' && i != end - 1)
result += prefix(indent);
}
return result;
}
std::string Formatter::formatBlock(const Node& node, const std::size_t indent, const bool after_newline)
{
if (node.constList().empty())
return "()";
const Node first = node.constList().front();
if (first.nodeType() == NodeType::Keyword)
{
switch (first.keyword())
{
case Keyword::Fun:
return formatFunction(node, indent);
case Keyword::Let:
[[fallthrough]];
case Keyword::Mut:
[[fallthrough]];
case Keyword::Set:
return formatVariable(node, indent);
case Keyword::If:
return formatCondition(node, indent);
case Keyword::While:
return formatLoop(node, indent);
case Keyword::Begin:
return formatBegin(node, indent, after_newline);
case Keyword::Import:
return formatImport(node, indent);
case Keyword::Del:
return formatDel(node, indent);
}
// HACK: should never reach, but the compiler insists that the function doesn't return in every code path
return "";
}
return formatCall(node, indent);
}
std::string Formatter::formatFunction(const Node& node, const std::size_t indent)
{
const Node args_node = node.constList()[1];
const Node body_node = node.constList()[2];
std::string formatted_args;
if (!args_node.comment().empty())
{
formatted_args += "\n";
formatted_args += formatComment(args_node.comment(), indent + 1);
formatted_args += prefix(indent + 1);
}
else
formatted_args += " ";
if (args_node.isListLike())
{
bool comment_in_args = false;
std::string args;
const bool split = (isLongLine(args_node) || !args_node.comment().empty());
for (std::size_t i = 0, end = args_node.constList().size(); i < end; ++i)
{
const Node arg_i = args_node.constList()[i];
if (!arg_i.comment().empty())
comment_in_args = true;
args += format(arg_i, indent + ((comment_in_args || split) ? 1 : 0), i > 0 && (comment_in_args || split));
if (i != end - 1)
args += (comment_in_args || split) ? '\n' : ' ';
}
formatted_args += fmt::format("({}{})", (comment_in_args ? "\n" : ""), args);
}
else
formatted_args += format(args_node, indent, false);
if (!shouldSplitOnNewline(body_node) && args_node.comment().empty())
return fmt::format("(fun{} {})", formatted_args, format(body_node, indent + 1, false));
return fmt::format("(fun{}\n{})", formatted_args, format(body_node, indent + 1, true));
}
std::string Formatter::formatVariable(const Node& node, const std::size_t indent)
{
const auto keyword = std::string(keywords[static_cast<std::size_t>(node.constList()[0].keyword())]);
const Node body_node = node.constList()[2];
const std::string formatted_bind = format(node.constList()[1], indent, false);
// we don't want to add another indentation level here, because it would result in a (let a (fun ()\n{indent+=4}...))
if (isFuncDef(body_node) || !shouldSplitOnNewline(body_node))
return fmt::format("({} {} {})", keyword, formatted_bind, format(body_node, indent, false));
return fmt::format("({} {}\n{})", keyword, formatted_bind, format(body_node, indent + 1, true));
}
std::string Formatter::formatCondition(const Node& node, const std::size_t indent, const bool is_macro)
{
const Node cond_node = node.constList()[1];
const Node then_node = node.constList()[2];
bool cond_on_newline = false;
const std::string formatted_cond = format(cond_node, indent + 1, false);
if (formatted_cond.find('\n') != std::string::npos)
cond_on_newline = true;
std::string if_cond_formatted = fmt::format(
"({}if{}{}",
is_macro ? "$" : "",
cond_on_newline ? "\n" : " ",
cond_on_newline ? format(cond_node, indent + 1, true) : formatted_cond);
const bool split_then_newline = shouldSplitOnNewline(then_node) || isBeginBlock(then_node);
// (if cond then)
if (node.constList().size() == 3)
{
if (cond_on_newline || split_then_newline)
return fmt::format("{}\n{})", if_cond_formatted, format(then_node, indent + 1, true));
return fmt::format("{} {})", if_cond_formatted, format(then_node, indent + 1, false));
}
// (if cond then else)
return fmt::format(
"{}\n{}\n{}{})",
if_cond_formatted,
format(then_node, indent + 1, true),
format(node.constList()[3], indent + 1, true),
node.constList()[3].commentAfter().empty() ? "" : ("\n" + prefix(indent)));
}
std::string Formatter::formatLoop(const Node& node, const std::size_t indent)
{
const Node cond_node = node.constList()[1];
const Node body_node = node.constList()[2];
bool cond_on_newline = false;
std::string formatted_cond = format(cond_node, indent + 1, false);
if (formatted_cond.find('\n') != std::string::npos)
cond_on_newline = true;
if (cond_on_newline || shouldSplitOnNewline(body_node))
return fmt::format(
"(while{}{}\n{})",
cond_on_newline ? "\n" : " ",
formatted_cond,
format(body_node, indent + 1, true));
return fmt::format(
"(while {} {})",
formatted_cond,
format(body_node, indent + 1, false));
}
std::string Formatter::formatBegin(const Node& node, const std::size_t indent, const bool after_newline)
{
// only the keyword begin is present
if (node.constList().size() == 1)
return "{}";
// after a new line, we need to increment our indentation level
// if the block is a top level one, we also need to increment indentation level
const std::size_t inner_indentation = indent + (after_newline ? 1 : 0) + (indent == 0 ? 1 : 0);
std::string result = "{\n";
// skip begin keyword
for (std::size_t i = 1, end = node.constList().size(); i < end; ++i)
{
const Node child = node.constList()[i];
// we want to preserve the node grouping by the user, but remove useless duplicate new line
// but that shouldn't apply to the first node of the block
if (shouldAddNewLineBetweenNodes(node, i) && i > 1)
result += "\n";
result += format(child, inner_indentation, true);
if (i != end - 1)
result += "\n";
}
// if the last node has a comment, add a new line
if (!node.constList().empty() && !node.constList().back().commentAfter().empty())
result += "\n" + prefix(indent) + "}";
else
result += " }";
return result;
}
std::string Formatter::formatImport(const Node& node, const std::size_t indent)
{
const Node package_node = node.constList()[1];
std::string package;
if (!package_node.comment().empty())
package += "\n" + formatComment(package_node.comment(), indent + 1) + prefix(indent + 1);
else
package += " ";
for (std::size_t i = 0, end = package_node.constList().size(); i < end; ++i)
{
package += format(package_node.constList()[i], indent + 1, false);
if (i != end - 1)
package += ".";
}
const Node symbols = node.constList()[2];
if (symbols.nodeType() == NodeType::Symbol && symbols.string() == "*")
package += ":*";
else // symbols is a list
{
if (const auto& sym_list = symbols.constList(); !sym_list.empty())
{
const bool comment_after_last = !sym_list.back().commentAfter().empty();
for (const auto& sym : sym_list)
{
if (sym.comment().empty())
{
if (comment_after_last)
package += "\n" + prefix(indent + 1) + ":" + sym.string();
else
package += " :" + sym.string();
}
else
package += "\n" + formatComment(sym.comment(), indent + 1) + prefix(indent + 1) + ":" + sym.string();
}
if (comment_after_last)
{
package += " " + formatComment(sym_list.back().commentAfter(), /* indent= */ 0);
package += "\n" + prefix(indent + 1);
}
}
}
return fmt::format("(import{})", package);
}
std::string Formatter::formatDel(const Node& node, const std::size_t indent)
{
std::string formatted_sym = format(node.constList()[1], indent + 1, false);
if (formatted_sym.find('\n') != std::string::npos)
return fmt::format("(del\n{})", formatted_sym);
return fmt::format("(del {})", formatted_sym);
}
std::string Formatter::formatCall(const Node& node, const std::size_t indent)
{
bool is_list = false;
bool is_dict = false;
bool is_multiline = false;
if (!node.constList().empty() && node.constList().front().nodeType() == NodeType::Symbol)
{
if (node.constList().front().string() == "list")
is_list = true;
else if (node.constList().front().string() == "dict")
is_dict = true;
}
std::vector<std::string> formatted_args;
for (std::size_t i = 1, end = node.constList().size(); i < end; ++i)
{
formatted_args.push_back(format(node.constList()[i], indent, false));
// if we have at least one argument taking multiple lines, split them all on their own line
if (formatted_args.back().find('\n') != std::string::npos || !node.constList()[i].commentAfter().empty())
is_multiline = true;
}
std::string result = is_list ? "[" : ("(" + format(node.constList()[0], indent, false));
// Split args on multiple lines even if, individually, they fit in the configured line length, if grouped together
// on a single line they are too long
const std::size_t args_line_length = std::accumulate(
formatted_args.begin(),
formatted_args.end(),
result.size() + 1, // +1 to count the closing paren/bracket
[](const std::size_t acc, const std::string& val) {
return acc + val.size() + 1_z;
});
if (args_line_length >= FormatterConfig::LongLineLength)
is_multiline = true;
for (std::size_t i = 0, end = formatted_args.size(); i < end; ++i)
{
const std::string& formatted_node = formatted_args[i];
if (is_dict)
{
if (i % 2 == 0 && formatted_args.size() > 2) // one pair per line if we have at least 2 key-value pairs
result += "\n" + format(node.constList()[i + 1], indent + 1, true);
else
result += " " + formatted_node;
}
else if (is_multiline)
result += "\n" + format(node.constList()[i + 1], indent + 1, true);
else if (is_list && i == 0)
result += formatted_node;
else // put all arguments on the same line
result += " " + formatted_node;
}
if (!node.constList().back().commentAfter().empty())
result += "\n" + prefix(indent);
result += is_list ? "]" : ")";
return result;
}
std::string Formatter::formatMacro(const Node& node, const std::size_t indent)
{
if (isListStartingWithKeyword(node, Keyword::If))
return formatCondition(node, indent, /* is_macro= */ true);
std::string result = "(macro ";
bool after_newline = false;
for (std::size_t i = 0, end = node.constList().size(); i < end; ++i)
{
result += format(node.constList()[i], indent + 1, after_newline);
after_newline = false;
if (!node.constList()[i].commentAfter().empty())
{
result += "\n";
after_newline = true;
}
else if (i != end - 1)
result += " ";
}
return result + ")";
}