- Timestamp:
- Sep 20, 2001, 4:08:10 PM (24 years ago)
- Location:
- branches/stage2/tools/database/occ
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stage2/tools/database/occ/parse.cpp
r6768 r6774 34 34 35 35 #if defined(_PARSE_VCC) 36 #define _MSC_VER 110036 #define _MSC_VER 1100 37 37 #endif 38 38 … … 48 48 { 49 49 if(where != nil){ 50 Ptree* head = where->Ca_ar();51 if(head != nil)52 ShowMessageHead(head->GetPosition());50 Ptree* head = where->Ca_ar(); 51 if(head != nil) 52 ShowMessageHead(head->GetPosition()); 53 53 } 54 54 55 55 cerr << msg; 56 56 if(name != nil) 57 name->Write(cerr);57 name->Write(cerr); 58 58 59 59 cerr << '\n'; … … 64 64 { 65 65 if(where != nil){ 66 Ptree* head = where->Ca_ar();67 if(head != nil)68 ShowMessageHead(head->GetPosition());66 Ptree* head = where->Ca_ar(); 67 if(head != nil) 68 ShowMessageHead(head->GetPosition()); 69 69 } 70 70 71 71 cerr << "warning: " << msg; 72 72 if(name != nil) 73 name->Write(cerr);73 name->Write(cerr); 74 74 75 75 cerr << '\n'; … … 87 87 cerr << "parse error before `"; 88 88 if(t.kind != '\0') 89 for(i = 0; i < t.len; ++i)90 cerr << t.ptr[i];89 for(i = 0; i < t.len; ++i) 90 cerr << t.ptr[i]; 91 91 92 92 if(t2.kind != '\0'){ 93 cerr << ' ';94 for(i = 0; i < t2.len; ++i)95 cerr << t2.ptr[i];93 cerr << ' '; 94 for(i = 0; i < t2.len; ++i) 95 cerr << t2.ptr[i]; 96 96 } 97 97 … … 100 100 } 101 101 102 #ifdef SDS 102 103 uint Parser::LineNumber(char* pos, char*& fname, int& fname_len) 103 104 { 105 int col_num, abs_len; 106 107 return LineNumber(pos, fname, fname_len, col_num, abs_len); 108 } 109 #endif 110 111 #ifdef SDS 112 uint Parser::LineNumber(char* pos, char*& fname, int& fname_len, int& col_num, int& abs_len) 113 #else 114 uint Parser::LineNumber(char* pos, char*& fname, int& fname_len) 115 #endif 116 { 117 #ifdef SDS 118 uint line_number = lex->LineNumber(pos, fname, fname_len, col_num, abs_len); 119 #else 104 120 uint line_number = lex->LineNumber(pos, fname, fname_len); 121 #endif 105 122 if(fname_len > 1){ 106 if(fname[0] == '"') {107 ++fname;108 --fname_len;109 }110 111 if(fname[fname_len - 1] == '"')112 --fname_len;123 if(fname[0] == '"') { 124 ++fname; 125 --fname_len; 126 } 127 128 if(fname[fname_len - 1] == '"') 129 --fname_len; 113 130 } 114 131 … … 124 141 int i = 0; 125 142 while(i < fname_len) 126 cerr << fname[i++];143 cerr << fname[i++]; 127 144 128 145 #if defined(_MSC_VER) … … 136 153 { 137 154 while(lex->LookAhead(0) != '\0') 138 if(rDefinition(def))139 return TRUE;140 else{141 Token tk;142 if(!SyntaxError())143 return FALSE;// too many errors144 145 SkipTo(';');146 lex->GetToken(tk);// ignore ';'147 }155 if(rDefinition(def)) 156 return TRUE; 157 else{ 158 Token tk; 159 if(!SyntaxError()) 160 return FALSE; // too many errors 161 162 SkipTo(';'); 163 lex->GetToken(tk); // ignore ';' 164 } 148 165 149 166 return FALSE; … … 167 184 int t = lex->LookAhead(0); 168 185 if(t == ';') 169 res = rNullDeclaration(p);186 res = rNullDeclaration(p); 170 187 else if(t == TYPEDEF) 171 res = rTypedef(p);188 res = rTypedef(p); 172 189 else if(t == TEMPLATE) 173 res = rTemplateDecl(p);190 res = rTemplateDecl(p); 174 191 else if(t == METACLASS) 175 res = rMetaclassDecl(p);192 res = rMetaclassDecl(p); 176 193 else if(t == EXTERN && lex->LookAhead(1) == StringL) 177 res = rLinkageSpec(p);194 res = rLinkageSpec(p); 178 195 else if(t == EXTERN && lex->LookAhead(1) == TEMPLATE) 179 res = rExternTemplateDecl(p);196 res = rExternTemplateDecl(p); 180 197 else if(t == NAMESPACE) 181 res = rNamespaceSpec(p);198 res = rNamespaceSpec(p); 182 199 else if(t == USING) 183 res = rUsing(p);200 res = rUsing(p); 184 201 else { 185 Ptree* c = lex->GetComments2();186 if (res = rDeclaration(p))187 Walker::SetDeclaratorComments(p, c);202 Ptree* c = lex->GetComments2(); 203 if (res = rDeclaration(p)) 204 Walker::SetDeclaratorComments(p, c); 188 205 } 189 206 … … 197 214 198 215 if(lex->GetToken(tk) != ';') 199 return FALSE;216 return FALSE; 200 217 201 218 decl = new PtreeDeclaration(nil, Ptree::List(nil, new Leaf(tk))); … … 214 231 215 232 if(lex->GetToken(tk) != TYPEDEF) 216 return FALSE;233 return FALSE; 217 234 218 235 def = new PtreeTypedef(new LeafReserved(tk)); 219 236 if(!rTypeSpecifier(type_name, FALSE, type_encode)) 220 return FALSE;237 return FALSE; 221 238 222 239 def = Ptree::Snoc(def, type_name); 223 240 if(!rDeclarators(decl, type_encode, TRUE)) 224 return FALSE;241 return FALSE; 225 242 226 243 if(lex->GetToken(tk) != ';') 227 return FALSE;244 return FALSE; 228 245 229 246 def = Ptree::Nconc(def, Ptree::List(decl, new Leaf(tk))); … … 240 257 241 258 if(!optCvQualify(cv_q) || !optIntegralTypeOrClassSpec(tspec, encode)) 242 return FALSE;259 return FALSE; 243 260 244 261 if(tspec == nil){ 245 if(check){246 Token tk;247 lex->LookAhead(0, tk);248 if(!MaybeTypeNameOrClassTemplate(tk))249 return FALSE;250 }251 252 if(!rName(tspec, encode))253 return FALSE;262 if(check){ 263 Token tk; 264 lex->LookAhead(0, tk); 265 if(!MaybeTypeNameOrClassTemplate(tk)) 266 return FALSE; 267 } 268 269 if(!rName(tspec, encode)) 270 return FALSE; 254 271 } 255 272 256 273 if(!optCvQualify(cv_q2)) 257 return FALSE;274 return FALSE; 258 275 259 276 if(cv_q != nil){ 260 tspec = Ptree::Snoc(cv_q, tspec);261 if(cv_q2 != nil)262 tspec = Ptree::Nconc(tspec, cv_q2);277 tspec = Ptree::Snoc(cv_q, tspec); 278 if(cv_q2 != nil) 279 tspec = Ptree::Nconc(tspec, cv_q2); 263 280 } 264 281 else if(cv_q2 != nil) 265 tspec = Ptree::Cons(tspec, cv_q2);282 tspec = Ptree::Cons(tspec, cv_q2); 266 283 267 284 encode.CvQualify(cv_q, cv_q2); … … 284 301 #endif 285 302 ) 286 return TRUE;303 return TRUE; 287 304 else 288 return FALSE;305 return FALSE; 289 306 } 290 307 … … 297 314 metaclass <metaclass> <class>(...); 298 315 metaclass <metaclass>; 299 metaclass <class> : <metaclass>(...); // for backward compatibility316 metaclass <class> : <metaclass>(...); // for backward compatibility 300 317 */ 301 318 bool Parser::rMetaclassDecl(Ptree*& decl) … … 306 323 307 324 if(lex->GetToken(tk1) != METACLASS) 308 return FALSE;325 return FALSE; 309 326 310 327 if(lex->GetToken(tk2) != Identifier) 311 return FALSE;328 return FALSE; 312 329 313 330 t = lex->GetToken(tk3); 314 331 if(t == Identifier){ 315 metaclass_name = new Leaf(tk2);316 decl = new PtreeMetaclassDecl(new LeafReserved(tk1),317 Ptree::List(metaclass_name,318 new Leaf(tk3)));332 metaclass_name = new Leaf(tk2); 333 decl = new PtreeMetaclassDecl(new LeafReserved(tk1), 334 Ptree::List(metaclass_name, 335 new Leaf(tk3))); 319 336 } 320 337 else if(t == ':'){ 321 if(lex->GetToken(tk4) != Identifier)322 return FALSE;323 324 metaclass_name = new Leaf(tk4);325 decl = new PtreeMetaclassDecl(new LeafReserved(tk1),326 Ptree::List(metaclass_name,327 new Leaf(tk2)));338 if(lex->GetToken(tk4) != Identifier) 339 return FALSE; 340 341 metaclass_name = new Leaf(tk4); 342 decl = new PtreeMetaclassDecl(new LeafReserved(tk1), 343 Ptree::List(metaclass_name, 344 new Leaf(tk2))); 328 345 } 329 346 else if(t == ';'){ 330 metaclass_name = new Leaf(tk2);331 decl = new PtreeMetaclassDecl(new LeafReserved(tk1),332 Ptree::List(metaclass_name, nil,333 new Leaf(tk3)));334 Metaclass::Load(metaclass_name);335 return TRUE;347 metaclass_name = new Leaf(tk2); 348 decl = new PtreeMetaclassDecl(new LeafReserved(tk1), 349 Ptree::List(metaclass_name, nil, 350 new Leaf(tk3))); 351 Metaclass::Load(metaclass_name); 352 return TRUE; 336 353 } 337 354 else 338 return FALSE;355 return FALSE; 339 356 340 357 t = lex->GetToken(tk1); 341 358 if(t == '('){ 342 Ptree* args;343 if(!rMetaArguments(args))344 return FALSE;345 346 if(lex->GetToken(tk2) != ')')347 return FALSE;348 349 decl = Ptree::Nconc(decl, Ptree::List(new Leaf(tk1), args,350 new Leaf(tk2)));351 t = lex->GetToken(tk1);359 Ptree* args; 360 if(!rMetaArguments(args)) 361 return FALSE; 362 363 if(lex->GetToken(tk2) != ')') 364 return FALSE; 365 366 decl = Ptree::Nconc(decl, Ptree::List(new Leaf(tk1), args, 367 new Leaf(tk2))); 368 t = lex->GetToken(tk1); 352 369 } 353 370 354 371 if(t == ';'){ 355 decl = Ptree::Snoc(decl, new Leaf(tk1));356 Metaclass::Load(metaclass_name);357 return TRUE;372 decl = Ptree::Snoc(decl, new Leaf(tk1)); 373 Metaclass::Load(metaclass_name); 374 return TRUE; 358 375 } 359 376 else 360 return FALSE;377 return FALSE; 361 378 } 362 379 … … 372 389 args = nil; 373 390 for(;;){ 374 t = lex->LookAhead(0);375 if(t == '\0')376 return FALSE;377 else if(t == '(')378 ++n;379 else if(t == ')')380 if(--n <= 0)381 return TRUE;382 383 lex->GetToken(tk);384 args = Ptree::Snoc(args, new Leaf(tk));391 t = lex->LookAhead(0); 392 if(t == '\0') 393 return FALSE; 394 else if(t == '(') 395 ++n; 396 else if(t == ')') 397 if(--n <= 0) 398 return TRUE; 399 400 lex->GetToken(tk); 401 args = Ptree::Snoc(args, new Leaf(tk)); 385 402 } 386 403 } … … 397 414 398 415 if(lex->GetToken(tk1) != EXTERN) 399 return FALSE;416 return FALSE; 400 417 401 418 if(lex->GetToken(tk2) != StringL) 402 return FALSE;419 return FALSE; 403 420 404 421 spec = new PtreeLinkageSpec(new LeafEXTERN(tk1), 405 Ptree::List(new Leaf(tk2)));422 Ptree::List(new Leaf(tk2))); 406 423 if(lex->LookAhead(0) == '{'){ 407 if(!rLinkageBody(body))408 return FALSE;424 if(!rLinkageBody(body)) 425 return FALSE; 409 426 } 410 427 else 411 if(!rDefinition(body))412 return FALSE;428 if(!rDefinition(body)) 429 return FALSE; 413 430 414 431 spec = Ptree::Snoc(spec, body); … … 428 445 429 446 if(lex->GetToken(tk1) != NAMESPACE) 430 return FALSE;447 return FALSE; 431 448 432 449 if(lex->LookAhead(0) == '{') 433 name = nil;450 name = nil; 434 451 else 435 if(lex->GetToken(tk2) == Identifier)436 name = new Leaf(tk2);437 else438 return FALSE;452 if(lex->GetToken(tk2) == Identifier) 453 name = new Leaf(tk2); 454 else 455 return FALSE; 439 456 440 457 if(lex->LookAhead(0) == '{'){ 441 if(!rLinkageBody(body))442 return FALSE;458 if(!rLinkageBody(body)) 459 return FALSE; 443 460 } 444 461 else 445 if(!rDefinition(body))446 return FALSE;462 if(!rDefinition(body)) 463 return FALSE; 447 464 448 465 spec = new PtreeNamespaceSpec(new LeafNAMESPACE(tk1), 449 Ptree::List(name, body));466 Ptree::List(name, body)); 450 467 return TRUE; 451 468 } … … 462 479 463 480 if(lex->GetToken(tk) != USING) 464 return FALSE;481 return FALSE; 465 482 466 483 Ptree* using0 = new LeafUSING(tk); 467 484 Ptree* using1; 468 485 if (lex->LookAhead(0) != NAMESPACE) 469 using1 = nil;486 using1 = nil; 470 487 else { 471 lex->GetToken(tk);472 using1 = new LeafNAMESPACE(tk);488 lex->GetToken(tk); 489 using1 = new LeafNAMESPACE(tk); 473 490 } 474 491 475 492 if (!rName(name, encode)) 476 return FALSE;493 return FALSE; 477 494 478 495 if (lex->GetToken(tk) != ';') 479 return FALSE;496 return FALSE; 480 497 481 498 decl = new PtreeUsing(using0, using1, name, encode, new Leaf(tk)); … … 495 512 496 513 if(lex->GetToken(op) != '{') 497 return FALSE;514 return FALSE; 498 515 499 516 body = nil; 500 517 while(lex->LookAhead(0) != '}'){ 501 if(!rDefinition(def)){502 if(!SyntaxError())503 return FALSE;// too many errors504 505 SkipTo('}');506 lex->GetToken(cp);507 body = Ptree::List(new Leaf(op), nil, new Leaf(cp));508 return TRUE;// error recovery509 }510 511 body = Ptree::Snoc(body, def);518 if(!rDefinition(def)){ 519 if(!SyntaxError()) 520 return FALSE; // too many errors 521 522 SkipTo('}'); 523 lex->GetToken(cp); 524 body = Ptree::List(new Leaf(op), nil, new Leaf(cp)); 525 return TRUE; // error recovery 526 } 527 528 body = Ptree::Snoc(body, def); 512 529 } 513 530 … … 541 558 542 559 if(!rTemplateDecl2(decl, kind)) 543 return FALSE;560 return FALSE; 544 561 545 562 if(!rDeclaration(body)) 546 return FALSE;563 return FALSE; 547 564 548 565 // Repackage the decl and body depending upon what kind of template … … 550 567 switch (kind) { 551 568 case tdk_instantiation: 552 // Repackage the decl as a PtreeTemplateInstantiation553 decl = body;554 // assumes that decl has the form: [nil [class ...] ;]555 if (Ptree::Length(decl) != 3)556 return FALSE;557 558 if (Ptree::First(decl) != nil)559 return FALSE;560 561 if (Ptree::Second(decl)->What() != ntClassSpec)562 return FALSE;563 564 if (!Ptree::Eq(Ptree::Third(decl), ';'))565 return FALSE;566 567 decl = new PtreeTemplateInstantiation(Ptree::Second(decl));568 break;569 // Repackage the decl as a PtreeTemplateInstantiation 570 decl = body; 571 // assumes that decl has the form: [nil [class ...] ;] 572 if (Ptree::Length(decl) != 3) 573 return FALSE; 574 575 if (Ptree::First(decl) != nil) 576 return FALSE; 577 578 if (Ptree::Second(decl)->What() != ntClassSpec) 579 return FALSE; 580 581 if (!Ptree::Eq(Ptree::Third(decl), ';')) 582 return FALSE; 583 584 decl = new PtreeTemplateInstantiation(Ptree::Second(decl)); 585 break; 569 586 case tdk_decl: 570 587 case tdk_specialization: 571 decl = Ptree::Snoc(decl, body);572 break;588 decl = Ptree::Snoc(decl, body); 589 break; 573 590 default: 574 MopErrorMessage("rTemplateDecl()", "fatal");575 break;591 MopErrorMessage("rTemplateDecl()", "fatal"); 592 break; 576 593 } 577 594 … … 585 602 586 603 if(lex->GetToken(tk) != TEMPLATE) 587 return FALSE;604 return FALSE; 588 605 589 606 if(lex->LookAhead(0) != '<') { 590 // template instantiation591 decl = nil;592 kind = tdk_instantiation;593 return TRUE;// ignore TEMPLATE607 // template instantiation 608 decl = nil; 609 kind = tdk_instantiation; 610 return TRUE; // ignore TEMPLATE 594 611 } 595 612 596 613 decl = new PtreeTemplateDecl(new LeafReserved(tk)); 597 614 if(lex->GetToken(tk) != '<') 598 return FALSE;615 return FALSE; 599 616 600 617 decl = Ptree::Snoc(decl, new Leaf(tk)); 601 618 if(!rTempArgList(args)) 602 return FALSE;619 return FALSE; 603 620 604 621 if(lex->GetToken(tk) != '>') 605 return FALSE;622 return FALSE; 606 623 607 624 decl = Ptree::Nconc(decl, Ptree::List(args, new Leaf(tk))); … … 609 626 // ignore nested TEMPLATE 610 627 while (lex->LookAhead(0) == TEMPLATE) { 611 lex->GetToken(tk);612 if(lex->LookAhead(0) != '<')613 break;614 615 lex->GetToken(tk);616 if(!rTempArgList(args))617 return FALSE;618 619 if(lex->GetToken(tk) != '>')620 return FALSE;628 lex->GetToken(tk); 629 if(lex->LookAhead(0) != '<') 630 break; 631 632 lex->GetToken(tk); 633 if(!rTempArgList(args)) 634 return FALSE; 635 636 if(lex->GetToken(tk) != '>') 637 return FALSE; 621 638 } 622 639 623 640 if (args == nil) 624 // template < > declaration625 kind = tdk_specialization;641 // template < > declaration 642 kind = tdk_specialization; 626 643 else 627 // template < ... > declaration628 kind = tdk_decl;644 // template < ... > declaration 645 kind = tdk_decl; 629 646 630 647 return TRUE; … … 642 659 643 660 if(lex->LookAhead(0) == '>'){ 644 args = nil;645 return TRUE;661 args = nil; 662 return TRUE; 646 663 } 647 664 648 665 if(!rTempArgDeclaration(a)) 649 return FALSE;666 return FALSE; 650 667 651 668 args = Ptree::List(a); 652 669 while(lex->LookAhead(0) == ','){ 653 lex->GetToken(tk);654 args = Ptree::Snoc(args, new Leaf(tk));655 if(!rTempArgDeclaration(a))656 return FALSE;657 658 args = Ptree::Snoc(args, a);670 lex->GetToken(tk); 671 args = Ptree::Snoc(args, new Leaf(tk)); 672 if(!rTempArgDeclaration(a)) 673 return FALSE; 674 675 args = Ptree::Snoc(args, a); 659 676 } 660 677 … … 674 691 int t0 = lex->LookAhead(0); 675 692 if(t0 == CLASS && lex->LookAhead(1) == Identifier){ 676 lex->GetToken(tk1);677 lex->GetToken(tk2);678 Ptree* name = new Leaf(tk2);679 decl = Ptree::List(new Leaf(tk1), name);680 681 if(lex->LookAhead(0) == '='){682 Ptree* default_type;683 684 lex->GetToken(tk1);685 if(!rTypeName(default_type))686 return FALSE;687 688 decl = Ptree::Nconc(decl, Ptree::List(new Leaf(tk1),689 default_type));690 }693 lex->GetToken(tk1); 694 lex->GetToken(tk2); 695 Ptree* name = new Leaf(tk2); 696 decl = Ptree::List(new Leaf(tk1), name); 697 698 if(lex->LookAhead(0) == '='){ 699 Ptree* default_type; 700 701 lex->GetToken(tk1); 702 if(!rTypeName(default_type)) 703 return FALSE; 704 705 decl = Ptree::Nconc(decl, Ptree::List(new Leaf(tk1), 706 default_type)); 707 } 691 708 } 692 709 else if (t0 == TEMPLATE) { 693 TemplateDeclKind kind;694 if(!rTemplateDecl2(decl, kind))695 return FALSE;696 697 if (lex->GetToken(tk1) != CLASS || lex->GetToken(tk2) != Identifier)698 return FALSE;699 700 Ptree* cspec = new PtreeClassSpec(new LeafReserved(tk1),701 Ptree::Cons(new Leaf(tk2),nil),702 nil);703 decl = Ptree::Snoc(decl, cspec);704 if(lex->LookAhead(0) == '='){710 TemplateDeclKind kind; 711 if(!rTemplateDecl2(decl, kind)) 712 return FALSE; 713 714 if (lex->GetToken(tk1) != CLASS || lex->GetToken(tk2) != Identifier) 715 return FALSE; 716 717 Ptree* cspec = new PtreeClassSpec(new LeafReserved(tk1), 718 Ptree::Cons(new Leaf(tk2),nil), 719 nil); 720 decl = Ptree::Snoc(decl, cspec); 721 if(lex->LookAhead(0) == '='){ 705 722 Ptree* default_type; 706 lex->GetToken(tk1);707 if(!rTypeName(default_type))708 return FALSE;709 710 decl = Ptree::Nconc(decl, Ptree::List(new Leaf(tk1),711 default_type));712 }723 lex->GetToken(tk1); 724 if(!rTypeName(default_type)) 725 return FALSE; 726 727 decl = Ptree::Nconc(decl, Ptree::List(new Leaf(tk1), 728 default_type)); 729 } 713 730 } 714 731 else{ 715 Ptree *type_name, *arg;716 Encoding type_encode, name_encode;717 if(!rTypeSpecifier(type_name, TRUE, type_encode))718 return FALSE;719 720 if(!rDeclarator(arg, kArgDeclarator, FALSE, type_encode, name_encode,721 TRUE))722 return FALSE;723 724 decl = Ptree::List(type_name, arg);725 if(lex->LookAhead(0) == '='){726 Ptree* exp;727 lex->GetToken(tk1);728 if(!rAdditiveExpr(exp))729 return FALSE;730 731 decl = Ptree::Nconc(decl, Ptree::List(new Leaf(tk1), exp));732 }732 Ptree *type_name, *arg; 733 Encoding type_encode, name_encode; 734 if(!rTypeSpecifier(type_name, TRUE, type_encode)) 735 return FALSE; 736 737 if(!rDeclarator(arg, kArgDeclarator, FALSE, type_encode, name_encode, 738 TRUE)) 739 return FALSE; 740 741 decl = Ptree::List(type_name, arg); 742 if(lex->LookAhead(0) == '='){ 743 Ptree* exp; 744 lex->GetToken(tk1); 745 if(!rAdditiveExpr(exp)) 746 return FALSE; 747 748 decl = Ptree::Nconc(decl, Ptree::List(new Leaf(tk1), exp)); 749 } 733 750 } 734 751 … … 746 763 747 764 if(lex->GetToken(tk1) != EXTERN) 748 return FALSE;765 return FALSE; 749 766 750 767 if(lex->GetToken(tk2) != TEMPLATE) 751 return FALSE;768 return FALSE; 752 769 753 770 if(!rDeclaration(body)) 754 return FALSE;771 return FALSE; 755 772 756 773 decl = new PtreeExternTemplate(new Leaf(tk1), 757 Ptree::List(new Leaf(tk2), body));774 Ptree::List(new Leaf(tk2), body)); 758 775 return TRUE; 759 776 } … … 794 811 795 812 if(!optMemberSpec(mem_s) || !optStorageSpec(storage_s)) 796 return FALSE;813 return FALSE; 797 814 798 815 if(mem_s == nil) 799 head = nil;816 head = nil; 800 817 else 801 head = mem_s;// mem_s is a list.818 head = mem_s; // mem_s is a list. 802 819 803 820 if(storage_s != nil) 804 head = Ptree::Snoc(head, storage_s);821 head = Ptree::Snoc(head, storage_s); 805 822 806 823 if(mem_s == nil) 807 if(optMemberSpec(mem_s))808 head = Ptree::Nconc(head, mem_s);809 else810 return FALSE;824 if(optMemberSpec(mem_s)) 825 head = Ptree::Nconc(head, mem_s); 826 else 827 return FALSE; 811 828 812 829 if(!optCvQualify(cv_q) 813 830 || !optIntegralTypeOrClassSpec(integral, type_encode)) 814 return FALSE;831 return FALSE; 815 832 816 833 if(integral != nil) 817 return rIntegralDeclaration(statement, type_encode,818 head, integral, cv_q);834 return rIntegralDeclaration(statement, type_encode, 835 head, integral, cv_q); 819 836 else{ 820 type_encode.Clear();821 int t = lex->LookAhead(0);822 if(cv_q != nil && ((t == Identifier && lex->LookAhead(1) == '=')823 || t == '*'))824 return rConstDeclaration(statement, type_encode, head, cv_q);825 else826 return rOtherDeclaration(statement, type_encode,827 mem_s, cv_q, head);837 type_encode.Clear(); 838 int t = lex->LookAhead(0); 839 if(cv_q != nil && ((t == Identifier && lex->LookAhead(1) == '=') 840 || t == '*')) 841 return rConstDeclaration(statement, type_encode, head, cv_q); 842 else 843 return rOtherDeclaration(statement, type_encode, 844 mem_s, cv_q, head); 828 845 } 829 846 } 830 847 831 848 bool Parser::rIntegralDeclaration(Ptree*& statement, Encoding& type_encode, 832 Ptree* head, Ptree* integral, Ptree* cv_q)849 Ptree* head, Ptree* integral, Ptree* cv_q) 833 850 { 834 851 Token tk; … … 836 853 837 854 if(!optCvQualify(cv_q2)) 838 return FALSE;855 return FALSE; 839 856 840 857 if(cv_q != nil) 841 if(cv_q2 == nil)842 integral = Ptree::Snoc(cv_q, integral);843 else844 integral = Ptree::Nconc(cv_q, Ptree::Cons(integral, cv_q2));858 if(cv_q2 == nil) 859 integral = Ptree::Snoc(cv_q, integral); 860 else 861 integral = Ptree::Nconc(cv_q, Ptree::Cons(integral, cv_q2)); 845 862 else if(cv_q2 != nil) 846 integral = Ptree::Cons(integral, cv_q2);863 integral = Ptree::Cons(integral, cv_q2); 847 864 848 865 type_encode.CvQualify(cv_q, cv_q2); 849 866 switch(lex->LookAhead(0)){ 850 867 case ';' : 851 lex->GetToken(tk);852 statement = new PtreeDeclaration(head, Ptree::List(integral,853 new Leaf(tk)));854 return TRUE;855 case ':' : // bit field856 lex->GetToken(tk);857 if(!rExpression(decl))858 return FALSE;859 860 decl = Ptree::List(Ptree::List(new Leaf(tk), decl));861 if(lex->GetToken(tk) != ';')862 return FALSE;863 864 statement = new PtreeDeclaration(head, Ptree::List(integral, decl,865 new Leaf(tk)));866 return TRUE;868 lex->GetToken(tk); 869 statement = new PtreeDeclaration(head, Ptree::List(integral, 870 new Leaf(tk))); 871 return TRUE; 872 case ':' : // bit field 873 lex->GetToken(tk); 874 if(!rExpression(decl)) 875 return FALSE; 876 877 decl = Ptree::List(Ptree::List(new Leaf(tk), decl)); 878 if(lex->GetToken(tk) != ';') 879 return FALSE; 880 881 statement = new PtreeDeclaration(head, Ptree::List(integral, decl, 882 new Leaf(tk))); 883 return TRUE; 867 884 default : 868 if(!rDeclarators(decl, type_encode, TRUE))869 return FALSE;870 871 if(lex->LookAhead(0) == ';'){872 lex->GetToken(tk);873 statement = new PtreeDeclaration(head, Ptree::List(integral, decl,874 new Leaf(tk)));875 return TRUE;876 }877 else{878 Ptree* body;879 if(!rFunctionBody(body))880 return FALSE;881 882 if(Ptree::Length(decl) != 1)883 return FALSE;884 885 statement = new PtreeDeclaration(head,886 Ptree::List(integral,887 decl->Car(), body));888 return TRUE;889 }885 if(!rDeclarators(decl, type_encode, TRUE)) 886 return FALSE; 887 888 if(lex->LookAhead(0) == ';'){ 889 lex->GetToken(tk); 890 statement = new PtreeDeclaration(head, Ptree::List(integral, decl, 891 new Leaf(tk))); 892 return TRUE; 893 } 894 else{ 895 Ptree* body; 896 if(!rFunctionBody(body)) 897 return FALSE; 898 899 if(Ptree::Length(decl) != 1) 900 return FALSE; 901 902 statement = new PtreeDeclaration(head, 903 Ptree::List(integral, 904 decl->Car(), body)); 905 return TRUE; 906 } 890 907 } 891 908 } 892 909 893 910 bool Parser::rConstDeclaration(Ptree*& statement, Encoding&, 894 Ptree* head, Ptree* cv_q)911 Ptree* head, Ptree* cv_q) 895 912 { 896 913 Ptree* decl; … … 900 917 type_encode.SimpleConst(); 901 918 if(!rDeclarators(decl, type_encode, FALSE)) 902 return FALSE;919 return FALSE; 903 920 904 921 if(lex->LookAhead(0) != ';') 905 return FALSE;922 return FALSE; 906 923 907 924 lex->GetToken(tk); 908 925 statement = new PtreeDeclaration(head, Ptree::List(cv_q, decl, 909 new Leaf(tk)));926 new Leaf(tk))); 910 927 return TRUE; 911 928 } 912 929 913 930 bool Parser::rOtherDeclaration(Ptree*& statement, Encoding& type_encode, 914 Ptree* mem_s, Ptree* cv_q, Ptree* head)931 Ptree* mem_s, Ptree* cv_q, Ptree* head) 915 932 { 916 933 Ptree *type_name, *decl, *cv_q2; … … 918 935 919 936 if(!rName(type_name, type_encode)) 920 return FALSE;937 return FALSE; 921 938 922 939 if(cv_q == nil && isConstructorDecl()){ 923 Encoding ftype_encode;924 if(!rConstructorDecl(decl, ftype_encode))925 return FALSE;926 927 decl = Ptree::List(new PtreeDeclarator(type_name, decl,928 ftype_encode, type_encode,929 type_name));930 type_name = nil;940 Encoding ftype_encode; 941 if(!rConstructorDecl(decl, ftype_encode)) 942 return FALSE; 943 944 decl = Ptree::List(new PtreeDeclarator(type_name, decl, 945 ftype_encode, type_encode, 946 type_name)); 947 type_name = nil; 931 948 } 932 949 else if(mem_s != nil && lex->LookAhead(0) == ';'){ 933 // FRIEND name ';'934 if(Ptree::Length(mem_s) == 1 && mem_s->Car()->What() == FRIEND){935 lex->GetToken(tk);936 statement = new PtreeDeclaration(head, Ptree::List(type_name,937 new Leaf(tk)));938 return TRUE;939 }940 else941 return FALSE;950 // FRIEND name ';' 951 if(Ptree::Length(mem_s) == 1 && mem_s->Car()->What() == FRIEND){ 952 lex->GetToken(tk); 953 statement = new PtreeDeclaration(head, Ptree::List(type_name, 954 new Leaf(tk))); 955 return TRUE; 956 } 957 else 958 return FALSE; 942 959 } 943 960 else{ 944 if(!optCvQualify(cv_q2))945 return FALSE;946 947 if(cv_q != nil)948 if(cv_q2 == nil)949 type_name = Ptree::Snoc(cv_q, type_name);950 else951 type_name = Ptree::Nconc(cv_q, Ptree::Cons(type_name, cv_q2));952 else if(cv_q2 != nil)953 type_name = Ptree::Cons(type_name, cv_q2);954 955 type_encode.CvQualify(cv_q, cv_q2);956 if(!rDeclarators(decl, type_encode, FALSE))957 return FALSE;961 if(!optCvQualify(cv_q2)) 962 return FALSE; 963 964 if(cv_q != nil) 965 if(cv_q2 == nil) 966 type_name = Ptree::Snoc(cv_q, type_name); 967 else 968 type_name = Ptree::Nconc(cv_q, Ptree::Cons(type_name, cv_q2)); 969 else if(cv_q2 != nil) 970 type_name = Ptree::Cons(type_name, cv_q2); 971 972 type_encode.CvQualify(cv_q, cv_q2); 973 if(!rDeclarators(decl, type_encode, FALSE)) 974 return FALSE; 958 975 } 959 976 960 977 if(lex->LookAhead(0) == ';'){ 961 lex->GetToken(tk);962 statement = new PtreeDeclaration(head, Ptree::List(type_name, decl,963 new Leaf(tk)));978 lex->GetToken(tk); 979 statement = new PtreeDeclaration(head, Ptree::List(type_name, decl, 980 new Leaf(tk))); 964 981 } 965 982 else{ 966 Ptree* body;967 if(!rFunctionBody(body))968 return FALSE;969 970 if(Ptree::Length(decl) != 1)971 return FALSE;972 973 statement = new PtreeDeclaration(head, Ptree::List(type_name,974 decl->Car(), body));983 Ptree* body; 984 if(!rFunctionBody(body)) 985 return FALSE; 986 987 if(Ptree::Length(decl) != 1) 988 return FALSE; 989 990 statement = new PtreeDeclaration(head, Ptree::List(type_name, 991 decl->Car(), body)); 975 992 } 976 993 … … 980 997 /* 981 998 This returns TRUE for an declaration like: 982 T (a);999 T (a); 983 1000 even if a is not a type name. This is a bug according to the ANSI 984 1001 specification, but I believe none says "T (a);" for a variable … … 988 1005 { 989 1006 if(lex->LookAhead(0) != '(') 990 return FALSE;1007 return FALSE; 991 1008 else{ 992 int t = lex->LookAhead(1);993 if(t == '*' || t == '&' || t == '(')994 return FALSE;// declarator995 else if(t == CONST || t == VOLATILE)996 return TRUE;// constructor or declarator997 else if(isPtrToMember(1))998 return FALSE;// declarator (::*)999 else1000 return TRUE;// maybe constructor1009 int t = lex->LookAhead(1); 1010 if(t == '*' || t == '&' || t == '(') 1011 return FALSE; // declarator 1012 else if(t == CONST || t == VOLATILE) 1013 return TRUE; // constructor or declarator 1014 else if(isPtrToMember(1)) 1015 return FALSE; // declarator (::*) 1016 else 1017 return TRUE; // maybe constructor 1001 1018 } 1002 1019 } … … 1011 1028 1012 1029 if(t0 == Scope) 1013 t0 = lex->LookAhead(i++);1030 t0 = lex->LookAhead(i++); 1014 1031 1015 1032 while(t0 == Identifier){ 1016 int t = lex->LookAhead(i++);1017 if(t == '<'){1018 int n = 1;1019 while(n > 0){1020 int u = lex->LookAhead(i++);1021 if(u == '<')1022 ++n;1023 else if(u == '>')1024 --n;1025 else if(u == '('){1026 int m = 1;1027 while(m > 0){1028 int v = lex->LookAhead(i++);1029 if(v == '(')1030 ++m;1031 else if(v == ')')1032 --m;1033 else if(v == '\0' || v == ';' || v == '}')1034 return FALSE;1035 }1036 }1037 else if(u == '\0' || u == ';' || u == '}')1038 return FALSE;1039 }1040 1041 t = lex->LookAhead(i++);1042 }1043 1044 if(t != Scope)1045 return FALSE;1046 1047 t0 = lex->LookAhead(i++);1048 if(t0 == '*')1049 return TRUE;1033 int t = lex->LookAhead(i++); 1034 if(t == '<'){ 1035 int n = 1; 1036 while(n > 0){ 1037 int u = lex->LookAhead(i++); 1038 if(u == '<') 1039 ++n; 1040 else if(u == '>') 1041 --n; 1042 else if(u == '('){ 1043 int m = 1; 1044 while(m > 0){ 1045 int v = lex->LookAhead(i++); 1046 if(v == '(') 1047 ++m; 1048 else if(v == ')') 1049 --m; 1050 else if(v == '\0' || v == ';' || v == '}') 1051 return FALSE; 1052 } 1053 } 1054 else if(u == '\0' || u == ';' || u == '}') 1055 return FALSE; 1056 } 1057 1058 t = lex->LookAhead(i++); 1059 } 1060 1061 if(t != Scope) 1062 return FALSE; 1063 1064 t0 = lex->LookAhead(i++); 1065 if(t0 == '*') 1066 return TRUE; 1050 1067 } 1051 1068 … … 1065 1082 p = nil; 1066 1083 while(t == FRIEND || t == INLINE || t == VIRTUAL || t == UserKeyword5){ 1067 if(t == UserKeyword5){1068 if(!rUserdefKeyword(lf))1069 return FALSE;1070 }1071 else{1072 lex->GetToken(tk);1073 if(t == INLINE)1074 lf = new LeafINLINE(tk);1075 else if(t == VIRTUAL)1076 lf = new LeafVIRTUAL(tk);1077 else1078 lf = new LeafFRIEND(tk);1079 }1080 1081 p = Ptree::Snoc(p, lf);1082 t = lex->LookAhead(0);1084 if(t == UserKeyword5){ 1085 if(!rUserdefKeyword(lf)) 1086 return FALSE; 1087 } 1088 else{ 1089 lex->GetToken(tk); 1090 if(t == INLINE) 1091 lf = new LeafINLINE(tk); 1092 else if(t == VIRTUAL) 1093 lf = new LeafVIRTUAL(tk); 1094 else 1095 lf = new LeafFRIEND(tk); 1096 } 1097 1098 p = Ptree::Snoc(p, lf); 1099 t = lex->LookAhead(0); 1083 1100 } 1084 1101 … … 1094 1111 if(t == STATIC || t == EXTERN || t == AUTO || t == REGISTER 1095 1112 || t == MUTABLE){ 1096 Token tk;1097 lex->GetToken(tk);1098 switch(t){1099 case STATIC :1100 p = new LeafSTATIC(tk);1101 break;1102 case EXTERN :1103 p = new LeafEXTERN(tk);1104 break;1105 case AUTO :1106 p = new LeafAUTO(tk);1107 break;1108 case REGISTER :1109 p = new LeafREGISTER(tk);1110 break;1111 case MUTABLE :1112 p = new LeafMUTABLE(tk);1113 break;1114 default :1115 MopErrorMessage("optStorageSpec()", "fatal");1116 break;1117 }1113 Token tk; 1114 lex->GetToken(tk); 1115 switch(t){ 1116 case STATIC : 1117 p = new LeafSTATIC(tk); 1118 break; 1119 case EXTERN : 1120 p = new LeafEXTERN(tk); 1121 break; 1122 case AUTO : 1123 p = new LeafAUTO(tk); 1124 break; 1125 case REGISTER : 1126 p = new LeafREGISTER(tk); 1127 break; 1128 case MUTABLE : 1129 p = new LeafMUTABLE(tk); 1130 break; 1131 default : 1132 MopErrorMessage("optStorageSpec()", "fatal"); 1133 break; 1134 } 1118 1135 } 1119 1136 else 1120 p = nil;// no storage specifier1137 p = nil; // no storage specifier 1121 1138 1122 1139 return TRUE; … … 1130 1147 Ptree* p = nil; 1131 1148 for(;;){ 1132 int t = lex->LookAhead(0);1133 if(t == CONST || t == VOLATILE){1134 Token tk;1135 lex->GetToken(tk);1136 switch(t){1137 case CONST :1138 p = Ptree::Snoc(p, new LeafCONST(tk));1139 break;1140 case VOLATILE :1141 p = Ptree::Snoc(p, new LeafVOLATILE(tk));1142 break;1143 default :1144 MopErrorMessage("optCvQualify()", "fatal");1145 break;1146 }1147 }1148 else1149 break;1149 int t = lex->LookAhead(0); 1150 if(t == CONST || t == VOLATILE){ 1151 Token tk; 1152 lex->GetToken(tk); 1153 switch(t){ 1154 case CONST : 1155 p = Ptree::Snoc(p, new LeafCONST(tk)); 1156 break; 1157 case VOLATILE : 1158 p = Ptree::Snoc(p, new LeafVOLATILE(tk)); 1159 break; 1160 default : 1161 MopErrorMessage("optCvQualify()", "fatal"); 1162 break; 1163 } 1164 } 1165 else 1166 break; 1150 1167 } 1151 1168 … … 1172 1189 p = nil; 1173 1190 for(;;){ 1174 t = lex->LookAhead(0);1175 if(t == CHAR || t == INT || t == SHORT || t == LONG || t == SIGNED1176 || t == UNSIGNED || t == FLOAT || t == DOUBLE || t == VOID1177 || t == BOOLEAN1191 t = lex->LookAhead(0); 1192 if(t == CHAR || t == INT || t == SHORT || t == LONG || t == SIGNED 1193 || t == UNSIGNED || t == FLOAT || t == DOUBLE || t == VOID 1194 || t == BOOLEAN 1178 1195 #if defined(_MSC_VER) 1179 || t == INT641196 || t == INT64 1180 1197 #endif 1181 ){1182 Token tk;1183 Ptree* kw;1184 lex->GetToken(tk);1185 switch(t){1186 case CHAR :1187 type = 'c';1188 kw = new LeafCHAR(tk);1189 break;1190 case INT :1198 ){ 1199 Token tk; 1200 Ptree* kw; 1201 lex->GetToken(tk); 1202 switch(t){ 1203 case CHAR : 1204 type = 'c'; 1205 kw = new LeafCHAR(tk); 1206 break; 1207 case INT : 1191 1208 #if defined(_MSC_VER) 1192 1209 case INT64 : // an int64 is *NOT* an int but... 1193 1210 #endif 1194 if(type != 's' && type != 'l' && type != 'j' && type != 'r')1195 type = 'i';1196 1197 kw = new LeafINT(tk);1198 break;1199 case SHORT :1200 type = 's';1201 kw = new LeafSHORT(tk);1202 break;1203 case LONG :1204 if(type == 'l')1205 type = 'j';// long long1206 else if(type == 'd')1207 type = 'r';// double long1208 else1209 type = 'l';1210 1211 kw = new LeafLONG(tk);1212 break;1213 case SIGNED :1214 flag = 'S';1215 kw = new LeafSIGNED(tk);1216 break;1217 case UNSIGNED :1218 flag = 'U';1219 kw = new LeafUNSIGNED(tk);1220 break;1221 case FLOAT :1222 type = 'f';1223 kw = new LeafFLOAT(tk);1224 break;1225 case DOUBLE :1226 if(type == 'l')1227 type = 'r';// long double1228 else1229 type = 'd';1230 1231 kw = new LeafDOUBLE(tk);1232 break;1233 case VOID :1234 type = 'v';1235 kw = new LeafVOID(tk);1236 break;1237 case BOOLEAN :1238 type = 'b';1239 kw = new LeafBOOLEAN(tk);1240 break;1241 default :1242 MopErrorMessage("optIntegralTypeOrClassSpec()", "fatal");1243 kw = nil;1244 break;1245 }1246 1247 p = Ptree::Snoc(p, kw);1248 is_integral = TRUE;1249 }1250 else1251 break;1211 if(type != 's' && type != 'l' && type != 'j' && type != 'r') 1212 type = 'i'; 1213 1214 kw = new LeafINT(tk); 1215 break; 1216 case SHORT : 1217 type = 's'; 1218 kw = new LeafSHORT(tk); 1219 break; 1220 case LONG : 1221 if(type == 'l') 1222 type = 'j'; // long long 1223 else if(type == 'd') 1224 type = 'r'; // double long 1225 else 1226 type = 'l'; 1227 1228 kw = new LeafLONG(tk); 1229 break; 1230 case SIGNED : 1231 flag = 'S'; 1232 kw = new LeafSIGNED(tk); 1233 break; 1234 case UNSIGNED : 1235 flag = 'U'; 1236 kw = new LeafUNSIGNED(tk); 1237 break; 1238 case FLOAT : 1239 type = 'f'; 1240 kw = new LeafFLOAT(tk); 1241 break; 1242 case DOUBLE : 1243 if(type == 'l') 1244 type = 'r'; // long double 1245 else 1246 type = 'd'; 1247 1248 kw = new LeafDOUBLE(tk); 1249 break; 1250 case VOID : 1251 type = 'v'; 1252 kw = new LeafVOID(tk); 1253 break; 1254 case BOOLEAN : 1255 type = 'b'; 1256 kw = new LeafBOOLEAN(tk); 1257 break; 1258 default : 1259 MopErrorMessage("optIntegralTypeOrClassSpec()", "fatal"); 1260 kw = nil; 1261 break; 1262 } 1263 1264 p = Ptree::Snoc(p, kw); 1265 is_integral = TRUE; 1266 } 1267 else 1268 break; 1252 1269 } 1253 1270 1254 1271 if(is_integral){ 1255 if(flag == 'S' && type != 'c')1256 flag = ' ';1257 1258 if(flag != ' ')1259 encode.Append(flag);1260 1261 if(type == ' ')1262 type = 'i';// signed, unsigned1263 1264 encode.Append(type);1265 return TRUE;1272 if(flag == 'S' && type != 'c') 1273 flag = ' '; 1274 1275 if(flag != ' ') 1276 encode.Append(flag); 1277 1278 if(type == ' ') 1279 type = 'i'; // signed, unsigned 1280 1281 encode.Append(type); 1282 return TRUE; 1266 1283 } 1267 1284 1268 1285 if(t == CLASS || t == STRUCT || t == UNION || t == UserKeyword) 1269 return rClassSpec(p, encode);1286 return rClassSpec(p, encode); 1270 1287 else if(t == ENUM) 1271 return rEnumSpec(p, encode);1288 return rEnumSpec(p, encode); 1272 1289 else{ 1273 p = nil;1274 return TRUE;1290 p = nil; 1291 return TRUE; 1275 1292 } 1276 1293 } … … 1287 1304 1288 1305 if(lex->GetToken(op) != '(') 1289 return FALSE;1306 return FALSE; 1290 1307 1291 1308 if(lex->LookAhead(0) == ')'){ 1292 args = nil;1293 encode.StartFuncArgs();1294 encode.Void();1295 encode.EndFuncArgs();1309 args = nil; 1310 encode.StartFuncArgs(); 1311 encode.Void(); 1312 encode.EndFuncArgs(); 1296 1313 } 1297 1314 else 1298 if(!rArgDeclList(args, encode))1299 return FALSE;1315 if(!rArgDeclList(args, encode)) 1316 return FALSE; 1300 1317 1301 1318 lex->GetToken(cp); … … 1303 1320 optCvQualify(cv); 1304 1321 if(cv != nil){ 1305 encode.CvQualify(cv);1306 constructor = Ptree::Nconc(constructor, cv);1307 } 1308 1309 optThrowDecl(throw_decl); // ignore in this version1322 encode.CvQualify(cv); 1323 constructor = Ptree::Nconc(constructor, cv); 1324 } 1325 1326 optThrowDecl(throw_decl); // ignore in this version 1310 1327 1311 1328 if(lex->LookAhead(0) == ':') 1312 if(rMemberInitializers(mi))1313 constructor = Ptree::Snoc(constructor, mi);1314 else1315 return FALSE;1329 if(rMemberInitializers(mi)) 1330 constructor = Ptree::Snoc(constructor, mi); 1331 else 1332 return FALSE; 1316 1333 1317 1334 if(lex->LookAhead(0) == '='){ 1318 Token eq, zero;1319 lex->GetToken(eq);1320 if(lex->GetToken(zero) != Constant)1321 return FALSE;1322 1323 constructor = Ptree::Nconc(constructor,1324 Ptree::List(new Leaf(eq), new Leaf(zero)));1335 Token eq, zero; 1336 lex->GetToken(eq); 1337 if(lex->GetToken(zero) != Constant) 1338 return FALSE; 1339 1340 constructor = Ptree::Nconc(constructor, 1341 Ptree::List(new Leaf(eq), new Leaf(zero))); 1325 1342 } 1326 1343 … … 1339 1356 1340 1357 if(lex->LookAhead(0) == THROW){ 1341 lex->GetToken(tk);1342 p = Ptree::Snoc(p, new LeafReserved(tk));1343 1344 if(lex->GetToken(tk) != '(')1345 return FALSE;1346 1347 p = Ptree::Snoc(p, new Leaf(tk));1348 1349 for(;;){1350 Ptree* q;1351 Encoding encode;1352 t = lex->LookAhead(0);1353 if(t == '\0')1354 return FALSE;1355 else if(t == ')')1356 break;1357 else if(rName(q, encode))1358 p = Ptree::Snoc(p, q);1359 else1360 return FALSE;1361 1362 if(lex->LookAhead(0) == ','){1363 lex->GetToken(tk);1364 p = Ptree::Snoc(p, new Leaf(tk));1365 }1366 else1367 break;1368 }1369 1370 if(lex->GetToken(tk) != ')')1371 return FALSE;1372 1373 p = Ptree::Snoc(p, new Leaf(tk));1358 lex->GetToken(tk); 1359 p = Ptree::Snoc(p, new LeafReserved(tk)); 1360 1361 if(lex->GetToken(tk) != '(') 1362 return FALSE; 1363 1364 p = Ptree::Snoc(p, new Leaf(tk)); 1365 1366 for(;;){ 1367 Ptree* q; 1368 Encoding encode; 1369 t = lex->LookAhead(0); 1370 if(t == '\0') 1371 return FALSE; 1372 else if(t == ')') 1373 break; 1374 else if(rName(q, encode)) 1375 p = Ptree::Snoc(p, q); 1376 else 1377 return FALSE; 1378 1379 if(lex->LookAhead(0) == ','){ 1380 lex->GetToken(tk); 1381 p = Ptree::Snoc(p, new Leaf(tk)); 1382 } 1383 else 1384 break; 1385 } 1386 1387 if(lex->GetToken(tk) != ')') 1388 return FALSE; 1389 1390 p = Ptree::Snoc(p, new Leaf(tk)); 1374 1391 } 1375 1392 … … 1384 1401 */ 1385 1402 bool Parser::rDeclarators(Ptree*& decls, Encoding& type_encode, 1386 bool should_be_declarator, bool is_statement)1403 bool should_be_declarator, bool is_statement) 1387 1404 { 1388 1405 Ptree* d; … … 1392 1409 decls = nil; 1393 1410 for(;;){ 1394 encode.Reset(type_encode);1395 if(!rDeclaratorWithInit(d, encode, should_be_declarator, is_statement))1396 return FALSE;1397 1398 decls = Ptree::Snoc(decls, d);1399 if(lex->LookAhead(0) == ','){1400 lex->GetToken(tk);1401 decls = Ptree::Snoc(decls, new Leaf(tk));1402 }1403 else1404 return TRUE;1411 encode.Reset(type_encode); 1412 if(!rDeclaratorWithInit(d, encode, should_be_declarator, is_statement)) 1413 return FALSE; 1414 1415 decls = Ptree::Snoc(decls, d); 1416 if(lex->LookAhead(0) == ','){ 1417 lex->GetToken(tk); 1418 decls = Ptree::Snoc(decls, new Leaf(tk)); 1419 } 1420 else 1421 return TRUE; 1405 1422 }; 1406 1423 } … … 1412 1429 */ 1413 1430 bool Parser::rDeclaratorWithInit(Ptree*& dw, Encoding& type_encode, 1414 bool should_be_declarator,1415 bool is_statement)1431 bool should_be_declarator, 1432 bool is_statement) 1416 1433 { 1417 1434 Ptree *d, *e; … … 1419 1436 Encoding name_encode; 1420 1437 1421 if(lex->LookAhead(0) == ':'){ // bit field1422 lex->GetToken(tk);1423 if(!rExpression(e))1424 return FALSE;1425 1426 dw = Ptree::List(new Leaf(tk), e);1427 return TRUE;1438 if(lex->LookAhead(0) == ':'){ // bit field 1439 lex->GetToken(tk); 1440 if(!rExpression(e)) 1441 return FALSE; 1442 1443 dw = Ptree::List(new Leaf(tk), e); 1444 return TRUE; 1428 1445 } 1429 1446 else{ 1430 if(!rDeclarator(d, kDeclarator, FALSE, type_encode, name_encode,1431 should_be_declarator, is_statement))1432 return FALSE;1433 1434 int t = lex->LookAhead(0);1435 if(t == '='){1436 lex->GetToken(tk);1437 if(!rInitializeExpr(e))1438 return FALSE;1439 1440 dw = Ptree::Nconc(d, Ptree::List(new Leaf(tk), e));1441 return TRUE;1442 }1443 else if(t == ':'){// bit field1444 lex->GetToken(tk);1445 if(!rExpression(e))1446 return FALSE;1447 1448 dw = Ptree::Nconc(d, Ptree::List(new Leaf(tk), e));1449 return TRUE;1450 }1451 else{1452 dw = d;1453 return TRUE;1454 }1447 if(!rDeclarator(d, kDeclarator, FALSE, type_encode, name_encode, 1448 should_be_declarator, is_statement)) 1449 return FALSE; 1450 1451 int t = lex->LookAhead(0); 1452 if(t == '='){ 1453 lex->GetToken(tk); 1454 if(!rInitializeExpr(e)) 1455 return FALSE; 1456 1457 dw = Ptree::Nconc(d, Ptree::List(new Leaf(tk), e)); 1458 return TRUE; 1459 } 1460 else if(t == ':'){ // bit field 1461 lex->GetToken(tk); 1462 if(!rExpression(e)) 1463 return FALSE; 1464 1465 dw = Ptree::Nconc(d, Ptree::List(new Leaf(tk), e)); 1466 return TRUE; 1467 } 1468 else{ 1469 dw = d; 1470 return TRUE; 1471 } 1455 1472 } 1456 1473 } … … 1459 1476 declarator 1460 1477 : (ptr.operator)* (name | '(' declarator ')') 1461 ('[' comma.expression ']')* {func.args.or.init}1478 ('[' comma.expression ']')* {func.args.or.init} 1462 1479 1463 1480 func.args.or.init … … 1466 1483 1467 1484 Note: We assume that '(' declarator ')' is followed by '(' or '['. 1468 This is to avoid accepting a function call F(x) as a pair of1469 a type F and a declarator x. This assumption is ignored1470 if should_be_declarator is true.1485 This is to avoid accepting a function call F(x) as a pair of 1486 a type F and a declarator x. This assumption is ignored 1487 if should_be_declarator is true. 1471 1488 1472 1489 Note: An argument declaration list and a function-style initializer 1473 take a different Ptree structure.1474 e.g.1475 int f(char) ==> .. [f ( [[[char] nil]] )]1476 Point f(1) ==> .. [f [( [1] )]]1490 take a different Ptree structure. 1491 e.g. 1492 int f(char) ==> .. [f ( [[[char] nil]] )] 1493 Point f(1) ==> .. [f [( [1] )]] 1477 1494 1478 1495 Note: is_statement changes the behavior of rArgDeclListOrInit(). 1479 1496 */ 1480 1497 bool Parser::rDeclarator(Ptree*& decl, DeclKind kind, bool recursive, 1481 Encoding& type_encode, Encoding& name_encode,1482 bool should_be_declarator, bool is_statement)1498 Encoding& type_encode, Encoding& name_encode, 1499 bool should_be_declarator, bool is_statement) 1483 1500 { 1484 1501 return rDeclarator2(decl, kind, recursive, type_encode, name_encode, 1485 should_be_declarator, is_statement, nil);1502 should_be_declarator, is_statement, nil); 1486 1503 } 1487 1504 1488 1505 bool Parser::rDeclarator2(Ptree*& decl, DeclKind kind, bool recursive, 1489 Encoding& type_encode, Encoding& name_encode,1490 bool should_be_declarator, bool is_statement,1491 Ptree** declared_name)1506 Encoding& type_encode, Encoding& name_encode, 1507 bool should_be_declarator, bool is_statement, 1508 Ptree** declared_name) 1492 1509 { 1493 1510 Encoding recursive_encode; … … 1498 1515 1499 1516 if(declared_name == nil) 1500 declared_name = &declared_name0;1517 declared_name = &declared_name0; 1501 1518 1502 1519 if(!optPtrOperator(d, type_encode)) 1503 return FALSE;1520 return FALSE; 1504 1521 1505 1522 t = lex->LookAhead(0); 1506 1523 if(t == '('){ 1507 Token op, cp;1508 Ptree* decl2;1509 lex->GetToken(op);1510 recursive_decl = TRUE;1511 if(!rDeclarator2(decl2, kind, TRUE, recursive_encode, name_encode,1512 TRUE, FALSE, declared_name))1513 return FALSE;1514 1515 if(lex->GetToken(cp) != ')')1516 return FALSE;1517 1518 if(!should_be_declarator)1519 if(kind == kDeclarator && d == nil){1520 t = lex->LookAhead(0);1521 if(t != '[' && t != '(')1522 return FALSE;1523 }1524 1525 d = Ptree::Snoc(d, Ptree::List(new Leaf(op), decl2, new Leaf(cp)));1524 Token op, cp; 1525 Ptree* decl2; 1526 lex->GetToken(op); 1527 recursive_decl = TRUE; 1528 if(!rDeclarator2(decl2, kind, TRUE, recursive_encode, name_encode, 1529 TRUE, FALSE, declared_name)) 1530 return FALSE; 1531 1532 if(lex->GetToken(cp) != ')') 1533 return FALSE; 1534 1535 if(!should_be_declarator) 1536 if(kind == kDeclarator && d == nil){ 1537 t = lex->LookAhead(0); 1538 if(t != '[' && t != '(') 1539 return FALSE; 1540 } 1541 1542 d = Ptree::Snoc(d, Ptree::List(new Leaf(op), decl2, new Leaf(cp))); 1526 1543 } 1527 1544 else if(kind != kCastDeclarator 1528 && (kind == kDeclarator || t == Identifier || t == Scope)){1529 // if this is an argument declarator, "int (*)()" is valid.1530 Ptree* name;1531 if(rName(name, name_encode))1532 d = Ptree::Snoc(d, name);1533 else1534 return FALSE;1535 1536 *declared_name = name;1545 && (kind == kDeclarator || t == Identifier || t == Scope)){ 1546 // if this is an argument declarator, "int (*)()" is valid. 1547 Ptree* name; 1548 if(rName(name, name_encode)) 1549 d = Ptree::Snoc(d, name); 1550 else 1551 return FALSE; 1552 1553 *declared_name = name; 1537 1554 } 1538 1555 else 1539 name_encode.Clear();// empty1556 name_encode.Clear(); // empty 1540 1557 1541 1558 for(;;){ 1542 t = lex->LookAhead(0);1543 if(t == '('){// function1544 Encoding args_encode;1545 Token op, cp;1546 Ptree *args, *cv, *throw_decl, *mi;1547 bool is_args = TRUE;1548 1549 lex->GetToken(op);1550 if(lex->LookAhead(0) == ')'){1551 args = nil;1552 args_encode.StartFuncArgs();1553 args_encode.Void();1554 args_encode.EndFuncArgs();1555 }1556 else1557 if(!rArgDeclListOrInit(args, is_args, args_encode,1558 is_statement))1559 return FALSE;1560 1561 if(lex->GetToken(cp) != ')')1562 return FALSE;1563 1564 if(is_args){1565 d = Ptree::Nconc(d, Ptree::List(new Leaf(op), args,1566 new Leaf(cp)));1567 optCvQualify(cv);1568 if(cv != nil){1569 args_encode.CvQualify(cv);1570 d = Ptree::Nconc(d, cv);1571 }1572 }1573 else1574 d = Ptree::Snoc(d, Ptree::List(new Leaf(op), args,1575 new Leaf(cp)));1576 1577 if(!args_encode.IsEmpty())1578 type_encode.Function(args_encode);1579 1580 optThrowDecl(throw_decl);// ignore in this version1581 1582 if(lex->LookAhead(0) == ':')1583 if(rMemberInitializers(mi))1584 d = Ptree::Snoc(d, mi);1585 else1586 return FALSE;1587 1588 break;// "T f(int)(char)" is invalid.1589 }1590 else if(t == '['){// array1591 Token ob, cb;1592 Ptree* expr;1593 lex->GetToken(ob);1594 if(lex->LookAhead(0) == ']')1595 expr = nil;1596 else1597 if(!rCommaExpression(expr))1598 return FALSE;1599 1600 if(lex->GetToken(cb) != ']')1601 return FALSE;1602 1603 type_encode.Array();1604 d = Ptree::Nconc(d, Ptree::List(new Leaf(ob), expr,1605 new Leaf(cb)));1606 }1607 else1608 break;1559 t = lex->LookAhead(0); 1560 if(t == '('){ // function 1561 Encoding args_encode; 1562 Token op, cp; 1563 Ptree *args, *cv, *throw_decl, *mi; 1564 bool is_args = TRUE; 1565 1566 lex->GetToken(op); 1567 if(lex->LookAhead(0) == ')'){ 1568 args = nil; 1569 args_encode.StartFuncArgs(); 1570 args_encode.Void(); 1571 args_encode.EndFuncArgs(); 1572 } 1573 else 1574 if(!rArgDeclListOrInit(args, is_args, args_encode, 1575 is_statement)) 1576 return FALSE; 1577 1578 if(lex->GetToken(cp) != ')') 1579 return FALSE; 1580 1581 if(is_args){ 1582 d = Ptree::Nconc(d, Ptree::List(new Leaf(op), args, 1583 new Leaf(cp))); 1584 optCvQualify(cv); 1585 if(cv != nil){ 1586 args_encode.CvQualify(cv); 1587 d = Ptree::Nconc(d, cv); 1588 } 1589 } 1590 else 1591 d = Ptree::Snoc(d, Ptree::List(new Leaf(op), args, 1592 new Leaf(cp))); 1593 1594 if(!args_encode.IsEmpty()) 1595 type_encode.Function(args_encode); 1596 1597 optThrowDecl(throw_decl); // ignore in this version 1598 1599 if(lex->LookAhead(0) == ':') 1600 if(rMemberInitializers(mi)) 1601 d = Ptree::Snoc(d, mi); 1602 else 1603 return FALSE; 1604 1605 break; // "T f(int)(char)" is invalid. 1606 } 1607 else if(t == '['){ // array 1608 Token ob, cb; 1609 Ptree* expr; 1610 lex->GetToken(ob); 1611 if(lex->LookAhead(0) == ']') 1612 expr = nil; 1613 else 1614 if(!rCommaExpression(expr)) 1615 return FALSE; 1616 1617 if(lex->GetToken(cb) != ']') 1618 return FALSE; 1619 1620 type_encode.Array(); 1621 d = Ptree::Nconc(d, Ptree::List(new Leaf(ob), expr, 1622 new Leaf(cb))); 1623 } 1624 else 1625 break; 1609 1626 } 1610 1627 1611 1628 if(recursive_decl) 1612 type_encode.Recursion(recursive_encode);1629 type_encode.Recursion(recursive_encode); 1613 1630 1614 1631 if(recursive) 1615 decl = d;1632 decl = d; 1616 1633 else 1617 if(d == nil)1618 decl = new PtreeDeclarator(type_encode, name_encode,1619 *declared_name);1620 else1621 decl = new PtreeDeclarator(d, type_encode, name_encode,1622 *declared_name);1634 if(d == nil) 1635 decl = new PtreeDeclarator(type_encode, name_encode, 1636 *declared_name); 1637 else 1638 decl = new PtreeDeclarator(d, type_encode, name_encode, 1639 *declared_name); 1623 1640 1624 1641 return TRUE; … … 1633 1650 ptrs = nil; 1634 1651 for(;;){ 1635 int t = lex->LookAhead(0);1636 if(t != '*' && t != '&' && !isPtrToMember(0))1637 break;1638 else{1639 Ptree *op, *cv;1640 if(t == '*' || t == '&'){1641 Token tk;1642 lex->GetToken(tk);1643 op = new Leaf(tk);1644 encode.PtrOperator(t);1645 }1646 else1647 if(!rPtrToMember(op, encode))1648 return FALSE;1649 1650 ptrs = Ptree::Snoc(ptrs, op);1651 optCvQualify(cv);1652 if(cv != nil){1653 ptrs = Ptree::Nconc(ptrs, cv);1654 encode.CvQualify(cv);1655 }1656 }1652 int t = lex->LookAhead(0); 1653 if(t != '*' && t != '&' && !isPtrToMember(0)) 1654 break; 1655 else{ 1656 Ptree *op, *cv; 1657 if(t == '*' || t == '&'){ 1658 Token tk; 1659 lex->GetToken(tk); 1660 op = new Leaf(tk); 1661 encode.PtrOperator(t); 1662 } 1663 else 1664 if(!rPtrToMember(op, encode)) 1665 return FALSE; 1666 1667 ptrs = Ptree::Snoc(ptrs, op); 1668 optCvQualify(cv); 1669 if(cv != nil){ 1670 ptrs = Ptree::Nconc(ptrs, cv); 1671 encode.CvQualify(cv); 1672 } 1673 } 1657 1674 } 1658 1675 … … 1670 1687 1671 1688 if(lex->GetToken(tk) != ':') 1672 return FALSE;1689 return FALSE; 1673 1690 1674 1691 init = Ptree::List(new Leaf(tk)); 1675 1692 if(!rMemberInit(m)) 1676 return FALSE;1693 return FALSE; 1677 1694 1678 1695 init = Ptree::Snoc(init, m); 1679 1696 while(lex->LookAhead(0) == ','){ 1680 lex->GetToken(tk);1681 init = Ptree::Snoc(init, new Leaf(tk));1682 if(!rMemberInit(m))1683 return FALSE;1684 1685 init = Ptree::Snoc(init, m);1697 lex->GetToken(tk); 1698 init = Ptree::Snoc(init, new Leaf(tk)); 1699 if(!rMemberInit(m)) 1700 return FALSE; 1701 1702 init = Ptree::Snoc(init, m); 1686 1703 } 1687 1704 … … 1700 1717 1701 1718 if(!rName(name, encode)) 1702 return FALSE;1719 return FALSE; 1703 1720 1704 1721 if(!name->IsLeaf()) 1705 name = new PtreeName(name, encode);1722 name = new PtreeName(name, encode); 1706 1723 1707 1724 if(lex->GetToken(tk1) != '(') 1708 return FALSE;1725 return FALSE; 1709 1726 1710 1727 if(!rFunctionArguments(args)) 1711 return FALSE;1728 return FALSE; 1712 1729 1713 1730 if(lex->GetToken(tk2) != ')') 1714 return FALSE;1731 return FALSE; 1715 1732 1716 1733 init = Ptree::List(name, new Leaf(tk1), args, new Leaf(tk2)); … … 1736 1753 1737 1754 if(lex->LookAhead(0) == Scope){ 1738 lex->GetToken(tk);1739 name = Ptree::List(new Leaf(tk));1740 encode.GlobalScope();1741 ++length;1755 lex->GetToken(tk); 1756 name = Ptree::List(new Leaf(tk)); 1757 encode.GlobalScope(); 1758 ++length; 1742 1759 } 1743 1760 else 1744 name = nil;1761 name = nil; 1745 1762 1746 1763 for(;;){ 1747 t = lex->GetToken(tk);1748 if(t == Identifier){1749 Ptree* n = new Leaf(tk);1750 t = lex->LookAhead(0);1751 if(t == '<'){1752 Ptree* args;1753 Encoding args_encode;1754 if(!rTemplateArgs(args, args_encode))1755 return FALSE;1756 1757 encode.Template(n, args_encode);1758 ++length;1759 n = Ptree::List(n, args);1760 t = lex->LookAhead(0);1761 }1762 else{1763 encode.SimpleName(n);1764 ++length;1765 }1766 1767 if(t == Scope){1768 lex->GetToken(tk);1769 name = Ptree::Nconc(name, Ptree::List(n, new Leaf(tk)));1770 }1771 else{1772 if(name == nil)1773 name = n;1774 else1775 name = Ptree::Snoc(name, n);1776 1777 if(length > 1)1778 encode.Qualified(length);1779 1780 return TRUE;1781 }1782 }1783 else if(t == '~'){1784 if(lex->LookAhead(0) != Identifier)1785 return FALSE;1786 1787 lex->GetToken(tk2);1788 Ptree* class_name = new Leaf(tk2);1789 Ptree* dt = Ptree::List(new Leaf(tk), class_name);1790 if(name == nil)1791 name = dt;1792 else1793 name = Ptree::Snoc(name, dt);1794 1795 encode.Destructor(class_name);1796 if(length > 0)1797 encode.Qualified(length + 1);1798 1799 return TRUE;1800 }1801 else if(t == OPERATOR){1802 Ptree* op;1803 Ptree* opf;1804 if(!rOperatorName(op, encode))1805 return FALSE;1806 1807 t = lex->LookAhead(0);1808 if(t != '<')1809 opf = Ptree::List(new LeafReserved(tk), op);1810 else {1811 Ptree* args;1812 Encoding args_encode;1813 if(!rTemplateArgs(args, args_encode))1814 return FALSE;1815 1816 // here, I must merge args_encode into encode.1817 // I'll do it in future. :p1818 1819 opf = Ptree::List(new LeafReserved(tk), op, args);1820 }1821 1822 if(name == nil)1823 name = opf;1824 else1825 name = Ptree::Snoc(name, opf);1826 1827 if(length > 0)1828 encode.Qualified(length + 1);1829 1830 return TRUE;1831 }1832 else1833 return FALSE;1764 t = lex->GetToken(tk); 1765 if(t == Identifier){ 1766 Ptree* n = new Leaf(tk); 1767 t = lex->LookAhead(0); 1768 if(t == '<'){ 1769 Ptree* args; 1770 Encoding args_encode; 1771 if(!rTemplateArgs(args, args_encode)) 1772 return FALSE; 1773 1774 encode.Template(n, args_encode); 1775 ++length; 1776 n = Ptree::List(n, args); 1777 t = lex->LookAhead(0); 1778 } 1779 else{ 1780 encode.SimpleName(n); 1781 ++length; 1782 } 1783 1784 if(t == Scope){ 1785 lex->GetToken(tk); 1786 name = Ptree::Nconc(name, Ptree::List(n, new Leaf(tk))); 1787 } 1788 else{ 1789 if(name == nil) 1790 name = n; 1791 else 1792 name = Ptree::Snoc(name, n); 1793 1794 if(length > 1) 1795 encode.Qualified(length); 1796 1797 return TRUE; 1798 } 1799 } 1800 else if(t == '~'){ 1801 if(lex->LookAhead(0) != Identifier) 1802 return FALSE; 1803 1804 lex->GetToken(tk2); 1805 Ptree* class_name = new Leaf(tk2); 1806 Ptree* dt = Ptree::List(new Leaf(tk), class_name); 1807 if(name == nil) 1808 name = dt; 1809 else 1810 name = Ptree::Snoc(name, dt); 1811 1812 encode.Destructor(class_name); 1813 if(length > 0) 1814 encode.Qualified(length + 1); 1815 1816 return TRUE; 1817 } 1818 else if(t == OPERATOR){ 1819 Ptree* op; 1820 Ptree* opf; 1821 if(!rOperatorName(op, encode)) 1822 return FALSE; 1823 1824 t = lex->LookAhead(0); 1825 if(t != '<') 1826 opf = Ptree::List(new LeafReserved(tk), op); 1827 else { 1828 Ptree* args; 1829 Encoding args_encode; 1830 if(!rTemplateArgs(args, args_encode)) 1831 return FALSE; 1832 1833 // here, I must merge args_encode into encode. 1834 // I'll do it in future. :p 1835 1836 opf = Ptree::List(new LeafReserved(tk), op, args); 1837 } 1838 1839 if(name == nil) 1840 name = opf; 1841 else 1842 name = Ptree::Snoc(name, opf); 1843 1844 if(length > 0) 1845 encode.Qualified(length + 1); 1846 1847 return TRUE; 1848 } 1849 else 1850 return FALSE; 1834 1851 } 1835 1852 } … … 1856 1873 || t == RelOp || t == LogAndOp || t == LogOrOp || t == IncOp 1857 1874 || t == ',' || t == PmOp || t == ArrowOp){ 1858 lex->GetToken(tk);1859 name = new Leaf(tk);1860 encode.SimpleName(name);1861 return TRUE;1875 lex->GetToken(tk); 1876 name = new Leaf(tk); 1877 encode.SimpleName(name); 1878 return TRUE; 1862 1879 } 1863 1880 else if(t == NEW || t == DELETE){ 1864 lex->GetToken(tk);1865 if(lex->LookAhead(0) != '['){1866 name = new LeafReserved(tk);1867 encode.SimpleName(name);1868 return TRUE;1869 }1870 else{1871 name = Ptree::List(new LeafReserved(tk));1872 lex->GetToken(tk);1873 name = Ptree::Snoc(name, new Leaf(tk));1874 if(lex->GetToken(tk) != ']')1875 return FALSE;1876 1877 name = Ptree::Snoc(name, new Leaf(tk));1878 if(t == NEW)1879 encode.AppendWithLen("new[]", 5);1880 else1881 encode.AppendWithLen("delete[]", 8);1882 1883 return TRUE;1884 }1881 lex->GetToken(tk); 1882 if(lex->LookAhead(0) != '['){ 1883 name = new LeafReserved(tk); 1884 encode.SimpleName(name); 1885 return TRUE; 1886 } 1887 else{ 1888 name = Ptree::List(new LeafReserved(tk)); 1889 lex->GetToken(tk); 1890 name = Ptree::Snoc(name, new Leaf(tk)); 1891 if(lex->GetToken(tk) != ']') 1892 return FALSE; 1893 1894 name = Ptree::Snoc(name, new Leaf(tk)); 1895 if(t == NEW) 1896 encode.AppendWithLen("new[]", 5); 1897 else 1898 encode.AppendWithLen("delete[]", 8); 1899 1900 return TRUE; 1901 } 1885 1902 } 1886 1903 else if(t == '('){ 1887 lex->GetToken(tk);1888 name = Ptree::List(new Leaf(tk));1889 if(lex->GetToken(tk) != ')')1890 return FALSE;1891 1892 encode.AppendWithLen("()", 2);1893 name = Ptree::Snoc(name, new Leaf(tk));1894 return TRUE;1904 lex->GetToken(tk); 1905 name = Ptree::List(new Leaf(tk)); 1906 if(lex->GetToken(tk) != ')') 1907 return FALSE; 1908 1909 encode.AppendWithLen("()", 2); 1910 name = Ptree::Snoc(name, new Leaf(tk)); 1911 return TRUE; 1895 1912 } 1896 1913 else if(t == '['){ 1897 lex->GetToken(tk);1898 name = Ptree::List(new Leaf(tk));1899 if(lex->GetToken(tk) != ']')1900 return FALSE;1901 1902 encode.AppendWithLen("[]", 2);1903 name = Ptree::Snoc(name, new Leaf(tk));1904 return TRUE;1914 lex->GetToken(tk); 1915 name = Ptree::List(new Leaf(tk)); 1916 if(lex->GetToken(tk) != ']') 1917 return FALSE; 1918 1919 encode.AppendWithLen("[]", 2); 1920 name = Ptree::Snoc(name, new Leaf(tk)); 1921 return TRUE; 1905 1922 } 1906 1923 else 1907 return rCastOperatorName(name, encode);1924 return rCastOperatorName(name, encode); 1908 1925 } 1909 1926 … … 1919 1936 1920 1937 if(!optCvQualify(cv1)) 1921 return FALSE;1938 return FALSE; 1922 1939 1923 1940 if(!optIntegralTypeOrClassSpec(type_name, type_encode)) 1924 return FALSE;1941 return FALSE; 1925 1942 1926 1943 if(type_name == nil){ 1927 type_encode.Clear();1928 if(!rName(type_name, type_encode))1929 return FALSE;1944 type_encode.Clear(); 1945 if(!rName(type_name, type_encode)) 1946 return FALSE; 1930 1947 } 1931 1948 1932 1949 if(!optCvQualify(cv2)) 1933 return FALSE;1950 return FALSE; 1934 1951 1935 1952 if(cv1 != nil) 1936 if(cv2 == nil)1937 type_name = Ptree::Snoc(cv1, type_name);1938 else1939 type_name = Ptree::Nconc(cv1, Ptree::Cons(type_name, cv2));1953 if(cv2 == nil) 1954 type_name = Ptree::Snoc(cv1, type_name); 1955 else 1956 type_name = Ptree::Nconc(cv1, Ptree::Cons(type_name, cv2)); 1940 1957 else if(cv2 != nil) 1941 type_name = Ptree::Cons(type_name, cv2);1958 type_name = Ptree::Cons(type_name, cv2); 1942 1959 1943 1960 type_encode.CvQualify(cv1, cv2); 1944 1961 1945 1962 if(!optPtrOperator(ptr, type_encode)) 1946 return FALSE;1963 return FALSE; 1947 1964 1948 1965 encode.CastOperator(type_encode); 1949 1966 if(ptr == nil){ 1950 name = type_name;1951 return TRUE;1967 name = type_name; 1968 return TRUE; 1952 1969 } 1953 1970 else{ 1954 name = Ptree::List(type_name, ptr);1955 return TRUE;1971 name = Ptree::List(type_name, ptr); 1972 return TRUE; 1956 1973 } 1957 1974 } … … 1969 1986 1970 1987 if(lex->LookAhead(0) == Scope){ 1971 lex->GetToken(tk);1972 p = Ptree::List(new Leaf(tk));1973 pm_encode.GlobalScope();1974 ++length;1988 lex->GetToken(tk); 1989 p = Ptree::List(new Leaf(tk)); 1990 pm_encode.GlobalScope(); 1991 ++length; 1975 1992 } 1976 1993 else 1977 p = nil;1994 p = nil; 1978 1995 1979 1996 for(;;){ 1980 if(lex->GetToken(tk) == Identifier)1981 n = new Leaf(tk);1982 else1983 return FALSE;1984 1985 int t = lex->LookAhead(0);1986 if(t == '<'){1987 Ptree* args;1988 Encoding args_encode;1989 if(!rTemplateArgs(args, args_encode))1990 return FALSE;1991 1992 pm_encode.Template(n, args_encode);1993 ++length;1994 n = Ptree::List(n, args);1995 t = lex->LookAhead(0);1996 }1997 else{1998 pm_encode.SimpleName(n);1999 ++length;2000 }2001 2002 if(lex->GetToken(tk) != Scope)2003 return FALSE;2004 2005 p = Ptree::Nconc(p, Ptree::List(n, new Leaf(tk)));2006 if(lex->LookAhead(0) == '*'){2007 lex->GetToken(tk);2008 p = Ptree::Snoc(p, new Leaf(tk));2009 break;2010 }1997 if(lex->GetToken(tk) == Identifier) 1998 n = new Leaf(tk); 1999 else 2000 return FALSE; 2001 2002 int t = lex->LookAhead(0); 2003 if(t == '<'){ 2004 Ptree* args; 2005 Encoding args_encode; 2006 if(!rTemplateArgs(args, args_encode)) 2007 return FALSE; 2008 2009 pm_encode.Template(n, args_encode); 2010 ++length; 2011 n = Ptree::List(n, args); 2012 t = lex->LookAhead(0); 2013 } 2014 else{ 2015 pm_encode.SimpleName(n); 2016 ++length; 2017 } 2018 2019 if(lex->GetToken(tk) != Scope) 2020 return FALSE; 2021 2022 p = Ptree::Nconc(p, Ptree::List(n, new Leaf(tk))); 2023 if(lex->LookAhead(0) == '*'){ 2024 lex->GetToken(tk); 2025 p = Ptree::Snoc(p, new Leaf(tk)); 2026 break; 2027 } 2011 2028 } 2012 2029 … … 2031 2048 2032 2049 if(lex->GetToken(tk1) != '<') 2033 return FALSE;2050 return FALSE; 2034 2051 2035 2052 // in case of Foo<> 2036 2053 if(lex->LookAhead(0) == '>') { 2037 lex->GetToken(tk2);2038 temp_args = Ptree::List(new Leaf(tk1), new Leaf(tk2));2039 return TRUE;2054 lex->GetToken(tk2); 2055 temp_args = Ptree::List(new Leaf(tk1), new Leaf(tk2)); 2056 return TRUE; 2040 2057 } 2041 2058 2042 2059 Ptree* args = nil; 2043 2060 for(;;){ 2044 Ptree* a;2045 char* pos = lex->Save();2046 type_encode.Clear();2047 if(rTypeName(a, type_encode))2048 encode.Append(type_encode);2049 else{2050 lex->Restore(pos); 2051 if(!rLogicalOrExpr(a, TRUE))2052 return FALSE;2053 2054 encode.ValueTempParam();2055 }2056 2057 args = Ptree::Snoc(args, a);2058 switch(lex->GetToken(tk2)){2059 case '>' :2060 temp_args = Ptree::List(new Leaf(tk1), args, new Leaf(tk2));2061 return TRUE;2062 case ',' :2063 args = Ptree::Snoc(args, new Leaf(tk2));2064 break;2065 case ShiftOp :2066 if(*tk2.ptr == '>'){2067 lex->GetOnlyClosingBracket(tk2);2068 temp_args = Ptree::List(new Leaf(tk1), args,2069 new Leaf(tk2.ptr, 1));2070 return TRUE;2071 }2072 2073 default :2074 return FALSE;2075 }2061 Ptree* a; 2062 char* pos = lex->Save(); 2063 type_encode.Clear(); 2064 if(rTypeName(a, type_encode)) 2065 encode.Append(type_encode); 2066 else{ 2067 lex->Restore(pos); 2068 if(!rLogicalOrExpr(a, TRUE)) 2069 return FALSE; 2070 2071 encode.ValueTempParam(); 2072 } 2073 2074 args = Ptree::Snoc(args, a); 2075 switch(lex->GetToken(tk2)){ 2076 case '>' : 2077 temp_args = Ptree::List(new Leaf(tk1), args, new Leaf(tk2)); 2078 return TRUE; 2079 case ',' : 2080 args = Ptree::Snoc(args, new Leaf(tk2)); 2081 break; 2082 case ShiftOp : 2083 if(*tk2.ptr == '>'){ 2084 lex->GetOnlyClosingBracket(tk2); 2085 temp_args = Ptree::List(new Leaf(tk1), args, 2086 new Leaf(tk2.ptr, 1)); 2087 return TRUE; 2088 } 2089 2090 default : 2091 return FALSE; 2092 } 2076 2093 } 2077 2094 } … … 2083 2100 2084 2101 This rule accepts function.arguments to parse declarations like: 2085 Point p(1, 3);2102 Point p(1, 3); 2086 2103 "(1, 3)" is arg.decl.list.or.init. 2087 2104 2088 2105 If maybe_init is true, we first examine whether tokens construct 2089 2106 function.arguments. This ordering is significant if tokens are 2090 Point p(s, t);2107 Point p(s, t); 2091 2108 s and t can be type names or variable names. 2092 2109 */ 2093 2110 bool Parser::rArgDeclListOrInit(Ptree*& arglist, bool& is_args, 2094 Encoding& encode, bool maybe_init)2111 Encoding& encode, bool maybe_init) 2095 2112 { 2096 2113 char* pos = lex->Save(); 2097 2114 if(maybe_init) { 2098 if(rFunctionArguments(arglist))2099 if(lex->LookAhead(0) == ')') {2100 is_args = FALSE;2101 encode.Clear();2102 return TRUE;2103 }2104 2105 lex->Restore(pos);2106 return(is_args = rArgDeclList(arglist, encode));2115 if(rFunctionArguments(arglist)) 2116 if(lex->LookAhead(0) == ')') { 2117 is_args = FALSE; 2118 encode.Clear(); 2119 return TRUE; 2120 } 2121 2122 lex->Restore(pos); 2123 return(is_args = rArgDeclList(arglist, encode)); 2107 2124 } 2108 2125 else 2109 if(is_args = rArgDeclList(arglist, encode))2110 return TRUE;2111 else{2112 lex->Restore(pos);2113 encode.Clear();2114 return rFunctionArguments(arglist);2115 }2126 if(is_args = rArgDeclList(arglist, encode)) 2127 return TRUE; 2128 else{ 2129 lex->Restore(pos); 2130 encode.Clear(); 2131 return rFunctionArguments(arglist); 2132 } 2116 2133 } 2117 2134 … … 2132 2149 list = nil; 2133 2150 for(;;){ 2134 arg_encode.Clear();2135 t = lex->LookAhead(0);2136 if(t == ')'){2137 if(list == nil)2138 encode.Void();2139 2140 arglist = list;2141 break;2142 }2143 else if(t == Ellipsis){2144 lex->GetToken(tk);2145 encode.EllipsisArg();2146 arglist = Ptree::Snoc(list, new Leaf(tk));2147 break;2148 }2149 else if(rArgDeclaration(d, arg_encode)){2150 encode.Append(arg_encode);2151 list = Ptree::Snoc(list, d);2152 t = lex->LookAhead(0);2153 if(t == ','){2154 lex->GetToken(tk);2155 list = Ptree::Snoc(list, new Leaf(tk));2156 }2157 else if(t != ')' && t != Ellipsis)2158 return FALSE;2159 }2160 else{2161 arglist = nil;2162 return FALSE;2163 }2151 arg_encode.Clear(); 2152 t = lex->LookAhead(0); 2153 if(t == ')'){ 2154 if(list == nil) 2155 encode.Void(); 2156 2157 arglist = list; 2158 break; 2159 } 2160 else if(t == Ellipsis){ 2161 lex->GetToken(tk); 2162 encode.EllipsisArg(); 2163 arglist = Ptree::Snoc(list, new Leaf(tk)); 2164 break; 2165 } 2166 else if(rArgDeclaration(d, arg_encode)){ 2167 encode.Append(arg_encode); 2168 list = Ptree::Snoc(list, d); 2169 t = lex->LookAhead(0); 2170 if(t == ','){ 2171 lex->GetToken(tk); 2172 list = Ptree::Snoc(list, new Leaf(tk)); 2173 } 2174 else if(t != ')' && t != Ellipsis) 2175 return FALSE; 2176 } 2177 else{ 2178 arglist = nil; 2179 return FALSE; 2180 } 2164 2181 } 2165 2182 … … 2181 2198 switch(lex->LookAhead(0)){ 2182 2199 case REGISTER : 2183 lex->GetToken(tk);2184 header = new LeafREGISTER(tk);2185 break;2200 lex->GetToken(tk); 2201 header = new LeafREGISTER(tk); 2202 break; 2186 2203 case UserKeyword : 2187 if(!rUserdefKeyword(header))2188 return FALSE;2204 if(!rUserdefKeyword(header)) 2205 return FALSE; 2189 2206 break; 2190 2207 default : 2191 header = nil;2208 header = nil; 2192 2209 break; 2193 2210 } 2194 2211 2195 2212 if(!rTypeSpecifier(type_name, TRUE, encode)) 2196 return FALSE;2213 return FALSE; 2197 2214 2198 2215 if(!rDeclarator(arg, kArgDeclarator, FALSE, encode, name_encode, TRUE)) 2199 return FALSE;2216 return FALSE; 2200 2217 2201 2218 if(header == nil) 2202 decl = Ptree::List(type_name, arg);2219 decl = Ptree::List(type_name, arg); 2203 2220 else 2204 decl = Ptree::List(header, type_name, arg);2221 decl = Ptree::List(header, type_name, arg); 2205 2222 2206 2223 int t = lex->LookAhead(0); 2207 2224 if(t == '='){ 2208 lex->GetToken(tk);2209 if(!rInitializeExpr(e))2210 return FALSE;2211 2212 decl = Ptree::Nconc(decl, Ptree::List(new Leaf(tk), e));2225 lex->GetToken(tk); 2226 if(!rInitializeExpr(e)) 2227 return FALSE; 2228 2229 decl = Ptree::Nconc(decl, Ptree::List(new Leaf(tk), e)); 2213 2230 } 2214 2231 … … 2227 2244 2228 2245 if(lex->LookAhead(0) != '{') 2229 return rExpression(exp);2246 return rExpression(exp); 2230 2247 else{ 2231 lex->GetToken(tk);2232 Ptree* ob = new Leaf(tk);2233 elist = nil;2234 int t = lex->LookAhead(0);2235 while(t != '}'){2236 if(!rInitializeExpr(e)){2237 if(!SyntaxError())2238 return FALSE;// too many errors2239 2240 SkipTo('}');2241 lex->GetToken(tk);2242 exp = Ptree::List(ob, nil, new Leaf(tk));2243 return TRUE;// error recovery2244 }2245 2246 elist = Ptree::Snoc(elist, e);2247 t = lex->LookAhead(0);2248 if(t == '}')2249 break;2250 else if(t == ','){2251 lex->GetToken(tk);2252 elist = Ptree::Snoc(elist, new Leaf(tk));2253 t = lex->LookAhead(0);2254 }2255 else{2256 if(!SyntaxError())2257 return FALSE;// too many errors2258 2259 SkipTo('}');2260 lex->GetToken(tk);2261 exp = Ptree::List(ob, nil, new Leaf(tk));2262 return TRUE;// error recovery2263 }2264 }2265 2266 lex->GetToken(tk);2267 exp = new PtreeBrace(ob, elist, new Leaf(tk));2268 return TRUE;2248 lex->GetToken(tk); 2249 Ptree* ob = new Leaf(tk); 2250 elist = nil; 2251 int t = lex->LookAhead(0); 2252 while(t != '}'){ 2253 if(!rInitializeExpr(e)){ 2254 if(!SyntaxError()) 2255 return FALSE; // too many errors 2256 2257 SkipTo('}'); 2258 lex->GetToken(tk); 2259 exp = Ptree::List(ob, nil, new Leaf(tk)); 2260 return TRUE; // error recovery 2261 } 2262 2263 elist = Ptree::Snoc(elist, e); 2264 t = lex->LookAhead(0); 2265 if(t == '}') 2266 break; 2267 else if(t == ','){ 2268 lex->GetToken(tk); 2269 elist = Ptree::Snoc(elist, new Leaf(tk)); 2270 t = lex->LookAhead(0); 2271 } 2272 else{ 2273 if(!SyntaxError()) 2274 return FALSE; // too many errors 2275 2276 SkipTo('}'); 2277 lex->GetToken(tk); 2278 exp = Ptree::List(ob, nil, new Leaf(tk)); 2279 return TRUE; // error recovery 2280 } 2281 } 2282 2283 lex->GetToken(tk); 2284 exp = new PtreeBrace(ob, elist, new Leaf(tk)); 2285 return TRUE; 2269 2286 } 2270 2287 } … … 2284 2301 args = nil; 2285 2302 if(lex->LookAhead(0) == ')') 2286 return TRUE;2303 return TRUE; 2287 2304 2288 2305 for(;;){ 2289 if(!rExpression(exp))2290 return FALSE;2291 2292 args = Ptree::Snoc(args, exp);2293 if(lex->LookAhead(0) != ',')2294 return TRUE;2295 else{2296 lex->GetToken(tk);2297 args = Ptree::Snoc(args, new Leaf(tk));2298 }2306 if(!rExpression(exp)) 2307 return FALSE; 2308 2309 args = Ptree::Snoc(args, exp); 2310 if(lex->LookAhead(0) != ',') 2311 return TRUE; 2312 else{ 2313 lex->GetToken(tk); 2314 args = Ptree::Snoc(args, new Leaf(tk)); 2315 } 2299 2316 } 2300 2317 } … … 2311 2328 2312 2329 if(lex->GetToken(tk) != ENUM) 2313 return FALSE;2330 return FALSE; 2314 2331 2315 2332 spec = new PtreeEnumSpec(new Leaf(tk)); 2316 2333 int t = lex->GetToken(tk); 2317 2334 if(t == Identifier){ 2318 Ptree* name = new Leaf(tk);2319 encode.SimpleName(name);2320 ((PtreeEnumSpec*)spec)->encoded_name = encode.Get();2321 spec = Ptree::Snoc(spec, name);2322 if(lex->LookAhead(0) == '{')2323 t = lex->GetToken(tk);2324 else2325 return TRUE;2335 Ptree* name = new Leaf(tk); 2336 encode.SimpleName(name); 2337 ((PtreeEnumSpec*)spec)->encoded_name = encode.Get(); 2338 spec = Ptree::Snoc(spec, name); 2339 if(lex->LookAhead(0) == '{') 2340 t = lex->GetToken(tk); 2341 else 2342 return TRUE; 2326 2343 } 2327 2344 else{ 2328 encode.NoName();2329 ((PtreeEnumSpec*)spec)->encoded_name = encode.Get();2330 spec = Ptree::Snoc(spec, nil);2345 encode.NoName(); 2346 ((PtreeEnumSpec*)spec)->encoded_name = encode.Get(); 2347 spec = Ptree::Snoc(spec, nil); 2331 2348 } 2332 2349 2333 2350 if(t != '{') 2334 return FALSE;2351 return FALSE; 2335 2352 2336 2353 if(lex->LookAhead(0) == '}') 2337 body = nil;2354 body = nil; 2338 2355 else 2339 if(!rEnumBody(body))2340 return FALSE;2356 if(!rEnumBody(body)) 2357 return FALSE; 2341 2358 2342 2359 if(lex->GetToken(tk2) != '}') 2343 return FALSE;2360 return FALSE; 2344 2361 2345 2362 spec = Ptree::Snoc(spec, new PtreeBrace(new Leaf(tk), body, 2346 new Leaf(tk2)));2363 new Leaf(tk2))); 2347 2364 return TRUE; 2348 2365 } … … 2359 2376 body = nil; 2360 2377 for(;;){ 2361 if(lex->LookAhead(0) == '}')2362 return TRUE;2363 2364 if(lex->GetToken(tk) != Identifier)2365 return FALSE;2366 2367 if(lex->LookAhead(0, tk2) != '=')2368 name = new Leaf(tk);2369 else{2370 lex->GetToken(tk2);2371 if(!rExpression(exp)){2372 if(!SyntaxError())2373 return FALSE;// too many errors2374 2375 SkipTo('}');2376 body = nil;// empty2377 return TRUE;// error recovery2378 }2379 2380 name = Ptree::List(new Leaf(tk), new Leaf(tk2), exp);2381 }2382 2383 if(lex->LookAhead(0) != ','){2384 body = Ptree::Snoc(body, name);2385 return TRUE;2386 }2387 else{2388 lex->GetToken(tk);2389 body = Ptree::Nconc(body, Ptree::List(name, new Leaf(tk)));2390 }2378 if(lex->LookAhead(0) == '}') 2379 return TRUE; 2380 2381 if(lex->GetToken(tk) != Identifier) 2382 return FALSE; 2383 2384 if(lex->LookAhead(0, tk2) != '=') 2385 name = new Leaf(tk); 2386 else{ 2387 lex->GetToken(tk2); 2388 if(!rExpression(exp)){ 2389 if(!SyntaxError()) 2390 return FALSE; // too many errors 2391 2392 SkipTo('}'); 2393 body = nil; // empty 2394 return TRUE; // error recovery 2395 } 2396 2397 name = Ptree::List(new Leaf(tk), new Leaf(tk2), exp); 2398 } 2399 2400 if(lex->LookAhead(0) != ','){ 2401 body = Ptree::Snoc(body, name); 2402 return TRUE; 2403 } 2404 else{ 2405 lex->GetToken(tk); 2406 body = Ptree::Nconc(body, Ptree::List(name, new Leaf(tk))); 2407 } 2391 2408 } 2392 2409 } … … 2408 2425 head = nil; 2409 2426 if(lex->LookAhead(0) == UserKeyword) 2410 if(!rUserdefKeyword(head))2411 return FALSE;2427 if(!rUserdefKeyword(head)) 2428 return FALSE; 2412 2429 2413 2430 int t = lex->GetToken(tk); 2414 2431 if(t != CLASS && t != STRUCT && t != UNION) 2415 return FALSE;2432 return FALSE; 2416 2433 2417 2434 Ptree* comments = lex->GetComments(); 2418 2435 spec = new PtreeClassSpec(new LeafReserved(tk), nil, comments); 2419 2436 if(head != nil) 2420 spec = new PtreeClassSpec(head, spec, comments);2437 spec = new PtreeClassSpec(head, spec, comments); 2421 2438 2422 2439 if(lex->LookAhead(0) == '{'){ 2423 encode.NoName();2424 spec = Ptree::Snoc(spec, Ptree::List(nil, nil));2440 encode.NoName(); 2441 spec = Ptree::Snoc(spec, Ptree::List(nil, nil)); 2425 2442 } 2426 2443 else{ 2427 if(!rName(name, encode))2428 return FALSE;2429 2430 spec = Ptree::Snoc(spec, name);2431 t = lex->LookAhead(0);2432 if(t == ':'){2433 if(!rBaseSpecifiers(bases))2434 return FALSE;2435 2436 spec = Ptree::Snoc(spec, bases);2437 }2438 else if(t == '{')2439 spec = Ptree::Snoc(spec, nil);2440 else{2441 ((PtreeClassSpec*)spec)->encoded_name = encode.Get();2442 return TRUE;// class.key Identifier2443 }2444 if(!rName(name, encode)) 2445 return FALSE; 2446 2447 spec = Ptree::Snoc(spec, name); 2448 t = lex->LookAhead(0); 2449 if(t == ':'){ 2450 if(!rBaseSpecifiers(bases)) 2451 return FALSE; 2452 2453 spec = Ptree::Snoc(spec, bases); 2454 } 2455 else if(t == '{') 2456 spec = Ptree::Snoc(spec, nil); 2457 else{ 2458 ((PtreeClassSpec*)spec)->encoded_name = encode.Get(); 2459 return TRUE; // class.key Identifier 2460 } 2444 2461 } 2445 2462 2446 2463 ((PtreeClassSpec*)spec)->encoded_name = encode.Get(); 2447 2464 if(!rClassBody(body)) 2448 return FALSE;2465 return FALSE; 2449 2466 2450 2467 spec = Ptree::Snoc(spec, body); … … 2467 2484 2468 2485 if(lex->GetToken(tk) != ':') 2469 return FALSE;2486 return FALSE; 2470 2487 2471 2488 bases = Ptree::List(new Leaf(tk)); 2472 2489 for(;;){ 2473 Ptree* super = nil;2474 t = lex->LookAhead(0);2475 if(t == VIRTUAL){2476 lex->GetToken(tk);2477 super = Ptree::Snoc(super, new LeafVIRTUAL(tk));2478 t = lex->LookAhead(0);2479 }2480 2481 if(t == PUBLIC || t == PROTECTED || t == PRIVATE){ /* kso | -> || */2482 Ptree* lf;2483 switch(lex->GetToken(tk)){2484 case PUBLIC :2485 lf = new LeafPUBLIC(tk);2486 break;2487 case PROTECTED :2488 lf = new LeafPROTECTED(tk);2489 break;2490 case PRIVATE :2491 lf = new LeafPRIVATE(tk);2492 break;2493 default :2494 MopErrorMessage("rBaseSpecifiers()", "fatal");2495 lf = nil;2496 break;2497 }2498 2499 super = Ptree::Snoc(super, lf);2500 t = lex->LookAhead(0);2501 }2502 2503 if(t == VIRTUAL){2504 lex->GetToken(tk);2505 super = Ptree::Snoc(super, new LeafVIRTUAL(tk));2506 }2507 2508 encode.Clear();2509 if(!rName(name, encode))2510 return FALSE;2511 2512 if(!name->IsLeaf())2513 name = new PtreeName(name, encode);2514 2515 super = Ptree::Snoc(super, name);2516 bases = Ptree::Snoc(bases, super);2517 if(lex->LookAhead(0) != ',')2518 return TRUE;2519 else{2520 lex->GetToken(tk);2521 bases = Ptree::Snoc(bases, new Leaf(tk));2522 }2490 Ptree* super = nil; 2491 t = lex->LookAhead(0); 2492 if(t == VIRTUAL){ 2493 lex->GetToken(tk); 2494 super = Ptree::Snoc(super, new LeafVIRTUAL(tk)); 2495 t = lex->LookAhead(0); 2496 } 2497 2498 if(t == PUBLIC || t == PROTECTED || t == PRIVATE){ /* kso | -> || */ 2499 Ptree* lf; 2500 switch(lex->GetToken(tk)){ 2501 case PUBLIC : 2502 lf = new LeafPUBLIC(tk); 2503 break; 2504 case PROTECTED : 2505 lf = new LeafPROTECTED(tk); 2506 break; 2507 case PRIVATE : 2508 lf = new LeafPRIVATE(tk); 2509 break; 2510 default : 2511 MopErrorMessage("rBaseSpecifiers()", "fatal"); 2512 lf = nil; 2513 break; 2514 } 2515 2516 super = Ptree::Snoc(super, lf); 2517 t = lex->LookAhead(0); 2518 } 2519 2520 if(t == VIRTUAL){ 2521 lex->GetToken(tk); 2522 super = Ptree::Snoc(super, new LeafVIRTUAL(tk)); 2523 } 2524 2525 encode.Clear(); 2526 if(!rName(name, encode)) 2527 return FALSE; 2528 2529 if(!name->IsLeaf()) 2530 name = new PtreeName(name, encode); 2531 2532 super = Ptree::Snoc(super, name); 2533 bases = Ptree::Snoc(bases, super); 2534 if(lex->LookAhead(0) != ',') 2535 return TRUE; 2536 else{ 2537 lex->GetToken(tk); 2538 bases = Ptree::Snoc(bases, new Leaf(tk)); 2539 } 2523 2540 } 2524 2541 } … … 2533 2550 2534 2551 if(lex->GetToken(tk) != '{') 2535 return FALSE;2552 return FALSE; 2536 2553 2537 2554 Ptree* ob = new Leaf(tk); 2538 2555 mems = nil; 2539 2556 while(lex->LookAhead(0) != '}'){ 2540 if(!rClassMember(m)){2541 if(!SyntaxError())2542 return FALSE;// too many errors2543 2544 SkipTo('}');2545 lex->GetToken(tk);2546 body = Ptree::List(ob, nil, new Leaf(tk));2547 return TRUE;// error recovery2548 }2549 2550 lex->GetComments();2551 mems = Ptree::Snoc(mems, m);2557 if(!rClassMember(m)){ 2558 if(!SyntaxError()) 2559 return FALSE; // too many errors 2560 2561 SkipTo('}'); 2562 lex->GetToken(tk); 2563 body = Ptree::List(ob, nil, new Leaf(tk)); 2564 return TRUE; // error recovery 2565 } 2566 2567 lex->GetComments(); 2568 mems = Ptree::Snoc(mems, m); 2552 2569 } 2553 2570 … … 2578 2595 int t = lex->LookAhead(0); 2579 2596 if(t == PUBLIC || t == PROTECTED || t == PRIVATE){ 2580 Ptree* lf;2581 switch(lex->GetToken(tk1)){2582 case PUBLIC :2583 lf = new LeafPUBLIC(tk1);2584 break;2585 case PROTECTED :2586 lf = new LeafPROTECTED(tk1);2587 break;2588 case PRIVATE :2589 lf = new LeafPRIVATE(tk1);2590 break;2591 default :2592 MopErrorMessage("rClassMember()", "fatal");2593 lf = nil;2594 break;2595 }2596 2597 if(lex->GetToken(tk2) != ':')2598 return FALSE;2599 2600 mem = new PtreeAccessSpec(lf, Ptree::List(new Leaf(tk2)));2601 return TRUE;2597 Ptree* lf; 2598 switch(lex->GetToken(tk1)){ 2599 case PUBLIC : 2600 lf = new LeafPUBLIC(tk1); 2601 break; 2602 case PROTECTED : 2603 lf = new LeafPROTECTED(tk1); 2604 break; 2605 case PRIVATE : 2606 lf = new LeafPRIVATE(tk1); 2607 break; 2608 default : 2609 MopErrorMessage("rClassMember()", "fatal"); 2610 lf = nil; 2611 break; 2612 } 2613 2614 if(lex->GetToken(tk2) != ':') 2615 return FALSE; 2616 2617 mem = new PtreeAccessSpec(lf, Ptree::List(new Leaf(tk2))); 2618 return TRUE; 2602 2619 } 2603 2620 else if(t == UserKeyword4) 2604 return rUserAccessSpec(mem);2621 return rUserAccessSpec(mem); 2605 2622 else if(t == ';') 2606 return rNullDeclaration(mem);2623 return rNullDeclaration(mem); 2607 2624 else if(t == TYPEDEF) 2608 return rTypedef(mem);2625 return rTypedef(mem); 2609 2626 else if(t == TEMPLATE) 2610 return rTemplateDecl(mem);2627 return rTemplateDecl(mem); 2611 2628 else if(t == USING) 2612 return rUsing(mem);2629 return rUsing(mem); 2613 2630 else if(t == METACLASS) 2614 return rMetaclassDecl(mem);2631 return rMetaclassDecl(mem); 2615 2632 else{ 2616 char* pos = lex->Save();2617 Ptree* comments = lex->GetComments2();2618 if(rDeclaration(mem)) {2619 Walker::SetDeclaratorComments(mem, comments);2620 return TRUE;2621 }2622 2623 lex->Restore(pos);2624 return rAccessDecl(mem);2633 char* pos = lex->Save(); 2634 Ptree* comments = lex->GetComments2(); 2635 if(rDeclaration(mem)) { 2636 Walker::SetDeclaratorComments(mem, comments); 2637 return TRUE; 2638 } 2639 2640 lex->Restore(pos); 2641 return rAccessDecl(mem); 2625 2642 } 2626 2643 } … … 2628 2645 /* 2629 2646 access.decl 2630 : name ';' e.g. <qualified class>::<member name>;2647 : name ';' e.g. <qualified class>::<member name>; 2631 2648 */ 2632 2649 bool Parser::rAccessDecl(Ptree*& mem) … … 2637 2654 2638 2655 if(!rName(name, encode)) 2639 return FALSE;2656 return FALSE; 2640 2657 2641 2658 if(lex->GetToken(tk) != ';') 2642 return FALSE;2659 return FALSE; 2643 2660 2644 2661 mem = new PtreeAccessDecl(new PtreeName(name, encode), 2645 Ptree::List(new Leaf(tk)));2662 Ptree::List(new Leaf(tk))); 2646 2663 return TRUE; 2647 2664 } … … 2658 2675 2659 2676 if(lex->GetToken(tk1) != UserKeyword4) 2660 return FALSE;2677 return FALSE; 2661 2678 2662 2679 int t = lex->GetToken(tk2); 2663 2680 if(t == ':'){ 2664 mem = new PtreeUserAccessSpec(new Leaf(tk1),2665 Ptree::List(new Leaf(tk2)));2666 return TRUE;2681 mem = new PtreeUserAccessSpec(new Leaf(tk1), 2682 Ptree::List(new Leaf(tk2))); 2683 return TRUE; 2667 2684 } 2668 2685 else if(t == '('){ 2669 if(!rFunctionArguments(args))2670 return FALSE;2671 2672 if(lex->GetToken(tk3) != ')')2673 return FALSE;2674 2675 if(lex->GetToken(tk4) != ':')2676 return FALSE;2677 2678 mem = new PtreeUserAccessSpec(new Leaf(tk1),2679 Ptree::List(new Leaf(tk2), args,2680 new Leaf(tk3),2681 new Leaf(tk4)));2682 return TRUE;2686 if(!rFunctionArguments(args)) 2687 return FALSE; 2688 2689 if(lex->GetToken(tk3) != ')') 2690 return FALSE; 2691 2692 if(lex->GetToken(tk4) != ':') 2693 return FALSE; 2694 2695 mem = new PtreeUserAccessSpec(new Leaf(tk1), 2696 Ptree::List(new Leaf(tk2), args, 2697 new Leaf(tk3), 2698 new Leaf(tk4))); 2699 return TRUE; 2683 2700 } 2684 2701 else 2685 return FALSE;2702 return FALSE; 2686 2703 } 2687 2704 … … 2689 2706 comma.expression 2690 2707 : expression 2691 | comma.expression ',' expression (left-to-right)2708 | comma.expression ',' expression (left-to-right) 2692 2709 */ 2693 2710 bool Parser::rCommaExpression(Ptree*& exp) … … 2697 2714 2698 2715 if(!rExpression(exp)) 2699 return FALSE;2716 return FALSE; 2700 2717 2701 2718 while(lex->LookAhead(0) == ','){ 2702 lex->GetToken(tk);2703 if(!rExpression(right))2704 return FALSE;2705 2706 exp = new PtreeCommaExpr(exp, Ptree::List(new Leaf(tk), right));2719 lex->GetToken(tk); 2720 if(!rExpression(right)) 2721 return FALSE; 2722 2723 exp = new PtreeCommaExpr(exp, Ptree::List(new Leaf(tk), right)); 2707 2724 } 2708 2725 … … 2712 2729 /* 2713 2730 expression 2714 : conditional.expr {(AssignOp | '=') expression} right-to-left2731 : conditional.expr {(AssignOp | '=') expression} right-to-left 2715 2732 */ 2716 2733 bool Parser::rExpression(Ptree*& exp) … … 2720 2737 2721 2738 if(!rConditionalExpr(left)) 2722 return FALSE;2739 return FALSE; 2723 2740 2724 2741 int t = lex->LookAhead(0); 2725 2742 if(t != '=' && t != AssignOp) 2726 exp = left;2743 exp = left; 2727 2744 else{ 2728 lex->GetToken(tk);2729 if(!rExpression(right))2730 return FALSE;2731 2732 exp = new PtreeAssignExpr(left, Ptree::List(new Leaf(tk), right));2745 lex->GetToken(tk); 2746 if(!rExpression(right)) 2747 return FALSE; 2748 2749 exp = new PtreeAssignExpr(left, Ptree::List(new Leaf(tk), right)); 2733 2750 } 2734 2751 … … 2746 2763 2747 2764 if(!rLogicalOrExpr(exp, FALSE)) 2748 return FALSE;2765 return FALSE; 2749 2766 2750 2767 if(lex->LookAhead(0) == '?'){ 2751 lex->GetToken(tk1);2752 if(!rCommaExpression(then))2753 return FALSE;2754 2755 if(lex->GetToken(tk2) != ':')2756 return FALSE;2757 2758 if(!rConditionalExpr(otherwise))2759 return FALSE;2760 2761 exp = new PtreeCondExpr(exp, Ptree::List(new Leaf(tk1), then,2762 new Leaf(tk2), otherwise));2768 lex->GetToken(tk1); 2769 if(!rCommaExpression(then)) 2770 return FALSE; 2771 2772 if(lex->GetToken(tk2) != ':') 2773 return FALSE; 2774 2775 if(!rConditionalExpr(otherwise)) 2776 return FALSE; 2777 2778 exp = new PtreeCondExpr(exp, Ptree::List(new Leaf(tk1), then, 2779 new Leaf(tk2), otherwise)); 2763 2780 } 2764 2781 … … 2769 2786 logical.or.expr 2770 2787 : logical.and.expr 2771 | logical.or.expr LogOrOp logical.and.expr left-to-right2788 | logical.or.expr LogOrOp logical.and.expr left-to-right 2772 2789 */ 2773 2790 bool Parser::rLogicalOrExpr(Ptree*& exp, bool temp_args) … … 2777 2794 2778 2795 if(!rLogicalAndExpr(exp, temp_args)) 2779 return FALSE;2796 return FALSE; 2780 2797 2781 2798 while(lex->LookAhead(0) == LogOrOp){ 2782 lex->GetToken(tk);2783 if(!rLogicalAndExpr(right, temp_args))2784 return FALSE;2785 2786 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right));2799 lex->GetToken(tk); 2800 if(!rLogicalAndExpr(right, temp_args)) 2801 return FALSE; 2802 2803 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right)); 2787 2804 } 2788 2805 … … 2801 2818 2802 2819 if(!rInclusiveOrExpr(exp, temp_args)) 2803 return FALSE;2820 return FALSE; 2804 2821 2805 2822 while(lex->LookAhead(0) == LogAndOp){ 2806 lex->GetToken(tk);2807 if(!rInclusiveOrExpr(right, temp_args))2808 return FALSE;2809 2810 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right));2823 lex->GetToken(tk); 2824 if(!rInclusiveOrExpr(right, temp_args)) 2825 return FALSE; 2826 2827 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right)); 2811 2828 } 2812 2829 … … 2825 2842 2826 2843 if(!rExclusiveOrExpr(exp, temp_args)) 2827 return FALSE;2844 return FALSE; 2828 2845 2829 2846 while(lex->LookAhead(0) == '|'){ 2830 lex->GetToken(tk);2831 if(!rExclusiveOrExpr(right, temp_args))2832 return FALSE;2833 2834 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right));2847 lex->GetToken(tk); 2848 if(!rExclusiveOrExpr(right, temp_args)) 2849 return FALSE; 2850 2851 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right)); 2835 2852 } 2836 2853 … … 2849 2866 2850 2867 if(!rAndExpr(exp, temp_args)) 2851 return FALSE;2868 return FALSE; 2852 2869 2853 2870 while(lex->LookAhead(0) == '^'){ 2854 lex->GetToken(tk);2855 if(!rAndExpr(right, temp_args))2856 return FALSE;2857 2858 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right));2871 lex->GetToken(tk); 2872 if(!rAndExpr(right, temp_args)) 2873 return FALSE; 2874 2875 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right)); 2859 2876 } 2860 2877 … … 2873 2890 2874 2891 if(!rEqualityExpr(exp, temp_args)) 2875 return FALSE;2892 return FALSE; 2876 2893 2877 2894 while(lex->LookAhead(0) == '&'){ 2878 lex->GetToken(tk);2879 if(!rEqualityExpr(right, temp_args))2880 return FALSE;2881 2882 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right));2895 lex->GetToken(tk); 2896 if(!rEqualityExpr(right, temp_args)) 2897 return FALSE; 2898 2899 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right)); 2883 2900 } 2884 2901 … … 2897 2914 2898 2915 if(!rRelationalExpr(exp, temp_args)) 2899 return FALSE;2916 return FALSE; 2900 2917 2901 2918 while(lex->LookAhead(0) == EqualOp){ 2902 lex->GetToken(tk);2903 if(!rRelationalExpr(right, temp_args))2904 return FALSE;2905 2906 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right));2919 lex->GetToken(tk); 2920 if(!rRelationalExpr(right, temp_args)) 2921 return FALSE; 2922 2923 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right)); 2907 2924 } 2908 2925 … … 2922 2939 2923 2940 if(!rShiftExpr(exp)) 2924 return FALSE;2941 return FALSE; 2925 2942 2926 2943 while(t = lex->LookAhead(0), 2927 (t == RelOp || t == '<' || (t == '>' && !temp_args))){2928 lex->GetToken(tk);2929 if(!rShiftExpr(right))2930 return FALSE;2931 2932 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right));2944 (t == RelOp || t == '<' || (t == '>' && !temp_args))){ 2945 lex->GetToken(tk); 2946 if(!rShiftExpr(right)) 2947 return FALSE; 2948 2949 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right)); 2933 2950 } 2934 2951 … … 2947 2964 2948 2965 if(!rAdditiveExpr(exp)) 2949 return FALSE;2966 return FALSE; 2950 2967 2951 2968 while(lex->LookAhead(0) == ShiftOp){ 2952 lex->GetToken(tk);2953 if(!rAdditiveExpr(right))2954 return FALSE;2955 2956 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right));2969 lex->GetToken(tk); 2970 if(!rAdditiveExpr(right)) 2971 return FALSE; 2972 2973 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right)); 2957 2974 } 2958 2975 … … 2972 2989 2973 2990 if(!rMultiplyExpr(exp)) 2974 return FALSE;2991 return FALSE; 2975 2992 2976 2993 while(t = lex->LookAhead(0), (t == '+' || t == '-')){ 2977 lex->GetToken(tk);2978 if(!rMultiplyExpr(right))2979 return FALSE;2980 2981 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right));2994 lex->GetToken(tk); 2995 if(!rMultiplyExpr(right)) 2996 return FALSE; 2997 2998 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right)); 2982 2999 } 2983 3000 … … 2997 3014 2998 3015 if(!rPmExpr(exp)) 2999 return FALSE;3016 return FALSE; 3000 3017 3001 3018 while(t = lex->LookAhead(0), (t == '*' || t == '/' || t == '%')){ 3002 lex->GetToken(tk);3003 if(!rPmExpr(right))3004 return FALSE;3005 3006 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right));3007 } 3008 3009 return TRUE; 3010 } 3011 3012 /* 3013 pm.expr (pointer to member .*, ->*)3019 lex->GetToken(tk); 3020 if(!rPmExpr(right)) 3021 return FALSE; 3022 3023 exp = new PtreeInfixExpr(exp, Ptree::List(new Leaf(tk), right)); 3024 } 3025 3026 return TRUE; 3027 } 3028 3029 /* 3030 pm.expr (pointer to member .*, ->*) 3014 3031 : cast.expr 3015 3032 | pm.expr PmOp cast.expr … … 3021 3038 3022 3039 if(!rCastExpr(exp)) 3023 return FALSE;3040 return FALSE; 3024 3041 3025 3042 while(lex->LookAhead(0) == PmOp){ 3026 lex->GetToken(tk);3027 if(!rCastExpr(right))3028 return FALSE;3029 3030 exp = new PtreePmExpr(exp, Ptree::List(new Leaf(tk), right));3043 lex->GetToken(tk); 3044 if(!rCastExpr(right)) 3045 return FALSE; 3046 3047 exp = new PtreePmExpr(exp, Ptree::List(new Leaf(tk), right)); 3031 3048 } 3032 3049 … … 3042 3059 { 3043 3060 if(lex->LookAhead(0) != '(') 3044 return rUnaryExpr(exp);3061 return rUnaryExpr(exp); 3045 3062 else{ 3046 Token tk1, tk2;3047 Ptree* tname;3048 char* pos = lex->Save();3049 lex->GetToken(tk1);3050 if(rTypeName(tname))3051 if(lex->GetToken(tk2) == ')')3052 if(rCastExpr(exp)){3053 exp = new PtreeCastExpr(new Leaf(tk1),3054 Ptree::List(tname, new Leaf(tk2),3055 exp));3056 return TRUE;3057 }3058 3059 lex->Restore(pos);3060 return rUnaryExpr(exp);3063 Token tk1, tk2; 3064 Ptree* tname; 3065 char* pos = lex->Save(); 3066 lex->GetToken(tk1); 3067 if(rTypeName(tname)) 3068 if(lex->GetToken(tk2) == ')') 3069 if(rCastExpr(exp)){ 3070 exp = new PtreeCastExpr(new Leaf(tk1), 3071 Ptree::List(tname, new Leaf(tk2), 3072 exp)); 3073 return TRUE; 3074 } 3075 3076 lex->Restore(pos); 3077 return rUnaryExpr(exp); 3061 3078 } 3062 3079 } … … 3078 3095 3079 3096 if(!rTypeSpecifier(type_name, TRUE, type_encode)) 3080 return FALSE;3097 return FALSE; 3081 3098 3082 3099 if(!rDeclarator(arg, kCastDeclarator, FALSE, type_encode, name_encode, 3083 FALSE))3084 return FALSE;3100 FALSE)) 3101 return FALSE; 3085 3102 3086 3103 tname = Ptree::List(type_name, arg); … … 3101 3118 if(t == '*' || t == '&' || t == '+' || t == '-' || t == '!' 3102 3119 || t == '~' || t == IncOp){ 3103 Token tk;3104 Ptree* right;3105 3106 lex->GetToken(tk);3107 if(!rCastExpr(right))3108 return FALSE;3109 3110 exp = new PtreeUnaryExpr(new Leaf(tk), Ptree::List(right));3111 return TRUE;3120 Token tk; 3121 Ptree* right; 3122 3123 lex->GetToken(tk); 3124 if(!rCastExpr(right)) 3125 return FALSE; 3126 3127 exp = new PtreeUnaryExpr(new Leaf(tk), Ptree::List(right)); 3128 return TRUE; 3112 3129 } 3113 3130 else if(t == SIZEOF) 3114 return rSizeofExpr(exp);3131 return rSizeofExpr(exp); 3115 3132 else if(t == THROW) 3116 return rThrowExpr(exp);3133 return rThrowExpr(exp); 3117 3134 else if(isAllocateExpr(t)) 3118 return rAllocateExpr(exp);3135 return rAllocateExpr(exp); 3119 3136 else 3120 return rPostfixExpr(exp);3137 return rPostfixExpr(exp); 3121 3138 } 3122 3139 … … 3131 3148 3132 3149 if(lex->GetToken(tk) != THROW) 3133 return FALSE;3150 return FALSE; 3134 3151 3135 3152 int t = lex->LookAhead(0); 3136 3153 if(t == ':' || t == ';') 3137 e = nil;3154 e = nil; 3138 3155 else 3139 if(!rExpression(e))3140 return FALSE;3156 if(!rExpression(e)) 3157 return FALSE; 3141 3158 3142 3159 exp = new PtreeThrowExpr(new LeafReserved(tk), Ptree::List(e)); … … 3155 3172 3156 3173 if(lex->GetToken(tk) != SIZEOF) 3157 return FALSE;3174 return FALSE; 3158 3175 3159 3176 if(lex->LookAhead(0) == '('){ 3160 Ptree* tname;3161 Token op, cp;3162 3163 char* pos = lex->Save();3164 lex->GetToken(op);3165 if(rTypeName(tname))3166 if(lex->GetToken(cp) == ')'){3167 exp = new PtreeSizeofExpr(new Leaf(tk),3168 Ptree::List(new Leaf(op), tname,3169 new Leaf(cp)));3170 return TRUE;3171 }3172 3173 lex->Restore(pos);3177 Ptree* tname; 3178 Token op, cp; 3179 3180 char* pos = lex->Save(); 3181 lex->GetToken(op); 3182 if(rTypeName(tname)) 3183 if(lex->GetToken(cp) == ')'){ 3184 exp = new PtreeSizeofExpr(new Leaf(tk), 3185 Ptree::List(new Leaf(op), tname, 3186 new Leaf(cp))); 3187 return TRUE; 3188 } 3189 3190 lex->Restore(pos); 3174 3191 } 3175 3192 3176 3193 if(!rUnaryExpr(unary)) 3177 return FALSE;3194 return FALSE; 3178 3195 3179 3196 exp = new PtreeSizeofExpr(new Leaf(tk), Ptree::List(unary)); … … 3184 3201 { 3185 3202 if(t == UserKeyword) 3186 return TRUE;3203 return TRUE; 3187 3204 else{ 3188 if(t == Scope)3189 t = lex->LookAhead(1);3190 3191 if(t == NEW || t == DELETE)3192 return TRUE;3193 else3194 return FALSE;3205 if(t == Scope) 3206 t = lex->LookAhead(1); 3207 3208 if(t == NEW || t == DELETE) 3209 return TRUE; 3210 else 3211 return FALSE; 3195 3212 } 3196 3213 } … … 3209 3226 int t = lex->LookAhead(0); 3210 3227 if(t == Scope){ 3211 lex->GetToken(tk);3212 head = new Leaf(tk);3228 lex->GetToken(tk); 3229 head = new Leaf(tk); 3213 3230 } 3214 3231 else if(t == UserKeyword){ 3215 if(!rUserdefKeyword(head))3216 return FALSE;3217 3218 ukey = TRUE;3232 if(!rUserdefKeyword(head)) 3233 return FALSE; 3234 3235 ukey = TRUE; 3219 3236 } 3220 3237 3221 3238 t = lex->GetToken(tk); 3222 3239 if(t == DELETE){ 3223 Ptree* obj;3224 if(ukey)3225 return FALSE;3226 3227 if(head == nil)3228 exp = new PtreeDeleteExpr(new LeafReserved(tk), nil);3229 else3230 exp = new PtreeDeleteExpr(head,3231 Ptree::List(new LeafReserved(tk)));3232 3233 if(lex->LookAhead(0) == '['){3234 lex->GetToken(tk);3235 exp = Ptree::Snoc(exp, new Leaf(tk));3236 if(lex->GetToken(tk) != ']')3237 return FALSE;3238 3239 exp = Ptree::Snoc(exp, new Leaf(tk));3240 }3241 3242 if(!rCastExpr(obj))3243 return FALSE;3244 3245 exp = Ptree::Snoc(exp, obj);3246 return TRUE;3240 Ptree* obj; 3241 if(ukey) 3242 return FALSE; 3243 3244 if(head == nil) 3245 exp = new PtreeDeleteExpr(new LeafReserved(tk), nil); 3246 else 3247 exp = new PtreeDeleteExpr(head, 3248 Ptree::List(new LeafReserved(tk))); 3249 3250 if(lex->LookAhead(0) == '['){ 3251 lex->GetToken(tk); 3252 exp = Ptree::Snoc(exp, new Leaf(tk)); 3253 if(lex->GetToken(tk) != ']') 3254 return FALSE; 3255 3256 exp = Ptree::Snoc(exp, new Leaf(tk)); 3257 } 3258 3259 if(!rCastExpr(obj)) 3260 return FALSE; 3261 3262 exp = Ptree::Snoc(exp, obj); 3263 return TRUE; 3247 3264 } 3248 3265 else if(t == NEW){ 3249 Ptree *atype;3250 if(head == nil)3251 exp = new PtreeNewExpr(new LeafReserved(tk), nil);3252 else3253 exp = new PtreeNewExpr(head, Ptree::List(new LeafReserved(tk)));3254 3255 if(!rAllocateType(atype))3256 return FALSE;3257 3258 exp = Ptree::Nconc(exp, atype);3259 return TRUE;3266 Ptree *atype; 3267 if(head == nil) 3268 exp = new PtreeNewExpr(new LeafReserved(tk), nil); 3269 else 3270 exp = new PtreeNewExpr(head, Ptree::List(new LeafReserved(tk))); 3271 3272 if(!rAllocateType(atype)) 3273 return FALSE; 3274 3275 exp = Ptree::Nconc(exp, atype); 3276 return TRUE; 3260 3277 } 3261 3278 else 3262 return FALSE;3279 return FALSE; 3263 3280 } 3264 3281 … … 3273 3290 int t = lex->GetToken(tk); 3274 3291 if(t != UserKeyword && t != UserKeyword5) 3275 return FALSE;3292 return FALSE; 3276 3293 3277 3294 if(lex->LookAhead(0) != '(') 3278 ukey = new PtreeUserdefKeyword(new Leaf(tk), nil);3295 ukey = new PtreeUserdefKeyword(new Leaf(tk), nil); 3279 3296 else{ 3280 Ptree* args;3281 Token op, cp;3282 lex->GetToken(op);3283 if(!rFunctionArguments(args))3284 return FALSE;3285 3286 if(lex->GetToken(cp) != ')')3287 return FALSE;3288 3289 ukey = new PtreeUserdefKeyword(new Leaf(tk),3290 Ptree::List(new Leaf(op), args, new Leaf(cp)));3297 Ptree* args; 3298 Token op, cp; 3299 lex->GetToken(op); 3300 if(!rFunctionArguments(args)) 3301 return FALSE; 3302 3303 if(lex->GetToken(cp) != ')') 3304 return FALSE; 3305 3306 ukey = new PtreeUserdefKeyword(new Leaf(tk), 3307 Ptree::List(new Leaf(op), args, new Leaf(cp))); 3291 3308 } 3292 3309 … … 3306 3323 3307 3324 if(lex->LookAhead(0) != '(') 3308 atype = Ptree::List(nil);3325 atype = Ptree::List(nil); 3309 3326 else{ 3310 lex->GetToken(op);3311 3312 char* pos = lex->Save();3313 if(rTypeName(tname))3314 if(lex->GetToken(cp) == ')')3315 if(lex->LookAhead(0) != '('){3316 atype = Ptree::List(nil, Ptree::List(new Leaf(op), tname,3317 new Leaf(cp)));3318 if(!isTypeSpecifier())3319 return TRUE;3320 }3321 else if(rAllocateInitializer(init)){3322 atype = Ptree::List(nil,3323 Ptree::List(new Leaf(op), tname,3324 new Leaf(cp)),3325 init);3326 // the next token cannot be '('3327 if(lex->LookAhead(0) != '(')3328 return TRUE;3329 }3330 3331 // if we reach here, we have to process '(' function.arguments ')'.3332 lex->Restore(pos);3333 if(!rFunctionArguments(exp))3334 return FALSE;3335 3336 if(lex->GetToken(cp) != ')')3337 return FALSE;3338 3339 atype = Ptree::List(Ptree::List(new Leaf(op), exp, new Leaf(cp)));3327 lex->GetToken(op); 3328 3329 char* pos = lex->Save(); 3330 if(rTypeName(tname)) 3331 if(lex->GetToken(cp) == ')') 3332 if(lex->LookAhead(0) != '('){ 3333 atype = Ptree::List(nil, Ptree::List(new Leaf(op), tname, 3334 new Leaf(cp))); 3335 if(!isTypeSpecifier()) 3336 return TRUE; 3337 } 3338 else if(rAllocateInitializer(init)){ 3339 atype = Ptree::List(nil, 3340 Ptree::List(new Leaf(op), tname, 3341 new Leaf(cp)), 3342 init); 3343 // the next token cannot be '(' 3344 if(lex->LookAhead(0) != '(') 3345 return TRUE; 3346 } 3347 3348 // if we reach here, we have to process '(' function.arguments ')'. 3349 lex->Restore(pos); 3350 if(!rFunctionArguments(exp)) 3351 return FALSE; 3352 3353 if(lex->GetToken(cp) != ')') 3354 return FALSE; 3355 3356 atype = Ptree::List(Ptree::List(new Leaf(op), exp, new Leaf(cp))); 3340 3357 } 3341 3358 3342 3359 if(lex->LookAhead(0) == '('){ 3343 lex->GetToken(op);3344 if(!rTypeName(tname))3345 return FALSE;3346 3347 if(lex->GetToken(cp) != ')')3348 return FALSE;3349 3350 atype = Ptree::Snoc(atype, Ptree::List(new Leaf(op), tname,3351 new Leaf(cp)));3360 lex->GetToken(op); 3361 if(!rTypeName(tname)) 3362 return FALSE; 3363 3364 if(lex->GetToken(cp) != ')') 3365 return FALSE; 3366 3367 atype = Ptree::Snoc(atype, Ptree::List(new Leaf(op), tname, 3368 new Leaf(cp))); 3352 3369 } 3353 3370 else{ 3354 Ptree* decl;3355 Encoding type_encode;3356 if(!rTypeSpecifier(tname, FALSE, type_encode))3357 return FALSE;3358 3359 if(!rNewDeclarator(decl, type_encode))3360 return FALSE;3361 3362 atype = Ptree::Snoc(atype, Ptree::List(tname, decl));3371 Ptree* decl; 3372 Encoding type_encode; 3373 if(!rTypeSpecifier(tname, FALSE, type_encode)) 3374 return FALSE; 3375 3376 if(!rNewDeclarator(decl, type_encode)) 3377 return FALSE; 3378 3379 atype = Ptree::Snoc(atype, Ptree::List(tname, decl)); 3363 3380 } 3364 3381 3365 3382 if(lex->LookAhead(0) == '('){ 3366 if(!rAllocateInitializer(init))3367 return FALSE;3368 3369 atype = Ptree::Snoc(atype, init);3383 if(!rAllocateInitializer(init)) 3384 return FALSE; 3385 3386 atype = Ptree::Snoc(atype, init); 3370 3387 } 3371 3388 … … 3383 3400 decl = nil; 3384 3401 if(lex->LookAhead(0) != '[') 3385 if(!optPtrOperator(decl, encode))3386 return FALSE;3402 if(!optPtrOperator(decl, encode)) 3403 return FALSE; 3387 3404 3388 3405 while(lex->LookAhead(0) == '['){ 3389 Token ob, cb;3390 Ptree* exp;3391 lex->GetToken(ob);3392 if(!rCommaExpression(exp))3393 return FALSE;3394 3395 if(lex->GetToken(cb) != ']')3396 return FALSE;3397 3398 encode.Array();3399 decl = Ptree::Nconc(decl, Ptree::List(new Leaf(ob), exp,3400 new Leaf(cb)));3406 Token ob, cb; 3407 Ptree* exp; 3408 lex->GetToken(ob); 3409 if(!rCommaExpression(exp)) 3410 return FALSE; 3411 3412 if(lex->GetToken(cb) != ']') 3413 return FALSE; 3414 3415 encode.Array(); 3416 decl = Ptree::Nconc(decl, Ptree::List(new Leaf(ob), exp, 3417 new Leaf(cb))); 3401 3418 } 3402 3419 3403 3420 if(decl == nil) 3404 decl = new PtreeDeclarator(encode);3421 decl = new PtreeDeclarator(encode); 3405 3422 else 3406 decl = new PtreeDeclarator(decl, encode);3423 decl = new PtreeDeclarator(decl, encode); 3407 3424 3408 3425 return TRUE; … … 3418 3435 3419 3436 if(lex->GetToken(op) != '(') 3420 return FALSE;3437 return FALSE; 3421 3438 3422 3439 if(lex->LookAhead(0) == ')'){ 3423 lex->GetToken(cp);3424 init = Ptree::List(new Leaf(op), nil, new Leaf(cp));3425 return TRUE;3440 lex->GetToken(cp); 3441 init = Ptree::List(new Leaf(op), nil, new Leaf(cp)); 3442 return TRUE; 3426 3443 } 3427 3444 3428 3445 init = nil; 3429 3446 for(;;){ 3430 Ptree* exp;3431 if(!rInitializeExpr(exp))3432 return FALSE;3433 3434 init = Ptree::Snoc(init, exp);3435 if(lex->LookAhead(0) != ',')3436 break;3437 else{3438 Token tk;3439 lex->GetToken(tk);3440 init = Ptree::Snoc(init, new Leaf(tk));3441 }3447 Ptree* exp; 3448 if(!rInitializeExpr(exp)) 3449 return FALSE; 3450 3451 init = Ptree::Snoc(init, exp); 3452 if(lex->LookAhead(0) != ',') 3453 break; 3454 else{ 3455 Token tk; 3456 lex->GetToken(tk); 3457 init = Ptree::Snoc(init, new Leaf(tk)); 3458 } 3442 3459 } 3443 3460 … … 3470 3487 3471 3488 if(!rPrimaryExpr(exp)) 3472 return FALSE;3489 return FALSE; 3473 3490 3474 3491 for(;;){ 3475 switch(lex->LookAhead(0)){3476 case '[' :3477 lex->GetToken(op);3478 if(!rCommaExpression(e))3479 return FALSE;3480 3481 if(lex->GetToken(cp) != ']')3482 return FALSE;3483 3484 exp = new PtreeArrayExpr(exp, Ptree::List(new Leaf(op),3485 e, new Leaf(cp)));3486 break;3487 case '(' :3488 lex->GetToken(op);3489 if(!rFunctionArguments(e))3490 return FALSE;3491 3492 if(lex->GetToken(cp) != ')')3493 return FALSE;3494 3495 exp = new PtreeFuncallExpr(exp, Ptree::List(new Leaf(op),3496 e, new Leaf(cp)));3497 break;3498 case IncOp :3499 lex->GetToken(op);3500 exp = new PtreePostfixExpr(exp, Ptree::List(new Leaf(op)));3501 break;3502 case '.' :3503 case ArrowOp :3504 t2 = lex->GetToken(op);3505 t = lex->LookAhead(0);3506 if(t == UserKeyword || t == UserKeyword2 || t == UserKeyword3){3507 if(!rUserdefStatement(e))3508 return FALSE;3509 3510 exp = new PtreeUserStatementExpr(exp,3511 Ptree::Cons(new Leaf(op), e));3512 break;3513 }3514 else{3515 if(!rVarName(e))3516 return FALSE;3517 3518 if(t2 == '.')3519 exp = new PtreeDotMemberExpr(exp,3520 Ptree::List(new Leaf(op), e));3521 else3522 exp = new PtreeArrowMemberExpr(exp,3523 Ptree::List(new Leaf(op), e));3524 break;3525 }3526 default :3527 return TRUE;3528 }3492 switch(lex->LookAhead(0)){ 3493 case '[' : 3494 lex->GetToken(op); 3495 if(!rCommaExpression(e)) 3496 return FALSE; 3497 3498 if(lex->GetToken(cp) != ']') 3499 return FALSE; 3500 3501 exp = new PtreeArrayExpr(exp, Ptree::List(new Leaf(op), 3502 e, new Leaf(cp))); 3503 break; 3504 case '(' : 3505 lex->GetToken(op); 3506 if(!rFunctionArguments(e)) 3507 return FALSE; 3508 3509 if(lex->GetToken(cp) != ')') 3510 return FALSE; 3511 3512 exp = new PtreeFuncallExpr(exp, Ptree::List(new Leaf(op), 3513 e, new Leaf(cp))); 3514 break; 3515 case IncOp : 3516 lex->GetToken(op); 3517 exp = new PtreePostfixExpr(exp, Ptree::List(new Leaf(op))); 3518 break; 3519 case '.' : 3520 case ArrowOp : 3521 t2 = lex->GetToken(op); 3522 t = lex->LookAhead(0); 3523 if(t == UserKeyword || t == UserKeyword2 || t == UserKeyword3){ 3524 if(!rUserdefStatement(e)) 3525 return FALSE; 3526 3527 exp = new PtreeUserStatementExpr(exp, 3528 Ptree::Cons(new Leaf(op), e)); 3529 break; 3530 } 3531 else{ 3532 if(!rVarName(e)) 3533 return FALSE; 3534 3535 if(t2 == '.') 3536 exp = new PtreeDotMemberExpr(exp, 3537 Ptree::List(new Leaf(op), e)); 3538 else 3539 exp = new PtreeArrowMemberExpr(exp, 3540 Ptree::List(new Leaf(op), e)); 3541 break; 3542 } 3543 default : 3544 return TRUE; 3545 } 3529 3546 } 3530 3547 } … … 3552 3569 switch(lex->LookAhead(0)){ 3553 3570 case Constant : case CharConst : case StringL : 3554 lex->GetToken(tk);3555 exp = new Leaf(tk);3556 return TRUE;3571 lex->GetToken(tk); 3572 exp = new Leaf(tk); 3573 return TRUE; 3557 3574 case THIS : 3558 lex->GetToken(tk);3559 exp = new LeafThis(tk);3560 return TRUE;3575 lex->GetToken(tk); 3576 exp = new LeafThis(tk); 3577 return TRUE; 3561 3578 case '(' : 3562 lex->GetToken(tk);3563 if(!rCommaExpression(exp2))3564 return FALSE;3565 3566 if(lex->GetToken(tk2) != ')')3567 return FALSE;3568 3569 exp = new PtreeParenExpr(new Leaf(tk),3570 Ptree::List(exp2, new Leaf(tk2)));3571 return TRUE;3579 lex->GetToken(tk); 3580 if(!rCommaExpression(exp2)) 3581 return FALSE; 3582 3583 if(lex->GetToken(tk2) != ')') 3584 return FALSE; 3585 3586 exp = new PtreeParenExpr(new Leaf(tk), 3587 Ptree::List(exp2, new Leaf(tk2))); 3588 return TRUE; 3572 3589 default : 3573 if(!optIntegralTypeOrClassSpec(exp, cast_type_encode))3574 return FALSE;3575 3576 if(exp != nil){// if integral.or.class.spec3577 if(lex->GetToken(tk) != '(')3578 return FALSE;3579 3580 if(!rFunctionArguments(exp2))3581 return FALSE;3582 3583 if(lex->GetToken(tk2) != ')')3584 return FALSE;3585 3586 exp = new PtreeFstyleCastExpr(cast_type_encode, exp,3587 Ptree::List(new Leaf(tk), exp2,3588 new Leaf(tk2)));3589 return TRUE;3590 }3591 else{3592 if(!rVarName(exp))3593 return FALSE;3594 3595 if(lex->LookAhead(0) == Scope){3596 lex->GetToken(tk);3597 if(!rUserdefStatement(exp2))3598 return FALSE;3599 3600 exp = new PtreeStaticUserStatementExpr(exp,3601 Ptree::Cons(new Leaf(tk), exp2));3602 }3603 3604 return TRUE;3605 }3590 if(!optIntegralTypeOrClassSpec(exp, cast_type_encode)) 3591 return FALSE; 3592 3593 if(exp != nil){ // if integral.or.class.spec 3594 if(lex->GetToken(tk) != '(') 3595 return FALSE; 3596 3597 if(!rFunctionArguments(exp2)) 3598 return FALSE; 3599 3600 if(lex->GetToken(tk2) != ')') 3601 return FALSE; 3602 3603 exp = new PtreeFstyleCastExpr(cast_type_encode, exp, 3604 Ptree::List(new Leaf(tk), exp2, 3605 new Leaf(tk2))); 3606 return TRUE; 3607 } 3608 else{ 3609 if(!rVarName(exp)) 3610 return FALSE; 3611 3612 if(lex->LookAhead(0) == Scope){ 3613 lex->GetToken(tk); 3614 if(!rUserdefStatement(exp2)) 3615 return FALSE; 3616 3617 exp = new PtreeStaticUserStatementExpr(exp, 3618 Ptree::Cons(new Leaf(tk), exp2)); 3619 } 3620 3621 return TRUE; 3622 } 3606 3623 } 3607 3624 } … … 3612 3629 | UserKeyword2 '(' arg.decl.list ')' compound.statement 3613 3630 | UserKeyword3 '(' expr.statement {comma.expression} ';' 3614 {comma.expression} ')' compound.statement3631 {comma.expression} ')' compound.statement 3615 3632 */ 3616 3633 bool Parser::rUserdefStatement(Ptree*& st) … … 3622 3639 int t = lex->GetToken(tk); 3623 3640 if(lex->GetToken(tk2) != '(') 3624 return FALSE;3641 return FALSE; 3625 3642 3626 3643 switch(t){ 3627 3644 case UserKeyword : 3628 keyword = new LeafReserved(tk);3629 if(!rFunctionArguments(exp))3630 return FALSE;3631 goto rest;3645 keyword = new LeafReserved(tk); 3646 if(!rFunctionArguments(exp)) 3647 return FALSE; 3648 goto rest; 3632 3649 case UserKeyword2 : 3633 keyword = new LeafUserKeyword2(tk);3634 if(!rArgDeclList(exp, dummy_encode))3635 return FALSE;3650 keyword = new LeafUserKeyword2(tk); 3651 if(!rArgDeclList(exp, dummy_encode)) 3652 return FALSE; 3636 3653 rest: 3637 if(lex->GetToken(tk3) != ')')3638 return FALSE;3639 3640 if(!rCompoundStatement(body))3641 return FALSE;3642 3643 st = Ptree::List(keyword, new Leaf(tk2), exp, new Leaf(tk3),3644 body);3645 return TRUE;3654 if(lex->GetToken(tk3) != ')') 3655 return FALSE; 3656 3657 if(!rCompoundStatement(body)) 3658 return FALSE; 3659 3660 st = Ptree::List(keyword, new Leaf(tk2), exp, new Leaf(tk3), 3661 body); 3662 return TRUE; 3646 3663 case UserKeyword3 : 3647 if(!rExprStatement(exp))3648 return FALSE;3649 3650 if(lex->LookAhead(0) == ';')3651 exp2 = nil;3652 else3653 if(!rCommaExpression(exp2))3654 return FALSE;3655 3656 if(lex->GetToken(tk3) != ';')3657 return FALSE;3658 3659 if(lex->LookAhead(0) == ')')3660 exp3 = nil;3661 else3662 if(!rCommaExpression(exp3))3663 return FALSE;3664 3665 if(lex->GetToken(tk4) != ')')3666 return FALSE;3667 3668 if(!rCompoundStatement(body))3669 return FALSE;3670 3671 st = Ptree::List(new Leaf(tk), new Leaf(tk2), exp, exp2,3672 new Leaf(tk3), exp3, new Leaf(tk4), body);3673 return TRUE;3664 if(!rExprStatement(exp)) 3665 return FALSE; 3666 3667 if(lex->LookAhead(0) == ';') 3668 exp2 = nil; 3669 else 3670 if(!rCommaExpression(exp2)) 3671 return FALSE; 3672 3673 if(lex->GetToken(tk3) != ';') 3674 return FALSE; 3675 3676 if(lex->LookAhead(0) == ')') 3677 exp3 = nil; 3678 else 3679 if(!rCommaExpression(exp3)) 3680 return FALSE; 3681 3682 if(lex->GetToken(tk4) != ')') 3683 return FALSE; 3684 3685 if(!rCompoundStatement(body)) 3686 return FALSE; 3687 3688 st = Ptree::List(new Leaf(tk), new Leaf(tk2), exp, exp2, 3689 new Leaf(tk3), exp3, new Leaf(tk4), body); 3690 return TRUE; 3674 3691 default : 3675 return FALSE;3692 return FALSE; 3676 3693 } 3677 3694 } … … 3692 3709 3693 3710 if(rVarNameCore(name, encode)){ 3694 if(!name->IsLeaf())3695 name = new PtreeName(name, encode);3696 3697 return TRUE;3711 if(!name->IsLeaf()) 3712 name = new PtreeName(name, encode); 3713 3714 return TRUE; 3698 3715 } 3699 3716 else 3700 return FALSE;3717 return FALSE; 3701 3718 } 3702 3719 … … 3707 3724 3708 3725 if(lex->LookAhead(0) == Scope){ 3709 lex->GetToken(tk);3710 name = Ptree::List(new Leaf(tk));3711 encode.GlobalScope();3712 ++length;3726 lex->GetToken(tk); 3727 name = Ptree::List(new Leaf(tk)); 3728 encode.GlobalScope(); 3729 ++length; 3713 3730 } 3714 3731 else 3715 name = nil;3732 name = nil; 3716 3733 3717 3734 for(;;){ 3718 int t = lex->GetToken(tk);3719 if(t == Identifier){3720 Ptree* n = new LeafName(tk);3721 if(isTemplateArgs()){3722 Ptree* args;3723 Encoding args_encode;3724 if(!rTemplateArgs(args, args_encode))3725 return FALSE;3726 3727 encode.Template(n, args_encode);3728 ++length;3729 n = Ptree::List(n, args);3730 }3731 else{3732 encode.SimpleName(n);3733 ++length;3734 }3735 3736 if(moreVarName()){3737 lex->GetToken(tk);3738 name = Ptree::Nconc(name, Ptree::List(n, new Leaf(tk)));3739 }3740 else{3741 if(name == nil)3742 name = n;3743 else3744 name = Ptree::Snoc(name, n);3745 3746 if(length > 1)3747 encode.Qualified(length);3748 3749 return TRUE;3750 }3751 }3752 else if(t == '~'){3753 Token tk2;3754 if(lex->LookAhead(0) != Identifier)3755 return FALSE;3756 3757 lex->GetToken(tk2);3758 Ptree* class_name = new Leaf(tk2);3759 Ptree* dt = Ptree::List(new Leaf(tk), class_name);3760 if(name == nil)3761 name = dt;3762 else3763 name = Ptree::Snoc(name, dt);3764 3765 encode.Destructor(class_name);3766 if(length > 0)3767 encode.Qualified(length + 1);3768 3769 return TRUE;3770 }3771 else if(t == OPERATOR){3772 Ptree* op;3773 if(!rOperatorName(op, encode))3774 return FALSE;3775 3776 Ptree* opf = Ptree::List(new LeafReserved(tk), op);3777 if(name == nil)3778 name = opf;3779 else3780 name = Ptree::Snoc(name, opf);3781 3782 if(length > 0)3783 encode.Qualified(length + 1);3784 3785 return TRUE;3786 }3787 else3788 return FALSE;3735 int t = lex->GetToken(tk); 3736 if(t == Identifier){ 3737 Ptree* n = new LeafName(tk); 3738 if(isTemplateArgs()){ 3739 Ptree* args; 3740 Encoding args_encode; 3741 if(!rTemplateArgs(args, args_encode)) 3742 return FALSE; 3743 3744 encode.Template(n, args_encode); 3745 ++length; 3746 n = Ptree::List(n, args); 3747 } 3748 else{ 3749 encode.SimpleName(n); 3750 ++length; 3751 } 3752 3753 if(moreVarName()){ 3754 lex->GetToken(tk); 3755 name = Ptree::Nconc(name, Ptree::List(n, new Leaf(tk))); 3756 } 3757 else{ 3758 if(name == nil) 3759 name = n; 3760 else 3761 name = Ptree::Snoc(name, n); 3762 3763 if(length > 1) 3764 encode.Qualified(length); 3765 3766 return TRUE; 3767 } 3768 } 3769 else if(t == '~'){ 3770 Token tk2; 3771 if(lex->LookAhead(0) != Identifier) 3772 return FALSE; 3773 3774 lex->GetToken(tk2); 3775 Ptree* class_name = new Leaf(tk2); 3776 Ptree* dt = Ptree::List(new Leaf(tk), class_name); 3777 if(name == nil) 3778 name = dt; 3779 else 3780 name = Ptree::Snoc(name, dt); 3781 3782 encode.Destructor(class_name); 3783 if(length > 0) 3784 encode.Qualified(length + 1); 3785 3786 return TRUE; 3787 } 3788 else if(t == OPERATOR){ 3789 Ptree* op; 3790 if(!rOperatorName(op, encode)) 3791 return FALSE; 3792 3793 Ptree* opf = Ptree::List(new LeafReserved(tk), op); 3794 if(name == nil) 3795 name = opf; 3796 else 3797 name = Ptree::Snoc(name, opf); 3798 3799 if(length > 0) 3800 encode.Qualified(length + 1); 3801 3802 return TRUE; 3803 } 3804 else 3805 return FALSE; 3789 3806 } 3790 3807 } … … 3793 3810 { 3794 3811 if(lex->LookAhead(0) == Scope){ 3795 int t = lex->LookAhead(1);3796 if(t == Identifier || t == '~' || t == OPERATOR)3797 return TRUE;3812 int t = lex->LookAhead(1); 3813 if(t == Identifier || t == '~' || t == OPERATOR) 3814 return TRUE; 3798 3815 } 3799 3816 … … 3811 3828 int t = lex->LookAhead(i++); 3812 3829 if(t == '<'){ 3813 int n = 1;3814 while(n > 0){3815 int u = lex->LookAhead(i++);3816 if(u == '<')3817 ++n;3818 else if(u == '>')3819 --n;3820 else if(u == '('){3821 int m = 1;3822 while(m > 0){3823 int v = lex->LookAhead(i++);3824 if(v == '(')3825 ++m;3826 else if(v == ')')3827 --m;3828 else if(v == '\0' || v == ';' || v == '}')3829 return FALSE;3830 }3831 }3832 else if(u == '\0' || u == ';' || u == '}')3833 return FALSE;3834 }3835 3836 t = lex->LookAhead(i);3837 return bool(t == Scope || t == '(');3830 int n = 1; 3831 while(n > 0){ 3832 int u = lex->LookAhead(i++); 3833 if(u == '<') 3834 ++n; 3835 else if(u == '>') 3836 --n; 3837 else if(u == '('){ 3838 int m = 1; 3839 while(m > 0){ 3840 int v = lex->LookAhead(i++); 3841 if(v == '(') 3842 ++m; 3843 else if(v == ')') 3844 --m; 3845 else if(v == '\0' || v == ';' || v == '}') 3846 return FALSE; 3847 } 3848 } 3849 else if(u == '\0' || u == ';' || u == '}') 3850 return FALSE; 3851 } 3852 3853 t = lex->LookAhead(i); 3854 return bool(t == Scope || t == '('); 3838 3855 } 3839 3856 … … 3858 3875 3859 3876 if(lex->GetToken(ob) != '{') 3860 return FALSE;3877 return FALSE; 3861 3878 3862 3879 Ptree* sts = nil; 3863 3880 while(lex->LookAhead(0) != '}'){ 3864 Ptree* st;3865 if(!rStatement(st)){3866 if(!SyntaxError())3867 return FALSE;// too many errors3868 3869 SkipTo('}');3870 lex->GetToken(cb);3871 body = Ptree::List(new Leaf(ob), nil, new Leaf(cb));3872 return TRUE;// error recovery3873 }3874 3875 lex->GetComments();3876 sts = Ptree::Snoc(sts, st);3881 Ptree* st; 3882 if(!rStatement(st)){ 3883 if(!SyntaxError()) 3884 return FALSE; // too many errors 3885 3886 SkipTo('}'); 3887 lex->GetToken(cb); 3888 body = Ptree::List(new Leaf(ob), nil, new Leaf(cb)); 3889 return TRUE; // error recovery 3890 } 3891 3892 lex->GetComments(); 3893 sts = Ptree::Snoc(sts, st); 3877 3894 } 3878 3895 3879 3896 if(lex->GetToken(cb) != '}') 3880 return FALSE;3897 return FALSE; 3881 3898 3882 3899 body = new PtreeBlock(new Leaf(ob), sts, new Leaf(cb)); … … 3911 3928 switch(k = lex->LookAhead(0)){ 3912 3929 case '{' : 3913 return rCompoundStatement(st);3930 return rCompoundStatement(st); 3914 3931 case TYPEDEF : 3915 return rTypedef(st);3932 return rTypedef(st); 3916 3933 case IF : 3917 return rIfStatement(st);3934 return rIfStatement(st); 3918 3935 case SWITCH : 3919 return rSwitchStatement(st);3936 return rSwitchStatement(st); 3920 3937 case WHILE : 3921 return rWhileStatement(st);3938 return rWhileStatement(st); 3922 3939 case DO : 3923 return rDoStatement(st);3940 return rDoStatement(st); 3924 3941 case FOR : 3925 return rForStatement(st);3942 return rForStatement(st); 3926 3943 case TRY : 3927 return rTryStatement(st);3944 return rTryStatement(st); 3928 3945 case BREAK : 3929 3946 case CONTINUE : 3930 lex->GetToken(tk1);3931 if(lex->GetToken(tk2) != ';')3932 return FALSE;3933 3934 if(k == BREAK)3935 st = new PtreeBreakStatement(new LeafReserved(tk1),3936 Ptree::List(new Leaf(tk2)));3937 else3938 st = new PtreeContinueStatement(new LeafReserved(tk1),3939 Ptree::List(new Leaf(tk2)));3940 return TRUE;3947 lex->GetToken(tk1); 3948 if(lex->GetToken(tk2) != ';') 3949 return FALSE; 3950 3951 if(k == BREAK) 3952 st = new PtreeBreakStatement(new LeafReserved(tk1), 3953 Ptree::List(new Leaf(tk2))); 3954 else 3955 st = new PtreeContinueStatement(new LeafReserved(tk1), 3956 Ptree::List(new Leaf(tk2))); 3957 return TRUE; 3941 3958 case RETURN : 3942 lex->GetToken(tk1);3943 if(lex->LookAhead(0) == ';'){3944 lex->GetToken(tk2);3945 st = new PtreeReturnStatement(new LeafReserved(tk1),3946 Ptree::List(new Leaf(tk2)));3947 return TRUE;3948 }3949 else{3950 if(!rCommaExpression(exp))3951 return FALSE;3952 3953 if(lex->GetToken(tk2) != ';')3954 return FALSE;3955 3956 st = new PtreeReturnStatement(new LeafReserved(tk1),3957 Ptree::List(exp, new Leaf(tk2)));3958 return TRUE;3959 }3959 lex->GetToken(tk1); 3960 if(lex->LookAhead(0) == ';'){ 3961 lex->GetToken(tk2); 3962 st = new PtreeReturnStatement(new LeafReserved(tk1), 3963 Ptree::List(new Leaf(tk2))); 3964 return TRUE; 3965 } 3966 else{ 3967 if(!rCommaExpression(exp)) 3968 return FALSE; 3969 3970 if(lex->GetToken(tk2) != ';') 3971 return FALSE; 3972 3973 st = new PtreeReturnStatement(new LeafReserved(tk1), 3974 Ptree::List(exp, new Leaf(tk2))); 3975 return TRUE; 3976 } 3960 3977 case GOTO : 3961 lex->GetToken(tk1);3962 if(lex->GetToken(tk2) != Identifier)3963 return FALSE;3964 3965 if(lex->GetToken(tk3) != ';')3966 return FALSE;3967 3968 st = new PtreeGotoStatement(new LeafReserved(tk1),3969 Ptree::List(new Leaf(tk2), new Leaf(tk3)));3970 return TRUE;3978 lex->GetToken(tk1); 3979 if(lex->GetToken(tk2) != Identifier) 3980 return FALSE; 3981 3982 if(lex->GetToken(tk3) != ';') 3983 return FALSE; 3984 3985 st = new PtreeGotoStatement(new LeafReserved(tk1), 3986 Ptree::List(new Leaf(tk2), new Leaf(tk3))); 3987 return TRUE; 3971 3988 case CASE : 3972 lex->GetToken(tk1);3973 if(!rExpression(exp))3974 return FALSE;3975 3976 if(lex->GetToken(tk2) != ':')3977 return FALSE;3978 3979 if(!rStatement(st2))3980 return FALSE;3981 3982 st = new PtreeCaseStatement(new LeafReserved(tk1),3983 Ptree::List(exp, new Leaf(tk2), st2));3984 return TRUE;3989 lex->GetToken(tk1); 3990 if(!rExpression(exp)) 3991 return FALSE; 3992 3993 if(lex->GetToken(tk2) != ':') 3994 return FALSE; 3995 3996 if(!rStatement(st2)) 3997 return FALSE; 3998 3999 st = new PtreeCaseStatement(new LeafReserved(tk1), 4000 Ptree::List(exp, new Leaf(tk2), st2)); 4001 return TRUE; 3985 4002 case DEFAULT : 3986 lex->GetToken(tk1);3987 if(lex->GetToken(tk2) != ':')3988 return FALSE;3989 3990 if(!rStatement(st2))3991 return FALSE;3992 3993 st = new PtreeDefaultStatement(new LeafReserved(tk1),3994 Ptree::List(new Leaf(tk2), st2));3995 return TRUE;4003 lex->GetToken(tk1); 4004 if(lex->GetToken(tk2) != ':') 4005 return FALSE; 4006 4007 if(!rStatement(st2)) 4008 return FALSE; 4009 4010 st = new PtreeDefaultStatement(new LeafReserved(tk1), 4011 Ptree::List(new Leaf(tk2), st2)); 4012 return TRUE; 3996 4013 case Identifier : 3997 if(lex->LookAhead(1) == ':'){// label statement3998 lex->GetToken(tk1);3999 lex->GetToken(tk2);4000 if(!rStatement(st2))4001 return FALSE;4002 4003 st = new PtreeLabelStatement(new Leaf(tk1),4004 Ptree::List(new Leaf(tk2), st2));4005 return TRUE;4006 }4007 // don't break here!4014 if(lex->LookAhead(1) == ':'){ // label statement 4015 lex->GetToken(tk1); 4016 lex->GetToken(tk2); 4017 if(!rStatement(st2)) 4018 return FALSE; 4019 4020 st = new PtreeLabelStatement(new Leaf(tk1), 4021 Ptree::List(new Leaf(tk2), st2)); 4022 return TRUE; 4023 } 4024 // don't break here! 4008 4025 default : 4009 return rExprStatement(st);4026 return rExprStatement(st); 4010 4027 } 4011 4028 } … … 4021 4038 4022 4039 if(lex->GetToken(tk1) != IF) 4023 return FALSE;4040 return FALSE; 4024 4041 4025 4042 if(lex->GetToken(tk2) != '(') 4026 return FALSE;4043 return FALSE; 4027 4044 4028 4045 if(!rCommaExpression(exp)) 4029 return FALSE;4046 return FALSE; 4030 4047 4031 4048 if(lex->GetToken(tk3) != ')') 4032 return FALSE;4049 return FALSE; 4033 4050 4034 4051 if(!rStatement(then)) 4035 return FALSE;4052 return FALSE; 4036 4053 4037 4054 st = new PtreeIfStatement(new LeafReserved(tk1), 4038 Ptree::List(new Leaf(tk2), exp, new Leaf(tk3),4039 then));4055 Ptree::List(new Leaf(tk2), exp, new Leaf(tk3), 4056 then)); 4040 4057 if(lex->LookAhead(0) == ELSE){ 4041 lex->GetToken(tk4);4042 if(!rStatement(otherwise))4043 return FALSE;4044 4045 st = Ptree::Nconc(st, Ptree::List(new Leaf(tk4), otherwise));4058 lex->GetToken(tk4); 4059 if(!rStatement(otherwise)) 4060 return FALSE; 4061 4062 st = Ptree::Nconc(st, Ptree::List(new Leaf(tk4), otherwise)); 4046 4063 } 4047 4064 … … 4059 4076 4060 4077 if(lex->GetToken(tk1) != SWITCH) 4061 return FALSE;4078 return FALSE; 4062 4079 4063 4080 if(lex->GetToken(tk2) != '(') 4064 return FALSE;4081 return FALSE; 4065 4082 4066 4083 if(!rCommaExpression(exp)) 4067 return FALSE;4084 return FALSE; 4068 4085 4069 4086 if(lex->GetToken(tk3) != ')') 4070 return FALSE;4087 return FALSE; 4071 4088 4072 4089 if(!rStatement(body)) 4073 return FALSE;4090 return FALSE; 4074 4091 4075 4092 st = new PtreeSwitchStatement(new LeafReserved(tk1), 4076 Ptree::List(new Leaf(tk2), exp,4077 new Leaf(tk3), body));4093 Ptree::List(new Leaf(tk2), exp, 4094 new Leaf(tk3), body)); 4078 4095 return TRUE; 4079 4096 } … … 4089 4106 4090 4107 if(lex->GetToken(tk1) != WHILE) 4091 return FALSE;4108 return FALSE; 4092 4109 4093 4110 if(lex->GetToken(tk2) != '(') 4094 return FALSE;4111 return FALSE; 4095 4112 4096 4113 if(!rCommaExpression(exp)) 4097 return FALSE;4114 return FALSE; 4098 4115 4099 4116 if(lex->GetToken(tk3) != ')') 4100 return FALSE;4117 return FALSE; 4101 4118 4102 4119 if(!rStatement(body)) 4103 return FALSE;4120 return FALSE; 4104 4121 4105 4122 st = new PtreeWhileStatement(new LeafReserved(tk1), 4106 Ptree::List(new Leaf(tk2), exp,4107 new Leaf(tk3), body));4123 Ptree::List(new Leaf(tk2), exp, 4124 new Leaf(tk3), body)); 4108 4125 return TRUE; 4109 4126 } … … 4119 4136 4120 4137 if(lex->GetToken(tk0) != DO) 4121 return FALSE;4138 return FALSE; 4122 4139 4123 4140 if(!rStatement(body)) 4124 return FALSE;4141 return FALSE; 4125 4142 4126 4143 if(lex->GetToken(tk1) != WHILE) 4127 return FALSE;4144 return FALSE; 4128 4145 4129 4146 if(lex->GetToken(tk2) != '(') 4130 return FALSE;4147 return FALSE; 4131 4148 4132 4149 if(!rCommaExpression(exp)) 4133 return FALSE;4150 return FALSE; 4134 4151 4135 4152 if(lex->GetToken(tk3) != ')') 4136 return FALSE;4153 return FALSE; 4137 4154 4138 4155 if(lex->GetToken(tk4) != ';') 4139 return FALSE;4156 return FALSE; 4140 4157 4141 4158 st = new PtreeDoStatement(new LeafReserved(tk0), 4142 Ptree::List(body, new LeafReserved(tk1),4143 new Leaf(tk2), exp,4144 new Leaf(tk3), new Leaf(tk4)));4159 Ptree::List(body, new LeafReserved(tk1), 4160 new Leaf(tk2), exp, 4161 new Leaf(tk3), new Leaf(tk4))); 4145 4162 return TRUE; 4146 4163 } … … 4157 4174 4158 4175 if(lex->GetToken(tk1) != FOR) 4159 return FALSE;4176 return FALSE; 4160 4177 4161 4178 if(lex->GetToken(tk2) != '(') 4162 return FALSE;4179 return FALSE; 4163 4180 4164 4181 if(!rExprStatement(exp1)) 4165 return FALSE;4182 return FALSE; 4166 4183 4167 4184 if(lex->LookAhead(0) == ';') 4168 exp2 = nil;4185 exp2 = nil; 4169 4186 else 4170 if(!rCommaExpression(exp2))4171 return FALSE;4187 if(!rCommaExpression(exp2)) 4188 return FALSE; 4172 4189 4173 4190 if(lex->GetToken(tk3) != ';') 4174 return FALSE;4191 return FALSE; 4175 4192 4176 4193 if(lex->LookAhead(0) == ')') 4177 exp3 = nil;4194 exp3 = nil; 4178 4195 else 4179 if(!rCommaExpression(exp3))4180 return FALSE;4196 if(!rCommaExpression(exp3)) 4197 return FALSE; 4181 4198 4182 4199 if(lex->GetToken(tk4) != ')') 4183 return FALSE;4200 return FALSE; 4184 4201 4185 4202 if(!rStatement(body)) 4186 return FALSE;4203 return FALSE; 4187 4204 4188 4205 4189 4206 st = new PtreeForStatement(new LeafReserved(tk1), 4190 Ptree::List(new Leaf(tk2), exp1, exp2,4191 new Leaf(tk3), exp3,4192 new Leaf(tk4), body));4207 Ptree::List(new Leaf(tk2), exp1, exp2, 4208 new Leaf(tk3), exp3, 4209 new Leaf(tk4), body)); 4193 4210 return TRUE; 4194 4211 } … … 4207 4224 4208 4225 if(lex->GetToken(tk) != TRY) 4209 return FALSE;4226 return FALSE; 4210 4227 4211 4228 if(!rCompoundStatement(body)) 4212 return FALSE;4229 return FALSE; 4213 4230 4214 4231 st = new PtreeTryStatement(new LeafReserved(tk), Ptree::List(body)); 4215 4232 4216 4233 do{ 4217 if(lex->GetToken(tk) != CATCH)4218 return FALSE;4219 4220 if(lex->GetToken(op) != '(')4221 return FALSE;4222 4223 if(lex->LookAhead(0) == Ellipsis){4224 lex->GetToken(cp);4225 handler = new Leaf(cp);4226 }4227 else{4228 Encoding encode;4229 if(!rArgDeclaration(handler, encode))4230 return FALSE;4231 }4232 4233 if(lex->GetToken(cp) != ')')4234 return FALSE;4235 4236 if(!rCompoundStatement(body))4237 return FALSE;4238 4239 st = Ptree::Snoc(st, Ptree::List(new LeafReserved(tk),4240 new Leaf(op), handler, new Leaf(cp),4241 body));4234 if(lex->GetToken(tk) != CATCH) 4235 return FALSE; 4236 4237 if(lex->GetToken(op) != '(') 4238 return FALSE; 4239 4240 if(lex->LookAhead(0) == Ellipsis){ 4241 lex->GetToken(cp); 4242 handler = new Leaf(cp); 4243 } 4244 else{ 4245 Encoding encode; 4246 if(!rArgDeclaration(handler, encode)) 4247 return FALSE; 4248 } 4249 4250 if(lex->GetToken(cp) != ')') 4251 return FALSE; 4252 4253 if(!rCompoundStatement(body)) 4254 return FALSE; 4255 4256 st = Ptree::Snoc(st, Ptree::List(new LeafReserved(tk), 4257 new Leaf(op), handler, new Leaf(cp), 4258 body)); 4242 4259 } while(lex->LookAhead(0) == CATCH); 4243 4260 return TRUE; … … 4257 4274 4258 4275 if(lex->LookAhead(0) == ';'){ 4259 lex->GetToken(tk);4260 st = new PtreeExprStatement(nil, Ptree::List(new Leaf(tk)));4261 return TRUE;4276 lex->GetToken(tk); 4277 st = new PtreeExprStatement(nil, Ptree::List(new Leaf(tk))); 4278 return TRUE; 4262 4279 } 4263 4280 else{ 4264 char* pos = lex->Save();4265 if(rDeclarationStatement(st))4266 return TRUE;4267 else{4268 Ptree* exp;4269 lex->Restore(pos);4270 if(!rCommaExpression(exp))4271 return FALSE;4272 4273 if(exp->IsA(ntUserStatementExpr, ntStaticUserStatementExpr)){4274 st = exp;4275 return TRUE;4276 }4277 4278 if(lex->GetToken(tk) != ';')4279 return FALSE;4280 4281 st = new PtreeExprStatement(exp, Ptree::List(new Leaf(tk)));4282 return TRUE;4283 }4281 char* pos = lex->Save(); 4282 if(rDeclarationStatement(st)) 4283 return TRUE; 4284 else{ 4285 Ptree* exp; 4286 lex->Restore(pos); 4287 if(!rCommaExpression(exp)) 4288 return FALSE; 4289 4290 if(exp->IsA(ntUserStatementExpr, ntStaticUserStatementExpr)){ 4291 st = exp; 4292 return TRUE; 4293 } 4294 4295 if(lex->GetToken(tk) != ';') 4296 return FALSE; 4297 4298 st = new PtreeExprStatement(exp, Ptree::List(new Leaf(tk))); 4299 return TRUE; 4300 } 4284 4301 } 4285 4302 } … … 4307 4324 || !optCvQualify(cv_q) 4308 4325 || !optIntegralTypeOrClassSpec(integral, type_encode)) 4309 return FALSE;4326 return FALSE; 4310 4327 4311 4328 Ptree* head = nil; 4312 4329 if(storage_s != nil) 4313 head = Ptree::Snoc(head, storage_s);4330 head = Ptree::Snoc(head, storage_s); 4314 4331 4315 4332 if(integral != nil) 4316 return rIntegralDeclStatement(statement, type_encode, integral,4317 cv_q, head);4333 return rIntegralDeclStatement(statement, type_encode, integral, 4334 cv_q, head); 4318 4335 else{ 4319 type_encode.Clear();4320 int t = lex->LookAhead(0);4321 if(cv_q != nil && ((t == Identifier && lex->LookAhead(1) == '=')4322 || t == '*'))4323 return rConstDeclaration(statement, type_encode, head, cv_q);4324 else4325 return rOtherDeclStatement(statement, type_encode, cv_q, head);4336 type_encode.Clear(); 4337 int t = lex->LookAhead(0); 4338 if(cv_q != nil && ((t == Identifier && lex->LookAhead(1) == '=') 4339 || t == '*')) 4340 return rConstDeclaration(statement, type_encode, head, cv_q); 4341 else 4342 return rOtherDeclStatement(statement, type_encode, cv_q, head); 4326 4343 } 4327 4344 } … … 4332 4349 */ 4333 4350 bool Parser::rIntegralDeclStatement(Ptree*& statement, Encoding& type_encode, 4334 Ptree* integral, Ptree* cv_q, Ptree* head)4351 Ptree* integral, Ptree* cv_q, Ptree* head) 4335 4352 { 4336 4353 Ptree *cv_q2, *decl; … … 4338 4355 4339 4356 if(!optCvQualify(cv_q2)) 4340 return FALSE;4357 return FALSE; 4341 4358 4342 4359 if(cv_q != nil) 4343 if(cv_q2 == nil)4344 integral = Ptree::Snoc(cv_q, integral);4345 else4346 integral = Ptree::Nconc(cv_q, Ptree::Cons(integral, cv_q2));4360 if(cv_q2 == nil) 4361 integral = Ptree::Snoc(cv_q, integral); 4362 else 4363 integral = Ptree::Nconc(cv_q, Ptree::Cons(integral, cv_q2)); 4347 4364 else if(cv_q2 != nil) 4348 integral = Ptree::Cons(integral, cv_q2);4365 integral = Ptree::Cons(integral, cv_q2); 4349 4366 4350 4367 type_encode.CvQualify(cv_q, cv_q2); 4351 4368 if(lex->LookAhead(0) == ';'){ 4352 lex->GetToken(tk);4353 statement = new PtreeDeclaration(head, Ptree::List(integral,4354 new Leaf(tk)));4355 return TRUE;4369 lex->GetToken(tk); 4370 statement = new PtreeDeclaration(head, Ptree::List(integral, 4371 new Leaf(tk))); 4372 return TRUE; 4356 4373 } 4357 4374 else{ 4358 if(!rDeclarators(decl, type_encode, FALSE, TRUE))4359 return FALSE;4360 4361 if(lex->GetToken(tk) != ';')4362 return FALSE;4363 4364 statement = new PtreeDeclaration(head, Ptree::List(integral, decl,4365 new Leaf(tk)));4366 return TRUE;4375 if(!rDeclarators(decl, type_encode, FALSE, TRUE)) 4376 return FALSE; 4377 4378 if(lex->GetToken(tk) != ';') 4379 return FALSE; 4380 4381 statement = new PtreeDeclaration(head, Ptree::List(integral, decl, 4382 new Leaf(tk))); 4383 return TRUE; 4367 4384 } 4368 4385 } … … 4373 4390 */ 4374 4391 bool Parser::rOtherDeclStatement(Ptree*& statement, Encoding& type_encode, 4375 Ptree* cv_q, Ptree* head)4392 Ptree* cv_q, Ptree* head) 4376 4393 { 4377 4394 Ptree *type_name, *cv_q2, *decl; … … 4379 4396 4380 4397 if(!rName(type_name, type_encode)) 4381 return FALSE;4398 return FALSE; 4382 4399 4383 4400 if(!optCvQualify(cv_q2)) 4384 return FALSE;4401 return FALSE; 4385 4402 4386 4403 if(cv_q != nil) 4387 if(cv_q2 == nil)4388 type_name = Ptree::Snoc(cv_q, type_name);4389 else4390 type_name = Ptree::Nconc(cv_q, Ptree::Cons(type_name, cv_q2));4404 if(cv_q2 == nil) 4405 type_name = Ptree::Snoc(cv_q, type_name); 4406 else 4407 type_name = Ptree::Nconc(cv_q, Ptree::Cons(type_name, cv_q2)); 4391 4408 else if(cv_q2 != nil) 4392 type_name = Ptree::Cons(type_name, cv_q2);4409 type_name = Ptree::Cons(type_name, cv_q2); 4393 4410 4394 4411 type_encode.CvQualify(cv_q, cv_q2); 4395 4412 if(!rDeclarators(decl, type_encode, FALSE, TRUE)) 4396 return FALSE;4413 return FALSE; 4397 4414 4398 4415 if(lex->GetToken(tk) != ';') 4399 return FALSE;4416 return FALSE; 4400 4417 4401 4418 statement = new PtreeDeclaration(head, Ptree::List(type_name, decl, 4402 new Leaf(tk)));4419 new Leaf(tk))); 4403 4420 return TRUE; 4404 4421 } … … 4414 4431 4415 4432 for(;;){ 4416 int t = lex->LookAhead(0);4417 if(t == token || t == '\0')4418 break;4419 else4420 lex->GetToken(tk);4433 int t = lex->LookAhead(0); 4434 if(t == token || t == '\0') 4435 break; 4436 else 4437 lex->GetToken(tk); 4421 4438 } 4422 4439 } … … 4429 4446 main() 4430 4447 { 4431 ProgramFromStdin prog;4432 Lex lex(&prog);4433 Parser parse(&lex);4434 Walker w(&parse);4435 Ptree* def;4448 ProgramFromStdin prog; 4449 Lex lex(&prog); 4450 Parser parse(&lex); 4451 Walker w(&parse); 4452 Ptree* def; 4436 4453 4437 4454 while(parse.rProgram(def)){ 4438 def->Display2(cout);4439 w.Translate(def);4455 def->Display2(cout); 4456 w.Translate(def); 4440 4457 } 4441 4458 -
branches/stage2/tools/database/occ/parse.h
r6768 r6774 2 2 Copyright (C) 1997-2001 Shigeru Chiba, Tokyo Institute of Technology. 3 3 4 Permission to use, copy, distribute and modify this software and 5 its documentation for any purpose is hereby granted without fee, 6 provided that the above copyright notice appear in all copies and that 7 both that copyright notice and this permission notice appear in 4 Permission to use, copy, distribute and modify this software and 5 its documentation for any purpose is hereby granted without fee, 6 provided that the above copyright notice appear in all copies and that 7 both that copyright notice and this permission notice appear in 8 8 supporting documentation. 9 9 10 Shigeru Chiba makes no representations about the suitability of this 10 Shigeru Chiba makes no representations about the suitability of this 11 11 software for any purpose. It is provided "as is" without express or 12 12 implied warranty. … … 39 39 int NumOfErrors() { return nerrors; } 40 40 uint LineNumber(char* pos, char*& fname, int& fname_len); 41 #ifdef SDS 42 uint LineNumber(char* pos, char*& fname, int& fname_len, int& col_num, int& abs_pos); 43 #endif 41 44 42 45 bool rProgram(Ptree*&); … … 44 47 protected: 45 48 enum DeclKind { kDeclarator, kArgDeclarator, kCastDeclarator }; 46 enum TemplateDeclKind { tdk_unknown, tdk_decl, tdk_instantiation, 47 tdk_specialization, num_tdks };49 enum TemplateDeclKind { tdk_unknown, tdk_decl, tdk_instantiation, 50 tdk_specialization, num_tdks }; 48 51 49 52 bool SyntaxError(); … … 84 87 bool rDeclaratorWithInit(Ptree*&, Encoding&, bool, bool); 85 88 bool rDeclarator(Ptree*&, DeclKind, bool, Encoding&, Encoding&, bool, 86 bool = FALSE);89 bool = FALSE); 87 90 bool rDeclarator2(Ptree*&, DeclKind, bool, Encoding&, Encoding&, bool, 88 bool, Ptree**);91 bool, Ptree**); 89 92 bool optPtrOperator(Ptree*&, Encoding&); 90 93 bool rMemberInitializers(Ptree*&); -
branches/stage2/tools/database/occ/token.cpp
r6771 r6774 42 42 43 43 #if defined(_PARSE_VCC) 44 #define _MSC_VER 110044 #define _MSC_VER 1100 45 45 #endif 46 46 47 47 #if defined(_MSC_VER) || defined(OS2) //kso: OS2 48 #include <assert.h> // for assert in InitializeOtherKeywords49 #endif 50 51 extern BOOL regularCpp; // defined in main.cc48 #include <assert.h> // for assert in InitializeOtherKeywords 49 #endif 50 51 extern BOOL regularCpp; // defined in main.cc 52 52 static void InitializeOtherKeywords(); 53 53 … … 55 55 56 56 #ifdef __GNUG__ 57 #define token(x) (long)#x57 #define token(x) (long)#x 58 58 #else 59 #define token(x) (long)"x"59 #define token(x) (long)"x" 60 60 #endif 61 61 62 62 #else 63 63 64 #define token(x) x64 #define token(x) x 65 65 66 66 #endif … … 160 160 161 161 if(keyword == nil) 162 return FALSE;162 return FALSE; 163 163 164 164 str = new(GC) char[strlen(keyword) + 1]; … … 166 166 167 167 if(user_keywords == nil) 168 user_keywords = new HashTable;168 user_keywords = new HashTable; 169 169 170 170 if(user_keywords->AddEntry(str, (HashValue)token, &index) >= 0) 171 return TRUE;171 return TRUE; 172 172 else 173 return bool(user_keywords->Peek(index) == (HashValue)token);173 return bool(user_keywords->Peek(index) == (HashValue)token); 174 174 } 175 175 … … 177 177 { 178 178 if(t == nil || !t->IsLeaf()) 179 return FALSE;179 return FALSE; 180 180 181 181 char* p = t->GetPosition(); … … 183 183 value = 0; 184 184 if(len > 2 && *p == '0' && is_xletter(p[1])){ 185 for(int i = 2; i < len; ++i){186 char c = p[i];187 if(is_digit(c))188 value = value * 0x10 + (c - '0');189 else if('A' <= c && c <= 'F')190 value = value * 0x10 + (c - 'A' + 10);191 else if('a' <= c && c <= 'f')192 value = value * 0x10 + (c - 'a' + 10);193 else if(is_int_suffix(c))194 break;195 else196 return FALSE;197 }198 199 return TRUE;185 for(int i = 2; i < len; ++i){ 186 char c = p[i]; 187 if(is_digit(c)) 188 value = value * 0x10 + (c - '0'); 189 else if('A' <= c && c <= 'F') 190 value = value * 0x10 + (c - 'A' + 10); 191 else if('a' <= c && c <= 'f') 192 value = value * 0x10 + (c - 'a' + 10); 193 else if(is_int_suffix(c)) 194 break; 195 else 196 return FALSE; 197 } 198 199 return TRUE; 200 200 } 201 201 else if(len > 0 && is_digit(*p)){ 202 for(int i = 0; i < len; ++i){203 char c = p[i];204 if(is_digit(c))205 value = value * 10 + c - '0';206 else if(is_int_suffix(c))207 break;208 else209 return FALSE;210 }211 212 return TRUE;202 for(int i = 0; i < len; ++i){ 203 char c = p[i]; 204 if(is_digit(c)) 205 value = value * 10 + c - '0'; 206 else if(is_int_suffix(c)) 207 break; 208 else 209 return FALSE; 210 } 211 212 return TRUE; 213 213 } 214 214 else 215 return FALSE;215 return FALSE; 216 216 } 217 217 … … 221 221 { 222 222 if(t == nil || !t->IsLeaf()) 223 return FALSE;223 return FALSE; 224 224 225 225 char* p = t->GetPosition(); 226 226 int length = t->GetLength(); 227 227 if(*p != '"') 228 return FALSE;228 return FALSE; 229 229 else{ 230 str = new(GC) char[length];231 char* sp = str;232 for(int i = 1; i < length; ++i)233 if(p[i] != '"'){234 *sp++ = p[i];235 if(p[i] == '\\' && i + 1 < length)236 *sp++ = p[++i];237 }238 else239 while(++i < length && p[i] != '"')240 ;241 242 *sp = '\0';243 return TRUE;230 str = new(GC) char[length]; 231 char* sp = str; 232 for(int i = 1; i < length; ++i) 233 if(p[i] != '"'){ 234 *sp++ = p[i]; 235 if(p[i] == '\\' && i + 1 < length) 236 *sp++ = p[++i]; 237 } 238 else 239 while(++i < length && p[i] != '"') 240 ; 241 242 *sp = '\0'; 243 return TRUE; 244 244 } 245 245 } … … 273 273 head = (head + 1) % size; 274 274 if(head == tail){ 275 Slot* ring2 = new (GC) Slot[size + Plus];275 Slot* ring2 = new (GC) Slot[size + Plus]; 276 276 int i = 0; 277 do{278 ring2[i++] = ring[tail];279 tail = (tail + 1) % size;280 } while(head != tail);281 head = i;282 tail = 0;283 size += Plus;284 // delete [] ring;285 ring = ring2;277 do{ 278 ring2[i++] = ring[tail]; 279 tail = (tail + 1) % size; 280 } while(head != tail); 281 head = i; 282 tail = 0; 283 size += Plus; 284 // delete [] ring; 285 ring = ring2; 286 286 } 287 287 } … … 290 290 { 291 291 if(head == tail) 292 return lex->ReadToken(pos, len);292 return lex->ReadToken(pos, len); 293 293 294 294 int t = ring[tail].token; … … 318 318 319 319 for(i = 0; i <= offset; ++i){ 320 if(head == cur){321 while(i++ <= offset){322 char* p;323 int l;324 int t = lex->ReadToken(p, l);325 Push(t, p, l);326 }327 328 break;329 }330 331 cur = (cur + 1) % size;320 if(head == cur){ 321 while(i++ <= offset){ 322 char* p; 323 int l; 324 int t = lex->ReadToken(p, l); 325 Push(t, p, l); 326 } 327 328 break; 329 } 330 331 cur = (cur + 1) % size; 332 332 } 333 333 … … 344 344 345 345 for(;;){ 346 t = ReadLine();346 t = ReadLine(); 347 347 348 348 if(t == Ignore) 349 continue;350 351 last_token = t;349 continue; 350 351 last_token = t; 352 352 353 353 #if defined(__GNUG__) || defined(_GNUG_SYNTAX) 354 if(t == ATTRIBUTE){355 SkipAttributeToken();356 continue;357 }358 else if(t == EXTENSION){359 t = SkipExtensionToken(ptr, len);360 if(t == Ignore)361 continue;362 else363 return t;364 }354 if(t == ATTRIBUTE){ 355 SkipAttributeToken(); 356 continue; 357 } 358 else if(t == EXTENSION){ 359 t = SkipExtensionToken(ptr, len); 360 if(t == Ignore) 361 continue; 362 else 363 return t; 364 } 365 365 #endif 366 366 #if defined(_MSC_VER) || defined(OS2) //kso: OS2 367 367 if(t == ASM){ 368 368 SkipAsmToken(); 369 continue;370 }369 continue; 370 } 371 371 else if(t == DECLSPEC){ 372 SkipDeclspecToken();373 continue;374 }375 #endif 376 if(t != '\n')377 break;372 SkipDeclspecToken(); 373 continue; 374 } 375 #endif 376 if(t != '\n') 377 break; 378 378 } 379 379 … … 390 390 391 391 do{ 392 c = file->Get();392 c = file->Get(); 393 393 }while(c != '(' && c != '\0'); 394 394 395 395 int i = 1; 396 396 do{ 397 c = file->Get();398 if(c == '(')399 ++i;400 else if(c == ')')401 --i;402 else if(c == '\0')403 break;397 c = file->Get(); 398 if(c == '(') 399 ++i; 400 else if(c == ')') 401 --i; 402 else if(c == '\0') 403 break; 404 404 } while(i > 0); 405 405 } … … 415 415 416 416 do{ 417 c = file->Get();417 c = file->Get(); 418 418 }while(is_blank(c) || c == '\n'); 419 419 420 420 if(c != '('){ 421 file->Unget();422 return Ignore;// if no (..) follows, ignore __extension__421 file->Unget(); 422 return Ignore; // if no (..) follows, ignore __extension__ 423 423 } 424 424 425 425 int i = 1; 426 426 do{ 427 c = file->Get();428 if(c == '(')429 ++i;430 else if(c == ')')431 --i;432 else if(c == '\0')433 break;427 c = file->Get(); 428 if(c == '(') 429 ++i; 430 else if(c == ')') 431 --i; 432 else if(c == '\0') 433 break; 434 434 } while(i > 0); 435 435 436 return Identifier; // regards it as the identifier __extension__436 return Identifier; // regards it as the identifier __extension__ 437 437 } 438 438 … … 440 440 441 441 #define CHECK_END_OF_INSTRUCTION(C, EOI) \ 442 if (C == '\0') return; \443 if (strchr(EOI, C)) { \444 this->file->Unget(); \445 return; \446 }442 if (C == '\0') return; \ 443 if (strchr(EOI, C)) { \ 444 this->file->Unget(); \ 445 return; \ 446 } 447 447 448 448 /* SkipAsmToken() skips __asm ... … … 467 467 468 468 do{ 469 c = file->Get();470 CHECK_END_OF_INSTRUCTION(c, "");469 c = file->Get(); 470 CHECK_END_OF_INSTRUCTION(c, ""); 471 471 }while(is_blank(c) || c == '\n'); 472 472 … … 474 474 int i = 1; 475 475 do{ 476 c = file->Get();477 CHECK_END_OF_INSTRUCTION(c, "");478 if(c == '{')479 ++i;480 else if(c == '}')481 --i;476 c = file->Get(); 477 CHECK_END_OF_INSTRUCTION(c, ""); 478 if(c == '{') 479 ++i; 480 else if(c == '}') 481 --i; 482 482 } while(i > 0); 483 483 } 484 484 else{ 485 485 for(;;){ 486 CHECK_END_OF_INSTRUCTION(c, "}\n");487 c = file->Get();486 CHECK_END_OF_INSTRUCTION(c, "}\n"); 487 c = file->Get(); 488 488 } 489 489 } … … 497 497 498 498 do{ 499 c = file->Get();500 CHECK_END_OF_INSTRUCTION(c, "");499 c = file->Get(); 500 CHECK_END_OF_INSTRUCTION(c, ""); 501 501 }while(is_blank(c)); 502 502 … … 504 504 int i = 1; 505 505 do{ 506 c = file->Get();507 CHECK_END_OF_INSTRUCTION(c, "};");508 if(c == '(')509 ++i;510 else if(c == ')')511 --i;506 c = file->Get(); 507 CHECK_END_OF_INSTRUCTION(c, "};"); 508 if(c == '(') 509 ++i; 510 else if(c == ')') 511 --i; 512 512 }while(i > 0); 513 513 } … … 524 524 for(;;){ 525 525 do{ 526 c = file->Get();526 c = file->Get(); 527 527 }while(is_blank(c)); 528 528 529 529 if(c != '\\') 530 break;530 break; 531 531 532 532 c = file->Get(); 533 533 if(c != '\n' && c!= '\r') { 534 file->Unget();535 break;536 }534 file->Unget(); 535 break; 536 } 537 537 } 538 538 … … 549 549 tokenp = top = file->GetCurPos(); 550 550 if(c == '\0'){ 551 file->Unget();552 return '\0';551 file->Unget(); 552 return '\0'; 553 553 } 554 554 else if(c == '\n') 555 return '\n';555 return '\n'; 556 556 else if(c == '#' && last_token == '\n'){ 557 if(ReadLineDirective())558 return '\n';559 else{560 file->Rewind(top + 1);561 token_len = 1;562 return SingleCharOp(c);563 }557 if(ReadLineDirective()) 558 return '\n'; 559 else{ 560 file->Rewind(top + 1); 561 token_len = 1; 562 return SingleCharOp(c); 563 } 564 564 } 565 565 else if(c == '\'' || c == '"'){ 566 if(c == '\''){567 if(ReadCharConst(top))568 return token(CharConst);569 }570 else{571 if(ReadStrConst(top))572 return token(StringL);573 }574 575 file->Rewind(top + 1);576 token_len = 1;577 return SingleCharOp(c);566 if(c == '\''){ 567 if(ReadCharConst(top)) 568 return token(CharConst); 569 } 570 else{ 571 if(ReadStrConst(top)) 572 return token(StringL); 573 } 574 575 file->Rewind(top + 1); 576 token_len = 1; 577 return SingleCharOp(c); 578 578 } 579 579 else if(is_digit(c)) 580 return ReadNumber(c, top);580 return ReadNumber(c, top); 581 581 else if(c == '.'){ 582 c = file->Get();583 if(is_digit(c))584 return ReadFloat(top);585 else{586 file->Unget();587 return ReadSeparator('.', top);588 }582 c = file->Get(); 583 if(is_digit(c)) 584 return ReadFloat(top); 585 else{ 586 file->Unget(); 587 return ReadSeparator('.', top); 588 } 589 589 } 590 590 else if(is_letter(c)) 591 return ReadIdentifier(top);591 return ReadIdentifier(top); 592 592 else 593 return ReadSeparator(c, top);593 return ReadSeparator(c, top); 594 594 } 595 595 … … 599 599 600 600 for(;;){ 601 c = file->Get();602 if(c == '\\'){603 c = file->Get();604 if(c == '\0')605 return FALSE;606 }607 else if(c == '\''){608 token_len = int(file->GetCurPos() - top + 1);609 return TRUE;610 }611 else if(c == '\n' || c == '\0')612 return FALSE;601 c = file->Get(); 602 if(c == '\\'){ 603 c = file->Get(); 604 if(c == '\0') 605 return FALSE; 606 } 607 else if(c == '\''){ 608 token_len = int(file->GetCurPos() - top + 1); 609 return TRUE; 610 } 611 else if(c == '\n' || c == '\0') 612 return FALSE; 613 613 } 614 614 } … … 616 616 /* 617 617 If text is a sequence of string constants like: 618 "string1" "string2"618 "string1" "string2" 619 619 then the string constants are delt with as a single constant. 620 620 */ … … 624 624 625 625 for(;;){ 626 c = file->Get();627 if(c == '\\'){628 c = file->Get();629 if(c == '\0')630 return FALSE;631 }632 else if(c == '"'){633 uint pos = file->GetCurPos() + 1;634 int nline = 0;635 do{636 c = file->Get();637 if(c == '\n')638 ++nline;639 } while(is_blank(c) || c == '\n');640 641 if(c == '"')642 /* line_number += nline; */ ;643 else{644 token_len = int(pos - top);645 file->Rewind(pos);646 return TRUE;647 }648 }649 else if(c == '\n' || c == '\0')650 return FALSE;626 c = file->Get(); 627 if(c == '\\'){ 628 c = file->Get(); 629 if(c == '\0') 630 return FALSE; 631 } 632 else if(c == '"'){ 633 uint pos = file->GetCurPos() + 1; 634 int nline = 0; 635 do{ 636 c = file->Get(); 637 if(c == '\n') 638 ++nline; 639 } while(is_blank(c) || c == '\n'); 640 641 if(c == '"') 642 /* line_number += nline; */ ; 643 else{ 644 token_len = int(pos - top); 645 file->Rewind(pos); 646 return TRUE; 647 } 648 } 649 else if(c == '\n' || c == '\0') 650 return FALSE; 651 651 } 652 652 } … … 657 657 658 658 if(c == '0' && is_xletter(c2)){ 659 do{660 c = file->Get();661 } while(is_hexdigit(c));662 while(is_int_suffix(c))663 c = file->Get();664 665 file->Unget();666 token_len = int(file->GetCurPos() - top + 1);667 return token(Constant);659 do{ 660 c = file->Get(); 661 } while(is_hexdigit(c)); 662 while(is_int_suffix(c)) 663 c = file->Get(); 664 665 file->Unget(); 666 token_len = int(file->GetCurPos() - top + 1); 667 return token(Constant); 668 668 } 669 669 670 670 while(is_digit(c2)) 671 c2 = file->Get();671 c2 = file->Get(); 672 672 673 673 if(is_int_suffix(c2)) 674 do{675 c2 = file->Get();676 }while(is_int_suffix(c2));674 do{ 675 c2 = file->Get(); 676 }while(is_int_suffix(c2)); 677 677 else if(c2 == '.') 678 return ReadFloat(top);678 return ReadFloat(top); 679 679 else if(is_eletter(c2)){ 680 file->Unget();681 return ReadFloat(top);680 file->Unget(); 681 return ReadFloat(top); 682 682 } 683 683 … … 692 692 693 693 do{ 694 c = file->Get();694 c = file->Get(); 695 695 }while(is_digit(c)); 696 696 if(is_float_suffix(c)) 697 do{698 c = file->Get();699 }while(is_float_suffix(c));697 do{ 698 c = file->Get(); 699 }while(is_float_suffix(c)); 700 700 else if(is_eletter(c)){ 701 uint p = file->GetCurPos();702 c = file->Get();703 if(c == '+' || c == '-'){704 c = file->Get();705 if(!is_digit(c)){706 file->Rewind(p);707 token_len = int(p - top);708 return token(Constant);709 }710 }711 else if(!is_digit(c)){712 file->Rewind(p);713 token_len = int(p - top);714 return token(Constant);715 }716 717 do{718 c = file->Get();719 }while(is_digit(c));720 721 while(is_float_suffix(c))722 c = file->Get();701 uint p = file->GetCurPos(); 702 c = file->Get(); 703 if(c == '+' || c == '-'){ 704 c = file->Get(); 705 if(!is_digit(c)){ 706 file->Rewind(p); 707 token_len = int(p - top); 708 return token(Constant); 709 } 710 } 711 else if(!is_digit(c)){ 712 file->Rewind(p); 713 token_len = int(p - top); 714 return token(Constant); 715 } 716 717 do{ 718 c = file->Get(); 719 }while(is_digit(c)); 720 721 while(is_float_suffix(c)) 722 c = file->Get(); 723 723 } 724 724 … … 735 735 736 736 do{ 737 c = file->Get();737 c = file->Get(); 738 738 }while(c != '\n' && c != '\0'); 739 739 return TRUE; … … 745 745 746 746 do{ 747 c = file->Get();747 c = file->Get(); 748 748 }while(is_letter(c) || is_digit(c)); 749 749 … … 760 760 */ 761 761 static struct rw_table { 762 char* name;763 long value;762 char* name; 763 long value; 764 764 } table[] = { 765 765 #if defined(__GNUG__) || defined(_GNUG_SYNTAX) 766 { "__alignof__", token(SIZEOF) },767 { "__asm__", token(ATTRIBUTE) },768 { "__attribute__", token(ATTRIBUTE) },769 { "__const", token(CONST) },770 { "__const__", token(Ignore) }, //kso: gcc supports this AFAIK771 { "__extension__", token(EXTENSION) },772 { "__inline__", token(INLINE) },773 { "__restrict", token(Ignore) },774 { "__signed", token(SIGNED) },775 { "__signed__", token(SIGNED) },776 #endif 777 { "asm", token(ATTRIBUTE) },778 { "auto", token(AUTO) },766 { "__alignof__", token(SIZEOF) }, 767 { "__asm__", token(ATTRIBUTE) }, 768 { "__attribute__", token(ATTRIBUTE) }, 769 { "__const", token(CONST) }, 770 { "__const__", token(CONST) }, //kso: gcc supports this AFAIK 771 { "__extension__", token(EXTENSION) }, 772 { "__inline__", token(INLINE) }, 773 { "__restrict", token(Ignore) }, 774 { "__signed", token(SIGNED) }, 775 { "__signed__", token(SIGNED) }, 776 #endif 777 { "asm", token(ATTRIBUTE) }, 778 { "auto", token(AUTO) }, 779 779 #if (!defined(_MSC_VER) || (_MSC_VER >= 1100)) && !defined(__IBMCPP__) 780 { "bool", token(BOOLEAN) },781 #endif 782 { "break", token(BREAK) },783 { "case", token(CASE) },784 { "catch", token(CATCH) },785 { "char", token(CHAR) },786 { "class", token(CLASS) },787 { "const", token(CONST) },788 { "continue", token(CONTINUE) },789 { "default", token(DEFAULT) },790 { "delete", token(DELETE) },791 { "do", token(DO) },792 { "double", token(DOUBLE) },793 { "else", token(ELSE) },794 { "enum", token(ENUM) },795 { "extern", token(EXTERN) },796 { "float", token(FLOAT) },797 { "for", token(FOR) },798 { "friend", token(FRIEND) },799 { "goto", token(GOTO) },800 { "if", token(IF) },801 { "inline", token(INLINE) },802 { "int", token(INT) },803 { "long", token(LONG) },804 { "metaclass", token(METACLASS) },// OpenC++805 { "mutable", token(MUTABLE) },806 { "namespace", token(NAMESPACE) },807 { "new", token(NEW) },808 { "operator", token(OPERATOR) },809 { "private", token(PRIVATE) },810 { "protected", token(PROTECTED) },811 { "public", token(PUBLIC) },812 { "register", token(REGISTER) },813 { "return", token(RETURN) },814 { "short", token(SHORT) },815 { "signed", token(SIGNED) },816 { "sizeof", token(SIZEOF) },817 { "static", token(STATIC) },818 { "struct", token(STRUCT) },819 { "switch", token(SWITCH) },820 { "template", token(TEMPLATE) },821 { "this", token(THIS) },822 { "throw", token(THROW) },823 { "try", token(TRY) },824 { "typedef", token(TYPEDEF) },825 { "typename", token(CLASS) },// it's not identical to class, but...826 { "union", token(UNION) },827 { "unsigned", token(UNSIGNED) },828 { "using", token(USING) },829 { "virtual", token(VIRTUAL) },830 { "void", token(VOID) },831 { "volatile", token(VOLATILE) },832 { "while", token(WHILE) },780 { "bool", token(BOOLEAN) }, 781 #endif 782 { "break", token(BREAK) }, 783 { "case", token(CASE) }, 784 { "catch", token(CATCH) }, 785 { "char", token(CHAR) }, 786 { "class", token(CLASS) }, 787 { "const", token(CONST) }, 788 { "continue", token(CONTINUE) }, 789 { "default", token(DEFAULT) }, 790 { "delete", token(DELETE) }, 791 { "do", token(DO) }, 792 { "double", token(DOUBLE) }, 793 { "else", token(ELSE) }, 794 { "enum", token(ENUM) }, 795 { "extern", token(EXTERN) }, 796 { "float", token(FLOAT) }, 797 { "for", token(FOR) }, 798 { "friend", token(FRIEND) }, 799 { "goto", token(GOTO) }, 800 { "if", token(IF) }, 801 { "inline", token(INLINE) }, 802 { "int", token(INT) }, 803 { "long", token(LONG) }, 804 { "metaclass", token(METACLASS) }, // OpenC++ 805 { "mutable", token(MUTABLE) }, 806 { "namespace", token(NAMESPACE) }, 807 { "new", token(NEW) }, 808 { "operator", token(OPERATOR) }, 809 { "private", token(PRIVATE) }, 810 { "protected", token(PROTECTED) }, 811 { "public", token(PUBLIC) }, 812 { "register", token(REGISTER) }, 813 { "return", token(RETURN) }, 814 { "short", token(SHORT) }, 815 { "signed", token(SIGNED) }, 816 { "sizeof", token(SIZEOF) }, 817 { "static", token(STATIC) }, 818 { "struct", token(STRUCT) }, 819 { "switch", token(SWITCH) }, 820 { "template", token(TEMPLATE) }, 821 { "this", token(THIS) }, 822 { "throw", token(THROW) }, 823 { "try", token(TRY) }, 824 { "typedef", token(TYPEDEF) }, 825 { "typename", token(CLASS) }, // it's not identical to class, but... 826 { "union", token(UNION) }, 827 { "unsigned", token(UNSIGNED) }, 828 { "using", token(USING) }, 829 { "virtual", token(VIRTUAL) }, 830 { "void", token(VOID) }, 831 { "volatile", token(VOLATILE) }, 832 { "while", token(WHILE) }, 833 833 /* NULL slot */ 834 834 }; … … 839 839 840 840 if(done) 841 return;841 return; 842 842 else 843 done = TRUE;843 done = TRUE; 844 844 845 845 if(regularCpp) 846 for(unsigned int i = 0; i < sizeof(table) / sizeof(table[0]); ++i)847 if(table[i].value == METACLASS){848 table[i].value = Identifier;849 break;850 }846 for(unsigned int i = 0; i < sizeof(table) / sizeof(table[0]); ++i) 847 if(table[i].value == METACLASS){ 848 table[i].value = Identifier; 849 break; 850 } 851 851 852 852 #if defined(_MSC_VER) || defined(OS2) //kso: OS2 … … 896 896 int Lex::Screening(char *identifier, int len) 897 897 { 898 struct rw_table *low, *high, *mid;899 int c, token;898 struct rw_table *low, *high, *mid; 899 int c, token; 900 900 901 901 low = table; 902 902 high = &table[sizeof(table) / sizeof(table[0]) - 1]; 903 903 while(low <= high){ 904 mid = low + (high - low) / 2;905 if((c = strncmp(mid->name, identifier, len)) == 0)906 if(mid->name[len] == '\0')907 return mid->value;908 else909 high = mid - 1;910 else if(c < 0)911 low = mid + 1;912 else913 high = mid - 1;904 mid = low + (high - low) / 2; 905 if((c = strncmp(mid->name, identifier, len)) == 0) 906 if(mid->name[len] == '\0') 907 return mid->value; 908 else 909 high = mid - 1; 910 else if(c < 0) 911 low = mid + 1; 912 else 913 high = mid - 1; 914 914 } 915 915 916 916 if(user_keywords == nil) 917 user_keywords = new HashTable;917 user_keywords = new HashTable; 918 918 919 919 if(user_keywords->Lookup(identifier, len, (HashValue*)&token)) 920 return token;920 return token; 921 921 922 922 return token(Identifier); … … 929 929 token_len = 2; 930 930 if(c1 == '='){ 931 switch(c){932 case '*' :933 case '/' :934 case '%' :935 case '+' :936 case '-' :937 case '&' :938 case '^' :939 case '|' :940 return token(AssignOp);941 case '=' :942 case '!' :943 return token(EqualOp);944 case '<' :945 case '>' :946 return token(RelOp);947 default :948 file->Unget();949 token_len = 1;950 return SingleCharOp(c);951 }931 switch(c){ 932 case '*' : 933 case '/' : 934 case '%' : 935 case '+' : 936 case '-' : 937 case '&' : 938 case '^' : 939 case '|' : 940 return token(AssignOp); 941 case '=' : 942 case '!' : 943 return token(EqualOp); 944 case '<' : 945 case '>' : 946 return token(RelOp); 947 default : 948 file->Unget(); 949 token_len = 1; 950 return SingleCharOp(c); 951 } 952 952 } 953 953 else if(c == c1){ 954 switch(c){955 case '<' :956 case '>' :957 if(file->Get() != '='){958 file->Unget();959 return token(ShiftOp);960 }961 else{962 token_len = 3;963 return token(AssignOp);964 }965 case '|' :966 return token(LogOrOp);967 case '&' :968 return token(LogAndOp);969 case '+' :970 case '-' :971 return token(IncOp);972 case ':' :973 return token(Scope);974 case '.' :975 if(file->Get() == '.'){976 token_len = 3;977 return token(Ellipsis);978 }979 else980 file->Unget();981 case '/' :982 return ReadComment(c1, top);983 default :984 file->Unget();985 token_len = 1;986 return SingleCharOp(c);987 }954 switch(c){ 955 case '<' : 956 case '>' : 957 if(file->Get() != '='){ 958 file->Unget(); 959 return token(ShiftOp); 960 } 961 else{ 962 token_len = 3; 963 return token(AssignOp); 964 } 965 case '|' : 966 return token(LogOrOp); 967 case '&' : 968 return token(LogAndOp); 969 case '+' : 970 case '-' : 971 return token(IncOp); 972 case ':' : 973 return token(Scope); 974 case '.' : 975 if(file->Get() == '.'){ 976 token_len = 3; 977 return token(Ellipsis); 978 } 979 else 980 file->Unget(); 981 case '/' : 982 return ReadComment(c1, top); 983 default : 984 file->Unget(); 985 token_len = 1; 986 return SingleCharOp(c); 987 } 988 988 } 989 989 else if(c == '.' && c1 == '*') 990 return token(PmOp);990 return token(PmOp); 991 991 else if(c == '-' && c1 == '>') 992 if(file->Get() == '*'){993 token_len = 3;994 return token(PmOp);995 }996 else{997 file->Unget();998 return token(ArrowOp);999 }992 if(file->Get() == '*'){ 993 token_len = 3; 994 return token(PmOp); 995 } 996 else{ 997 file->Unget(); 998 return token(ArrowOp); 999 } 1000 1000 else if(c == '/' && c1 == '*') 1001 return ReadComment(c1, top);1001 return ReadComment(c1, top); 1002 1002 else{ 1003 file->Unget();1004 token_len = 1;1005 return SingleCharOp(c);1003 file->Unget(); 1004 token_len = 1; 1005 return SingleCharOp(c); 1006 1006 } 1007 1007 1008 1008 cerr << "*** An invalid character has been found! (" 1009 << (int)c << ',' << (int)c1 << ")\n";1009 << (int)c << ',' << (int)c1 << ")\n"; 1010 1010 return token(BadToken); 1011 1011 } … … 1013 1013 int Lex::SingleCharOp(unsigned char c) 1014 1014 { 1015 /* !"#$%&'()*+,-./0123456789:;<=>? */1015 /* !"#$%&'()*+,-./0123456789:;<=>? */ 1016 1016 static char valid[] = "x xx xxxxxxxx xxxxxx"; 1017 1017 1018 1018 if('!' <= c && c <= '?' && valid[c - '!'] == 'x') 1019 return c;1019 return c; 1020 1020 else if(c == '[' || c == ']' || c == '^') 1021 return c;1021 return c; 1022 1022 else if('{' <= c && c <= '~') 1023 return c;1023 return c; 1024 1024 else 1025 return token(BadToken);1025 return token(BadToken); 1026 1026 } 1027 1027 … … 1031 1031 bool foneliner = false; 1032 1032 #endif 1033 if (c == '*') // a nested C-style comment is prohibited.1034 do {1035 c = file->Get();1036 if (c == '*') {1037 c = file->Get();1038 if (c == '/') {1039 len = 1;1040 break;1041 }1042 else1043 file->Unget();1044 }1045 }while(c != '\0');1033 if (c == '*') // a nested C-style comment is prohibited. 1034 do { 1035 c = file->Get(); 1036 if (c == '*') { 1037 c = file->Get(); 1038 if (c == '/') { 1039 len = 1; 1040 break; 1041 } 1042 else 1043 file->Unget(); 1044 } 1045 }while(c != '\0'); 1046 1046 else /* if (c == '/') */ 1047 1047 #ifdef SDS … … 1054 1054 } 1055 1055 #else 1056 do {1057 c = file->Get();1058 }while(c != '\n' && c != '\0');1056 do { 1057 c = file->Get(); 1058 }while(c != '\n' && c != '\0'); 1059 1059 #endif 1060 1060 … … 1099 1099 Lex lex(new ProgramFromStdin); 1100 1100 for(;;){ 1101 // int t = lex.GetToken(token);1102 int t = lex.LookAhead(i++, token);1103 if(t == 0)1104 break;1105 else if(t < 128)1106 printf("%c (%x): ", t, t);1107 else1108 printf("%-10.10s (%x): ", (char*)t, t);1109 1110 putchar('"');1111 while(token.len-- > 0)1112 putchar(*token.ptr++);1113 1114 puts("\"");1101 // int t = lex.GetToken(token); 1102 int t = lex.LookAhead(i++, token); 1103 if(t == 0) 1104 break; 1105 else if(t < 128) 1106 printf("%c (%x): ", t, t); 1107 else 1108 printf("%-10.10s (%x): ", (char*)t, t); 1109 1110 putchar('"'); 1111 while(token.len-- > 0) 1112 putchar(*token.ptr++); 1113 1114 puts("\""); 1115 1115 }; 1116 1116 } … … 1125 1125 ^"#"{blank}*"pragma".*\n 1126 1126 1127 Constant {digit}+{int_suffix}*1128 "0"{xletter}{hexdigit}+{int_suffix}*1129 {digit}*\.{digit}+{float_suffix}*1130 {digit}+\.{float_suffix}*1131 {digit}*\.{digit}+"e"("+"|"-")*{digit}+{float_suffix}*1132 {digit}+\."e"("+"|"-")*{digit}+{float_suffix}*1133 {digit}+"e"("+"|"-")*{digit}+{float_suffix}*1134 1135 CharConst \'([^'\n]|\\[^\n])\'1136 1137 StringL \"([^"\n]|\\["\n])*\"1138 1139 Identifier {letter}+({letter}|{digit})*1140 1141 AssignOp *= /= %= += -= &= ^= <<= >>=1142 1143 EqualOp == !=1144 1145 RelOp <= >=1146 1147 ShiftOp << >>1148 1149 LogOrOp ||1150 1151 LogAndOp &&1152 1153 IncOp ++ --1154 1155 Scope ::1156 1157 Ellipsis ...1158 1159 PmOp .* ->*1160 1161 ArrowOp ->1162 1163 others !%^&*()-+={}|~[];:<>?,./1164 1165 BadToken others1127 Constant {digit}+{int_suffix}* 1128 "0"{xletter}{hexdigit}+{int_suffix}* 1129 {digit}*\.{digit}+{float_suffix}* 1130 {digit}+\.{float_suffix}* 1131 {digit}*\.{digit}+"e"("+"|"-")*{digit}+{float_suffix}* 1132 {digit}+\."e"("+"|"-")*{digit}+{float_suffix}* 1133 {digit}+"e"("+"|"-")*{digit}+{float_suffix}* 1134 1135 CharConst \'([^'\n]|\\[^\n])\' 1136 1137 StringL \"([^"\n]|\\["\n])*\" 1138 1139 Identifier {letter}+({letter}|{digit})* 1140 1141 AssignOp *= /= %= += -= &= ^= <<= >>= 1142 1143 EqualOp == != 1144 1145 RelOp <= >= 1146 1147 ShiftOp << >> 1148 1149 LogOrOp || 1150 1151 LogAndOp && 1152 1153 IncOp ++ -- 1154 1155 Scope :: 1156 1157 Ellipsis ... 1158 1159 PmOp .* ->* 1160 1161 ArrowOp -> 1162 1163 others !%^&*()-+={}|~[];:<>?,./ 1164 1165 BadToken others 1166 1166 1167 1167 */
Note:
See TracChangeset
for help on using the changeset viewer.
