Changeset 78 for branches/Transpascal/Compiler/Analyze/UPascalParser.pas
- Timestamp:
- Oct 22, 2010, 11:34:06 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Transpascal/Compiler/Analyze/UPascalParser.pas
r77 r78 41 41 AssignSymbol: string = '='); 42 42 function ParseType(TypeList: TTypeList; ExpectName: Boolean = True; AssignSymbol: string = '='): TType; 43 function ParseTypeSubType(TypeList: TTypeList; Name: string; ExpectName: Boolean): TType; 44 function ParseTypeBase(TypeList: TTypeList; Name: string): TType; 45 function ParseTypePointer(TypeList: TTypeList; Name: string): TType; 43 46 function ParseTypeEnumeration(TypeList: TTypeList; Name: string): TType; 44 47 function ParseTypeRecord(TypeList: TTypeList; Name: string): TType; 48 function ParseTypeClass(TypeList: TTypeList; Name: string): TType; 49 function ParseTypeArray(TypeList: TTypeList; Name: string): TType; 50 function ParseTypeSubRange(TypeList: TTypeList; Name: string): TType; 45 51 property OnGetSource: TGetSourceEvent read FOnGetSource 46 52 write FOnGetSource; … … 61 67 SFunctionNotDeclared = 'Function "%s" not declared.'; 62 68 SUnknownProcName = 'Unknown proc name "%s".'; 63 69 SUnknownModuleType = 'Unknown module name "%s".'; 70 SInvalidConstruction = 'Invalid construction.'; 64 71 65 72 implementation … … 83 90 Parser.FileName := Name; 84 91 Parser.OnErrorMessage := OnErrorMessage; 85 NewModule := Parser.ParseModule(ProgramCode); 86 ProgramCode.Modules.Add(NewModule); 92 //NewModule := 93 Parser.ParseModule(ProgramCode); 94 //ProgramCode.Modules.Add(NewModule); 87 95 Result := True; 88 96 end else Result := False; … … 127 135 (((NextToken = ')') or (NextToken = ']'))) and not (NextTokenType = ttEndOfFile) do begin 128 136 IdentifierType := NextTokenType; 129 Identifier := Read Code;137 Identifier := ReadToken; 130 138 if Identifier = '(' then begin 131 139 // Subexpression … … 305 313 Result := TAssignment.Create; 306 314 TAssignment(Result).CommonBlock := SourceCode; 307 IdentName := Read Code;315 IdentName := ReadToken; 308 316 TAssignment(Result).Target := SourceCode.Variables.Search(IdentName); 309 317 Expect(':='); … … 314 322 if Assigned(SourceCode.Functions.Search(NextToken)) then begin 315 323 // Function call 316 FunctionName := Read Code;324 FunctionName := ReadToken; 317 325 Result := TFunctionCall.Create; 318 326 TFunctionCall(Result).CommonBlock := SourceCode; … … 331 339 end else begin 332 340 Result := nil; 333 ErrorMessage(SUnknownIdentifier, [Read Code], -1);341 ErrorMessage(SUnknownIdentifier, [ReadToken], -1); 334 342 end; 335 343 end else … … 338 346 else begin 339 347 Result := nil; 340 ErrorMessage(SIllegalExpression, [Read Code], -1);348 ErrorMessage(SIllegalExpression, [ReadToken], -1); 341 349 end; 342 350 end; … … 352 360 Result.ParentProgram := ProgramCode; 353 361 ParseUnit(TModuleUnit(Result)); 354 end else begin //if FNextToken = 'program' then begin 362 end else 363 if NextToken = 'program' then begin 355 364 Result := TModuleProgram.Create; 356 365 Result.ParentProgram := ProgramCode; 357 366 ParseProgram(TModuleProgram(Result)); 358 end; 367 end else 368 ErrorMessage(SUnknownModuleType, [NextToken]); 359 369 end; 360 370 … … 366 376 if NextToken = 'program' then begin 367 377 Expect('program'); 368 Name := Read Code;378 Name := ReadToken; 369 379 Expect(';'); 370 380 end else Name := ''; … … 385 395 Expect('unit'); 386 396 with Sourcecode do begin 387 Name := Read Code;397 Name := ReadToken; 388 398 end; 389 399 Expect(';'); … … 479 489 else begin 480 490 ErrorMessage(SUnknownIdentifier, [NextToken], -1); 481 Read Code;491 ReadToken; 482 492 end; 483 493 end; … … 502 512 //ShowMessage(NextCode); 503 513 if NextToken = ';' then 504 Read Code;514 ReadToken; 505 515 end; 506 516 Expect('end'); … … 540 550 541 551 // Read function name 542 UseName := Read Code;552 UseName := ReadToken; 543 553 UseType := SourceCode.Parent.Types.Search(UseName); 544 554 if Assigned(UseType) and ((UseType is TTypeRecord) or 545 555 (UseType is TTypeClass)) then begin 546 556 Expect('.'); 547 UseName := Read Code;557 UseName := ReadToken; 548 558 if UseType is TTypeRecord then begin 549 559 UseFunction := TTypeRecord(UseType).CommonBlock.Functions.Search(UseName); … … 569 579 if FunctionType = ftFunction then begin 570 580 Expect(':'); 571 TypeName := Read Code;581 TypeName := ReadToken; 572 582 NewValueType := Parent.Types.Search(TypeName); 573 583 if not Assigned(NewValueType) then … … 615 625 // while IsIdentificator(NextCode) do begin 616 626 with TParameterList(Parameters) do begin 617 VariableName := Read Code;627 VariableName := ReadToken; 618 628 if VariableName = 'var' then begin 619 629 end else … … 625 635 while NextToken = ',' do begin 626 636 Expect(','); 627 Identifiers.Add(Read Code);637 Identifiers.Add(ReadToken); 628 638 end; 629 639 end else 630 640 ErrorMessage(SRedefineIdentifier, [VariableName], -1); 631 641 Expect(':'); 632 TypeName := Read Code;642 TypeName := ReadToken; 633 643 UseType := Parent.Types.Search(TypeName); 634 644 if not Assigned(UseType) then … … 677 687 begin 678 688 Expect('for'); 679 VariableName := Read Code;689 VariableName := ReadToken; 680 690 ControlVariable := SourceCode.CommonBlock.Variables.Search(VariableName); 681 691 if not Assigned(ControlVariable) then … … 708 718 while IsIdentificator(NextToken) and (NextTokenType <> ttEndOfFile) do begin 709 719 Identifiers.Clear; 710 VariableName := Read Code;720 VariableName := ReadToken; 711 721 Variable := Search(VariableName); 712 722 if not Assigned(Variable) then begin … … 714 724 while NextToken = ',' do begin 715 725 Expect(','); 716 Identifiers.Add(Read Code);726 Identifiers.Add(ReadToken); 717 727 end; 718 728 end else 719 729 ErrorMessage(SRedefineIdentifier, [VariableName], -1); 720 730 Expect(':'); 721 TypeName := Read Code;731 TypeName := ReadToken; 722 732 NewValueType := Parent.Types.Search(TypeName); 723 733 if NewValueType = nil then … … 763 773 with SourceCode do begin 764 774 while IsIdentificator(NextToken) do begin 765 ConstantName := Read Code;775 ConstantName := ReadToken; 766 776 Constant := Search(ConstantName); 767 777 if not Assigned(Constant) then begin … … 769 779 while NextToken = ',' do begin 770 780 Expect(','); 771 Identifiers.Add(Read Code);781 Identifiers.Add(ReadToken); 772 782 end; 773 783 end else … … 775 785 if NextToken = ':' then begin 776 786 Expect(':'); 777 TypeName := Read Code;787 TypeName := ReadToken; 778 788 NewValueType := Parent.Types.Search(TypeName); 779 789 end; 780 790 Expect('='); 781 ConstantValue := Read Code;791 ConstantValue := ReadToken; 782 792 Expect(';'); 783 793 … … 828 838 begin 829 839 if ExpectName then begin 830 Name := Read Code;840 Name := ReadToken; 831 841 Expect(AssignSymbol); 832 842 end; 833 if NextToken = '(' then begin 834 // Enumeration 835 Result := ParseTypeEnumeration(TypeList, Name); 836 end else 837 if NextToken = 'record' then begin 838 Result := ParseTypeRecord(TypeList, Name); 839 end else 840 if NextToken = 'class' then begin 841 Expect('class'); 842 Result := TTypeClass.Create; 843 TTypeClass(Result).Parent := TypeList; 844 TTypeClass(Result).Name := Name; 845 if NextToken <> ';' then begin 846 while (NextToken <> 'end') and (NextTokenType <> ttEndOfFile) do 847 begin 848 TTypeClass(Result).Items.Add(ParseType(TypeList, True, ':')); 849 Expect(';'); 850 end; 851 Expect('end'); 852 end; 853 end else 854 if NextToken = 'array' then begin 855 Expect('array'); 856 Result := TTypeArray.Create; 857 TTypeArray(Result).Parent := TypeList; 858 TType(Result).Name := Name; 859 if NextToken = '[' then begin 860 Expect('['); 861 TypeName := NextToken; 862 TTypeArray(Result).IndexType := ParseType(TypeList, False); 863 if not Assigned(TTypeArray(Result).IndexType) then 864 ErrorMessage(SUndefinedType, [TypeName], -1); 865 Expect(']'); 866 end; 867 Expect('of'); 868 TypeName := NextToken; 869 TTypeArray(Result).ItemType := ParseType(TypeList, False); 870 if not Assigned(TTypeArray(Result).ItemType) then 871 ErrorMessage(SUndefinedType, [TypeName], -1); 872 end else 873 if NextToken = '^' then begin 874 // Pointer 875 Expect('^'); 876 Result := TTypePointer.Create; 877 TTypePointer(Result).Parent := TypeList; 878 TTypePointer(Result).Name := Name; 879 TTypePointer(Result).UsedType := ParseType(TypeList, False); 880 end else 881 if NextToken = 'type' then begin 843 Result := ParseTypeEnumeration(TypeList, Name); 844 if not Assigned(Result) then Result := ParseTypeRecord(TypeList, Name); 845 if not Assigned(Result) then Result := ParseTypeClass(TypeList, Name); 846 if not Assigned(Result) then Result := ParseTypeArray(TypeList, Name); 847 if not Assigned(Result) then Result := ParseTypePointer(TypeList, Name); 848 if not Assigned(Result) then Result := ParseTypeBase(TypeList, Name); 849 if not Assigned(Result) then Result := ParseTypeSubType(TypeList, Name, ExpectName); 850 if not Assigned(Result) then Result := ParseTypeSubRange(TypeList, Name); 851 if not Assigned(Result) then 852 ErrorMessage(SInvalidConstruction, []); 853 end; 854 end; 855 856 function TPascalParser.ParseTypeSubType(TypeList: TTypeList; Name: string; 857 ExpectName: Boolean): TType; 858 var 859 TypeName: string; 860 begin 861 // Use existed type 862 if NextTokenType = ttIdentifier then begin 863 TypeName := ReadToken; 864 if ExpectName then begin 865 Result := TType.Create; 866 TType(Result).Parent := TypeList; 867 TType(Result).Name := Name; 868 TType(Result).UsedType := TypeList.Search(TypeName); 869 if not Assigned(TType(Result).UsedType) then 870 ErrorMessage(SUndefinedType, [TypeName], -1); 871 end else begin 872 TType(Result) := TypeList.Search(TypeName); 873 if not Assigned(TType(Result)) then 874 ErrorMessage(SUndefinedType, [TypeName], -1); 875 end; 876 end else Result := nil; 877 end; 878 879 function TPascalParser.ParseTypeBase(TypeList: TTypeList; Name: string): TType; 880 begin 882 881 // Buildin base type construction 883 Expect('type'); 884 Result := TTypeInherited.Create; 885 TTypeInherited(Result).Parent := TypeList; 886 TTypeInherited(Result).Name := Name; 887 if NextToken = '(' then begin 888 Expect('('); 889 TTypeInherited(Result).UsedType := ParseType(TypeList, False); 890 Expect(')'); 891 end else TTypeInherited(Result).UsedType := nil; 892 end else begin 893 // Use existed type 894 TypeName := ReadCode; 895 if ExpectName then begin 896 Result := TType.Create; 897 TType(Result).Parent := TypeList; 898 TType(Result).Name := Name; 899 TType(Result).UsedType := TypeList.Search(TypeName); 900 if not Assigned(TType(Result).UsedType) then 901 ErrorMessage(SUndefinedType, [TypeName], -1); 902 end else begin 903 TType(Result) := TypeList.Search(TypeName); 904 if not Assigned(TType(Result)) then 905 ErrorMessage(SUndefinedType, [TypeName], -1); 906 end; 907 end; 908 end; 882 if NextToken = 'type' then begin 883 Expect('type'); 884 Result := TTypeInherited.Create; 885 TTypeInherited(Result).Parent := TypeList; 886 TTypeInherited(Result).Name := Name; 887 if NextToken = '(' then begin 888 Expect('('); 889 TTypeInherited(Result).UsedType := ParseType(TypeList, False); 890 Expect(')'); 891 end else TTypeInherited(Result).UsedType := nil; 892 end else Result := nil; 893 end; 894 895 function TPascalParser.ParseTypePointer(TypeList: TTypeList; Name: string 896 ): TType; 897 begin 898 if NextToken = '^' then begin 899 Expect('^'); 900 Result := TTypePointer.Create; 901 TTypePointer(Result).Parent := TypeList; 902 TTypePointer(Result).Name := Name; 903 TTypePointer(Result).UsedType := ParseType(TypeList, False); 904 end else Result := nil; 909 905 end; 910 906 911 907 function TPascalParser.ParseTypeEnumeration(TypeList: TTypeList; Name: string): TType; 912 908 begin 909 if NextToken = '(' then begin 913 910 Expect('('); 914 911 Result := TTypeEnumeration.Create; … … 917 914 with TTypeEnumeration(Result) do 918 915 with TEnumItem(Items[Items.Add(TEnumItem.Create)]) do begin 919 Name := Read Code;916 Name := ReadToken; 920 917 if (NextToken = '=') and (NextTokenType = ttConstantNumber) then begin 921 918 Expect('='); 922 Index := StrToInt(Read Code);919 Index := StrToInt(ReadToken); 923 920 end; 924 921 end; … … 928 925 with TTypeEnumeration(Result) do 929 926 with TEnumItem(Items[Items.Add(TEnumItem.Create)]) do begin 930 Name := Read Code;927 Name := ReadToken; 931 928 if (NextToken = '=') and (NextTokenType = ttConstantNumber) then begin 932 929 Expect('='); 933 Index := StrToInt(Read Code);930 Index := StrToInt(ReadToken); 934 931 end; 935 932 end; 936 933 end; 937 934 Expect(')'); 935 end else Result := nil; 938 936 end; 939 937 … … 946 944 SectionType: TSectionType; 947 945 begin 946 if NextToken = 'record' then begin 948 947 SectionType := stVar; 949 948 Visibility := tvPublic; … … 990 989 if IsIdentificator(NextToken) then 991 990 ParseVariableList(TTypeRecord(Result).CommonBlock.Variables, True) 992 else Read Code;991 else ReadToken; 993 992 //TTypeRecord(Result).CommonBlock.Types.Add(ParseType(TypeList, True, ':')); 994 993 //TType(TTypeRecord(Result).CommonBlock.Types.Last).Visibility := Visibility; … … 1001 1000 end; 1002 1001 Expect('end'); 1002 end else Result := nil; 1003 end; 1004 1005 function TPascalParser.ParseTypeClass(TypeList: TTypeList; Name: string 1006 ): TType; 1007 begin 1008 if NextToken = 'class' then begin 1009 Expect('class'); 1010 Result := TTypeClass.Create; 1011 TTypeClass(Result).Parent := TypeList; 1012 TTypeClass(Result).Name := Name; 1013 if NextToken <> ';' then begin 1014 while (NextToken <> 'end') and (NextTokenType <> ttEndOfFile) do 1015 begin 1016 TTypeClass(Result).Items.Add(ParseType(TypeList, True, ':')); 1017 Expect(';'); 1018 end; 1019 Expect('end'); 1020 end; 1021 end else Result := nil; 1022 end; 1023 1024 function TPascalParser.ParseTypeArray(TypeList: TTypeList; Name: string 1025 ): TType; 1026 var 1027 UseName: string; 1028 UseType: TType; 1029 begin 1030 if NextToken = 'array' then begin 1031 Expect('array'); 1032 Result := TTypeArray.Create; 1033 TTypeArray(Result).Parent := TypeList; 1034 TType(Result).Name := Name; 1035 if NextToken = '[' then begin 1036 Expect('['); 1037 UseName := NextToken; 1038 if NextTokenType = ttIdentifier then begin 1039 UseType := TypeList.Parent.Types.Search(UseName); 1040 if not Assigned(TTypeArray(Result).IndexType) then 1041 ErrorMessage(SUndefinedType, [UseName], -1) else 1042 TTypeArray(Result).IndexType := UseType; 1043 end else 1044 if NextTokenType = ttConstantNumber then begin 1045 1046 1047 end; 1048 Expect(']'); 1049 end; 1050 Expect('of'); 1051 UseName := NextToken; 1052 TTypeArray(Result).ItemType := ParseType(TypeList, False); 1053 if not Assigned(TTypeArray(Result).ItemType) then 1054 ErrorMessage(SUndefinedType, [UseName], -1); 1055 end else Result := nil; 1056 end; 1057 1058 function TPascalParser.ParseTypeSubRange(TypeList: TTypeList; Name: string 1059 ): TType; 1060 var 1061 UseName: string; 1062 begin 1063 if NextTokenType = ttConstantString then begin 1064 Result := TTypeSubRange.Create; 1065 TTypeSubRange(Result).Bottom := ReadToken; 1066 Expect('..'); 1067 TTypeSubRange(Result).Top := ReadToken; 1068 end else 1069 if NextTokenType = ttConstantNumber then begin 1070 Result := TTypeSubRange.Create; 1071 TTypeSubRange(Result).Bottom := ReadToken; 1072 Expect('..'); 1073 TTypeSubRange(Result).Top := ReadToken; 1074 end else Result := nil; 1003 1075 end; 1004 1076 … … 1022 1094 with TUsedModule(SourceCode.Items[SourceCode.Add(TUsedModule.Create)]) do 1023 1095 begin 1024 Name := Read Code;1096 Name := ReadToken; 1025 1097 if NextToken = 'in' then begin 1026 1098 Expect('in'); 1027 Location := Read Code;1099 Location := ReadToken; 1028 1100 end else Location := Name + '.pas'; 1029 1101 Module := SourceCode.ParentModule.ParentProgram.Modules.Search(Name); … … 1042 1114 with TUsedModule(SourceCode.Items[SourceCode.Add(TUsedModule.Create)]) do 1043 1115 begin 1044 Name := Read Code;1116 Name := ReadToken; 1045 1117 if NextToken = 'in' then begin 1046 1118 Expect('in'); 1047 Location := Read Code;1119 Location := ReadToken; 1048 1120 end else Location := Name + '.pas'; 1049 1121 Module := SourceCode.ParentModule.ParentProgram.Modules.Search(Name);
Note:
See TracChangeset
for help on using the changeset viewer.