From 2f1dd92012afd89ce22e0babf774749a4d77d245 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 12:57:54 -0700 Subject: [PATCH] fix empty line consumption for else, fixes #276 --- moonscript/parse.lua | 8 ++++---- moonscript/parse.moon | 7 ++++--- spec/inputs/cond.moon | 26 ++++++++++++++++++++++++++ spec/outputs/cond.lua | 19 ++++++++++++++++++- 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 240041b..10643a2 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -27,10 +27,10 @@ Num = Space * (Num / function(v) v } end) -local Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, join_chain, wrap_decorator, check_lua_string, self_assign +local Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, join_chain, wrap_decorator, check_lua_string, self_assign, got do local _obj_0 = require("moonscript.parse.util") - Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, join_chain, wrap_decorator, check_lua_string, self_assign = _obj_0.Indent, _obj_0.Cut, _obj_0.ensure, _obj_0.extract_line, _obj_0.mark, _obj_0.pos, _obj_0.flatten_or_mark, _obj_0.is_assignable, _obj_0.check_assignable, _obj_0.format_assign, _obj_0.format_single_assign, _obj_0.sym, _obj_0.symx, _obj_0.simple_string, _obj_0.wrap_func_arg, _obj_0.join_chain, _obj_0.wrap_decorator, _obj_0.check_lua_string, _obj_0.self_assign + Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, join_chain, wrap_decorator, check_lua_string, self_assign, got = _obj_0.Indent, _obj_0.Cut, _obj_0.ensure, _obj_0.extract_line, _obj_0.mark, _obj_0.pos, _obj_0.flatten_or_mark, _obj_0.is_assignable, _obj_0.check_assignable, _obj_0.format_assign, _obj_0.format_single_assign, _obj_0.sym, _obj_0.symx, _obj_0.simple_string, _obj_0.wrap_func_arg, _obj_0.join_chain, _obj_0.wrap_decorator, _obj_0.check_lua_string, _obj_0.self_assign, _obj_0.got end local build_grammar = wrap_env(debug_grammar, function(root) local _indent = Stack(0) @@ -131,8 +131,8 @@ local build_grammar = wrap_env(debug_grammar, function(root) SwitchCase = key("when") * Ct(ExpList) * key("then") ^ -1 * Body / mark("case"), SwitchElse = key("else") * Body / mark("else"), IfCond = Exp * Assign ^ -1 / format_single_assign, - IfElse = (Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("else") * Body / mark("else"), - IfElseIf = (Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("elseif") * pos(IfCond) * key("then") ^ -1 * Body / mark("elseif"), + IfElse = (Break * EmptyLine ^ 0 * CheckIndent) ^ -1 * key("else") * Body / mark("else"), + IfElseIf = (Break * EmptyLine ^ 0 * CheckIndent) ^ -1 * key("elseif") * pos(IfCond) * key("then") ^ -1 * Body / mark("elseif"), If = key("if") * IfCond * key("then") ^ -1 * Body * IfElseIf ^ 0 * IfElse ^ -1 / mark("if"), Unless = key("unless") * IfCond * key("then") ^ -1 * Body * IfElseIf ^ 0 * IfElse ^ -1 / mark("unless"), While = key("while") * DisableDo * ensure(Exp, PopDo) * key("do") ^ -1 * Body / mark("while"), diff --git a/moonscript/parse.moon b/moonscript/parse.moon index b309de3..6daf9ee 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -27,7 +27,8 @@ Num = Space * (Num / (v) -> {"number", v}) :Indent, :Cut, :ensure, :extract_line, :mark, :pos, :flatten_or_mark, :is_assignable, :check_assignable, :format_assign, :format_single_assign, :sym, :symx, :simple_string, :wrap_func_arg, :join_chain, - :wrap_decorator, :check_lua_string, :self_assign + :wrap_decorator, :check_lua_string, :self_assign, :got + } = require "moonscript.parse.util" @@ -150,8 +151,8 @@ build_grammar = wrap_env debug_grammar, (root) -> IfCond: Exp * Assign^-1 / format_single_assign - IfElse: (Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else" - IfElseIf: (Break * CheckIndent)^-1 * EmptyLine^0 * key"elseif" * pos(IfCond) * key"then"^-1 * Body / mark"elseif" + IfElse: (Break * EmptyLine^0 * CheckIndent)^-1 * key"else" * Body / mark"else" + IfElseIf: (Break * EmptyLine^0 * CheckIndent)^-1 * key"elseif" * pos(IfCond) * key"then"^-1 * Body / mark"elseif" If: key"if" * IfCond * key"then"^-1 * Body * IfElseIf^0 * IfElse^-1 / mark"if" Unless: key"unless" * IfCond * key"then"^-1 * Body * IfElseIf^0 * IfElse^-1 / mark"unless" diff --git a/spec/inputs/cond.moon b/spec/inputs/cond.moon index 8909721..3bf966f 100644 --- a/spec/inputs/cond.moon +++ b/spec/inputs/cond.moon @@ -154,3 +154,29 @@ a,c,b = "cool" if something +--- + +j = if 1 + if 2 + 3 +else 6 + + +m = if 1 + + + + if 2 + + + 3 + + +else 6 + + + +nil + + + diff --git a/spec/outputs/cond.lua b/spec/outputs/cond.lua index 036f546..30e8762 100644 --- a/spec/outputs/cond.lua +++ b/spec/outputs/cond.lua @@ -250,4 +250,21 @@ local a = 12 local c, b if something then a, c, b = "cool" -end \ No newline at end of file +end +local j +if 1 then + if 2 then + j = 3 + end +else + j = 6 +end +local m +if 1 then + if 2 then + m = 3 + end +else + m = 6 +end +return nil \ No newline at end of file