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