allow line breaks inside of quote strings

This commit is contained in:
leaf corcoran 2013-04-17 21:19:40 -07:00
parent becba7f27a
commit ce3d4adbb1
6 changed files with 45 additions and 6 deletions

View File

@ -64,11 +64,19 @@ Comments are not written to the output.
## Literals & Operators
MoonScript supports all the same primitive literals as Lua and uses the same
syntax. This applies to numbers, strings, booleans, and `nil`.
All of the primitive literals in Lua can be used. This applies to numbers,
strings, booleans, and `nil`.
MoonScript also supports all the same binary and unary operators. Additionally
`!=` is as an alias for `~=`.
All of Lua's binary and unary operators are available. Additionally `!=` is as
an alias for `~=`.
Unlike Lua, Line breaks are allowed inside of single and double quote strings
without an escape sequence:
```moon
some_string = "Here is a string
that has a line break in it."
```
## Function Literals

View File

@ -13,6 +13,10 @@ end
local concat, insert = table.concat, table.insert
local unpack = util.unpack
local table_delim = ","
local string_chars = {
["\r"] = "\\r",
["\n"] = "\\n"
}
local value_compilers = {
exp = function(self, node)
local _comp
@ -61,6 +65,9 @@ local value_compilers = {
string = function(self, node)
local _, delim, inner = unpack(node)
local end_delim = delim:gsub("%[", "]")
if delim == "'" or delim == '"' then
inner = inner:gsub("[\r\n]", string_chars)
end
return delim .. inner .. end_delim
end,
chain = function(self, node)

View File

@ -9,6 +9,11 @@ import unpack from util
table_delim = ","
string_chars = {
"\r": "\\r"
"\n": "\\n"
}
value_compilers =
-- list of values separated by binary operators
exp: (node) =>
@ -31,6 +36,9 @@ value_compilers =
string: (node) =>
_, delim, inner = unpack node
end_delim = delim\gsub "%[", "]"
if delim == "'" or delim == '"'
inner = inner\gsub "[\r\n]", string_chars
delim..inner..end_delim
chain: (node) =>

View File

@ -227,7 +227,7 @@ local function symx(chars)
end
local function simple_string(delim, allow_interpolation)
local inner = P('\\'..delim) + "\\\\" + (1 - S('\r\n'..delim))
local inner = P('\\'..delim) + "\\\\" + (1 - P(delim))
if allow_interpolation then
inter = symx"#{" * V"Exp" * sym"}"
inner = (C((inner - inter)^1) + inter / mark"interpolate")^0

View File

@ -23,3 +23,16 @@
"another world"
'what world'
"
hello world
"
'yeah
what is going on
here is something cool'
nil

View File

@ -14,4 +14,7 @@ _ = [[ hello world ]]
_ = [=[ hello world ]=]
_ = [====[ hello world ]====]
_ = "another world"
return 'what world'
_ = 'what world'
_ = "\nhello world\n"
_ = 'yeah\nwhat is going on\nhere is something cool'
return nil