diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 706d2e5..e4fec26 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -26,6 +26,8 @@ end local R, S, V, P = lpeg.R, lpeg.S, lpeg.V, lpeg.P local C, Ct, Cmt, Cg, Cb, Cc = lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cg, lpeg.Cb, lpeg.Cc +lpeg.setmaxstack(3000) + local White = S" \t\n"^0 local _Space = S" \t"^0 local Break = S"\n" @@ -159,7 +161,7 @@ local build_grammar = wrap(function() -- makes sure the last item in a chain is an index - local _assignable = { index = true, dot = true} + local _assignable = { index = true, dot = true, slice = true } local function check_assignable(str, pos, value) if ntype(value) == "chain" and _assignable[ntype(value[#value])] or type(value) == "string" @@ -256,9 +258,12 @@ local build_grammar = wrap(function() Assignable = Cmt(Chain, check_assignable) + Name, AssignableList = Assignable * (sym"," * Assignable)^0, - Exp = Ct(Factor * (OtherOps * Factor)^0) / flatten_or_mark"exp", - Factor = Ct(Term * (FactorOp * Term)^0) / flatten_or_mark"exp", - Term = Ct(Value * (TermOp * Value)^0) / flatten_or_mark"exp", + + Exp = Ct(Value * ((OtherOps + FactorOp + TermOp) * Value)^0) / flatten_or_mark"exp", + + -- Exp = Ct(Factor * (OtherOps * Factor)^0) / flatten_or_mark"exp", + -- Factor = Ct(Term * (FactorOp * Term)^0) / flatten_or_mark"exp", + -- Term = Ct(Value * (TermOp * Value)^0) / flatten_or_mark"exp", Value = sym"-" * Exp / mark"minus" + @@ -271,6 +276,7 @@ local build_grammar = wrap(function() Assign + FunLit + String + (Chain + Callable) * Ct(ExpList^0) / flatten_func + Num, + String = Space * DoubleString + Space * SingleString + LuaString, SingleString = simple_string("'"), DoubleString = simple_string('"'), @@ -296,10 +302,13 @@ local build_grammar = wrap(function() ChainItem = Invoke + + Slice + symx"[" * Exp/mark"index" * sym"]" + symx"." * _Name/mark"dot" + ColonCall, + Slice = symx"[" * Num * sym":" * Num * (sym":" * Num)^-1 *sym"]" / mark"slice", + ColonCall = symx":" * (_Name * Invoke) / mark"colon", Invoke = FnArgs/mark"call" + SingleString / wrap_func_arg + diff --git a/moonscript/util.lua b/moonscript/util.lua index 3e6db7a..380a7fd 100644 --- a/moonscript/util.lua +++ b/moonscript/util.lua @@ -18,6 +18,20 @@ function map(tbl, fn) return out end +function every(tbl, fn) + for i=1,#tbl do + local pass + if fn then + pass = fn(tbl[i]) + else + pass = tbl[i] + end + + if not pass then return false end + end + return true +end + function bind(obj, name) return function(...) return obj[name](obj, ...)