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

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