OILS / prebuilt / asdl / runtime.mycpp.cc View on Github | oilshell.org

1047 lines, 723 significant
1// prebuilt/asdl/runtime.mycpp.cc: GENERATED by mycpp
2
3#include "prebuilt/asdl/runtime.mycpp.h"
4// BEGIN mycpp output
5
6#include "mycpp/runtime.h"
7
8GLOBAL_STR(str0, "(");
9GLOBAL_STR(str1, ")");
10GLOBAL_STR(str2, "_");
11GLOBAL_STR(str3, "T");
12GLOBAL_STR(str4, "F");
13GLOBAL_STR(str5, "\n<html>\n <head>\n <title>Oils AST</title>\n <style>\n .n { color: brown }\n .s { font-weight: bold }\n .o { color: darkgreen }\n </style>\n </head>\n <body>\n <pre>\n");
14GLOBAL_STR(str6, "\n </pre>\n </body>\n</html>\n ");
15GLOBAL_STR(str7, "n");
16GLOBAL_STR(str8, "s");
17GLOBAL_STR(str9, "o");
18GLOBAL_STR(str10, "o");
19GLOBAL_STR(str11, "o");
20GLOBAL_STR(str12, "<span class=\"%s\">");
21GLOBAL_STR(str13, "</span>");
22GLOBAL_STR(str14, " ");
23GLOBAL_STR(str15, "\n");
24GLOBAL_STR(str16, " ");
25GLOBAL_STR(str17, "]");
26GLOBAL_STR(str18, " ");
27GLOBAL_STR(str19, " ");
28GLOBAL_STR(str20, "\n");
29GLOBAL_STR(str21, "\n");
30GLOBAL_STR(str22, " ");
31GLOBAL_STR(str23, "%s%s: [");
32GLOBAL_STR(str24, "\n");
33GLOBAL_STR(str25, "\n");
34GLOBAL_STR(str26, "%s]");
35GLOBAL_STR(str27, "%s%s: ");
36GLOBAL_STR(str28, "\n");
37GLOBAL_STR(str29, "\n");
38GLOBAL_STR(str30, " ");
39GLOBAL_STR(str31, "UNTYPED any");
40GLOBAL_STR(str32, "...0x%s");
41GLOBAL_STR(str33, " ");
42GLOBAL_STR(str34, " ");
43GLOBAL_STR(str35, " %s:");
44GLOBAL_STR(str36, "UNTYPED any");
45GLOBAL_STR(str37, "[");
46GLOBAL_STR(str38, " ");
47GLOBAL_STR(str39, "]");
48GLOBAL_STR(str40, "...0x%s");
49GLOBAL_STR(str41, "foo");
50GLOBAL_STR(str42, "\n");
51GLOBAL_STR(str43, "\u001b[0;0m");
52GLOBAL_STR(str44, "\u001b[1m");
53GLOBAL_STR(str45, "\u001b[4m");
54GLOBAL_STR(str46, "\u001b[7m");
55GLOBAL_STR(str47, "\u001b[31m");
56GLOBAL_STR(str48, "\u001b[32m");
57GLOBAL_STR(str49, "\u001b[33m");
58GLOBAL_STR(str50, "\u001b[34m");
59GLOBAL_STR(str51, "\u001b[35m");
60GLOBAL_STR(str52, "\u001b[36m");
61GLOBAL_STR(str53, "\u001b[37m");
62GLOBAL_STR(str54, "\n");
63GLOBAL_STR(str55, "&");
64GLOBAL_STR(str56, "&amp;");
65GLOBAL_STR(str57, "<");
66GLOBAL_STR(str58, "&lt;");
67GLOBAL_STR(str59, ">");
68GLOBAL_STR(str60, "&gt;");
69
70namespace ansi { // forward declare
71
72
73} // forward declare namespace ansi
74
75namespace pretty { // forward declare
76
77 class PrettyPrinter;
78
79} // forward declare namespace pretty
80
81namespace cgi { // forward declare
82
83
84} // forward declare namespace cgi
85
86namespace j8_lite { // forward declare
87
88
89} // forward declare namespace j8_lite
90
91namespace ansi { // declare
92
93extern BigStr* RESET;
94extern BigStr* BOLD;
95extern BigStr* UNDERLINE;
96extern BigStr* REVERSE;
97extern BigStr* RED;
98extern BigStr* GREEN;
99extern BigStr* YELLOW;
100extern BigStr* BLUE;
101extern BigStr* MAGENTA;
102extern BigStr* CYAN;
103extern BigStr* WHITE;
104
105} // declare namespace ansi
106
107namespace pretty { // declare
108
109pretty_asdl::Measure* _EmptyMeasure();
110pretty_asdl::Measure* _FlattenMeasure(pretty_asdl::Measure* measure);
111pretty_asdl::Measure* _ConcatMeasure(pretty_asdl::Measure* m1, pretty_asdl::Measure* m2);
112int _SuffixLen(pretty_asdl::Measure* measure);
113pretty_asdl::MeasuredDoc* AsciiText(BigStr* string);
114pretty_asdl::MeasuredDoc* _Break(BigStr* string);
115pretty_asdl::MeasuredDoc* _Indent(int indent, pretty_asdl::MeasuredDoc* mdoc);
116pretty_asdl::MeasuredDoc* _Concat(List<pretty_asdl::MeasuredDoc*>* mdocs);
117pretty_asdl::MeasuredDoc* _Group(pretty_asdl::MeasuredDoc* mdoc);
118pretty_asdl::MeasuredDoc* _IfFlat(pretty_asdl::MeasuredDoc* flat_mdoc, pretty_asdl::MeasuredDoc* nonflat_mdoc);
119pretty_asdl::MeasuredDoc* _Flat(pretty_asdl::MeasuredDoc* mdoc);
120class PrettyPrinter {
121 public:
122 PrettyPrinter(int max_width);
123 bool _Fits(int prefix_len, doc::Group* group, pretty_asdl::Measure* suffix_measure);
124 void PrintDoc(pretty_asdl::MeasuredDoc* document, mylib::BufWriter* buf);
125 int max_width{};
126
127 static constexpr ObjHeader obj_header() {
128 return ObjHeader::ClassScanned(0, sizeof(PrettyPrinter));
129 }
130
131 DISALLOW_COPY_AND_ASSIGN(PrettyPrinter)
132};
133
134
135} // declare namespace pretty
136
137namespace cgi { // declare
138
139BigStr* escape(BigStr* s);
140
141} // declare namespace cgi
142
143namespace j8_lite { // declare
144
145BigStr* EncodeString(BigStr* s, bool unquoted_ok = false);
146BigStr* YshEncodeString(BigStr* s);
147BigStr* MaybeShellEncode(BigStr* s);
148BigStr* ShellEncode(BigStr* s);
149BigStr* YshEncode(BigStr* s, bool unquoted_ok = false);
150
151} // declare namespace j8_lite
152
153namespace runtime { // define
154
155using hnode_asdl::hnode;
156using hnode_asdl::color_t;
157using hnode_asdl::color_e;
158int NO_SPID = -1;
159
160hnode::Record* NewRecord(BigStr* node_type) {
161 StackRoot _root0(&node_type);
162
163 return Alloc<hnode::Record>(node_type, Alloc<List<hnode_asdl::Field*>>(), false, str0, str1, Alloc<List<hnode_asdl::hnode_t*>>());
164}
165
166hnode::Leaf* NewLeaf(BigStr* s, hnode_asdl::color_t e_color) {
167 StackRoot _root0(&s);
168
169 if (s == nullptr) {
170 return Alloc<hnode::Leaf>(str2, color_e::OtherConst);
171 }
172 else {
173 return Alloc<hnode::Leaf>(s, e_color);
174 }
175}
176
177TraversalState::TraversalState() {
178 this->seen = Alloc<Dict<int, bool>>();
179 this->ref_count = Alloc<Dict<int, int>>();
180}
181BigStr* TRUE_STR = str3;
182BigStr* FALSE_STR = str4;
183
184} // define namespace runtime
185
186namespace format { // define
187
188using hnode_asdl::hnode;
189using hnode_asdl::hnode_e;
190using hnode_asdl::hnode_t;
191using hnode_asdl::color_e;
192using hnode_asdl::color_t;
193
194format::ColorOutput* DetectConsoleOutput(mylib::Writer* f) {
195 StackRoot _root0(&f);
196
197 if (f->isatty()) {
198 return Alloc<AnsiOutput>(f);
199 }
200 else {
201 return Alloc<TextOutput>(f);
202 }
203}
204
205ColorOutput::ColorOutput(mylib::Writer* f) {
206 this->f = f;
207 this->num_chars = 0;
208}
209
210format::ColorOutput* ColorOutput::NewTempBuffer() {
211 FAIL(kNotImplemented); // Python NotImplementedError
212}
213
214void ColorOutput::FileHeader() {
215 ; // pass
216}
217
218void ColorOutput::FileFooter() {
219 ; // pass
220}
221
222void ColorOutput::PushColor(hnode_asdl::color_t e_color) {
223 FAIL(kNotImplemented); // Python NotImplementedError
224}
225
226void ColorOutput::PopColor() {
227 FAIL(kNotImplemented); // Python NotImplementedError
228}
229
230void ColorOutput::write(BigStr* s) {
231 StackRoot _root0(&s);
232
233 this->f->write(s);
234 this->num_chars += len(s);
235}
236
237void ColorOutput::WriteRaw(Tuple2<BigStr*, int>* raw) {
238 BigStr* s = nullptr;
239 int num_chars;
240 StackRoot _root0(&raw);
241 StackRoot _root1(&s);
242
243 Tuple2<BigStr*, int>* tup0 = raw;
244 s = tup0->at0();
245 num_chars = tup0->at1();
246 this->f->write(s);
247 this->num_chars += num_chars;
248}
249
250int ColorOutput::NumChars() {
251 return this->num_chars;
252}
253
254Tuple2<BigStr*, int> ColorOutput::GetRaw() {
255 mylib::BufWriter* f = nullptr;
256 StackRoot _root0(&f);
257
258 f = static_cast<mylib::BufWriter*>(this->f);
259 return Tuple2<BigStr*, int>(f->getvalue(), this->num_chars);
260}
261
262TextOutput::TextOutput(mylib::Writer* f) : ::format::ColorOutput(f) {
263}
264
265format::TextOutput* TextOutput::NewTempBuffer() {
266 return Alloc<TextOutput>(Alloc<mylib::BufWriter>());
267}
268
269void TextOutput::PushColor(hnode_asdl::color_t e_color) {
270 ; // pass
271}
272
273void TextOutput::PopColor() {
274 ; // pass
275}
276
277HtmlOutput::HtmlOutput(mylib::Writer* f) : ::format::ColorOutput(f) {
278}
279
280format::HtmlOutput* HtmlOutput::NewTempBuffer() {
281 return Alloc<HtmlOutput>(Alloc<mylib::BufWriter>());
282}
283
284void HtmlOutput::FileHeader() {
285 this->f->write(str5);
286}
287
288void HtmlOutput::FileFooter() {
289 this->f->write(str6);
290}
291
292void HtmlOutput::PushColor(hnode_asdl::color_t e_color) {
293 BigStr* css_class = nullptr;
294 StackRoot _root0(&css_class);
295
296 if (e_color == color_e::TypeName) {
297 css_class = str7;
298 }
299 else {
300 if (e_color == color_e::StringConst) {
301 css_class = str8;
302 }
303 else {
304 if (e_color == color_e::OtherConst) {
305 css_class = str9;
306 }
307 else {
308 if (e_color == color_e::External) {
309 css_class = str10;
310 }
311 else {
312 if (e_color == color_e::UserType) {
313 css_class = str11;
314 }
315 else {
316 assert(0); // AssertionError
317 }
318 }
319 }
320 }
321 }
322 this->f->write(StrFormat("<span class=\"%s\">", css_class));
323}
324
325void HtmlOutput::PopColor() {
326 this->f->write(str13);
327}
328
329void HtmlOutput::write(BigStr* s) {
330 StackRoot _root0(&s);
331
332 this->f->write(cgi::escape(s));
333 this->num_chars += len(s);
334}
335
336AnsiOutput::AnsiOutput(mylib::Writer* f) : ::format::ColorOutput(f) {
337}
338
339format::AnsiOutput* AnsiOutput::NewTempBuffer() {
340 return Alloc<AnsiOutput>(Alloc<mylib::BufWriter>());
341}
342
343void AnsiOutput::PushColor(hnode_asdl::color_t e_color) {
344 if (e_color == color_e::TypeName) {
345 this->f->write(ansi::YELLOW);
346 }
347 else {
348 if (e_color == color_e::StringConst) {
349 this->f->write(ansi::BOLD);
350 }
351 else {
352 if (e_color == color_e::OtherConst) {
353 this->f->write(ansi::GREEN);
354 }
355 else {
356 if (e_color == color_e::External) {
357 this->f->write(str_concat(ansi::BOLD, ansi::BLUE));
358 }
359 else {
360 if (e_color == color_e::UserType) {
361 this->f->write(ansi::GREEN);
362 }
363 else {
364 assert(0); // AssertionError
365 }
366 }
367 }
368 }
369 }
370}
371
372void AnsiOutput::PopColor() {
373 this->f->write(ansi::RESET);
374}
375int INDENT = 2;
376
377_PrettyPrinter::_PrettyPrinter(int max_col) {
378 this->max_col = max_col;
379}
380
381bool _PrettyPrinter::_PrintWrappedArray(List<hnode_asdl::hnode_t*>* array, int prefix_len, format::ColorOutput* f, int indent) {
382 bool all_fit;
383 int chars_so_far;
384 int i;
385 format::ColorOutput* single_f = nullptr;
386 BigStr* s = nullptr;
387 int num_chars;
388 StackRoot _root0(&array);
389 StackRoot _root1(&f);
390 StackRoot _root2(&single_f);
391 StackRoot _root3(&s);
392
393 all_fit = true;
394 chars_so_far = prefix_len;
395 i = 0;
396 for (ListIter<hnode_asdl::hnode_t*> it(array); !it.Done(); it.Next(), ++i) {
397 hnode_asdl::hnode_t* val = it.Value();
398 StackRoot _for(&val );
399 if (i != 0) {
400 f->write(str14);
401 }
402 single_f = f->NewTempBuffer();
403 if (_TrySingleLine(val, single_f, (this->max_col - chars_so_far))) {
404 Tuple2<BigStr*, int> tup1 = single_f->GetRaw();
405 s = tup1.at0();
406 num_chars = tup1.at1();
407 f->WriteRaw((Alloc<Tuple2<BigStr*, int>>(s, num_chars)));
408 chars_so_far += single_f->NumChars();
409 }
410 else {
411 f->write(str15);
412 this->PrintNode(val, f, (indent + INDENT));
413 chars_so_far = 0;
414 all_fit = false;
415 }
416 }
417 return all_fit;
418}
419
420bool _PrettyPrinter::_PrintWholeArray(List<hnode_asdl::hnode_t*>* array, int prefix_len, format::ColorOutput* f, int indent) {
421 bool all_fit;
422 List<Tuple2<BigStr*, int>*>* pieces = nullptr;
423 int chars_so_far;
424 format::ColorOutput* single_f = nullptr;
425 BigStr* s = nullptr;
426 int num_chars;
427 int i;
428 StackRoot _root0(&array);
429 StackRoot _root1(&f);
430 StackRoot _root2(&pieces);
431 StackRoot _root3(&single_f);
432 StackRoot _root4(&s);
433
434 all_fit = true;
435 pieces = Alloc<List<Tuple2<BigStr*, int>*>>();
436 chars_so_far = prefix_len;
437 for (ListIter<hnode_asdl::hnode_t*> it(array); !it.Done(); it.Next()) {
438 hnode_asdl::hnode_t* item = it.Value();
439 StackRoot _for(&item );
440 single_f = f->NewTempBuffer();
441 if (_TrySingleLine(item, single_f, (this->max_col - chars_so_far))) {
442 Tuple2<BigStr*, int> tup2 = single_f->GetRaw();
443 s = tup2.at0();
444 num_chars = tup2.at1();
445 pieces->append((Alloc<Tuple2<BigStr*, int>>(s, num_chars)));
446 chars_so_far += single_f->NumChars();
447 }
448 else {
449 all_fit = false;
450 break;
451 }
452 }
453 if (all_fit) {
454 i = 0;
455 for (ListIter<Tuple2<BigStr*, int>*> it(pieces); !it.Done(); it.Next(), ++i) {
456 Tuple2<BigStr*, int>* p = it.Value();
457 StackRoot _for(&p );
458 if (i != 0) {
459 f->write(str16);
460 }
461 f->WriteRaw(p);
462 }
463 f->write(str17);
464 }
465 return all_fit;
466}
467
468void _PrettyPrinter::_PrintRecord(hnode::Record* node, format::ColorOutput* f, int indent) {
469 BigStr* ind = nullptr;
470 BigStr* prefix = nullptr;
471 int prefix_len;
472 bool all_fit;
473 BigStr* name = nullptr;
474 hnode_asdl::hnode_t* val = nullptr;
475 BigStr* ind1 = nullptr;
476 hnode_asdl::hnode_t* UP_val = nullptr;
477 int tag;
478 BigStr* name_str = nullptr;
479 format::ColorOutput* single_f = nullptr;
480 BigStr* s = nullptr;
481 int num_chars;
482 StackRoot _root0(&node);
483 StackRoot _root1(&f);
484 StackRoot _root2(&ind);
485 StackRoot _root3(&prefix);
486 StackRoot _root4(&name);
487 StackRoot _root5(&val);
488 StackRoot _root6(&ind1);
489 StackRoot _root7(&UP_val);
490 StackRoot _root8(&name_str);
491 StackRoot _root9(&single_f);
492 StackRoot _root10(&s);
493
494 ind = str_repeat(str18, indent);
495 if (node->abbrev) {
496 prefix = str_concat(ind, node->left);
497 f->write(prefix);
498 if (len(node->node_type)) {
499 f->PushColor(color_e::TypeName);
500 f->write(node->node_type);
501 f->PopColor();
502 f->write(str19);
503 }
504 prefix_len = ((len(prefix) + len(node->node_type)) + 1);
505 all_fit = this->_PrintWrappedArray(node->unnamed_fields, prefix_len, f, indent);
506 if (!all_fit) {
507 f->write(str20);
508 f->write(ind);
509 }
510 f->write(node->right);
511 }
512 else {
513 f->write(str_concat(ind, node->left));
514 f->PushColor(color_e::TypeName);
515 f->write(node->node_type);
516 f->PopColor();
517 f->write(str21);
518 for (ListIter<hnode_asdl::Field*> it(node->fields); !it.Done(); it.Next()) {
519 hnode_asdl::Field* field = it.Value();
520 StackRoot _for(&field );
521 name = field->name;
522 val = field->val;
523 ind1 = str_repeat(str22, (indent + INDENT));
524 UP_val = val;
525 tag = val->tag();
526 if (tag == hnode_e::Array) {
527 hnode::Array* val = static_cast<hnode::Array*>(UP_val);
528 name_str = StrFormat("%s%s: [", ind1, name);
529 f->write(name_str);
530 prefix_len = len(name_str);
531 if (!this->_PrintWholeArray(val->children, prefix_len, f, indent)) {
532 f->write(str24);
533 for (ListIter<hnode_asdl::hnode_t*> it(val->children); !it.Done(); it.Next()) {
534 hnode_asdl::hnode_t* child = it.Value();
535 StackRoot _for(&child );
536 this->PrintNode(child, f, ((indent + INDENT) + INDENT));
537 f->write(str25);
538 }
539 f->write(StrFormat("%s]", ind1));
540 }
541 }
542 else {
543 name_str = StrFormat("%s%s: ", ind1, name);
544 f->write(name_str);
545 prefix_len = len(name_str);
546 single_f = f->NewTempBuffer();
547 if (_TrySingleLine(val, single_f, (this->max_col - prefix_len))) {
548 Tuple2<BigStr*, int> tup3 = single_f->GetRaw();
549 s = tup3.at0();
550 num_chars = tup3.at1();
551 f->WriteRaw((Alloc<Tuple2<BigStr*, int>>(s, num_chars)));
552 }
553 else {
554 f->write(str28);
555 this->PrintNode(val, f, ((indent + INDENT) + INDENT));
556 }
557 }
558 f->write(str29);
559 }
560 f->write(str_concat(ind, node->right));
561 }
562}
563
564void _PrettyPrinter::PrintNode(hnode_asdl::hnode_t* node, format::ColorOutput* f, int indent) {
565 BigStr* ind = nullptr;
566 format::ColorOutput* single_f = nullptr;
567 BigStr* s = nullptr;
568 int num_chars;
569 hnode_asdl::hnode_t* UP_node = nullptr;
570 int tag;
571 StackRoot _root0(&node);
572 StackRoot _root1(&f);
573 StackRoot _root2(&ind);
574 StackRoot _root3(&single_f);
575 StackRoot _root4(&s);
576 StackRoot _root5(&UP_node);
577
578 ind = str_repeat(str30, indent);
579 single_f = f->NewTempBuffer();
580 single_f->write(ind);
581 if (_TrySingleLine(node, single_f, (this->max_col - indent))) {
582 Tuple2<BigStr*, int> tup4 = single_f->GetRaw();
583 s = tup4.at0();
584 num_chars = tup4.at1();
585 f->WriteRaw((Alloc<Tuple2<BigStr*, int>>(s, num_chars)));
586 return ;
587 }
588 UP_node = node;
589 tag = node->tag();
590 if (tag == hnode_e::Leaf) {
591 hnode::Leaf* node = static_cast<hnode::Leaf*>(UP_node);
592 f->PushColor(node->color);
593 f->write(j8_lite::EncodeString(node->s, true));
594 f->PopColor();
595 }
596 else {
597 if (tag == hnode_e::External) {
598 hnode::External* node = static_cast<hnode::External*>(UP_node);
599 f->PushColor(color_e::External);
600 // if not PYTHON
601 {
602 f->write(str31);
603 }
604 // endif MYCPP
605 f->PopColor();
606 }
607 else {
608 if (tag == hnode_e::Record) {
609 hnode::Record* node = static_cast<hnode::Record*>(UP_node);
610 this->_PrintRecord(node, f, indent);
611 }
612 else {
613 if (tag == hnode_e::AlreadySeen) {
614 hnode::AlreadySeen* node = static_cast<hnode::AlreadySeen*>(UP_node);
615 f->write(StrFormat("...0x%s", mylib::hex_lower(node->heap_id)));
616 }
617 else {
618 assert(0); // AssertionError
619 }
620 }
621 }
622 }
623}
624
625bool _TrySingleLineObj(hnode::Record* node, format::ColorOutput* f, int max_chars) {
626 int i;
627 StackRoot _root0(&node);
628 StackRoot _root1(&f);
629
630 f->write(node->left);
631 if (node->abbrev) {
632 if (len(node->node_type)) {
633 f->PushColor(color_e::TypeName);
634 f->write(node->node_type);
635 f->PopColor();
636 f->write(str33);
637 }
638 i = 0;
639 for (ListIter<hnode_asdl::hnode_t*> it(node->unnamed_fields); !it.Done(); it.Next(), ++i) {
640 hnode_asdl::hnode_t* val = it.Value();
641 StackRoot _for(&val );
642 if (i != 0) {
643 f->write(str34);
644 }
645 if (!_TrySingleLine(val, f, max_chars)) {
646 return false;
647 }
648 }
649 }
650 else {
651 f->PushColor(color_e::TypeName);
652 f->write(node->node_type);
653 f->PopColor();
654 for (ListIter<hnode_asdl::Field*> it(node->fields); !it.Done(); it.Next()) {
655 hnode_asdl::Field* field = it.Value();
656 StackRoot _for(&field );
657 f->write(StrFormat(" %s:", field->name));
658 if (!_TrySingleLine(field->val, f, max_chars)) {
659 return false;
660 }
661 }
662 }
663 f->write(node->right);
664 return true;
665}
666
667bool _TrySingleLine(hnode_asdl::hnode_t* node, format::ColorOutput* f, int max_chars) {
668 hnode_asdl::hnode_t* UP_node = nullptr;
669 int tag;
670 int i;
671 int num_chars_so_far;
672 StackRoot _root0(&node);
673 StackRoot _root1(&f);
674 StackRoot _root2(&UP_node);
675
676 UP_node = node;
677 tag = node->tag();
678 if (tag == hnode_e::Leaf) {
679 hnode::Leaf* node = static_cast<hnode::Leaf*>(UP_node);
680 f->PushColor(node->color);
681 f->write(j8_lite::EncodeString(node->s, true));
682 f->PopColor();
683 }
684 else {
685 if (tag == hnode_e::External) {
686 hnode::External* node = static_cast<hnode::External*>(UP_node);
687 f->PushColor(color_e::External);
688 // if not PYTHON
689 {
690 f->write(str36);
691 }
692 // endif MYCPP
693 f->PopColor();
694 }
695 else {
696 if (tag == hnode_e::Array) {
697 hnode::Array* node = static_cast<hnode::Array*>(UP_node);
698 f->write(str37);
699 i = 0;
700 for (ListIter<hnode_asdl::hnode_t*> it(node->children); !it.Done(); it.Next(), ++i) {
701 hnode_asdl::hnode_t* item = it.Value();
702 StackRoot _for(&item );
703 if (i != 0) {
704 f->write(str38);
705 }
706 if (!_TrySingleLine(item, f, max_chars)) {
707 return false;
708 }
709 }
710 f->write(str39);
711 }
712 else {
713 if (tag == hnode_e::Record) {
714 hnode::Record* node = static_cast<hnode::Record*>(UP_node);
715 return _TrySingleLineObj(node, f, max_chars);
716 }
717 else {
718 if (tag == hnode_e::AlreadySeen) {
719 hnode::AlreadySeen* node = static_cast<hnode::AlreadySeen*>(UP_node);
720 f->write(StrFormat("...0x%s", mylib::hex_lower(node->heap_id)));
721 }
722 else {
723 assert(0); // AssertionError
724 }
725 }
726 }
727 }
728 }
729 num_chars_so_far = f->NumChars();
730 if (num_chars_so_far > max_chars) {
731 return false;
732 }
733 return true;
734}
735
736void PrintTree(hnode_asdl::hnode_t* node, format::ColorOutput* f) {
737 format::_PrettyPrinter* pp = nullptr;
738 StackRoot _root0(&node);
739 StackRoot _root1(&f);
740 StackRoot _root2(&pp);
741
742 pp = Alloc<_PrettyPrinter>(100);
743 pp->PrintNode(node, f, 0);
744}
745
746void PrintTree2(hnode_asdl::hnode_t* node, format::ColorOutput* f) {
747 pretty_asdl::MeasuredDoc* doc = nullptr;
748 pretty::PrettyPrinter* printer = nullptr;
749 mylib::BufWriter* buf = nullptr;
750 StackRoot _root0(&node);
751 StackRoot _root1(&f);
752 StackRoot _root2(&doc);
753 StackRoot _root3(&printer);
754 StackRoot _root4(&buf);
755
756 doc = pretty::AsciiText(str41);
757 printer = Alloc<pretty::PrettyPrinter>(20);
758 buf = Alloc<mylib::BufWriter>();
759 printer->PrintDoc(doc, buf);
760 f->write(buf->getvalue());
761 f->write(str42);
762}
763
764} // define namespace format
765
766namespace ansi { // define
767
768BigStr* RESET = str43;
769BigStr* BOLD = str44;
770BigStr* UNDERLINE = str45;
771BigStr* REVERSE = str46;
772BigStr* RED = str47;
773BigStr* GREEN = str48;
774BigStr* YELLOW = str49;
775BigStr* BLUE = str50;
776BigStr* MAGENTA = str51;
777BigStr* CYAN = str52;
778BigStr* WHITE = str53;
779
780} // define namespace ansi
781
782namespace pretty { // define
783
784using pretty_asdl::doc;
785using pretty_asdl::doc_e;
786using pretty_asdl::DocFragment;
787using pretty_asdl::Measure;
788using pretty_asdl::MeasuredDoc;
789using mylib::BufWriter;
790
791pretty_asdl::Measure* _EmptyMeasure() {
792 return Alloc<Measure>(0, -1);
793}
794
795pretty_asdl::Measure* _FlattenMeasure(pretty_asdl::Measure* measure) {
796 StackRoot _root0(&measure);
797
798 return Alloc<Measure>(measure->flat, -1);
799}
800
801pretty_asdl::Measure* _ConcatMeasure(pretty_asdl::Measure* m1, pretty_asdl::Measure* m2) {
802 StackRoot _root0(&m1);
803 StackRoot _root1(&m2);
804
805 if (m1->nonflat != -1) {
806 return Alloc<Measure>((m1->flat + m2->flat), m1->nonflat);
807 }
808 else {
809 if (m2->nonflat != -1) {
810 return Alloc<Measure>((m1->flat + m2->flat), (m1->flat + m2->nonflat));
811 }
812 else {
813 return Alloc<Measure>((m1->flat + m2->flat), -1);
814 }
815 }
816}
817
818int _SuffixLen(pretty_asdl::Measure* measure) {
819 StackRoot _root0(&measure);
820
821 if (measure->nonflat != -1) {
822 return measure->nonflat;
823 }
824 else {
825 return measure->flat;
826 }
827}
828
829pretty_asdl::MeasuredDoc* AsciiText(BigStr* string) {
830 StackRoot _root0(&string);
831
832 return Alloc<MeasuredDoc>(Alloc<doc::Text>(string), Alloc<Measure>(len(string), -1));
833}
834
835pretty_asdl::MeasuredDoc* _Break(BigStr* string) {
836 StackRoot _root0(&string);
837
838 return Alloc<MeasuredDoc>(Alloc<doc::Break>(string), Alloc<Measure>(len(string), 0));
839}
840
841pretty_asdl::MeasuredDoc* _Indent(int indent, pretty_asdl::MeasuredDoc* mdoc) {
842 StackRoot _root0(&mdoc);
843
844 return Alloc<MeasuredDoc>(Alloc<doc::Indent>(indent, mdoc), mdoc->measure);
845}
846
847pretty_asdl::MeasuredDoc* _Concat(List<pretty_asdl::MeasuredDoc*>* mdocs) {
848 pretty_asdl::Measure* measure = nullptr;
849 StackRoot _root0(&mdocs);
850 StackRoot _root1(&measure);
851
852 measure = _EmptyMeasure();
853 for (ListIter<pretty_asdl::MeasuredDoc*> it(mdocs); !it.Done(); it.Next()) {
854 pretty_asdl::MeasuredDoc* mdoc = it.Value();
855 StackRoot _for(&mdoc );
856 measure = _ConcatMeasure(measure, mdoc->measure);
857 }
858 return Alloc<MeasuredDoc>(Alloc<doc::Concat>(mdocs), measure);
859}
860
861pretty_asdl::MeasuredDoc* _Group(pretty_asdl::MeasuredDoc* mdoc) {
862 StackRoot _root0(&mdoc);
863
864 return Alloc<MeasuredDoc>(Alloc<doc::Group>(mdoc), mdoc->measure);
865}
866
867pretty_asdl::MeasuredDoc* _IfFlat(pretty_asdl::MeasuredDoc* flat_mdoc, pretty_asdl::MeasuredDoc* nonflat_mdoc) {
868 StackRoot _root0(&flat_mdoc);
869 StackRoot _root1(&nonflat_mdoc);
870
871 return Alloc<MeasuredDoc>(Alloc<doc::IfFlat>(flat_mdoc, nonflat_mdoc), Alloc<Measure>(flat_mdoc->measure->flat, nonflat_mdoc->measure->nonflat));
872}
873
874pretty_asdl::MeasuredDoc* _Flat(pretty_asdl::MeasuredDoc* mdoc) {
875 StackRoot _root0(&mdoc);
876
877 return Alloc<MeasuredDoc>(Alloc<doc::Flat>(mdoc), _FlattenMeasure(mdoc->measure));
878}
879
880PrettyPrinter::PrettyPrinter(int max_width) {
881 this->max_width = max_width;
882}
883
884bool PrettyPrinter::_Fits(int prefix_len, doc::Group* group, pretty_asdl::Measure* suffix_measure) {
885 pretty_asdl::Measure* measure = nullptr;
886 StackRoot _root0(&group);
887 StackRoot _root1(&suffix_measure);
888 StackRoot _root2(&measure);
889
890 measure = _ConcatMeasure(_FlattenMeasure(group->mdoc->measure), suffix_measure);
891 return (prefix_len + _SuffixLen(measure)) <= this->max_width;
892}
893
894void PrettyPrinter::PrintDoc(pretty_asdl::MeasuredDoc* document, mylib::BufWriter* buf) {
895 int prefix_len;
896 List<pretty_asdl::DocFragment*>* fragments = nullptr;
897 pretty_asdl::DocFragment* frag = nullptr;
898 doc::Text* text = nullptr;
899 BigStr* break_str = nullptr;
900 doc::Indent* indented = nullptr;
901 doc::Concat* concat = nullptr;
902 pretty_asdl::Measure* measure = nullptr;
903 doc::Group* group = nullptr;
904 bool flat;
905 doc::IfFlat* if_flat = nullptr;
906 pretty_asdl::MeasuredDoc* subdoc = nullptr;
907 doc::Flat* flat_doc = nullptr;
908 StackRoot _root0(&document);
909 StackRoot _root1(&buf);
910 StackRoot _root2(&fragments);
911 StackRoot _root3(&frag);
912 StackRoot _root4(&text);
913 StackRoot _root5(&break_str);
914 StackRoot _root6(&indented);
915 StackRoot _root7(&concat);
916 StackRoot _root8(&measure);
917 StackRoot _root9(&group);
918 StackRoot _root10(&if_flat);
919 StackRoot _root11(&subdoc);
920 StackRoot _root12(&flat_doc);
921
922 prefix_len = 0;
923 fragments = NewList<pretty_asdl::DocFragment*>(std::initializer_list<pretty_asdl::DocFragment*>{Alloc<DocFragment>(_Group(document), 0, false, _EmptyMeasure())});
924 while (len(fragments) > 0) {
925 frag = fragments->pop();
926 switch (frag->mdoc->doc->tag()) {
927 case doc_e::Text: {
928 text = static_cast<doc::Text*>(frag->mdoc->doc);
929 buf->write(text->string);
930 prefix_len += frag->mdoc->measure->flat;
931 }
932 break;
933 case doc_e::Break: {
934 if (frag->is_flat) {
935 break_str = static_cast<doc::Break*>(frag->mdoc->doc)->string;
936 buf->write(break_str);
937 prefix_len += frag->mdoc->measure->flat;
938 }
939 else {
940 buf->write(str54);
941 buf->write_spaces(frag->indent);
942 prefix_len = frag->indent;
943 }
944 }
945 break;
946 case doc_e::Indent: {
947 indented = static_cast<doc::Indent*>(frag->mdoc->doc);
948 fragments->append(Alloc<DocFragment>(indented->mdoc, (frag->indent + indented->indent), frag->is_flat, frag->measure));
949 }
950 break;
951 case doc_e::Concat: {
952 concat = static_cast<doc::Concat*>(frag->mdoc->doc);
953 measure = frag->measure;
954 for (ReverseListIter<pretty_asdl::MeasuredDoc*> it(concat->mdocs); !it.Done(); it.Next()) {
955 pretty_asdl::MeasuredDoc* mdoc = it.Value();
956 StackRoot _for(&mdoc );
957 fragments->append(Alloc<DocFragment>(mdoc, frag->indent, frag->is_flat, measure));
958 measure = _ConcatMeasure(mdoc->measure, measure);
959 }
960 }
961 break;
962 case doc_e::Group: {
963 group = static_cast<doc::Group*>(frag->mdoc->doc);
964 flat = this->_Fits(prefix_len, group, frag->measure);
965 fragments->append(Alloc<DocFragment>(group->mdoc, frag->indent, flat, frag->measure));
966 }
967 break;
968 case doc_e::IfFlat: {
969 if_flat = static_cast<doc::IfFlat*>(frag->mdoc->doc);
970 if (frag->is_flat) {
971 subdoc = if_flat->flat_mdoc;
972 }
973 else {
974 subdoc = if_flat->nonflat_mdoc;
975 }
976 fragments->append(Alloc<DocFragment>(subdoc, frag->indent, frag->is_flat, frag->measure));
977 }
978 break;
979 case doc_e::Flat: {
980 flat_doc = static_cast<doc::Flat*>(frag->mdoc->doc);
981 fragments->append(Alloc<DocFragment>(flat_doc->mdoc, frag->indent, true, frag->measure));
982 }
983 break;
984 }
985 }
986}
987
988} // define namespace pretty
989
990namespace cgi { // define
991
992
993BigStr* escape(BigStr* s) {
994 StackRoot _root0(&s);
995
996 s = s->replace(str55, str56);
997 s = s->replace(str57, str58);
998 s = s->replace(str59, str60);
999 return s;
1000}
1001
1002} // define namespace cgi
1003
1004namespace j8_lite { // define
1005
1006
1007BigStr* EncodeString(BigStr* s, bool unquoted_ok) {
1008 StackRoot _root0(&s);
1009
1010 if ((unquoted_ok and fastfunc::CanOmitQuotes(s))) {
1011 return s;
1012 }
1013 return fastfunc::J8EncodeString(s, 1);
1014}
1015
1016BigStr* YshEncodeString(BigStr* s) {
1017 StackRoot _root0(&s);
1018
1019 return fastfunc::ShellEncodeString(s, 1);
1020}
1021
1022BigStr* MaybeShellEncode(BigStr* s) {
1023 StackRoot _root0(&s);
1024
1025 if (fastfunc::CanOmitQuotes(s)) {
1026 return s;
1027 }
1028 return fastfunc::ShellEncodeString(s, 0);
1029}
1030
1031BigStr* ShellEncode(BigStr* s) {
1032 StackRoot _root0(&s);
1033
1034 return fastfunc::ShellEncodeString(s, 0);
1035}
1036
1037BigStr* YshEncode(BigStr* s, bool unquoted_ok) {
1038 StackRoot _root0(&s);
1039
1040 if ((unquoted_ok and fastfunc::CanOmitQuotes(s))) {
1041 return s;
1042 }
1043 return fastfunc::ShellEncodeString(s, 1);
1044}
1045
1046} // define namespace j8_lite
1047