mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
strings
This commit is contained in:
parent
4d489581ba
commit
6e0927f55d
@ -155,6 +155,11 @@ local compilers = {
|
|||||||
return table.concat(values, " ")
|
return table.concat(values, " ")
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
string = function(self, node)
|
||||||
|
local _, delim, inner, delim_end = unpack(node)
|
||||||
|
return delim..inner..(delim_end or delim)
|
||||||
|
end,
|
||||||
|
|
||||||
value = function(self, node)
|
value = function(self, node)
|
||||||
if type(node) == "table" then
|
if type(node) == "table" then
|
||||||
return self[node[1]](self, node)
|
return self[node[1]](self, node)
|
||||||
|
@ -9,6 +9,7 @@ local dump = require"moonscript.dump"
|
|||||||
local data = require"moonscript.data"
|
local data = require"moonscript.data"
|
||||||
|
|
||||||
local ntype = compile.ntype
|
local ntype = compile.ntype
|
||||||
|
local trim = util.trim
|
||||||
|
|
||||||
local Stack = data.Stack
|
local Stack = data.Stack
|
||||||
|
|
||||||
@ -22,7 +23,7 @@ local function count_indent(str)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local R, S, V, P = lpeg.R, lpeg.S, lpeg.V, lpeg.P
|
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 White = S" \t\n"^0
|
||||||
local Space = S" \t"^0
|
local Space = S" \t"^0
|
||||||
@ -31,11 +32,12 @@ local Break = S"\n"
|
|||||||
local Stop = Break + -1
|
local Stop = Break + -1
|
||||||
local Indent = C(S"\t "^0) / count_indent
|
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 Num = Space * C(R("09")^1) / tonumber
|
||||||
|
|
||||||
local FactorOp = Space * lpeg.C(S"+-")
|
local FactorOp = Space * C(S"+-")
|
||||||
local TermOp = Space * lpeg.C(S"*/%")
|
local TermOp = Space * C(S"*/%")
|
||||||
|
|
||||||
local function wrap(fn)
|
local function wrap(fn)
|
||||||
local env = getfenv(fi)
|
local env = getfenv(fi)
|
||||||
@ -156,9 +158,17 @@ local build_grammar = wrap(function()
|
|||||||
return true, name
|
return true, name
|
||||||
end)
|
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{
|
local g = lpeg.P{
|
||||||
File,
|
File,
|
||||||
File = Block^-1,
|
File = Block + Ct"",
|
||||||
Block = Ct(Line * (Break^1 * Line)^0),
|
Block = Ct(Line * (Break^1 * Line)^0),
|
||||||
Line = Cmt(Indent, check_indent) * Statement,
|
Line = Cmt(Indent, check_indent) * Statement,
|
||||||
Statement = Ct(If) + Exp,
|
Statement = Ct(If) + Exp,
|
||||||
@ -174,11 +184,19 @@ local build_grammar = wrap(function()
|
|||||||
|
|
||||||
Assignable = Cmt(Chain, check_assignable) + Name,
|
Assignable = Cmt(Chain, check_assignable) + Name,
|
||||||
AssignableList = Assignable * (sym"," * Assignable)^0,
|
AssignableList = Assignable * (sym"," * Assignable)^0,
|
||||||
|
|
||||||
Exp = Ct(Term * (FactorOp * Term)^0) / flatten_or_mark"exp",
|
Exp = Ct(Term * (FactorOp * Term)^0) / flatten_or_mark"exp",
|
||||||
Term = Ct(Value * (TermOp * Value)^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,
|
Callable = Name + Parens,
|
||||||
Parens = sym"(" * Exp * sym")",
|
Parens = sym"(" * Exp * sym")",
|
||||||
|
29
tests/inputs/string.moon
Normal file
29
tests/inputs/string.moon
Normal 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
14
tests/outputs/string.lua
Normal 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]]
|
@ -12,7 +12,7 @@ eat(function() end, world)
|
|||||||
local a = 1 + 2 * 3 / 6
|
local a = 1 + 2 * 3 / 6
|
||||||
a = another
|
a = another
|
||||||
local bunch, go, here = world
|
local bunch, go, here = world
|
||||||
func(arg(1, arg(2, another, arg(3))))
|
func(arg1, arg2, another, arg3)
|
||||||
here = function() end
|
here = function() end
|
||||||
local we = yeah
|
local we = yeah
|
||||||
local the, different = function() approach end, yeah
|
local the, different = function() approach end, yeah
|
||||||
|
Loading…
Reference in New Issue
Block a user