Ignore:
Timestamp:
Oct 18, 2010, 2:14:52 PM (14 years ago)
Author:
george
Message:
  • Modified: Advanced record parsing.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Transpascal/Compiler/Analyze/UParser.pas

    r67 r68  
    8383    procedure ParseTypeList(SourceCode: TTypeList; Exported: Boolean = False);
    8484    function ParseType(TypeList: TTypeList; ExpectName: Boolean = True; AssignSymbol: string = '='): TType;
     85    function ParseTypeEnumeration(TypeList: TTypeList; Name: string): TType;
     86    function ParseTypeRecord(TypeList: TTypeList; Name: string): TType;
    8587    property OnGetSource: TGetSourceEvent read FOnGetSource
    8688      write FOnGetSource;
     
    744746begin
    745747  with SourceCode do begin
    746     while FNextToken <> 'implementation' do begin
     748    while (FNextToken <> 'implementation') and (FNextTokenType <> ttEndOfFile) do begin
    747749      if FNextToken = 'var' then
    748750        ParseVariableList(Variables)
     
    755757      else if FNextToken = 'function' then
    756758        ParseFunctionList(Functions, True)
    757       else ErrorMessage(SUnknownIdentifier, [FNextToken]);
     759      else begin
     760        ErrorMessage(SUnknownIdentifier, [FNextToken]);
     761        ReadCode;
     762      end;
    758763    end;
    759764  end;
     
    10711076    end;
    10721077    if NextToken = '(' then begin
    1073       // Enumeration
    1074       Expect('(');
    1075       Result := TTypeEnumeration.Create;
    1076       TTypeEnumeration(Result).Parent := TypeList;
    1077       TTypeEnumeration(Result).Name := Name;
    1078       with TTypeEnumeration(Result) do
    1079       with TEnumItem(Items[Items.Add(TEnumItem.Create)]) do begin
    1080         Name := ReadCode;
    1081         if (NextToken = '=') and (FNextTokenType = ttConstantNumber) then begin
    1082           Expect('=');
    1083           Index := StrToInt(ReadCode);
    1084         end;
    1085       end;
    1086       while (NextToken = ',') and (FNextTokenType <> ttEndOfFile) do
    1087       begin
    1088         Expect(',');
    1089         with TTypeEnumeration(Result) do
    1090         with TEnumItem(Items[Items.Add(TEnumItem.Create)]) do begin
    1091           Name := ReadCode;
    1092           if (NextToken = '=') and (FNextTokenType = ttConstantNumber) then begin
    1093             Expect('=');
    1094             Index := StrToInt(ReadCode);
    1095           end;
    1096         end;
    1097       end;
    1098       Expect(')');
     1078      Result := ParseTypeEnumeration(TypeList, Name);
    10991079    end else
    11001080    if NextToken = 'record' then begin
    1101       Expect('record');
    1102       Result := TTypeRecord.Create;
    1103       TTypeRecord(Result).Parent := TypeList;
    1104       TType(Result).Name := Name;
    1105       while (NextToken <> 'end') and (FNextTokenType <> ttEndOfFile) do
    1106       begin
    1107         TTypeRecord(Result).Items.Add(ParseType(TypeList, True, ':'));
    1108         Expect(';');
    1109       end;
    1110       Expect('end');
     1081      Result := ParseTypeRecord(TypeList, Name);
    11111082    end else
    11121083    if NextToken = 'class' then begin
     
    11781149end;
    11791150
     1151function TPascalParser.ParseTypeEnumeration(TypeList: TTypeList; Name: string): TType;
     1152begin
     1153      Expect('(');
     1154      Result := TTypeEnumeration.Create;
     1155      TTypeEnumeration(Result).Parent := TypeList;
     1156      TTypeEnumeration(Result).Name := Name;
     1157      with TTypeEnumeration(Result) do
     1158      with TEnumItem(Items[Items.Add(TEnumItem.Create)]) do begin
     1159        Name := ReadCode;
     1160        if (NextToken = '=') and (FNextTokenType = ttConstantNumber) then begin
     1161          Expect('=');
     1162          Index := StrToInt(ReadCode);
     1163        end;
     1164      end;
     1165      while (NextToken = ',') and (FNextTokenType <> ttEndOfFile) do
     1166      begin
     1167        Expect(',');
     1168        with TTypeEnumeration(Result) do
     1169        with TEnumItem(Items[Items.Add(TEnumItem.Create)]) do begin
     1170          Name := ReadCode;
     1171          if (NextToken = '=') and (FNextTokenType = ttConstantNumber) then begin
     1172            Expect('=');
     1173            Index := StrToInt(ReadCode);
     1174          end;
     1175        end;
     1176      end;
     1177      Expect(')');
     1178end;
     1179
     1180function TPascalParser.ParseTypeRecord(TypeList: TTypeList; Name: string
     1181  ): TType;
     1182var
     1183  Visibility: TTypeVisibility;
     1184begin
     1185  Visibility := tvPublic;
     1186      Expect('record');
     1187      Result := TTypeRecord.Create;
     1188      TTypeRecord(Result).Parent := TypeList;
     1189      TType(Result).Name := Name;
     1190      while (NextToken <> 'end') and (FNextTokenType <> ttEndOfFile) do
     1191      begin
     1192        if NextToken = 'public' then begin
     1193          Expect('public');
     1194          Visibility := tvPublic;
     1195        end else
     1196        if NextToken = 'private' then begin
     1197          Expect('private');
     1198          Visibility := tvPrivate;
     1199        end else
     1200        if NextToken = 'published' then begin
     1201          Expect('published');
     1202          Visibility := tvPublished;
     1203        end else
     1204        if NextToken = 'protected' then begin
     1205          Expect('protected');
     1206          Visibility := tvProtected;
     1207        end else
     1208        if NextToken = 'var' then
     1209          ParseVariableList(TTypeRecord(Result).CommonBlock.Variables)
     1210        else if FNextToken = 'const' then
     1211          ParseConstantList(TTypeRecord(Result).CommonBlock.Constants, True)
     1212        else if FNextToken = 'type' then
     1213          ParseTypeList(TTypeRecord(Result).CommonBlock.Types, True)
     1214        else if FNextToken = 'procedure' then
     1215          ParseFunctionList(TTypeRecord(Result).CommonBlock.Functions, True)
     1216        else if FNextToken = 'function' then
     1217          ParseFunctionList(TTypeRecord(Result).CommonBlock.Functions, True)
     1218        else begin
     1219          TTypeRecord(Result).CommonBlock.Types.Add(ParseType(TypeList, True, ':'));
     1220          TType(TTypeRecord(Result).CommonBlock.Types.Last).Visibility := Visibility;
     1221        end;
     1222        Expect(';');
     1223      end;
     1224      Expect('end');
     1225end;
     1226
    11801227constructor TPascalParser.Create;
    11811228begin
Note: See TracChangeset for help on using the changeset viewer.