OILS / prebuilt / frontend / args.mycpp.cc View on Github | oils.pub

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