OILS / prebuilt / frontend / args.mycpp.cc View on Github | oilshell.org

2191 lines, 1491 significant
1// prebuilt/frontend/args.mycpp.cc: GENERATED by mycpp
2
3#include "prebuilt/frontend/args.mycpp.h"
4// BEGIN mycpp output
5
6#include "mycpp/runtime.h"
7
8GLOBAL_STR(str0, "(");
9GLOBAL_STR(str1, ")");
10GLOBAL_STR(str2, "_");
11GLOBAL_STR(str3, "T");
12GLOBAL_STR(str4, "F");
13GLOBAL_STR(str5, "\n<html>\n <head>\n <title>Oils AST</title>\n <style>\n .n { color: brown }\n .s { font-weight: bold }\n .o { color: darkgreen }\n </style>\n </head>\n <body>\n <pre>\n");
14GLOBAL_STR(str6, "\n </pre>\n </body>\n</html>\n ");
15GLOBAL_STR(str7, "n");
16GLOBAL_STR(str8, "s");
17GLOBAL_STR(str9, "o");
18GLOBAL_STR(str10, "o");
19GLOBAL_STR(str11, "o");
20GLOBAL_STR(str12, "<span class=\"%s\">");
21GLOBAL_STR(str13, "</span>");
22GLOBAL_STR(str14, " ");
23GLOBAL_STR(str15, "\n");
24GLOBAL_STR(str16, " ");
25GLOBAL_STR(str17, "]");
26GLOBAL_STR(str18, " ");
27GLOBAL_STR(str19, " ");
28GLOBAL_STR(str20, "\n");
29GLOBAL_STR(str21, "\n");
30GLOBAL_STR(str22, " ");
31GLOBAL_STR(str23, "%s%s: [");
32GLOBAL_STR(str24, "\n");
33GLOBAL_STR(str25, "\n");
34GLOBAL_STR(str26, "%s]");
35GLOBAL_STR(str27, "%s%s: ");
36GLOBAL_STR(str28, "\n");
37GLOBAL_STR(str29, "\n");
38GLOBAL_STR(str30, " ");
39GLOBAL_STR(str31, "UNTYPED any");
40GLOBAL_STR(str32, "...0x%s");
41GLOBAL_STR(str33, " ");
42GLOBAL_STR(str34, " ");
43GLOBAL_STR(str35, " %s:");
44GLOBAL_STR(str36, "UNTYPED any");
45GLOBAL_STR(str37, "[");
46GLOBAL_STR(str38, " ");
47GLOBAL_STR(str39, "]");
48GLOBAL_STR(str40, "...0x%s");
49GLOBAL_STR(str41, "foo");
50GLOBAL_STR(str42, "\n");
51GLOBAL_STR(str43, "\u001b[0;0m");
52GLOBAL_STR(str44, "\u001b[1m");
53GLOBAL_STR(str45, "\u001b[4m");
54GLOBAL_STR(str46, "\u001b[7m");
55GLOBAL_STR(str47, "\u001b[31m");
56GLOBAL_STR(str48, "\u001b[32m");
57GLOBAL_STR(str49, "\u001b[33m");
58GLOBAL_STR(str50, "\u001b[34m");
59GLOBAL_STR(str51, "\u001b[35m");
60GLOBAL_STR(str52, "\u001b[36m");
61GLOBAL_STR(str53, "\u001b[37m");
62GLOBAL_STR(str54, "\n");
63GLOBAL_STR(str55, "&");
64GLOBAL_STR(str56, "&amp;");
65GLOBAL_STR(str57, "<");
66GLOBAL_STR(str58, "&lt;");
67GLOBAL_STR(str59, ">");
68GLOBAL_STR(str60, "&gt;");
69GLOBAL_STR(str61, "<%s %r>");
70GLOBAL_STR(str62, "code");
71GLOBAL_STR(str63, "message");
72GLOBAL_STR(str64, "%s, got %s");
73GLOBAL_STR(str65, " (line %d, offset %d-%d: %r)");
74GLOBAL_STR(str66, "-");
75GLOBAL_STR(str67, "_");
76GLOBAL_STR(str68, "extern");
77GLOBAL_STR(str69, "extern_");
78GLOBAL_STR(str70, "<_Attributes %s>");
79GLOBAL_STR(str71, "<args.Reader %r %d>");
80GLOBAL_STR(str72, "got too many arguments");
81GLOBAL_STR(str73, "expected argument to %r");
82GLOBAL_STR(str74, "-");
83GLOBAL_STR(str75, "Integer too big: %s");
84GLOBAL_STR(str76, "expected integer after %s, got %r");
85GLOBAL_STR(str77, "-");
86GLOBAL_STR(str78, "got invalid integer for %s: %s");
87GLOBAL_STR(str79, "-");
88GLOBAL_STR(str80, "expected number after %r, got %r");
89GLOBAL_STR(str81, "-");
90GLOBAL_STR(str82, "got invalid float for %s: %s");
91GLOBAL_STR(str83, "-");
92GLOBAL_STR(str84, "got invalid argument %r to %r, expected one of: %s");
93GLOBAL_STR(str85, "-");
94GLOBAL_STR(str86, "|");
95GLOBAL_STR(str87, "0");
96GLOBAL_STR(str88, "F");
97GLOBAL_STR(str89, "false");
98GLOBAL_STR(str90, "False");
99GLOBAL_STR(str91, "1");
100GLOBAL_STR(str92, "T");
101GLOBAL_STR(str93, "true");
102GLOBAL_STR(str94, "Talse");
103GLOBAL_STR(str95, "got invalid argument to boolean flag: %r");
104GLOBAL_STR(str96, "-");
105GLOBAL_STR(str97, "-");
106GLOBAL_STR(str98, "Invalid option %r");
107GLOBAL_STR(str99, "Expected argument for action");
108GLOBAL_STR(str100, "Invalid action name %r");
109GLOBAL_STR(str101, "--");
110GLOBAL_STR(str102, "--");
111GLOBAL_STR(str103, "=");
112GLOBAL_STR(str104, "got invalid flag %r");
113GLOBAL_STR(str105, "-");
114GLOBAL_STR(str106, "0");
115GLOBAL_STR(str107, "Z");
116GLOBAL_STR(str108, "-");
117GLOBAL_STR(str109, "doesn't accept flag %s");
118GLOBAL_STR(str110, "-");
119GLOBAL_STR(str111, "+");
120GLOBAL_STR(str112, "+");
121GLOBAL_STR(str113, "doesn't accept option %s");
122GLOBAL_STR(str114, "+");
123GLOBAL_STR(str115, "-");
124GLOBAL_STR(str116, "--");
125GLOBAL_STR(str117, "--");
126GLOBAL_STR(str118, "got invalid flag %r");
127GLOBAL_STR(str119, "-");
128GLOBAL_STR(str120, "+");
129GLOBAL_STR(str121, "got invalid flag %r");
130GLOBAL_STR(str122, "-");
131
132namespace ansi { // forward declare
133
134
135} // forward declare namespace ansi
136
137namespace pretty { // forward declare
138
139 class PrettyPrinter;
140
141} // forward declare namespace pretty
142
143namespace cgi { // forward declare
144
145
146} // forward declare namespace cgi
147
148namespace j8_lite { // forward declare
149
150
151} // forward declare namespace j8_lite
152
153namespace error { // forward declare
154
155 class _ErrorWithLocation;
156 class Usage;
157 class Parse;
158 class FailGlob;
159 class RedirectEval;
160 class FatalRuntime;
161 class Strict;
162 class ErrExit;
163 class Expr;
164 class Structured;
165 class AssertionErr;
166 class TypeErrVerbose;
167 class TypeErr;
168 class Runtime;
169 class Decode;
170 class Encode;
171
172} // forward declare namespace error
173
174namespace num { // forward declare
175
176
177} // forward declare namespace num
178
179namespace ansi { // declare
180
181extern BigStr* RESET;
182extern BigStr* BOLD;
183extern BigStr* UNDERLINE;
184extern BigStr* REVERSE;
185extern BigStr* RED;
186extern BigStr* GREEN;
187extern BigStr* YELLOW;
188extern BigStr* BLUE;
189extern BigStr* MAGENTA;
190extern BigStr* CYAN;
191extern BigStr* WHITE;
192
193} // declare namespace ansi
194
195namespace pretty { // declare
196
197pretty_asdl::Measure* _EmptyMeasure();
198pretty_asdl::Measure* _FlattenMeasure(pretty_asdl::Measure* measure);
199pretty_asdl::Measure* _ConcatMeasure(pretty_asdl::Measure* m1, pretty_asdl::Measure* m2);
200int _SuffixLen(pretty_asdl::Measure* measure);
201pretty_asdl::MeasuredDoc* AsciiText(BigStr* string);
202pretty_asdl::MeasuredDoc* _Break(BigStr* string);
203pretty_asdl::MeasuredDoc* _Indent(int indent, pretty_asdl::MeasuredDoc* mdoc);
204pretty_asdl::MeasuredDoc* _Concat(List<pretty_asdl::MeasuredDoc*>* mdocs);
205pretty_asdl::MeasuredDoc* _Group(pretty_asdl::MeasuredDoc* mdoc);
206pretty_asdl::MeasuredDoc* _IfFlat(pretty_asdl::MeasuredDoc* flat_mdoc, pretty_asdl::MeasuredDoc* nonflat_mdoc);
207pretty_asdl::MeasuredDoc* _Flat(pretty_asdl::MeasuredDoc* mdoc);
208class PrettyPrinter {
209 public:
210 PrettyPrinter(int max_width);
211 bool _Fits(int prefix_len, doc::Group* group, pretty_asdl::Measure* suffix_measure);
212 void PrintDoc(pretty_asdl::MeasuredDoc* document, mylib::BufWriter* buf);
213 int max_width{};
214
215 static constexpr ObjHeader obj_header() {
216 return ObjHeader::ClassScanned(0, sizeof(PrettyPrinter));
217 }
218
219 DISALLOW_COPY_AND_ASSIGN(PrettyPrinter)
220};
221
222
223} // declare namespace pretty
224
225namespace cgi { // declare
226
227BigStr* escape(BigStr* s);
228
229} // declare namespace cgi
230
231namespace j8_lite { // declare
232
233BigStr* EncodeString(BigStr* s, bool unquoted_ok = false);
234BigStr* YshEncodeString(BigStr* s);
235BigStr* MaybeShellEncode(BigStr* s);
236BigStr* ShellEncode(BigStr* s);
237BigStr* YshEncode(BigStr* s, bool unquoted_ok = false);
238
239} // declare namespace j8_lite
240
241namespace error { // declare
242
243using syntax_asdl::loc;
244BigStr* _ValType(value_asdl::value_t* val);
245class _ErrorWithLocation {
246 public:
247 _ErrorWithLocation(BigStr* msg, syntax_asdl::loc_t* location);
248 bool HasLocation();
249 BigStr* UserErrorString();
250 syntax_asdl::loc_t* location{};
251 BigStr* msg{};
252
253 static constexpr uint32_t field_mask() {
254 return maskbit(offsetof(_ErrorWithLocation, location))
255 | maskbit(offsetof(_ErrorWithLocation, msg));
256 }
257
258 static constexpr ObjHeader obj_header() {
259 return ObjHeader::ClassFixed(field_mask(), sizeof(_ErrorWithLocation));
260 }
261
262 DISALLOW_COPY_AND_ASSIGN(_ErrorWithLocation)
263};
264
265class Usage : public ::error::_ErrorWithLocation {
266 public:
267 Usage(BigStr* msg, syntax_asdl::loc_t* location);
268
269 static constexpr uint32_t field_mask() {
270 return ::error::_ErrorWithLocation::field_mask();
271 }
272
273 static constexpr ObjHeader obj_header() {
274 return ObjHeader::ClassFixed(field_mask(), sizeof(Usage));
275 }
276
277 DISALLOW_COPY_AND_ASSIGN(Usage)
278};
279
280class Parse : public ::error::_ErrorWithLocation {
281 public:
282 Parse(BigStr* msg, syntax_asdl::loc_t* location);
283
284 static constexpr uint32_t field_mask() {
285 return ::error::_ErrorWithLocation::field_mask();
286 }
287
288 static constexpr ObjHeader obj_header() {
289 return ObjHeader::ClassFixed(field_mask(), sizeof(Parse));
290 }
291
292 DISALLOW_COPY_AND_ASSIGN(Parse)
293};
294
295class FailGlob : public ::error::_ErrorWithLocation {
296 public:
297 FailGlob(BigStr* msg, syntax_asdl::loc_t* location);
298
299 static constexpr uint32_t field_mask() {
300 return ::error::_ErrorWithLocation::field_mask();
301 }
302
303 static constexpr ObjHeader obj_header() {
304 return ObjHeader::ClassFixed(field_mask(), sizeof(FailGlob));
305 }
306
307 DISALLOW_COPY_AND_ASSIGN(FailGlob)
308};
309
310class RedirectEval : public ::error::_ErrorWithLocation {
311 public:
312 RedirectEval(BigStr* msg, syntax_asdl::loc_t* location);
313
314 static constexpr uint32_t field_mask() {
315 return ::error::_ErrorWithLocation::field_mask();
316 }
317
318 static constexpr ObjHeader obj_header() {
319 return ObjHeader::ClassFixed(field_mask(), sizeof(RedirectEval));
320 }
321
322 DISALLOW_COPY_AND_ASSIGN(RedirectEval)
323};
324
325class FatalRuntime : public ::error::_ErrorWithLocation {
326 public:
327 FatalRuntime(int exit_status, BigStr* msg, syntax_asdl::loc_t* location);
328 int ExitStatus();
329
330 int exit_status{};
331
332 static constexpr uint32_t field_mask() {
333 return ::error::_ErrorWithLocation::field_mask();
334 }
335
336 static constexpr ObjHeader obj_header() {
337 return ObjHeader::ClassFixed(field_mask(), sizeof(FatalRuntime));
338 }
339
340 DISALLOW_COPY_AND_ASSIGN(FatalRuntime)
341};
342
343class Strict : public ::error::FatalRuntime {
344 public:
345 Strict(BigStr* msg, syntax_asdl::loc_t* location);
346
347 static constexpr uint32_t field_mask() {
348 return ::error::FatalRuntime::field_mask();
349 }
350
351 static constexpr ObjHeader obj_header() {
352 return ObjHeader::ClassFixed(field_mask(), sizeof(Strict));
353 }
354
355 DISALLOW_COPY_AND_ASSIGN(Strict)
356};
357
358class ErrExit : public ::error::FatalRuntime {
359 public:
360 ErrExit(int exit_status, BigStr* msg, syntax_asdl::loc_t* location, bool show_code = false);
361
362 bool show_code{};
363
364 static constexpr uint32_t field_mask() {
365 return ::error::FatalRuntime::field_mask();
366 }
367
368 static constexpr ObjHeader obj_header() {
369 return ObjHeader::ClassFixed(field_mask(), sizeof(ErrExit));
370 }
371
372 DISALLOW_COPY_AND_ASSIGN(ErrExit)
373};
374
375class Expr : public ::error::FatalRuntime {
376 public:
377 Expr(BigStr* msg, syntax_asdl::loc_t* location);
378
379 static constexpr uint32_t field_mask() {
380 return ::error::FatalRuntime::field_mask();
381 }
382
383 static constexpr ObjHeader obj_header() {
384 return ObjHeader::ClassFixed(field_mask(), sizeof(Expr));
385 }
386
387 DISALLOW_COPY_AND_ASSIGN(Expr)
388};
389
390class Structured : public ::error::FatalRuntime {
391 public:
392 Structured(int status, BigStr* msg, syntax_asdl::loc_t* location, Dict<BigStr*, value_asdl::value_t*>* properties = nullptr);
393 value::Dict* ToDict();
394
395 Dict<BigStr*, value_asdl::value_t*>* properties{};
396
397 static constexpr uint32_t field_mask() {
398 return ::error::FatalRuntime::field_mask()
399 | maskbit(offsetof(Structured, properties));
400 }
401
402 static constexpr ObjHeader obj_header() {
403 return ObjHeader::ClassFixed(field_mask(), sizeof(Structured));
404 }
405
406 DISALLOW_COPY_AND_ASSIGN(Structured)
407};
408
409class AssertionErr : public ::error::Expr {
410 public:
411 AssertionErr(BigStr* msg, syntax_asdl::loc_t* location);
412
413 static constexpr uint32_t field_mask() {
414 return ::error::Expr::field_mask();
415 }
416
417 static constexpr ObjHeader obj_header() {
418 return ObjHeader::ClassFixed(field_mask(), sizeof(AssertionErr));
419 }
420
421 DISALLOW_COPY_AND_ASSIGN(AssertionErr)
422};
423
424class TypeErrVerbose : public ::error::Expr {
425 public:
426 TypeErrVerbose(BigStr* msg, syntax_asdl::loc_t* location);
427
428 static constexpr uint32_t field_mask() {
429 return ::error::Expr::field_mask();
430 }
431
432 static constexpr ObjHeader obj_header() {
433 return ObjHeader::ClassFixed(field_mask(), sizeof(TypeErrVerbose));
434 }
435
436 DISALLOW_COPY_AND_ASSIGN(TypeErrVerbose)
437};
438
439class TypeErr : public ::error::TypeErrVerbose {
440 public:
441 TypeErr(value_asdl::value_t* actual_val, BigStr* msg, syntax_asdl::loc_t* location);
442
443 static constexpr uint32_t field_mask() {
444 return ::error::TypeErrVerbose::field_mask();
445 }
446
447 static constexpr ObjHeader obj_header() {
448 return ObjHeader::ClassFixed(field_mask(), sizeof(TypeErr));
449 }
450
451 DISALLOW_COPY_AND_ASSIGN(TypeErr)
452};
453
454class Runtime {
455 public:
456 Runtime(BigStr* msg);
457 BigStr* UserErrorString();
458 BigStr* msg{};
459
460 static constexpr ObjHeader obj_header() {
461 return ObjHeader::ClassScanned(1, sizeof(Runtime));
462 }
463
464 DISALLOW_COPY_AND_ASSIGN(Runtime)
465};
466
467class Decode {
468 public:
469 Decode(BigStr* msg, BigStr* s, int start_pos, int end_pos, int line_num);
470 BigStr* Message();
471 BigStr* __str__();
472 BigStr* msg{};
473 BigStr* s{};
474 int start_pos{};
475 int end_pos{};
476 int line_num{};
477
478 static constexpr ObjHeader obj_header() {
479 return ObjHeader::ClassScanned(2, sizeof(Decode));
480 }
481
482 DISALLOW_COPY_AND_ASSIGN(Decode)
483};
484
485class Encode {
486 public:
487 Encode(BigStr* msg);
488 BigStr* Message();
489 BigStr* msg{};
490
491 static constexpr ObjHeader obj_header() {
492 return ObjHeader::ClassScanned(1, sizeof(Encode));
493 }
494
495 DISALLOW_COPY_AND_ASSIGN(Encode)
496};
497
498[[noreturn]] void e_usage(BigStr* msg, syntax_asdl::loc_t* location);
499[[noreturn]] void e_strict(BigStr* msg, syntax_asdl::loc_t* location);
500[[noreturn]] void p_die(BigStr* msg, syntax_asdl::loc_t* location);
501[[noreturn]] void e_die(BigStr* msg, syntax_asdl::loc_t* location = nullptr);
502[[noreturn]] void e_die_status(int status, BigStr* msg, syntax_asdl::loc_t* location = nullptr);
503
504} // declare namespace error
505
506namespace num { // declare
507
508value::Int* ToBig(int i);
509mops::BigInt Exponent(mops::BigInt x, mops::BigInt y);
510
511} // declare namespace num
512
513namespace runtime { // define
514
515using hnode_asdl::hnode;
516using hnode_asdl::color_t;
517using hnode_asdl::color_e;
518int NO_SPID = -1;
519
520hnode::Record* NewRecord(BigStr* node_type) {
521 StackRoot _root0(&node_type);
522
523 return Alloc<hnode::Record>(node_type, Alloc<List<hnode_asdl::Field*>>(), false, str0, str1, Alloc<List<hnode_asdl::hnode_t*>>());
524}
525
526hnode::Leaf* NewLeaf(BigStr* s, hnode_asdl::color_t e_color) {
527 StackRoot _root0(&s);
528
529 if (s == nullptr) {
530 return Alloc<hnode::Leaf>(str2, color_e::OtherConst);
531 }
532 else {
533 return Alloc<hnode::Leaf>(s, e_color);
534 }
535}
536
537TraversalState::TraversalState() {
538 this->seen = Alloc<Dict<int, bool>>();
539 this->ref_count = Alloc<Dict<int, int>>();
540}
541BigStr* TRUE_STR = str3;
542BigStr* FALSE_STR = str4;
543
544} // define namespace runtime
545
546namespace format { // define
547
548using hnode_asdl::hnode;
549using hnode_asdl::hnode_e;
550using hnode_asdl::hnode_t;
551using hnode_asdl::color_e;
552using hnode_asdl::color_t;
553
554format::ColorOutput* DetectConsoleOutput(mylib::Writer* f) {
555 StackRoot _root0(&f);
556
557 if (f->isatty()) {
558 return Alloc<AnsiOutput>(f);
559 }
560 else {
561 return Alloc<TextOutput>(f);
562 }
563}
564
565ColorOutput::ColorOutput(mylib::Writer* f) {
566 this->f = f;
567 this->num_chars = 0;
568}
569
570format::ColorOutput* ColorOutput::NewTempBuffer() {
571 FAIL(kNotImplemented); // Python NotImplementedError
572}
573
574void ColorOutput::FileHeader() {
575 ; // pass
576}
577
578void ColorOutput::FileFooter() {
579 ; // pass
580}
581
582void ColorOutput::PushColor(hnode_asdl::color_t e_color) {
583 FAIL(kNotImplemented); // Python NotImplementedError
584}
585
586void ColorOutput::PopColor() {
587 FAIL(kNotImplemented); // Python NotImplementedError
588}
589
590void ColorOutput::write(BigStr* s) {
591 StackRoot _root0(&s);
592
593 this->f->write(s);
594 this->num_chars += len(s);
595}
596
597void ColorOutput::WriteRaw(Tuple2<BigStr*, int>* raw) {
598 BigStr* s = nullptr;
599 int num_chars;
600 StackRoot _root0(&raw);
601 StackRoot _root1(&s);
602
603 Tuple2<BigStr*, int>* tup0 = raw;
604 s = tup0->at0();
605 num_chars = tup0->at1();
606 this->f->write(s);
607 this->num_chars += num_chars;
608}
609
610int ColorOutput::NumChars() {
611 return this->num_chars;
612}
613
614Tuple2<BigStr*, int> ColorOutput::GetRaw() {
615 mylib::BufWriter* f = nullptr;
616 StackRoot _root0(&f);
617
618 f = static_cast<mylib::BufWriter*>(this->f);
619 return Tuple2<BigStr*, int>(f->getvalue(), this->num_chars);
620}
621
622TextOutput::TextOutput(mylib::Writer* f) : ::format::ColorOutput(f) {
623}
624
625format::TextOutput* TextOutput::NewTempBuffer() {
626 return Alloc<TextOutput>(Alloc<mylib::BufWriter>());
627}
628
629void TextOutput::PushColor(hnode_asdl::color_t e_color) {
630 ; // pass
631}
632
633void TextOutput::PopColor() {
634 ; // pass
635}
636
637HtmlOutput::HtmlOutput(mylib::Writer* f) : ::format::ColorOutput(f) {
638}
639
640format::HtmlOutput* HtmlOutput::NewTempBuffer() {
641 return Alloc<HtmlOutput>(Alloc<mylib::BufWriter>());
642}
643
644void HtmlOutput::FileHeader() {
645 this->f->write(str5);
646}
647
648void HtmlOutput::FileFooter() {
649 this->f->write(str6);
650}
651
652void HtmlOutput::PushColor(hnode_asdl::color_t e_color) {
653 BigStr* css_class = nullptr;
654 StackRoot _root0(&css_class);
655
656 if (e_color == color_e::TypeName) {
657 css_class = str7;
658 }
659 else {
660 if (e_color == color_e::StringConst) {
661 css_class = str8;
662 }
663 else {
664 if (e_color == color_e::OtherConst) {
665 css_class = str9;
666 }
667 else {
668 if (e_color == color_e::External) {
669 css_class = str10;
670 }
671 else {
672 if (e_color == color_e::UserType) {
673 css_class = str11;
674 }
675 else {
676 assert(0); // AssertionError
677 }
678 }
679 }
680 }
681 }
682 this->f->write(StrFormat("<span class=\"%s\">", css_class));
683}
684
685void HtmlOutput::PopColor() {
686 this->f->write(str13);
687}
688
689void HtmlOutput::write(BigStr* s) {
690 StackRoot _root0(&s);
691
692 this->f->write(cgi::escape(s));
693 this->num_chars += len(s);
694}
695
696AnsiOutput::AnsiOutput(mylib::Writer* f) : ::format::ColorOutput(f) {
697}
698
699format::AnsiOutput* AnsiOutput::NewTempBuffer() {
700 return Alloc<AnsiOutput>(Alloc<mylib::BufWriter>());
701}
702
703void AnsiOutput::PushColor(hnode_asdl::color_t e_color) {
704 if (e_color == color_e::TypeName) {
705 this->f->write(ansi::YELLOW);
706 }
707 else {
708 if (e_color == color_e::StringConst) {
709 this->f->write(ansi::BOLD);
710 }
711 else {
712 if (e_color == color_e::OtherConst) {
713 this->f->write(ansi::GREEN);
714 }
715 else {
716 if (e_color == color_e::External) {
717 this->f->write(str_concat(ansi::BOLD, ansi::BLUE));
718 }
719 else {
720 if (e_color == color_e::UserType) {
721 this->f->write(ansi::GREEN);
722 }
723 else {
724 assert(0); // AssertionError
725 }
726 }
727 }
728 }
729 }
730}
731
732void AnsiOutput::PopColor() {
733 this->f->write(ansi::RESET);
734}
735int INDENT = 2;
736
737_PrettyPrinter::_PrettyPrinter(int max_col) {
738 this->max_col = max_col;
739}
740
741bool _PrettyPrinter::_PrintWrappedArray(List<hnode_asdl::hnode_t*>* array, int prefix_len, format::ColorOutput* f, int indent) {
742 bool all_fit;
743 int chars_so_far;
744 int i;
745 format::ColorOutput* single_f = nullptr;
746 BigStr* s = nullptr;
747 int num_chars;
748 StackRoot _root0(&array);
749 StackRoot _root1(&f);
750 StackRoot _root2(&single_f);
751 StackRoot _root3(&s);
752
753 all_fit = true;
754 chars_so_far = prefix_len;
755 i = 0;
756 for (ListIter<hnode_asdl::hnode_t*> it(array); !it.Done(); it.Next(), ++i) {
757 hnode_asdl::hnode_t* val = it.Value();
758 StackRoot _for(&val );
759 if (i != 0) {
760 f->write(str14);
761 }
762 single_f = f->NewTempBuffer();
763 if (_TrySingleLine(val, single_f, (this->max_col - chars_so_far))) {
764 Tuple2<BigStr*, int> tup1 = single_f->GetRaw();
765 s = tup1.at0();
766 num_chars = tup1.at1();
767 f->WriteRaw((Alloc<Tuple2<BigStr*, int>>(s, num_chars)));
768 chars_so_far += single_f->NumChars();
769 }
770 else {
771 f->write(str15);
772 this->PrintNode(val, f, (indent + INDENT));
773 chars_so_far = 0;
774 all_fit = false;
775 }
776 }
777 return all_fit;
778}
779
780bool _PrettyPrinter::_PrintWholeArray(List<hnode_asdl::hnode_t*>* array, int prefix_len, format::ColorOutput* f, int indent) {
781 bool all_fit;
782 List<Tuple2<BigStr*, int>*>* pieces = nullptr;
783 int chars_so_far;
784 format::ColorOutput* single_f = nullptr;
785 BigStr* s = nullptr;
786 int num_chars;
787 int i;
788 StackRoot _root0(&array);
789 StackRoot _root1(&f);
790 StackRoot _root2(&pieces);
791 StackRoot _root3(&single_f);
792 StackRoot _root4(&s);
793
794 all_fit = true;
795 pieces = Alloc<List<Tuple2<BigStr*, int>*>>();
796 chars_so_far = prefix_len;
797 for (ListIter<hnode_asdl::hnode_t*> it(array); !it.Done(); it.Next()) {
798 hnode_asdl::hnode_t* item = it.Value();
799 StackRoot _for(&item );
800 single_f = f->NewTempBuffer();
801 if (_TrySingleLine(item, single_f, (this->max_col - chars_so_far))) {
802 Tuple2<BigStr*, int> tup2 = single_f->GetRaw();
803 s = tup2.at0();
804 num_chars = tup2.at1();
805 pieces->append((Alloc<Tuple2<BigStr*, int>>(s, num_chars)));
806 chars_so_far += single_f->NumChars();
807 }
808 else {
809 all_fit = false;
810 break;
811 }
812 }
813 if (all_fit) {
814 i = 0;
815 for (ListIter<Tuple2<BigStr*, int>*> it(pieces); !it.Done(); it.Next(), ++i) {
816 Tuple2<BigStr*, int>* p = it.Value();
817 StackRoot _for(&p );
818 if (i != 0) {
819 f->write(str16);
820 }
821 f->WriteRaw(p);
822 }
823 f->write(str17);
824 }
825 return all_fit;
826}
827
828void _PrettyPrinter::_PrintRecord(hnode::Record* node, format::ColorOutput* f, int indent) {
829 BigStr* ind = nullptr;
830 BigStr* prefix = nullptr;
831 int prefix_len;
832 bool all_fit;
833 BigStr* name = nullptr;
834 hnode_asdl::hnode_t* val = nullptr;
835 BigStr* ind1 = nullptr;
836 hnode_asdl::hnode_t* UP_val = nullptr;
837 int tag;
838 BigStr* name_str = nullptr;
839 format::ColorOutput* single_f = nullptr;
840 BigStr* s = nullptr;
841 int num_chars;
842 StackRoot _root0(&node);
843 StackRoot _root1(&f);
844 StackRoot _root2(&ind);
845 StackRoot _root3(&prefix);
846 StackRoot _root4(&name);
847 StackRoot _root5(&val);
848 StackRoot _root6(&ind1);
849 StackRoot _root7(&UP_val);
850 StackRoot _root8(&name_str);
851 StackRoot _root9(&single_f);
852 StackRoot _root10(&s);
853
854 ind = str_repeat(str18, indent);
855 if (node->abbrev) {
856 prefix = str_concat(ind, node->left);
857 f->write(prefix);
858 if (len(node->node_type)) {
859 f->PushColor(color_e::TypeName);
860 f->write(node->node_type);
861 f->PopColor();
862 f->write(str19);
863 }
864 prefix_len = ((len(prefix) + len(node->node_type)) + 1);
865 all_fit = this->_PrintWrappedArray(node->unnamed_fields, prefix_len, f, indent);
866 if (!all_fit) {
867 f->write(str20);
868 f->write(ind);
869 }
870 f->write(node->right);
871 }
872 else {
873 f->write(str_concat(ind, node->left));
874 f->PushColor(color_e::TypeName);
875 f->write(node->node_type);
876 f->PopColor();
877 f->write(str21);
878 for (ListIter<hnode_asdl::Field*> it(node->fields); !it.Done(); it.Next()) {
879 hnode_asdl::Field* field = it.Value();
880 StackRoot _for(&field );
881 name = field->name;
882 val = field->val;
883 ind1 = str_repeat(str22, (indent + INDENT));
884 UP_val = val;
885 tag = val->tag();
886 if (tag == hnode_e::Array) {
887 hnode::Array* val = static_cast<hnode::Array*>(UP_val);
888 name_str = StrFormat("%s%s: [", ind1, name);
889 f->write(name_str);
890 prefix_len = len(name_str);
891 if (!this->_PrintWholeArray(val->children, prefix_len, f, indent)) {
892 f->write(str24);
893 for (ListIter<hnode_asdl::hnode_t*> it(val->children); !it.Done(); it.Next()) {
894 hnode_asdl::hnode_t* child = it.Value();
895 StackRoot _for(&child );
896 this->PrintNode(child, f, ((indent + INDENT) + INDENT));
897 f->write(str25);
898 }
899 f->write(StrFormat("%s]", ind1));
900 }
901 }
902 else {
903 name_str = StrFormat("%s%s: ", ind1, name);
904 f->write(name_str);
905 prefix_len = len(name_str);
906 single_f = f->NewTempBuffer();
907 if (_TrySingleLine(val, single_f, (this->max_col - prefix_len))) {
908 Tuple2<BigStr*, int> tup3 = single_f->GetRaw();
909 s = tup3.at0();
910 num_chars = tup3.at1();
911 f->WriteRaw((Alloc<Tuple2<BigStr*, int>>(s, num_chars)));
912 }
913 else {
914 f->write(str28);
915 this->PrintNode(val, f, ((indent + INDENT) + INDENT));
916 }
917 }
918 f->write(str29);
919 }
920 f->write(str_concat(ind, node->right));
921 }
922}
923
924void _PrettyPrinter::PrintNode(hnode_asdl::hnode_t* node, format::ColorOutput* f, int indent) {
925 BigStr* ind = nullptr;
926 format::ColorOutput* single_f = nullptr;
927 BigStr* s = nullptr;
928 int num_chars;
929 hnode_asdl::hnode_t* UP_node = nullptr;
930 int tag;
931 StackRoot _root0(&node);
932 StackRoot _root1(&f);
933 StackRoot _root2(&ind);
934 StackRoot _root3(&single_f);
935 StackRoot _root4(&s);
936 StackRoot _root5(&UP_node);
937
938 ind = str_repeat(str30, indent);
939 single_f = f->NewTempBuffer();
940 single_f->write(ind);
941 if (_TrySingleLine(node, single_f, (this->max_col - indent))) {
942 Tuple2<BigStr*, int> tup4 = single_f->GetRaw();
943 s = tup4.at0();
944 num_chars = tup4.at1();
945 f->WriteRaw((Alloc<Tuple2<BigStr*, int>>(s, num_chars)));
946 return ;
947 }
948 UP_node = node;
949 tag = node->tag();
950 if (tag == hnode_e::Leaf) {
951 hnode::Leaf* node = static_cast<hnode::Leaf*>(UP_node);
952 f->PushColor(node->color);
953 f->write(j8_lite::EncodeString(node->s, true));
954 f->PopColor();
955 }
956 else {
957 if (tag == hnode_e::External) {
958 hnode::External* node = static_cast<hnode::External*>(UP_node);
959 f->PushColor(color_e::External);
960 // if not PYTHON
961 {
962 f->write(str31);
963 }
964 // endif MYCPP
965 f->PopColor();
966 }
967 else {
968 if (tag == hnode_e::Record) {
969 hnode::Record* node = static_cast<hnode::Record*>(UP_node);
970 this->_PrintRecord(node, f, indent);
971 }
972 else {
973 if (tag == hnode_e::AlreadySeen) {
974 hnode::AlreadySeen* node = static_cast<hnode::AlreadySeen*>(UP_node);
975 f->write(StrFormat("...0x%s", mylib::hex_lower(node->heap_id)));
976 }
977 else {
978 assert(0); // AssertionError
979 }
980 }
981 }
982 }
983}
984
985bool _TrySingleLineObj(hnode::Record* node, format::ColorOutput* f, int max_chars) {
986 int i;
987 StackRoot _root0(&node);
988 StackRoot _root1(&f);
989
990 f->write(node->left);
991 if (node->abbrev) {
992 if (len(node->node_type)) {
993 f->PushColor(color_e::TypeName);
994 f->write(node->node_type);
995 f->PopColor();
996 f->write(str33);
997 }
998 i = 0;
999 for (ListIter<hnode_asdl::hnode_t*> it(node->unnamed_fields); !it.Done(); it.Next(), ++i) {
1000 hnode_asdl::hnode_t* val = it.Value();
1001 StackRoot _for(&val );
1002 if (i != 0) {
1003 f->write(str34);
1004 }
1005 if (!_TrySingleLine(val, f, max_chars)) {
1006 return false;
1007 }
1008 }
1009 }
1010 else {
1011 f->PushColor(color_e::TypeName);
1012 f->write(node->node_type);
1013 f->PopColor();
1014 for (ListIter<hnode_asdl::Field*> it(node->fields); !it.Done(); it.Next()) {
1015 hnode_asdl::Field* field = it.Value();
1016 StackRoot _for(&field );
1017 f->write(StrFormat(" %s:", field->name));
1018 if (!_TrySingleLine(field->val, f, max_chars)) {
1019 return false;
1020 }
1021 }
1022 }
1023 f->write(node->right);
1024 return true;
1025}
1026
1027bool _TrySingleLine(hnode_asdl::hnode_t* node, format::ColorOutput* f, int max_chars) {
1028 hnode_asdl::hnode_t* UP_node = nullptr;
1029 int tag;
1030 int i;
1031 int num_chars_so_far;
1032 StackRoot _root0(&node);
1033 StackRoot _root1(&f);
1034 StackRoot _root2(&UP_node);
1035
1036 UP_node = node;
1037 tag = node->tag();
1038 if (tag == hnode_e::Leaf) {
1039 hnode::Leaf* node = static_cast<hnode::Leaf*>(UP_node);
1040 f->PushColor(node->color);
1041 f->write(j8_lite::EncodeString(node->s, true));
1042 f->PopColor();
1043 }
1044 else {
1045 if (tag == hnode_e::External) {
1046 hnode::External* node = static_cast<hnode::External*>(UP_node);
1047 f->PushColor(color_e::External);
1048 // if not PYTHON
1049 {
1050 f->write(str36);
1051 }
1052 // endif MYCPP
1053 f->PopColor();
1054 }
1055 else {
1056 if (tag == hnode_e::Array) {
1057 hnode::Array* node = static_cast<hnode::Array*>(UP_node);
1058 f->write(str37);
1059 i = 0;
1060 for (ListIter<hnode_asdl::hnode_t*> it(node->children); !it.Done(); it.Next(), ++i) {
1061 hnode_asdl::hnode_t* item = it.Value();
1062 StackRoot _for(&item );
1063 if (i != 0) {
1064 f->write(str38);
1065 }
1066 if (!_TrySingleLine(item, f, max_chars)) {
1067 return false;
1068 }
1069 }
1070 f->write(str39);
1071 }
1072 else {
1073 if (tag == hnode_e::Record) {
1074 hnode::Record* node = static_cast<hnode::Record*>(UP_node);
1075 return _TrySingleLineObj(node, f, max_chars);
1076 }
1077 else {
1078 if (tag == hnode_e::AlreadySeen) {
1079 hnode::AlreadySeen* node = static_cast<hnode::AlreadySeen*>(UP_node);
1080 f->write(StrFormat("...0x%s", mylib::hex_lower(node->heap_id)));
1081 }
1082 else {
1083 assert(0); // AssertionError
1084 }
1085 }
1086 }
1087 }
1088 }
1089 num_chars_so_far = f->NumChars();
1090 if (num_chars_so_far > max_chars) {
1091 return false;
1092 }
1093 return true;
1094}
1095
1096void PrintTree(hnode_asdl::hnode_t* node, format::ColorOutput* f) {
1097 format::_PrettyPrinter* pp = nullptr;
1098 StackRoot _root0(&node);
1099 StackRoot _root1(&f);
1100 StackRoot _root2(&pp);
1101
1102 pp = Alloc<_PrettyPrinter>(100);
1103 pp->PrintNode(node, f, 0);
1104}
1105
1106void PrintTree2(hnode_asdl::hnode_t* node, format::ColorOutput* f) {
1107 pretty_asdl::MeasuredDoc* doc = nullptr;
1108 pretty::PrettyPrinter* printer = nullptr;
1109 mylib::BufWriter* buf = nullptr;
1110 StackRoot _root0(&node);
1111 StackRoot _root1(&f);
1112 StackRoot _root2(&doc);
1113 StackRoot _root3(&printer);
1114 StackRoot _root4(&buf);
1115
1116 doc = pretty::AsciiText(str41);
1117 printer = Alloc<pretty::PrettyPrinter>(20);
1118 buf = Alloc<mylib::BufWriter>();
1119 printer->PrintDoc(doc, buf);
1120 f->write(buf->getvalue());
1121 f->write(str42);
1122}
1123
1124} // define namespace format
1125
1126namespace ansi { // define
1127
1128BigStr* RESET = str43;
1129BigStr* BOLD = str44;
1130BigStr* UNDERLINE = str45;
1131BigStr* REVERSE = str46;
1132BigStr* RED = str47;
1133BigStr* GREEN = str48;
1134BigStr* YELLOW = str49;
1135BigStr* BLUE = str50;
1136BigStr* MAGENTA = str51;
1137BigStr* CYAN = str52;
1138BigStr* WHITE = str53;
1139
1140} // define namespace ansi
1141
1142namespace pretty { // define
1143
1144using pretty_asdl::doc;
1145using pretty_asdl::doc_e;
1146using pretty_asdl::DocFragment;
1147using pretty_asdl::Measure;
1148using pretty_asdl::MeasuredDoc;
1149using mylib::BufWriter;
1150
1151pretty_asdl::Measure* _EmptyMeasure() {
1152 return Alloc<Measure>(0, -1);
1153}
1154
1155pretty_asdl::Measure* _FlattenMeasure(pretty_asdl::Measure* measure) {
1156 StackRoot _root0(&measure);
1157
1158 return Alloc<Measure>(measure->flat, -1);
1159}
1160
1161pretty_asdl::Measure* _ConcatMeasure(pretty_asdl::Measure* m1, pretty_asdl::Measure* m2) {
1162 StackRoot _root0(&m1);
1163 StackRoot _root1(&m2);
1164
1165 if (m1->nonflat != -1) {
1166 return Alloc<Measure>((m1->flat + m2->flat), m1->nonflat);
1167 }
1168 else {
1169 if (m2->nonflat != -1) {
1170 return Alloc<Measure>((m1->flat + m2->flat), (m1->flat + m2->nonflat));
1171 }
1172 else {
1173 return Alloc<Measure>((m1->flat + m2->flat), -1);
1174 }
1175 }
1176}
1177
1178int _SuffixLen(pretty_asdl::Measure* measure) {
1179 StackRoot _root0(&measure);
1180
1181 if (measure->nonflat != -1) {
1182 return measure->nonflat;
1183 }
1184 else {
1185 return measure->flat;
1186 }
1187}
1188
1189pretty_asdl::MeasuredDoc* AsciiText(BigStr* string) {
1190 StackRoot _root0(&string);
1191
1192 return Alloc<MeasuredDoc>(Alloc<doc::Text>(string), Alloc<Measure>(len(string), -1));
1193}
1194
1195pretty_asdl::MeasuredDoc* _Break(BigStr* string) {
1196 StackRoot _root0(&string);
1197
1198 return Alloc<MeasuredDoc>(Alloc<doc::Break>(string), Alloc<Measure>(len(string), 0));
1199}
1200
1201pretty_asdl::MeasuredDoc* _Indent(int indent, pretty_asdl::MeasuredDoc* mdoc) {
1202 StackRoot _root0(&mdoc);
1203
1204 return Alloc<MeasuredDoc>(Alloc<doc::Indent>(indent, mdoc), mdoc->measure);
1205}
1206
1207pretty_asdl::MeasuredDoc* _Concat(List<pretty_asdl::MeasuredDoc*>* mdocs) {
1208 pretty_asdl::Measure* measure = nullptr;
1209 StackRoot _root0(&mdocs);
1210 StackRoot _root1(&measure);
1211
1212 measure = _EmptyMeasure();
1213 for (ListIter<pretty_asdl::MeasuredDoc*> it(mdocs); !it.Done(); it.Next()) {
1214 pretty_asdl::MeasuredDoc* mdoc = it.Value();
1215 StackRoot _for(&mdoc );
1216 measure = _ConcatMeasure(measure, mdoc->measure);
1217 }
1218 return Alloc<MeasuredDoc>(Alloc<doc::Concat>(mdocs), measure);
1219}
1220
1221pretty_asdl::MeasuredDoc* _Group(pretty_asdl::MeasuredDoc* mdoc) {
1222 StackRoot _root0(&mdoc);
1223
1224 return Alloc<MeasuredDoc>(Alloc<doc::Group>(mdoc), mdoc->measure);
1225}
1226
1227pretty_asdl::MeasuredDoc* _IfFlat(pretty_asdl::MeasuredDoc* flat_mdoc, pretty_asdl::MeasuredDoc* nonflat_mdoc) {
1228 StackRoot _root0(&flat_mdoc);
1229 StackRoot _root1(&nonflat_mdoc);
1230
1231 return Alloc<MeasuredDoc>(Alloc<doc::IfFlat>(flat_mdoc, nonflat_mdoc), Alloc<Measure>(flat_mdoc->measure->flat, nonflat_mdoc->measure->nonflat));
1232}
1233
1234pretty_asdl::MeasuredDoc* _Flat(pretty_asdl::MeasuredDoc* mdoc) {
1235 StackRoot _root0(&mdoc);
1236
1237 return Alloc<MeasuredDoc>(Alloc<doc::Flat>(mdoc), _FlattenMeasure(mdoc->measure));
1238}
1239
1240PrettyPrinter::PrettyPrinter(int max_width) {
1241 this->max_width = max_width;
1242}
1243
1244bool PrettyPrinter::_Fits(int prefix_len, doc::Group* group, pretty_asdl::Measure* suffix_measure) {
1245 pretty_asdl::Measure* measure = nullptr;
1246 StackRoot _root0(&group);
1247 StackRoot _root1(&suffix_measure);
1248 StackRoot _root2(&measure);
1249
1250 measure = _ConcatMeasure(_FlattenMeasure(group->mdoc->measure), suffix_measure);
1251 return (prefix_len + _SuffixLen(measure)) <= this->max_width;
1252}
1253
1254void PrettyPrinter::PrintDoc(pretty_asdl::MeasuredDoc* document, mylib::BufWriter* buf) {
1255 int prefix_len;
1256 List<pretty_asdl::DocFragment*>* fragments = nullptr;
1257 pretty_asdl::DocFragment* frag = nullptr;
1258 doc::Text* text = nullptr;
1259 BigStr* break_str = nullptr;
1260 doc::Indent* indented = nullptr;
1261 doc::Concat* concat = nullptr;
1262 pretty_asdl::Measure* measure = nullptr;
1263 doc::Group* group = nullptr;
1264 bool flat;
1265 doc::IfFlat* if_flat = nullptr;
1266 pretty_asdl::MeasuredDoc* subdoc = nullptr;
1267 doc::Flat* flat_doc = nullptr;
1268 StackRoot _root0(&document);
1269 StackRoot _root1(&buf);
1270 StackRoot _root2(&fragments);
1271 StackRoot _root3(&frag);
1272 StackRoot _root4(&text);
1273 StackRoot _root5(&break_str);
1274 StackRoot _root6(&indented);
1275 StackRoot _root7(&concat);
1276 StackRoot _root8(&measure);
1277 StackRoot _root9(&group);
1278 StackRoot _root10(&if_flat);
1279 StackRoot _root11(&subdoc);
1280 StackRoot _root12(&flat_doc);
1281
1282 prefix_len = 0;
1283 fragments = NewList<pretty_asdl::DocFragment*>(std::initializer_list<pretty_asdl::DocFragment*>{Alloc<DocFragment>(_Group(document), 0, false, _EmptyMeasure())});
1284 while (len(fragments) > 0) {
1285 frag = fragments->pop();
1286 switch (frag->mdoc->doc->tag()) {
1287 case doc_e::Text: {
1288 text = static_cast<doc::Text*>(frag->mdoc->doc);
1289 buf->write(text->string);
1290 prefix_len += frag->mdoc->measure->flat;
1291 }
1292 break;
1293 case doc_e::Break: {
1294 if (frag->is_flat) {
1295 break_str = static_cast<doc::Break*>(frag->mdoc->doc)->string;
1296 buf->write(break_str);
1297 prefix_len += frag->mdoc->measure->flat;
1298 }
1299 else {
1300 buf->write(str54);
1301 buf->write_spaces(frag->indent);
1302 prefix_len = frag->indent;
1303 }
1304 }
1305 break;
1306 case doc_e::Indent: {
1307 indented = static_cast<doc::Indent*>(frag->mdoc->doc);
1308 fragments->append(Alloc<DocFragment>(indented->mdoc, (frag->indent + indented->indent), frag->is_flat, frag->measure));
1309 }
1310 break;
1311 case doc_e::Concat: {
1312 concat = static_cast<doc::Concat*>(frag->mdoc->doc);
1313 measure = frag->measure;
1314 for (ReverseListIter<pretty_asdl::MeasuredDoc*> it(concat->mdocs); !it.Done(); it.Next()) {
1315 pretty_asdl::MeasuredDoc* mdoc = it.Value();
1316 StackRoot _for(&mdoc );
1317 fragments->append(Alloc<DocFragment>(mdoc, frag->indent, frag->is_flat, measure));
1318 measure = _ConcatMeasure(mdoc->measure, measure);
1319 }
1320 }
1321 break;
1322 case doc_e::Group: {
1323 group = static_cast<doc::Group*>(frag->mdoc->doc);
1324 flat = this->_Fits(prefix_len, group, frag->measure);
1325 fragments->append(Alloc<DocFragment>(group->mdoc, frag->indent, flat, frag->measure));
1326 }
1327 break;
1328 case doc_e::IfFlat: {
1329 if_flat = static_cast<doc::IfFlat*>(frag->mdoc->doc);
1330 if (frag->is_flat) {
1331 subdoc = if_flat->flat_mdoc;
1332 }
1333 else {
1334 subdoc = if_flat->nonflat_mdoc;
1335 }
1336 fragments->append(Alloc<DocFragment>(subdoc, frag->indent, frag->is_flat, frag->measure));
1337 }
1338 break;
1339 case doc_e::Flat: {
1340 flat_doc = static_cast<doc::Flat*>(frag->mdoc->doc);
1341 fragments->append(Alloc<DocFragment>(flat_doc->mdoc, frag->indent, true, frag->measure));
1342 }
1343 break;
1344 }
1345 }
1346}
1347
1348} // define namespace pretty
1349
1350namespace cgi { // define
1351
1352
1353BigStr* escape(BigStr* s) {
1354 StackRoot _root0(&s);
1355
1356 s = s->replace(str55, str56);
1357 s = s->replace(str57, str58);
1358 s = s->replace(str59, str60);
1359 return s;
1360}
1361
1362} // define namespace cgi
1363
1364namespace j8_lite { // define
1365
1366
1367BigStr* EncodeString(BigStr* s, bool unquoted_ok) {
1368 StackRoot _root0(&s);
1369
1370 if ((unquoted_ok and fastfunc::CanOmitQuotes(s))) {
1371 return s;
1372 }
1373 return fastfunc::J8EncodeString(s, 1);
1374}
1375
1376BigStr* YshEncodeString(BigStr* s) {
1377 StackRoot _root0(&s);
1378
1379 return fastfunc::ShellEncodeString(s, 1);
1380}
1381
1382BigStr* MaybeShellEncode(BigStr* s) {
1383 StackRoot _root0(&s);
1384
1385 if (fastfunc::CanOmitQuotes(s)) {
1386 return s;
1387 }
1388 return fastfunc::ShellEncodeString(s, 0);
1389}
1390
1391BigStr* ShellEncode(BigStr* s) {
1392 StackRoot _root0(&s);
1393
1394 return fastfunc::ShellEncodeString(s, 0);
1395}
1396
1397BigStr* YshEncode(BigStr* s, bool unquoted_ok) {
1398 StackRoot _root0(&s);
1399
1400 if ((unquoted_ok and fastfunc::CanOmitQuotes(s))) {
1401 return s;
1402 }
1403 return fastfunc::ShellEncodeString(s, 1);
1404}
1405
1406} // define namespace j8_lite
1407
1408namespace error { // define
1409
1410using syntax_asdl::loc_e;
1411using syntax_asdl::loc_t;
1412using syntax_asdl::loc;
1413using value_asdl::value;
1414using value_asdl::value_t;
1415using value_asdl::value_str;
1416
1417BigStr* _ValType(value_asdl::value_t* val) {
1418 StackRoot _root0(&val);
1419
1420 return value_str(val->tag(), false);
1421}
1422
1423_ErrorWithLocation::_ErrorWithLocation(BigStr* msg, syntax_asdl::loc_t* location) {
1424 this->msg = msg;
1425 if (location == nullptr) {
1426 this->location = loc::Missing;
1427 }
1428 else {
1429 this->location = location;
1430 }
1431}
1432
1433bool _ErrorWithLocation::HasLocation() {
1434 return this->location->tag() != loc_e::Missing;
1435}
1436
1437BigStr* _ErrorWithLocation::UserErrorString() {
1438 return this->msg;
1439}
1440
1441Usage::Usage(BigStr* msg, syntax_asdl::loc_t* location) : ::error::_ErrorWithLocation(msg, location) {
1442}
1443
1444Parse::Parse(BigStr* msg, syntax_asdl::loc_t* location) : ::error::_ErrorWithLocation(msg, location) {
1445}
1446
1447FailGlob::FailGlob(BigStr* msg, syntax_asdl::loc_t* location) : ::error::_ErrorWithLocation(msg, location) {
1448}
1449
1450RedirectEval::RedirectEval(BigStr* msg, syntax_asdl::loc_t* location) : ::error::_ErrorWithLocation(msg, location) {
1451}
1452
1453FatalRuntime::FatalRuntime(int exit_status, BigStr* msg, syntax_asdl::loc_t* location) : ::error::_ErrorWithLocation(msg, location) {
1454 this->exit_status = exit_status;
1455}
1456
1457int FatalRuntime::ExitStatus() {
1458 return this->exit_status;
1459}
1460
1461Strict::Strict(BigStr* msg, syntax_asdl::loc_t* location) : ::error::FatalRuntime(1, msg, location) {
1462}
1463
1464ErrExit::ErrExit(int exit_status, BigStr* msg, syntax_asdl::loc_t* location, bool show_code) : ::error::FatalRuntime(exit_status, msg, location) {
1465 this->show_code = show_code;
1466}
1467
1468Expr::Expr(BigStr* msg, syntax_asdl::loc_t* location) : ::error::FatalRuntime(3, msg, location) {
1469}
1470
1471Structured::Structured(int status, BigStr* msg, syntax_asdl::loc_t* location, Dict<BigStr*, value_asdl::value_t*>* properties) : ::error::FatalRuntime(status, msg, location) {
1472 this->properties = properties;
1473}
1474
1475value::Dict* Structured::ToDict() {
1476 Dict<BigStr*, value_asdl::value_t*>* d = nullptr;
1477 StackRoot _root0(&d);
1478
1479 d = Alloc<Dict<BigStr*, value_asdl::value_t*>>();
1480 if (this->properties != nullptr) {
1481 d->update(this->properties);
1482 }
1483 d->set(str62, num::ToBig(this->ExitStatus()));
1484 d->set(str63, Alloc<value::Str>(this->msg));
1485 return Alloc<value::Dict>(d);
1486}
1487
1488AssertionErr::AssertionErr(BigStr* msg, syntax_asdl::loc_t* location) : ::error::Expr(msg, location) {
1489}
1490
1491TypeErrVerbose::TypeErrVerbose(BigStr* msg, syntax_asdl::loc_t* location) : ::error::Expr(msg, location) {
1492}
1493
1494TypeErr::TypeErr(value_asdl::value_t* actual_val, BigStr* msg, syntax_asdl::loc_t* location) : ::error::TypeErrVerbose(StrFormat("%s, got %s", msg, _ValType(actual_val)), location) {
1495}
1496
1497Runtime::Runtime(BigStr* msg) {
1498 this->msg = msg;
1499}
1500
1501BigStr* Runtime::UserErrorString() {
1502 return this->msg;
1503}
1504
1505Decode::Decode(BigStr* msg, BigStr* s, int start_pos, int end_pos, int line_num) {
1506 this->msg = msg;
1507 this->s = s;
1508 this->start_pos = start_pos;
1509 this->end_pos = end_pos;
1510 this->line_num = line_num;
1511}
1512
1513BigStr* Decode::Message() {
1514 int start;
1515 int end;
1516 BigStr* part = nullptr;
1517 StackRoot _root0(&part);
1518
1519 start = max(0, (this->start_pos - 4));
1520 end = min(len(this->s), (this->end_pos + 4));
1521 part = this->s->slice(start, end);
1522 return str_concat(this->msg, StrFormat(" (line %d, offset %d-%d: %r)", this->line_num, this->start_pos, this->end_pos, part));
1523}
1524
1525BigStr* Decode::__str__() {
1526 return this->Message();
1527}
1528
1529Encode::Encode(BigStr* msg) {
1530 this->msg = msg;
1531}
1532
1533BigStr* Encode::Message() {
1534 return this->msg;
1535}
1536
1537[[noreturn]] void e_usage(BigStr* msg, syntax_asdl::loc_t* location) {
1538 StackRoot _root0(&msg);
1539 StackRoot _root1(&location);
1540
1541 throw Alloc<Usage>(msg, location);
1542}
1543
1544[[noreturn]] void e_strict(BigStr* msg, syntax_asdl::loc_t* location) {
1545 StackRoot _root0(&msg);
1546 StackRoot _root1(&location);
1547
1548 throw Alloc<Strict>(msg, location);
1549}
1550
1551[[noreturn]] void p_die(BigStr* msg, syntax_asdl::loc_t* location) {
1552 StackRoot _root0(&msg);
1553 StackRoot _root1(&location);
1554
1555 throw Alloc<Parse>(msg, location);
1556}
1557
1558[[noreturn]] void e_die(BigStr* msg, syntax_asdl::loc_t* location) {
1559 StackRoot _root0(&msg);
1560 StackRoot _root1(&location);
1561
1562 throw Alloc<FatalRuntime>(1, msg, location);
1563}
1564
1565[[noreturn]] void e_die_status(int status, BigStr* msg, syntax_asdl::loc_t* location) {
1566 StackRoot _root0(&msg);
1567 StackRoot _root1(&location);
1568
1569 throw Alloc<FatalRuntime>(status, msg, location);
1570}
1571
1572} // define namespace error
1573
1574namespace num { // define
1575
1576using value_asdl::value;
1577
1578value::Int* ToBig(int i) {
1579 return Alloc<value::Int>(mops::IntWiden(i));
1580}
1581
1582mops::BigInt Exponent(mops::BigInt x, mops::BigInt y) {
1583 int y_int;
1584 mops::BigInt result;
1585 y_int = mops::BigTruncate(y);
1586 result = mops::BigInt(1);
1587 for (int i = 0; i < y_int; ++i) {
1588 result = mops::Mul(result, x);
1589 }
1590 return result;
1591}
1592
1593} // define namespace num
1594
1595namespace args { // define
1596
1597using syntax_asdl::loc;
1598using syntax_asdl::loc_t;
1599using syntax_asdl::CompoundWord;
1600using value_asdl::value;
1601using value_asdl::value_e;
1602using value_asdl::value_t;
1603using error::e_usage;
1604int String = 1;
1605int Int = 2;
1606int Float = 3;
1607int Bool = 4;
1608
1609_Attributes::_Attributes(Dict<BigStr*, value_asdl::value_t*>* defaults) {
1610 this->attrs = Alloc<Dict<BigStr*, value_asdl::value_t*>>();
1611 this->opt_changes = Alloc<List<Tuple2<BigStr*, bool>*>>();
1612 this->shopt_changes = Alloc<List<Tuple2<BigStr*, bool>*>>();
1613 this->show_options = false;
1614 this->actions = Alloc<List<BigStr*>>();
1615 this->saw_double_dash = false;
1616 for (DictIter<BigStr*, value_asdl::value_t*> it(defaults); !it.Done(); it.Next()) {
1617 BigStr* name = it.Key();
1618 value_asdl::value_t* v = it.Value();
1619 this->Set(name, v);
1620 }
1621}
1622
1623void _Attributes::SetTrue(BigStr* name) {
1624 StackRoot _root0(&name);
1625
1626 this->Set(name, Alloc<value::Bool>(true));
1627}
1628
1629void _Attributes::Set(BigStr* name, value_asdl::value_t* val) {
1630 StackRoot _root0(&name);
1631 StackRoot _root1(&val);
1632
1633 name = name->replace(str66, str67);
1634 if (str_equals(name, str68)) {
1635 name = str69;
1636 }
1637 this->attrs->set(name, val);
1638}
1639
1640Reader::Reader(List<BigStr*>* argv, List<syntax_asdl::CompoundWord*>* locs) {
1641 this->argv = argv;
1642 this->locs = locs;
1643 this->n = len(argv);
1644 this->i = 0;
1645}
1646
1647void Reader::Next() {
1648 this->i += 1;
1649}
1650
1651BigStr* Reader::Peek() {
1652 if (this->i >= this->n) {
1653 return nullptr;
1654 }
1655 else {
1656 return this->argv->at(this->i);
1657 }
1658}
1659
1660Tuple2<BigStr*, syntax_asdl::loc_t*> Reader::Peek2() {
1661 if (this->i >= this->n) {
1662 return Tuple2<BigStr*, syntax_asdl::loc_t*>(nullptr, loc::Missing);
1663 }
1664 else {
1665 return Tuple2<BigStr*, syntax_asdl::loc_t*>(this->argv->at(this->i), this->locs->at(this->i));
1666 }
1667}
1668
1669BigStr* Reader::ReadRequired(BigStr* error_msg) {
1670 BigStr* arg = nullptr;
1671 StackRoot _root0(&error_msg);
1672 StackRoot _root1(&arg);
1673
1674 arg = this->Peek();
1675 if (arg == nullptr) {
1676 e_usage(error_msg, this->_FirstLocation());
1677 }
1678 this->Next();
1679 return arg;
1680}
1681
1682Tuple2<BigStr*, syntax_asdl::loc_t*> Reader::ReadRequired2(BigStr* error_msg) {
1683 BigStr* arg = nullptr;
1684 syntax_asdl::CompoundWord* location = nullptr;
1685 StackRoot _root0(&error_msg);
1686 StackRoot _root1(&arg);
1687 StackRoot _root2(&location);
1688
1689 arg = this->Peek();
1690 if (arg == nullptr) {
1691 e_usage(error_msg, this->_FirstLocation());
1692 }
1693 location = this->locs->at(this->i);
1694 this->Next();
1695 return Tuple2<BigStr*, syntax_asdl::loc_t*>(arg, location);
1696}
1697
1698List<BigStr*>* Reader::Rest() {
1699 return this->argv->slice(this->i);
1700}
1701
1702Tuple2<List<BigStr*>*, List<syntax_asdl::CompoundWord*>*> Reader::Rest2() {
1703 return Tuple2<List<BigStr*>*, List<syntax_asdl::CompoundWord*>*>(this->argv->slice(this->i), this->locs->slice(this->i));
1704}
1705
1706bool Reader::AtEnd() {
1707 return this->i >= this->n;
1708}
1709
1710void Reader::Done() {
1711 if (!this->AtEnd()) {
1712 e_usage(str72, this->Location());
1713 }
1714}
1715
1716syntax_asdl::loc_t* Reader::_FirstLocation() {
1717 if ((this->locs != nullptr and this->locs->at(0) != nullptr)) {
1718 return this->locs->at(0);
1719 }
1720 else {
1721 return loc::Missing;
1722 }
1723}
1724
1725syntax_asdl::loc_t* Reader::Location() {
1726 int i;
1727 if (this->locs != nullptr) {
1728 if (this->i == this->n) {
1729 i = (this->n - 1);
1730 }
1731 else {
1732 i = this->i;
1733 }
1734 if (this->locs->at(i) != nullptr) {
1735 return this->locs->at(i);
1736 }
1737 else {
1738 return loc::Missing;
1739 }
1740 }
1741 else {
1742 return loc::Missing;
1743 }
1744}
1745
1746_Action::_Action() {
1747 ; // pass
1748}
1749
1750bool _Action::OnMatch(BigStr* attached_arg, args::Reader* arg_r, args::_Attributes* out) {
1751 StackRoot _root0(&attached_arg);
1752 StackRoot _root1(&arg_r);
1753 StackRoot _root2(&out);
1754
1755 FAIL(kNotImplemented); // Python NotImplementedError
1756}
1757
1758_ArgAction::_ArgAction(BigStr* name, bool quit_parsing_flags, List<BigStr*>* valid) {
1759 this->name = name;
1760 this->quit_parsing_flags = quit_parsing_flags;
1761 this->valid = valid;
1762}
1763
1764value_asdl::value_t* _ArgAction::_Value(BigStr* arg, syntax_asdl::loc_t* location) {
1765 StackRoot _root0(&arg);
1766 StackRoot _root1(&location);
1767
1768 FAIL(kNotImplemented); // Python NotImplementedError
1769}
1770
1771bool _ArgAction::OnMatch(BigStr* attached_arg, args::Reader* arg_r, args::_Attributes* out) {
1772 BigStr* arg = nullptr;
1773 value_asdl::value_t* val = nullptr;
1774 StackRoot _root0(&attached_arg);
1775 StackRoot _root1(&arg_r);
1776 StackRoot _root2(&out);
1777 StackRoot _root3(&arg);
1778 StackRoot _root4(&val);
1779
1780 if (attached_arg != nullptr) {
1781 arg = attached_arg;
1782 }
1783 else {
1784 arg_r->Next();
1785 arg = arg_r->Peek();
1786 if (arg == nullptr) {
1787 e_usage(StrFormat("expected argument to %r", str_concat(str74, this->name)), arg_r->Location());
1788 }
1789 }
1790 val = this->_Value(arg, arg_r->Location());
1791 out->Set(this->name, val);
1792 return this->quit_parsing_flags;
1793}
1794
1795SetToInt::SetToInt(BigStr* name) : ::args::_ArgAction(name, false, nullptr) {
1796}
1797
1798value_asdl::value_t* SetToInt::_Value(BigStr* arg, syntax_asdl::loc_t* location) {
1799 bool ok;
1800 mops::BigInt i;
1801 StackRoot _root0(&arg);
1802 StackRoot _root1(&location);
1803
1804 if (match::LooksLikeInteger(arg)) {
1805 Tuple2<bool, mops::BigInt> tup0 = mops::FromStr2(arg);
1806 ok = tup0.at0();
1807 i = tup0.at1();
1808 if (!ok) {
1809 e_usage(StrFormat("Integer too big: %s", arg), location);
1810 }
1811 }
1812 else {
1813 e_usage(StrFormat("expected integer after %s, got %r", str_concat(str77, this->name), arg), location);
1814 }
1815 if (mops::Greater(mops::BigInt(0), i)) {
1816 e_usage(StrFormat("got invalid integer for %s: %s", str_concat(str79, this->name), arg), location);
1817 }
1818 return Alloc<value::Int>(i);
1819}
1820
1821SetToFloat::SetToFloat(BigStr* name) : ::args::_ArgAction(name, false, nullptr) {
1822}
1823
1824value_asdl::value_t* SetToFloat::_Value(BigStr* arg, syntax_asdl::loc_t* location) {
1825 double f;
1826 StackRoot _root0(&arg);
1827 StackRoot _root1(&location);
1828
1829 try {
1830 f = to_float(arg);
1831 }
1832 catch (ValueError*) {
1833 e_usage(StrFormat("expected number after %r, got %r", str_concat(str81, this->name), arg), location);
1834 }
1835 if (f < 0) {
1836 e_usage(StrFormat("got invalid float for %s: %s", str_concat(str83, this->name), arg), location);
1837 }
1838 return Alloc<value::Float>(f);
1839}
1840
1841SetToString::SetToString(BigStr* name, bool quit_parsing_flags, List<BigStr*>* valid) : ::args::_ArgAction(name, quit_parsing_flags, valid) {
1842}
1843
1844value_asdl::value_t* SetToString::_Value(BigStr* arg, syntax_asdl::loc_t* location) {
1845 StackRoot _root0(&arg);
1846 StackRoot _root1(&location);
1847
1848 if ((this->valid != nullptr and !list_contains(this->valid, arg))) {
1849 e_usage(StrFormat("got invalid argument %r to %r, expected one of: %s", arg, str_concat(str85, this->name), str86->join(this->valid)), location);
1850 }
1851 return Alloc<value::Str>(arg);
1852}
1853
1854SetAttachedBool::SetAttachedBool(BigStr* name) {
1855 this->name = name;
1856}
1857
1858bool SetAttachedBool::OnMatch(BigStr* attached_arg, args::Reader* arg_r, args::_Attributes* out) {
1859 bool b;
1860 StackRoot _root0(&attached_arg);
1861 StackRoot _root1(&arg_r);
1862 StackRoot _root2(&out);
1863
1864 if (attached_arg != nullptr) {
1865 if ((str_equals(attached_arg, str87) || str_equals(attached_arg, str88) || str_equals(attached_arg, str89) || str_equals(attached_arg, str90))) {
1866 b = false;
1867 }
1868 else {
1869 if ((str_equals(attached_arg, str91) || str_equals(attached_arg, str92) || str_equals(attached_arg, str93) || str_equals(attached_arg, str94))) {
1870 b = true;
1871 }
1872 else {
1873 e_usage(StrFormat("got invalid argument to boolean flag: %r", attached_arg), loc::Missing);
1874 }
1875 }
1876 }
1877 else {
1878 b = true;
1879 }
1880 out->Set(this->name, Alloc<value::Bool>(b));
1881 return false;
1882}
1883
1884SetToTrue::SetToTrue(BigStr* name) {
1885 this->name = name;
1886}
1887
1888bool SetToTrue::OnMatch(BigStr* attached_arg, args::Reader* arg_r, args::_Attributes* out) {
1889 StackRoot _root0(&attached_arg);
1890 StackRoot _root1(&arg_r);
1891 StackRoot _root2(&out);
1892
1893 out->SetTrue(this->name);
1894 return false;
1895}
1896
1897SetOption::SetOption(BigStr* name) {
1898 this->name = name;
1899}
1900
1901bool SetOption::OnMatch(BigStr* attached_arg, args::Reader* arg_r, args::_Attributes* out) {
1902 bool b;
1903 StackRoot _root0(&attached_arg);
1904 StackRoot _root1(&arg_r);
1905 StackRoot _root2(&out);
1906
1907 b = maybe_str_equals(attached_arg, str96);
1908 out->opt_changes->append((Alloc<Tuple2<BigStr*, bool>>(this->name, b)));
1909 return false;
1910}
1911
1912SetNamedOption::SetNamedOption(bool shopt) {
1913 this->names = Alloc<List<BigStr*>>();
1914 this->shopt = shopt;
1915}
1916
1917void SetNamedOption::ArgName(BigStr* name) {
1918 StackRoot _root0(&name);
1919
1920 this->names->append(name);
1921}
1922
1923bool SetNamedOption::OnMatch(BigStr* attached_arg, args::Reader* arg_r, args::_Attributes* out) {
1924 bool b;
1925 BigStr* arg = nullptr;
1926 BigStr* attr_name = nullptr;
1927 List<Tuple2<BigStr*, bool>*>* changes = nullptr;
1928 StackRoot _root0(&attached_arg);
1929 StackRoot _root1(&arg_r);
1930 StackRoot _root2(&out);
1931 StackRoot _root3(&arg);
1932 StackRoot _root4(&attr_name);
1933 StackRoot _root5(&changes);
1934
1935 b = maybe_str_equals(attached_arg, str97);
1936 arg_r->Next();
1937 arg = arg_r->Peek();
1938 if (arg == nullptr) {
1939 out->show_options = true;
1940 return true;
1941 }
1942 attr_name = arg;
1943 if ((len(this->names) and !list_contains(this->names, attr_name))) {
1944 e_usage(StrFormat("Invalid option %r", arg), loc::Missing);
1945 }
1946 changes = this->shopt ? out->shopt_changes : out->opt_changes;
1947 changes->append((Alloc<Tuple2<BigStr*, bool>>(attr_name, b)));
1948 return false;
1949}
1950
1951SetAction::SetAction(BigStr* name) {
1952 this->name = name;
1953}
1954
1955bool SetAction::OnMatch(BigStr* attached_arg, args::Reader* arg_r, args::_Attributes* out) {
1956 StackRoot _root0(&attached_arg);
1957 StackRoot _root1(&arg_r);
1958 StackRoot _root2(&out);
1959
1960 out->actions->append(this->name);
1961 return false;
1962}
1963
1964SetNamedAction::SetNamedAction() {
1965 this->names = Alloc<List<BigStr*>>();
1966}
1967
1968void SetNamedAction::ArgName(BigStr* name) {
1969 StackRoot _root0(&name);
1970
1971 this->names->append(name);
1972}
1973
1974bool SetNamedAction::OnMatch(BigStr* attached_arg, args::Reader* arg_r, args::_Attributes* out) {
1975 BigStr* arg = nullptr;
1976 BigStr* attr_name = nullptr;
1977 StackRoot _root0(&attached_arg);
1978 StackRoot _root1(&arg_r);
1979 StackRoot _root2(&out);
1980 StackRoot _root3(&arg);
1981 StackRoot _root4(&attr_name);
1982
1983 arg_r->Next();
1984 arg = arg_r->Peek();
1985 if (arg == nullptr) {
1986 e_usage(str99, loc::Missing);
1987 }
1988 attr_name = arg;
1989 if ((len(this->names) and !list_contains(this->names, attr_name))) {
1990 e_usage(StrFormat("Invalid action name %r", arg), loc::Missing);
1991 }
1992 out->actions->append(attr_name);
1993 return false;
1994}
1995
1996args::_Attributes* Parse(flag_spec::_FlagSpec* spec, args::Reader* arg_r) {
1997 args::_Attributes* out = nullptr;
1998 BigStr* arg = nullptr;
1999 int pos;
2000 BigStr* suffix = nullptr;
2001 BigStr* flag_name = nullptr;
2002 args::_Action* action = nullptr;
2003 int n;
2004 BigStr* ch = nullptr;
2005 BigStr* attached_arg = nullptr;
2006 StackRoot _root0(&spec);
2007 StackRoot _root1(&arg_r);
2008 StackRoot _root2(&out);
2009 StackRoot _root3(&arg);
2010 StackRoot _root4(&suffix);
2011 StackRoot _root5(&flag_name);
2012 StackRoot _root6(&action);
2013 StackRoot _root7(&ch);
2014 StackRoot _root8(&attached_arg);
2015
2016 out = Alloc<_Attributes>(spec->defaults);
2017 while (!arg_r->AtEnd()) {
2018 arg = arg_r->Peek();
2019 if (maybe_str_equals(arg, str101)) {
2020 out->saw_double_dash = true;
2021 arg_r->Next();
2022 break;
2023 }
2024 if ((len(spec->actions_long) and arg->startswith(str102))) {
2025 pos = arg->find(str103, 2);
2026 if (pos == -1) {
2027 suffix = nullptr;
2028 flag_name = arg->slice(2);
2029 }
2030 else {
2031 suffix = arg->slice((pos + 1));
2032 flag_name = arg->slice(2, pos);
2033 }
2034 action = spec->actions_long->get(flag_name);
2035 if (action == nullptr) {
2036 e_usage(StrFormat("got invalid flag %r", arg), arg_r->Location());
2037 }
2038 action->OnMatch(suffix, arg_r, out);
2039 arg_r->Next();
2040 continue;
2041 }
2042 else {
2043 if ((arg->startswith(str105) and len(arg) > 1)) {
2044 n = len(arg);
2045 for (int i = 1; i < n; ++i) {
2046 ch = arg->at(i);
2047 if (str_equals(ch, str106)) {
2048 ch = str107;
2049 }
2050 if (list_contains(spec->plus_flags, ch)) {
2051 out->Set(ch, Alloc<value::Str>(str108));
2052 continue;
2053 }
2054 if (list_contains(spec->arity0, ch)) {
2055 out->SetTrue(ch);
2056 continue;
2057 }
2058 if (dict_contains(spec->arity1, ch)) {
2059 action = spec->arity1->at(ch);
2060 attached_arg = i < (n - 1) ? arg->slice((i + 1)) : nullptr;
2061 action->OnMatch(attached_arg, arg_r, out);
2062 break;
2063 }
2064 e_usage(StrFormat("doesn't accept flag %s", str_concat(str110, ch)), arg_r->Location());
2065 }
2066 arg_r->Next();
2067 }
2068 else {
2069 if ((len(spec->plus_flags) and (arg->startswith(str111) and len(arg) > 1))) {
2070 n = len(arg);
2071 for (int i = 1; i < n; ++i) {
2072 ch = arg->at(i);
2073 if (list_contains(spec->plus_flags, ch)) {
2074 out->Set(ch, Alloc<value::Str>(str112));
2075 continue;
2076 }
2077 e_usage(StrFormat("doesn't accept option %s", str_concat(str114, ch)), arg_r->Location());
2078 }
2079 arg_r->Next();
2080 }
2081 else {
2082 break;
2083 }
2084 }
2085 }
2086 }
2087 return out;
2088}
2089
2090args::_Attributes* ParseLikeEcho(flag_spec::_FlagSpec* spec, args::Reader* arg_r) {
2091 args::_Attributes* out = nullptr;
2092 BigStr* arg = nullptr;
2093 BigStr* chars = nullptr;
2094 bool done;
2095 StackRoot _root0(&spec);
2096 StackRoot _root1(&arg_r);
2097 StackRoot _root2(&out);
2098 StackRoot _root3(&arg);
2099 StackRoot _root4(&chars);
2100
2101 out = Alloc<_Attributes>(spec->defaults);
2102 while (!arg_r->AtEnd()) {
2103 arg = arg_r->Peek();
2104 chars = arg->slice(1);
2105 if ((arg->startswith(str115) and len(chars))) {
2106 done = false;
2107 for (StrIter it(chars); !it.Done(); it.Next()) {
2108 BigStr* c = it.Value();
2109 StackRoot _for(&c );
2110 if (!list_contains(spec->arity0, c)) {
2111 done = true;
2112 break;
2113 }
2114 }
2115 if (done) {
2116 break;
2117 }
2118 for (StrIter it(chars); !it.Done(); it.Next()) {
2119 BigStr* ch = it.Value();
2120 StackRoot _for(&ch );
2121 out->SetTrue(ch);
2122 }
2123 }
2124 else {
2125 break;
2126 }
2127 arg_r->Next();
2128 }
2129 return out;
2130}
2131
2132args::_Attributes* ParseMore(flag_spec::_FlagSpecAndMore* spec, args::Reader* arg_r) {
2133 args::_Attributes* out = nullptr;
2134 bool quit;
2135 BigStr* arg = nullptr;
2136 args::_Action* action = nullptr;
2137 BigStr* char0 = nullptr;
2138 BigStr* attached_arg = nullptr;
2139 StackRoot _root0(&spec);
2140 StackRoot _root1(&arg_r);
2141 StackRoot _root2(&out);
2142 StackRoot _root3(&arg);
2143 StackRoot _root4(&action);
2144 StackRoot _root5(&char0);
2145 StackRoot _root6(&attached_arg);
2146
2147 out = Alloc<_Attributes>(spec->defaults);
2148 quit = false;
2149 while (!arg_r->AtEnd()) {
2150 arg = arg_r->Peek();
2151 if (maybe_str_equals(arg, str116)) {
2152 out->saw_double_dash = true;
2153 arg_r->Next();
2154 break;
2155 }
2156 if (arg->startswith(str117)) {
2157 action = spec->actions_long->get(arg->slice(2));
2158 if (action == nullptr) {
2159 e_usage(StrFormat("got invalid flag %r", arg), arg_r->Location());
2160 }
2161 action->OnMatch(nullptr, arg_r, out);
2162 arg_r->Next();
2163 continue;
2164 }
2165 if (((arg->startswith(str119) or arg->startswith(str120)) and len(arg) > 1)) {
2166 char0 = arg->at(0);
2167 for (StrIter it(arg->slice(1)); !it.Done(); it.Next()) {
2168 BigStr* ch = it.Value();
2169 StackRoot _for(&ch );
2170 action = spec->actions_short->get(ch);
2171 if (action == nullptr) {
2172 e_usage(StrFormat("got invalid flag %r", str_concat(str122, ch)), arg_r->Location());
2173 }
2174 attached_arg = list_contains(spec->plus_flags, ch) ? char0 : nullptr;
2175 quit = action->OnMatch(attached_arg, arg_r, out);
2176 }
2177 arg_r->Next();
2178 if (quit) {
2179 break;
2180 }
2181 else {
2182 continue;
2183 }
2184 }
2185 break;
2186 }
2187 return out;
2188}
2189
2190} // define namespace args
2191