* fixed scannerfiles for macros

+ $I %<environment>%
This commit is contained in:
peter 1998-08-26 15:35:30 +00:00
parent 23225bea44
commit 547dca7111
4 changed files with 375 additions and 247 deletions

View File

@ -31,36 +31,43 @@ unit files;
const
{$ifdef FPC}
maxunits = 1024;
extbufsize = 65535;
InputFileBufSize=32*1024;
{$else}
maxunits = 128;
extbufsize=1024;
InputFileBufSize=1024;
{$endif}
type
pinputfile = ^tinputfile;
tinputfile = object
path,name : pstring; { path and filename }
next : pinputfile; { next file for reading }
path,name : pstring; { path and filename }
next : pinputfile; { next file for reading }
savebufstart, { save fields for scanner }
savebufsize,
savelastlinepos,
saveline_no : longint;
saveinputbuffer,
saveinputpointer : pchar;
f : file; { current file handle }
is_macro,
endoffile, { still bytes left to read }
closed : boolean; { is the file closed }
inputbufsize : longint; { max size of the input buffer }
linebuf : plongint; { line buffer to retrieve lines }
maxlinebuf : longint;
savebufstart, { save fields for scanner }
savebufsize,
savelastlinepos,
saveline_no : longint;
ref_count : longint; { to handle the browser refs }
ref_index : longint;
ref_next : pinputfile;
saveinputbuffer,
saveinputpointer : pchar;
constructor init(const fn:string);
destructor done;
linebuf : plongint; { line buffer to retrieve lines }
maxlinebuf : longint;
ref_count : longint; { to handle the browser refs }
ref_index : longint;
ref_next : pinputfile;
constructor init(const fn:string);
destructor done;
{$ifdef SourceLine}
function getlinestr(l:longint):string;
function getlinestr(l:longint):string;
{$endif SourceLine}
end;
@ -172,6 +179,17 @@ unit files;
name:=stringdup(n+e);
path:=stringdup(p);
next:=nil;
{ file info }
is_macro:=false;
endoffile:=false;
closed:=true;
inputbufsize:=InputFileBufSize;
saveinputbuffer:=nil;
saveinputpointer:=nil;
savebufstart:=0;
savebufsize:=0;
saveline_no:=0;
savelastlinepos:=0;
{ indexing refs }
ref_next:=nil;
ref_count:=0;
@ -647,7 +665,11 @@ unit files;
end.
{
$Log$
Revision 1.40 1998-08-26 10:08:48 peter
Revision 1.41 1998-08-26 15:35:30 peter
* fixed scannerfiles for macros
+ $I %<environment>%
Revision 1.40 1998/08/26 10:08:48 peter
* fixed problem with libprefix at the wrong place
* fixed lib generation with smartlinking and no -CS used

View File

@ -274,10 +274,10 @@ unit pmodules;
Message1(unit_f_cant_compile_unit,current_module^.modulename^)
else
begin
current_scanner^.close;
current_scanner^.tempclose;
compile(current_module^.mainsource^,compile_system);
if (not old_current_module^.compiled) then
current_scanner^.reopen;
current_scanner^.tempopen;
end;
end
else
@ -901,7 +901,11 @@ unit pmodules;
end.
{
$Log$
Revision 1.43 1998-08-26 10:08:47 peter
Revision 1.44 1998-08-26 15:35:33 peter
* fixed scannerfiles for macros
+ $I %<environment>%
Revision 1.43 1998/08/26 10:08:47 peter
* fixed problem with libprefix at the wrong place
* fixed lib generation with smartlinking and no -CS used

View File

@ -500,22 +500,58 @@ const
hs:=current_scanner^.readcomment;
while (hs<>'') and (hs[length(hs)]=' ') do
dec(byte(hs[0]));
hs:=FixFileName(hs);
fsplit(hs,path,name,ext);
{ first look in the path of _d then currentmodule }
path:=search(name+ext,path+';'+current_scanner^.inputfile^.path^+';'+includesearchpath,found);
{ shutdown current file }
current_scanner^.close;
{ load new file }
hp:=new(pinputfile,init(path+name+ext));
current_scanner^.addfile(hp);
if not current_scanner^.open then
Message1(scan_f_cannot_open_includefile,hs);
Message1(scan_u_start_include_file,current_scanner^.inputfile^.path^+current_scanner^.inputfile^.name^);
current_scanner^.reload;
{ register for refs }
current_module^.sourcefiles.register_file(hp);
current_module^.current_index:=hp^.ref_index;
if hs='' then
exit;
if (hs[1]='%') then
begin
{ save old }
path:=hs;
{ remove %'s }
Delete(hs,1,1);
if hs[length(hs)]='%' then
Delete(hs,length(hs),1);
{ first check for internal macros }
if hs='TIME' then
hs:=gettimestr
else
if hs='DATE' then
hs:=getdatestr
else
if hs='FPCVERSION' then
hs:=version_string
else
if hs='FPCTARGET' then
hs:=target_string
else
hs:=getenv(hs);
if hs='' then
Comment(V_Warning,'Include environment '+path+' not found in environment')
else
begin
{ make it a stringconst }
hs:=''''+hs+'''';
current_scanner^.insertmacro(@hs[1],length(hs));
end;
end
else
begin
hs:=FixFileName(hs);
fsplit(hs,path,name,ext);
{ first look in the path of _d then currentmodule }
path:=search(name+ext,path+';'+current_scanner^.inputfile^.path^+';'+includesearchpath,found);
{ shutdown current file }
current_scanner^.tempclose;
{ load new file }
hp:=new(pinputfile,init(path+name+ext));
current_scanner^.addfile(hp);
if not current_scanner^.open then
Message1(scan_f_cannot_open_includefile,hs);
Message1(scan_u_start_include_file,current_scanner^.inputfile^.path^+current_scanner^.inputfile^.name^);
current_scanner^.reload;
{ register for refs }
current_module^.sourcefiles.register_file(hp);
current_module^.current_index:=hp^.ref_index;
end;
end;
@ -732,7 +768,11 @@ const
{
$Log$
Revision 1.22 1998-08-19 14:57:50 peter
Revision 1.23 1998-08-26 15:35:34 peter
* fixed scannerfiles for macros
+ $I %<environment>%
Revision 1.22 1998/08/19 14:57:50 peter
* small fix for aktfilepos
Revision 1.20 1998/08/18 15:11:52 peter

View File

@ -33,11 +33,9 @@ unit scanner;
const
{$ifdef TP}
maxmacrolen=1024;
InputFileBufSize=1024;
linebufincrease=64;
{$else}
maxmacrolen=16*1024;
InputFileBufSize=32*1024;
linebufincrease=512;
{$endif}
@ -148,26 +146,18 @@ unit scanner;
tscannerfile = object
inputfile : pinputfile; { current inputfile list }
f : file; { current file handle }
filenotatend, { still bytes left to read }
closed : boolean; { is the file closed }
inputbufsize : longint; { max size of the input buffer }
{ these fields are called save* in inputfile, and are here
for speed reasons (PFV) }
bufstart,
bufsize,
line_no,
lastlinepos : longint;
inputbuffer,
inputpointer : pchar;
bufstart,
bufsize : longint;
line_no,
lasttokenpos,
lastlinepos : longint;
lasttokenpos : longint;
lasttoken : ttoken;
maxlinebuf : longint;
linebuf : plongint;
do_special, { 1=point after nr, 2=caret after id }
comment_level,
yylexcount : longint;
@ -179,7 +169,8 @@ unit scanner;
{ File buffer things }
function open:boolean;
procedure close;
function reopen:boolean;
procedure tempclose;
function tempopen:boolean;
procedure seekbuf(fpos:longint);
procedure readbuf;
procedure saveinputfile;
@ -188,6 +179,7 @@ unit scanner;
procedure addfile(hp:pinputfile);
procedure reload;
procedure setbuf(p:pchar;l:longint);
procedure insertmacro(p:pchar;len:longint);
{ Scanner things }
procedure gettokenpos;
procedure inc_comment_level;
@ -314,25 +306,14 @@ implementation
inputfile:=new(pinputfile,init(fn));
current_module^.sourcefiles.register_file(inputfile);
current_module^.current_index:=inputfile^.ref_index;
{ load inputfile values }
restoreinputfile;
{ reset scanner }
preprocstack:=nil;
comment_level:=0;
do_special:=0;
block_type:=bt_general;
{ reset buf }
closed:=true;
filenotatend:=true;
inputbufsize:=InputFileBufSize;
inputbuffer:=nil;
inputpointer:=nil;
bufstart:=0;
bufsize:=0;
{ line }
line_no:=0;
lastlinepos:=0;
lasttokenpos:=0;
linebuf:=nil;
maxlinebuf:=0;
{ load block }
if not open then
Message1(scan_f_cannot_open_input,fn);
@ -344,18 +325,21 @@ implementation
begin
checkpreprocstack;
{ close file }
if not closed then
if not inputfile^.closed then
close;
end;
end;
procedure tscannerfile.seekbuf(fpos:longint);
begin
if closed then
exit;
seek(f,fpos);
bufstart:=fpos;
bufsize:=0;
with inputfile^ do
begin
if closed then
exit;
seek(f,fpos);
bufstart:=fpos;
bufsize:=0;
end;
end;
@ -365,17 +349,22 @@ implementation
w : word;
{$endif}
begin
if closed then
exit;
inc(bufstart,bufsize);
{$ifdef TP}
blockread(f,inputbuffer^,inputbufsize-1,w);
bufsize:=w;
{$else}
blockread(f,inputbuffer^,inputbufsize-1,bufsize);
{$endif}
inputbuffer[bufsize]:=#0;
Filenotatend:=(bufsize=inputbufsize-1);
with inputfile^ do
begin
if is_macro then
endoffile:=true;
if closed then
exit;
inc(bufstart,bufsize);
{$ifdef TP}
blockread(f,inputbuffer^,inputbufsize-1,w);
bufsize:=w;
{$else}
blockread(f,inputbuffer^,inputbufsize-1,bufsize);
{$endif}
inputbuffer[bufsize]:=#0;
endoffile:=not(bufsize=inputbufsize-1);
end;
end;
@ -383,30 +372,33 @@ implementation
var
ofm : byte;
begin
open:=false;
if not closed then
Close;
ofm:=filemode;
filemode:=0;
Assign(f,inputfile^.path^+inputfile^.name^);
{$I-}
reset(f,1);
{$I+}
filemode:=ofm;
if ioresult<>0 then
exit;
{ file }
closed:=false;
filenotatend:=true;
Getmem(inputbuffer,inputbufsize);
inputpointer:=inputbuffer;
bufstart:=0;
bufsize:=0;
{ line }
line_no:=0;
lastlinepos:=0;
lasttokenpos:=0;
open:=true;
with inputfile^ do
begin
open:=false;
if not closed then
Close;
ofm:=filemode;
filemode:=0;
Assign(f,inputfile^.path^+inputfile^.name^);
{$I-}
reset(f,1);
{$I+}
filemode:=ofm;
if ioresult<>0 then
exit;
{ file }
endoffile:=false;
closed:=false;
Getmem(inputbuffer,inputbufsize);
inputpointer:=inputbuffer;
bufstart:=0;
bufsize:=0;
{ line }
line_no:=0;
lastlinepos:=0;
lasttokenpos:=0;
open:=true;
end;
end;
@ -414,46 +406,89 @@ implementation
var
i : word;
begin
inc(bufstart,inputpointer-inputbuffer);
if not closed then
with inputfile^ do
begin
{$I-}
system.close(f);
{$I+}
i:=ioresult;
Freemem(inputbuffer,InputFileBufSize);
inputbuffer:=nil;
inputpointer:=nil;
closed:=true;
if is_macro then
begin
Freemem(inputbuffer,InputFileBufSize);
is_macro:=false;
inputbuffer:=nil;
inputpointer:=nil;
closed:=true;
exit;
end;
if not closed then
begin
{$I-}
system.close(f);
{$I+}
i:=ioresult;
Freemem(inputbuffer,InputFileBufSize);
inputbuffer:=nil;
inputpointer:=nil;
closed:=true;
end;
end;
end;
function tscannerfile.reopen:boolean;
procedure tscannerfile.tempclose;
var
i : word;
begin
with inputfile^ do
begin
inc(bufstart,inputpointer-inputbuffer);
if is_macro then
exit;
if not closed then
begin
{$I-}
system.close(f);
{$I+}
i:=ioresult;
Freemem(inputbuffer,InputFileBufSize);
inputbuffer:=nil;
inputpointer:=nil;
closed:=true;
end;
end;
end;
function tscannerfile.tempopen:boolean;
var
ofm : byte;
begin
reopen:=false;
if not closed then
exit;
ofm:=filemode;
filemode:=0;
Assign(f,inputfile^.path^+inputfile^.name^);
{$I-}
reset(f,1);
{$I+}
filemode:=ofm;
if ioresult<>0 then
exit;
closed:=false;
{ get new mem }
Getmem(inputbuffer,inputbufsize);
inputpointer:=inputbuffer;
{ restore state }
seek(f,BufStart);
bufsize:=0;
readbuf;
reopen:=true;
with inputfile^ do
begin
tempopen:=false;
if is_macro then
begin
tempopen:=true;
exit;
end;
if not closed then
exit;
ofm:=filemode;
filemode:=0;
Assign(f,inputfile^.path^+inputfile^.name^);
{$I-}
reset(f,1);
{$I+}
filemode:=ofm;
if ioresult<>0 then
exit;
closed:=false;
{ get new mem }
Getmem(inputbuffer,inputbufsize);
inputpointer:=inputbuffer;
{ restore state }
seek(f,BufStart);
bufsize:=0;
readbuf;
tempopen:=true;
end;
end;
@ -461,12 +496,10 @@ implementation
begin
inputfile^.savebufstart:=bufstart;
inputfile^.savebufsize:=bufsize;
inputfile^.savelastlinepos:=lastlinepos;
inputfile^.saveline_no:=line_no;
inputfile^.saveinputbuffer:=inputbuffer;
inputfile^.saveinputpointer:=inputpointer;
inputfile^.linebuf:=linebuf;
inputfile^.maxlinebuf:=maxlinebuf;
inputfile^.savelastlinepos:=lastlinepos;
inputfile^.saveline_no:=line_no;
end;
@ -478,8 +511,6 @@ implementation
line_no:=inputfile^.saveline_no;
inputbuffer:=inputfile^.saveinputbuffer;
inputpointer:=inputfile^.saveinputpointer;
linebuf:=inputfile^.linebuf;
maxlinebuf:=inputfile^.maxlinebuf;
end;
@ -506,55 +537,94 @@ implementation
procedure tscannerfile.reload;
begin
{ safety check }
if closed then
exit;
repeat
{ still more to read?, then change the #0 to a space so its seen
as a seperator }
if (bufsize>0) and (inputpointer-inputbuffer<bufsize) then
begin
c:=' ';
inc(longint(inputpointer));
exit;
end;
{ can we read more from this file ? }
if filenotatend then
begin
readbuf;
if line_no=0 then
line_no:=1;
inputpointer:=inputbuffer;
end
else
begin
close;
{ no next module, than EOF }
if not assigned(inputfile^.next) then
with inputfile^ do
begin
repeat
{ still more to read?, then change the #0 to a space so its seen
as a seperator }
if (bufsize>0) and (inputpointer-inputbuffer<bufsize) then
begin
c:=#26;
c:=' ';
inc(longint(inputpointer));
exit;
end;
{ load next file and reopen it }
nextfile;
reopen;
{ status }
Message1(scan_d_back_in,inputfile^.name^);
{ load some current_module fields }
current_module^.current_index:=inputfile^.ref_index;
end;
{ load next char }
c:=inputpointer^;
inc(longint(inputpointer));
until c<>#0; { if also end, then reload again }
{ can we read more from this file ? }
if not endoffile then
begin
readbuf;
if line_no=0 then
line_no:=1;
inputpointer:=inputbuffer;
end
else
begin
close;
{ no next module, than EOF }
if not assigned(inputfile^.next) then
begin
c:=#26;
exit;
end;
{ load next file and reopen it }
nextfile;
tempopen;
{ status }
Message1(scan_d_back_in,inputfile^.name^);
{ load some current_module fields }
current_module^.current_index:=inputfile^.ref_index;
end;
{ load next char }
c:=inputpointer^;
inc(longint(inputpointer));
until c<>#0; { if also end, then reload again }
end;
end;
procedure tscannerfile.setbuf(p:pchar;l:longint);
begin
inputbuffer:=p;
inputbufsize:=l;
inputpointer:=inputbuffer;
with inputfile^ do
begin
inputbufsize:=l;
inputbuffer:=p;
inputpointer:=p;
end;
end;
procedure tscannerfile.insertmacro(p:pchar;len:longint);
{ load the values of tokenpos and lasttokenpos }
var
macbuf : pchar;
hp : pinputfile;
begin
{ save old postion }
dec(longint(inputpointer));
current_scanner^.tempclose;
{ create macro 'file' }
hp:=new(pinputfile,init('Macro'));
addfile(hp);
getmem(macbuf,len+1);
setbuf(macbuf,len+1);
{ fill buffer }
with inputfile^ do
begin
move(p^,inputbuffer^,len);
inputbuffer[len]:=#0;
{ reset }
inputpointer:=inputbuffer;
bufstart:=0;
bufsize:=len;
line_no:=0;
lastlinepos:=0;
lasttokenpos:=0;
is_macro:=true;
endoffile:=true;
closed:=true;
{ load new c }
c:=inputpointer^;
inc(longint(inputpointer));
end;
end;
@ -602,50 +672,52 @@ implementation
{$endif SourceLine}
oldtokenpos,oldaktfilepos : tfileposinfo;
begin
if (byte(inputpointer^)=0) and
filenotatend then
begin
cur:=c;
reload;
if byte(cur)+byte(c)<>23 then
dec(longint(inputpointer));
end
else
with inputfile^ do
begin
{ Fix linebreak to be only newline (=#10) for all types of linebreaks }
if (byte(inputpointer^)+byte(c)=23) then
inc(longint(inputpointer));
end;
c:=newline;
{ increase line counters }
lastlinepos:=bufstart+(inputpointer-inputbuffer);
inc(line_no);
{ update linebuffer }
{$ifdef SourceLine}
if line_no>maxlinebuf then
begin
{ create new linebuf and move old info }
getmem(hp,maxlinebuf+linebufincrease);
if assigned(linebuf) then
if (byte(inputpointer^)=0) and not(endoffile) then
begin
move(linebuf^,hp^,maxlinebuf shl 2);
freemem(linebuf,maxlinebuf);
cur:=c;
reload;
if byte(cur)+byte(c)<>23 then
dec(longint(inputpointer));
end
else
begin
{ Fix linebreak to be only newline (=#10) for all types of linebreaks }
if (byte(inputpointer^)+byte(c)=23) then
inc(longint(inputpointer));
end;
{ set new linebuf }
linebuf:=hp;
inc(maxlinebuf,linebufincrease);
c:=newline;
{ increase line counters }
lastlinepos:=bufstart+(inputpointer-inputbuffer);
inc(line_no);
{ update linebuffer }
{$ifdef SourceLine}
if line_no>maxlinebuf then
begin
{ create new linebuf and move old info }
getmem(hp,maxlinebuf+linebufincrease);
if assigned(linebuf) then
begin
move(linebuf^,hp^,maxlinebuf shl 2);
freemem(linebuf,maxlinebuf);
end;
{ set new linebuf }
linebuf:=hp;
inc(maxlinebuf,linebufincrease);
end;
plongint(longint(linebuf)+line_no*2)^:=lastlinepos;
{$endif SourceLine}
{ update for status and call the show status routine,
but don't touch aktfilepos ! }
oldaktfilepos:=aktfilepos;
oldtokenpos:=tokenpos;
gettokenpos; { update for v_status }
inc(status.compiledlines);
ShowStatus;
aktfilepos:=oldaktfilepos;
tokenpos:=oldtokenpos;
end;
plongint(longint(linebuf)+line_no*2)^:=lastlinepos;
{$endif SourceLine}
{ update for status and call the show status routine,
but don't touch aktfilepos ! }
oldaktfilepos:=aktfilepos;
oldtokenpos:=tokenpos;
gettokenpos; { update for v_status }
inc(status.compiledlines);
ShowStatus;
aktfilepos:=oldaktfilepos;
tokenpos:=oldtokenpos;
end;
@ -1004,8 +1076,6 @@ implementation
code : word;
l : longint;
mac : pmacrosym;
hp : pinputfile;
macbuf : pchar;
asciinr : string[3];
label
exit_label;
@ -1064,19 +1134,7 @@ implementation
mac:=pmacrosym(macros^.search(pattern));
if assigned(mac) and (assigned(mac^.buftext)) then
begin
{ don't forget the last char }
dec(longint(inputpointer));
hp:=new(pinputfile,init('Macro '+pattern));
addfile(hp);
getmem(macbuf,mac^.buflen+1);
setbuf(macbuf,mac^.buflen+1);
{ copy text }
move(mac^.buftext^,inputbuffer^,mac^.buflen);
{ put end sign }
inputbuffer[mac^.buflen+1]:=#0;
{ load c }
c:=inputbuffer^;
inputpointer:=inputbuffer+1;
insertmacro(mac^.buftext,mac^.buflen);
{ handle empty macros }
if c=#0 then
reload;
@ -1561,7 +1619,11 @@ exit_label:
end.
{
$Log$
Revision 1.44 1998-08-20 16:09:55 pierre
Revision 1.45 1998-08-26 15:35:35 peter
* fixed scannerfiles for macros
+ $I %<environment>%
Revision 1.44 1998/08/20 16:09:55 pierre
* tokenpos has to be restored also after
printstatus