Updating to support multiple method calls.
This commit is contained in:
parent
a7adc8cbff
commit
83a40b7493
@ -270,6 +270,9 @@ externally (%tree as lua expr) means:
|
||||
..local tree_lua = compile(\%tree)
|
||||
if \%tree.type == 'Block' then
|
||||
tree_lua = LuaCode:from(\%tree.source, '(function()\\n ', tree_lua, '\\nend)()')
|
||||
elseif \%tree.type == 'MethodCall' and #\%tree > 2 then
|
||||
compile_error_at(\%tree, "This must be a single value instead of "..(#\%tree - 1).." method calls.",
|
||||
"Replace this with a single method call.")
|
||||
end
|
||||
return tree_lua"
|
||||
|
||||
|
@ -122,7 +122,7 @@ inline_action (Action):
|
||||
inline_arg: inline_expression / inline_block
|
||||
inline_methodcall (MethodCall):
|
||||
(index_chain / noindex_inline_expression / "(" inline_block ")")
|
||||
"|" inline_action
|
||||
"|" inline_action (ws* ";" ws* inline_action)*
|
||||
|
||||
action (Action):
|
||||
!section_division
|
||||
@ -132,7 +132,11 @@ linesplit: (ws* "\")? eol nl_nodent ".." ws*
|
||||
arg: expression / inline_block / indented_block
|
||||
methodcall (MethodCall):
|
||||
(index_chain / noindex_inline_expression / indented_expression / "(" inline_block ")" / indented_block)
|
||||
linesplit? "|" linesplit? action
|
||||
linesplit? "|"
|
||||
((ws* inline_action ws* ";")* ws* action
|
||||
/ eol nl_indent
|
||||
(action eol) (nl_nodent action eol)*
|
||||
(%nl (ws* %nl)* nodent (comment / eol / unexpected_code))*)
|
||||
|
||||
word: !number { operator_char+ / ident_char+ }
|
||||
|
||||
|
@ -250,29 +250,32 @@ local compile = setmetatable({
|
||||
lua:add(")")
|
||||
return lua
|
||||
elseif "MethodCall" == _exp_0 then
|
||||
local stub = tree[2].stub
|
||||
local lua = LuaCode:from(tree.source)
|
||||
local target_lua = compile(tree[1])
|
||||
local target_text = target_lua:text()
|
||||
if target_text:match("^%(.*%)$") or target_text:match("^[_a-zA-Z][_a-zA-Z0-9.]*$") or tree[1].type == "IndexChain" then
|
||||
lua:add(target_lua, ":")
|
||||
else
|
||||
lua:add("(", target_lua, "):")
|
||||
if not (target_text:match("^%(.*%)$") or target_text:match("^[_a-zA-Z][_a-zA-Z0-9.]*$") or tree[1].type == "IndexChain") then
|
||||
target_lua:parenthesize()
|
||||
end
|
||||
assert(tree[2].type == "Action")
|
||||
lua:add((stub):as_lua_id(), "(")
|
||||
for i, arg in ipairs(tree[2]:get_args()) do
|
||||
for i = 2, #tree do
|
||||
if i > 2 then
|
||||
lua:add("\n")
|
||||
end
|
||||
lua:add(target_lua, ":")
|
||||
lua:add((tree[i].stub):as_lua_id(), "(")
|
||||
for argnum, arg in ipairs(tree[i]:get_args()) do
|
||||
local arg_lua = compile(arg)
|
||||
if arg.type == "Block" then
|
||||
arg_lua = LuaCode:from(arg.source, "(function()\n ", arg_lua, "\nend)()")
|
||||
end
|
||||
if i > 1 then
|
||||
lua:add(",")
|
||||
if lua:trailing_line_len() + #arg_lua:text() > MAX_LINE then
|
||||
lua:add(argnum > 1 and ",\n " or "\n ")
|
||||
elseif argnum > 1 then
|
||||
lua:add(", ")
|
||||
end
|
||||
lua:add(lua:trailing_line_len() + #arg_lua:text() > MAX_LINE and "\n " or " ")
|
||||
lua:add(arg_lua)
|
||||
end
|
||||
lua:add(")")
|
||||
end
|
||||
return lua
|
||||
elseif "EscapedNomsu" == _exp_0 then
|
||||
local lua = LuaCode:from(tree.source, "SyntaxTree{")
|
||||
|
@ -185,26 +185,26 @@ compile = setmetatable({
|
||||
return lua
|
||||
|
||||
when "MethodCall"
|
||||
stub = tree[2].stub
|
||||
lua = LuaCode\from tree.source
|
||||
target_lua = compile tree[1]
|
||||
target_text = target_lua\text!
|
||||
-- TODO: this parenthesizing is maybe overly conservative
|
||||
if target_text\match("^%(.*%)$") or target_text\match("^[_a-zA-Z][_a-zA-Z0-9.]*$") or
|
||||
tree[1].type == "IndexChain"
|
||||
lua\add target_lua, ":"
|
||||
else
|
||||
lua\add "(", target_lua, "):"
|
||||
if not (target_text\match("^%(.*%)$") or target_text\match("^[_a-zA-Z][_a-zA-Z0-9.]*$") or
|
||||
tree[1].type == "IndexChain")
|
||||
target_lua\parenthesize!
|
||||
|
||||
-- TODO: de-duplicate this code
|
||||
assert tree[2].type == "Action"
|
||||
lua\add((stub)\as_lua_id!,"(")
|
||||
for i, arg in ipairs tree[2]\get_args!
|
||||
for i=2,#tree
|
||||
lua\add "\n" if i > 2
|
||||
lua\add target_lua, ":"
|
||||
lua\add((tree[i].stub)\as_lua_id!,"(")
|
||||
for argnum, arg in ipairs tree[i]\get_args!
|
||||
arg_lua = compile(arg)
|
||||
if arg.type == "Block"
|
||||
arg_lua = LuaCode\from(arg.source, "(function()\n ", arg_lua, "\nend)()")
|
||||
lua\add "," if i > 1
|
||||
lua\add(lua\trailing_line_len! + #arg_lua\text! > MAX_LINE and "\n " or " ")
|
||||
if lua\trailing_line_len! + #arg_lua\text! > MAX_LINE
|
||||
lua\add(argnum > 1 and ",\n " or "\n ")
|
||||
elseif argnum > 1
|
||||
lua\add ", "
|
||||
lua\add arg_lua
|
||||
lua\add ")"
|
||||
return lua
|
||||
|
@ -85,7 +85,14 @@ tree_to_inline_nomsu = function(tree)
|
||||
end
|
||||
return nomsu
|
||||
elseif "MethodCall" == _exp_0 then
|
||||
return NomsuCode:from(tree.source, tree_to_inline_nomsu(tree[1]), "|", tree_to_inline_nomsu(tree[2]))
|
||||
local nomsu = NomsuCode:from(tree.source, tree_to_inline_nomsu(tree[1]), "|")
|
||||
for i = 2, #tree do
|
||||
if i > 2 then
|
||||
nomsu:add("; ")
|
||||
end
|
||||
nomsu:add(tree_to_inline_nomsu(tree[i]))
|
||||
end
|
||||
return nomsu
|
||||
elseif "EscapedNomsu" == _exp_0 then
|
||||
local inner_nomsu = tree_to_inline_nomsu(tree[1])
|
||||
if not (tree[1].type == "List" or tree[1].type == "Dict" or tree[1].type == "Var") then
|
||||
@ -190,7 +197,7 @@ tree_to_inline_nomsu = function(tree)
|
||||
end
|
||||
return NomsuCode:from(tree.source, s)
|
||||
elseif "Var" == _exp_0 then
|
||||
local varname = tree[1]:gsub("_", " ")
|
||||
local varname = tree[1]
|
||||
if varname == "" or is_identifier(varname) then
|
||||
return NomsuCode:from(tree.source, "$", varname)
|
||||
else
|
||||
@ -340,7 +347,18 @@ tree_to_nomsu = function(tree)
|
||||
end
|
||||
nomsu:add(target_nomsu)
|
||||
nomsu:add(target_nomsu:is_multiline() and "\n..|" or "|")
|
||||
nomsu:add(tree_to_nomsu(tree[2]))
|
||||
local inner_nomsu = NomsuCode()
|
||||
for i = 2, #tree do
|
||||
if i > 2 then
|
||||
inner_nomsu:add("\n")
|
||||
end
|
||||
inner_nomsu:add(tree_to_nomsu(tree[i]))
|
||||
end
|
||||
if #tree == 2 and nomsu:trailing_line_len() + #inner_nomsu:text():match("^[^\n]*") < MAX_LINE then
|
||||
nomsu:add(inner_nomsu)
|
||||
else
|
||||
nomsu:add("\n ", inner_nomsu)
|
||||
end
|
||||
return nomsu
|
||||
elseif "EscapedNomsu" == _exp_0 then
|
||||
nomsu = recurse(tree[1])
|
||||
|
@ -57,7 +57,11 @@ tree_to_inline_nomsu = (tree)->
|
||||
return nomsu
|
||||
|
||||
when "MethodCall"
|
||||
return NomsuCode\from(tree.source, tree_to_inline_nomsu(tree[1]), "|", tree_to_inline_nomsu(tree[2]))
|
||||
nomsu = NomsuCode\from(tree.source, tree_to_inline_nomsu(tree[1]), "|")
|
||||
for i=2,#tree
|
||||
nomsu\add "; " if i > 2
|
||||
nomsu\add tree_to_inline_nomsu(tree[i])
|
||||
return nomsu
|
||||
|
||||
when "EscapedNomsu"
|
||||
inner_nomsu = tree_to_inline_nomsu(tree[1])
|
||||
@ -139,8 +143,7 @@ tree_to_inline_nomsu = (tree)->
|
||||
return NomsuCode\from(tree.source, s)
|
||||
|
||||
when "Var"
|
||||
-- TODO: remove this hack:
|
||||
varname = tree[1]\gsub("_", " ")
|
||||
varname = tree[1]
|
||||
if varname == "" or is_identifier(varname)
|
||||
return NomsuCode\from(tree.source, "$", varname)
|
||||
else
|
||||
@ -265,7 +268,14 @@ tree_to_nomsu = (tree)->
|
||||
target_nomsu\parenthesize!
|
||||
nomsu\add target_nomsu
|
||||
nomsu\add(target_nomsu\is_multiline! and "\n..|" or "|")
|
||||
nomsu\add tree_to_nomsu(tree[2])
|
||||
inner_nomsu = NomsuCode!
|
||||
for i=2,#tree
|
||||
inner_nomsu\add "\n" if i > 2
|
||||
inner_nomsu\add tree_to_nomsu(tree[i])
|
||||
if #tree == 2 and nomsu\trailing_line_len! + #inner_nomsu\text!\match("^[^\n]*") < MAX_LINE
|
||||
nomsu\add inner_nomsu
|
||||
else
|
||||
nomsu\add "\n ", inner_nomsu
|
||||
return nomsu
|
||||
|
||||
when "EscapedNomsu"
|
||||
|
Loading…
Reference in New Issue
Block a user