From 8ea934964aecbeba9343129861386daa91d1ee22 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 13:26:29 -0800 Subject: [PATCH] allow whitespace/newlines after binary operator --- moonscript/parse.lua | 10 +++------- spec/inputs/operators.moon | 33 +++++++++++++++++++++++++++++++++ spec/outputs/operators.lua | 13 +++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 spec/inputs/operators.moon create mode 100644 spec/outputs/operators.lua diff --git a/moonscript/parse.lua b/moonscript/parse.lua index b26562d..726dda7 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -436,16 +436,12 @@ local build_grammar = wrap_env(function() Assign = sym"=" * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark"assign", Update = ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=" + sym"or=" + sym"and=") / trim) * Exp / mark"update", - -- we can ignore precedence for now + -- we don't need to parse precedence, lua will handle it for us OtherOps = op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op".." + op"<" + op">", + BinaryOperator = (OtherOps + FactorOp + TermOp) * SpaceBreak^0, Assignable = Cmt(DotChain + Chain, check_assignable) + Name + SelfName, - - 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", + Exp = Ct(Value * (BinaryOperator * Value)^0) / flatten_or_mark"exp", SimpleValue = If + Unless + diff --git a/spec/inputs/operators.moon b/spec/inputs/operators.moon new file mode 100644 index 0000000..742ad7f --- /dev/null +++ b/spec/inputs/operators.moon @@ -0,0 +1,33 @@ + +-- +x = 1 + 3 + +y = 1 + + 3 + +z = 1 + + 3 + + 4 + +-- + +k = b and c and + g + + +h = thing and + -> + print "hello world" + +-- TODO: should fail, indent still set to previous line so it thinks body is +-- indented +i = thing or + -> + print "hello world" + +p = thing and + -> +print "hello world" + +s = thing or + -> and 234 diff --git a/spec/outputs/operators.lua b/spec/outputs/operators.lua new file mode 100644 index 0000000..2c2a956 --- /dev/null +++ b/spec/outputs/operators.lua @@ -0,0 +1,13 @@ +local x = 1 + 3 +local y = 1 + 3 +local z = 1 + 3 + 4 +local k = b and c and g +local h = thing and function() + return print("hello world") +end +local i = thing or function() + return print("hello world") +end +local p = thing and function() end +print("hello world") +local s = thing or function() end and 234 \ No newline at end of file