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

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