Ignore:
Timestamp:
Aug 5, 2010, 4:17:18 PM (14 years ago)
Author:
george
Message:

End parsing process on reaching data end.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DelphiToC/Analyze/UPascalParser.pas

    r42 r43  
    1010
    1111type
     12  EEndOfData = class(Exception);
     13
    1214  TPascalParser = class;
    1315
     
    2426  TParserExpression = class
    2527    class function Parse(Parser: TPascalParser; SourceCode: TExpression): TExpression;
     28  end;
     29
     30  { TParserUsedModuleList }
     31
     32  TParserUsedModuleList = class
     33    class procedure Parse(Parser: TPascalParser; SourceCode: TUsedModuleList);
    2634  end;
    2735
     
    108116  SRedefineIdentifier = 'Identificator "%s" redefinition.';
    109117  STypeNotDefined = 'Type "%s" not defined.';
     118  SEndOfDataReached = 'Parser reached to end of input data.';
     119
    110120
    111121{ TPascalParser }
     
    122132  if NextCode <> Code then begin
    123133    ErrorMessage(SExpectedButFound, [Code, NextCode]);
     134
     135    // Recovery: try to find nearest same code
     136    while NextCode <> Code do
     137      ReadCode;
    124138  end;
    125139  ReadCode;
     
    210224  with SourceCodeText do
    211225  while Result = '' do begin
    212     while IsWhiteSpace(Text[I]) do Inc(I);
     226    while IsWhiteSpace(Text[I]) and (I < Length(Text)) do Inc(I);
     227    if I = Length(Text) then
     228      raise EEndOfData.Create(SEndOfDataReached);
    213229    J := I;
    214     if Copy(Text, J, 1) = '//' then begin
     230    if Copy(Text, J, 2) = '//' then begin
     231      // Line comment
    215232      while (Text[I] <> #13) and (Text[I] <> #10) do Inc(I);
    216233      Result := '';
    217234    end else
    218235    if Copy(Text, J, 1) = '{' then begin
     236      // Block comment 1
    219237      while (Text[I] <> '}') do Inc(I);
    220238      Result := '';
    221239    end else
    222     if Copy(Text, J, 1) = '(*' then begin
     240    if Copy(Text, J, 2) = '(*' then begin
     241      // Block comment 2
    223242      while not((Text[I] = '*') and (Text[I + 1] = ')')) do Inc(I);
    224243      Result := '';
    225244    end else
    226245    if Text[J] = '''' then begin
     246      // String constant
    227247      I := J + 1;
    228248      while not ((Text[I] = '''') and (Text[I + 1] <> '''')) do Inc(I);
    229249      Inc(I);
    230       Result := Copy(Text, J, I - J );
     250      Result := Copy(Text, J, I - J);
    231251    end else
    232252    if (Text[J] in SpecChar) then begin
     253      // Special char token
    233254      if (Text[J + 1] in SpecChar) then begin
    234255        for II := 0 to High(DoubleSpecChar) do
     
    245266      end;
    246267    end else begin
    247       while IsAlphanumeric(Text[I]) do Inc(I);
    248       Result := LowerCase(Copy(Text, J, I - J));
     268      if IsAlphabetic(Text[I]) then begin
     269        // Identifier
     270        while IsAlphanumeric(Text[I]) do Inc(I);
     271        Result := Copy(Text, J, I - J);
     272      end else begin
     273        while not IsWhiteSpace(Text[I]) do Inc(I);
     274        Result := Copy(Text, J, I - J);
     275      end;
    249276    end;
    250277    J := I;
     
    472499    end;
    473500  end;
    474 
    475 (*    begin
    476       Expect('if');
    477       with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
    478         Instruction := inConditionalJump;
    479         ExpressionTree := ParseCommonBlockExpression(CommonBlock);
    480         Negative := True;
    481       end;
    482       First := Operations[Operations.Count - 1];
    483       Expect('then');
    484       ParseCommonBlockOperation(CommonBlock);
    485       if NextCode = 'else' then begin
    486         with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
    487           Instruction := inJump;
    488         end;
    489         Second := Operations[Operations.Count - 1];
    490         First.GotoAddress := Operations.Count;
    491         Expect('else');
    492         ParseCommonBlockOperation(CommonBlock);
    493         Second.GotoAddress := Operations.Count;
    494       end else First.GotoAddress := Operations.Count;
    495     end
    496     else if NextCode = 'repeat' then begin
    497       Expect('repeat');
    498       StartIndex := Operations.Count;
    499       ParseCommonBlockOperation(CommonBlock);
    500       Expect('until');
    501       with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
    502         Instruction := inConditionalJump;
    503         ExpressionTree := ParseCommonBlockExpression(CommonBlock);
    504         GotoAddress := StartIndex;
    505       end;
    506     end
    507     else if NextCode = 'while' then begin
    508       Expect('while');
    509       with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
    510         Instruction := inConditionalJump;
    511         ExpressionTree := ParseCommonBlockExpression(CommonBlock);
    512       end;
    513       First := Operations[Operations.Count - 1];
    514       StartIndex := Operations.Count - 1;
    515       Expect('do');
    516       ParseCommonBlockOperation(CommonBlock);
    517       with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
    518         Instruction := inJump;
    519         GotoAddress := StartIndex;
    520       end;
    521       First.GotoAddress := Operations.Count;
    522     end
    523     else if NextCode = 'for' then begin
    524       Expect('for');
    525       with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
    526         Instruction := inExpressionEvaluation;
    527         ExpressionTree := ParseCommonBlockExpression(CommonBlock);
    528         if (ExpressionTree.NodeType <> ntOperator) and
    529           (ExpressionTree.OperatorName <> ':=') then ErrorMessage('Expected assigment in for loop');
    530         if TExpression(TExpression(ExpressionTree).SubItems[0]).NodeType <> ntVariable then
    531           ErrorMessage('Index in FOR loop have to be variable');
    532         LoopVaraible := TExpression(TExpression(ExpressionTree).SubItems[0]).Variable;
    533       end;
    534       Expect('to');
    535       with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
    536         Instruction := inExpressionEvaluation;
    537         ExpressionTree := TExpression.Create;
    538         with ExpressionTree do begin
    539           NodeType := ntOperator;
    540           OperatorName := '=';
    541           SubItems[0] := TExpression.Create;
    542           with TExpression(SubItems[0]) do begin
    543             NodeType := ntVariable;
    544             Variable := LoopVaraible;
    545           end;
    546           SubItems[1] := ParseCommonBlockExpression(CommonBlock);
    547         end;
    548         Negative := True;
    549       end;
    550       First := Operations[Operations.Count - 1];
    551       StartIndex := Operations.Count - 1;
    552       Expect('do');
    553       ParseCommonBlockOperation(CommonBlock);
    554       with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
    555         Instruction := inExpressionEvaluation;
    556         ExpressionTree := TExpression.Create;
    557         with ExpressionTree do begin
    558           NodeType := ntOperator;
    559           OperatorName := ':=';
    560           SubItems[0] := TExpression.Create;
    561           with TExpression(SubItems[0]) do begin
    562             NodeType := ntVariable;
    563             Variable := LoopVaraible;
    564           end;
    565           SubItems[1] := TExpression.Create;
    566           with TExpression(SubItems[1]) do begin
    567             NodeType := ntOperator;
    568             OperatorName := '+';
    569             SubItems[0] := TExpression.Create;
    570             with TExpression(SubItems[0]) do begin
    571               NodeType := ntVariable;
    572               Variable := LoopVaraible;
    573             end;
    574             SubItems[1] := TExpression.Create;
    575             with TExpression(SubItems[1]) do begin
    576               NodeType := ntConstant;
    577               //SetLength(Value, 1);
    578               //Value[0] := 1;
    579               Value := 1;
    580             end;
    581           end;
    582         end;
    583       end;
    584       with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
    585         Instruction := inJump;
    586         GotoAddress := StartIndex;
    587       end;
    588       First.GotoAddress := Operations.Count;
    589     end
    590     else begin
    591       with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
    592         Instruction := inExpressionEvaluation;
    593         ExpressionTree := ParseCommonBlockExpression(CommonBlock);
    594       end;
    595     end;
    596   *)
    597501end;
    598502
     
    623527
    624528    // Uses section
    625     if NextCode = 'uses' then begin
    626       Identifier := ReadCode;
    627       while NextCode = ',' do begin
    628         Identifier := ReadCode;
    629 
    630       end;
    631     end;
     529    if NextCode = 'uses' then
     530      TParserUsedModuleList.Parse(Parser, UsedModules);
     531
    632532    TParserCommonBlock.Parse(Parser, SourceCode, '.');
    633533  end;
     
    912812end;
    913813
     814{ TParserUsedModuleList }
     815
     816class procedure TParserUsedModuleList.Parse(Parser: TPascalParser;
     817  SourceCode: TUsedModuleList);
     818var
     819  NewUsedModule: TUsedModule;
     820begin
     821  with Parser do begin
     822    Expect('uses');
     823    with TUsedModule(SourceCode.Items[SourceCode.Add(TUsedModule.Create)]) do begin
     824      Name := ReadCode;
     825    end;
     826    while NextCode = ',' do begin
     827      Expect(',');
     828      with TUsedModule(SourceCode.Items[SourceCode.Add(TUsedModule.Create)]) do begin
     829        Name := ReadCode;
     830      end;
     831    end;
     832    Expect(';');
     833  end;
     834end;
     835
    914836end.
Note: See TracChangeset for help on using the changeset viewer.