OILS / prebuilt / asdl / runtime.mycpp.cc View on Github | oils.pub

979 lines, 690 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
8namespace ansi { // forward declare
9}
10
11namespace pretty { // forward declare
12 class PrettyPrinter;
13}
14
15namespace pp_hnode { // forward declare
16 class BaseEncoder;
17 class HNodeEncoder;
18}
19
20namespace cgi { // forward declare
21}
22
23namespace j8_lite { // forward declare
24}
25
26GLOBAL_STR(S_Aoo, "");
27GLOBAL_STR(S_nfs, "\n");
28GLOBAL_STR(S_yfk, "\u001b[0;0m");
29GLOBAL_STR(S_aaF, "\u001b[1m");
30GLOBAL_STR(S_sqm, "\u001b[31m");
31GLOBAL_STR(S_eda, "\u001b[32m");
32GLOBAL_STR(S_ysf, "\u001b[33m");
33GLOBAL_STR(S_osl, "\u001b[34m");
34GLOBAL_STR(S_vie, "\u001b[35m");
35GLOBAL_STR(S_mmi, "\u001b[36m");
36GLOBAL_STR(S_rpo, "\u001b[37m");
37GLOBAL_STR(S_sCc, "\u001b[4m");
38GLOBAL_STR(S_woy, "\u001b[7m");
39GLOBAL_STR(S_yfw, " ");
40GLOBAL_STR(S_ctw, "%s%s%s");
41GLOBAL_STR(S_Clt, "&");
42GLOBAL_STR(S_usD, "&");
43GLOBAL_STR(S_dyr, ">");
44GLOBAL_STR(S_Dcl, "<");
45GLOBAL_STR(S_ijB, "(");
46GLOBAL_STR(S_hxb, ")");
47GLOBAL_STR(S_Cpi, "...0x%s");
48GLOBAL_STR(S_fyj, ":");
49GLOBAL_STR(S_eox, "<");
50GLOBAL_STR(S_jye, ">");
51GLOBAL_STR(S_gFh, "F");
52GLOBAL_STR(S_cor, "T");
53GLOBAL_STR(S_Eax, "[");
54GLOBAL_STR(S_xmu, "[]");
55GLOBAL_STR(S_pcD, "]");
56GLOBAL_STR(S_tci, "_");
57GLOBAL_STR(S_bme, "___ DOC COUNT %d");
58GLOBAL_STR(S_gfw, "___ GC: after printing");
59GLOBAL_STR(S_ray, "___ HNODE COUNT %d");
60
61namespace ansi { // declare
62
63extern BigStr* RESET;
64extern BigStr* BOLD;
65extern BigStr* UNDERLINE;
66extern BigStr* REVERSE;
67extern BigStr* RED;
68extern BigStr* GREEN;
69extern BigStr* YELLOW;
70extern BigStr* BLUE;
71extern BigStr* MAGENTA;
72extern BigStr* CYAN;
73extern BigStr* WHITE;
74
75} // declare namespace ansi
76
77namespace pretty { // declare
78
79pretty_asdl::Measure* _EmptyMeasure();
80pretty_asdl::Measure* _FlattenMeasure(pretty_asdl::Measure* measure);
81pretty_asdl::Measure* _ConcatMeasure(pretty_asdl::Measure* m1, pretty_asdl::Measure* m2);
82int _SuffixLen(pretty_asdl::Measure* measure);
83pretty_asdl::MeasuredDoc* AsciiText(BigStr* string);
84pretty_asdl::MeasuredDoc* _Break(BigStr* string);
85pretty_asdl::MeasuredDoc* _Indent(int indent, pretty_asdl::MeasuredDoc* mdoc);
86pretty_asdl::Measure* _Splice(List<pretty_asdl::MeasuredDoc*>* out, List<pretty_asdl::MeasuredDoc*>* mdocs);
87pretty_asdl::MeasuredDoc* _Concat(List<pretty_asdl::MeasuredDoc*>* mdocs);
88pretty_asdl::MeasuredDoc* _Group(pretty_asdl::MeasuredDoc* mdoc);
89pretty_asdl::MeasuredDoc* _IfFlat(pretty_asdl::MeasuredDoc* flat_mdoc, pretty_asdl::MeasuredDoc* nonflat_mdoc);
90pretty_asdl::MeasuredDoc* _Flat(pretty_asdl::MeasuredDoc* mdoc);
91class PrettyPrinter {
92 public:
93 PrettyPrinter(int max_width);
94 bool _Fits(int prefix_len, pretty_asdl::MeasuredDoc* group, pretty_asdl::Measure* suffix_measure);
95 void PrintDoc(pretty_asdl::MeasuredDoc* document, mylib::BufWriter* buf);
96 int max_width{};
97
98 static constexpr ObjHeader obj_header() {
99 return ObjHeader::ClassScanned(0, sizeof(PrettyPrinter));
100 }
101
102 DISALLOW_COPY_AND_ASSIGN(PrettyPrinter)
103};
104
105
106} // declare namespace pretty
107
108namespace pp_hnode { // declare
109
110class BaseEncoder {
111 public:
112 BaseEncoder();
113 void SetIndent(int indent);
114 void SetUseStyles(bool use_styles);
115 void SetMaxTabularWidth(int max_tabular_width);
116 pretty_asdl::MeasuredDoc* _Styled(BigStr* style, pretty_asdl::MeasuredDoc* mdoc);
117 pretty_asdl::MeasuredDoc* _StyledAscii(BigStr* style, BigStr* s);
118 pretty_asdl::MeasuredDoc* _Surrounded(BigStr* left, pretty_asdl::MeasuredDoc* mdoc, BigStr* right);
119 pretty_asdl::MeasuredDoc* _SurroundedAndPrefixed(BigStr* left, pretty_asdl::MeasuredDoc* prefix, BigStr* sep, pretty_asdl::MeasuredDoc* mdoc, BigStr* right);
120 pretty_asdl::MeasuredDoc* _Join(List<pretty_asdl::MeasuredDoc*>* items, BigStr* sep, BigStr* space);
121 pretty_asdl::MeasuredDoc* _Tabular(List<pretty_asdl::MeasuredDoc*>* items, BigStr* sep);
122 int indent{};
123 int max_tabular_width{};
124 bool use_styles{};
125 Dict<int, bool>* visiting{};
126
127 static constexpr uint32_t field_mask() {
128 return maskbit(offsetof(BaseEncoder, visiting));
129 }
130
131 static constexpr ObjHeader obj_header() {
132 return ObjHeader::ClassFixed(field_mask(), sizeof(BaseEncoder));
133 }
134
135 DISALLOW_COPY_AND_ASSIGN(BaseEncoder)
136};
137
138class HNodeEncoder : public ::pp_hnode::BaseEncoder {
139 public:
140 HNodeEncoder();
141 pretty_asdl::MeasuredDoc* HNode(hnode_asdl::hnode_t* h);
142 pretty_asdl::MeasuredDoc* _Field(hnode_asdl::Field* field);
143 pretty_asdl::MeasuredDoc* _HNode(hnode_asdl::hnode_t* h);
144
145 BigStr* field_color{};
146 BigStr* type_color{};
147
148 static constexpr uint32_t field_mask() {
149 return ::pp_hnode::BaseEncoder::field_mask()
150 | maskbit(offsetof(HNodeEncoder, field_color))
151 | maskbit(offsetof(HNodeEncoder, type_color));
152 }
153
154 static constexpr ObjHeader obj_header() {
155 return ObjHeader::ClassFixed(field_mask(), sizeof(HNodeEncoder));
156 }
157
158 DISALLOW_COPY_AND_ASSIGN(HNodeEncoder)
159};
160
161
162} // declare namespace pp_hnode
163
164namespace cgi { // declare
165
166BigStr* escape(BigStr* s);
167
168} // declare namespace cgi
169
170namespace j8_lite { // declare
171
172BigStr* EncodeString(BigStr* s, bool unquoted_ok = false);
173BigStr* YshEncodeString(BigStr* s);
174BigStr* MaybeShellEncode(BigStr* s);
175BigStr* ShellEncode(BigStr* s);
176BigStr* YshEncode(BigStr* s, bool unquoted_ok = false);
177
178} // declare namespace j8_lite
179
180namespace runtime { // define
181
182using hnode_asdl::hnode;
183using hnode_asdl::color_t;
184using hnode_asdl::color_e;
185int NO_SPID = -1;
186
187hnode::Record* NewRecord(BigStr* node_type) {
188 StackRoot _root0(&node_type);
189
190 return Alloc<hnode::Record>(node_type, S_ijB, S_hxb, Alloc<List<hnode_asdl::Field*>>(), nullptr);
191}
192
193hnode::Leaf* NewLeaf(BigStr* s, hnode_asdl::color_t e_color) {
194 StackRoot _root0(&s);
195
196 if (s == nullptr) {
197 return Alloc<hnode::Leaf>(S_tci, color_e::OtherConst);
198 }
199 else {
200 return Alloc<hnode::Leaf>(s, e_color);
201 }
202}
203
204TraversalState::TraversalState() {
205 this->seen = Alloc<Dict<int, bool>>();
206 this->ref_count = Alloc<Dict<int, int>>();
207}
208BigStr* TRUE_STR = S_cor;
209BigStr* FALSE_STR = S_gFh;
210
211} // define namespace runtime
212
213namespace format { // define
214
215using hnode_asdl::hnode;
216using hnode_asdl::hnode_e;
217using hnode_asdl::hnode_t;
218using pretty_asdl::doc;
219using pretty_asdl::doc_e;
220using pretty_asdl::doc_t;
221using pretty_asdl::MeasuredDoc;
222using pretty_asdl::List_Measured;
223
224int _HNodeCount(hnode_asdl::hnode_t* h) {
225 hnode_asdl::hnode_t* UP_h = nullptr;
226 int n;
227 StackRoot _root0(&h);
228 StackRoot _root1(&UP_h);
229
230 UP_h = h;
231 switch (h->tag()) {
232 case hnode_e::AlreadySeen: {
233 return 1;
234 }
235 break;
236 case hnode_e::Leaf: {
237 return 1;
238 }
239 break;
240 case hnode_e::Array: {
241 hnode::Array* h = static_cast<hnode::Array*>(UP_h);
242 n = 1;
243 for (ListIter<hnode_asdl::hnode_t*> it(h->children); !it.Done(); it.Next()) {
244 hnode_asdl::hnode_t* child = it.Value();
245 StackRoot _for(&child );
246 n += _HNodeCount(child);
247 }
248 return n;
249 }
250 break;
251 case hnode_e::Record: {
252 hnode::Record* h = static_cast<hnode::Record*>(UP_h);
253 n = 1;
254 for (ListIter<hnode_asdl::Field*> it(h->fields); !it.Done(); it.Next()) {
255 hnode_asdl::Field* field = it.Value();
256 StackRoot _for(&field );
257 n += _HNodeCount(field->val);
258 }
259 if (h->unnamed_fields != nullptr) {
260 for (ListIter<hnode_asdl::hnode_t*> it(h->unnamed_fields); !it.Done(); it.Next()) {
261 hnode_asdl::hnode_t* child = it.Value();
262 StackRoot _for(&child );
263 n += _HNodeCount(child);
264 }
265 }
266 return n;
267 }
268 break;
269 default: {
270 assert(0); // AssertionError
271 }
272 }
273}
274
275int _DocCount(pretty_asdl::doc_t* d) {
276 pretty_asdl::doc_t* UP_d = nullptr;
277 int n;
278 StackRoot _root0(&d);
279 StackRoot _root1(&UP_d);
280
281 UP_d = d;
282 switch (d->tag()) {
283 case doc_e::Break: {
284 return 1;
285 }
286 break;
287 case doc_e::Text: {
288 return 1;
289 }
290 break;
291 case doc_e::Indent: {
292 doc::Indent* d = static_cast<doc::Indent*>(UP_d);
293 return (1 + _DocCount(d->mdoc->doc));
294 }
295 break;
296 case doc_e::Group: {
297 MeasuredDoc* d = static_cast<MeasuredDoc*>(UP_d);
298 return (1 + _DocCount(d->doc));
299 }
300 break;
301 case doc_e::Flat: {
302 doc::Flat* d = static_cast<doc::Flat*>(UP_d);
303 return (1 + _DocCount(d->mdoc->doc));
304 }
305 break;
306 case doc_e::IfFlat: {
307 doc::IfFlat* d = static_cast<doc::IfFlat*>(UP_d);
308 return ((1 + _DocCount(d->flat_mdoc->doc)) + _DocCount(d->nonflat_mdoc->doc));
309 }
310 break;
311 case doc_e::Concat: {
312 List_Measured* d = static_cast<List_Measured*>(UP_d);
313 n = 1;
314 for (ListIter<pretty_asdl::MeasuredDoc*> it(d); !it.Done(); it.Next()) {
315 pretty_asdl::MeasuredDoc* mdoc = it.Value();
316 StackRoot _for(&mdoc );
317 n += _DocCount(mdoc->doc);
318 }
319 return n;
320 }
321 break;
322 default: {
323 assert(0); // AssertionError
324 }
325 }
326}
327
328void _HNodePrettyPrint(bool perf_stats, bool doc_debug, hnode_asdl::hnode_t* node, mylib::Writer* f, int max_width) {
329 pp_hnode::HNodeEncoder* enc = nullptr;
330 pretty_asdl::MeasuredDoc* d = nullptr;
331 hnode_asdl::hnode_t* p = nullptr;
332 pretty::PrettyPrinter* printer = nullptr;
333 mylib::BufWriter* buf = nullptr;
334 StackRoot _root0(&node);
335 StackRoot _root1(&f);
336 StackRoot _root2(&enc);
337 StackRoot _root3(&d);
338 StackRoot _root4(&p);
339 StackRoot _root5(&printer);
340 StackRoot _root6(&buf);
341
342 mylib::MaybeCollect();
343 if (perf_stats) {
344 mylib::print_stderr(StrFormat("___ HNODE COUNT %d", _HNodeCount(node)));
345 mylib::print_stderr(S_Aoo);
346 }
347 enc = Alloc<pp_hnode::HNodeEncoder>();
348 enc->SetUseStyles(f->isatty());
349 enc->SetIndent(2);
350 d = enc->HNode(node);
351 mylib::MaybeCollect();
352 if (perf_stats) {
353 if (doc_debug) {
354 p = d->PrettyTree(false);
355 _HNodePrettyPrint(perf_stats, false, p, f);
356 }
357 mylib::print_stderr(StrFormat("___ DOC COUNT %d", _DocCount(d)));
358 mylib::print_stderr(S_Aoo);
359 }
360 printer = Alloc<pretty::PrettyPrinter>(max_width);
361 buf = Alloc<mylib::BufWriter>();
362 printer->PrintDoc(d, buf);
363 f->write(buf->getvalue());
364 f->write(S_nfs);
365 mylib::MaybeCollect();
366 if (perf_stats) {
367 mylib::print_stderr(S_gfw);
368 mylib::PrintGcStats();
369 mylib::print_stderr(S_Aoo);
370 }
371}
372
373void HNodePrettyPrint(hnode_asdl::hnode_t* node, mylib::Writer* f, int max_width) {
374 StackRoot _root0(&node);
375 StackRoot _root1(&f);
376
377 _HNodePrettyPrint(false, true, node, f, max_width);
378}
379
380} // define namespace format
381
382namespace ansi { // define
383
384BigStr* RESET = S_yfk;
385BigStr* BOLD = S_aaF;
386BigStr* UNDERLINE = S_sCc;
387BigStr* REVERSE = S_woy;
388BigStr* RED = S_sqm;
389BigStr* GREEN = S_eda;
390BigStr* YELLOW = S_ysf;
391BigStr* BLUE = S_osl;
392BigStr* MAGENTA = S_vie;
393BigStr* CYAN = S_mmi;
394BigStr* WHITE = S_rpo;
395
396} // define namespace ansi
397
398namespace pretty { // define
399
400using pretty_asdl::doc;
401using pretty_asdl::doc_e;
402using pretty_asdl::DocFragment;
403using pretty_asdl::Measure;
404using pretty_asdl::MeasuredDoc;
405using pretty_asdl::List_Measured;
406using mylib::BufWriter;
407
408pretty_asdl::Measure* _EmptyMeasure() {
409 return Alloc<Measure>(0, -1);
410}
411
412pretty_asdl::Measure* _FlattenMeasure(pretty_asdl::Measure* measure) {
413 StackRoot _root0(&measure);
414
415 return Alloc<Measure>(measure->flat, -1);
416}
417
418pretty_asdl::Measure* _ConcatMeasure(pretty_asdl::Measure* m1, pretty_asdl::Measure* m2) {
419 StackRoot _root0(&m1);
420 StackRoot _root1(&m2);
421
422 if (m1->nonflat != -1) {
423 return Alloc<Measure>((m1->flat + m2->flat), m1->nonflat);
424 }
425 else {
426 if (m2->nonflat != -1) {
427 return Alloc<Measure>((m1->flat + m2->flat), (m1->flat + m2->nonflat));
428 }
429 else {
430 return Alloc<Measure>((m1->flat + m2->flat), -1);
431 }
432 }
433}
434
435int _SuffixLen(pretty_asdl::Measure* measure) {
436 StackRoot _root0(&measure);
437
438 if (measure->nonflat != -1) {
439 return measure->nonflat;
440 }
441 else {
442 return measure->flat;
443 }
444}
445
446pretty_asdl::MeasuredDoc* AsciiText(BigStr* string) {
447 StackRoot _root0(&string);
448
449 return Alloc<MeasuredDoc>(Alloc<doc::Text>(string), Alloc<Measure>(len(string), -1));
450}
451
452pretty_asdl::MeasuredDoc* _Break(BigStr* string) {
453 StackRoot _root0(&string);
454
455 return Alloc<MeasuredDoc>(Alloc<doc::Break>(string), Alloc<Measure>(len(string), 0));
456}
457
458pretty_asdl::MeasuredDoc* _Indent(int indent, pretty_asdl::MeasuredDoc* mdoc) {
459 StackRoot _root0(&mdoc);
460
461 return Alloc<MeasuredDoc>(Alloc<doc::Indent>(indent, mdoc), mdoc->measure);
462}
463
464pretty_asdl::Measure* _Splice(List<pretty_asdl::MeasuredDoc*>* out, List<pretty_asdl::MeasuredDoc*>* mdocs) {
465 pretty_asdl::Measure* measure = nullptr;
466 pretty_asdl::List_Measured* child = nullptr;
467 StackRoot _root0(&out);
468 StackRoot _root1(&mdocs);
469 StackRoot _root2(&measure);
470 StackRoot _root3(&child);
471
472 measure = _EmptyMeasure();
473 for (ListIter<pretty_asdl::MeasuredDoc*> it(mdocs); !it.Done(); it.Next()) {
474 pretty_asdl::MeasuredDoc* mdoc = it.Value();
475 StackRoot _for(&mdoc );
476 switch (mdoc->doc->tag()) {
477 case doc_e::Concat: {
478 child = static_cast<List_Measured*>(mdoc->doc);
479 _Splice(out, child);
480 }
481 break;
482 default: {
483 out->append(mdoc);
484 }
485 }
486 measure = _ConcatMeasure(measure, mdoc->measure);
487 }
488 return measure;
489}
490
491pretty_asdl::MeasuredDoc* _Concat(List<pretty_asdl::MeasuredDoc*>* mdocs) {
492 pretty_asdl::List_Measured* flattened = nullptr;
493 pretty_asdl::Measure* measure = nullptr;
494 StackRoot _root0(&mdocs);
495 StackRoot _root1(&flattened);
496 StackRoot _root2(&measure);
497
498 flattened = List_Measured::New();
499 measure = _Splice(flattened, mdocs);
500 return Alloc<MeasuredDoc>(flattened, measure);
501}
502
503pretty_asdl::MeasuredDoc* _Group(pretty_asdl::MeasuredDoc* mdoc) {
504 StackRoot _root0(&mdoc);
505
506 return Alloc<MeasuredDoc>(mdoc, mdoc->measure);
507}
508
509pretty_asdl::MeasuredDoc* _IfFlat(pretty_asdl::MeasuredDoc* flat_mdoc, pretty_asdl::MeasuredDoc* nonflat_mdoc) {
510 StackRoot _root0(&flat_mdoc);
511 StackRoot _root1(&nonflat_mdoc);
512
513 return Alloc<MeasuredDoc>(Alloc<doc::IfFlat>(flat_mdoc, nonflat_mdoc), Alloc<Measure>(flat_mdoc->measure->flat, nonflat_mdoc->measure->nonflat));
514}
515
516pretty_asdl::MeasuredDoc* _Flat(pretty_asdl::MeasuredDoc* mdoc) {
517 StackRoot _root0(&mdoc);
518
519 return Alloc<MeasuredDoc>(Alloc<doc::Flat>(mdoc), _FlattenMeasure(mdoc->measure));
520}
521
522PrettyPrinter::PrettyPrinter(int max_width) {
523 this->max_width = max_width;
524}
525
526bool PrettyPrinter::_Fits(int prefix_len, pretty_asdl::MeasuredDoc* group, pretty_asdl::Measure* suffix_measure) {
527 pretty_asdl::Measure* measure = nullptr;
528 StackRoot _root0(&group);
529 StackRoot _root1(&suffix_measure);
530 StackRoot _root2(&measure);
531
532 measure = _ConcatMeasure(_FlattenMeasure(group->measure), suffix_measure);
533 return (prefix_len + _SuffixLen(measure)) <= this->max_width;
534}
535
536void PrettyPrinter::PrintDoc(pretty_asdl::MeasuredDoc* document, mylib::BufWriter* buf) {
537 int prefix_len;
538 List<pretty_asdl::DocFragment*>* fragments = nullptr;
539 int max_stack;
540 pretty_asdl::DocFragment* frag = nullptr;
541 pretty_asdl::doc_t* UP_doc = nullptr;
542 pretty_asdl::Measure* measure = nullptr;
543 bool is_flat;
544 pretty_asdl::MeasuredDoc* subdoc = nullptr;
545 StackRoot _root0(&document);
546 StackRoot _root1(&buf);
547 StackRoot _root2(&fragments);
548 StackRoot _root3(&frag);
549 StackRoot _root4(&UP_doc);
550 StackRoot _root5(&measure);
551 StackRoot _root6(&subdoc);
552
553 prefix_len = 0;
554 fragments = NewList<pretty_asdl::DocFragment*>(std::initializer_list<pretty_asdl::DocFragment*>{Alloc<DocFragment>(_Group(document), 0, false, _EmptyMeasure())});
555 max_stack = len(fragments);
556 while (len(fragments) > 0) {
557 max_stack = max(max_stack, len(fragments));
558 frag = fragments->pop();
559 UP_doc = frag->mdoc->doc;
560 switch (UP_doc->tag()) {
561 case doc_e::Text: {
562 doc::Text* text = static_cast<doc::Text*>(UP_doc);
563 buf->write(text->string);
564 prefix_len += frag->mdoc->measure->flat;
565 }
566 break;
567 case doc_e::Break: {
568 doc::Break* break_ = static_cast<doc::Break*>(UP_doc);
569 if (frag->is_flat) {
570 buf->write(break_->string);
571 prefix_len += frag->mdoc->measure->flat;
572 }
573 else {
574 buf->write(S_nfs);
575 buf->write_spaces(frag->indent);
576 prefix_len = frag->indent;
577 }
578 }
579 break;
580 case doc_e::Indent: {
581 doc::Indent* indented = static_cast<doc::Indent*>(UP_doc);
582 fragments->append(Alloc<DocFragment>(indented->mdoc, (frag->indent + indented->indent), frag->is_flat, frag->measure));
583 }
584 break;
585 case doc_e::Concat: {
586 List_Measured* concat = static_cast<List_Measured*>(UP_doc);
587 measure = frag->measure;
588 for (ReverseListIter<pretty_asdl::MeasuredDoc*> it(concat); !it.Done(); it.Next()) {
589 pretty_asdl::MeasuredDoc* mdoc = it.Value();
590 StackRoot _for(&mdoc );
591 fragments->append(Alloc<DocFragment>(mdoc, frag->indent, frag->is_flat, measure));
592 measure = _ConcatMeasure(mdoc->measure, measure);
593 }
594 }
595 break;
596 case doc_e::Group: {
597 MeasuredDoc* group = static_cast<MeasuredDoc*>(UP_doc);
598 is_flat = this->_Fits(prefix_len, group, frag->measure);
599 fragments->append(Alloc<DocFragment>(group, frag->indent, is_flat, frag->measure));
600 }
601 break;
602 case doc_e::IfFlat: {
603 doc::IfFlat* if_flat = static_cast<doc::IfFlat*>(UP_doc);
604 if (frag->is_flat) {
605 subdoc = if_flat->flat_mdoc;
606 }
607 else {
608 subdoc = if_flat->nonflat_mdoc;
609 }
610 fragments->append(Alloc<DocFragment>(subdoc, frag->indent, frag->is_flat, frag->measure));
611 }
612 break;
613 case doc_e::Flat: {
614 doc::Flat* flat_doc = static_cast<doc::Flat*>(UP_doc);
615 fragments->append(Alloc<DocFragment>(flat_doc->mdoc, frag->indent, true, frag->measure));
616 }
617 break;
618 }
619 }
620}
621
622} // define namespace pretty
623
624namespace pp_hnode { // define
625
626using hnode_asdl::hnode;
627using hnode_asdl::hnode_e;
628using hnode_asdl::hnode_t;
629using hnode_asdl::Field;
630using hnode_asdl::color_e;
631using pretty_asdl::doc;
632using pretty_asdl::MeasuredDoc;
633using pretty_asdl::Measure;
634using pretty::_Break;
635using pretty::_Concat;
636using pretty::_Flat;
637using pretty::_Group;
638using pretty::_IfFlat;
639using pretty::_Indent;
640using pretty::_EmptyMeasure;
641using pretty::AsciiText;
642
643BaseEncoder::BaseEncoder() {
644 this->indent = 4;
645 this->use_styles = true;
646 this->max_tabular_width = 22;
647 this->visiting = Alloc<Dict<int, bool>>();
648}
649
650void BaseEncoder::SetIndent(int indent) {
651 this->indent = indent;
652}
653
654void BaseEncoder::SetUseStyles(bool use_styles) {
655 this->use_styles = use_styles;
656}
657
658void BaseEncoder::SetMaxTabularWidth(int max_tabular_width) {
659 this->max_tabular_width = max_tabular_width;
660}
661
662pretty_asdl::MeasuredDoc* BaseEncoder::_Styled(BigStr* style, pretty_asdl::MeasuredDoc* mdoc) {
663 StackRoot _root0(&style);
664 StackRoot _root1(&mdoc);
665
666 if (this->use_styles) {
667 return _Concat(NewList<pretty_asdl::MeasuredDoc*>(std::initializer_list<pretty_asdl::MeasuredDoc*>{Alloc<MeasuredDoc>(Alloc<doc::Text>(style), _EmptyMeasure()), mdoc, Alloc<MeasuredDoc>(Alloc<doc::Text>(ansi::RESET), _EmptyMeasure())}));
668 }
669 else {
670 return mdoc;
671 }
672}
673
674pretty_asdl::MeasuredDoc* BaseEncoder::_StyledAscii(BigStr* style, BigStr* s) {
675 pretty_asdl::Measure* measure = nullptr;
676 StackRoot _root0(&style);
677 StackRoot _root1(&s);
678 StackRoot _root2(&measure);
679
680 measure = Alloc<Measure>(len(s), -1);
681 if (this->use_styles) {
682 s = StrFormat("%s%s%s", style, s, ansi::RESET);
683 }
684 return Alloc<MeasuredDoc>(Alloc<doc::Text>(s), measure);
685}
686
687pretty_asdl::MeasuredDoc* BaseEncoder::_Surrounded(BigStr* left, pretty_asdl::MeasuredDoc* mdoc, BigStr* right) {
688 StackRoot _root0(&left);
689 StackRoot _root1(&mdoc);
690 StackRoot _root2(&right);
691
692 return _Group(_Concat(NewList<pretty_asdl::MeasuredDoc*>(std::initializer_list<pretty_asdl::MeasuredDoc*>{AsciiText(left), _Indent(this->indent, _Concat(NewList<pretty_asdl::MeasuredDoc*>(std::initializer_list<pretty_asdl::MeasuredDoc*>{_Break(S_Aoo), mdoc}))), _Break(S_Aoo), AsciiText(right)})));
693}
694
695pretty_asdl::MeasuredDoc* BaseEncoder::_SurroundedAndPrefixed(BigStr* left, pretty_asdl::MeasuredDoc* prefix, BigStr* sep, pretty_asdl::MeasuredDoc* mdoc, BigStr* right) {
696 StackRoot _root0(&left);
697 StackRoot _root1(&prefix);
698 StackRoot _root2(&sep);
699 StackRoot _root3(&mdoc);
700 StackRoot _root4(&right);
701
702 return _Group(_Concat(NewList<pretty_asdl::MeasuredDoc*>(std::initializer_list<pretty_asdl::MeasuredDoc*>{AsciiText(left), prefix, _Indent(this->indent, _Concat(NewList<pretty_asdl::MeasuredDoc*>(std::initializer_list<pretty_asdl::MeasuredDoc*>{_Break(sep), mdoc}))), _Break(S_Aoo), AsciiText(right)})));
703}
704
705pretty_asdl::MeasuredDoc* BaseEncoder::_Join(List<pretty_asdl::MeasuredDoc*>* items, BigStr* sep, BigStr* space) {
706 List<pretty_asdl::MeasuredDoc*>* seq = nullptr;
707 int i;
708 StackRoot _root0(&items);
709 StackRoot _root1(&sep);
710 StackRoot _root2(&space);
711 StackRoot _root3(&seq);
712
713 seq = Alloc<List<pretty_asdl::MeasuredDoc*>>();
714 i = 0;
715 for (ListIter<pretty_asdl::MeasuredDoc*> it(items); !it.Done(); it.Next(), ++i) {
716 pretty_asdl::MeasuredDoc* item = it.Value();
717 StackRoot _for(&item );
718 if (i != 0) {
719 seq->append(AsciiText(sep));
720 seq->append(_Break(space));
721 }
722 seq->append(item);
723 }
724 return _Concat(seq);
725}
726
727pretty_asdl::MeasuredDoc* BaseEncoder::_Tabular(List<pretty_asdl::MeasuredDoc*>* items, BigStr* sep) {
728 int max_flat_len;
729 List<pretty_asdl::MeasuredDoc*>* seq = nullptr;
730 int i;
731 pretty_asdl::MeasuredDoc* non_tabular = nullptr;
732 int sep_width;
733 List<pretty_asdl::MeasuredDoc*>* tabular_seq = nullptr;
734 int padding;
735 pretty_asdl::MeasuredDoc* tabular = nullptr;
736 StackRoot _root0(&items);
737 StackRoot _root1(&sep);
738 StackRoot _root2(&seq);
739 StackRoot _root3(&non_tabular);
740 StackRoot _root4(&tabular_seq);
741 StackRoot _root5(&tabular);
742
743 if (len(items) == 0) {
744 return AsciiText(S_Aoo);
745 }
746 max_flat_len = 0;
747 seq = Alloc<List<pretty_asdl::MeasuredDoc*>>();
748 i = 0;
749 for (ListIter<pretty_asdl::MeasuredDoc*> it(items); !it.Done(); it.Next(), ++i) {
750 pretty_asdl::MeasuredDoc* item = it.Value();
751 StackRoot _for(&item );
752 if (i != 0) {
753 seq->append(AsciiText(sep));
754 seq->append(_Break(S_yfw));
755 }
756 seq->append(item);
757 max_flat_len = max(max_flat_len, item->measure->flat);
758 }
759 non_tabular = _Concat(seq);
760 sep_width = len(sep);
761 if (((max_flat_len + sep_width) + 1) <= this->max_tabular_width) {
762 tabular_seq = Alloc<List<pretty_asdl::MeasuredDoc*>>();
763 i = 0;
764 for (ListIter<pretty_asdl::MeasuredDoc*> it(items); !it.Done(); it.Next(), ++i) {
765 pretty_asdl::MeasuredDoc* item = it.Value();
766 StackRoot _for(&item );
767 tabular_seq->append(_Flat(item));
768 if (i != (len(items) - 1)) {
769 padding = ((max_flat_len - item->measure->flat) + 1);
770 tabular_seq->append(AsciiText(sep));
771 tabular_seq->append(_Group(_Break(str_repeat(S_yfw, padding))));
772 }
773 }
774 tabular = _Concat(tabular_seq);
775 return _Group(_IfFlat(non_tabular, tabular));
776 }
777 else {
778 return non_tabular;
779 }
780}
781
782HNodeEncoder::HNodeEncoder() : ::pp_hnode::BaseEncoder() {
783 this->type_color = ansi::YELLOW;
784 this->field_color = ansi::MAGENTA;
785}
786
787pretty_asdl::MeasuredDoc* HNodeEncoder::HNode(hnode_asdl::hnode_t* h) {
788 StackRoot _root0(&h);
789
790 this->visiting->clear();
791 return this->_HNode(h);
792}
793
794pretty_asdl::MeasuredDoc* HNodeEncoder::_Field(hnode_asdl::Field* field) {
795 pretty_asdl::MeasuredDoc* name = nullptr;
796 StackRoot _root0(&field);
797 StackRoot _root1(&name);
798
799 name = AsciiText(str_concat(field->name, S_fyj));
800 return _Concat(NewList<pretty_asdl::MeasuredDoc*>(std::initializer_list<pretty_asdl::MeasuredDoc*>{name, this->_HNode(field->val)}));
801}
802
803pretty_asdl::MeasuredDoc* HNodeEncoder::_HNode(hnode_asdl::hnode_t* h) {
804 hnode_asdl::hnode_t* UP_h = nullptr;
805 BigStr* color = nullptr;
806 BigStr* s = nullptr;
807 List<pretty_asdl::MeasuredDoc*>* children = nullptr;
808 pretty_asdl::MeasuredDoc* type_name = nullptr;
809 List<pretty_asdl::MeasuredDoc*>* mdocs = nullptr;
810 List<pretty_asdl::MeasuredDoc*>* m = nullptr;
811 pretty_asdl::MeasuredDoc* child = nullptr;
812 StackRoot _root0(&h);
813 StackRoot _root1(&UP_h);
814 StackRoot _root2(&color);
815 StackRoot _root3(&s);
816 StackRoot _root4(&children);
817 StackRoot _root5(&type_name);
818 StackRoot _root6(&mdocs);
819 StackRoot _root7(&m);
820 StackRoot _root8(&child);
821
822 UP_h = h;
823 switch (h->tag()) {
824 case hnode_e::AlreadySeen: {
825 hnode::AlreadySeen* h = static_cast<hnode::AlreadySeen*>(UP_h);
826 return pretty::AsciiText(StrFormat("...0x%s", mylib::hex_lower(h->heap_id)));
827 }
828 break;
829 case hnode_e::Leaf: {
830 hnode::Leaf* h = static_cast<hnode::Leaf*>(UP_h);
831 switch (h->color) {
832 case color_e::TypeName: {
833 color = ansi::YELLOW;
834 }
835 break;
836 case color_e::StringConst: {
837 color = ansi::BOLD;
838 }
839 break;
840 case color_e::OtherConst: {
841 color = ansi::GREEN;
842 }
843 break;
844 case color_e::External: {
845 color = str_concat(ansi::BOLD, ansi::BLUE);
846 }
847 break;
848 case color_e::UserType: {
849 color = ansi::GREEN;
850 }
851 break;
852 default: {
853 assert(0); // AssertionError
854 }
855 }
856 s = j8_lite::EncodeString(h->s, true);
857 return this->_StyledAscii(color, s);
858 }
859 break;
860 case hnode_e::Array: {
861 hnode::Array* h = static_cast<hnode::Array*>(UP_h);
862 mylib::MaybeCollect();
863 if (len(h->children) == 0) {
864 return AsciiText(S_xmu);
865 }
866 children = Alloc<List<pretty_asdl::MeasuredDoc*>>();
867 for (ListIter<hnode_asdl::hnode_t*> it(h->children); !it.Done(); it.Next()) {
868 hnode_asdl::hnode_t* item = it.Value();
869 children->append(this->_HNode(item));
870 }
871 return this->_Surrounded(S_Eax, this->_Tabular(children, S_Aoo), S_pcD);
872 }
873 break;
874 case hnode_e::Record: {
875 hnode::Record* h = static_cast<hnode::Record*>(UP_h);
876 type_name = nullptr;
877 if (len(h->node_type)) {
878 type_name = this->_StyledAscii(this->type_color, h->node_type);
879 }
880 mdocs = nullptr;
881 if ((h->unnamed_fields != nullptr and len(h->unnamed_fields))) {
882 mdocs = Alloc<List<pretty_asdl::MeasuredDoc*>>();
883 for (ListIter<hnode_asdl::hnode_t*> it(h->unnamed_fields); !it.Done(); it.Next()) {
884 hnode_asdl::hnode_t* item = it.Value();
885 mdocs->append(this->_HNode(item));
886 }
887 }
888 else {
889 if (len(h->fields) != 0) {
890 mdocs = Alloc<List<pretty_asdl::MeasuredDoc*>>();
891 for (ListIter<hnode_asdl::Field*> it(h->fields); !it.Done(); it.Next()) {
892 hnode_asdl::Field* field = it.Value();
893 mdocs->append(this->_Field(field));
894 }
895 }
896 }
897 if (mdocs == nullptr) {
898 m = NewList<pretty_asdl::MeasuredDoc*>(std::initializer_list<pretty_asdl::MeasuredDoc*>{AsciiText(h->left)});
899 if (type_name != nullptr) {
900 m->append(type_name);
901 }
902 m->append(AsciiText(h->right));
903 return _Concat(m);
904 }
905 child = this->_Join(mdocs, S_Aoo, S_yfw);
906 if (type_name != nullptr) {
907 return this->_SurroundedAndPrefixed(h->left, type_name, S_yfw, child, h->right);
908 }
909 else {
910 return this->_Surrounded(h->left, child, h->right);
911 }
912 }
913 break;
914 default: {
915 assert(0); // AssertionError
916 }
917 }
918}
919
920} // define namespace pp_hnode
921
922namespace cgi { // define
923
924
925BigStr* escape(BigStr* s) {
926 StackRoot _root0(&s);
927
928 s = s->replace(S_Clt, S_usD);
929 s = s->replace(S_eox, S_Dcl);
930 s = s->replace(S_jye, S_dyr);
931 return s;
932}
933
934} // define namespace cgi
935
936namespace j8_lite { // define
937
938
939BigStr* EncodeString(BigStr* s, bool unquoted_ok) {
940 StackRoot _root0(&s);
941
942 if ((unquoted_ok and fastfunc::CanOmitQuotes(s))) {
943 return s;
944 }
945 return fastfunc::J8EncodeString(s, 1);
946}
947
948BigStr* YshEncodeString(BigStr* s) {
949 StackRoot _root0(&s);
950
951 return fastfunc::ShellEncodeString(s, 1);
952}
953
954BigStr* MaybeShellEncode(BigStr* s) {
955 StackRoot _root0(&s);
956
957 if (fastfunc::CanOmitQuotes(s)) {
958 return s;
959 }
960 return fastfunc::ShellEncodeString(s, 0);
961}
962
963BigStr* ShellEncode(BigStr* s) {
964 StackRoot _root0(&s);
965
966 return fastfunc::ShellEncodeString(s, 0);
967}
968
969BigStr* YshEncode(BigStr* s, bool unquoted_ok) {
970 StackRoot _root0(&s);
971
972 if ((unquoted_ok and fastfunc::CanOmitQuotes(s))) {
973 return s;
974 }
975 return fastfunc::ShellEncodeString(s, 1);
976}
977
978} // define namespace j8_lite
979