View Issue Details

IDProjectCategoryView StatusLast Update
0036396FPCFCLpublic2019-12-04 14:43
ReporterwpAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityhave not tried
Status closedResolutionfixed 
Product VersionProduct Build 
Target VersionFixed in Version3.3.1 
Summary0036396: Delete a built-in identifier in TFPExpessionParser
DescriptionWhile the BuiltinIdentifiers needed by TFPExpressionParser allow to add functions and variables there is no way to delete an existing identifier. A possible use case, for example, is to replace the trigonometric functions by degree-aware functions as requested in a forum post (https://forum.lazarus.freepascal.org/index.php/topic,19627.msg341477.html#msg341477).

A patch is provided.
Steps To ReproduceThe following code works after application of the patch:

program Project1;

uses
  Math, fpExprPars;

var
  parser: TFPExpressionParser;
  idx: Integer;

  Procedure ExprCos(Var Result : TFPExpressionResult; Const Args : TExprParameterArray);
  begin
    Result.resFloat:=Cos(DegToRad(ArgToFloat(Args[0])));
  end;

begin
  idx := BuiltinIdentifiers.IndexOfIdentifier('cos');
  BuiltinIdentifiers.Delete(idx);
  BuiltinIdentifiers.AddFunction(bcMath, 'cos', 'F', 'F', @ExprCos);

  parser := TFPExpressionParser.Create(nil);
  try
    parser.BuiltIns := [bcMath];
    parser.Expression := 'cos(45.0)';
    WriteLn(parser.Evaluate.ResFloat:0:5);
    parser.Expression := 'cos(90.0)';
    WriteLn(parser.Evaluate.ResFloat:0:5);
    parser.Expression := 'cos(180.0)';
    WriteLn(parser.Evaluate.ResFloat:0:5);
  finally
    parser.Free;
  end;

  WriteLn('Press ENTER to close.');
  ReadLn;

end.
Additional InformationWithout the patch, an ugly cast to a descendant of TBuiltInManager would be needed to access the protected Defs collection.

type
  TMyBuiltinManager = class(TExprBuiltinManager);
var
  idx: Integer;
...
  idx := BuiltinIdentifiers.IndexOfIdentifier('cos');
  TMyBuiltinManager(BuiltinIdentifiers).Defs.Delete(idx);
  BuiltinIdentifiers.AddFunction(bcMath, 'cos', 'F', 'F', @ExprCos);
TagsNo tags attached.
Fixed in Revision43636
FPCOldBugId
FPCTarget3.2.0
Attached Files
  • fpexprpars.pp.patch (1,626 bytes)
    Index: packages/fcl-base/src/fpexprpars.pp
    ===================================================================
    --- packages/fcl-base/src/fpexprpars.pp	(revision 43635)
    +++ packages/fcl-base/src/fpexprpars.pp	(working copy)
    @@ -807,6 +807,7 @@
         Function AddFunction(Const ACategory : TBuiltInCategory; Const AName : ShortString; Const AResultType : Char; Const AParamTypes : String; ACallBack : TFPExprFunctionCallBack) : TFPBuiltInExprIdentifierDef;
         Function AddFunction(Const ACategory : TBuiltInCategory; Const AName : ShortString; Const AResultType : Char; Const AParamTypes : String; ACallBack : TFPExprFunctionEvent) : TFPBuiltInExprIdentifierDef;
         Function AddFunction(Const ACategory : TBuiltInCategory; Const AName : ShortString; Const AResultType : Char; Const AParamTypes : String; ANodeClass : TFPExprFunctionClass) : TFPBuiltInExprIdentifierDef;
    +    Procedure Delete(AIndex: Integer);
         Property IdentifierCount : Integer Read GetCount;
         Property Identifiers[AIndex : Integer] :TFPBuiltInExprIdentifierDef Read GetI;
       end;
    @@ -2299,6 +2300,7 @@
       Result.FNodeType:=ANodeClass;
     end;
     
    +
     { ---------------------------------------------------------------------
       TFPExprIdentifierDef
       ---------------------------------------------------------------------}
    @@ -2667,7 +2669,12 @@
       Result. Category:=ACategory;
     end;
     
    +procedure TExprBuiltInManager.Delete(AIndex: Integer);
    +begin
    +  FDefs.Delete(AIndex);
    +end;
     
    +
     { ---------------------------------------------------------------------
       Various Nodes
       ---------------------------------------------------------------------}
    
    fpexprpars.pp.patch (1,626 bytes)

Activities

wp

2019-12-04 12:17

reporter  

fpexprpars.pp.patch (1,626 bytes)
Index: packages/fcl-base/src/fpexprpars.pp
===================================================================
--- packages/fcl-base/src/fpexprpars.pp	(revision 43635)
+++ packages/fcl-base/src/fpexprpars.pp	(working copy)
@@ -807,6 +807,7 @@
     Function AddFunction(Const ACategory : TBuiltInCategory; Const AName : ShortString; Const AResultType : Char; Const AParamTypes : String; ACallBack : TFPExprFunctionCallBack) : TFPBuiltInExprIdentifierDef;
     Function AddFunction(Const ACategory : TBuiltInCategory; Const AName : ShortString; Const AResultType : Char; Const AParamTypes : String; ACallBack : TFPExprFunctionEvent) : TFPBuiltInExprIdentifierDef;
     Function AddFunction(Const ACategory : TBuiltInCategory; Const AName : ShortString; Const AResultType : Char; Const AParamTypes : String; ANodeClass : TFPExprFunctionClass) : TFPBuiltInExprIdentifierDef;
+    Procedure Delete(AIndex: Integer);
     Property IdentifierCount : Integer Read GetCount;
     Property Identifiers[AIndex : Integer] :TFPBuiltInExprIdentifierDef Read GetI;
   end;
@@ -2299,6 +2300,7 @@
   Result.FNodeType:=ANodeClass;
 end;
 
+
 { ---------------------------------------------------------------------
   TFPExprIdentifierDef
   ---------------------------------------------------------------------}
@@ -2667,7 +2669,12 @@
   Result. Category:=ACategory;
 end;
 
+procedure TExprBuiltInManager.Delete(AIndex: Integer);
+begin
+  FDefs.Delete(AIndex);
+end;
 
+
 { ---------------------------------------------------------------------
   Various Nodes
   ---------------------------------------------------------------------}
fpexprpars.pp.patch (1,626 bytes)

Michael Van Canneyt

2019-12-04 12:31

administrator   ~0119609

Added a Remove() as well, plus testcases for fcl-base/tests

wp

2019-12-04 14:43

reporter   ~0119613

Thanks. That was super-fast!

Issue History

Date Modified Username Field Change
2019-12-04 12:17 wp New Issue
2019-12-04 12:17 wp File Added: fpexprpars.pp.patch
2019-12-04 12:20 Michael Van Canneyt Assigned To => Michael Van Canneyt
2019-12-04 12:20 Michael Van Canneyt Status new => assigned
2019-12-04 12:30 Michael Van Canneyt Project Lazarus => FPC
2019-12-04 12:31 Michael Van Canneyt Status assigned => resolved
2019-12-04 12:31 Michael Van Canneyt Resolution open => fixed
2019-12-04 12:31 Michael Van Canneyt Fixed in Version => 3.3.1
2019-12-04 12:31 Michael Van Canneyt Fixed in Revision => 43636
2019-12-04 12:31 Michael Van Canneyt FPCTarget => 3.2.0
2019-12-04 12:31 Michael Van Canneyt Note Added: 0119609
2019-12-04 14:43 wp Status resolved => closed
2019-12-04 14:43 wp Note Added: 0119613