| 1 | // prebuilt/asdl/runtime.mycpp.cc: GENERATED by mycpp
|
| 2 |
|
| 3 | #include "prebuilt/asdl/runtime.mycpp.h"
|
| 4 | // This file is GENERATED by mycpp, from Python source code
|
| 5 |
|
| 6 | #include "mycpp/runtime.h"
|
| 7 |
|
| 8 | GLOBAL_STR(S_Aoo, "");
|
| 9 | GLOBAL_STR(S_nfs, "\n");
|
| 10 | GLOBAL_STR(S_yfk, "\u001b[0;0m");
|
| 11 | GLOBAL_STR(S_aaF, "\u001b[1m");
|
| 12 | GLOBAL_STR(S_sqm, "\u001b[31m");
|
| 13 | GLOBAL_STR(S_eda, "\u001b[32m");
|
| 14 | GLOBAL_STR(S_ysf, "\u001b[33m");
|
| 15 | GLOBAL_STR(S_osl, "\u001b[34m");
|
| 16 | GLOBAL_STR(S_vie, "\u001b[35m");
|
| 17 | GLOBAL_STR(S_mmi, "\u001b[36m");
|
| 18 | GLOBAL_STR(S_rpo, "\u001b[37m");
|
| 19 | GLOBAL_STR(S_sCc, "\u001b[4m");
|
| 20 | GLOBAL_STR(S_woy, "\u001b[7m");
|
| 21 | GLOBAL_STR(S_yfw, " ");
|
| 22 | GLOBAL_STR(S_Clt, "&");
|
| 23 | GLOBAL_STR(S_usD, "&");
|
| 24 | GLOBAL_STR(S_dyr, ">");
|
| 25 | GLOBAL_STR(S_Dcl, "<");
|
| 26 | GLOBAL_STR(S_ijB, "(");
|
| 27 | GLOBAL_STR(S_hxb, ")");
|
| 28 | GLOBAL_STR(S_fyj, ":");
|
| 29 | GLOBAL_STR(S_eox, "<");
|
| 30 | GLOBAL_STR(S_jye, ">");
|
| 31 | GLOBAL_STR(S_gFh, "F");
|
| 32 | GLOBAL_STR(S_cor, "T");
|
| 33 | GLOBAL_STR(S_Eax, "[");
|
| 34 | GLOBAL_STR(S_xmu, "[]");
|
| 35 | GLOBAL_STR(S_pcD, "]");
|
| 36 | GLOBAL_STR(S_tci, "_");
|
| 37 | GLOBAL_STR(S_gfw, "___ GC: after printing");
|
| 38 |
|
| 39 | namespace ansi { // forward declare
|
| 40 | }
|
| 41 |
|
| 42 | namespace pretty { // forward declare
|
| 43 | class PrettyPrinter;
|
| 44 | }
|
| 45 |
|
| 46 | namespace pp_hnode { // forward declare
|
| 47 | class BaseEncoder;
|
| 48 | class HNodeEncoder;
|
| 49 | }
|
| 50 |
|
| 51 | namespace cgi { // forward declare
|
| 52 | }
|
| 53 |
|
| 54 | namespace j8_lite { // forward declare
|
| 55 | }
|
| 56 |
|
| 57 | namespace ansi { // declare
|
| 58 |
|
| 59 | extern BigStr* RESET;
|
| 60 | extern BigStr* BOLD;
|
| 61 | extern BigStr* UNDERLINE;
|
| 62 | extern BigStr* REVERSE;
|
| 63 | extern BigStr* RED;
|
| 64 | extern BigStr* GREEN;
|
| 65 | extern BigStr* YELLOW;
|
| 66 | extern BigStr* BLUE;
|
| 67 | extern BigStr* MAGENTA;
|
| 68 | extern BigStr* CYAN;
|
| 69 | extern BigStr* WHITE;
|
| 70 |
|
| 71 | } // declare namespace ansi
|
| 72 |
|
| 73 | namespace pretty { // declare
|
| 74 |
|
| 75 | pretty_asdl::Measure* _EmptyMeasure();
|
| 76 | pretty_asdl::Measure* _FlattenMeasure(pretty_asdl::Measure* measure);
|
| 77 | pretty_asdl::Measure* _ConcatMeasure(pretty_asdl::Measure* m1, pretty_asdl::Measure* m2);
|
| 78 | int _SuffixLen(pretty_asdl::Measure* measure);
|
| 79 | pretty_asdl::MeasuredDoc* AsciiText(BigStr* string);
|
| 80 | pretty_asdl::MeasuredDoc* _Break(BigStr* string);
|
| 81 | pretty_asdl::MeasuredDoc* _Indent(int indent, pretty_asdl::MeasuredDoc* mdoc);
|
| 82 | pretty_asdl::Measure* _Splice(List<pretty_asdl::MeasuredDoc*>* out, List<pretty_asdl::MeasuredDoc*>* mdocs);
|
| 83 | pretty_asdl::MeasuredDoc* _Concat(List<pretty_asdl::MeasuredDoc*>* mdocs);
|
| 84 | pretty_asdl::MeasuredDoc* _Group(pretty_asdl::MeasuredDoc* mdoc);
|
| 85 | pretty_asdl::MeasuredDoc* _IfFlat(pretty_asdl::MeasuredDoc* flat_mdoc, pretty_asdl::MeasuredDoc* nonflat_mdoc);
|
| 86 | pretty_asdl::MeasuredDoc* _Flat(pretty_asdl::MeasuredDoc* mdoc);
|
| 87 | class PrettyPrinter {
|
| 88 | public:
|
| 89 | PrettyPrinter(int max_width);
|
| 90 | bool _Fits(int prefix_len, pretty_asdl::MeasuredDoc* group, pretty_asdl::Measure* suffix_measure);
|
| 91 | void PrintDoc(pretty_asdl::MeasuredDoc* document, mylib::BufWriter* buf);
|
| 92 | int max_width{};
|
| 93 |
|
| 94 | static constexpr ObjHeader obj_header() {
|
| 95 | return ObjHeader::ClassScanned(0, sizeof(PrettyPrinter));
|
| 96 | }
|
| 97 |
|
| 98 | DISALLOW_COPY_AND_ASSIGN(PrettyPrinter)
|
| 99 | };
|
| 100 |
|
| 101 |
|
| 102 | } // declare namespace pretty
|
| 103 |
|
| 104 | namespace pp_hnode { // declare
|
| 105 |
|
| 106 | class BaseEncoder {
|
| 107 | public:
|
| 108 | BaseEncoder();
|
| 109 | void SetIndent(int indent);
|
| 110 | void SetUseStyles(bool use_styles);
|
| 111 | void SetMaxTabularWidth(int max_tabular_width);
|
| 112 | pretty_asdl::MeasuredDoc* _Styled(BigStr* style, pretty_asdl::MeasuredDoc* mdoc);
|
| 113 | pretty_asdl::MeasuredDoc* _StyledAscii(BigStr* style, BigStr* s);
|
| 114 | pretty_asdl::MeasuredDoc* _Surrounded(BigStr* left, pretty_asdl::MeasuredDoc* mdoc, BigStr* right);
|
| 115 | pretty_asdl::MeasuredDoc* _SurroundedAndPrefixed(BigStr* left, pretty_asdl::MeasuredDoc* prefix, BigStr* sep, pretty_asdl::MeasuredDoc* mdoc, BigStr* right);
|
| 116 | pretty_asdl::MeasuredDoc* _Join(List<pretty_asdl::MeasuredDoc*>* items, BigStr* sep, BigStr* space);
|
| 117 | pretty_asdl::MeasuredDoc* _Tabular(List<pretty_asdl::MeasuredDoc*>* items, BigStr* sep);
|
| 118 | int indent{};
|
| 119 | int max_tabular_width{};
|
| 120 | bool use_styles{};
|
| 121 | Dict<int, bool>* visiting{};
|
| 122 |
|
| 123 | static constexpr uint32_t field_mask() {
|
| 124 | return maskbit(offsetof(BaseEncoder, visiting));
|
| 125 | }
|
| 126 |
|
| 127 | static constexpr ObjHeader obj_header() {
|
| 128 | return ObjHeader::ClassFixed(field_mask(), sizeof(BaseEncoder));
|
| 129 | }
|
| 130 |
|
| 131 | DISALLOW_COPY_AND_ASSIGN(BaseEncoder)
|
| 132 | };
|
| 133 |
|
| 134 | class HNodeEncoder : public ::pp_hnode::BaseEncoder {
|
| 135 | public:
|
| 136 | HNodeEncoder();
|
| 137 | pretty_asdl::MeasuredDoc* HNode(hnode_asdl::hnode_t* h);
|
| 138 | pretty_asdl::MeasuredDoc* _Field(hnode_asdl::Field* field);
|
| 139 | pretty_asdl::MeasuredDoc* _HNode(hnode_asdl::hnode_t* h);
|
| 140 |
|
| 141 | BigStr* field_color{};
|
| 142 | BigStr* type_color{};
|
| 143 |
|
| 144 | static constexpr uint32_t field_mask() {
|
| 145 | return ::pp_hnode::BaseEncoder::field_mask()
|
| 146 | | maskbit(offsetof(HNodeEncoder, field_color))
|
| 147 | | maskbit(offsetof(HNodeEncoder, type_color));
|
| 148 | }
|
| 149 |
|
| 150 | static constexpr ObjHeader obj_header() {
|
| 151 | return ObjHeader::ClassFixed(field_mask(), sizeof(HNodeEncoder));
|
| 152 | }
|
| 153 |
|
| 154 | DISALLOW_COPY_AND_ASSIGN(HNodeEncoder)
|
| 155 | };
|
| 156 |
|
| 157 |
|
| 158 | } // declare namespace pp_hnode
|
| 159 |
|
| 160 | namespace cgi { // declare
|
| 161 |
|
| 162 | BigStr* escape(BigStr* s);
|
| 163 |
|
| 164 | } // declare namespace cgi
|
| 165 |
|
| 166 | namespace j8_lite { // declare
|
| 167 |
|
| 168 | BigStr* EncodeString(BigStr* s, bool unquoted_ok = false);
|
| 169 | BigStr* YshEncodeString(BigStr* s);
|
| 170 | BigStr* MaybeShellEncode(BigStr* s);
|
| 171 | BigStr* ShellEncode(BigStr* s);
|
| 172 | BigStr* YshEncode(BigStr* s, bool unquoted_ok = false);
|
| 173 |
|
| 174 | } // declare namespace j8_lite
|
| 175 |
|
| 176 | namespace runtime { // define
|
| 177 |
|
| 178 | using hnode_asdl::hnode;
|
| 179 | using hnode_asdl::color_t;
|
| 180 | using hnode_asdl::color_e;
|
| 181 | int NO_SPID = -1;
|
| 182 |
|
| 183 | hnode::Record* NewRecord(BigStr* node_type) {
|
| 184 | return Alloc<hnode::Record>(node_type, S_ijB, S_hxb, Alloc<List<hnode_asdl::Field*>>(), nullptr);
|
| 185 | }
|
| 186 |
|
| 187 | hnode::Leaf* NewLeaf(BigStr* s, hnode_asdl::color_t e_color) {
|
| 188 | if (s == nullptr) {
|
| 189 | return Alloc<hnode::Leaf>(S_tci, color_e::OtherConst);
|
| 190 | }
|
| 191 | else {
|
| 192 | return Alloc<hnode::Leaf>(s, e_color);
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | TraversalState::TraversalState() {
|
| 197 | this->seen = Alloc<Dict<int, bool>>();
|
| 198 | this->ref_count = Alloc<Dict<int, int>>();
|
| 199 | }
|
| 200 | BigStr* TRUE_STR = S_cor;
|
| 201 | BigStr* FALSE_STR = S_gFh;
|
| 202 |
|
| 203 | } // define namespace runtime
|
| 204 |
|
| 205 | namespace format { // define
|
| 206 |
|
| 207 | using hnode_asdl::hnode;
|
| 208 | using hnode_asdl::hnode_e;
|
| 209 | using hnode_asdl::hnode_t;
|
| 210 | using pretty_asdl::doc;
|
| 211 | using pretty_asdl::doc_e;
|
| 212 | using pretty_asdl::doc_t;
|
| 213 | using pretty_asdl::MeasuredDoc;
|
| 214 | using pretty_asdl::List_Measured;
|
| 215 |
|
| 216 | int _HNodeCount(hnode_asdl::hnode_t* h) {
|
| 217 | hnode_asdl::hnode_t* UP_h = nullptr;
|
| 218 | int n;
|
| 219 | UP_h = h;
|
| 220 | switch (h->tag()) {
|
| 221 | case hnode_e::AlreadySeen: {
|
| 222 | return 1;
|
| 223 | }
|
| 224 | break;
|
| 225 | case hnode_e::Leaf: {
|
| 226 | return 1;
|
| 227 | }
|
| 228 | break;
|
| 229 | case hnode_e::Array: {
|
| 230 | hnode::Array* h = static_cast<hnode::Array*>(UP_h);
|
| 231 | n = 1;
|
| 232 | for (ListIter<hnode_asdl::hnode_t*> it(h->children); !it.Done(); it.Next()) {
|
| 233 | hnode_asdl::hnode_t* child = it.Value();
|
| 234 | n += _HNodeCount(child);
|
| 235 | }
|
| 236 | return n;
|
| 237 | }
|
| 238 | break;
|
| 239 | case hnode_e::Record: {
|
| 240 | hnode::Record* h = static_cast<hnode::Record*>(UP_h);
|
| 241 | n = 1;
|
| 242 | for (ListIter<hnode_asdl::Field*> it(h->fields); !it.Done(); it.Next()) {
|
| 243 | hnode_asdl::Field* field = it.Value();
|
| 244 | n += _HNodeCount(field->val);
|
| 245 | }
|
| 246 | if (h->unnamed_fields != nullptr) {
|
| 247 | for (ListIter<hnode_asdl::hnode_t*> it(h->unnamed_fields); !it.Done(); it.Next()) {
|
| 248 | hnode_asdl::hnode_t* child = it.Value();
|
| 249 | n += _HNodeCount(child);
|
| 250 | }
|
| 251 | }
|
| 252 | return n;
|
| 253 | }
|
| 254 | break;
|
| 255 | default: {
|
| 256 | assert(0); // AssertionError
|
| 257 | }
|
| 258 | }
|
| 259 | }
|
| 260 |
|
| 261 | int _DocCount(pretty_asdl::doc_t* d) {
|
| 262 | pretty_asdl::doc_t* UP_d = nullptr;
|
| 263 | int n;
|
| 264 | UP_d = d;
|
| 265 | switch (d->tag()) {
|
| 266 | case doc_e::Break: {
|
| 267 | return 1;
|
| 268 | }
|
| 269 | break;
|
| 270 | case doc_e::Text: {
|
| 271 | return 1;
|
| 272 | }
|
| 273 | break;
|
| 274 | case doc_e::Indent: {
|
| 275 | doc::Indent* d = static_cast<doc::Indent*>(UP_d);
|
| 276 | return (1 + _DocCount(d->mdoc->doc));
|
| 277 | }
|
| 278 | break;
|
| 279 | case doc_e::Group: {
|
| 280 | MeasuredDoc* d = static_cast<MeasuredDoc*>(UP_d);
|
| 281 | return (1 + _DocCount(d->doc));
|
| 282 | }
|
| 283 | break;
|
| 284 | case doc_e::Flat: {
|
| 285 | doc::Flat* d = static_cast<doc::Flat*>(UP_d);
|
| 286 | return (1 + _DocCount(d->mdoc->doc));
|
| 287 | }
|
| 288 | break;
|
| 289 | case doc_e::IfFlat: {
|
| 290 | doc::IfFlat* d = static_cast<doc::IfFlat*>(UP_d);
|
| 291 | return ((1 + _DocCount(d->flat_mdoc->doc)) + _DocCount(d->nonflat_mdoc->doc));
|
| 292 | }
|
| 293 | break;
|
| 294 | case doc_e::Concat: {
|
| 295 | List_Measured* d = static_cast<List_Measured*>(UP_d);
|
| 296 | n = 1;
|
| 297 | for (ListIter<pretty_asdl::MeasuredDoc*> it(d); !it.Done(); it.Next()) {
|
| 298 | pretty_asdl::MeasuredDoc* mdoc = it.Value();
|
| 299 | n += _DocCount(mdoc->doc);
|
| 300 | }
|
| 301 | return n;
|
| 302 | }
|
| 303 | break;
|
| 304 | default: {
|
| 305 | assert(0); // AssertionError
|
| 306 | }
|
| 307 | }
|
| 308 | }
|
| 309 |
|
| 310 | void _HNodePrettyPrint(bool perf_stats, bool doc_debug, hnode_asdl::hnode_t* node, mylib::Writer* f, int max_width) {
|
| 311 | pp_hnode::HNodeEncoder* enc = nullptr;
|
| 312 | pretty_asdl::MeasuredDoc* d = nullptr;
|
| 313 | hnode_asdl::hnode_t* p = nullptr;
|
| 314 | pretty::PrettyPrinter* printer = nullptr;
|
| 315 | mylib::BufWriter* buf = nullptr;
|
| 316 | StackRoot _root0(&node);
|
| 317 | StackRoot _root1(&f);
|
| 318 | StackRoot _root2(&enc);
|
| 319 | StackRoot _root3(&d);
|
| 320 | StackRoot _root4(&p);
|
| 321 |
|
| 322 | mylib::MaybeCollect();
|
| 323 | if (perf_stats) {
|
| 324 | mylib::print_stderr(StrFormat("___ HNODE COUNT %d", _HNodeCount(node)));
|
| 325 | mylib::print_stderr(S_Aoo);
|
| 326 | }
|
| 327 | enc = Alloc<pp_hnode::HNodeEncoder>();
|
| 328 | enc->SetUseStyles(f->isatty());
|
| 329 | enc->SetIndent(2);
|
| 330 | d = enc->HNode(node);
|
| 331 | mylib::MaybeCollect();
|
| 332 | if (perf_stats) {
|
| 333 | if (doc_debug) {
|
| 334 | p = d->PrettyTree(false);
|
| 335 | _HNodePrettyPrint(perf_stats, false, p, f);
|
| 336 | }
|
| 337 | mylib::print_stderr(StrFormat("___ DOC COUNT %d", _DocCount(d)));
|
| 338 | mylib::print_stderr(S_Aoo);
|
| 339 | }
|
| 340 | printer = Alloc<pretty::PrettyPrinter>(max_width);
|
| 341 | buf = Alloc<mylib::BufWriter>();
|
| 342 | printer->PrintDoc(d, buf);
|
| 343 | f->write(buf->getvalue());
|
| 344 | f->write(S_nfs);
|
| 345 | mylib::MaybeCollect();
|
| 346 | if (perf_stats) {
|
| 347 | mylib::print_stderr(S_gfw);
|
| 348 | mylib::PrintGcStats();
|
| 349 | mylib::print_stderr(S_Aoo);
|
| 350 | }
|
| 351 | }
|
| 352 |
|
| 353 | void HNodePrettyPrint(hnode_asdl::hnode_t* node, mylib::Writer* f, int max_width) {
|
| 354 | StackRoot _root0(&node);
|
| 355 | StackRoot _root1(&f);
|
| 356 |
|
| 357 | _HNodePrettyPrint(false, true, node, f, max_width);
|
| 358 | }
|
| 359 |
|
| 360 | } // define namespace format
|
| 361 |
|
| 362 | namespace ansi { // define
|
| 363 |
|
| 364 | BigStr* RESET = S_yfk;
|
| 365 | BigStr* BOLD = S_aaF;
|
| 366 | BigStr* UNDERLINE = S_sCc;
|
| 367 | BigStr* REVERSE = S_woy;
|
| 368 | BigStr* RED = S_sqm;
|
| 369 | BigStr* GREEN = S_eda;
|
| 370 | BigStr* YELLOW = S_ysf;
|
| 371 | BigStr* BLUE = S_osl;
|
| 372 | BigStr* MAGENTA = S_vie;
|
| 373 | BigStr* CYAN = S_mmi;
|
| 374 | BigStr* WHITE = S_rpo;
|
| 375 |
|
| 376 | } // define namespace ansi
|
| 377 |
|
| 378 | namespace pretty { // define
|
| 379 |
|
| 380 | using pretty_asdl::doc;
|
| 381 | using pretty_asdl::doc_e;
|
| 382 | using pretty_asdl::DocFragment;
|
| 383 | using pretty_asdl::Measure;
|
| 384 | using pretty_asdl::MeasuredDoc;
|
| 385 | using pretty_asdl::List_Measured;
|
| 386 | using mylib::BufWriter;
|
| 387 |
|
| 388 | pretty_asdl::Measure* _EmptyMeasure() {
|
| 389 | return Alloc<Measure>(0, -1);
|
| 390 | }
|
| 391 |
|
| 392 | pretty_asdl::Measure* _FlattenMeasure(pretty_asdl::Measure* measure) {
|
| 393 | return Alloc<Measure>(measure->flat, -1);
|
| 394 | }
|
| 395 |
|
| 396 | pretty_asdl::Measure* _ConcatMeasure(pretty_asdl::Measure* m1, pretty_asdl::Measure* m2) {
|
| 397 | if (m1->nonflat != -1) {
|
| 398 | return Alloc<Measure>((m1->flat + m2->flat), m1->nonflat);
|
| 399 | }
|
| 400 | else {
|
| 401 | if (m2->nonflat != -1) {
|
| 402 | return Alloc<Measure>((m1->flat + m2->flat), (m1->flat + m2->nonflat));
|
| 403 | }
|
| 404 | else {
|
| 405 | return Alloc<Measure>((m1->flat + m2->flat), -1);
|
| 406 | }
|
| 407 | }
|
| 408 | }
|
| 409 |
|
| 410 | int _SuffixLen(pretty_asdl::Measure* measure) {
|
| 411 | if (measure->nonflat != -1) {
|
| 412 | return measure->nonflat;
|
| 413 | }
|
| 414 | else {
|
| 415 | return measure->flat;
|
| 416 | }
|
| 417 | }
|
| 418 |
|
| 419 | pretty_asdl::MeasuredDoc* AsciiText(BigStr* string) {
|
| 420 | return Alloc<MeasuredDoc>(Alloc<doc::Text>(string), Alloc<Measure>(len(string), -1));
|
| 421 | }
|
| 422 |
|
| 423 | pretty_asdl::MeasuredDoc* _Break(BigStr* string) {
|
| 424 | return Alloc<MeasuredDoc>(Alloc<doc::Break>(string), Alloc<Measure>(len(string), 0));
|
| 425 | }
|
| 426 |
|
| 427 | pretty_asdl::MeasuredDoc* _Indent(int indent, pretty_asdl::MeasuredDoc* mdoc) {
|
| 428 | return Alloc<MeasuredDoc>(Alloc<doc::Indent>(indent, mdoc), mdoc->measure);
|
| 429 | }
|
| 430 |
|
| 431 | pretty_asdl::Measure* _Splice(List<pretty_asdl::MeasuredDoc*>* out, List<pretty_asdl::MeasuredDoc*>* mdocs) {
|
| 432 | pretty_asdl::Measure* measure = nullptr;
|
| 433 | pretty_asdl::List_Measured* child = nullptr;
|
| 434 | measure = _EmptyMeasure();
|
| 435 | for (ListIter<pretty_asdl::MeasuredDoc*> it(mdocs); !it.Done(); it.Next()) {
|
| 436 | pretty_asdl::MeasuredDoc* mdoc = it.Value();
|
| 437 | switch (mdoc->doc->tag()) {
|
| 438 | case doc_e::Concat: {
|
| 439 | child = static_cast<List_Measured*>(mdoc->doc);
|
| 440 | _Splice(out, child);
|
| 441 | }
|
| 442 | break;
|
| 443 | default: {
|
| 444 | out->append(mdoc);
|
| 445 | }
|
| 446 | }
|
| 447 | measure = _ConcatMeasure(measure, mdoc->measure);
|
| 448 | }
|
| 449 | return measure;
|
| 450 | }
|
| 451 |
|
| 452 | pretty_asdl::MeasuredDoc* _Concat(List<pretty_asdl::MeasuredDoc*>* mdocs) {
|
| 453 | pretty_asdl::List_Measured* flattened = nullptr;
|
| 454 | pretty_asdl::Measure* measure = nullptr;
|
| 455 | flattened = List_Measured::New();
|
| 456 | measure = _Splice(flattened, mdocs);
|
| 457 | return Alloc<MeasuredDoc>(flattened, measure);
|
| 458 | }
|
| 459 |
|
| 460 | pretty_asdl::MeasuredDoc* _Group(pretty_asdl::MeasuredDoc* mdoc) {
|
| 461 | return Alloc<MeasuredDoc>(mdoc, mdoc->measure);
|
| 462 | }
|
| 463 |
|
| 464 | pretty_asdl::MeasuredDoc* _IfFlat(pretty_asdl::MeasuredDoc* flat_mdoc, pretty_asdl::MeasuredDoc* nonflat_mdoc) {
|
| 465 | return Alloc<MeasuredDoc>(Alloc<doc::IfFlat>(flat_mdoc, nonflat_mdoc), Alloc<Measure>(flat_mdoc->measure->flat, nonflat_mdoc->measure->nonflat));
|
| 466 | }
|
| 467 |
|
| 468 | pretty_asdl::MeasuredDoc* _Flat(pretty_asdl::MeasuredDoc* mdoc) {
|
| 469 | return Alloc<MeasuredDoc>(Alloc<doc::Flat>(mdoc), _FlattenMeasure(mdoc->measure));
|
| 470 | }
|
| 471 |
|
| 472 | PrettyPrinter::PrettyPrinter(int max_width) {
|
| 473 | this->max_width = max_width;
|
| 474 | }
|
| 475 |
|
| 476 | bool PrettyPrinter::_Fits(int prefix_len, pretty_asdl::MeasuredDoc* group, pretty_asdl::Measure* suffix_measure) {
|
| 477 | pretty_asdl::Measure* measure = nullptr;
|
| 478 | measure = _ConcatMeasure(_FlattenMeasure(group->measure), suffix_measure);
|
| 479 | return (prefix_len + _SuffixLen(measure)) <= this->max_width;
|
| 480 | }
|
| 481 |
|
| 482 | void PrettyPrinter::PrintDoc(pretty_asdl::MeasuredDoc* document, mylib::BufWriter* buf) {
|
| 483 | int prefix_len;
|
| 484 | List<pretty_asdl::DocFragment*>* fragments = nullptr;
|
| 485 | int max_stack;
|
| 486 | pretty_asdl::DocFragment* frag = nullptr;
|
| 487 | pretty_asdl::doc_t* UP_doc = nullptr;
|
| 488 | pretty_asdl::Measure* measure = nullptr;
|
| 489 | bool is_flat;
|
| 490 | pretty_asdl::MeasuredDoc* subdoc = nullptr;
|
| 491 | prefix_len = 0;
|
| 492 | fragments = NewList<pretty_asdl::DocFragment*>(std::initializer_list<pretty_asdl::DocFragment*>{Alloc<DocFragment>(_Group(document), 0, false, _EmptyMeasure())});
|
| 493 | max_stack = len(fragments);
|
| 494 | while (len(fragments) > 0) {
|
| 495 | max_stack = max(max_stack, len(fragments));
|
| 496 | frag = fragments->pop();
|
| 497 | UP_doc = frag->mdoc->doc;
|
| 498 | switch (UP_doc->tag()) {
|
| 499 | case doc_e::Text: {
|
| 500 | doc::Text* text = static_cast<doc::Text*>(UP_doc);
|
| 501 | buf->write(text->string);
|
| 502 | prefix_len += frag->mdoc->measure->flat;
|
| 503 | }
|
| 504 | break;
|
| 505 | case doc_e::Break: {
|
| 506 | doc::Break* break_ = static_cast<doc::Break*>(UP_doc);
|
| 507 | if (frag->is_flat) {
|
| 508 | buf->write(break_->string);
|
| 509 | prefix_len += frag->mdoc->measure->flat;
|
| 510 | }
|
| 511 | else {
|
| 512 | buf->write(S_nfs);
|
| 513 | buf->write_spaces(frag->indent);
|
| 514 | prefix_len = frag->indent;
|
| 515 | }
|
| 516 | }
|
| 517 | break;
|
| 518 | case doc_e::Indent: {
|
| 519 | doc::Indent* indented = static_cast<doc::Indent*>(UP_doc);
|
| 520 | fragments->append(Alloc<DocFragment>(indented->mdoc, (frag->indent + indented->indent), frag->is_flat, frag->measure));
|
| 521 | }
|
| 522 | break;
|
| 523 | case doc_e::Concat: {
|
| 524 | List_Measured* concat = static_cast<List_Measured*>(UP_doc);
|
| 525 | measure = frag->measure;
|
| 526 | for (ReverseListIter<pretty_asdl::MeasuredDoc*> it(concat); !it.Done(); it.Next()) {
|
| 527 | pretty_asdl::MeasuredDoc* mdoc = it.Value();
|
| 528 | fragments->append(Alloc<DocFragment>(mdoc, frag->indent, frag->is_flat, measure));
|
| 529 | measure = _ConcatMeasure(mdoc->measure, measure);
|
| 530 | }
|
| 531 | }
|
| 532 | break;
|
| 533 | case doc_e::Group: {
|
| 534 | MeasuredDoc* group = static_cast<MeasuredDoc*>(UP_doc);
|
| 535 | is_flat = this->_Fits(prefix_len, group, frag->measure);
|
| 536 | fragments->append(Alloc<DocFragment>(group, frag->indent, is_flat, frag->measure));
|
| 537 | }
|
| 538 | break;
|
| 539 | case doc_e::IfFlat: {
|
| 540 | doc::IfFlat* if_flat = static_cast<doc::IfFlat*>(UP_doc);
|
| 541 | if (frag->is_flat) {
|
| 542 | subdoc = if_flat->flat_mdoc;
|
| 543 | }
|
| 544 | else {
|
| 545 | subdoc = if_flat->nonflat_mdoc;
|
| 546 | }
|
| 547 | fragments->append(Alloc<DocFragment>(subdoc, frag->indent, frag->is_flat, frag->measure));
|
| 548 | }
|
| 549 | break;
|
| 550 | case doc_e::Flat: {
|
| 551 | doc::Flat* flat_doc = static_cast<doc::Flat*>(UP_doc);
|
| 552 | fragments->append(Alloc<DocFragment>(flat_doc->mdoc, frag->indent, true, frag->measure));
|
| 553 | }
|
| 554 | break;
|
| 555 | }
|
| 556 | }
|
| 557 | }
|
| 558 |
|
| 559 | } // define namespace pretty
|
| 560 |
|
| 561 | namespace pp_hnode { // define
|
| 562 |
|
| 563 | using hnode_asdl::hnode;
|
| 564 | using hnode_asdl::hnode_e;
|
| 565 | using hnode_asdl::hnode_t;
|
| 566 | using hnode_asdl::Field;
|
| 567 | using hnode_asdl::color_e;
|
| 568 | using pretty_asdl::doc;
|
| 569 | using pretty_asdl::MeasuredDoc;
|
| 570 | using pretty_asdl::Measure;
|
| 571 | using pretty::_Break;
|
| 572 | using pretty::_Concat;
|
| 573 | using pretty::_Flat;
|
| 574 | using pretty::_Group;
|
| 575 | using pretty::_IfFlat;
|
| 576 | using pretty::_Indent;
|
| 577 | using pretty::_EmptyMeasure;
|
| 578 | using pretty::AsciiText;
|
| 579 |
|
| 580 | BaseEncoder::BaseEncoder() {
|
| 581 | this->indent = 4;
|
| 582 | this->use_styles = true;
|
| 583 | this->max_tabular_width = 22;
|
| 584 | this->visiting = Alloc<Dict<int, bool>>();
|
| 585 | }
|
| 586 |
|
| 587 | void BaseEncoder::SetIndent(int indent) {
|
| 588 | this->indent = indent;
|
| 589 | }
|
| 590 |
|
| 591 | void BaseEncoder::SetUseStyles(bool use_styles) {
|
| 592 | this->use_styles = use_styles;
|
| 593 | }
|
| 594 |
|
| 595 | void BaseEncoder::SetMaxTabularWidth(int max_tabular_width) {
|
| 596 | this->max_tabular_width = max_tabular_width;
|
| 597 | }
|
| 598 |
|
| 599 | pretty_asdl::MeasuredDoc* BaseEncoder::_Styled(BigStr* style, pretty_asdl::MeasuredDoc* mdoc) {
|
| 600 | if (this->use_styles) {
|
| 601 | 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())}));
|
| 602 | }
|
| 603 | else {
|
| 604 | return mdoc;
|
| 605 | }
|
| 606 | }
|
| 607 |
|
| 608 | pretty_asdl::MeasuredDoc* BaseEncoder::_StyledAscii(BigStr* style, BigStr* s) {
|
| 609 | pretty_asdl::Measure* measure = nullptr;
|
| 610 | measure = Alloc<Measure>(len(s), -1);
|
| 611 | if (this->use_styles) {
|
| 612 | s = StrFormat("%s%s%s", style, s, ansi::RESET);
|
| 613 | }
|
| 614 | return Alloc<MeasuredDoc>(Alloc<doc::Text>(s), measure);
|
| 615 | }
|
| 616 |
|
| 617 | pretty_asdl::MeasuredDoc* BaseEncoder::_Surrounded(BigStr* left, pretty_asdl::MeasuredDoc* mdoc, BigStr* right) {
|
| 618 | 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)})));
|
| 619 | }
|
| 620 |
|
| 621 | pretty_asdl::MeasuredDoc* BaseEncoder::_SurroundedAndPrefixed(BigStr* left, pretty_asdl::MeasuredDoc* prefix, BigStr* sep, pretty_asdl::MeasuredDoc* mdoc, BigStr* right) {
|
| 622 | 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)})));
|
| 623 | }
|
| 624 |
|
| 625 | pretty_asdl::MeasuredDoc* BaseEncoder::_Join(List<pretty_asdl::MeasuredDoc*>* items, BigStr* sep, BigStr* space) {
|
| 626 | List<pretty_asdl::MeasuredDoc*>* seq = nullptr;
|
| 627 | int i;
|
| 628 | seq = Alloc<List<pretty_asdl::MeasuredDoc*>>();
|
| 629 | i = 0;
|
| 630 | for (ListIter<pretty_asdl::MeasuredDoc*> it(items); !it.Done(); it.Next(), ++i) {
|
| 631 | pretty_asdl::MeasuredDoc* item = it.Value();
|
| 632 | if (i != 0) {
|
| 633 | seq->append(AsciiText(sep));
|
| 634 | seq->append(_Break(space));
|
| 635 | }
|
| 636 | seq->append(item);
|
| 637 | }
|
| 638 | return _Concat(seq);
|
| 639 | }
|
| 640 |
|
| 641 | pretty_asdl::MeasuredDoc* BaseEncoder::_Tabular(List<pretty_asdl::MeasuredDoc*>* items, BigStr* sep) {
|
| 642 | int max_flat_len;
|
| 643 | List<pretty_asdl::MeasuredDoc*>* seq = nullptr;
|
| 644 | int i;
|
| 645 | pretty_asdl::MeasuredDoc* non_tabular = nullptr;
|
| 646 | int sep_width;
|
| 647 | List<pretty_asdl::MeasuredDoc*>* tabular_seq = nullptr;
|
| 648 | int padding;
|
| 649 | pretty_asdl::MeasuredDoc* tabular = nullptr;
|
| 650 | if (len(items) == 0) {
|
| 651 | return AsciiText(S_Aoo);
|
| 652 | }
|
| 653 | max_flat_len = 0;
|
| 654 | seq = Alloc<List<pretty_asdl::MeasuredDoc*>>();
|
| 655 | i = 0;
|
| 656 | for (ListIter<pretty_asdl::MeasuredDoc*> it(items); !it.Done(); it.Next(), ++i) {
|
| 657 | pretty_asdl::MeasuredDoc* item = it.Value();
|
| 658 | if (i != 0) {
|
| 659 | seq->append(AsciiText(sep));
|
| 660 | seq->append(_Break(S_yfw));
|
| 661 | }
|
| 662 | seq->append(item);
|
| 663 | max_flat_len = max(max_flat_len, item->measure->flat);
|
| 664 | }
|
| 665 | non_tabular = _Concat(seq);
|
| 666 | sep_width = len(sep);
|
| 667 | if (((max_flat_len + sep_width) + 1) <= this->max_tabular_width) {
|
| 668 | tabular_seq = Alloc<List<pretty_asdl::MeasuredDoc*>>();
|
| 669 | i = 0;
|
| 670 | for (ListIter<pretty_asdl::MeasuredDoc*> it(items); !it.Done(); it.Next(), ++i) {
|
| 671 | pretty_asdl::MeasuredDoc* item = it.Value();
|
| 672 | tabular_seq->append(_Flat(item));
|
| 673 | if (i != (len(items) - 1)) {
|
| 674 | padding = ((max_flat_len - item->measure->flat) + 1);
|
| 675 | tabular_seq->append(AsciiText(sep));
|
| 676 | tabular_seq->append(_Group(_Break(str_repeat(S_yfw, padding))));
|
| 677 | }
|
| 678 | }
|
| 679 | tabular = _Concat(tabular_seq);
|
| 680 | return _Group(_IfFlat(non_tabular, tabular));
|
| 681 | }
|
| 682 | else {
|
| 683 | return non_tabular;
|
| 684 | }
|
| 685 | }
|
| 686 |
|
| 687 | HNodeEncoder::HNodeEncoder() : ::pp_hnode::BaseEncoder() {
|
| 688 | this->type_color = ansi::YELLOW;
|
| 689 | this->field_color = ansi::MAGENTA;
|
| 690 | }
|
| 691 |
|
| 692 | pretty_asdl::MeasuredDoc* HNodeEncoder::HNode(hnode_asdl::hnode_t* h) {
|
| 693 | StackRoot _root0(&h);
|
| 694 |
|
| 695 | this->visiting->clear();
|
| 696 | return this->_HNode(h);
|
| 697 | }
|
| 698 |
|
| 699 | pretty_asdl::MeasuredDoc* HNodeEncoder::_Field(hnode_asdl::Field* field) {
|
| 700 | pretty_asdl::MeasuredDoc* name = nullptr;
|
| 701 | StackRoot _root0(&field);
|
| 702 | StackRoot _root1(&name);
|
| 703 |
|
| 704 | name = AsciiText(str_concat(field->name, S_fyj));
|
| 705 | return _Concat(NewList<pretty_asdl::MeasuredDoc*>(std::initializer_list<pretty_asdl::MeasuredDoc*>{name, this->_HNode(field->val)}));
|
| 706 | }
|
| 707 |
|
| 708 | pretty_asdl::MeasuredDoc* HNodeEncoder::_HNode(hnode_asdl::hnode_t* h) {
|
| 709 | hnode_asdl::hnode_t* UP_h = nullptr;
|
| 710 | BigStr* color = nullptr;
|
| 711 | BigStr* s = nullptr;
|
| 712 | List<pretty_asdl::MeasuredDoc*>* children = nullptr;
|
| 713 | pretty_asdl::MeasuredDoc* type_name = nullptr;
|
| 714 | List<pretty_asdl::MeasuredDoc*>* mdocs = nullptr;
|
| 715 | List<pretty_asdl::MeasuredDoc*>* m = nullptr;
|
| 716 | pretty_asdl::MeasuredDoc* child = nullptr;
|
| 717 | StackRoot _root0(&h);
|
| 718 | StackRoot _root1(&children);
|
| 719 | StackRoot _root2(&type_name);
|
| 720 | StackRoot _root3(&mdocs);
|
| 721 |
|
| 722 | UP_h = h;
|
| 723 | switch (h->tag()) {
|
| 724 | case hnode_e::AlreadySeen: {
|
| 725 | hnode::AlreadySeen* h = static_cast<hnode::AlreadySeen*>(UP_h);
|
| 726 | return pretty::AsciiText(StrFormat("...0x%s", mylib::hex_lower(h->heap_id)));
|
| 727 | }
|
| 728 | break;
|
| 729 | case hnode_e::Leaf: {
|
| 730 | hnode::Leaf* h = static_cast<hnode::Leaf*>(UP_h);
|
| 731 | switch (h->color) {
|
| 732 | case color_e::TypeName: {
|
| 733 | color = ansi::YELLOW;
|
| 734 | }
|
| 735 | break;
|
| 736 | case color_e::StringConst: {
|
| 737 | color = ansi::BOLD;
|
| 738 | }
|
| 739 | break;
|
| 740 | case color_e::OtherConst: {
|
| 741 | color = ansi::GREEN;
|
| 742 | }
|
| 743 | break;
|
| 744 | case color_e::External: {
|
| 745 | color = str_concat(ansi::BOLD, ansi::BLUE);
|
| 746 | }
|
| 747 | break;
|
| 748 | case color_e::UserType: {
|
| 749 | color = ansi::GREEN;
|
| 750 | }
|
| 751 | break;
|
| 752 | default: {
|
| 753 | assert(0); // AssertionError
|
| 754 | }
|
| 755 | }
|
| 756 | s = j8_lite::EncodeString(h->s, true);
|
| 757 | return this->_StyledAscii(color, s);
|
| 758 | }
|
| 759 | break;
|
| 760 | case hnode_e::Array: {
|
| 761 | hnode::Array* h = static_cast<hnode::Array*>(UP_h);
|
| 762 | mylib::MaybeCollect();
|
| 763 | if (len(h->children) == 0) {
|
| 764 | return AsciiText(S_xmu);
|
| 765 | }
|
| 766 | children = Alloc<List<pretty_asdl::MeasuredDoc*>>();
|
| 767 | for (ListIter<hnode_asdl::hnode_t*> it(h->children); !it.Done(); it.Next()) {
|
| 768 | hnode_asdl::hnode_t* item = it.Value();
|
| 769 | children->append(this->_HNode(item));
|
| 770 | }
|
| 771 | return this->_Surrounded(S_Eax, this->_Tabular(children, S_Aoo), S_pcD);
|
| 772 | }
|
| 773 | break;
|
| 774 | case hnode_e::Record: {
|
| 775 | hnode::Record* h = static_cast<hnode::Record*>(UP_h);
|
| 776 | type_name = nullptr;
|
| 777 | if (len(h->node_type)) {
|
| 778 | type_name = this->_StyledAscii(this->type_color, h->node_type);
|
| 779 | }
|
| 780 | mdocs = nullptr;
|
| 781 | if ((h->unnamed_fields != nullptr and len(h->unnamed_fields))) {
|
| 782 | mdocs = Alloc<List<pretty_asdl::MeasuredDoc*>>();
|
| 783 | for (ListIter<hnode_asdl::hnode_t*> it(h->unnamed_fields); !it.Done(); it.Next()) {
|
| 784 | hnode_asdl::hnode_t* item = it.Value();
|
| 785 | mdocs->append(this->_HNode(item));
|
| 786 | }
|
| 787 | }
|
| 788 | else {
|
| 789 | if (len(h->fields) != 0) {
|
| 790 | mdocs = Alloc<List<pretty_asdl::MeasuredDoc*>>();
|
| 791 | for (ListIter<hnode_asdl::Field*> it(h->fields); !it.Done(); it.Next()) {
|
| 792 | hnode_asdl::Field* field = it.Value();
|
| 793 | mdocs->append(this->_Field(field));
|
| 794 | }
|
| 795 | }
|
| 796 | }
|
| 797 | if (mdocs == nullptr) {
|
| 798 | m = NewList<pretty_asdl::MeasuredDoc*>(std::initializer_list<pretty_asdl::MeasuredDoc*>{AsciiText(h->left)});
|
| 799 | if (type_name != nullptr) {
|
| 800 | m->append(type_name);
|
| 801 | }
|
| 802 | m->append(AsciiText(h->right));
|
| 803 | return _Concat(m);
|
| 804 | }
|
| 805 | child = this->_Join(mdocs, S_Aoo, S_yfw);
|
| 806 | if (type_name != nullptr) {
|
| 807 | return this->_SurroundedAndPrefixed(h->left, type_name, S_yfw, child, h->right);
|
| 808 | }
|
| 809 | else {
|
| 810 | return this->_Surrounded(h->left, child, h->right);
|
| 811 | }
|
| 812 | }
|
| 813 | break;
|
| 814 | default: {
|
| 815 | assert(0); // AssertionError
|
| 816 | }
|
| 817 | }
|
| 818 | }
|
| 819 |
|
| 820 | } // define namespace pp_hnode
|
| 821 |
|
| 822 | namespace cgi { // define
|
| 823 |
|
| 824 |
|
| 825 | BigStr* escape(BigStr* s) {
|
| 826 | s = s->replace(S_Clt, S_usD);
|
| 827 | s = s->replace(S_eox, S_Dcl);
|
| 828 | s = s->replace(S_jye, S_dyr);
|
| 829 | return s;
|
| 830 | }
|
| 831 |
|
| 832 | } // define namespace cgi
|
| 833 |
|
| 834 | namespace j8_lite { // define
|
| 835 |
|
| 836 |
|
| 837 | BigStr* EncodeString(BigStr* s, bool unquoted_ok) {
|
| 838 | if ((unquoted_ok and fastfunc::CanOmitQuotes(s))) {
|
| 839 | return s;
|
| 840 | }
|
| 841 | return fastfunc::J8EncodeString(s, 1);
|
| 842 | }
|
| 843 |
|
| 844 | BigStr* YshEncodeString(BigStr* s) {
|
| 845 | return fastfunc::ShellEncodeString(s, 1);
|
| 846 | }
|
| 847 |
|
| 848 | BigStr* MaybeShellEncode(BigStr* s) {
|
| 849 | if (fastfunc::CanOmitQuotes(s)) {
|
| 850 | return s;
|
| 851 | }
|
| 852 | return fastfunc::ShellEncodeString(s, 0);
|
| 853 | }
|
| 854 |
|
| 855 | BigStr* ShellEncode(BigStr* s) {
|
| 856 | return fastfunc::ShellEncodeString(s, 0);
|
| 857 | }
|
| 858 |
|
| 859 | BigStr* YshEncode(BigStr* s, bool unquoted_ok) {
|
| 860 | if ((unquoted_ok and fastfunc::CanOmitQuotes(s))) {
|
| 861 | return s;
|
| 862 | }
|
| 863 | return fastfunc::ShellEncodeString(s, 1);
|
| 864 | }
|
| 865 |
|
| 866 | } // define namespace j8_lite
|
| 867 |
|