I think Nesting.pas should be something like below.
It really is an overkill to make it full class with an objectlist an all. This is pure and simple. All users of this code manipulate the 'Levels' array directly.
TNestingLevelList = Class(TObject) { a list of the items above }
Private
Public
Levels: Array[Low(TNestingLevelType)..High(TNestingLevelType)] Of Integer;
Constructor Create;
Destructor Destroy; Override;
Procedure Clear;
Function FinalTest: String;
Function Total: Integer;
End;
Implementation
Uses
SysUtils;
Constructor TNestingLevelList.Create;
Begin
Inherited;
Clear;
End;
Destructor TNestingLevelList.Destroy;
Begin
Inherited;
End;
Procedure TNestingLevelList.Clear;
Var
Index1: TNestingLevelType;
Begin
For Index1 := Low(TNestingLevelType) To High(TNestingLevelType) Do Levels[Index1] := 0;
End;
{ at the end of it all, all should be back to zero }
Function TNestingLevelList.FinalTest: String;
Var
Index1: TNestingLevelType;
Begin
Result := '';
For Index1 := Low(TNestingLevelType) To High(TNestingLevelType) Do Begin
If Levels[Index1] > 0 Then Begin
Result := 'Final nesting level = ' + IntToStr(Levels[Index1]);
Break;
End;
End;
End;
Function TNestingLevelList.Total: Integer;
Var
Index1: TNestingLevelType;
Begin
Result := 0;
For Index1 := Low(TNestingLevelType) To High(TNestingLevelType) Do Result := Result + Levels[Index1];
End;
End.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Didn't test for speed. It definitely is not slower.
BTW, why dont you scrap that whole class and integrate it into TSourceToken where it really belongs.
You dont need another object to simply encapsulate an array.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
> I'd prefer to keep in in an object. There is
> some consistency checking with the data.
You could always do that (sanity check) in the TSourceToken too. After all it is one TNestingLevels per TSourceToken. Just an idea. It is of course upto you.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I think Nesting.pas should be something like below.
It really is an overkill to make it full class with an objectlist an all. This is pure and simple. All users of this code manipulate the 'Levels' array directly.
Interface
Type
TNestingLevelType = (
nlBlock, // generic code indent
nlCaseSelector,
nlRecordType,
nlRecordVariantSection,
nlProcedure,
nlRoundBracket, nlSquareBracket,
nlStatementLabel
);
TNestingLevelList = Class(TObject) { a list of the items above }
Private
Public
Levels: Array[Low(TNestingLevelType)..High(TNestingLevelType)] Of Integer;
Constructor Create;
Destructor Destroy; Override;
Procedure Clear;
Function FinalTest: String;
Function Total: Integer;
End;
Implementation
Uses
SysUtils;
Constructor TNestingLevelList.Create;
Begin
Inherited;
Clear;
End;
Destructor TNestingLevelList.Destroy;
Begin
Inherited;
End;
Procedure TNestingLevelList.Clear;
Var
Index1: TNestingLevelType;
Begin
For Index1 := Low(TNestingLevelType) To High(TNestingLevelType) Do Levels[Index1] := 0;
End;
{ at the end of it all, all should be back to zero }
Function TNestingLevelList.FinalTest: String;
Var
Index1: TNestingLevelType;
Begin
Result := '';
For Index1 := Low(TNestingLevelType) To High(TNestingLevelType) Do Begin
If Levels[Index1] > 0 Then Begin
Result := 'Final nesting level = ' + IntToStr(Levels[Index1]);
Break;
End;
End;
End;
Function TNestingLevelList.Total: Integer;
Var
Index1: TNestingLevelType;
Begin
Result := 0;
For Index1 := Low(TNestingLevelType) To High(TNestingLevelType) Do Result := Result + Levels[Index1];
End;
End.
That is simpler and just as readable. Is it significantly faster?
Didn't test for speed. It definitely is not slower.
BTW, why dont you scrap that whole class and integrate it into TSourceToken where it really belongs.
You dont need another object to simply encapsulate an array.
I've used the "fiValues: array[TNestingLevelType] of integer;" idea
It is a few seconds faster, and several lines shorter.
> why dont you scrap that whole class and integrate it into TSourceToken
I'd prefer to keep in in an object. There is some consistency checking with the data.
> I'd prefer to keep in in an object. There is
> some consistency checking with the data.
You could always do that (sanity check) in the TSourceToken too. After all it is one TNestingLevels per TSourceToken. Just an idea. It is of course upto you.