OILS / _devbuild / gen / pretty_asdl.py View on Github | oils.pub

336 lines, 234 significant
1from asdl import pybase
2from mycpp import mops
3from typing import Optional, List, Tuple, Dict, Any, cast, TYPE_CHECKING
4
5from asdl import runtime # For runtime.NO_SPID
6from asdl.runtime import NewRecord, NewLeaf, TraversalState
7from _devbuild.gen.hnode_asdl import color_e, hnode, hnode_e, hnode_t, Field
8
9class doc_e(object):
10 Break = 1
11 Text = 2
12 Indent = 3
13 Group = 64
14 Flat = 5
15 IfFlat = 6
16 Concat = 66
17
18_doc_str = {
19 1: 'Break',
20 2: 'Text',
21 3: 'Indent',
22 5: 'Flat',
23 6: 'IfFlat',
24 64: 'Group',
25 66: 'Concat',
26}
27
28def doc_str(tag, dot=True):
29 # type: (int, bool) -> str
30 v = _doc_str[tag]
31 if dot:
32 return "doc.%s" % v
33 else:
34 return v
35
36class doc_t(pybase.CompoundObj):
37 def tag(self):
38 # type: () -> int
39 return self._type_tag
40
41class 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, do_abbrev, trav=None):
56 # type: (bool, 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
63 out_node = NewRecord('doc.Break')
64 L = out_node.fields
65
66 x0 = NewLeaf(self.string, color_e.StringConst)
67 L.append(Field('string', x0))
68
69 return out_node
70
71 class Text(doc_t):
72 _type_tag = 2
73 __slots__ = ('string',)
74
75 def __init__(self, string):
76 # type: (str) -> None
77 self.string = string
78
79 @staticmethod
80 def CreateNull(alloc_lists=False):
81 # type: () -> doc.Text
82 return doc.Text('')
83
84 def PrettyTree(self, do_abbrev, trav=None):
85 # type: (bool, Optional[TraversalState]) -> hnode_t
86 trav = trav or TraversalState()
87 heap_id = id(self)
88 if heap_id in trav.seen:
89 return hnode.AlreadySeen(heap_id)
90 trav.seen[heap_id] = True
91
92 out_node = NewRecord('doc.Text')
93 L = out_node.fields
94
95 x0 = NewLeaf(self.string, color_e.StringConst)
96 L.append(Field('string', x0))
97
98 return out_node
99
100 class Indent(doc_t):
101 _type_tag = 3
102 __slots__ = ('indent', 'mdoc')
103
104 def __init__(self, indent, mdoc):
105 # type: (int, MeasuredDoc) -> None
106 self.indent = indent
107 self.mdoc = mdoc
108
109 @staticmethod
110 def CreateNull(alloc_lists=False):
111 # type: () -> doc.Indent
112 return doc.Indent(-1, cast('MeasuredDoc', None))
113
114 def PrettyTree(self, do_abbrev, trav=None):
115 # type: (bool, Optional[TraversalState]) -> hnode_t
116 trav = trav or TraversalState()
117 heap_id = id(self)
118 if heap_id in trav.seen:
119 return hnode.AlreadySeen(heap_id)
120 trav.seen[heap_id] = True
121
122 out_node = NewRecord('doc.Indent')
123 L = out_node.fields
124
125 x0 = hnode.Leaf(str(self.indent), color_e.OtherConst)
126 L.append(Field('indent', x0))
127
128 assert self.mdoc is not None
129 x1 = self.mdoc.PrettyTree(do_abbrev, trav=trav)
130 L.append(Field('mdoc', x1))
131
132 return out_node
133
134 class Flat(doc_t):
135 _type_tag = 5
136 __slots__ = ('mdoc',)
137
138 def __init__(self, mdoc):
139 # type: (MeasuredDoc) -> None
140 self.mdoc = mdoc
141
142 @staticmethod
143 def CreateNull(alloc_lists=False):
144 # type: () -> doc.Flat
145 return doc.Flat(cast('MeasuredDoc', None))
146
147 def PrettyTree(self, do_abbrev, trav=None):
148 # type: (bool, Optional[TraversalState]) -> hnode_t
149 trav = trav or TraversalState()
150 heap_id = id(self)
151 if heap_id in trav.seen:
152 return hnode.AlreadySeen(heap_id)
153 trav.seen[heap_id] = True
154
155 out_node = NewRecord('doc.Flat')
156 L = out_node.fields
157
158 assert self.mdoc is not None
159 x0 = self.mdoc.PrettyTree(do_abbrev, trav=trav)
160 L.append(Field('mdoc', x0))
161
162 return out_node
163
164 class IfFlat(doc_t):
165 _type_tag = 6
166 __slots__ = ('flat_mdoc', 'nonflat_mdoc')
167
168 def __init__(self, flat_mdoc, nonflat_mdoc):
169 # type: (MeasuredDoc, MeasuredDoc) -> None
170 self.flat_mdoc = flat_mdoc
171 self.nonflat_mdoc = nonflat_mdoc
172
173 @staticmethod
174 def CreateNull(alloc_lists=False):
175 # type: () -> doc.IfFlat
176 return doc.IfFlat(cast('MeasuredDoc', None), cast('MeasuredDoc', None))
177
178 def PrettyTree(self, do_abbrev, trav=None):
179 # type: (bool, Optional[TraversalState]) -> hnode_t
180 trav = trav or TraversalState()
181 heap_id = id(self)
182 if heap_id in trav.seen:
183 return hnode.AlreadySeen(heap_id)
184 trav.seen[heap_id] = True
185
186 out_node = NewRecord('doc.IfFlat')
187 L = out_node.fields
188
189 assert self.flat_mdoc is not None
190 x0 = self.flat_mdoc.PrettyTree(do_abbrev, trav=trav)
191 L.append(Field('flat_mdoc', x0))
192
193 assert self.nonflat_mdoc is not None
194 x1 = self.nonflat_mdoc.PrettyTree(do_abbrev, trav=trav)
195 L.append(Field('nonflat_mdoc', x1))
196
197 return out_node
198
199 pass
200
201class MeasuredDoc(doc_t):
202 _type_tag = 64
203 __slots__ = ('doc', 'measure')
204
205 def __init__(self, doc, measure):
206 # type: (doc_t, Measure) -> None
207 self.doc = doc
208 self.measure = measure
209
210 @staticmethod
211 def CreateNull(alloc_lists=False):
212 # type: () -> MeasuredDoc
213 return MeasuredDoc(cast('doc_t', None), cast('Measure', None))
214
215 def PrettyTree(self, do_abbrev, trav=None):
216 # type: (bool, Optional[TraversalState]) -> hnode_t
217 trav = trav or TraversalState()
218 heap_id = id(self)
219 if heap_id in trav.seen:
220 return hnode.AlreadySeen(heap_id)
221 trav.seen[heap_id] = True
222
223 out_node = NewRecord('MeasuredDoc')
224 L = out_node.fields
225
226 assert self.doc is not None
227 x0 = self.doc.PrettyTree(do_abbrev, trav=trav)
228 L.append(Field('doc', x0))
229
230 assert self.measure is not None
231 x1 = self.measure.PrettyTree(do_abbrev, trav=trav)
232 L.append(Field('measure', x1))
233
234 return out_node
235
236class Measure(pybase.CompoundObj):
237 _type_tag = 65
238 __slots__ = ('flat', 'nonflat')
239
240 def __init__(self, flat, nonflat):
241 # type: (int, int) -> None
242 self.flat = flat
243 self.nonflat = nonflat
244
245 @staticmethod
246 def CreateNull(alloc_lists=False):
247 # type: () -> Measure
248 return Measure(-1, -1)
249
250 def PrettyTree(self, do_abbrev, trav=None):
251 # type: (bool, Optional[TraversalState]) -> hnode_t
252 trav = trav or TraversalState()
253 heap_id = id(self)
254 if heap_id in trav.seen:
255 return hnode.AlreadySeen(heap_id)
256 trav.seen[heap_id] = True
257
258 out_node = NewRecord('Measure')
259 L = out_node.fields
260
261 x0 = hnode.Leaf(str(self.flat), color_e.OtherConst)
262 L.append(Field('flat', x0))
263
264 x1 = hnode.Leaf(str(self.nonflat), color_e.OtherConst)
265 L.append(Field('nonflat', x1))
266
267 return out_node
268
269class DocFragment(pybase.CompoundObj):
270 _type_tag = 67
271 __slots__ = ('mdoc', 'indent', 'is_flat', 'measure')
272
273 def __init__(self, mdoc, indent, is_flat, measure):
274 # type: (MeasuredDoc, int, bool, Measure) -> None
275 self.mdoc = mdoc
276 self.indent = indent
277 self.is_flat = is_flat
278 self.measure = measure
279
280 @staticmethod
281 def CreateNull(alloc_lists=False):
282 # type: () -> DocFragment
283 return DocFragment(cast('MeasuredDoc', None), -1, False, cast('Measure', None))
284
285 def PrettyTree(self, do_abbrev, trav=None):
286 # type: (bool, Optional[TraversalState]) -> hnode_t
287 trav = trav or TraversalState()
288 heap_id = id(self)
289 if heap_id in trav.seen:
290 return hnode.AlreadySeen(heap_id)
291 trav.seen[heap_id] = True
292
293 out_node = NewRecord('DocFragment')
294 L = out_node.fields
295
296 assert self.mdoc is not None
297 x0 = self.mdoc.PrettyTree(do_abbrev, trav=trav)
298 L.append(Field('mdoc', x0))
299
300 x1 = hnode.Leaf(str(self.indent), color_e.OtherConst)
301 L.append(Field('indent', x1))
302
303 x2 = hnode.Leaf('T' if self.is_flat else 'F', color_e.OtherConst)
304 L.append(Field('is_flat', x2))
305
306 assert self.measure is not None
307 x3 = self.measure.PrettyTree(do_abbrev, trav=trav)
308 L.append(Field('measure', x3))
309
310 return out_node
311
312class List_Measured(doc_t, List[MeasuredDoc]):
313 _type_tag = 66
314 @staticmethod
315 def New():
316 # type: () -> List_Measured
317 return List_Measured()
318
319 @staticmethod
320 def Take(plain_list):
321 # type: (List[MeasuredDoc]) -> List_Measured
322 result = List_Measured(plain_list)
323 del plain_list[:]
324 return result
325
326 def PrettyTree(self, do_abbrev, trav=None):
327 # type: (bool, Optional[TraversalState]) -> hnode_t
328 trav = trav or TraversalState()
329 heap_id = id(self)
330 if heap_id in trav.seen:
331 return hnode.AlreadySeen(heap_id)
332 trav.seen[heap_id] = True
333 h = runtime.NewRecord('List_Measured')
334 h.unnamed_fields = [c.PrettyTree(do_abbrev) for c in self]
335 return h
336