Changeset 75 for branches/Transpascal/Compiler/Analyze/UPascalParser.pas
- Timestamp:
- Oct 21, 2010, 7:56:25 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Transpascal/Compiler/Analyze/UPascalParser.pas
r74 r75 26 26 procedure ParseUnitImplementation(SourceCode: TModuleUnit); 27 27 procedure ParseProgram(SourceCode: TModuleProgram); 28 procedure ParseCommonBlock(SourceCode: TCommonBlock; EndSymbol: char = ';'); 28 procedure ParseCommonBlock(SourceCode: TCommonBlock; EndSymbol: char = ';'; 29 WithBody: Boolean = True); 29 30 procedure ParseCommonBlockInterface(SourceCode: TCommonBlock); 30 31 function ParseCommand(SourceCode: TCommonBlock): TCommand; … … 368 369 369 370 ParseCommonBlock(Body, '.'); 371 SourceCode.ParentProgram.Modules.Add(SourceCode); 370 372 end; 371 373 end; … … 384 386 if NextToken = 'implementation' then 385 387 ParseUnitImplementation(SourceCode); 388 389 SourceCode.ParentProgram.Modules.Add(SourceCode); 386 390 387 391 if NextToken = 'initialization' then begin … … 411 415 ParseUses(SourceCode.UsedModules, False); 412 416 413 ParseCommonBlock(SourceCode.Body, '.' );417 ParseCommonBlock(SourceCode.Body, '.', False); 414 418 end; 415 419 … … 417 421 418 422 procedure TPascalParser.ParseCommonBlock(SourceCode: TCommonBlock; 419 EndSymbol: char = ';' );423 EndSymbol: char = ';'; WithBody: Boolean = True); 420 424 begin 421 425 with SourceCode do begin 422 426 while (NextToken <> EndSymbol) do begin 423 if NextToken = 'var' then 427 if NextToken = 'var' then begin 428 Expect('var'); 424 429 ParseVariableList(Variables) 425 else if NextToken = 'const' then 430 end else 431 if NextToken = 'const' then begin 432 Expect('const'); 426 433 ParseConstantList(Constants) 427 else if NextToken = 'type' then 428 ParseTypeList(Types) 429 else if NextToken = 'procedure' then 434 end else 435 if NextToken = 'type' then begin 436 Expect('type'); 437 ParseTypeList(Types); 438 end else 439 if NextToken = 'procedure' then 430 440 ParseFunctionList(Functions) 431 441 else if NextToken = 'function' then 432 442 ParseFunctionList(Functions) 433 443 else begin 434 ParseBeginEnd(Code); 444 if WithBody then 445 ParseBeginEnd(Code); 435 446 Break; 436 447 end; 437 448 end; 438 Expect(EndSymbol);449 if WithBody then Expect(EndSymbol); 439 450 end; 440 451 end; … … 444 455 with SourceCode do begin 445 456 while (NextToken <> 'implementation') and (NextTokenType <> ttEndOfFile) do begin 446 if NextToken = 'var' then 447 ParseVariableList(Variables) 448 else if NextToken = 'const' then 449 ParseConstantList(Constants, True) 450 else if NextToken = 'type' then 451 ParseTypeList(Types, True) 452 else if NextToken = 'procedure' then 457 if NextToken = 'var' then begin 458 Expect('var'); 459 ParseVariableList(Variables); 460 end else 461 if NextToken = 'const' then begin 462 Expect('const'); 463 ParseConstantList(Constants, True); 464 end else 465 if NextToken = 'type' then begin 466 Expect('type'); 467 ParseTypeList(Types, True); 468 end else 469 if NextToken = 'procedure' then 453 470 ParseFunctionList(Functions, True) 454 471 else if NextToken = 'function' then … … 496 513 I: integer; 497 514 begin 515 try 498 516 Identifiers := TStringList.Create; 499 517 with SourceCode do begin 500 518 with TFunction(Items[Add(TFunction.Create)]) do begin 501 519 Parent := SourceCode.Parent; 502 if NextToken = 'procedure' then 503 begin 520 if NextToken = 'procedure' then begin 504 521 Expect('procedure'); 505 522 HaveResult := False; 506 end 507 else 508 begin 523 end else begin 509 524 Expect('function'); 510 525 HaveResult := True; … … 545 560 end; 546 561 end; 562 if NextToken = ';' then Expect(';'); 547 563 end; 548 564 Expect(')'); … … 580 596 if not Exported then ParseCommonBlock(TFunction(Last)); 581 597 end; 582 Identifiers.Destroy; 598 finally 599 Identifiers.Free; 600 end; 583 601 end; 584 602 … … 638 656 Identifiers := TStringList.Create; 639 657 with SourceCode do begin 640 Expect('var');641 658 while IsIdentificator(NextToken) and (NextTokenType <> ttEndOfFile) do begin 642 659 Identifiers.Clear; … … 694 711 begin 695 712 Identifiers := TStringList.Create; 696 with SourceCode do 697 begin 698 Expect('const'); 699 while IsIdentificator(NextToken) do 700 begin 713 with SourceCode do begin 714 while IsIdentificator(NextToken) do begin 701 715 ConstantName := ReadCode; 702 716 Constant := Search(ConstantName); 703 if not Assigned(Constant) then 704 begin 717 if not Assigned(Constant) then begin 705 718 Identifiers.Add(ConstantName); 706 while NextToken = ',' do 707 begin 719 while NextToken = ',' do begin 708 720 Expect(','); 709 721 Identifiers.Add(ReadCode); 710 722 end; 711 end 712 else 723 end else 713 724 ErrorMessage(SRedefineIdentifier, [ConstantName], -1); 714 725 Expect(':'); … … 742 753 with SourceCode do 743 754 begin 744 Expect('type');745 755 while IsIdentificator(NextToken) do begin 746 756 NewType := ParseType(SourceCode); … … 872 882 function TPascalParser.ParseTypeRecord(TypeList: TTypeList; Name: string 873 883 ): TType; 884 type 885 TSectionType = (stVar, stType, stConst); 874 886 var 875 887 Visibility: TTypeVisibility; 876 begin 888 SectionType: TSectionType; 889 begin 890 SectionType := stVar; 877 891 Visibility := tvPublic; 878 892 Expect('record'); 879 893 Result := TTypeRecord.Create; 880 894 TTypeRecord(Result).Parent := TypeList; 895 TTypeRecord(Result).CommonBlock.Parent := TypeList.Parent; 881 896 TType(Result).Name := Name; 882 897 while (NextToken <> 'end') and (NextTokenType <> ttEndOfFile) do … … 898 913 Visibility := tvProtected; 899 914 end else 900 if NextToken = 'var' then 901 ParseVariableList(TTypeRecord(Result).CommonBlock.Variables) 902 else if NextToken = 'const' then 915 if NextToken = 'var' then begin 916 Expect('var'); 917 SectionType := stVar 918 end else 919 if NextToken = 'const' then begin 920 Expect('const'); 921 SectionType := stConst 922 end else 923 if NextToken = 'type' then begin 924 Expect('type'); 925 SectionType := stType; 926 end; 927 928 if SectionType = stVar then begin 929 if NextToken = 'procedure' then 930 ParseFunctionList(TTypeRecord(Result).CommonBlock.Functions, True) 931 else if NextToken = 'function' then 932 ParseFunctionList(TTypeRecord(Result).CommonBlock.Functions, True) 933 else begin 934 ParseVariableList(TTypeRecord(Result).CommonBlock.Variables, True) 935 //TTypeRecord(Result).CommonBlock.Types.Add(ParseType(TypeList, True, ':')); 936 //TType(TTypeRecord(Result).CommonBlock.Types.Last).Visibility := Visibility; 937 end; ParseVariableList(TTypeRecord(Result).CommonBlock.Variables) 938 end 939 else if SectionType = stConst then 903 940 ParseConstantList(TTypeRecord(Result).CommonBlock.Constants, True) 904 else if NextToken = 'type' then 905 ParseTypeList(TTypeRecord(Result).CommonBlock.Types, True) 906 else if NextToken = 'procedure' then 907 ParseFunctionList(TTypeRecord(Result).CommonBlock.Functions, True) 908 else if NextToken = 'function' then 909 ParseFunctionList(TTypeRecord(Result).CommonBlock.Functions, True) 910 else begin 911 TTypeRecord(Result).CommonBlock.Types.Add(ParseType(TypeList, True, ':')); 912 TType(TTypeRecord(Result).CommonBlock.Types.Last).Visibility := Visibility; 913 end; 914 Expect(';'); 941 else if SectionType = stType then 942 ParseTypeList(TTypeRecord(Result).CommonBlock.Types, True); 915 943 end; 916 944 Expect('end');
Note:
See TracChangeset
for help on using the changeset viewer.