Skip to content

Commit 53f5870

Browse files
committed
support for indented unbound tables as last argument to function calls
1 parent e797114 commit 53f5870

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

moonscript/parse.lua

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ local Shebang = P"#!" * P(1 - Stop)^0
5959
-- can't have P(false) because it causes preceding patterns not to run
6060
local Cut = P(function() return false end)
6161

62+
local function ensure(patt, finally)
63+
return patt * finally + finally * Cut
64+
end
65+
6266
-- auto declare Proper variables with lpeg.V
6367
local function wrap_env(fn)
6468
local env = getfenv(fn)
@@ -224,7 +228,8 @@ local build_grammar = wrap_env(function()
224228
end
225229

226230
local function advance_indent(str, pos, indent)
227-
if indent > _indent:top() then
231+
local top = _indent:top()
232+
if top ~= -1 and indent > _indent:top() then
228233
_indent:push(indent)
229234
return true
230235
end
@@ -283,6 +288,7 @@ local build_grammar = wrap_env(function()
283288

284289
Advance = #Cmt(Indent, advance_indent), -- Advances the indent, gives back whitespace for CheckIndent
285290
PushIndent = Cmt(Indent, push_indent),
291+
PreventIndent = Cmt(Cc(-1), push_indent),
286292
PopIndent = Cmt("", pop_indent),
287293
InBlock = Advance * Block * PopIndent,
288294

@@ -420,9 +426,9 @@ local build_grammar = wrap_env(function()
420426

421427
-- the unbounded table
422428
TableBlockInner = Ct(KeyValueLine * (SpaceBreak^1 * KeyValueLine)^0),
423-
TableBlock = SpaceBreak^1 * Advance * TableBlockInner * PopIndent / mark"table",
429+
TableBlock = SpaceBreak^1 * Advance * ensure(TableBlockInner, PopIndent) / mark"table",
424430

425-
ClassDecl = key"class" * Name * (key"extends" * Exp + C"")^-1 * ClassBlock / mark"class",
431+
ClassDecl = key"class" * Name * (key"extends" * PreventIndent * ensure(Exp, PopIndent) + C"")^-1 * ClassBlock / mark"class",
426432

427433
ClassBlock = SpaceBreak^1 * Advance *
428434
Ct(ClassLine * (SpaceBreak^1 * ClassLine)^0) * PopIndent,
@@ -455,7 +461,7 @@ local build_grammar = wrap_env(function()
455461
ExpList = Exp * (sym"," * Exp)^0,
456462
ExpListLow = Exp * ((sym"," + sym";") * Exp)^0,
457463

458-
InvokeArgs = ExpList * (sym"," * SpaceBreak * Advance * ArgBlock)^-1,
464+
InvokeArgs = ExpList * (sym"," * (TableBlock + SpaceBreak * Advance * ArgBlock * TableBlock^-1) + TableBlock)^-1 + TableBlock,
459465
ArgBlock = ArgLine * (sym"," * SpaceBreak * ArgLine)^0 * PopIndent,
460466
ArgLine = CheckIndent * ExpList
461467
}

tests/inputs/syntax.moon

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,32 @@ y = ifsomething
192192
z = x and b
193193
z = x andb
194194

195+
196+
-- undelimited tables
197+
198+
while 10 > something
199+
something: "world"
200+
print "yeah"
201+
202+
x =
203+
okay: sure
204+
205+
yeah
206+
okay: man
207+
sure: sir
208+
209+
hello "no comma"
210+
yeah: dada
211+
another: world
212+
213+
hello "comma",
214+
something: hello_world
215+
frick: you
216+
217+
-- creates two tables
218+
another hello, one,
219+
two, three, four, yeah: man
220+
okay: yeah
221+
222+
195223
-- cooool

tests/outputs/syntax.lua

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,29 @@ end
191191
x = notsomething
192192
y = ifsomething
193193
local z = x and b
194-
z = x(andb)
194+
z = x(andb)
195+
while 10 > something({
196+
something = "world"
197+
}) do
198+
print("yeah")
199+
end
200+
x = {
201+
okay = sure
202+
}
203+
yeah({
204+
okay = man,
205+
sure = sir
206+
})
207+
hello("no comma", {
208+
yeah = dada,
209+
another = world
210+
})
211+
hello("comma", {
212+
something = hello_world,
213+
frick = you
214+
})
215+
another(hello, one, two, three, four, {
216+
yeah = man
217+
}, {
218+
okay = yeah
219+
})

0 commit comments

Comments
 (0)