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 if( tryEat("{") ) 280 if( tryEat("{") )
281 { 281 {
282 AST e = new App(pos, new Var(pos,"{}")); 282 AST e = new App(pos, new Var(pos,"{}"));
283 return parseTableSetAfterBrace(e); 283 return parseTableSetAfterBrace(e);
284 } 284 }
285 if( tryEat("if") ) 285 if( tryEat("if") )
286 { 286 {
287 eat("(", "after if"); | 287 return parseIfAfterIf(pos);
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.fron <
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 ); <
307 } 288 }
308 if( tryEat("case") ) 289 if( tryEat("case") )
309 { 290 {
310 return parsePatternMatch(pos); 291 return parsePatternMatch(pos);
311 } 292 }
312 if( tryEat("fun") || tryEat("\u03BB") ) // lambda!! 293 if( tryEat("fun") || tryEat("\u03BB") ) // lambda!!
313 { 294 {
314 eat("(", "after fun"); 295 eat("(", "after fun");
315 return parseLambdaAfterOpenParen(pos); 296 return parseLambdaAfterOpenParen(pos);
316 } 297 }
317 scope(exit) lex.popFront; 298 scope(exit) lex.popFront;
318 return new Var(pos, lex.front.str); 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,[],
> 325 }
320 326
321 AST parsePatternMatch(LexPosition pos) 327 AST parsePatternMatch(LexPosition pos)
322 { 328 {
323 // case "(" pmExpr ")" CASES 329 // case "(" pmExpr ")" CASES
324 //==> 330 //==>
325 // let pmVar = pmExpr in (... let pmTryFirst = ... in pmTryFir 331 // let pmVar = pmExpr in (... let pmTryFirst = ... in pmTryFir
326 eat("(", "after case"); 332 eat("(", "after case");
................................................................................................................................................................................
665 `)); 671 `));
666 assert_nothrow(parseString(` 672 assert_nothrow(parseString(`
667 case( 1 ) 673 case( 1 )
668 when({aaaa:@value(x)}){1} 674 when({aaaa:@value(x)}){1}
669 when({aaaa:{bbb:_}, ccc:123}){1} 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 }