mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
reorganize update and assign parsing
This commit is contained in:
parent
6c0ec441d5
commit
7f3f17a8e4
@ -175,6 +175,22 @@ local function check_assignable(str, pos, value)
|
||||
return false
|
||||
end
|
||||
|
||||
local flatten_explist = flatten_or_mark"explist"
|
||||
local function format_assign(lhs_exps, assign)
|
||||
if not assign then
|
||||
return flatten_explist(lhs_exps)
|
||||
end
|
||||
|
||||
local t = ntype(assign)
|
||||
if t == "assign" then
|
||||
return {"assign", lhs_exps, unpack(assign, 2)}
|
||||
elseif t == "update" then
|
||||
return {"update", lhs_exps[1], unpack(assign, 2)}
|
||||
end
|
||||
|
||||
error "unknown assign expression"
|
||||
end
|
||||
|
||||
local function sym(chars)
|
||||
return Space * chars
|
||||
end
|
||||
@ -335,9 +351,9 @@ local build_grammar = wrap_env(function()
|
||||
Line = (CheckIndent * Statement + Space * #Stop),
|
||||
|
||||
Statement = (
|
||||
Import + While + With + For + ForEach + Switch + Return + ClassDecl + Local +
|
||||
Export + BreakLoop + Assign + Update +
|
||||
Ct(ExpList) / flatten_or_mark"explist"
|
||||
Import + While + With + For + ForEach + Switch + Return + ClassDecl +
|
||||
Local + Export + BreakLoop +
|
||||
Ct(ExpList) * (Update + Assign)^-1 / format_assign
|
||||
) * Space * ((
|
||||
-- statement decorators
|
||||
key"if" * Exp * (key"else" * Exp)^-1 * Space / mark"if" +
|
||||
@ -392,8 +408,8 @@ local build_grammar = wrap_env(function()
|
||||
CompFor = key"for" * Ct(NameList) * key"in" * (sym"*" * Exp / mark"unpack" + Exp) / mark"for",
|
||||
CompClause = CompFor + key"when" * Exp / mark"when",
|
||||
|
||||
Assign = Ct(AssignableList) * sym"=" * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark"assign",
|
||||
Update = Assignable * ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=")/trim) * Exp / mark"update",
|
||||
Assign = sym"=" * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark"assign",
|
||||
Update = ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=") / trim) * Exp / mark"update",
|
||||
|
||||
-- we can ignore precedence for now
|
||||
OtherOps = op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op".." + op"<" + op">",
|
||||
|
Loading…
Reference in New Issue
Block a user