OILS / _devbuild / gen / pretty_asdl.py View on Github | oilshell.org

582 lines, 418 significant
1from asdl import pybase
2from mycpp import mops
3from typing import Optional, List, Tuple, Dict, Any, cast, TYPE_CHECKING
4
5
6from asdl import runtime # For runtime.NO_SPID
7from asdl.runtime import NewRecord, NewLeaf, TraversalState
8from _devbuild.gen.hnode_asdl import color_e, hnode, hnode_e, hnode_t, Field
9
10class doc_e(object):
11 Break = 1
12 Text = 2
13 Indent = 3
14 Concat = 4
15 Group = 5
16 IfFlat = 6
17 Flat = 7
18
19_doc_str = {
20 1: 'Break',
21 2: 'Text',
22 3: 'Indent',
23 4: 'Concat',
24 5: 'Group',
25 6: 'IfFlat',
26 7: 'Flat',
27}
28
29def doc_str(tag, dot=True):
30 # type: (int, bool) -> str
31 v = _doc_str[tag]
32 if dot:
33 return "doc.%s" % v
34 else:
35 return v
36
37class doc_t(pybase.CompoundObj):
38 def tag(self):
39 # type: () -> int
40 return self._type_tag
41 pass
42
43class doc(object):
44 class Break(doc_t):
45 _type_tag = 1
46 __slots__ = ('string',)
47
48 def __init__(self, string):
49 # type: (str) -> None
50 self.string = string
51
52 @staticmethod
53 def CreateNull(alloc_lists=False):
54 # type: () -> doc.Break
55 return doc.Break('')
56
57 def PrettyTree(self, trav=None):
58 # type: (Optional[TraversalState]) -> hnode_t
59 trav = trav or TraversalState()
60 heap_id = id(self)
61 if heap_id in trav.seen:
62 return hnode.AlreadySeen(heap_id)
63 trav.seen[heap_id] = True
64 out_node = NewRecord('doc.Break')
65 L = out_node.fields
66
67 x0 = NewLeaf(self.string, color_e.StringConst)
68 L.append(Field('string', x0))
69
70 return out_node
71
72 def _AbbreviatedTree(self, trav=None):
73 # type: (Optional[TraversalState]) -> hnode_t
74 trav = trav or TraversalState()
75 heap_id = id(self)
76 if heap_id in trav.seen:
77 return hnode.AlreadySeen(heap_id)
78 trav.seen[heap_id] = True
79 out_node = NewRecord('doc.Break')
80 L = out_node.fields
81 x0 = NewLeaf(self.string, color_e.StringConst)
82 L.append(Field('string', x0))
83
84 return out_node
85
86 def AbbreviatedTree(self, trav=None):
87 # type: (Optional[TraversalState]) -> hnode_t
88 return self._AbbreviatedTree(trav=trav)
89
90 class Text(doc_t):
91 _type_tag = 2
92 __slots__ = ('string',)
93
94 def __init__(self, string):
95 # type: (str) -> None
96 self.string = string
97
98 @staticmethod
99 def CreateNull(alloc_lists=False):
100 # type: () -> doc.Text
101 return doc.Text('')
102
103 def PrettyTree(self, trav=None):
104 # type: (Optional[TraversalState]) -> hnode_t
105 trav = trav or TraversalState()
106 heap_id = id(self)
107 if heap_id in trav.seen:
108 return hnode.AlreadySeen(heap_id)
109 trav.seen[heap_id] = True
110 out_node = NewRecord('doc.Text')
111 L = out_node.fields
112
113 x0 = NewLeaf(self.string, color_e.StringConst)
114 L.append(Field('string', x0))
115
116 return out_node
117
118 def _AbbreviatedTree(self, trav=None):
119 # type: (Optional[TraversalState]) -> hnode_t
120 trav = trav or TraversalState()
121 heap_id = id(self)
122 if heap_id in trav.seen:
123 return hnode.AlreadySeen(heap_id)
124 trav.seen[heap_id] = True
125 out_node = NewRecord('doc.Text')
126 L = out_node.fields
127 x0 = NewLeaf(self.string, color_e.StringConst)
128 L.append(Field('string', x0))
129
130 return out_node
131
132 def AbbreviatedTree(self, trav=None):
133 # type: (Optional[TraversalState]) -> hnode_t
134 return self._AbbreviatedTree(trav=trav)
135
136 class Indent(doc_t):
137 _type_tag = 3
138 __slots__ = ('indent', 'mdoc')
139
140 def __init__(self, indent, mdoc):
141 # type: (int, MeasuredDoc) -> None
142 self.indent = indent
143 self.mdoc = mdoc
144
145 @staticmethod
146 def CreateNull(alloc_lists=False):
147 # type: () -> doc.Indent
148 return doc.Indent(-1, cast(MeasuredDoc, None))
149
150 def PrettyTree(self, trav=None):
151 # type: (Optional[TraversalState]) -> hnode_t
152 trav = trav or TraversalState()
153 heap_id = id(self)
154 if heap_id in trav.seen:
155 return hnode.AlreadySeen(heap_id)
156 trav.seen[heap_id] = True
157 out_node = NewRecord('doc.Indent')
158 L = out_node.fields
159
160 x0 = hnode.Leaf(str(self.indent), color_e.OtherConst)
161 L.append(Field('indent', x0))
162
163 assert self.mdoc is not None
164 x1 = self.mdoc.PrettyTree(trav=trav)
165 L.append(Field('mdoc', x1))
166
167 return out_node
168
169 def _AbbreviatedTree(self, trav=None):
170 # type: (Optional[TraversalState]) -> hnode_t
171 trav = trav or TraversalState()
172 heap_id = id(self)
173 if heap_id in trav.seen:
174 return hnode.AlreadySeen(heap_id)
175 trav.seen[heap_id] = True
176 out_node = NewRecord('doc.Indent')
177 L = out_node.fields
178 x0 = hnode.Leaf(str(self.indent), color_e.OtherConst)
179 L.append(Field('indent', x0))
180
181 assert self.mdoc is not None
182 x1 = self.mdoc.AbbreviatedTree(trav=trav)
183 L.append(Field('mdoc', x1))
184
185 return out_node
186
187 def AbbreviatedTree(self, trav=None):
188 # type: (Optional[TraversalState]) -> hnode_t
189 return self._AbbreviatedTree(trav=trav)
190
191 class Concat(doc_t):
192 _type_tag = 4
193 __slots__ = ('mdocs',)
194
195 def __init__(self, mdocs):
196 # type: (List[MeasuredDoc]) -> None
197 self.mdocs = mdocs
198
199 @staticmethod
200 def CreateNull(alloc_lists=False):
201 # type: () -> doc.Concat
202 return doc.Concat([] if alloc_lists else cast('List[MeasuredDoc]', None))
203
204 def PrettyTree(self, trav=None):
205 # type: (Optional[TraversalState]) -> hnode_t
206 trav = trav or TraversalState()
207 heap_id = id(self)
208 if heap_id in trav.seen:
209 return hnode.AlreadySeen(heap_id)
210 trav.seen[heap_id] = True
211 out_node = NewRecord('doc.Concat')
212 L = out_node.fields
213
214 if self.mdocs is not None: # List
215 x0 = hnode.Array([])
216 for i0 in self.mdocs:
217 h = (hnode.Leaf("_", color_e.OtherConst) if i0 is None else
218 i0.PrettyTree(trav=trav))
219 x0.children.append(h)
220 L.append(Field('mdocs', x0))
221
222 return out_node
223
224 def _AbbreviatedTree(self, trav=None):
225 # type: (Optional[TraversalState]) -> hnode_t
226 trav = trav or TraversalState()
227 heap_id = id(self)
228 if heap_id in trav.seen:
229 return hnode.AlreadySeen(heap_id)
230 trav.seen[heap_id] = True
231 out_node = NewRecord('doc.Concat')
232 L = out_node.fields
233 if self.mdocs is not None: # List
234 x0 = hnode.Array([])
235 for i0 in self.mdocs:
236 h = (hnode.Leaf("_", color_e.OtherConst) if i0 is None else
237 i0.AbbreviatedTree(trav=trav))
238 x0.children.append(h)
239 L.append(Field('mdocs', x0))
240
241 return out_node
242
243 def AbbreviatedTree(self, trav=None):
244 # type: (Optional[TraversalState]) -> hnode_t
245 return self._AbbreviatedTree(trav=trav)
246
247 class Group(doc_t):
248 _type_tag = 5
249 __slots__ = ('mdoc',)
250
251 def __init__(self, mdoc):
252 # type: (MeasuredDoc) -> None
253 self.mdoc = mdoc
254
255 @staticmethod
256 def CreateNull(alloc_lists=False):
257 # type: () -> doc.Group
258 return doc.Group(cast(MeasuredDoc, None))
259
260 def PrettyTree(self, trav=None):
261 # type: (Optional[TraversalState]) -> hnode_t
262 trav = trav or TraversalState()
263 heap_id = id(self)
264 if heap_id in trav.seen:
265 return hnode.AlreadySeen(heap_id)
266 trav.seen[heap_id] = True
267 out_node = NewRecord('doc.Group')
268 L = out_node.fields
269
270 assert self.mdoc is not None
271 x0 = self.mdoc.PrettyTree(trav=trav)
272 L.append(Field('mdoc', x0))
273
274 return out_node
275
276 def _AbbreviatedTree(self, trav=None):
277 # type: (Optional[TraversalState]) -> hnode_t
278 trav = trav or TraversalState()
279 heap_id = id(self)
280 if heap_id in trav.seen:
281 return hnode.AlreadySeen(heap_id)
282 trav.seen[heap_id] = True
283 out_node = NewRecord('doc.Group')
284 L = out_node.fields
285 assert self.mdoc is not None
286 x0 = self.mdoc.AbbreviatedTree(trav=trav)
287 L.append(Field('mdoc', x0))
288
289 return out_node
290
291 def AbbreviatedTree(self, trav=None):
292 # type: (Optional[TraversalState]) -> hnode_t
293 return self._AbbreviatedTree(trav=trav)
294
295 class IfFlat(doc_t):
296 _type_tag = 6
297 __slots__ = ('flat_mdoc', 'nonflat_mdoc')
298
299 def __init__(self, flat_mdoc, nonflat_mdoc):
300 # type: (MeasuredDoc, MeasuredDoc) -> None
301 self.flat_mdoc = flat_mdoc
302 self.nonflat_mdoc = nonflat_mdoc
303
304 @staticmethod
305 def CreateNull(alloc_lists=False):
306 # type: () -> doc.IfFlat
307 return doc.IfFlat(cast(MeasuredDoc, None), cast(MeasuredDoc, None))
308
309 def PrettyTree(self, trav=None):
310 # type: (Optional[TraversalState]) -> hnode_t
311 trav = trav or TraversalState()
312 heap_id = id(self)
313 if heap_id in trav.seen:
314 return hnode.AlreadySeen(heap_id)
315 trav.seen[heap_id] = True
316 out_node = NewRecord('doc.IfFlat')
317 L = out_node.fields
318
319 assert self.flat_mdoc is not None
320 x0 = self.flat_mdoc.PrettyTree(trav=trav)
321 L.append(Field('flat_mdoc', x0))
322
323 assert self.nonflat_mdoc is not None
324 x1 = self.nonflat_mdoc.PrettyTree(trav=trav)
325 L.append(Field('nonflat_mdoc', x1))
326
327 return out_node
328
329 def _AbbreviatedTree(self, trav=None):
330 # type: (Optional[TraversalState]) -> hnode_t
331 trav = trav or TraversalState()
332 heap_id = id(self)
333 if heap_id in trav.seen:
334 return hnode.AlreadySeen(heap_id)
335 trav.seen[heap_id] = True
336 out_node = NewRecord('doc.IfFlat')
337 L = out_node.fields
338 assert self.flat_mdoc is not None
339 x0 = self.flat_mdoc.AbbreviatedTree(trav=trav)
340 L.append(Field('flat_mdoc', x0))
341
342 assert self.nonflat_mdoc is not None
343 x1 = self.nonflat_mdoc.AbbreviatedTree(trav=trav)
344 L.append(Field('nonflat_mdoc', x1))
345
346 return out_node
347
348 def AbbreviatedTree(self, trav=None):
349 # type: (Optional[TraversalState]) -> hnode_t
350 return self._AbbreviatedTree(trav=trav)
351
352 class Flat(doc_t):
353 _type_tag = 7
354 __slots__ = ('mdoc',)
355
356 def __init__(self, mdoc):
357 # type: (MeasuredDoc) -> None
358 self.mdoc = mdoc
359
360 @staticmethod
361 def CreateNull(alloc_lists=False):
362 # type: () -> doc.Flat
363 return doc.Flat(cast(MeasuredDoc, None))
364
365 def PrettyTree(self, trav=None):
366 # type: (Optional[TraversalState]) -> hnode_t
367 trav = trav or TraversalState()
368 heap_id = id(self)
369 if heap_id in trav.seen:
370 return hnode.AlreadySeen(heap_id)
371 trav.seen[heap_id] = True
372 out_node = NewRecord('doc.Flat')
373 L = out_node.fields
374
375 assert self.mdoc is not None
376 x0 = self.mdoc.PrettyTree(trav=trav)
377 L.append(Field('mdoc', x0))
378
379 return out_node
380
381 def _AbbreviatedTree(self, trav=None):
382 # type: (Optional[TraversalState]) -> hnode_t
383 trav = trav or TraversalState()
384 heap_id = id(self)
385 if heap_id in trav.seen:
386 return hnode.AlreadySeen(heap_id)
387 trav.seen[heap_id] = True
388 out_node = NewRecord('doc.Flat')
389 L = out_node.fields
390 assert self.mdoc is not None
391 x0 = self.mdoc.AbbreviatedTree(trav=trav)
392 L.append(Field('mdoc', x0))
393
394 return out_node
395
396 def AbbreviatedTree(self, trav=None):
397 # type: (Optional[TraversalState]) -> hnode_t
398 return self._AbbreviatedTree(trav=trav)
399
400 pass
401
402class MeasuredDoc(pybase.CompoundObj):
403 _type_tag = 64
404 __slots__ = ('doc', 'measure')
405
406 def __init__(self, doc, measure):
407 # type: (doc_t, Measure) -> None
408 self.doc = doc
409 self.measure = measure
410
411 @staticmethod
412 def CreateNull(alloc_lists=False):
413 # type: () -> MeasuredDoc
414 return MeasuredDoc(cast(doc_t, None), cast(Measure, None))
415
416 def PrettyTree(self, trav=None):
417 # type: (Optional[TraversalState]) -> hnode_t
418 trav = trav or TraversalState()
419 heap_id = id(self)
420 if heap_id in trav.seen:
421 return hnode.AlreadySeen(heap_id)
422 trav.seen[heap_id] = True
423 out_node = NewRecord('MeasuredDoc')
424 L = out_node.fields
425
426 assert self.doc is not None
427 x0 = self.doc.PrettyTree(trav=trav)
428 L.append(Field('doc', x0))
429
430 assert self.measure is not None
431 x1 = self.measure.PrettyTree(trav=trav)
432 L.append(Field('measure', x1))
433
434 return out_node
435
436 def _AbbreviatedTree(self, trav=None):
437 # type: (Optional[TraversalState]) -> hnode_t
438 trav = trav or TraversalState()
439 heap_id = id(self)
440 if heap_id in trav.seen:
441 return hnode.AlreadySeen(heap_id)
442 trav.seen[heap_id] = True
443 out_node = NewRecord('MeasuredDoc')
444 L = out_node.fields
445 assert self.doc is not None
446 x0 = self.doc.AbbreviatedTree(trav=trav)
447 L.append(Field('doc', x0))
448
449 assert self.measure is not None
450 x1 = self.measure.AbbreviatedTree(trav=trav)
451 L.append(Field('measure', x1))
452
453 return out_node
454
455 def AbbreviatedTree(self, trav=None):
456 # type: (Optional[TraversalState]) -> hnode_t
457 return self._AbbreviatedTree(trav=trav)
458
459class Measure(pybase.CompoundObj):
460 _type_tag = 65
461 __slots__ = ('flat', 'nonflat')
462
463 def __init__(self, flat, nonflat):
464 # type: (int, int) -> None
465 self.flat = flat
466 self.nonflat = nonflat
467
468 @staticmethod
469 def CreateNull(alloc_lists=False):
470 # type: () -> Measure
471 return Measure(-1, -1)
472
473 def PrettyTree(self, trav=None):
474 # type: (Optional[TraversalState]) -> hnode_t
475 trav = trav or TraversalState()
476 heap_id = id(self)
477 if heap_id in trav.seen:
478 return hnode.AlreadySeen(heap_id)
479 trav.seen[heap_id] = True
480 out_node = NewRecord('Measure')
481 L = out_node.fields
482
483 x0 = hnode.Leaf(str(self.flat), color_e.OtherConst)
484 L.append(Field('flat', x0))
485
486 x1 = hnode.Leaf(str(self.nonflat), color_e.OtherConst)
487 L.append(Field('nonflat', x1))
488
489 return out_node
490
491 def _AbbreviatedTree(self, trav=None):
492 # type: (Optional[TraversalState]) -> hnode_t
493 trav = trav or TraversalState()
494 heap_id = id(self)
495 if heap_id in trav.seen:
496 return hnode.AlreadySeen(heap_id)
497 trav.seen[heap_id] = True
498 out_node = NewRecord('Measure')
499 L = out_node.fields
500 x0 = hnode.Leaf(str(self.flat), color_e.OtherConst)
501 L.append(Field('flat', x0))
502
503 x1 = hnode.Leaf(str(self.nonflat), color_e.OtherConst)
504 L.append(Field('nonflat', x1))
505
506 return out_node
507
508 def AbbreviatedTree(self, trav=None):
509 # type: (Optional[TraversalState]) -> hnode_t
510 return self._AbbreviatedTree(trav=trav)
511
512class DocFragment(pybase.CompoundObj):
513 _type_tag = 66
514 __slots__ = ('mdoc', 'indent', 'is_flat', 'measure')
515
516 def __init__(self, mdoc, indent, is_flat, measure):
517 # type: (MeasuredDoc, int, bool, Measure) -> None
518 self.mdoc = mdoc
519 self.indent = indent
520 self.is_flat = is_flat
521 self.measure = measure
522
523 @staticmethod
524 def CreateNull(alloc_lists=False):
525 # type: () -> DocFragment
526 return DocFragment(cast(MeasuredDoc, None), -1, False, cast(Measure, None))
527
528 def PrettyTree(self, trav=None):
529 # type: (Optional[TraversalState]) -> hnode_t
530 trav = trav or TraversalState()
531 heap_id = id(self)
532 if heap_id in trav.seen:
533 return hnode.AlreadySeen(heap_id)
534 trav.seen[heap_id] = True
535 out_node = NewRecord('DocFragment')
536 L = out_node.fields
537
538 assert self.mdoc is not None
539 x0 = self.mdoc.PrettyTree(trav=trav)
540 L.append(Field('mdoc', x0))
541
542 x1 = hnode.Leaf(str(self.indent), color_e.OtherConst)
543 L.append(Field('indent', x1))
544
545 x2 = hnode.Leaf('T' if self.is_flat else 'F', color_e.OtherConst)
546 L.append(Field('is_flat', x2))
547
548 assert self.measure is not None
549 x3 = self.measure.PrettyTree(trav=trav)
550 L.append(Field('measure', x3))
551
552 return out_node
553
554 def _AbbreviatedTree(self, trav=None):
555 # type: (Optional[TraversalState]) -> hnode_t
556 trav = trav or TraversalState()
557 heap_id = id(self)
558 if heap_id in trav.seen:
559 return hnode.AlreadySeen(heap_id)
560 trav.seen[heap_id] = True
561 out_node = NewRecord('DocFragment')
562 L = out_node.fields
563 assert self.mdoc is not None
564 x0 = self.mdoc.AbbreviatedTree(trav=trav)
565 L.append(Field('mdoc', x0))
566
567 x1 = hnode.Leaf(str(self.indent), color_e.OtherConst)
568 L.append(Field('indent', x1))
569
570 x2 = hnode.Leaf('T' if self.is_flat else 'F', color_e.OtherConst)
571 L.append(Field('is_flat', x2))
572
573 assert self.measure is not None
574 x3 = self.measure.AbbreviatedTree(trav=trav)
575 L.append(Field('measure', x3))
576
577 return out_node
578
579 def AbbreviatedTree(self, trav=None):
580 # type: (Optional[TraversalState]) -> hnode_t
581 return self._AbbreviatedTree(trav=trav)
582