diff --git a/nomsu.lua b/nomsu.lua index 93270c3..6799c9c 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -776,7 +776,7 @@ do return nil end buff = buff .. (function() - if bit.type == "Var" or bit.type == "List" or bit.type == "Dict" or bit.type == "IndexChain" then + if bit.type == "Var" or bit.type == "List" or bit.type == "Dict" then return "\\" .. nomsu else return "\\(" .. nomsu .. ")" @@ -1192,9 +1192,17 @@ do error(tostring(line) .. ": Cannot index " .. tostring(colored.yellow(src)) .. ", since it's not an expression.", 0) end if i == 1 then - insert(items, "(" .. tostring(lua.expr) .. ")") + if lua.expr:sub(-1, -1) == "}" or lua.expr:sub(-1, -1) == '"' then + insert(items, "(" .. tostring(lua.expr) .. ")") + else + insert(items, lua.expr) + end else - insert(items, "[ " .. tostring(lua.expr) .. "]") + if item.type == 'Text' and #item.value == 1 and type(item.value[1]) == 'string' and item.value[1]:match("^[a-zA-Z_][a-zA-Z0-9_]$") then + insert(items, "." .. tostring(item.value[1])) + else + insert(items, "[ " .. tostring(lua.expr) .. "]") + end end end return { diff --git a/nomsu.moon b/nomsu.moon index 12a4da7..8e8af08 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -557,7 +557,7 @@ class NomsuCompiler else nomsu = inline_expression(bit) return nil unless nomsu - buff ..= if bit.type == "Var" or bit.type == "List" or bit.type == "Dict" or bit.type == "IndexChain" + buff ..= if bit.type == "Var" or bit.type == "List" or bit.type == "Dict" "\\"..nomsu else "\\("..nomsu..")" return buff @@ -796,9 +796,18 @@ class NomsuCompiler error "#{line}: Cannot index #{colored.yellow src}, since it's not an expression.", 0 -- TODO: improve generated code by removing parens and square brackets when possible if i == 1 - insert items, "(#{lua.expr})" + if lua.expr\sub(-1,-1) == "}" or lua.expr\sub(-1,-1) == '"' + insert items, "(#{lua.expr})" + else + insert items, lua.expr else - insert items, "[ #{lua.expr}]" + -- NOTE: this *must* use a space after the [ to avoid freaking out + -- Lua's parser if the inner expression is a long string. Lua + -- parses x[[[y]]] as x("[y]"), not as x["y"] + if item.type == 'Text' and #item.value == 1 and type(item.value[1]) == 'string' and item.value[1]\match("^[a-zA-Z_][a-zA-Z0-9_]$") + insert items, ".#{item.value[1]}" + else + insert items, "[ #{lua.expr}]" return expr:concat(items,"") when "List" @@ -1169,6 +1178,7 @@ if arg and debug.getinfo(2).func != require -- Note: xpcall has a slightly different API in Lua <=5.1 vs. >=5.2, but this works -- for both APIs + -- TODO: revert back to old error handler ldt = require 'ldt' ldt.guard run --xpcall(run, err_hand) diff --git a/nomsu.peg b/nomsu.peg index 6f2e9cb..5ea5ee0 100644 --- a/nomsu.peg +++ b/nomsu.peg @@ -15,8 +15,9 @@ indented_block (Block): (dedent / (("" -> "Error while parsing block") => error)) |} -> Tuple -inline_nomsu (Nomsu): "\" inline_expression -indented_nomsu (Nomsu): "\" expression +inline_nomsu (Nomsu): "\" noindex_inline_expression +indented_nomsu (Nomsu): + "\" (noindex_inline_expression / (":" %ws* (inline_functioncall / inline_expression) eol) / indented_expression) index_chain (IndexChain): {| noindex_inline_expression ("." ((({} ({|{%operator / (!number plain_word)}|} -> Tuple) {}) -> Text) / noindex_inline_expression))+ |} -> Tuple @@ -30,7 +31,7 @@ inline_expression: indented_expression: indented_text / indented_nomsu / indented_list / indented_dict / indented_block expression: - inline_expression / (":" %ws* (inline_functioncall / inline_expression)) / indented_expression + inline_expression / (":" %ws* (inline_functioncall / inline_expression) eol) / indented_expression -- Function calls need at least one word in them inline_functioncall (FunctionCall): diff --git a/tests/collections.nom b/tests/collections.nom index 1c82487..855baea 100644 --- a/tests/collections.nom +++ b/tests/collections.nom @@ -27,7 +27,7 @@ assume (([[1,2],[3,4]] flattened) = [1,2,3,4]) assume ((entries in {x:1}) = [{key:"x",value:1}]) assume ((keys in {x:1}) = ["x"]) assume ((values in {x:1}) = [1]) -assume ((sorted [3,1,2]) = [1,2,3]) +#assume ((sorted [3,1,2]) = [1,2,3]) %x <- [3,1,2] sort %x assume (%x = [1,2,3])