Changeset 43 for branches/DelphiToC/Analyze/UPascalParser.pas
- Timestamp:
- Aug 5, 2010, 4:17:18 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DelphiToC/Analyze/UPascalParser.pas
r42 r43 10 10 11 11 type 12 EEndOfData = class(Exception); 13 12 14 TPascalParser = class; 13 15 … … 24 26 TParserExpression = class 25 27 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); 26 34 end; 27 35 … … 108 116 SRedefineIdentifier = 'Identificator "%s" redefinition.'; 109 117 STypeNotDefined = 'Type "%s" not defined.'; 118 SEndOfDataReached = 'Parser reached to end of input data.'; 119 110 120 111 121 { TPascalParser } … … 122 132 if NextCode <> Code then begin 123 133 ErrorMessage(SExpectedButFound, [Code, NextCode]); 134 135 // Recovery: try to find nearest same code 136 while NextCode <> Code do 137 ReadCode; 124 138 end; 125 139 ReadCode; … … 210 224 with SourceCodeText do 211 225 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); 213 229 J := I; 214 if Copy(Text, J, 1) = '//' then begin 230 if Copy(Text, J, 2) = '//' then begin 231 // Line comment 215 232 while (Text[I] <> #13) and (Text[I] <> #10) do Inc(I); 216 233 Result := ''; 217 234 end else 218 235 if Copy(Text, J, 1) = '{' then begin 236 // Block comment 1 219 237 while (Text[I] <> '}') do Inc(I); 220 238 Result := ''; 221 239 end else 222 if Copy(Text, J, 1) = '(*' then begin 240 if Copy(Text, J, 2) = '(*' then begin 241 // Block comment 2 223 242 while not((Text[I] = '*') and (Text[I + 1] = ')')) do Inc(I); 224 243 Result := ''; 225 244 end else 226 245 if Text[J] = '''' then begin 246 // String constant 227 247 I := J + 1; 228 248 while not ((Text[I] = '''') and (Text[I + 1] <> '''')) do Inc(I); 229 249 Inc(I); 230 Result := Copy(Text, J, I - J 250 Result := Copy(Text, J, I - J); 231 251 end else 232 252 if (Text[J] in SpecChar) then begin 253 // Special char token 233 254 if (Text[J + 1] in SpecChar) then begin 234 255 for II := 0 to High(DoubleSpecChar) do … … 245 266 end; 246 267 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; 249 276 end; 250 277 J := I; … … 472 499 end; 473 500 end; 474 475 (* begin476 Expect('if');477 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin478 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 begin486 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin487 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 end496 else if NextCode = 'repeat' then begin497 Expect('repeat');498 StartIndex := Operations.Count;499 ParseCommonBlockOperation(CommonBlock);500 Expect('until');501 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin502 Instruction := inConditionalJump;503 ExpressionTree := ParseCommonBlockExpression(CommonBlock);504 GotoAddress := StartIndex;505 end;506 end507 else if NextCode = 'while' then begin508 Expect('while');509 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin510 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 begin518 Instruction := inJump;519 GotoAddress := StartIndex;520 end;521 First.GotoAddress := Operations.Count;522 end523 else if NextCode = 'for' then begin524 Expect('for');525 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin526 Instruction := inExpressionEvaluation;527 ExpressionTree := ParseCommonBlockExpression(CommonBlock);528 if (ExpressionTree.NodeType <> ntOperator) and529 (ExpressionTree.OperatorName <> ':=') then ErrorMessage('Expected assigment in for loop');530 if TExpression(TExpression(ExpressionTree).SubItems[0]).NodeType <> ntVariable then531 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 begin536 Instruction := inExpressionEvaluation;537 ExpressionTree := TExpression.Create;538 with ExpressionTree do begin539 NodeType := ntOperator;540 OperatorName := '=';541 SubItems[0] := TExpression.Create;542 with TExpression(SubItems[0]) do begin543 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 begin555 Instruction := inExpressionEvaluation;556 ExpressionTree := TExpression.Create;557 with ExpressionTree do begin558 NodeType := ntOperator;559 OperatorName := ':=';560 SubItems[0] := TExpression.Create;561 with TExpression(SubItems[0]) do begin562 NodeType := ntVariable;563 Variable := LoopVaraible;564 end;565 SubItems[1] := TExpression.Create;566 with TExpression(SubItems[1]) do begin567 NodeType := ntOperator;568 OperatorName := '+';569 SubItems[0] := TExpression.Create;570 with TExpression(SubItems[0]) do begin571 NodeType := ntVariable;572 Variable := LoopVaraible;573 end;574 SubItems[1] := TExpression.Create;575 with TExpression(SubItems[1]) do begin576 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 begin585 Instruction := inJump;586 GotoAddress := StartIndex;587 end;588 First.GotoAddress := Operations.Count;589 end590 else begin591 with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin592 Instruction := inExpressionEvaluation;593 ExpressionTree := ParseCommonBlockExpression(CommonBlock);594 end;595 end;596 *)597 501 end; 598 502 … … 623 527 624 528 // 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 632 532 TParserCommonBlock.Parse(Parser, SourceCode, '.'); 633 533 end; … … 912 812 end; 913 813 814 { TParserUsedModuleList } 815 816 class procedure TParserUsedModuleList.Parse(Parser: TPascalParser; 817 SourceCode: TUsedModuleList); 818 var 819 NewUsedModule: TUsedModule; 820 begin 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; 834 end; 835 914 836 end.
Note:
See TracChangeset
for help on using the changeset viewer.