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

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