Ignore:
Timestamp:
Oct 21, 2010, 7:56:25 AM (14 years ago)
Author:
george
Message:
  • Modified: Parsing sections in record type.
File:
1 edited

Legend:

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

    r74 r75  
    2626    procedure ParseUnitImplementation(SourceCode: TModuleUnit);
    2727    procedure ParseProgram(SourceCode: TModuleProgram);
    28     procedure ParseCommonBlock(SourceCode: TCommonBlock; EndSymbol: char = ';');
     28    procedure ParseCommonBlock(SourceCode: TCommonBlock; EndSymbol: char = ';';
     29      WithBody: Boolean = True);
    2930    procedure ParseCommonBlockInterface(SourceCode: TCommonBlock);
    3031    function ParseCommand(SourceCode: TCommonBlock): TCommand;
     
    368369
    369370    ParseCommonBlock(Body, '.');
     371    SourceCode.ParentProgram.Modules.Add(SourceCode);
    370372  end;
    371373end;
     
    384386  if NextToken = 'implementation' then
    385387    ParseUnitImplementation(SourceCode);
     388
     389  SourceCode.ParentProgram.Modules.Add(SourceCode);
    386390
    387391  if NextToken = 'initialization' then begin
     
    411415    ParseUses(SourceCode.UsedModules, False);
    412416
    413   ParseCommonBlock(SourceCode.Body, '.');
     417  ParseCommonBlock(SourceCode.Body, '.', False);
    414418end;
    415419
     
    417421
    418422procedure TPascalParser.ParseCommonBlock(SourceCode: TCommonBlock;
    419   EndSymbol: char = ';');
     423  EndSymbol: char = ';'; WithBody: Boolean = True);
    420424begin
    421425  with SourceCode do begin
    422426    while (NextToken <> EndSymbol) do begin
    423       if NextToken = 'var' then
     427      if NextToken = 'var' then begin
     428        Expect('var');
    424429        ParseVariableList(Variables)
    425       else if NextToken = 'const' then
     430      end else
     431      if NextToken = 'const' then begin
     432        Expect('const');
    426433        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
    430440        ParseFunctionList(Functions)
    431441      else if NextToken = 'function' then
    432442        ParseFunctionList(Functions)
    433443      else begin
    434         ParseBeginEnd(Code);
     444        if WithBody then
     445          ParseBeginEnd(Code);
    435446        Break;
    436447      end;
    437448    end;
    438     Expect(EndSymbol);
     449    if WithBody then Expect(EndSymbol);
    439450  end;
    440451end;
     
    444455  with SourceCode do begin
    445456    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
    453470        ParseFunctionList(Functions, True)
    454471      else if NextToken = 'function' then
     
    496513  I: integer;
    497514begin
     515  try
    498516  Identifiers := TStringList.Create;
    499517  with SourceCode do begin
    500518    with TFunction(Items[Add(TFunction.Create)]) do begin
    501519      Parent := SourceCode.Parent;
    502       if NextToken = 'procedure' then
    503       begin
     520      if NextToken = 'procedure' then begin
    504521        Expect('procedure');
    505522        HaveResult := False;
    506       end
    507       else
    508       begin
     523      end else begin
    509524        Expect('function');
    510525        HaveResult := True;
     
    545560            end;
    546561          end;
     562          if NextToken = ';' then Expect(';');
    547563        end;
    548564        Expect(')');
     
    580596    if not Exported then ParseCommonBlock(TFunction(Last));
    581597  end;
    582   Identifiers.Destroy;
     598  finally
     599    Identifiers.Free;
     600  end;
    583601end;
    584602
     
    638656  Identifiers := TStringList.Create;
    639657  with SourceCode do begin
    640     Expect('var');
    641658    while IsIdentificator(NextToken) and (NextTokenType <> ttEndOfFile) do begin
    642659      Identifiers.Clear;
     
    694711begin
    695712  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
    701715      ConstantName := ReadCode;
    702716      Constant := Search(ConstantName);
    703       if not Assigned(Constant) then
    704       begin
     717      if not Assigned(Constant) then begin
    705718        Identifiers.Add(ConstantName);
    706         while NextToken = ',' do
    707         begin
     719        while NextToken = ',' do begin
    708720          Expect(',');
    709721          Identifiers.Add(ReadCode);
    710722        end;
    711       end
    712       else
     723      end else
    713724        ErrorMessage(SRedefineIdentifier, [ConstantName], -1);
    714725      Expect(':');
     
    742753  with SourceCode do
    743754  begin
    744     Expect('type');
    745755    while IsIdentificator(NextToken) do begin
    746756      NewType := ParseType(SourceCode);
     
    872882function TPascalParser.ParseTypeRecord(TypeList: TTypeList; Name: string
    873883  ): TType;
     884type
     885  TSectionType = (stVar, stType, stConst);
    874886var
    875887  Visibility: TTypeVisibility;
    876 begin
     888  SectionType: TSectionType;
     889begin
     890  SectionType := stVar;
    877891  Visibility := tvPublic;
    878892      Expect('record');
    879893      Result := TTypeRecord.Create;
    880894      TTypeRecord(Result).Parent := TypeList;
     895      TTypeRecord(Result).CommonBlock.Parent := TypeList.Parent;
    881896      TType(Result).Name := Name;
    882897      while (NextToken <> 'end') and (NextTokenType <> ttEndOfFile) do
     
    898913          Visibility := tvProtected;
    899914        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
    903940          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);
    915943      end;
    916944      Expect('end');
Note: See TracChangeset for help on using the changeset viewer.