Skip to content

Commit 2f1dd92

Browse files
committed
fix empty line consumption for else, fixes #276
1 parent f2363f5 commit 2f1dd92

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

moonscript/parse.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ Num = Space * (Num / function(v)
2727
v
2828
}
2929
end)
30-
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
30+
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
3131
do
3232
local _obj_0 = require("moonscript.parse.util")
33-
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
33+
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
3434
end
3535
local build_grammar = wrap_env(debug_grammar, function(root)
3636
local _indent = Stack(0)
@@ -131,8 +131,8 @@ local build_grammar = wrap_env(debug_grammar, function(root)
131131
SwitchCase = key("when") * Ct(ExpList) * key("then") ^ -1 * Body / mark("case"),
132132
SwitchElse = key("else") * Body / mark("else"),
133133
IfCond = Exp * Assign ^ -1 / format_single_assign,
134-
IfElse = (Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("else") * Body / mark("else"),
135-
IfElseIf = (Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("elseif") * pos(IfCond) * key("then") ^ -1 * Body / mark("elseif"),
134+
IfElse = (Break * EmptyLine ^ 0 * CheckIndent) ^ -1 * key("else") * Body / mark("else"),
135+
IfElseIf = (Break * EmptyLine ^ 0 * CheckIndent) ^ -1 * key("elseif") * pos(IfCond) * key("then") ^ -1 * Body / mark("elseif"),
136136
If = key("if") * IfCond * key("then") ^ -1 * Body * IfElseIf ^ 0 * IfElse ^ -1 / mark("if"),
137137
Unless = key("unless") * IfCond * key("then") ^ -1 * Body * IfElseIf ^ 0 * IfElse ^ -1 / mark("unless"),
138138
While = key("while") * DisableDo * ensure(Exp, PopDo) * key("do") ^ -1 * Body / mark("while"),

moonscript/parse.moon

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ Num = Space * (Num / (v) -> {"number", v})
2727
:Indent, :Cut, :ensure, :extract_line, :mark, :pos, :flatten_or_mark,
2828
:is_assignable, :check_assignable, :format_assign, :format_single_assign,
2929
:sym, :symx, :simple_string, :wrap_func_arg, :join_chain,
30-
:wrap_decorator, :check_lua_string, :self_assign
30+
:wrap_decorator, :check_lua_string, :self_assign, :got
31+
3132
} = require "moonscript.parse.util"
3233

3334

@@ -150,8 +151,8 @@ build_grammar = wrap_env debug_grammar, (root) ->
150151

151152
IfCond: Exp * Assign^-1 / format_single_assign
152153

153-
IfElse: (Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else"
154-
IfElseIf: (Break * CheckIndent)^-1 * EmptyLine^0 * key"elseif" * pos(IfCond) * key"then"^-1 * Body / mark"elseif"
154+
IfElse: (Break * EmptyLine^0 * CheckIndent)^-1 * key"else" * Body / mark"else"
155+
IfElseIf: (Break * EmptyLine^0 * CheckIndent)^-1 * key"elseif" * pos(IfCond) * key"then"^-1 * Body / mark"elseif"
155156

156157
If: key"if" * IfCond * key"then"^-1 * Body * IfElseIf^0 * IfElse^-1 / mark"if"
157158
Unless: key"unless" * IfCond * key"then"^-1 * Body * IfElseIf^0 * IfElse^-1 / mark"unless"

spec/inputs/cond.moon

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,29 @@ a,c,b = "cool" if something
154154

155155

156156

157+
---
158+
159+
j = if 1
160+
if 2
161+
3
162+
else 6
163+
164+
165+
m = if 1
166+
167+
168+
169+
if 2
170+
171+
172+
3
173+
174+
175+
else 6
176+
177+
178+
179+
nil
180+
181+
182+

spec/outputs/cond.lua

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,21 @@ local a = 12
250250
local c, b
251251
if something then
252252
a, c, b = "cool"
253-
end
253+
end
254+
local j
255+
if 1 then
256+
if 2 then
257+
j = 3
258+
end
259+
else
260+
j = 6
261+
end
262+
local m
263+
if 1 then
264+
if 2 then
265+
m = 3
266+
end
267+
else
268+
m = 6
269+
end
270+
return nil

0 commit comments

Comments
 (0)