move compile chunks into the compiler

This commit is contained in:
leaf corcoran
2017-11-10 20:52:01 -08:00
parent 0157b28ed6
commit 3d81e1f05c
2 changed files with 64 additions and 62 deletions

View File

@@ -58,6 +58,7 @@ local Compiler
do do
local _class_0 local _class_0
local _base_0 = { local _base_0 = {
html_escape = true,
render = function(self) render = function(self)
return table.concat(self.buffer) return table.concat(self.buffer)
end, end,
@@ -86,6 +87,37 @@ do
if ... then if ... then
return self:push("\n") return self:push("\n")
end end
end,
compile_chunks = function(self, chunks)
self:header()
for _index_0 = 1, #chunks do
local chunk = chunks[_index_0]
local t = type(chunk)
if t == "table" then
t = chunk[1]
end
local _exp_0 = t
if "string" == _exp_0 then
self:increment()
self:assign(("%q"):format(chunk))
elseif "code" == _exp_0 then
self:mark(chunk[3])
self:push(chunk[2], "\n")
elseif "=" == _exp_0 or "-" == _exp_0 then
self:increment()
self:mark(chunk[3])
self:assign()
if t == "=" and self.html_escape then
self:push("_escape(_tostring(", chunk[2], "))\n")
else
self:push("_tostring(", chunk[2], ")\n")
end
else
error("unknown type " .. tostring(t))
end
end
self:footer()
return self:render()
end end
} }
_base_0.__index = _base_0 _base_0.__index = _base_0
@@ -114,7 +146,6 @@ do
open_tag = "<%", open_tag = "<%",
close_tag = "%>", close_tag = "%>",
modifiers = "^[=-]", modifiers = "^[=-]",
html_escape = true,
next_tag = function(self) next_tag = function(self)
local start, stop = self.str:find(self.open_tag, self.pos, true) local start, stop = self.str:find(self.open_tag, self.pos, true)
if not (start) then if not (start) then
@@ -333,37 +364,7 @@ do
if compiler_cls == nil then if compiler_cls == nil then
compiler_cls = Compiler compiler_cls = Compiler
end end
local r = compiler_cls() return compiler_cls():compile_chunks(self.chunks)
r:header()
local _list_0 = self.chunks
for _index_0 = 1, #_list_0 do
local chunk = _list_0[_index_0]
local t = type(chunk)
if t == "table" then
t = chunk[1]
end
local _exp_0 = t
if "string" == _exp_0 then
r:increment()
r:assign(("%q"):format(chunk))
elseif "code" == _exp_0 then
r:mark(chunk[3])
r:push(chunk[2], "\n")
elseif "=" == _exp_0 or "-" == _exp_0 then
r:increment()
r:mark(chunk[3])
r:assign()
if t == "=" and self.html_escape then
r:push("_escape(_tostring(", chunk[2], "))\n")
else
r:push("_tostring(", chunk[2], ")\n")
end
else
error("unknown type " .. tostring(t))
end
end
r:footer()
return r:render()
end end
} }
_base_0.__index = _base_0 _base_0.__index = _base_0

View File

@@ -40,6 +40,8 @@ pos_to_line = (str, pos) ->
line line
class Compiler class Compiler
html_escape: true
new: => new: =>
@buffer = {} @buffer = {}
@i = 0 @i = 0
@@ -69,11 +71,38 @@ class Compiler
@push "_b[_b_i] = ", ... @push "_b[_b_i] = ", ...
@push "\n" if ... @push "\n" if ...
compile_chunks: (chunks) =>
@header!
for chunk in *chunks
t = type chunk
t = chunk[1] if t == "table"
switch t
when "string"
@increment!
@assign ("%q")\format(chunk)
when "code"
@mark chunk[3]
@push chunk[2], "\n"
when "=", "-"
@increment!
@mark chunk[3]
@assign!
if t == "=" and @html_escape
@push "_escape(_tostring(", chunk[2], "))\n"
else
@push "_tostring(", chunk[2], ")\n"
else
error "unknown type #{t}"
@footer!
@render!
class Parser class Parser
open_tag: "<%" open_tag: "<%"
close_tag: "%>" close_tag: "%>"
modifiers: "^[=-]" modifiers: "^[=-]"
html_escape: true
next_tag: => next_tag: =>
start, stop = @str\find @open_tag, @pos, true start, stop = @str\find @open_tag, @pos, true
@@ -242,35 +271,7 @@ class Parser
-- generates the code of the template -- generates the code of the template
chunks_to_lua: (compiler_cls=Compiler) => chunks_to_lua: (compiler_cls=Compiler) =>
r = compiler_cls! compiler_cls!\compile_chunks @chunks
r\header!
for chunk in *@chunks
t = type chunk
t = chunk[1] if t == "table"
switch t
when "string"
r\increment!
r\assign ("%q")\format(chunk)
when "code"
r\mark chunk[3]
r\push chunk[2], "\n"
when "=", "-"
r\increment!
r\mark chunk[3]
r\assign!
if t == "=" and @html_escape
r\push "_escape(_tostring(", chunk[2], "))\n"
else
r\push "_tostring(", chunk[2], ")\n"
else
error "unknown type #{t}"
r\footer!
r\render!
compile = Parser!\compile compile = Parser!\compile