mirror of
https://github.com/leafo/moonscript.git
synced 2024-10-05 05:34:19 +00:00
delayed line
This commit is contained in:
parent
0306608e30
commit
fa06e6103d
@ -30,7 +30,7 @@ local concat, insert = table.concat, table.insert
|
|||||||
local pos_to_line, get_closest_line, trim, unpack = util.pos_to_line, util.get_closest_line, util.trim, util.unpack
|
local pos_to_line, get_closest_line, trim, unpack = util.pos_to_line, util.get_closest_line, util.trim, util.unpack
|
||||||
local mtype = util.moon.type
|
local mtype = util.moon.type
|
||||||
local indent_char = " "
|
local indent_char = " "
|
||||||
local Line, Lines, Block, RootBlock
|
local Line, DelayedLine, Lines, Block, RootBlock
|
||||||
do
|
do
|
||||||
local _parent_0 = nil
|
local _parent_0 = nil
|
||||||
local _base_0 = {
|
local _base_0 = {
|
||||||
@ -62,13 +62,13 @@ do
|
|||||||
end
|
end
|
||||||
local posmap = self.posmap
|
local posmap = self.posmap
|
||||||
for i, l in ipairs(self) do
|
for i, l in ipairs(self) do
|
||||||
local _exp_0 = type(l)
|
local _exp_0 = mtype(l)
|
||||||
if "table" == _exp_0 then
|
if "string" == _exp_0 or DelayedLine == _exp_0 then
|
||||||
local _
|
|
||||||
_, line_no = l:flatten_posmap(line_no, out)
|
|
||||||
elseif "string" == _exp_0 then
|
|
||||||
line_no = line_no + 1
|
line_no = line_no + 1
|
||||||
out[line_no] = posmap[i]
|
out[line_no] = posmap[i]
|
||||||
|
elseif "table" == _exp_0 then
|
||||||
|
local _
|
||||||
|
_, line_no = l:flatten_posmap(line_no, out)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return out, line_no
|
return out, line_no
|
||||||
@ -82,7 +82,12 @@ do
|
|||||||
end
|
end
|
||||||
for i = 1, #self do
|
for i = 1, #self do
|
||||||
local l = self[i]
|
local l = self[i]
|
||||||
local _exp_0 = type(l)
|
local t = mtype(l)
|
||||||
|
if t == DelayedLine then
|
||||||
|
l = l:render()
|
||||||
|
t = "string"
|
||||||
|
end
|
||||||
|
local _exp_0 = t
|
||||||
if "string" == _exp_0 then
|
if "string" == _exp_0 then
|
||||||
if indent then
|
if indent then
|
||||||
insert(buffer, indent)
|
insert(buffer, indent)
|
||||||
@ -264,6 +269,47 @@ do
|
|||||||
end
|
end
|
||||||
Line = _class_0
|
Line = _class_0
|
||||||
end
|
end
|
||||||
|
do
|
||||||
|
local _parent_0 = nil
|
||||||
|
local _base_0 = {
|
||||||
|
prepare = function() end,
|
||||||
|
render = function(self)
|
||||||
|
self:prepare()
|
||||||
|
return concat(self)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
_base_0.__index = _base_0
|
||||||
|
if _parent_0 then
|
||||||
|
setmetatable(_base_0, _parent_0.__base)
|
||||||
|
end
|
||||||
|
local _class_0 = setmetatable({
|
||||||
|
__init = function(self, fn)
|
||||||
|
self.prepare = fn
|
||||||
|
end,
|
||||||
|
__base = _base_0,
|
||||||
|
__name = "DelayedLine",
|
||||||
|
__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
|
||||||
|
DelayedLine = _class_0
|
||||||
|
end
|
||||||
do
|
do
|
||||||
local _parent_0 = nil
|
local _parent_0 = nil
|
||||||
local _base_0 = {
|
local _base_0 = {
|
||||||
|
@ -17,7 +17,7 @@ mtype = util.moon.type
|
|||||||
|
|
||||||
indent_char = " "
|
indent_char = " "
|
||||||
|
|
||||||
local Line, Lines, Block, RootBlock
|
local Line, DelayedLine, Lines, Block, RootBlock
|
||||||
|
|
||||||
-- a buffer for building up lines
|
-- a buffer for building up lines
|
||||||
class Lines
|
class Lines
|
||||||
@ -34,26 +34,32 @@ class Lines
|
|||||||
item\render self
|
item\render self
|
||||||
when Block
|
when Block
|
||||||
item\render self
|
item\render self
|
||||||
else
|
else -- also captures DelayedLine
|
||||||
@[#@ + 1] = item
|
@[#@ + 1] = item
|
||||||
@
|
@
|
||||||
|
|
||||||
flatten_posmap: (line_no=0, out={}) =>
|
flatten_posmap: (line_no=0, out={}) =>
|
||||||
posmap = @posmap
|
posmap = @posmap
|
||||||
for i, l in ipairs @
|
for i, l in ipairs @
|
||||||
switch type l
|
switch mtype l
|
||||||
when "table"
|
when "string", DelayedLine
|
||||||
_, line_no = l\flatten_posmap line_no, out
|
|
||||||
when "string"
|
|
||||||
line_no += 1
|
line_no += 1
|
||||||
out[line_no] = posmap[i]
|
out[line_no] = posmap[i]
|
||||||
|
when "table"
|
||||||
|
_, line_no = l\flatten_posmap line_no, out
|
||||||
|
|
||||||
out, line_no
|
out, line_no
|
||||||
|
|
||||||
flatten: (indent=nil, buffer={}) =>
|
flatten: (indent=nil, buffer={}) =>
|
||||||
for i = 1, #@
|
for i = 1, #@
|
||||||
l = @[i]
|
l = @[i]
|
||||||
switch type l
|
t = mtype l
|
||||||
|
|
||||||
|
if t == DelayedLine
|
||||||
|
l = l\render!
|
||||||
|
t = "string"
|
||||||
|
|
||||||
|
switch t
|
||||||
when "string"
|
when "string"
|
||||||
insert buffer, indent if indent
|
insert buffer, indent if indent
|
||||||
insert buffer, l
|
insert buffer, l
|
||||||
@ -66,7 +72,7 @@ class Lines
|
|||||||
|
|
||||||
insert buffer, "\n"
|
insert buffer, "\n"
|
||||||
last = l
|
last = l
|
||||||
when "table"
|
when "table" -- Lines
|
||||||
l\flatten indent and indent .. indent_char or indent_char, buffer
|
l\flatten indent and indent .. indent_char or indent_char, buffer
|
||||||
buffer
|
buffer
|
||||||
|
|
||||||
@ -134,6 +140,16 @@ class Line
|
|||||||
__tostring: =>
|
__tostring: =>
|
||||||
"Line<#{util.dump(@)\sub 1, -2}>"
|
"Line<#{util.dump(@)\sub 1, -2}>"
|
||||||
|
|
||||||
|
class DelayedLine
|
||||||
|
new: (fn) =>
|
||||||
|
@prepare = fn
|
||||||
|
|
||||||
|
prepare: ->
|
||||||
|
|
||||||
|
render: =>
|
||||||
|
@prepare!
|
||||||
|
concat @
|
||||||
|
|
||||||
class Block
|
class Block
|
||||||
header: "do"
|
header: "do"
|
||||||
footer: "end"
|
footer: "end"
|
||||||
|
@ -33,7 +33,5 @@ lua_keywords = Set{
|
|||||||
'until', 'while'
|
'until', 'while'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
{ :Set, :Stack, :lua_keywords }
|
{ :Set, :Stack, :lua_keywords }
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user