Differences From Artifact [f6a85fc83dd90cb0]:
- File
polemy/parse.d
- 2010-11-24 12:14:00 - part of checkin [3ae09b8cbf] on branch trunk - changed if-then-else syntax (user: kinaba) [annotate]
To Artifact [38516f68b159d6bd]:
- File
polemy/parse.d
- 2010-11-24 13:22:04 - part of checkin [f9c31f3cd8] on branch trunk - Fixed the null dereference bug when directly wrote "case 1 when 2: 3" in REPL. It was due to null LexPosition in the AST. Now AST.pos !is null is an invariant of AST. (user: kinaba) [annotate]
248 248 }
249 249
250 250 AST BaseExpression()
251 251 {
252 252 if( lex.empty )
253 253 throw genex!UnexpectedEOF(currentPosition(), "Reached EOF when tried to parse an expression");
254 254
255 - auto pos = lex.front.pos;
255 + auto pos = currentPosition();
256 256 if( lex.front.quoted )
257 257 {
258 258 scope(exit) lex.popFront;
259 259 return new Str(pos, lex.front.str);
260 260 }
261 261 if( isNumber(lex.front.str) )
262 262 {
................................................................................
321 321 {
322 322 // case pmExpr CASES
323 323 //==>
324 324 // let pmVar = pmExpr in (... let pmTryFirst = ... in pmTryFirst())
325 325 AST pmExpr = E(0);
326 326 string pmVar = freshVarName();
327 327 string pmTryFirst = freshVarName();
328 - AST pmBody = parsePatternMatchCases(pmVar, pmTryFirst,
328 + AST pmBody = parsePatternMatchCases(pos, pmVar, pmTryFirst,
329 329 new App(pos, new Var(pos, pmTryFirst)));
330 330 return new Let(pos, pmVar, [], pmExpr, pmBody);
331 331 }
332 332
333 - AST parsePatternMatchCases(string pmVar, string tryThisBranchVar, AST thenDoThis)
333 + AST parsePatternMatchCases(LexPosition casePos, string pmVar, string tryThisBranchVar, AST thenDoThis)
334 334 {
335 335 // when pat: cBody
336 336 //==>
337 337 // ... let failBranchVar = ... in
338 338 // let tryThisBranchVar = fun(){ if(test){cBody}else{failBranchVar()} } in thenDoThis
339 339 if( tryEat("when") )
340 340 {
................................................................................
343 343
344 344 auto pr = parsePattern();
345 345 eat(":", "after when pattern");
346 346 AST cBody = E(0);
347 347 AST judgement = new App(pos, new Var(pos, "if"),
348 348 ppTest(pmVar, pr), new Fun(pos,[],ppBind(pmVar, pr, cBody)),
349 349 new Var(pos, failBranchVar));
350 - return parsePatternMatchCases(pmVar, failBranchVar,
350 + return parsePatternMatchCases(casePos, pmVar, failBranchVar,
351 351 new Let(pos, tryThisBranchVar, [],
352 352 new Fun(pos,[],judgement), thenDoThis)
353 353 );
354 354 }
355 355 else
356 356 {
357 - auto pos = currentPosition();
358 - AST doNothing = new Fun(pos,[],
359 - new Str(pos, sprintf!"(pattern match failure:%s)"(pos)));
360 - return new Let(currentPosition(), tryThisBranchVar, [], doNothing, thenDoThis);
357 + AST doNothing = new Fun(casePos,[],
358 + new Str(casePos, sprintf!"(pattern match failure:%s)"(casePos)));
359 + return new Let(casePos, tryThisBranchVar, [], doNothing, thenDoThis);
361 360 }
362 361 }
363 362
364 363 // hageshiku tenuki
365 364 abstract class SinglePattern
366 365 {
367 366 string[] path;
................................................................................
555 554 AST doNothingExpression()
556 555 {
557 556 return new Str(currentPosition(), "(empty function body)");
558 557 }
559 558
560 559 LexPosition currentPosition()
561 560 {
562 - return lex.empty ? null : lex.front.pos;
561 + return lex.empty ? new LexPosition("EOF",0,0) : lex.front.pos;
563 562 }
564 563 }
565 564
566 565 unittest
567 566 {
568 567 mixin EasyAST;
569 568