Recent

Author Topic: A comedy of errors: TParser  (Read 2226 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 16652
  • Kallstadt seems a good place to evict Trump to.
A comedy of errors: TParser
« on: January 04, 2025, 07:52:09 pm »
I wonder if I even should attempt to file a bug report against TParser, since it is not meant to be used as a true parser anyway. The class is basically only for dfm/lfm parsing.
So why the shakespearian title?
Well both Delphi and FPC versions of TParser expose the same error and at the same spot.
Problem is:
- Delphi recovers
- Freepascal crashes
Sample code that is supposed to parse its own source:
Code: Pascal  [Select][+][-]
  1. program testrtlparser;
  2. {$ifdef fpc}{$mode objfpc}{$H+}{$endif}
  3. {$apptype console}
  4.  
  5. uses sysutils, classes;
  6.  
  7. var
  8.   fs: TFileStream;
  9.   pp: TParser;
  10.  
  11. begin
  12.   fs := TFileStream.Create('testrtlparser.pas', fmOpenread);// parse myself
  13.   try
  14.     pp := TParser.Create(fs);
  15.     try
  16.       repeat
  17.         case pp.token of
  18.           toSymbol:
  19.             writeln('symbol: ', pp.tokenstring);
  20.           toString:
  21.             writeln('string: ', pp.tokenstring);
  22.           toInteger:
  23.             writeln('integer: ', pp.tokenstring);
  24.           toFloat:
  25.             writeln('float: ', pp.tokenstring);
  26.           toWString:
  27.             writeln('widestring: ', pp.tokenstring);
  28.         else
  29.           // writeln(pp.tokenstring);
  30.         end;
  31.         pp.nextToken;
  32.       until pp.token = toEOF;
  33.     finally
  34.       pp.free;
  35.     end;
  36.   finally
  37.     fs.free;
  38.   end;
  39.   readln;
  40.  
  41. end.
Of course, if I add a try/except I get the same result as Delphi, with the same error in both flavors.
Why all the fuzz?
Simple, I have TParser derivative code (20+ years old) that parses uses clauses.
(It adds skiptoken, basically when it finds "uses" it copies all tokens upto the ';' pushes them into a stringtree and later try to recurse the lot. The real code is quite a lot more lines  :o )

BTW the first error is that if a $ is encountered it is prematurely interpreted as an integer, the second error is that if the next char is within a..f the rest of the token is ignored.
I am not quite sure the clean-rooming of almost two decades ago can explain why the exact same error exists in both flavors.... It is quite easy to fix.
« Last Edit: January 04, 2025, 08:07:00 pm by Thaddy »
But I am sure they don't want the Trumps back...

ASerge

  • Hero Member
  • *****
  • Posts: 2389
Re: A comedy of errors: TParser
« Reply #1 on: January 04, 2025, 11:37:18 pm »
I wonder if I even should attempt to file a bug report against TParser, since it is not meant to be used as a true parser anyway. The class is basically only for dfm/lfm parsing.
It parses the dfm files, which is slightly different from the source code.

Thaddy

  • Hero Member
  • *****
  • Posts: 16652
  • Kallstadt seems a good place to evict Trump to.
Re: A comedy of errors: TParser
« Reply #2 on: January 04, 2025, 11:49:08 pm »
I know that, but dfm/lfm files can also contain $a etc. My old code is a functioning unit parser.
I find it funny that the same bug is in both Delphi and Freepascal.
It is also easy to fix. (I believe I already done that 20 years ago)
I just found that old code. The example above is a reduction of course.

Actually, technically it is not a parser, but a tokenizer.
« Last Edit: January 04, 2025, 11:50:53 pm by Thaddy »
But I am sure they don't want the Trumps back...

 

TinyPortal © 2005-2018