mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
add Lines
This commit is contained in:
parent
6d52237380
commit
05d83f399d
@ -19,7 +19,75 @@ end
|
||||
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 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()
|
||||
local _parent_0 = nil
|
||||
local _base_0 = {
|
||||
@ -57,21 +125,21 @@ Line = (function()
|
||||
local current = { }
|
||||
local add_current
|
||||
add_current = function()
|
||||
return insert(buffer, concat(current))
|
||||
return buffer:add(concat(current))
|
||||
end
|
||||
local _list_0 = self
|
||||
for _index_0 = 1, #_list_0 do
|
||||
local chunk = _list_0[_index_0]
|
||||
local _exp_0 = mtype(chunk)
|
||||
if Block == _exp_0 then
|
||||
local _list_1 = chunk:render({ })
|
||||
local _list_1 = chunk:render(Lines())
|
||||
for _index_1 = 1, #_list_1 do
|
||||
local block_chunk = _list_1[_index_1]
|
||||
if "string" == type(block_chunk) then
|
||||
insert(current, block_chunk)
|
||||
else
|
||||
add_current()
|
||||
insert(buffer, block_chunk)
|
||||
buffer:add(block_chunk)
|
||||
current = { }
|
||||
end
|
||||
end
|
||||
@ -85,7 +153,7 @@ Line = (function()
|
||||
return buffer
|
||||
end,
|
||||
__tostring = function(self)
|
||||
return "Line<" .. tostring(self:render()) .. ">"
|
||||
return "Line<" .. tostring(util.dump(self):sub(1, -2)) .. ">"
|
||||
end
|
||||
}
|
||||
_base_0.__index = _base_0
|
||||
@ -123,7 +191,7 @@ Line = (function()
|
||||
return _class_0
|
||||
end)()
|
||||
Block = (function()
|
||||
local add_to_buffer, block_iterator
|
||||
local block_iterator
|
||||
local _parent_0 = nil
|
||||
local _base_0 = {
|
||||
header = "do",
|
||||
@ -275,44 +343,21 @@ Block = (function()
|
||||
print("appending pos", self)
|
||||
self._posmap[#self._posmap + 1] = map
|
||||
end,
|
||||
add_raw = function(self, item)
|
||||
return insert(self._lines, item)
|
||||
end,
|
||||
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
|
||||
add = function(self, item)
|
||||
self._lines:add(item)
|
||||
return item
|
||||
end,
|
||||
render = function(self, buffer)
|
||||
add_to_buffer(buffer, 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)()
|
||||
buffer:add(self.header)
|
||||
if self.next then
|
||||
insert(buffer, lines)
|
||||
buffer:add(self._lines)
|
||||
self.next:render(buffer)
|
||||
else
|
||||
if #lines == 0 and "string" == type(buffer[#buffer]) then
|
||||
buffer[#buffer] = buffer[#buffer] .. (" " .. (unpack(add_to_buffer({ }, self.footer))))
|
||||
if #self._lines == 0 and "string" == type(buffer[#buffer]) then
|
||||
buffer[#buffer] = buffer[#buffer] .. (" " .. (unpack(Lines():add(self.footer))))
|
||||
else
|
||||
insert(buffer, lines)
|
||||
add_to_buffer(buffer, self.footer)
|
||||
buffer:add(self._lines)
|
||||
buffer:add(self.footer)
|
||||
end
|
||||
end
|
||||
return buffer
|
||||
@ -438,7 +483,7 @@ Block = (function()
|
||||
"lines",
|
||||
self._lines
|
||||
}
|
||||
self._lines = { }
|
||||
self._lines = Lines()
|
||||
return self:stms(fn(lines))
|
||||
end
|
||||
}
|
||||
@ -449,7 +494,7 @@ Block = (function()
|
||||
local _class_0 = setmetatable({
|
||||
__init = function(self, parent, header, footer)
|
||||
self.parent, self.header, self.footer = parent, header, footer
|
||||
self._lines = { }
|
||||
self._lines = Lines()
|
||||
self._posmap = { }
|
||||
self._names = { }
|
||||
self._state = { }
|
||||
@ -494,15 +539,6 @@ Block = (function()
|
||||
})
|
||||
_base_0.__class = _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)
|
||||
return coroutine.wrap(function()
|
||||
local _list_0 = list
|
||||
|
@ -21,7 +21,34 @@ mtype = util.moon.type
|
||||
export tree, value, format_error
|
||||
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
|
||||
_append_single: (item) =>
|
||||
if util.moon.type(item) == Line
|
||||
@ -39,22 +66,22 @@ class Line
|
||||
@_append_single item for item in *{...}
|
||||
nil
|
||||
|
||||
-- todo: remove concats from here
|
||||
-- todo: try to remove concats from here
|
||||
render: (buffer) =>
|
||||
current = {}
|
||||
|
||||
add_current = ->
|
||||
insert buffer, concat current
|
||||
buffer\add concat current
|
||||
|
||||
for chunk in *@
|
||||
switch mtype chunk
|
||||
when Block
|
||||
for block_chunk in *chunk\render{}
|
||||
for block_chunk in *chunk\render Lines!
|
||||
if "string" == type block_chunk
|
||||
insert current, block_chunk
|
||||
else
|
||||
add_current!
|
||||
insert buffer, block_chunk
|
||||
buffer\add block_chunk
|
||||
current = {}
|
||||
else
|
||||
insert current, chunk
|
||||
@ -64,7 +91,8 @@ class Line
|
||||
|
||||
buffer
|
||||
|
||||
__tostring: => "Line<#{@render!}>"
|
||||
__tostring: =>
|
||||
"Line<#{util.dump(@)\sub 1, -2}>"
|
||||
|
||||
class Block
|
||||
header: "do"
|
||||
@ -82,8 +110,9 @@ class Block
|
||||
"Block<#{h}> <- " .. tostring @parent
|
||||
|
||||
new: (@parent, @header, @footer) =>
|
||||
@_lines = {}
|
||||
@_posmap = {}
|
||||
@_lines = Lines!
|
||||
|
||||
@_posmap = {} -- todo: kill me
|
||||
@_names = {}
|
||||
@_state = {}
|
||||
@_listeners = {}
|
||||
@ -186,10 +215,6 @@ class Block
|
||||
print "appending pos", self
|
||||
@_posmap[#@_posmap + 1] = map
|
||||
|
||||
-- add raw text as new line
|
||||
add_raw: (item) =>
|
||||
insert @_lines, item
|
||||
|
||||
-- append_line_table: (sub_table, offset) =>
|
||||
-- offset = offset + @current_line
|
||||
|
||||
@ -211,44 +236,24 @@ class Block
|
||||
-- current = current.next
|
||||
|
||||
-- add a line object
|
||||
add: (line) =>
|
||||
-- print "adding", line
|
||||
switch util.moon.type line
|
||||
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
|
||||
add: (item) =>
|
||||
@_lines\add item
|
||||
item
|
||||
|
||||
-- todo: pass in buffer as argument
|
||||
render: (buffer) =>
|
||||
add_to_buffer buffer, @header
|
||||
|
||||
-- copy lines
|
||||
lines = [l for l in *@_lines]
|
||||
buffer\add @header
|
||||
|
||||
if @next
|
||||
insert buffer, lines
|
||||
buffer\add @_lines
|
||||
@next\render buffer
|
||||
else
|
||||
-- join an empty block into a single line
|
||||
if #lines == 0 and "string" == type buffer[#buffer]
|
||||
buffer[#buffer] ..= " " .. (unpack add_to_buffer {}, @footer)
|
||||
if #@_lines == 0 and "string" == type buffer[#buffer]
|
||||
buffer[#buffer] ..= " " .. (unpack Lines!\add @footer)
|
||||
else
|
||||
insert buffer, lines
|
||||
add_to_buffer buffer, @footer
|
||||
buffer\add @_lines
|
||||
buffer\add @footer
|
||||
|
||||
buffer
|
||||
|
||||
@ -330,9 +335,10 @@ class Block
|
||||
|
||||
splice: (fn) =>
|
||||
lines = {"lines", @_lines}
|
||||
@_lines = {}
|
||||
@_lines = Lines!
|
||||
@stms fn lines
|
||||
|
||||
-- move this into Lines
|
||||
flatten_lines = (lines, indent=nil, buffer={}) ->
|
||||
for i = 1, #lines
|
||||
l = lines[i]
|
||||
|
@ -18,7 +18,7 @@ line_compile = {
|
||||
local _list_0 = node[2]
|
||||
for _index_0 = 1, #_list_0 do
|
||||
local line = _list_0[_index_0]
|
||||
self:add_raw(line)
|
||||
self:add(line)
|
||||
end
|
||||
end,
|
||||
declare = function(self, node)
|
||||
|
@ -17,7 +17,7 @@ line_compile =
|
||||
|
||||
lines: (node) =>
|
||||
for line in *node[2]
|
||||
@add_raw line
|
||||
@add line
|
||||
|
||||
declare: (node) =>
|
||||
names = node[2]
|
||||
|
Loading…
Reference in New Issue
Block a user