Differences From Artifact [64474993b2d6bb44]:
- File
polemy/parse.d
- 2010-11-24 03:30:56 - part of checkin [20be503cae] on branch trunk - set up referece manual (user: kinaba) [annotate]
To Artifact [6c61d557752efef5]:
- File
polemy/parse.d
- 2010-11-24 11:20:42 - part of checkin [153a14cec0] on branch trunk - if-then-else without {}s. some cosmetic changes (user: kinaba) [annotate]
280 280 if( tryEat("{") )
281 281 {
282 282 AST e = new App(pos, new Var(pos,"{}"));
283 283 return parseTableSetAfterBrace(e);
284 284 }
285 285 if( tryEat("if") )
286 286 {
287 - eat("(", "after if");
288 - auto cond = E(0);
289 - eat(")", "after if condition");
290 - auto thenPos = lex.front.pos;
291 - eat("{", "after if condition");
292 - auto th = Body();
293 - eat("}", "after if-then body");
294 - auto el = doNothingExpression();
295 - auto elsePos = (lex.empty ? LexPosition.dummy : lex.front.pos);
296 - if( tryEat("else") ) {
297 - eat("{", "after else");
298 - el = Body();
299 - eat("}", "after else body");
300 - }
301 - return new App(pos,
302 - new Var(pos, "if"),
303 - cond,
304 - new Fun(thenPos, [], th),
305 - new Fun(elsePos, [], el)
306 - );
287 + return parseIfAfterIf(pos);
307 288 }
308 289 if( tryEat("case") )
309 290 {
310 291 return parsePatternMatch(pos);
311 292 }
312 293 if( tryEat("fun") || tryEat("\u03BB") ) // lambda!!
313 294 {
314 295 eat("(", "after fun");
315 296 return parseLambdaAfterOpenParen(pos);
316 297 }
317 298 scope(exit) lex.popFront;
318 299 return new Var(pos, lex.front.str);
319 300 }
301 +
302 + AST parseIfAfterIf(LexPosition pos)
303 + {
304 + eat("(", "after if");
305 + auto cond = E(0);
306 + eat(")", "after if condition");
307 + auto thenPos = lex.front.pos;
308 + AST th;
309 + if( tryEat("{") ) {
310 + th = Body();
311 + eat("}", "after if-then body");
312 + } else {
313 + th = E(0);
314 + }
315 + auto el = doNothingExpression();
316 + auto elsePos = (lex.empty ? LexPosition.dummy : lex.front.pos);
317 + if( tryEat("else") )
318 + if( tryEat("{") ) {
319 + el = Body();
320 + eat("}", "after else body");
321 + } else {
322 + el = E(0);
323 + }
324 + return new App(pos, new Var(pos,"if"), cond, new Fun(thenPos,[],th), new Fun(elsePos,[],el));
325 + }
320 326
321 327 AST parsePatternMatch(LexPosition pos)
322 328 {
323 329 // case "(" pmExpr ")" CASES
324 330 //==>
325 331 // let pmVar = pmExpr in (... let pmTryFirst = ... in pmTryFirst())
326 332 eat("(", "after case");
................................................................................
665 671 `));
666 672 assert_nothrow(parseString(`
667 673 case( 1 )
668 674 when({aaaa:@value(x)}){1}
669 675 when({aaaa:{bbb:_}, ccc:123}){1}
670 676 `));
671 677 }
678 +
679 +unittest
680 +{
681 + // test for omitting { .. }
682 + assert_nothrow(parseString(`
683 + if(1) 2 else 3
684 + `));
685 + assert_nothrow(parseString(`
686 + if(1) x{y:z} else 3
687 + `));
688 +}