@@ -283,28 +283,9 @@ return parseTableSetAfterBrace(e); } if( tryEat("if") ) { - eat("(", "after if"); - auto cond = E(0); - eat(")", "after if condition"); - auto thenPos = lex.front.pos; - eat("{", "after if condition"); - auto th = Body(); - eat("}", "after if-then body"); - auto el = doNothingExpression(); - auto elsePos = (lex.empty ? LexPosition.dummy : lex.front.pos); - if( tryEat("else") ) { - eat("{", "after else"); - el = Body(); - eat("}", "after else body"); - } - return new App(pos, - new Var(pos, "if"), - cond, - new Fun(thenPos, [], th), - new Fun(elsePos, [], el) - ); + return parseIfAfterIf(pos); } if( tryEat("case") ) { return parsePatternMatch(pos); @@ -316,8 +297,33 @@ } scope(exit) lex.popFront; return new Var(pos, lex.front.str); } + + AST parseIfAfterIf(LexPosition pos) + { + eat("(", "after if"); + auto cond = E(0); + eat(")", "after if condition"); + auto thenPos = lex.front.pos; + AST th; + if( tryEat("{") ) { + th = Body(); + eat("}", "after if-then body"); + } else { + th = E(0); + } + auto el = doNothingExpression(); + auto elsePos = (lex.empty ? LexPosition.dummy : lex.front.pos); + if( tryEat("else") ) + if( tryEat("{") ) { + el = Body(); + eat("}", "after else body"); + } else { + el = E(0); + } + return new App(pos, new Var(pos,"if"), cond, new Fun(thenPos,[],th), new Fun(elsePos,[],el)); + } AST parsePatternMatch(LexPosition pos) { // case "(" pmExpr ")" CASES @@ -667,5 +673,16 @@ case( 1 ) when({aaaa:@value(x)}){1} when({aaaa:{bbb:_}, ccc:123}){1} `)); -} +} + +unittest +{ + // test for omitting { .. } + assert_nothrow(parseString(` + if(1) 2 else 3 + `)); + assert_nothrow(parseString(` + if(1) x{y:z} else 3 + `)); +}