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 |
|
8 | GLOBAL_STR(str0, "(");
|
9 | GLOBAL_STR(str1, ")");
|
10 | GLOBAL_STR(str2, "_");
|
11 | GLOBAL_STR(str3, "T");
|
12 | GLOBAL_STR(str4, "F");
|
13 | GLOBAL_STR(str5, "\n<html>\n <head>\n <title>oil 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");
|
14 | GLOBAL_STR(str6, "\n </pre>\n </body>\n</html>\n ");
|
15 | GLOBAL_STR(str7, "n");
|
16 | GLOBAL_STR(str8, "s");
|
17 | GLOBAL_STR(str9, "o");
|
18 | GLOBAL_STR(str10, "o");
|
19 | GLOBAL_STR(str11, "o");
|
20 | GLOBAL_STR(str12, "<span class=\"%s\">");
|
21 | GLOBAL_STR(str13, "</span>");
|
22 | GLOBAL_STR(str14, " ");
|
23 | GLOBAL_STR(str15, "\n");
|
24 | GLOBAL_STR(str16, " ");
|
25 | GLOBAL_STR(str17, "]");
|
26 | GLOBAL_STR(str18, " ");
|
27 | GLOBAL_STR(str19, " ");
|
28 | GLOBAL_STR(str20, "\n");
|
29 | GLOBAL_STR(str21, "\n");
|
30 | GLOBAL_STR(str22, " ");
|
31 | GLOBAL_STR(str23, "%s%s: [");
|
32 | GLOBAL_STR(str24, "\n");
|
33 | GLOBAL_STR(str25, "\n");
|
34 | GLOBAL_STR(str26, "%s]");
|
35 | GLOBAL_STR(str27, "%s%s: ");
|
36 | GLOBAL_STR(str28, "\n");
|
37 | GLOBAL_STR(str29, "\n");
|
38 | GLOBAL_STR(str30, " ");
|
39 | GLOBAL_STR(str31, "UNTYPED any");
|
40 | GLOBAL_STR(str32, "...0x%s");
|
41 | GLOBAL_STR(str33, " ");
|
42 | GLOBAL_STR(str34, " ");
|
43 | GLOBAL_STR(str35, " %s:");
|
44 | GLOBAL_STR(str36, "UNTYPED any");
|
45 | GLOBAL_STR(str37, "[");
|
46 | GLOBAL_STR(str38, " ");
|
47 | GLOBAL_STR(str39, "]");
|
48 | GLOBAL_STR(str40, "...0x%s");
|
49 | GLOBAL_STR(str41, "foo");
|
50 | GLOBAL_STR(str42, "\n");
|
51 | GLOBAL_STR(str43, "\u001b[0;0m");
|
52 | GLOBAL_STR(str44, "\u001b[1m");
|
53 | GLOBAL_STR(str45, "\u001b[4m");
|
54 | GLOBAL_STR(str46, "\u001b[7m");
|
55 | GLOBAL_STR(str47, "\u001b[31m");
|
56 | GLOBAL_STR(str48, "\u001b[32m");
|
57 | GLOBAL_STR(str49, "\u001b[33m");
|
58 | GLOBAL_STR(str50, "\u001b[34m");
|
59 | GLOBAL_STR(str51, "\u001b[35m");
|
60 | GLOBAL_STR(str52, "\u001b[36m");
|
61 | GLOBAL_STR(str53, "\u001b[37m");
|
62 | GLOBAL_STR(str54, "\n");
|
63 | GLOBAL_STR(str55, "&");
|
64 | GLOBAL_STR(str56, "&");
|
65 | GLOBAL_STR(str57, "<");
|
66 | GLOBAL_STR(str58, "<");
|
67 | GLOBAL_STR(str59, ">");
|
68 | GLOBAL_STR(str60, ">");
|
69 |
|
70 | namespace ansi { // forward declare
|
71 |
|
72 |
|
73 | } // forward declare namespace ansi
|
74 |
|
75 | namespace pretty { // forward declare
|
76 |
|
77 | class PrettyPrinter;
|
78 |
|
79 | } // forward declare namespace pretty
|
80 |
|
81 | namespace cgi { // forward declare
|
82 |
|
83 |
|
84 | } // forward declare namespace cgi
|
85 |
|
86 | namespace j8_lite { // forward declare
|
87 |
|
88 |
|
89 | } // forward declare namespace j8_lite
|
90 |
|
91 | namespace ansi { // declare
|
92 |
|
93 | extern BigStr* RESET;
|
94 | extern BigStr* BOLD;
|
95 | extern BigStr* UNDERLINE;
|
96 | extern BigStr* REVERSE;
|
97 | extern BigStr* RED;
|
98 | extern BigStr* GREEN;
|
99 | extern BigStr* YELLOW;
|
100 | extern BigStr* BLUE;
|
101 | extern BigStr* MAGENTA;
|
102 | extern BigStr* CYAN;
|
103 | extern BigStr* WHITE;
|
104 |
|
105 | } // declare namespace ansi
|
106 |
|
107 | namespace pretty { // declare
|
108 |
|
109 | pretty_asdl::Measure* _EmptyMeasure();
|
110 | pretty_asdl::Measure* _FlattenMeasure(pretty_asdl::Measure* measure);
|
111 | pretty_asdl::Measure* _ConcatMeasure(pretty_asdl::Measure* m1, pretty_asdl::Measure* m2);
|
112 | int _SuffixLen(pretty_asdl::Measure* measure);
|
113 | pretty_asdl::MeasuredDoc* AsciiText(BigStr* string);
|
114 | pretty_asdl::MeasuredDoc* _Break(BigStr* string);
|
115 | pretty_asdl::MeasuredDoc* _Indent(int indent, pretty_asdl::MeasuredDoc* mdoc);
|
116 | pretty_asdl::MeasuredDoc* _Concat(List<pretty_asdl::MeasuredDoc*>* mdocs);
|
117 | pretty_asdl::MeasuredDoc* _Group(pretty_asdl::MeasuredDoc* mdoc);
|
118 | pretty_asdl::MeasuredDoc* _IfFlat(pretty_asdl::MeasuredDoc* flat_mdoc, pretty_asdl::MeasuredDoc* nonflat_mdoc);
|
119 | pretty_asdl::MeasuredDoc* _Flat(pretty_asdl::MeasuredDoc* mdoc);
|
120 | class 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 |
|
137 | namespace cgi { // declare
|
138 |
|
139 | BigStr* escape(BigStr* s);
|
140 |
|
141 | } // declare namespace cgi
|
142 |
|
143 | namespace j8_lite { // declare
|
144 |
|
145 | BigStr* EncodeString(BigStr* s, bool unquoted_ok = false);
|
146 | BigStr* YshEncodeString(BigStr* s);
|
147 | BigStr* MaybeShellEncode(BigStr* s);
|
148 | BigStr* ShellEncode(BigStr* s);
|
149 | BigStr* YshEncode(BigStr* s, bool unquoted_ok = false);
|
150 |
|
151 | } // declare namespace j8_lite
|
152 |
|
153 | namespace runtime { // define
|
154 |
|
155 | using hnode_asdl::hnode;
|
156 | using hnode_asdl::color_t;
|
157 | using hnode_asdl::color_e;
|
158 | int NO_SPID = -1;
|
159 |
|
160 | hnode::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 |
|
166 | hnode::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 |
|
177 | TraversalState::TraversalState() {
|
178 | this->seen = Alloc<Dict<int, bool>>();
|
179 | this->ref_count = Alloc<Dict<int, int>>();
|
180 | }
|
181 | BigStr* TRUE_STR = str3;
|
182 | BigStr* FALSE_STR = str4;
|
183 |
|
184 | } // define namespace runtime
|
185 |
|
186 | namespace format { // define
|
187 |
|
188 | using hnode_asdl::hnode;
|
189 | using hnode_asdl::hnode_e;
|
190 | using hnode_asdl::hnode_t;
|
191 | using hnode_asdl::color_e;
|
192 | using hnode_asdl::color_t;
|
193 |
|
194 | format::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 |
|
205 | ColorOutput::ColorOutput(mylib::Writer* f) {
|
206 | this->f = f;
|
207 | this->num_chars = 0;
|
208 | }
|
209 |
|
210 | format::ColorOutput* ColorOutput::NewTempBuffer() {
|
211 | FAIL(kNotImplemented); // Python NotImplementedError
|
212 | }
|
213 |
|
214 | void ColorOutput::FileHeader() {
|
215 | ; // pass
|
216 | }
|
217 |
|
218 | void ColorOutput::FileFooter() {
|
219 | ; // pass
|
220 | }
|
221 |
|
222 | void ColorOutput::PushColor(hnode_asdl::color_t e_color) {
|
223 | FAIL(kNotImplemented); // Python NotImplementedError
|
224 | }
|
225 |
|
226 | void ColorOutput::PopColor() {
|
227 | FAIL(kNotImplemented); // Python NotImplementedError
|
228 | }
|
229 |
|
230 | void ColorOutput::write(BigStr* s) {
|
231 | StackRoot _root0(&s);
|
232 |
|
233 | this->f->write(s);
|
234 | this->num_chars += len(s);
|
235 | }
|
236 |
|
237 | void 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 |
|
250 | int ColorOutput::NumChars() {
|
251 | return this->num_chars;
|
252 | }
|
253 |
|
254 | Tuple2<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 |
|
262 | TextOutput::TextOutput(mylib::Writer* f) : ::format::ColorOutput(f) {
|
263 | }
|
264 |
|
265 | format::TextOutput* TextOutput::NewTempBuffer() {
|
266 | return Alloc<TextOutput>(Alloc<mylib::BufWriter>());
|
267 | }
|
268 |
|
269 | void TextOutput::PushColor(hnode_asdl::color_t e_color) {
|
270 | ; // pass
|
271 | }
|
272 |
|
273 | void TextOutput::PopColor() {
|
274 | ; // pass
|
275 | }
|
276 |
|
277 | HtmlOutput::HtmlOutput(mylib::Writer* f) : ::format::ColorOutput(f) {
|
278 | }
|
279 |
|
280 | format::HtmlOutput* HtmlOutput::NewTempBuffer() {
|
281 | return Alloc<HtmlOutput>(Alloc<mylib::BufWriter>());
|
282 | }
|
283 |
|
284 | void HtmlOutput::FileHeader() {
|
285 | this->f->write(str5);
|
286 | }
|
287 |
|
288 | void HtmlOutput::FileFooter() {
|
289 | this->f->write(str6);
|
290 | }
|
291 |
|
292 | void 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 |
|
325 | void HtmlOutput::PopColor() {
|
326 | this->f->write(str13);
|
327 | }
|
328 |
|
329 | void HtmlOutput::write(BigStr* s) {
|
330 | StackRoot _root0(&s);
|
331 |
|
332 | this->f->write(cgi::escape(s));
|
333 | this->num_chars += len(s);
|
334 | }
|
335 |
|
336 | AnsiOutput::AnsiOutput(mylib::Writer* f) : ::format::ColorOutput(f) {
|
337 | }
|
338 |
|
339 | format::AnsiOutput* AnsiOutput::NewTempBuffer() {
|
340 | return Alloc<AnsiOutput>(Alloc<mylib::BufWriter>());
|
341 | }
|
342 |
|
343 | void 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 |
|
372 | void AnsiOutput::PopColor() {
|
373 | this->f->write(ansi::RESET);
|
374 | }
|
375 | int INDENT = 2;
|
376 |
|
377 | _PrettyPrinter::_PrettyPrinter(int max_col) {
|
378 | this->max_col = max_col;
|
379 | }
|
380 |
|
381 | bool _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 |
|
420 | bool _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 |
|
468 | void _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 |
|
564 | void _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 |
|
625 | bool _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 |
|
667 | bool _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 |
|
736 | void 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 |
|
746 | void 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 |
|
766 | namespace ansi { // define
|
767 |
|
768 | BigStr* RESET = str43;
|
769 | BigStr* BOLD = str44;
|
770 | BigStr* UNDERLINE = str45;
|
771 | BigStr* REVERSE = str46;
|
772 | BigStr* RED = str47;
|
773 | BigStr* GREEN = str48;
|
774 | BigStr* YELLOW = str49;
|
775 | BigStr* BLUE = str50;
|
776 | BigStr* MAGENTA = str51;
|
777 | BigStr* CYAN = str52;
|
778 | BigStr* WHITE = str53;
|
779 |
|
780 | } // define namespace ansi
|
781 |
|
782 | namespace pretty { // define
|
783 |
|
784 | using pretty_asdl::doc;
|
785 | using pretty_asdl::doc_e;
|
786 | using pretty_asdl::DocFragment;
|
787 | using pretty_asdl::Measure;
|
788 | using pretty_asdl::MeasuredDoc;
|
789 | using mylib::BufWriter;
|
790 |
|
791 | pretty_asdl::Measure* _EmptyMeasure() {
|
792 | return Alloc<Measure>(0, -1);
|
793 | }
|
794 |
|
795 | pretty_asdl::Measure* _FlattenMeasure(pretty_asdl::Measure* measure) {
|
796 | StackRoot _root0(&measure);
|
797 |
|
798 | return Alloc<Measure>(measure->flat, -1);
|
799 | }
|
800 |
|
801 | pretty_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 |
|
818 | int _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 |
|
829 | pretty_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 |
|
835 | pretty_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 |
|
841 | pretty_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 |
|
847 | pretty_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 |
|
861 | pretty_asdl::MeasuredDoc* _Group(pretty_asdl::MeasuredDoc* mdoc) {
|
862 | StackRoot _root0(&mdoc);
|
863 |
|
864 | return Alloc<MeasuredDoc>(Alloc<doc::Group>(mdoc), mdoc->measure);
|
865 | }
|
866 |
|
867 | pretty_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 |
|
874 | pretty_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 |
|
880 | PrettyPrinter::PrettyPrinter(int max_width) {
|
881 | this->max_width = max_width;
|
882 | }
|
883 |
|
884 | bool 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 |
|
894 | void 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 |
|
990 | namespace cgi { // define
|
991 |
|
992 |
|
993 | BigStr* 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 |
|
1004 | namespace j8_lite { // define
|
1005 |
|
1006 |
|
1007 | BigStr* 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 |
|
1016 | BigStr* YshEncodeString(BigStr* s) {
|
1017 | StackRoot _root0(&s);
|
1018 |
|
1019 | return fastfunc::ShellEncodeString(s, 1);
|
1020 | }
|
1021 |
|
1022 | BigStr* 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 |
|
1031 | BigStr* ShellEncode(BigStr* s) {
|
1032 | StackRoot _root0(&s);
|
1033 |
|
1034 | return fastfunc::ShellEncodeString(s, 0);
|
1035 | }
|
1036 |
|
1037 | BigStr* 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 |
|