Skip to content

Commit

Permalink
fix empty line consumption for else, fixes #276
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Sep 25, 2016
1 parent f2363f5 commit 2f1dd92
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
8 changes: 4 additions & 4 deletions moonscript/parse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"),
Expand Down
7 changes: 4 additions & 3 deletions moonscript/parse.moon
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down Expand Up @@ -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"
Expand Down
26 changes: 26 additions & 0 deletions spec/inputs/cond.moon
Original file line number Diff line number Diff line change
Expand Up @@ -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



19 changes: 18 additions & 1 deletion spec/outputs/cond.lua
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,21 @@ local a = 12
local c, b
if something then
a, c, b = "cool"
end
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

0 comments on commit 2f1dd92

Please sign in to comment.