1919local concat , insert = table.concat , table.insert
2020local pos_to_line , get_line , get_closest_line , trim = util .pos_to_line , util .get_line , util .get_closest_line , util .trim
2121local mtype = util .moon .type
22- local Line
22+ local Line , Lines
23+ Lines = (function ()
24+ local _parent_0 = nil
25+ local _base_0 = {
26+ add = function (self , item )
27+ local _exp_0 = mtype (item )
28+ if Line == _exp_0 then
29+ item :render (self )
30+ elseif Block == _exp_0 then
31+ item :render (self )
32+ else
33+ self [# self + 1 ] = item
34+ end
35+ return self
36+ end ,
37+ __tostring = function (self )
38+ local strip
39+ strip = function (t )
40+ if " table" == type (t ) then
41+ return (function ()
42+ local _accum_0 = { }
43+ local _len_0 = 0
44+ local _list_0 = t
45+ for _index_0 = 1 , # _list_0 do
46+ local v = _list_0 [_index_0 ]
47+ _len_0 = _len_0 + 1
48+ _accum_0 [_len_0 ] = strip (v )
49+ end
50+ return _accum_0
51+ end )()
52+ else
53+ return t
54+ end
55+ end
56+ return " Lines<" .. tostring (util .dump (strip (self )):sub (1 , - 2 )) .. " >"
57+ end
58+ }
59+ _base_0 .__index = _base_0
60+ if _parent_0 then
61+ setmetatable (_base_0 , _parent_0 .__base )
62+ end
63+ local _class_0 = setmetatable ({
64+ __init = function (self )
65+ self .posmap = { }
66+ end ,
67+ __base = _base_0 ,
68+ __name = " Lines" ,
69+ __parent = _parent_0
70+ }, {
71+ __index = function (cls , name )
72+ local val = rawget (_base_0 , name )
73+ if val == nil and _parent_0 then
74+ return _parent_0 [name ]
75+ else
76+ return val
77+ end
78+ end ,
79+ __call = function (cls , ...)
80+ local _self_0 = setmetatable ({}, _base_0 )
81+ cls .__init (_self_0 , ... )
82+ return _self_0
83+ end
84+ })
85+ _base_0 .__class = _class_0
86+ if _parent_0 and _parent_0 .__inherited then
87+ _parent_0 .__inherited (_parent_0 , _class_0 )
88+ end
89+ return _class_0
90+ end )()
2391Line = (function ()
2492 local _parent_0 = nil
2593 local _base_0 = {
@@ -57,21 +125,21 @@ Line = (function()
57125 local current = { }
58126 local add_current
59127 add_current = function ()
60- return insert ( buffer , concat (current ))
128+ return buffer : add ( concat (current ))
61129 end
62130 local _list_0 = self
63131 for _index_0 = 1 , # _list_0 do
64132 local chunk = _list_0 [_index_0 ]
65133 local _exp_0 = mtype (chunk )
66134 if Block == _exp_0 then
67- local _list_1 = chunk :render ({ } )
135+ local _list_1 = chunk :render (Lines () )
68136 for _index_1 = 1 , # _list_1 do
69137 local block_chunk = _list_1 [_index_1 ]
70138 if " string" == type (block_chunk ) then
71139 insert (current , block_chunk )
72140 else
73141 add_current ()
74- insert ( buffer , block_chunk )
142+ buffer : add ( block_chunk )
75143 current = { }
76144 end
77145 end
@@ -85,7 +153,7 @@ Line = (function()
85153 return buffer
86154 end ,
87155 __tostring = function (self )
88- return " Line<" .. tostring (self : render ( )) .. " >"
156+ return " Line<" .. tostring (util . dump ( self ): sub ( 1 , - 2 )) .. " >"
89157 end
90158 }
91159 _base_0 .__index = _base_0
@@ -123,7 +191,7 @@ Line = (function()
123191 return _class_0
124192end )()
125193Block = (function ()
126- local add_to_buffer , block_iterator
194+ local block_iterator
127195 local _parent_0 = nil
128196 local _base_0 = {
129197 header = " do" ,
@@ -275,44 +343,21 @@ Block = (function()
275343 print (" appending pos" , self )
276344 self ._posmap [# self ._posmap + 1 ] = map
277345 end ,
278- add_raw = function (self , item )
279- return insert (self ._lines , item )
280- end ,
281- add = function (self , line )
282- local _exp_0 = util .moon .type (line )
283- if " string" == _exp_0 then
284- insert (self ._lines , line )
285- elseif Block == _exp_0 then
286- line :render (self ._lines )
287- elseif Line == _exp_0 then
288- line :render (self ._lines )
289- else
290- error (" Adding unknown item" )
291- end
292- return line
346+ add = function (self , item )
347+ self ._lines :add (item )
348+ return item
293349 end ,
294350 render = function (self , buffer )
295- add_to_buffer (buffer , self .header )
296- local lines = (function ()
297- local _accum_0 = { }
298- local _len_0 = 0
299- local _list_0 = self ._lines
300- for _index_0 = 1 , # _list_0 do
301- local l = _list_0 [_index_0 ]
302- _len_0 = _len_0 + 1
303- _accum_0 [_len_0 ] = l
304- end
305- return _accum_0
306- end )()
351+ buffer :add (self .header )
307352 if self .next then
308- insert ( buffer , lines )
353+ buffer : add ( self . _lines )
309354 self .next :render (buffer )
310355 else
311- if # lines == 0 and " string" == type (buffer [# buffer ]) then
312- buffer [# buffer ] = buffer [# buffer ] .. (" " .. (unpack (add_to_buffer ({ }, self .footer ))))
356+ if # self . _lines == 0 and " string" == type (buffer [# buffer ]) then
357+ buffer [# buffer ] = buffer [# buffer ] .. (" " .. (unpack (Lines (): add ( self .footer ))))
313358 else
314- insert ( buffer , lines )
315- add_to_buffer ( buffer , self .footer )
359+ buffer : add ( self . _lines )
360+ buffer : add ( self .footer )
316361 end
317362 end
318363 return buffer
@@ -438,7 +483,7 @@ Block = (function()
438483 " lines" ,
439484 self ._lines
440485 }
441- self ._lines = { }
486+ self ._lines = Lines ()
442487 return self :stms (fn (lines ))
443488 end
444489 }
@@ -449,7 +494,7 @@ Block = (function()
449494 local _class_0 = setmetatable ({
450495 __init = function (self , parent , header , footer )
451496 self .parent , self .header , self .footer = parent , header , footer
452- self ._lines = { }
497+ self ._lines = Lines ()
453498 self ._posmap = { }
454499 self ._names = { }
455500 self ._state = { }
@@ -494,15 +539,6 @@ Block = (function()
494539 })
495540 _base_0 .__class = _class_0
496541 local self = _class_0
497- add_to_buffer = function (buffer , line )
498- local _exp_0 = mtype (line )
499- if Line == _exp_0 then
500- line :render (buffer )
501- else
502- insert (buffer , line )
503- end
504- return buffer
505- end
506542 block_iterator = function (list )
507543 return coroutine.wrap (function ()
508544 local _list_0 = list
0 commit comments