Changeset 3 for trunk/Compiler/Analyze


Ignore:
Timestamp:
Nov 4, 2010, 12:19:14 PM (14 years ago)
Author:
george
Message:
  • Modified: More parsing procedures transformed to functions.
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore set to
      lib
  • trunk/Compiler

    • Property svn:ignore set to
      lib
  • trunk/Compiler/Analyze/UParser.pas

    r2 r3  
    360360        // Update cursor position
    361361        Inc(CodePosition.X);
    362         if (CurrentChar = LineEnding) then begin
     362        if (CurrentChar = LineEnding[1]) then begin
    363363          CodePosition.X := 0;
    364364          Inc(CodePosition.Y);
  • trunk/Compiler/Analyze/UPascalParser.pas

    r2 r3  
    1818  public
    1919    function ParseFile(Name: string): Boolean;
    20     procedure ParseWhileDo(SourceCode: TWhileDo);
     20    function ParseWhileDo(var WhileDo: TWhileDo; SourceCode: TCommonBlock): Boolean;
    2121    procedure ParseExpression(SourceCode: TExpression);
    2222    function ParseRightValue(SourceCode: TExpression): TObject;
    2323    function ParseFunctionCall(SourceCode: TExpression): TObject;
    24     procedure ParseUses(SourceCode: TUsedModuleList; AExported: Boolean);
     24    function ParseUses(SourceCode: TUsedModuleList; AExported: Boolean): Boolean;
    2525    function ParseModule(ProgramCode: TProgram): TModule;
    26     procedure ParseUnit(SourceCode: TModuleUnit);
    27     procedure ParseUnitInterface(SourceCode: TModuleUnit);
    28     procedure ParseUnitImplementation(SourceCode: TModuleUnit);
     26    function ParseUnit(var SourceCode: TModuleUnit; ProgramCode: TProgram): Boolean;
     27    function ParseUnitInterface(SourceCode: TModuleUnit): Boolean;
     28    function ParseUnitImplementation(SourceCode: TModuleUnit): Boolean;
    2929    procedure ParseProgram(SourceCode: TModuleProgram);
    3030    procedure ParseCommonBlock(SourceCode: TCommonBlock; EndSymbol: char = ';';
     
    3232    procedure ParseCommonBlockInterface(SourceCode: TCommonBlock);
    3333    function ParseCommand(SourceCode: TCommonBlock): TCommand;
    34     procedure ParseBeginEnd(SourceCode: TBeginEnd);
     34    function ParseBeginEnd(var BeginEnd: TBeginEnd; SourceCode: TCommonBlock): Boolean;
    3535    function ParseFunctionList(SourceCode: TFunctionList; Exported: Boolean = False): Boolean;
    3636    procedure ParseFunctionParameters(SourceCode: TFunction; ValidateParams: Boolean = False);
    37     procedure ParseIfThenElse(SourceCode: TIfThenElse);
    38     procedure ParseForToDo(SourceCode: TForToDo);
     37    function ParseIfThenElse(var IfThenElse: TIfThenElse; SourceCode: TCommonBlock): Boolean;
     38    function ParseForToDo(var ForToDo: TForToDo; SourceCode: TCommonBlock): Boolean;
    3939    function ParseVariableList(SourceCode: TVariableList; Exported: Boolean = False): Boolean;
    4040    procedure ParseVariable(SourceCode: TVariableList; Exported: Boolean = False);
     
    107107end;
    108108
    109 procedure TPascalParser.ParseWhileDo(SourceCode: TWhileDo);
    110 begin
    111   with SourceCode do
    112   begin
     109function TPascalParser.ParseWhileDo(var WhileDo: TWhileDo; SourceCode: TCommonBlock): Boolean;
     110begin
     111  if NextToken = 'while' then begin
    113112    Expect('while');
    114     Condition.CommonBlock := CommonBlock;
    115     ParseExpression(Condition);
    116     Expect('do');
    117     Command := ParseCommand(CommonBlock);
    118   end;
     113    WhileDo := TWhileDo.Create;
     114    WhileDo.CommonBlock := SourceCode;
     115    with WhileDo do begin
     116      Condition.CommonBlock := CommonBlock;
     117      ParseExpression(Condition);
     118      Expect('do');
     119      Command := ParseCommand(CommonBlock);
     120    end;
     121    Result := True;
     122  end else Result := False;
    119123end;
    120124
     
    311315begin
    312316  begin
    313     if NextToken = 'begin' then begin
    314       Result := TBeginEnd.Create;
    315       TBeginEnd(Result).CommonBlock := SourceCode;
    316       //ShowMessage(IntToStr(Integer(SourceCode))
    317       // + ' ' + IntToStr(Integer(Result)));
    318       ParseBeginEnd(TBeginEnd(Result));
    319     end else
    320     if NextToken = 'if' then begin
    321       Result := TIfThenElse.Create;
    322       TIfThenElse(Result).CommonBlock := SourceCode;
    323       ParseIfThenElse(TIfThenElse(Result));
    324     end else
    325     if NextToken = 'while' then begin
    326       Result := TWhileDo.Create;
    327       TWhileDo(Result).CommonBlock := SourceCode;
    328       ParseWhileDo(TWhileDo(Result));
    329     end else
    330     if NextToken = 'for' then begin
    331       Result := TForToDo.Create;
    332       TForToDo(Result).CommonBlock := SourceCode;
    333       ParseForToDo(TForToDo(Result));
    334     end else
     317    if not ParseBeginEnd(TBeginEnd(Result), SourceCode) then
     318    if not ParseIfThenElse(TIfThenElse(Result), SourceCode) then
     319    if not ParseWhileDo(TWhileDo(Result), SourceCode) then
     320    if not ParseForToDo(TForToDo(Result), SourceCode) then
    335321    if IsIdentificator(NextToken) then begin
    336322      if Assigned(SourceCode.Variables.Search(NextToken)) then begin
     
    381367begin
    382368  Self.ProgramCode := ProgramCode;
    383   if NextToken = 'unit' then begin
    384     Result := TModuleUnit.Create;
    385     Result.ParentProgram := ProgramCode;
    386     ParseUnit(TModuleUnit(Result));
    387   end else
    388   if NextToken = 'program' then begin
     369  if not ParseUnit(TModuleUnit(Result), ProgramCode) then begin
    389370    Result := TModuleProgram.Create;
    390371    Result.ParentProgram := ProgramCode;
     
    414395end;
    415396
    416 procedure TPascalParser.ParseUnit(SourceCode: TModuleUnit);
     397function TPascalParser.ParseUnit(var SourceCode: TModuleUnit; ProgramCode: TProgram): Boolean;
    417398var
    418399  NewModule: TModule;
    419400begin
    420   Expect('unit');
    421   with Sourcecode do begin
    422     Name := ReadToken;
    423   end;
    424   Expect(';');
    425 
    426   ParseUnitInterface(SourceCode);
    427   if NextToken = 'implementation' then
    428     ParseUnitImplementation(SourceCode);
    429 
    430   SourceCode.ParentProgram.Modules.Add(SourceCode);
    431 
    432   if NextToken = 'initialization' then begin
    433     Expect('initialization');
    434   end;
    435   if NextToken = 'finalization' then begin
    436     Expect('finalization');
    437   end;
    438 end;
    439 
    440 procedure TPascalParser.ParseUnitInterface(SourceCode: TModuleUnit);
    441 begin
    442   Expect('interface');
    443   // Uses section
    444   if NextToken = 'uses' then
     401  if NextToken = 'unit' then begin
     402    SourceCode := TModuleUnit.Create;
     403    SourceCode.ParentProgram := ProgramCode;
     404    Expect('unit');
     405    with Sourcecode do begin
     406      Name := ReadToken;
     407    end;
     408    Expect(';');
     409
     410    if not ParseUnitInterface(SourceCode) then
     411      ErrorMessage(SExpectedButFound, ['interface', NextToken]);
     412
     413    if not ParseUnitImplementation(SourceCode) then
     414      ErrorMessage(SExpectedButFound, ['implementation', NextToken]);
     415
     416    SourceCode.ParentProgram.Modules.Add(SourceCode);
     417
     418    if NextToken = 'initialization' then begin
     419      Expect('initialization');
     420    end;
     421    if NextToken = 'finalization' then begin
     422      Expect('finalization');
     423    end;
     424    Result := True;
     425  end else Result := False;
     426end;
     427
     428function TPascalParser.ParseUnitInterface(SourceCode: TModuleUnit): Boolean;
     429begin
     430  if NextToken = 'interface' then begin
     431    Expect('interface');
     432    // Uses section
    445433    ParseUses(SourceCode.UsedModules, True);
    446434
    447   ParseCommonBlockInterface(SourceCode.Body);
    448 end;
    449 
    450 procedure TPascalParser.ParseUnitImplementation(SourceCode: TModuleUnit);
    451 begin
    452   Expect('implementation');
    453 
    454   // Uses section
    455   if NextToken = 'uses' then
    456     ParseUses(SourceCode.UsedModules, False);
    457 
    458   ParseCommonBlock(SourceCode.Body, '.', False);
     435    ParseCommonBlockInterface(SourceCode.Body);
     436    Result := True;
     437  end else Result := False;
     438end;
     439
     440function TPascalParser.ParseUnitImplementation(SourceCode: TModuleUnit): Boolean;
     441begin
     442  if NextToken = 'implementation' then begin
     443    Expect('implementation');
     444
     445    // Uses section
     446    if NextToken = 'uses' then
     447      ParseUses(SourceCode.UsedModules, False);
     448
     449    ParseCommonBlock(SourceCode.Body, '.', False);
     450    Result := True;
     451  end else Result := False;
    459452end;
    460453
     
    471464      if not ParseFunctionList(Functions) then begin
    472465        if WithBody then
    473           ParseBeginEnd(Code);
     466          if not ParseBeginEnd(Code, SourceCode) then
     467            ErrorMessage(SExpectedButFound, ['begin', NextToken]);
    474468        Break;
    475469      end;
     
    496490{ TParserBeginEnd }
    497491
    498 procedure TPascalParser.ParseBeginEnd(SourceCode: TBeginEnd);
     492function TPascalParser.ParseBeginEnd(var BeginEnd: TBeginEnd; SourceCode: TCommonBlock): Boolean;
    499493var
    500494  NewCommand: TCommand;
    501495begin
    502   //ShowMessage(IntToStr(Integer(SourceCode)) + ' ' + IntToStr(Integer(SourceCode.CommonBlock)));
    503   with SourceCode do
    504   begin
    505     Expect('begin');
    506     while (NextToken <> 'end') and (NextTokenType <> ttEndOfFile) do
     496  if NextToken = 'begin' then begin
     497    //ShowMessage(IntToStr(Integer(SourceCode)) + ' ' + IntToStr(Integer(SourceCode.CommonBlock)));
     498    BeginEnd := TBeginEnd.Create;
     499    TBeginEnd(BeginEnd).CommonBlock := SourceCode;
     500    with BeginEnd do
    507501    begin
    508       NewCommand := ParseCommand(CommonBlock);
    509       if Assigned(NewCommand) then
    510         Commands.Add(NewCommand);
    511       //ShowMessage(NextCode);
    512       if NextToken = ';' then
    513         ReadToken;
    514     end;
    515     Expect('end');
    516   end;
     502      Expect('begin');
     503      while (NextToken <> 'end') and (NextTokenType <> ttEndOfFile) do
     504      begin
     505        NewCommand := ParseCommand(CommonBlock);
     506        if Assigned(NewCommand) then
     507          Commands.Add(NewCommand);
     508       //ShowMessage(NextCode);
     509        if NextToken = ';' then
     510          ReadToken;
     511      end;
     512      Expect('end');
     513    end;
     514    Result := True;
     515  end else Result := False;
    517516end;
    518517
     
    680679{ TParserIfThenElse }
    681680
    682 procedure TPascalParser.ParseIfThenElse(SourceCode: TIfThenElse);
    683 begin
    684   with SourceCode do begin
    685     Expect('if');
    686     Condition.CommonBlock := CommonBlock;
    687     ParseExpression(Condition);
    688     Expect('then');
    689     Command := ParseCommand(CommonBlock);
    690     if NextToken = 'else' then
    691     begin
    692       Expect('else');
    693       ElseCommand := ParseCommand(CommonBlock);
    694     end;
    695   end;
    696 end;
    697 
    698 procedure TPascalParser.ParseForToDo(SourceCode: TForToDo);
     681function TPascalParser.ParseIfThenElse(var IfThenElse: TIfThenElse; SourceCode: TCommonBlock): Boolean;
     682begin
     683  if NextToken = 'if' then begin
     684    IfThenElse := TIfThenElse.Create;
     685    IfThenElse.CommonBlock := SourceCode;
     686    with IfThenElse do begin
     687      Expect('if');
     688      Condition.CommonBlock := CommonBlock;
     689      ParseExpression(Condition);
     690      Expect('then');
     691      Command := ParseCommand(CommonBlock);
     692      if NextToken = 'else' then
     693      begin
     694        Expect('else');
     695        ElseCommand := ParseCommand(CommonBlock);
     696      end;
     697    end;
     698    Result := True;
     699  end else Result := False;
     700end;
     701
     702function TPascalParser.ParseForToDo(var ForToDo: TForToDo; SourceCode: TCommonBlock): Boolean;
    699703var
    700704  VariableName: string;
    701705begin
    702   with SourceCode do
    703   begin
    704     Expect('for');
    705     VariableName := ReadToken;
    706     ControlVariable := SourceCode.CommonBlock.Variables.Search(VariableName);
    707     if not Assigned(ControlVariable) then
    708       ErrorMessage(SUndefinedVariable, [VariableName], -1);
    709     Expect(':=');
    710     Start.CommonBlock := CommonBlock;
    711     ParseExpression(Start);
    712     Expect('to');
    713     Stop.CommonBlock := CommonBlock;
    714     ParseExpression(Stop);
    715     Expect('do');
    716     Command := ParseCommand(CommonBlock);
    717   end;
     706  if NextToken = 'for' then begin
     707    ForToDo := TForToDo.Create;
     708    ForToDo.CommonBlock := SourceCode;
     709    with ForToDo do begin
     710      Expect('for');
     711      VariableName := ReadToken;
     712      ControlVariable := ForToDo.CommonBlock.Variables.Search(VariableName);
     713      if not Assigned(ControlVariable) then
     714        ErrorMessage(SUndefinedVariable, [VariableName], -1);
     715      Expect(':=');
     716      Start.CommonBlock := CommonBlock;
     717      ParseExpression(Start);
     718      Expect('to');
     719      Stop.CommonBlock := CommonBlock;
     720      ParseExpression(Stop);
     721      Expect('do');
     722      Command := ParseCommand(CommonBlock);
     723    end;
     724    Result := True;
     725  end else Result := False;
    718726end;
    719727
     
    11281136{ TParserUsedModuleList }
    11291137
    1130 procedure TPascalParser.ParseUses(SourceCode: TUsedModuleList; AExported: Boolean = False);
     1138function TPascalParser.ParseUses(SourceCode: TUsedModuleList; AExported: Boolean = False): Boolean;
    11311139var
    11321140  NewUsedModule: TUsedModule;
    11331141begin
    1134   Expect('uses');
    1135   with TUsedModule(SourceCode.Items[SourceCode.Add(TUsedModule.Create)]) do
    1136   begin
    1137     Name := ReadToken;
    1138     if NextToken = 'in' then begin
    1139       Expect('in');
    1140       Location := ReadToken;
    1141     end else Location := Name + '.pas';
    1142     Module := SourceCode.ParentModule.ParentProgram.Modules.Search(Name);
    1143     if not Assigned(Module) then begin
    1144       if ParseFile(Name) then begin
    1145         Module := SourceCode.ParentModule.ParentProgram.Modules.Search(Name);
    1146         Exported := AExported;
    1147       end else begin
    1148         ErrorMessage(SUnitNotFound, [Name], -2);
    1149         SourceCode.Delete(SourceCode.Count - 1);
    1150       end;
    1151     end;
    1152   end;
    1153   while NextToken = ',' do begin
    1154     Expect(',');
    1155     with TUsedModule(SourceCode.Items[SourceCode.Add(TUsedModule.Create)]) do
    1156     begin
     1142  if NextToken = 'uses' then begin
     1143    Expect('uses');
     1144    with TUsedModule(SourceCode.Items[SourceCode.Add(TUsedModule.Create)]) do begin
    11571145      Name := ReadToken;
    11581146      if NextToken = 'in' then begin
     
    11621150      Module := SourceCode.ParentModule.ParentProgram.Modules.Search(Name);
    11631151      if not Assigned(Module) then begin
    1164         if not ParseFile(Name) then begin
     1152        if ParseFile(Name) then begin
     1153          Module := SourceCode.ParentModule.ParentProgram.Modules.Search(Name);
     1154          Exported := AExported;
     1155        end else begin
    11651156          ErrorMessage(SUnitNotFound, [Name], -2);
    11661157          SourceCode.Delete(SourceCode.Count - 1);
     
    11681159      end;
    11691160    end;
    1170   end;
    1171   Expect(';');
     1161    while NextToken = ',' do begin
     1162      Expect(',');
     1163      with TUsedModule(SourceCode.Items[SourceCode.Add(TUsedModule.Create)]) do
     1164      begin
     1165        Name := ReadToken;
     1166        if NextToken = 'in' then begin
     1167          Expect('in');
     1168          Location := ReadToken;
     1169        end else Location := Name + '.pas';
     1170        Module := SourceCode.ParentModule.ParentProgram.Modules.Search(Name);
     1171        if not Assigned(Module) then begin
     1172          if not ParseFile(Name) then begin
     1173            ErrorMessage(SUnitNotFound, [Name], -2);
     1174            SourceCode.Delete(SourceCode.Count - 1);
     1175          end;
     1176        end;
     1177      end;
     1178    end;
     1179    Expect(';');
     1180    Result := True;
     1181  end else Result := False;
    11721182end;
    11731183
Note: See TracChangeset for help on using the changeset viewer.