diff --git a/docs/reference.md b/docs/reference.md index 1f357bb..2d2b158 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -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 diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 19a160f..28b2cb3 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -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) diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 52006ed..89b21e9 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -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) => diff --git a/moonscript/parse.lua b/moonscript/parse.lua index f6d04c2..a4e36b9 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -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 diff --git a/tests/inputs/literals.moon b/tests/inputs/literals.moon index 125b494..7f58b6e 100644 --- a/tests/inputs/literals.moon +++ b/tests/inputs/literals.moon @@ -23,3 +23,16 @@ "another world" 'what world' + + +" +hello world +" + +'yeah +what is going on +here is something cool' + + +nil + diff --git a/tests/outputs/literals.lua b/tests/outputs/literals.lua index 93c0629..b49c57a 100644 --- a/tests/outputs/literals.lua +++ b/tests/outputs/literals.lua @@ -14,4 +14,7 @@ _ = [[ hello world ]] _ = [=[ hello world ]=] _ = [====[ hello world ]====] _ = "another world" -return 'what world' \ No newline at end of file +_ = 'what world' +_ = "\nhello world\n" +_ = 'yeah\nwhat is going on\nhere is something cool' +return nil \ No newline at end of file