add Lines

This commit is contained in:
leaf corcoran 2012-10-29 22:15:06 -07:00
parent 6d52237380
commit 05d83f399d
4 changed files with 135 additions and 93 deletions

View File

@ -19,7 +19,75 @@ end
local concat, insert = table.concat, table.insert local concat, insert = table.concat, table.insert
local pos_to_line, get_line, get_closest_line, trim = util.pos_to_line, util.get_line, util.get_closest_line, util.trim local pos_to_line, get_line, get_closest_line, trim = util.pos_to_line, util.get_line, util.get_closest_line, util.trim
local mtype = util.moon.type local mtype = util.moon.type
local Line local Line, Lines
Lines = (function()
local _parent_0 = nil
local _base_0 = {
add = function(self, item)
local _exp_0 = mtype(item)
if Line == _exp_0 then
item:render(self)
elseif Block == _exp_0 then
item:render(self)
else
self[#self + 1] = item
end
return self
end,
__tostring = function(self)
local strip
strip = function(t)
if "table" == type(t) then
return (function()
local _accum_0 = { }
local _len_0 = 0
local _list_0 = t
for _index_0 = 1, #_list_0 do
local v = _list_0[_index_0]
_len_0 = _len_0 + 1
_accum_0[_len_0] = strip(v)
end
return _accum_0
end)()
else
return t
end
end
return "Lines<" .. tostring(util.dump(strip(self)):sub(1, -2)) .. ">"
end
}
_base_0.__index = _base_0
if _parent_0 then
setmetatable(_base_0, _parent_0.__base)
end
local _class_0 = setmetatable({
__init = function(self)
self.posmap = { }
end,
__base = _base_0,
__name = "Lines",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil and _parent_0 then
return _parent_0[name]
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
return _class_0
end)()
Line = (function() Line = (function()
local _parent_0 = nil local _parent_0 = nil
local _base_0 = { local _base_0 = {
@ -57,21 +125,21 @@ Line = (function()
local current = { } local current = { }
local add_current local add_current
add_current = function() add_current = function()
return insert(buffer, concat(current)) return buffer:add(concat(current))
end end
local _list_0 = self local _list_0 = self
for _index_0 = 1, #_list_0 do for _index_0 = 1, #_list_0 do
local chunk = _list_0[_index_0] local chunk = _list_0[_index_0]
local _exp_0 = mtype(chunk) local _exp_0 = mtype(chunk)
if Block == _exp_0 then if Block == _exp_0 then
local _list_1 = chunk:render({ }) local _list_1 = chunk:render(Lines())
for _index_1 = 1, #_list_1 do for _index_1 = 1, #_list_1 do
local block_chunk = _list_1[_index_1] local block_chunk = _list_1[_index_1]
if "string" == type(block_chunk) then if "string" == type(block_chunk) then
insert(current, block_chunk) insert(current, block_chunk)
else else
add_current() add_current()
insert(buffer, block_chunk) buffer:add(block_chunk)
current = { } current = { }
end end
end end
@ -85,7 +153,7 @@ Line = (function()
return buffer return buffer
end, end,
__tostring = function(self) __tostring = function(self)
return "Line<" .. tostring(self:render()) .. ">" return "Line<" .. tostring(util.dump(self):sub(1, -2)) .. ">"
end end
} }
_base_0.__index = _base_0 _base_0.__index = _base_0
@ -123,7 +191,7 @@ Line = (function()
return _class_0 return _class_0
end)() end)()
Block = (function() Block = (function()
local add_to_buffer, block_iterator local block_iterator
local _parent_0 = nil local _parent_0 = nil
local _base_0 = { local _base_0 = {
header = "do", header = "do",
@ -275,44 +343,21 @@ Block = (function()
print("appending pos", self) print("appending pos", self)
self._posmap[#self._posmap + 1] = map self._posmap[#self._posmap + 1] = map
end, end,
add_raw = function(self, item) add = function(self, item)
return insert(self._lines, item) self._lines:add(item)
end, return item
add = function(self, line)
local _exp_0 = util.moon.type(line)
if "string" == _exp_0 then
insert(self._lines, line)
elseif Block == _exp_0 then
line:render(self._lines)
elseif Line == _exp_0 then
line:render(self._lines)
else
error("Adding unknown item")
end
return line
end, end,
render = function(self, buffer) render = function(self, buffer)
add_to_buffer(buffer, self.header) buffer:add(self.header)
local lines = (function()
local _accum_0 = { }
local _len_0 = 0
local _list_0 = self._lines
for _index_0 = 1, #_list_0 do
local l = _list_0[_index_0]
_len_0 = _len_0 + 1
_accum_0[_len_0] = l
end
return _accum_0
end)()
if self.next then if self.next then
insert(buffer, lines) buffer:add(self._lines)
self.next:render(buffer) self.next:render(buffer)
else else
if #lines == 0 and "string" == type(buffer[#buffer]) then if #self._lines == 0 and "string" == type(buffer[#buffer]) then
buffer[#buffer] = buffer[#buffer] .. (" " .. (unpack(add_to_buffer({ }, self.footer)))) buffer[#buffer] = buffer[#buffer] .. (" " .. (unpack(Lines():add(self.footer))))
else else
insert(buffer, lines) buffer:add(self._lines)
add_to_buffer(buffer, self.footer) buffer:add(self.footer)
end end
end end
return buffer return buffer
@ -438,7 +483,7 @@ Block = (function()
"lines", "lines",
self._lines self._lines
} }
self._lines = { } self._lines = Lines()
return self:stms(fn(lines)) return self:stms(fn(lines))
end end
} }
@ -449,7 +494,7 @@ Block = (function()
local _class_0 = setmetatable({ local _class_0 = setmetatable({
__init = function(self, parent, header, footer) __init = function(self, parent, header, footer)
self.parent, self.header, self.footer = parent, header, footer self.parent, self.header, self.footer = parent, header, footer
self._lines = { } self._lines = Lines()
self._posmap = { } self._posmap = { }
self._names = { } self._names = { }
self._state = { } self._state = { }
@ -494,15 +539,6 @@ Block = (function()
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
local self = _class_0 local self = _class_0
add_to_buffer = function(buffer, line)
local _exp_0 = mtype(line)
if Line == _exp_0 then
line:render(buffer)
else
insert(buffer, line)
end
return buffer
end
block_iterator = function(list) block_iterator = function(list)
return coroutine.wrap(function() return coroutine.wrap(function()
local _list_0 = list local _list_0 = list

View File

@ -21,7 +21,34 @@ mtype = util.moon.type
export tree, value, format_error export tree, value, format_error
export Block, RootBlock export Block, RootBlock
-- buffer for building up a line local Line, Lines
-- a buffer for building up lines
class Lines
new: =>
@posmap = {}
-- append a line or lines to the buffer
add: (item) =>
switch mtype item
when Line
item\render self
when Block
item\render self
else
@[#@ + 1] = item
@
__tostring: =>
-- strip non-array elements
strip = (t) ->
if "table" == type t
[strip v for v in *t]
else
t
-- copy with only array elements
"Lines<#{util.dump(strip @)\sub 1, -2}>"
class Line class Line
_append_single: (item) => _append_single: (item) =>
if util.moon.type(item) == Line if util.moon.type(item) == Line
@ -39,22 +66,22 @@ class Line
@_append_single item for item in *{...} @_append_single item for item in *{...}
nil nil
-- todo: remove concats from here -- todo: try to remove concats from here
render: (buffer) => render: (buffer) =>
current = {} current = {}
add_current = -> add_current = ->
insert buffer, concat current buffer\add concat current
for chunk in *@ for chunk in *@
switch mtype chunk switch mtype chunk
when Block when Block
for block_chunk in *chunk\render{} for block_chunk in *chunk\render Lines!
if "string" == type block_chunk if "string" == type block_chunk
insert current, block_chunk insert current, block_chunk
else else
add_current! add_current!
insert buffer, block_chunk buffer\add block_chunk
current = {} current = {}
else else
insert current, chunk insert current, chunk
@ -64,7 +91,8 @@ class Line
buffer buffer
__tostring: => "Line<#{@render!}>" __tostring: =>
"Line<#{util.dump(@)\sub 1, -2}>"
class Block class Block
header: "do" header: "do"
@ -82,8 +110,9 @@ class Block
"Block<#{h}> <- " .. tostring @parent "Block<#{h}> <- " .. tostring @parent
new: (@parent, @header, @footer) => new: (@parent, @header, @footer) =>
@_lines = {} @_lines = Lines!
@_posmap = {}
@_posmap = {} -- todo: kill me
@_names = {} @_names = {}
@_state = {} @_state = {}
@_listeners = {} @_listeners = {}
@ -186,10 +215,6 @@ class Block
print "appending pos", self print "appending pos", self
@_posmap[#@_posmap + 1] = map @_posmap[#@_posmap + 1] = map
-- add raw text as new line
add_raw: (item) =>
insert @_lines, item
-- append_line_table: (sub_table, offset) => -- append_line_table: (sub_table, offset) =>
-- offset = offset + @current_line -- offset = offset + @current_line
@ -211,44 +236,24 @@ class Block
-- current = current.next -- current = current.next
-- add a line object -- add a line object
add: (line) => add: (item) =>
-- print "adding", line @_lines\add item
switch util.moon.type line item
when "string"
insert @_lines, line
when Block
line\render @_lines
when Line
line\render @_lines
else
error "Adding unknown item"
line
add_to_buffer = (buffer, line) ->
switch mtype line
when Line
line\render buffer
else
insert buffer, line
buffer
-- todo: pass in buffer as argument -- todo: pass in buffer as argument
render: (buffer) => render: (buffer) =>
add_to_buffer buffer, @header buffer\add @header
-- copy lines
lines = [l for l in *@_lines]
if @next if @next
insert buffer, lines buffer\add @_lines
@next\render buffer @next\render buffer
else else
-- join an empty block into a single line -- join an empty block into a single line
if #lines == 0 and "string" == type buffer[#buffer] if #@_lines == 0 and "string" == type buffer[#buffer]
buffer[#buffer] ..= " " .. (unpack add_to_buffer {}, @footer) buffer[#buffer] ..= " " .. (unpack Lines!\add @footer)
else else
insert buffer, lines buffer\add @_lines
add_to_buffer buffer, @footer buffer\add @footer
buffer buffer
@ -330,9 +335,10 @@ class Block
splice: (fn) => splice: (fn) =>
lines = {"lines", @_lines} lines = {"lines", @_lines}
@_lines = {} @_lines = Lines!
@stms fn lines @stms fn lines
-- move this into Lines
flatten_lines = (lines, indent=nil, buffer={}) -> flatten_lines = (lines, indent=nil, buffer={}) ->
for i = 1, #lines for i = 1, #lines
l = lines[i] l = lines[i]

View File

@ -18,7 +18,7 @@ line_compile = {
local _list_0 = node[2] local _list_0 = node[2]
for _index_0 = 1, #_list_0 do for _index_0 = 1, #_list_0 do
local line = _list_0[_index_0] local line = _list_0[_index_0]
self:add_raw(line) self:add(line)
end end
end, end,
declare = function(self, node) declare = function(self, node)

View File

@ -17,7 +17,7 @@ line_compile =
lines: (node) => lines: (node) =>
for line in *node[2] for line in *node[2]
@add_raw line @add line
declare: (node) => declare: (node) =>
names = node[2] names = node[2]