diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-04-08 15:41:05 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-04-08 15:41:55 -0700 |
| commit | fd621a1062c12b808efd7f8adf1957e0fe978ecc (patch) | |
| tree | 6e19a251c58c1187a128989f983073bb214052db | |
| parent | a49e97f0e3a8b69afbc375fac7e04fe49aaf0591 (diff) | |
Cleaned up LHS of "." operator to avoid adding unnecessary parens so
Lua's parser doesn't get confused by (x).y = 1.
| -rw-r--r-- | nomsu.lua | 14 | ||||
| -rwxr-xr-x | nomsu.moon | 16 | ||||
| -rw-r--r-- | nomsu.peg | 7 | ||||
| -rw-r--r-- | tests/collections.nom | 2 |
4 files changed, 29 insertions, 10 deletions
@@ -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 { @@ -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) @@ -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]) |
