comparison dmd2/parse.c @ 1526:54b3c1394d62

Merged dmdfe 2.031.
author Robert Clipsham <robert@octarineparrot.com>
date Tue, 07 Jul 2009 02:26:11 +0100
parents 638d16625da2
children e4f7b5d9c68a
comparison
equal deleted inserted replaced
1525:d28cd7c45267 1526:54b3c1394d62
3142 { Statement *s; 3142 { Statement *s;
3143 Token *t; 3143 Token *t;
3144 Condition *condition; 3144 Condition *condition;
3145 Statement *ifbody; 3145 Statement *ifbody;
3146 Statement *elsebody; 3146 Statement *elsebody;
3147 bool isfinal;
3147 Loc loc = this->loc; 3148 Loc loc = this->loc;
3148 3149
3149 //printf("parseStatement()\n"); 3150 //printf("parseStatement()\n");
3150 3151
3151 if (flags & PScurly && token.value != TOKlcurly) 3152 if (flags & PScurly && token.value != TOKlcurly)
3243 goto Lcondition; 3244 goto Lcondition;
3244 } 3245 }
3245 goto Ldeclaration; 3246 goto Ldeclaration;
3246 } 3247 }
3247 3248
3249 case TOKfinal:
3250 if (peekNext() == TOKswitch)
3251 {
3252 nextToken();
3253 isfinal = TRUE;
3254 goto Lswitch;
3255 }
3256 goto Ldeclaration;
3257
3248 CASE_BASIC_TYPES: 3258 CASE_BASIC_TYPES:
3249 case TOKtypedef: 3259 case TOKtypedef:
3250 case TOKalias: 3260 case TOKalias:
3251 case TOKconst: 3261 case TOKconst:
3252 case TOKauto: 3262 case TOKauto:
3253 case TOKextern: 3263 case TOKextern:
3254 case TOKfinal:
3255 case TOKinvariant: 3264 case TOKinvariant:
3256 #if DMDV2 3265 #if DMDV2
3257 case TOKimmutable: 3266 case TOKimmutable:
3258 case TOKshared: 3267 case TOKshared:
3259 case TOKnothrow: 3268 case TOKnothrow:
3649 s = new PragmaStatement(loc, ident, args, body); 3658 s = new PragmaStatement(loc, ident, args, body);
3650 break; 3659 break;
3651 } 3660 }
3652 3661
3653 case TOKswitch: 3662 case TOKswitch:
3654 { Expression *condition; 3663 isfinal = FALSE;
3655 Statement *body; 3664 goto Lswitch;
3656 3665
3666 Lswitch:
3667 {
3657 nextToken(); 3668 nextToken();
3658 check(TOKlparen); 3669 check(TOKlparen);
3659 condition = parseExpression(); 3670 Expression *condition = parseExpression();
3660 check(TOKrparen); 3671 check(TOKrparen);
3661 body = parseStatement(PSscope); 3672 Statement *body = parseStatement(PSscope);
3662 s = new SwitchStatement(loc, condition, body); 3673 s = new SwitchStatement(loc, condition, body, isfinal);
3663 break; 3674 break;
3664 } 3675 }
3665 3676
3666 case TOKcase: 3677 case TOKcase:
3667 { Expression *exp; 3678 { Expression *exp;
3668 Statements *statements; 3679 Statements *statements;
3669 Array cases; // array of Expression's 3680 Array cases; // array of Expression's
3681 Expression *last = NULL;
3670 3682
3671 while (1) 3683 while (1)
3672 { 3684 {
3673 nextToken(); 3685 nextToken();
3674 exp = parseAssignExp(); 3686 exp = parseAssignExp();
3676 if (token.value != TOKcomma) 3688 if (token.value != TOKcomma)
3677 break; 3689 break;
3678 } 3690 }
3679 check(TOKcolon); 3691 check(TOKcolon);
3680 3692
3693 #if DMDV2
3694 /* case exp: .. case last:
3695 */
3696 if (token.value == TOKslice)
3697 {
3698 if (cases.dim > 1)
3699 error("only one case allowed for start of case range");
3700 nextToken();
3701 check(TOKcase);
3702 last = parseAssignExp();
3703 check(TOKcolon);
3704 }
3705 #endif
3706
3681 statements = new Statements(); 3707 statements = new Statements();
3682 while (token.value != TOKcase && 3708 while (token.value != TOKcase &&
3683 token.value != TOKdefault && 3709 token.value != TOKdefault &&
3684 token.value != TOKrcurly) 3710 token.value != TOKrcurly)
3685 { 3711 {
3686 statements->push(parseStatement(PSsemi | PScurlyscope)); 3712 statements->push(parseStatement(PSsemi | PScurlyscope));
3687 } 3713 }
3688 s = new CompoundStatement(loc, statements); 3714 s = new CompoundStatement(loc, statements);
3689 s = new ScopeStatement(loc, s); 3715 s = new ScopeStatement(loc, s);
3690 3716
3691 // Keep cases in order by building the case statements backwards 3717 #if DMDV2
3692 for (int i = cases.dim; i; i--) 3718 if (last)
3693 { 3719 {
3694 exp = (Expression *)cases.data[i - 1]; 3720 s = new CaseRangeStatement(loc, exp, last, s);
3695 s = new CaseStatement(loc, exp, s); 3721 }
3722 else
3723 #endif
3724 {
3725 // Keep cases in order by building the case statements backwards
3726 for (int i = cases.dim; i; i--)
3727 {
3728 exp = (Expression *)cases.data[i - 1];
3729 s = new CaseStatement(loc, exp, s);
3730 }
3696 } 3731 }
3697 break; 3732 break;
3698 } 3733 }
3699 3734
3700 case TOKdefault: 3735 case TOKdefault:
4022 * Determine if the scanner is sitting on the start of a declaration. 4057 * Determine if the scanner is sitting on the start of a declaration.
4023 * Input: 4058 * Input:
4024 * needId 0 no identifier 4059 * needId 0 no identifier
4025 * 1 identifier optional 4060 * 1 identifier optional
4026 * 2 must have identifier 4061 * 2 must have identifier
4062 * Output:
4063 * if *pt is not NULL, it is set to the ending token, which would be endtok
4027 */ 4064 */
4028 4065
4029 int Parser::isDeclaration(Token *t, int needId, enum TOK endtok, Token **pt) 4066 int Parser::isDeclaration(Token *t, int needId, enum TOK endtok, Token **pt)
4030 { 4067 {
4031 //printf("isDeclaration(needId = %d)\n", needId); 4068 //printf("isDeclaration(needId = %d)\n", needId);
4060 } 4097 }
4061 else 4098 else
4062 goto Lisnot; 4099 goto Lisnot;
4063 4100
4064 Lis: 4101 Lis:
4065 //printf("\tis declaration\n"); 4102 //printf("\tis declaration, t = %s\n", t->toChars());
4066 return TRUE; 4103 return TRUE;
4067 4104
4068 Lisnot: 4105 Lisnot:
4069 //printf("\tis not declaration\n"); 4106 //printf("\tis not declaration\n");
4070 return FALSE; 4107 return FALSE;
4355 4392
4356 4393
4357 int Parser::isParameters(Token **pt) 4394 int Parser::isParameters(Token **pt)
4358 { // This code parallels parseParameters() 4395 { // This code parallels parseParameters()
4359 Token *t = *pt; 4396 Token *t = *pt;
4360 int tmp;
4361 4397
4362 //printf("isParameters()\n"); 4398 //printf("isParameters()\n");
4363 if (t->value != TOKlparen) 4399 if (t->value != TOKlparen)
4364 return FALSE; 4400 return FALSE;
4365 4401
4366 t = peek(t); 4402 t = peek(t);
4367 for (;1; t = peek(t)) 4403 for (;1; t = peek(t))
4368 { 4404 {
4405 L1:
4369 switch (t->value) 4406 switch (t->value)
4370 { 4407 {
4371 case TOKrparen: 4408 case TOKrparen:
4372 break; 4409 break;
4373 4410
4378 case TOKin: 4415 case TOKin:
4379 case TOKout: 4416 case TOKout:
4380 case TOKinout: 4417 case TOKinout:
4381 case TOKref: 4418 case TOKref:
4382 case TOKlazy: 4419 case TOKlazy:
4420 case TOKfinal:
4421 continue;
4422
4383 case TOKconst: 4423 case TOKconst:
4384 case TOKinvariant: 4424 case TOKinvariant:
4385 case TOKimmutable: 4425 case TOKimmutable:
4386 case TOKshared: 4426 case TOKshared:
4387 case TOKfinal: 4427 t = peek(t);
4388 continue; 4428 if (t->value == TOKlparen)
4429 {
4430 t = peek(t);
4431 if (!isDeclaration(t, 0, TOKrparen, &t))
4432 return FALSE;
4433 t = peek(t); // skip past closing ')'
4434 goto L2;
4435 }
4436 goto L1;
4389 4437
4390 #if 0 4438 #if 0
4391 case TOKstatic: 4439 case TOKstatic:
4392 continue; 4440 continue;
4393 case TOKauto: 4441 case TOKauto:
4402 } 4450 }
4403 goto L3; 4451 goto L3;
4404 #endif 4452 #endif
4405 4453
4406 default: 4454 default:
4407 if (!isBasicType(&t)) 4455 { if (!isBasicType(&t))
4408 return FALSE; 4456 return FALSE;
4409 tmp = FALSE; 4457 L2:
4458 int tmp = FALSE;
4410 if (t->value != TOKdotdotdot && 4459 if (t->value != TOKdotdotdot &&
4411 !isDeclarator(&t, &tmp, TOKreserved)) 4460 !isDeclarator(&t, &tmp, TOKreserved))
4412 return FALSE; 4461 return FALSE;
4413 if (t->value == TOKassign) 4462 if (t->value == TOKassign)
4414 { t = peek(t); 4463 { t = peek(t);
4418 if (t->value == TOKdotdotdot) 4467 if (t->value == TOKdotdotdot)
4419 { 4468 {
4420 t = peek(t); 4469 t = peek(t);
4421 break; 4470 break;
4422 } 4471 }
4472 }
4423 L3: 4473 L3:
4424 if (t->value == TOKcomma) 4474 if (t->value == TOKcomma)
4425 { 4475 {
4426 continue; 4476 continue;
4427 } 4477 }