Ignore:
Timestamp:
Oct 14, 2010, 8:41:34 AM (14 years ago)
Author:
george
Message:
  • Added: On parsing uses section load and parse unit files.
File:
1 edited

Legend:

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

    r58 r59  
    4545  end;
    4646
     47  TGetSourceEvent = function (Name: string; Source: TStringList): Boolean of object;
     48
    4749  { TPascalParser }
    4850
    4951  TPascalParser = class(TBaseParser)
     52  private
     53    FOnGetSource: TGetSourceEvent;
     54  public
     55    function ParseFile(Name: string): Boolean;
    5056    procedure ParseWhileDo(SourceCode: TWhileDo);
    5157    procedure ParseExpression(SourceCode: TExpression);
    52     procedure ParseUsedModuleList(SourceCode: TUsedModuleList);
    53     function ParseModule: TModule;
     58    procedure ParseUses(SourceCode: TUsedModuleList);
     59    function ParseModule(ProgramCode: TProgram): TModule;
    5460    procedure ParseUnit(SourceCode: TModuleUnit);
    5561    procedure ParseUnitInterface(SourceCode: TUnitInterface);
     
    6874    procedure ParseTypeList(SourceCode: TTypeList);
    6975    function ParseType(TypeList: TTypeList; ExpectName: Boolean = True; AssignSymbol: string = '='): TType;
    70   private
     76    property OnGetSource: TGetSourceEvent read FOnGetSource
     77      write FOnGetSource;
     78    constructor Create;
     79    destructor Destroy; override;
    7180  end;
    7281
     
    8493  SUndefinedType = 'Undefined type "%s".';
    8594  SUndefinedConstant = 'Undefined constant "%s".';
     95  SUnitNotFound = 'Unit "%s" not found.';
    8696
    8797{ TBaseParser }
     
    335345end;
    336346
    337 { TParserWhileDo }
     347{ TPascalParser }
     348
     349function TPascalParser.ParseFile(Name: string): Boolean;
     350var
     351  Parser: TPascalParser;
     352  NewModule: TModule;
     353begin
     354  try
     355    Parser := TPascalParser.Create;
     356    Parser.SourceCodeText := TStringList.Create;
     357    Parser.ProgramCode := ProgramCode;
     358    if Assigned(FOnGetSource) then begin
     359      if FOnGetSource(Name, Parser.SourceCodeText) then begin
     360        Parser.Init;
     361        NewModule := Parser.ParseModule(ProgramCode);
     362        ProgramCode.Modules.Add(NewModule);
     363        Result := True;
     364      end else Result := False;
     365    end else Result := False;
     366  finally
     367    Parser.SourceCodeText.Free;
     368    Parser.Free;
     369  end;
     370end;
    338371
    339372procedure TPascalParser.ParseWhileDo(SourceCode: TWhileDo);
     
    631664{ TParserModule }
    632665
    633 function TPascalParser.ParseModule: TModule;
    634 begin
     666function TPascalParser.ParseModule(ProgramCode: TProgram): TModule;
     667begin
     668  Self.ProgramCode := ProgramCode;
    635669  if FNextToken = 'unit' then begin
    636670    Result := TModuleUnit.Create;
     671    Result.ParentProgram := ProgramCode;
    637672    ParseUnit(TModuleUnit(Result));
    638673  end else begin //if FNextToken = 'program' then begin
    639674    Result := TModuleProgram.Create;
     675    Result.ParentProgram := ProgramCode;
    640676    ParseProgram(TModuleProgram(Result));
    641677  end;
     
    655691    // Uses section
    656692    if FNextToken = 'uses' then
    657       ParseUsedModuleList(UsedModules);
     693      ParseUses(UsedModules);
    658694
    659695    ParseCommonBlock(Body, '.');
     
    681717  // Uses section
    682718  if FNextToken = 'uses' then
    683     ParseUsedModuleList(SourceCode.UsedModules);
     719    ParseUses(SourceCode.UsedModules);
    684720
    685721  ParseCommonBlockInterface(SourceCode.Body);
     
    692728  // Uses section
    693729  if FNextToken = 'uses' then
    694     ParseUsedModuleList(SourceCode.UsedModules);
     730    ParseUses(SourceCode.UsedModules);
    695731
    696732  ParseCommonBlock(SourceCode.Body, '.');
     
    11031139end;
    11041140
     1141constructor TPascalParser.Create;
     1142begin
     1143end;
     1144
     1145destructor TPascalParser.Destroy;
     1146begin
     1147  inherited Destroy;
     1148end;
     1149
    11051150{ TParserUsedModuleList }
    11061151
    1107 procedure TPascalParser.ParseUsedModuleList(SourceCode: TUsedModuleList);
     1152procedure TPascalParser.ParseUses(SourceCode: TUsedModuleList);
    11081153var
    11091154  NewUsedModule: TUsedModule;
     
    11131158  begin
    11141159    Name := ReadCode;
    1115   end;
    1116   while FNextToken = ',' do
    1117   begin
     1160    Module := SourceCode.ParentModule.ParentProgram.Modules.Search(Name);
     1161    if not Assigned(Module) then begin
     1162      if not ParseFile(Name) then ErrorMessage(SUnitNotFound, [Name]);
     1163    end;
     1164  end;
     1165  while FNextToken = ',' do begin
    11181166    Expect(',');
    11191167    with TUsedModule(SourceCode.Items[SourceCode.Add(TUsedModule.Create)]) do
    11201168    begin
    11211169      Name := ReadCode;
     1170      Module := SourceCode.ParentModule.ParentProgram.Modules.Search(Name);
     1171      if not Assigned(Module) then begin
     1172        if not ParseFile(Name) then ErrorMessage(SUnitNotFound, [Name]);
     1173      end;
    11221174    end;
    11231175  end;
Note: See TracChangeset for help on using the changeset viewer.