aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nomsu_compiler.lua33
-rw-r--r--nomsu_compiler.moon21
-rw-r--r--parser.lua5
-rw-r--r--parser.moon5
4 files changed, 51 insertions, 13 deletions
diff --git a/nomsu_compiler.lua b/nomsu_compiler.lua
index d600bff..da4a155 100644
--- a/nomsu_compiler.lua
+++ b/nomsu_compiler.lua
@@ -854,6 +854,9 @@ do
inline = true
}))
end
+ if tostring(arg_nomsu) ~= '"\\n"' and tostring(arg_nomsu):match("\\n") then
+ arg_nomsu = nil
+ end
if bit.type == "Block" then
next_space = match(next_space, "[^ ]*")
end
@@ -931,10 +934,13 @@ do
local make_text
make_text = function(tree)
local nomsu = NomsuCode(tree.source)
- for _index_0 = 1, #tree do
- local bit = tree[_index_0]
+ for i, bit in ipairs(tree) do
if type(bit) == 'string' then
- nomsu:append((gsub(gsub(gsub(bit, "\\", "\\\\"), "\n", "\\n"), '"', '\\"')))
+ bit = (gsub(gsub(gsub(bit, "\\", "\\\\"), "\n", "\\n"), '"', '\\"'))
+ bit = gsub(bit, "%G", (function(c)
+ return c == ' ' and c or "\\" .. tostring(c:byte())
+ end))
+ nomsu:append(bit)
elseif bit.type == "Text" then
nomsu:append(make_text(bit))
else
@@ -943,6 +949,8 @@ do
}))
if bit.type ~= "Var" and bit.type ~= "List" and bit.type ~= "Dict" then
interp_nomsu:parenthesize()
+ elseif bit.type == "Var" and type(tree[i + 1]) == 'string' and not match(tree[i + 1], "^[ \n\t,.:;#(){}[%]]") then
+ interp_nomsu:parenthesize()
end
nomsu:append("\\", interp_nomsu)
end
@@ -1002,6 +1010,8 @@ do
if interp_nomsu then
if bit.type ~= "Var" and bit.type ~= "List" and bit.type ~= "Dict" then
interp_nomsu:parenthesize()
+ elseif bit.type == "Var" and type(tree[i + 1]) == 'string' and not match(tree[i + 1], "^[ \n\t,.:;#(){}[%]]") then
+ interp_nomsu:parenthesize()
end
nomsu:append("\\", interp_nomsu)
else
@@ -1009,7 +1019,11 @@ do
if not (interp_nomsu) then
return nil
end
- nomsu:append("\\\n ", interp_nomsu)
+ if bit.type ~= "List" and bit.type ~= "Dict" and bit.type ~= "Text" and bit.type ~= "Block" then
+ nomsu:append("\\\n(..) ", interp_nomsu)
+ else
+ nomsu:append("\\", interp_nomsu)
+ end
if i < #tree then
nomsu:append("\n..")
end
@@ -1136,9 +1150,14 @@ do
end
elseif "DictEntry" == _exp_0 then
local key, value = tree[1], tree[2]
- local key_nomsu = assert(recurse(key, {
- inline = true
- }))
+ local key_nomsu
+ if key.type == "Text" and #key == 1 and Parser.is_identifier(key[1]) then
+ key_nomsu = NomsuCode(key.source, key[1])
+ else
+ key_nomsu = assert(recurse(key, {
+ inline = true
+ }))
+ end
if key.type == "Action" or key.type == "Block" then
key_nomsu:parenthesize()
end
diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon
index 08385e8..dd08994 100644
--- a/nomsu_compiler.moon
+++ b/nomsu_compiler.moon
@@ -552,6 +552,8 @@ with NomsuCompiler
else
arg_nomsu = if bit.type == "Block" and #bit > 1 then nil
else assert recurse(bit,inline:true)
+ if tostring(arg_nomsu) != '"\\n"' and tostring(arg_nomsu)\match("\\n")
+ arg_nomsu = nil -- Force long text for multi-line text
next_space = match(next_space, "[^ ]*") if bit.type == "Block"
nomsu\append next_space
if arg_nomsu and nomsu\trailing_line_len! + #tostring(arg_nomsu) < MAX_LINE
@@ -611,16 +613,20 @@ with NomsuCompiler
if options.inline
make_text = (tree)->
nomsu = NomsuCode(tree.source)
- for bit in *tree
+ for i, bit in ipairs tree
if type(bit) == 'string'
-- TODO: unescape better?
- nomsu\append (gsub(gsub(gsub(bit,"\\","\\\\"),"\n","\\n"),'"','\\"'))
+ bit = (gsub(gsub(gsub(bit,"\\","\\\\"),"\n","\\n"),'"','\\"'))
+ bit = gsub(bit, "%G", ((c)-> c == ' ' and c or "\\#{c\byte!}"))
+ nomsu\append bit
elseif bit.type == "Text"
nomsu\append(make_text(bit))
else
interp_nomsu = assert recurse(bit, inline:true)
if bit.type != "Var" and bit.type != "List" and bit.type != "Dict"
interp_nomsu\parenthesize!
+ elseif bit.type == "Var" and type(tree[i+1]) == 'string' and not match(tree[i+1], "^[ \n\t,.:;#(){}[%]]")
+ interp_nomsu\parenthesize!
nomsu\append "\\", interp_nomsu
return nomsu
return NomsuCode(tree.source, '"', make_text(tree), '"')
@@ -660,11 +666,16 @@ with NomsuCompiler
if interp_nomsu
if bit.type != "Var" and bit.type != "List" and bit.type != "Dict"
interp_nomsu\parenthesize!
+ elseif bit.type == "Var" and type(tree[i+1]) == 'string' and not match(tree[i+1], "^[ \n\t,.:;#(){}[%]]")
+ interp_nomsu\parenthesize!
nomsu\append "\\", interp_nomsu
else
interp_nomsu = assert(recurse(bit))
return nil unless interp_nomsu
- nomsu\append "\\\n ", interp_nomsu
+ if bit.type != "List" and bit.type != "Dict" and bit.type != "Text" and bit.type != "Block"
+ nomsu\append "\\\n(..) ", interp_nomsu
+ else
+ nomsu\append "\\", interp_nomsu
if i < #tree
nomsu\append "\n.."
return nomsu
@@ -742,7 +753,9 @@ with NomsuCompiler
when "DictEntry"
key, value = tree[1], tree[2]
- key_nomsu = assert recurse(key, inline:true)
+ key_nomsu = if key.type == "Text" and #key == 1 and Parser.is_identifier(key[1])
+ NomsuCode(key.source, key[1])
+ else assert recurse(key, inline:true)
key_nomsu\parenthesize! if key.type == "Action" or key.type == "Block"
value_nomsu = if value
assert recurse(value, inline:true)
diff --git a/parser.lua b/parser.lua
index 1ccd263..c3478dc 100644
--- a/parser.lua
+++ b/parser.lua
@@ -229,6 +229,9 @@ Parser.parse = function(nomsu_code, source, version)
return tree
end
Parser.is_operator = function(s)
- return not not (NOMSU_DEFS.operator_char ^ 1):match(s)
+ return not not (NOMSU_DEFS.operator_char ^ 1 * -1):match(s)
+end
+Parser.is_identifier = function(s)
+ return not not (NOMSU_DEFS.ident_char ^ 1 * -1):match(s)
end
return Parser
diff --git a/parser.moon b/parser.moon
index 886f4c4..993fd54 100644
--- a/parser.moon
+++ b/parser.moon
@@ -139,6 +139,9 @@ Parser.parse = (nomsu_code, source=nil, version=nil)->
return tree
Parser.is_operator = (s)->
- return not not (NOMSU_DEFS.operator_char^1)\match(s)
+ return not not (NOMSU_DEFS.operator_char^1 * -1)\match(s)
+
+Parser.is_identifier = (s)->
+ return not not (NOMSU_DEFS.ident_char^1 * -1)\match(s)
return Parser