Recent

Author Topic: TFPExpressionParser and hex constants....  (Read 1429 times)

Prime

  • New Member
  • *
  • Posts: 37
TFPExpressionParser and hex constants....
« on: February 23, 2018, 12:25:29 pm »
Hi All,

I'm trying to use TFPExpressionParser to evaluate expressions that use hexidecimal constants prefixed by $ but this does not seem to be working :

Code condensed to remove other stuff not related to TFPExpressionParser

Code: Pascal  [Select][+][-]
  1. VAR
  2.     FParser     : TFPExpressionParser;
  3.  
  4. BEGIN;
  5.   FParser := TFPExpressionParser.Create(nil);
  6.   TRY
  7.      // TESTVAR has been added with FParser.Identifiers.AddIntegerVariable
  8.      FParser.Expression:='TESTVAR + $20';  // << throws an exception.
  9.  
  10.   FINALLY
  11.     FParser.Free;
  12.   END;
  13. END;
  14.  

The exception I get is "unknown character '$' at position x".

Does anyone have any idea how to make this work?

Cheers.

Phill.


ASerge

  • Hero Member
  • *****
  • Posts: 1557
Re: TFPExpressionParser and hex constants....
« Reply #1 on: February 23, 2018, 01:03:58 pm »
I'm trying to use TFPExpressionParser to evaluate expressions that use hexidecimal constants prefixed by $ but this does not seem to be working:
...
Does anyone have any idea how to make this work?
Copy fpexprpars.pp into your project. Rewrite TFPExpressionScanner.DoNumber so that it understands '$', and also if you want '&' and '%'.

wp

  • Hero Member
  • *****
  • Posts: 7043
Re: TFPExpressionParser and hex constants....
« Reply #2 on: February 23, 2018, 01:12:36 pm »
Normally I would have said: Do what ASerge says and add '$' to the set "Digits' in fpexprpars (immediately after "implementation").

But the parser still fails. The problem is in the line "val(FToken,X,I)" of TFPExpressionScanner.IsNumber, where FToken is the number string with the $, e.g. '$10'.

If I call "val('$10', X, I)" by itself it is converting the hex number without an issue, but inside fpexprpars it fails. What's going on here?

See attached project (it contains already a patched recent fpexprpars).
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

ASerge

  • Hero Member
  • *****
  • Posts: 1557
Re: TFPExpressionParser and hex constants....
« Reply #3 on: February 23, 2018, 01:51:27 pm »
If I call "val('$10', X, I)" by itself it is converting the hex number without an issue, but inside fpexprpars it fails. What's going on here?
Added the corrected procedure Val and also support for octal numbers.
Test.

wp

  • Hero Member
  • *****
  • Posts: 7043
Re: TFPExpressionParser and hex constants....
« Reply #4 on: February 23, 2018, 02:11:44 pm »
Ah, I see, the X is a float, and therefore, the '$' does not make sense.

Please extract a patch out of your fpexprpars and submit it along with the demo as a bug report to bug tracker so that Michael Van Canneyt can add it to the official code?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Prime

  • New Member
  • *
  • Posts: 37
Re: TFPExpressionParser and hex constants....
« Reply #5 on: February 23, 2018, 02:18:44 pm »
If I call "val('$10', X, I)" by itself it is converting the hex number without an issue, but inside fpexprpars it fails. What's going on here?
Added the corrected procedure Val and also support for octal numbers.
Test.

just tested this and it fails if I pass it '$A' complains with 'unexpected character in number.

Cheers.

Phill.

wp

  • Hero Member
  • *****
  • Posts: 7043
Re: TFPExpressionParser and hex constants....
« Reply #6 on: February 23, 2018, 02:37:49 pm »
Of course! The allowed number characters should be extended by 'A'..'F', 'a'..'f', but only if '$' is the first character. Otherwise the number detection conflicts with the detection of variables. I guess that some other procedures of the scanner will have to be changed.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

wp

  • Hero Member
  • *****
  • Posts: 7043
Re: TFPExpressionParser and hex constants....
« Reply #7 on: February 23, 2018, 10:22:12 pm »
Another try... This one handles hexadecimal (including 'A'..'F','a'..'f') octal and binary numbers with their leading identifiers $, & and %, respectively. There seems to be no conflict with variables named 'A'..'F','a'..'f'.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

ASerge

  • Hero Member
  • *****
  • Posts: 1557
Re: TFPExpressionParser and hex constants....
« Reply #8 on: February 23, 2018, 10:23:25 pm »
Next patch.

ASerge

  • Hero Member
  • *****
  • Posts: 1557
Re: TFPExpressionParser and hex constants....
« Reply #9 on: February 24, 2018, 09:46:32 am »
Please extract a patch out of your fpexprpars and submit it along with the demo as a bug report to bug tracker so that Michael Van Canneyt can add it to the official code?
Done.

wp

  • Hero Member
  • *****
  • Posts: 7043
Re: TFPExpressionParser and hex constants....
« Reply #10 on: February 24, 2018, 10:49:39 am »
Thank you. Clever usage of the val() procedure for finding the end of the number. But val() is called twice. Therefore, I added also my own patch because I think that it follows more closely the way the scanner works. Michael will decide.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

 

TinyPortal © 2005-2018