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

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