add string interpolation to double quote string

This commit is contained in:
leaf corcoran 2012-09-27 18:13:03 -07:00
parent 98ebb97b16
commit 8b2fa75e4c
7 changed files with 134 additions and 11 deletions

View File

@ -86,8 +86,13 @@ value_compile = {
return self:line("(", self:value(node[2]), ")")
end,
string = function(self, node)
local _, delim, inner, delim_end = unpack(node)
return delim .. inner .. (delim_end or delim)
local _, delim, inner = unpack(node)
local end_delim = delim:gsub("%[", "]")
if inner then
return delim .. inner .. end_delim
else
return delim .. end_delim
end
end,
chain = function(self, node)
local callee = node[2]

View File

@ -44,8 +44,12 @@ value_compile =
@line "(", @value(node[2]), ")"
string: (node) =>
_, delim, inner, delim_end = unpack node
delim..inner..(delim_end or delim)
_, delim, inner = unpack node
end_delim = delim\gsub "%[", "]"
if inner
delim..inner..end_delim
else
delim..end_delim
chain: (node) =>
callee = node[2]

View File

@ -148,10 +148,17 @@ local function symx(chars)
return chars
end
local function simple_string(delim, x)
return C(symx(delim)) * C((P('\\'..delim) +
"\\\\" +
(1 - S('\r\n'..delim)))^0) * sym(delim) / mark"string"
local function simple_string(delim, allow_interpolation)
local inner = P('\\'..delim) + "\\\\" + (1 - S('\r\n'..delim))
if allow_interpolation then
inter = symx"#{" * V"Exp" * sym"}"
inner = (C((inner - inter)^1) + inter / mark"interpolate")^0
else
inner = C(inner^0)
end
return C(symx(delim)) *
inner * sym(delim) / mark"string"
end
local function wrap_func_arg(value)
@ -384,11 +391,11 @@ local build_grammar = wrap_env(function()
String = Space * DoubleString + Space * SingleString + LuaString,
SingleString = simple_string("'"),
DoubleString = simple_string('"'),
DoubleString = simple_string('"', true),
LuaString = Cg(LuaStringOpen, "string_open") * Cb"string_open" * Break^-1 *
C((1 - Cmt(C(LuaStringClose) * Cb"string_open", check_lua_string))^0) *
C(LuaStringClose) / mark"string",
LuaStringClose / mark"string",
LuaStringOpen = sym"[" * P"="^0 * "[" / trim,
LuaStringClose = "]" * P"="^0 * "]",

View File

@ -40,6 +40,9 @@ LocalName = (function()
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)()
NameProxy = (function()
@ -127,6 +130,9 @@ NameProxy = (function()
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)()
Run = (function()
@ -164,6 +170,9 @@ Run = (function()
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)()
local apply_to_last
@ -303,6 +312,9 @@ Transformer = (function()
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)()
local construct_comprehension
@ -1082,6 +1094,9 @@ Accumulator = (function()
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)()
local default_accumulator
@ -1120,6 +1135,44 @@ Value = Transformer({
["for"] = default_accumulator,
["while"] = default_accumulator,
foreach = default_accumulator,
string = function(self, node)
if #node <= 3 then
return node
end
local delim = node[2]
local convert_part
convert_part = function(part)
if part == nil then
return
end
if type(part) == "string" then
return {
"string",
delim,
part
}
else
return build.chain({
base = "tostring",
{
"call",
{
part[2]
}
}
})
end
end
local e = {
"exp",
convert_part(node[3])
}
for i = 4, #node do
insert(e, "..")
insert(e, convert_part(node[i]))
end
return e
end,
comprehension = function(self, node)
local a = Accumulator()
node = self.transform.statement(node, function(exp)

View File

@ -593,6 +593,24 @@ Value = Transformer {
while: default_accumulator
foreach: default_accumulator
string: (node) =>
return node if #node <= 3
delim = node[2]
convert_part = (part) ->
return if part == nil
if type(part) == "string"
{"string", delim, part}
else
build.chain { base: "tostring", {"call", {part[2]}} }
e = {"exp", convert_part node[3]}
for i=4,#node
insert e, ".."
insert e, convert_part node[i]
e
comprehension: (node) =>
a = Accumulator!
node = @transform.statement node, (exp) ->

View File

@ -31,3 +31,27 @@ x = "\\"
x = "a\\b"
x = "\\\n"
x = "\""
--
a = "hello #{hello} hello"
b = "#{hello} hello"
c = "hello #{hello}"
d = ""
y = "hello" .. "world"
y = [[hello world]]
--
a = 'hello #{hello} hello'
b = '#{hello} hello'
c = 'hello #{hello}'
d = ''
y = 'hello' .. 'world'
y = [[hello world]]

View File

@ -15,4 +15,16 @@ local mm = [[well trhere]]
local x = "\\"
x = "a\\b"
x = "\\\n"
x = "\""
x = "\""
local a = "hello " .. tostring(hello) .. " hello"
local b = tostring(hello) .. " hello"
local c = "hello " .. tostring(hello)
local d = ""
local y = "hello" .. "world"
y = [[hello world]]
a = 'hello #{hello} hello'
b = '#{hello} hello'
c = 'hello #{hello}'
d = ''
y = 'hello' .. 'world'
y = [[hello world]]