Skip to content

Commit

Permalink
Codetools: Use nullable unit
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Oct 17, 2024
1 parent dd984c3 commit cf30716
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 124 deletions.
39 changes: 0 additions & 39 deletions Source/ide/codetools/simba.ide_codetools_base.pas
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,6 @@ generic TKeywordDictionary<_T> = class(TObject)
property Value[Key: PChar]: _T read getValue; default;
end;

// not initialized, so only "safe" to use when used within a class
generic TCache<_T> = record
type
TSelf = specialize TCache<_T>;
private
FValue: _T;
FCached: Boolean;

function getEmpty: Boolean;
procedure setEmpty(Value: Boolean);
public
property Empty: Boolean read getEmpty write setEmpty;

class operator := (AValue: _T): TSelf;
class operator := (AValue: TSelf): _T;
end;
TStringCache = specialize TCache<String>;

{$IFDEF PARSER_LEAK_CHECKS}
TLeakChecker = class(TObject)
protected
Expand Down Expand Up @@ -126,27 +108,6 @@ procedure TKeywordDictionary.Add(Key: String; Value: _T);
FBuckets[Bucket].Value := Value;
end;

function TCache.getEmpty: Boolean;
begin
Result := not FCached;
end;

procedure TCache.setEmpty(Value: Boolean);
begin
FCached := Value;
end;

class operator TCache.:=(AValue: _T): TSelf;
begin
Result.FValue := AValue;
Result.FCached := True;
end;

class operator TCache.:=(AValue: TSelf): _T;
begin
Result := AValue.FValue;
end;

{$IFDEF PARSER_LEAK_CHECKS}
class constructor TLeakChecker.Create;
begin
Expand Down
3 changes: 1 addition & 2 deletions Source/ide/codetools/simba.ide_codetools_includes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ TCodetoolsInclude = class(TCodeParser)
function IsOutdated: Boolean;
function IncRef: TCodetoolsInclude;
function DecRef: TCodetoolsInclude;

end;

TCodetoolsPlugin = class(TCodetoolsInclude)
Expand Down Expand Up @@ -115,7 +114,7 @@ function TCodetoolsInclude.GetPlugins: TStringArray;

function TCodetoolsInclude.GetHash: String;
begin
if FHash.Empty then
if FHash.IsNull then
FHash := inherited + FInDefines.Defines + IntToStr(FInDefines.Stack) + FPlugins.Text;

Result := FHash;
Expand Down
128 changes: 45 additions & 83 deletions Source/ide/codetools/simba.ide_codetools_parser.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
interface

uses
SysUtils, Classes,
SysUtils, Classes, nullable,
simba.base,
simba.containers,
simba.ide_codetools_base,
Expand All @@ -25,7 +25,8 @@ TDeclarationClass = class of TDeclaration;
TDeclarationArray = array of TDeclaration;

TDeclarationStack = specialize TSimbaStack<TDeclaration>;
TDeclarationCache = specialize TCache<TDeclarationArray>;
TDeclarationCache = specialize TNullable<TDeclarationArray>;
TStringCache = specialize TNullable<String>;

TDeclarationList = class(TObject)
protected
Expand Down Expand Up @@ -722,7 +723,7 @@ procedure TDeclarationList.Clear(const FreeDecls: Boolean);

function TDeclaration.GetText: String;
begin
if FText.Empty then
if FText.IsNull then
FText := FLexer.CopyDoc(FStartPos, FEndPos);

Result := FText;
Expand Down Expand Up @@ -750,7 +751,7 @@ function TDeclaration.GetTextNoComments: String;
end;

begin
if FTextNoComments.Empty then
if FTextNoComments.IsNull then
FTextNoComments := Filter(FLexer.CopyDoc(FStartPos, FEndPos));

Result := FTextNoComments;
Expand Down Expand Up @@ -786,18 +787,15 @@ function TDeclaration.GetTextNoCommentsSingleLine: String;
end;

begin
if FTextNoCommentsSingleLine.Empty then
if FTextNoCommentsSingleLine.IsNull then
FTextNoCommentsSingleLine := Filter(FLexer.CopyDoc(FStartPos, FEndPos));

Result := FTextNoCommentsSingleLine;
end;

function TDeclaration.GetName: String;
begin
if FName.Empty then
Result := ''
else
Result := FName;
Result := FName.ValueOrDefault;
end;

function TDeclaration.GetFullName: String;
Expand Down Expand Up @@ -1002,31 +1000,23 @@ constructor TDeclaration_Keyword.Create(Keyword: String);

function TDeclaration_Anchor.GetHeader: String;
begin
if FHeader.Empty then
begin
Result := 'Anchor "' + Name + '"';

FHeader := Result;
end;
if FHeader.IsNull then
FHeader := 'Anchor "' + Name + '"';

Result := FHeader;
end;

function TDeclaration_Type.GetHeader: String;
begin
if FHeader.Empty then
begin
Result := 'type ' + Name + ' = ' + TextNoCommentsSingleLine;

FHeader := Result;
end;
if FHeader.IsNull then
FHeader := 'type ' + Name + ' = ' + TextNoCommentsSingleLine;

Result := FHeader;
end;

function TDeclaration_EnumElement.GetName: string;
begin
if FName.Empty then
if FName.IsNull then
FName := Items.GetTextOfClass(TDeclaration_EnumElementName);

Result := inherited;
Expand All @@ -1042,13 +1032,10 @@ function TDeclaration_EnumElement.GetFullName: String;

function TDeclaration_EnumElement.GetHeader: String;
begin
if FHeader.Empty then
begin
if (FParent is TDeclaration_TypeEnumScoped) then
Result := FParent.Name + '.' + Name
else
Result := Name;
end;
if FHeader.IsNull then
FHeader := IfThen(FParent is TDeclaration_TypeEnumScoped, FParent.Name + '.' + Name, Name);

Result := FHeader;
end;

function TDeclaration_TypeAlias.Dump: String;
Expand Down Expand Up @@ -1098,36 +1085,24 @@ function TDeclaration_TypeArray.VarType: TDeclaration;

function TDeclaration_TypeRecord.GetFields: TDeclarationArray;
begin
if FFields.Empty then
begin
Result := Items.GetByClass(TDeclaration_Field);

FFields := Result;
end;
if FFields.IsNull then
FFields := Items.GetByClass(TDeclaration_Field, True);

Result := FFields;
end;

function TDeclaration_TypeRecord.GetConsts: TDeclarationArray;
begin
if FConsts.Empty then
begin
Result := Items.GetByClass(TDeclaration_Const);

FConsts := Result;
end;
if FConsts.IsNull then
FConsts := Items.GetByClass(TDeclaration_Const, True);

Result := FConsts;
end;

function TDeclaration_Var.GetHeader: String;
begin
if FHeader.Empty then
begin
Result := 'var ' + Name + VarTypeString + VarDefaultString;

FHeader := Result;
end;
if FHeader.IsNull then
FHeader := 'var ' + Name + VarTypeString + VarDefaultString;

Result := FHeader;
end;
Expand All @@ -1139,15 +1114,15 @@ function TDeclaration_Var.GetVarType: TDeclaration;

function TDeclaration_Var.GetVarTypeString: String;
begin
if FVarTypeString.Empty then
if FVarTypeString.IsNull then
FVarTypeString := Items.GetTextOfClassNoCommentsSingleLine(TDeclaration_VarType, ': ');

Result := FVarTypeString;
end;

function TDeclaration_Var.GetVarDefaultString: String;

function ReplaceunPrintable(const Str: String): String;
function ReplaceUnPrintable(const Str: String): String;
var
I: Integer = 1;
begin
Expand All @@ -1165,27 +1140,24 @@ function TDeclaration_Var.GetVarDefaultString: String;
end;

begin
if FVarDefaultString.Empty then
if FVarDefaultString.IsNull then
begin
case DefToken of
tokAssign: FVarDefaultString := Items.GetTextOfClassNoCommentsSingleLine(TDeclaration_VarDefault, ' := ');
tokEqual: FVarDefaultString := Items.GetTextOfClassNoCommentsSingleLine(TDeclaration_VarDefault, ' = ');
else
FVarDefaultString := '';
end;

FVarDefaultString := ReplaceunPrintable(FVarDefaultString);
FVarDefaultString := ReplaceUnPrintable(FVarDefaultString);
end;

Result := FVarDefaultString;
end;

function TDeclaration_Const.GetHeader: String;
begin
if FHeader.Empty then
begin
Result := 'const ' + Name + VarDefaultString;

FHeader := Result;
end;
if FHeader.IsNull then
FHeader := 'const ' + Name + VarDefaultString;

Result := FHeader;
end;
Expand All @@ -1199,32 +1171,24 @@ function TDeclaration_Field.Dump: String;

function TDeclaration_EnumElementName.GetName: string;
begin
if FName.Empty then
if FName.IsNull then
FName := Text;

Result := FName;
end;

function TDeclaration_TypeEnum.GetElements: TDeclarationArray;
begin
if FElements.Empty then
begin
Result := FItems.GetByClass(TDeclaration_EnumElement);

FElements := Result;
end;
if FElements.IsNull then
FElements := FItems.GetByClass(TDeclaration_EnumElement);

Result := FElements;
end;

function TDeclaration_TypeSet.GetEnumElements: TDeclarationArray;
begin
if FEnumElements.Empty then
begin
Result := FItems.GetByClass(TDeclaration_EnumElement);

FEnumElements := Result;
end;
if FEnumElements.IsNull then
FEnumElements := FItems.GetByClass(TDeclaration_EnumElement);

Result := FEnumElements;
end;
Expand Down Expand Up @@ -1301,15 +1265,15 @@ function TDeclaration_Method.GetParamVarType(Index: Integer): TDeclaration;

function TDeclaration_Method.GetParamString: String;
begin
if FParamString.Empty then
if FParamString.IsNull then
FParamString := FItems.GetTextOfClassNoCommentsSingleLine(TDeclaration_ParamList);

Result := FParamString;
end;

function TDeclaration_Method.GetResultString: String;
begin
if FResultString.Empty then
if FResultString.IsNull then
FResultString := FItems.GetTextOfClassNoCommentsSingleLine(TDeclaration_MethodResult, ': ');

Result := FResultString;
Expand All @@ -1319,7 +1283,7 @@ function TDeclaration_Method.GetHeader: String;
var
Builder: TSimbaStringBuilder;
begin
if FHeader.Empty then
if FHeader.IsNull then
begin
if isFunc then Builder.Append('function') else
if isProc then Builder.Append('procedure') else
Expand All @@ -1346,16 +1310,14 @@ function TDeclaration_Method.GetParams: TDeclarationArray;
var
Decl: TDeclaration;
begin
Result := [];

if FParams.Empty then
if FParams.IsNull then
begin
FParams := [];

Decl := Items.GetByClassFirst(TDeclaration_ParamList);
if (Decl <> nil) then
for Decl in Decl.Items.GetByClass(TDeclaration_ParamGroup) do
Result.Add(Decl.Items.GetByClass(TDeclaration_Parameter));

FParams := Result;
FParams.Value.Add(Decl.Items.GetByClass(TDeclaration_Parameter));
end;

Result := FParams;
Expand All @@ -1370,7 +1332,7 @@ function TDeclaration_MethodOfType.GetHeader: String;
var
Builder: TSimbaStringBuilder;
begin
if FHeader.Empty then
if FHeader.IsNull then
begin
if isFunc then Builder.Append('function') else
if isProc then Builder.Append('procedure') else
Expand Down Expand Up @@ -1488,7 +1450,7 @@ function TCodeParser.GetHash: String;
Builder: TSimbaStringBuilder;
I: Integer;
begin
if FHash.Empty then
if FHash.IsNull then
begin
with Lexer.SaveDefines() do
Builder.Append(Defines + IntToStr(Stack));
Expand Down Expand Up @@ -2037,7 +1999,7 @@ procedure TCodeParser.Reset;
begin
inherited Reset();

FHash.Empty := True;
FHash.Clear();
FManagedItems.Clear(True);

FRoot.Items.Clear();
Expand Down

0 comments on commit cf30716

Please sign in to comment.