[fpc-devel] Patch for lexlib
Felipe Monteiro de Carvalho
felipemonteiro.carvalho at gmail.com
Sun Mar 16 11:58:06 CET 2008
Hello,
I am writing scanners using plex, and I found a problem with the
current lexlib. It has a function called yywrap which will close the
input and the output, and there is no way to prevent that. I would
like to parse the same file several times, and simply closing the
file, reopening it and reseting it somehow doesn't work. It soon
crashes. If I prevent yywrap from closing the file (only possible
changing lexlib), then everything works good.
I searched how people solve this in C, and there there is some
pre-processor magic which allows them declare their own yywrap
function. All yywrap declarations I saw in my search would just return
true, so I added a variable called yycloseiowhendone, which is
initially true and when set to false will do what everyone wants when
redeclaring yywrap: Just return true
Please review the following patch. Existing applications shouldn't be
affected by it. If accepted, I'll apply it to trunk, thanks.
Index: h2pas/lexlib.pas
===================================================================
--- h2pas/lexlib.pas (revision 10497)
+++ h2pas/lexlib.pas (working copy)
@@ -45,6 +45,8 @@
yytext : String; (* matched text (should be considered r/o) *)
yyleng : Byte (* length of matched text *)
absolute yytext;
+yycloseiowhendone : Boolean; (* Identifies if yyinput and yyoutput
+ shall be closed when yylex ends *)
(* I/O routines:
@@ -307,7 +309,11 @@
function yywrap : Boolean;
begin
- close(yyinput); close(yyoutput);
+ if yycloseiowhendone then
+ begin
+ close(yyinput);
+ close(yyoutput);
+ end;
yywrap := true;
end(*yywrap*);
@@ -401,6 +407,7 @@
end(*yyclear*);
begin
+ yycloseiowhendone := True;
assign(yyinput, '');
assign(yyoutput, '');
reset(yyinput);
More information about the fpc-devel
mailing list