This commit is contained in:
leaf corcoran 2011-05-22 00:58:17 -07:00
parent 4d489581ba
commit 6e0927f55d
6 changed files with 79 additions and 8 deletions

View File

@ -155,6 +155,11 @@ local compilers = {
return table.concat(values, " ")
end,
string = function(self, node)
local _, delim, inner, delim_end = unpack(node)
return delim..inner..(delim_end or delim)
end,
value = function(self, node)
if type(node) == "table" then
return self[node[1]](self, node)

View File

@ -9,6 +9,7 @@ local dump = require"moonscript.dump"
local data = require"moonscript.data"
local ntype = compile.ntype
local trim = util.trim
local Stack = data.Stack
@ -22,7 +23,7 @@ local function count_indent(str)
end
local R, S, V, P = lpeg.R, lpeg.S, lpeg.V, lpeg.P
local C, Ct, Cmt = lpeg.C, lpeg.Ct, lpeg.Cmt
local C, Ct, Cmt, Cg, Cb = lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cg, lpeg.Cb
local White = S" \t\n"^0
local Space = S" \t"^0
@ -31,11 +32,12 @@ local Break = S"\n"
local Stop = Break + -1
local Indent = C(S"\t "^0) / count_indent
local Name = Space * C(R("az", "AZ", "__") * R("az", "AZ", "__")^0)
local Name = Space * C(R("az", "AZ", "__") * R("az", "AZ", "09", "__")^0)
local Num = Space * C(R("09")^1) / tonumber
local FactorOp = Space * lpeg.C(S"+-")
local TermOp = Space * lpeg.C(S"*/%")
local FactorOp = Space * C(S"+-")
local TermOp = Space * C(S"*/%")
local function wrap(fn)
local env = getfenv(fi)
@ -156,9 +158,17 @@ local build_grammar = wrap(function()
return true, name
end)
local function simple_string(delim)
return sym(delim) / trim * C((P('\\'..delim) + (1 - S('\n'..delim)))^0) * sym(delim) / mark"string"
end
local function check_lua_string(str, pos, right, left)
return #left == #right
end
local g = lpeg.P{
File,
File = Block^-1,
File = Block + Ct"",
Block = Ct(Line * (Break^1 * Line)^0),
Line = Cmt(Indent, check_indent) * Statement,
Statement = Ct(If) + Exp,
@ -174,11 +184,19 @@ local build_grammar = wrap(function()
Assignable = Cmt(Chain, check_assignable) + Name,
AssignableList = Assignable * (sym"," * Assignable)^0,
Exp = Ct(Term * (FactorOp * Term)^0) / flatten_or_mark"exp",
Term = Ct(Value * (TermOp * Value)^0) / flatten_or_mark"exp",
Value = Assign + FunLit + (Chain + Callable) * Ct(ExpList^0) / flatten_func + Num,
Value = Assign + FunLit + String + (Chain + Callable) * Ct(ExpList^0) / flatten_func + Num,
String = simple_string("'") + simple_string('"') + LuaString,
LuaString = Cg(LuaStringOpen, "string_open") * Cb"string_open" * P"\n"^-1 *
C((1 - Cmt(C(LuaStringClose) * Cb"string_open", check_lua_string))^0) *
C(LuaStringClose) / mark"string",
LuaStringOpen = sym"[" * P"="^0 * "[" / trim,
LuaStringClose = "]" * P"="^0 * "]",
Callable = Name + Parens,
Parens = sym"(" * Exp * sym")",

29
tests/inputs/string.moon Normal file
View File

@ -0,0 +1,29 @@
hi = "hello"
hello = "what the heckyes"
print hi
umm = 'umm'
here, another = "yeah", 'world'
aye = "YU'M"
you '"hmmm" I said'
print aye, you
another = [[ hello world ]]
hi_there = [[
hi there
]]
well = [==[ "helo" ]==]
hola = [===[
eat noots]===]
mm = [[well trhere]]

14
tests/outputs/string.lua Normal file
View File

@ -0,0 +1,14 @@
local hi = "hello"
local hello = "what the heckyes"
print(hi)
local umm = 'umm'
local here, another = "yeah", 'world'
local aye = "YU'M"
you('"hmmm" I said')
print(aye, you)
another = [[ hello world ]]
local hi_there = [[ hi there
]]
local well = [==[ "helo" ]==]
local hola = [===[ eat noots]===]
local mm = [[well trhere]]

View File

@ -12,7 +12,7 @@ eat(function() end, world)
local a = 1 + 2 * 3 / 6
a = another
local bunch, go, here = world
func(arg(1, arg(2, another, arg(3))))
func(arg1, arg2, another, arg3)
here = function() end
local we = yeah
local the, different = function() approach end, yeah

View File

@ -66,3 +66,8 @@ function split(str, delim)
end
function trim(str)
return str:match("^%s*(.-)%s*$")
end