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

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