scanner.pas 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  1. {
  2. $Id$
  3. Copyright (c) 1993,97 by Florian Klaempfl
  4. This unit implements the scanner part and handling of the switches
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$ifdef tp}
  19. {$F+,N+,E+,R-}
  20. {$endif}
  21. unit scanner;
  22. interface
  23. uses
  24. cobjects,globals,verbose,files;
  25. const
  26. {$ifdef TP}
  27. maxmacrolen=1024;
  28. InputFileBufSize=75;
  29. {$else}
  30. maxmacrolen=16*1024;
  31. InputFileBufSize=32*1024;
  32. {$endif}
  33. id_len = 14;
  34. Newline = #10;
  35. type
  36. ident = string[id_len];
  37. const
  38. max_keywords = 70;
  39. anz_keywords : longint = max_keywords;
  40. { the following keywords are no keywords in TP, they
  41. are internal procedures
  42. CONTINUE, DISPOSE, EXIT, FAIL, FALSE, NEW, SELF
  43. TRUE
  44. }
  45. { INLINE is a keyword in TP, but only an modifier in FPC }
  46. keyword : array[1..max_keywords] of ident = (
  47. { 'ABSOLUTE',}
  48. 'AND',
  49. 'ARRAY','AS','ASM',
  50. { 'ASSEMBLER',}
  51. 'BEGIN',
  52. 'CASE','CLASS',
  53. 'CONST','CONSTRUCTOR',
  54. 'DESTRUCTOR','DISPOSE','DIV','DO','DOWNTO','ELSE','END',
  55. 'EXCEPT',
  56. 'EXIT',
  57. { 'EXPORT',}
  58. 'EXPORTS',
  59. { 'EXTERNAL',}
  60. 'FAIL','FALSE',
  61. { 'FAR',}
  62. 'FILE','FINALIZATION','FINALLY','FOR',
  63. { 'FORWARD',}
  64. 'FUNCTION','GOTO','IF','IMPLEMENTATION','IN',
  65. 'INHERITED','INITIALIZATION',
  66. { 'INLINE',} {INLINE is a reserved word in TP. Why?}
  67. 'INTERFACE',
  68. { 'INTERRUPT',}
  69. 'IS',
  70. 'LABEL','LIBRARY','MOD',
  71. { 'NEAR',}
  72. 'NEW','NIL','NOT','OBJECT',
  73. 'OF','ON','OPERATOR','OR','OTHERWISE','PACKED',
  74. 'PROCEDURE','PROGRAM','PROPERTY',
  75. 'RAISE','RECORD','REPEAT','SELF',
  76. 'SET','SHL','SHR','STRING','THEN','TO',
  77. 'TRUE','TRY','TYPE','UNIT','UNTIL',
  78. 'USES','VAR',
  79. { 'VIRTUAL',}
  80. 'WHILE','WITH','XOR');
  81. keyword_token : array[1..max_keywords] of ttoken = (
  82. { _ABSOLUTE,}
  83. _AND,
  84. _ARRAY,_AS,_ASM,
  85. { _ASSEMBLER,}
  86. _BEGIN,
  87. _CASE,_CLASS,
  88. _CONST,_CONSTRUCTOR,
  89. _DESTRUCTOR,_DISPOSE,_DIV,_DO,_DOWNTO,
  90. _ELSE,_END,_EXCEPT,
  91. _EXIT,
  92. { _EXPORT,}
  93. _EXPORTS,
  94. { _EXTERNAL,}
  95. _FAIL,_FALSE,
  96. { _FAR,}
  97. _FILE,_FINALIZATION,_FINALLY,_FOR,
  98. { _FORWARD,}
  99. _FUNCTION,_GOTO,_IF,_IMPLEMENTATION,_IN,
  100. _INHERITED,_INITIALIZATION,
  101. { _INLINE,}
  102. _INTERFACE,
  103. { _INTERRUPT,}
  104. _IS,
  105. _LABEL,_LIBRARY,_MOD,
  106. { _NEAR,}
  107. _NEW,_NIL,_NOT,_OBJECT,
  108. _OF,_ON,_OPERATOR,_OR,_OTHERWISE,_PACKED,
  109. _PROCEDURE,_PROGRAM,_PROPERTY,
  110. _RAISE,_RECORD,_REPEAT,_SELF,
  111. _SET,_SHL,_SHR,_STRING,_THEN,_TO,
  112. _TRUE,_TRY,_TYPE,_UNIT,_UNTIL,
  113. _USES,_VAR,
  114. { _VIRTUAL,}
  115. _WHILE,_WITH,_XOR);
  116. type
  117. pmacrobuffer = ^tmacrobuffer;
  118. tmacrobuffer = array[0..maxmacrolen-1] of char;
  119. ppreprocstack = ^tpreprocstack;
  120. tpreprocstack = object
  121. accept : boolean;
  122. next : ppreprocstack;
  123. name : stringid;
  124. line_nb : longint;
  125. constructor init(a:boolean;n:ppreprocstack);
  126. destructor done;
  127. end;
  128. {$ifdef NEWINPUT}
  129. pscannerfile = ^tscannerfile;
  130. tscannerfile = object
  131. inputfile : pinputfile; { current inputfile list }
  132. f : file; { current file handle }
  133. filenotatend, { still bytes left to read }
  134. closed : boolean; { is the file closed }
  135. inputbufsize : longint; { max size of the input buffer }
  136. inputbuffer,
  137. inputpointer : pchar;
  138. bufstart,
  139. bufidx,
  140. bufsize : longint;
  141. line_no,
  142. lasttokenpos,
  143. lastlinepos : longint;
  144. s_point : boolean;
  145. comment_level,
  146. yylexcount : longint;
  147. lastasmgetchar : char;
  148. preprocstack : ppreprocstack;
  149. constructor init(const fn:string);
  150. destructor done;
  151. { File buffer things }
  152. function open:boolean;
  153. procedure close;
  154. function reopen:boolean;
  155. procedure readbuf;
  156. procedure saveinputfile;
  157. procedure restoreinputfile;
  158. procedure nextfile;
  159. procedure addfile(hp:pinputfile);
  160. procedure reload;
  161. { function fixbuf:boolean; }
  162. procedure setbuf(p:pchar;l:longint);
  163. { function setbufidx(idx:longint):longint;
  164. function setlinebreak(idx:longint):longint; }
  165. { Scanner things }
  166. procedure gettokenpos;
  167. procedure inc_comment_level;
  168. procedure dec_comment_level;
  169. procedure poppreprocstack;
  170. procedure addpreprocstack(a:boolean;const s:string;w:tmsgconst);
  171. procedure elsepreprocstack;
  172. procedure linebreak;
  173. procedure readchar;
  174. procedure readstring;
  175. procedure readnumber;
  176. function readid:string;
  177. function readval:longint;
  178. function readcomment:string;
  179. procedure skipspace;
  180. procedure skipuntildirective;
  181. procedure skipcomment;
  182. procedure skipdelphicomment;
  183. procedure skipoldtpcomment;
  184. function yylex:ttoken;
  185. function readpreproc:ttoken;
  186. function asmgetchar:char;
  187. end;
  188. {$endif NEWINPUT}
  189. var
  190. c : char;
  191. orgpattern,
  192. pattern : string;
  193. {$ifdef NEWINPUT}
  194. current_scanner : pscannerfile;
  195. {$else}
  196. currlinepos,
  197. lastlinepos,
  198. lasttokenpos,
  199. inputbuffer,
  200. inputpointer : pchar;
  201. s_point : boolean;
  202. comment_level,
  203. yylexcount,
  204. macropos : longint;
  205. lastasmgetchar : char;
  206. preprocstack : ppreprocstack;
  207. {$endif NEWINPUT}
  208. {$ifndef NEWINPUT}
  209. procedure poppreprocstack;
  210. procedure addpreprocstack(a:boolean;const s:string;w:tmsgconst);
  211. procedure elsepreprocstack;
  212. procedure gettokenpos;
  213. function yylex : ttoken;
  214. function asmgetchar : char;
  215. { column position of last token }
  216. function get_current_col : longint;
  217. { column position of file }
  218. function get_file_col : longint;
  219. procedure get_cur_file_pos(var fileinfo : tfileposinfo);
  220. procedure set_cur_file_pos(const fileinfo : tfileposinfo);
  221. procedure InitScanner(const fn: string);
  222. procedure DoneScanner(testendif:boolean);
  223. {$endif}
  224. { changes to keywords to be tp compatible }
  225. procedure change_to_tp_keywords;
  226. implementation
  227. uses
  228. dos,systems,symtable,switches;
  229. {*****************************************************************************
  230. Helper routines
  231. *****************************************************************************}
  232. function is_keyword(var token : ttoken) : boolean;
  233. var
  234. high,low,mid : longint;
  235. begin
  236. low:=1;
  237. high:=anz_keywords;
  238. while low<high do
  239. begin
  240. mid:=(high+low+1) shr 1;
  241. if pattern<keyword[mid] then
  242. high:=mid-1
  243. else
  244. low:=mid;
  245. end;
  246. if pattern=keyword[high] then
  247. begin
  248. token:=keyword_token[high];
  249. is_keyword:=true;
  250. end
  251. else
  252. is_keyword:=false;
  253. end;
  254. procedure remove_keyword(const s : string);
  255. var
  256. i,j : longint;
  257. begin
  258. for i:=1 to anz_keywords do
  259. begin
  260. if keyword[i]=s then
  261. begin
  262. for j:=i to anz_keywords-1 do
  263. begin
  264. keyword[j]:=keyword[j+1];
  265. keyword_token[j]:=keyword_token[j+1];
  266. end;
  267. dec(anz_keywords);
  268. break;
  269. end;
  270. end;
  271. end;
  272. procedure change_to_tp_keywords;
  273. const
  274. non_tp : array[0..14] of string[id_len] = (
  275. 'AS','CLASS','EXCEPT','FINALLY','INITIALIZATION','IS',
  276. 'ON','OPERATOR','OTHERWISE','PROPERTY','RAISE','TRY',
  277. 'EXPORTS','LIBRARY','FINALIZATION');
  278. var
  279. i : longint;
  280. begin
  281. for i:=0 to 13 do
  282. remove_keyword(non_tp[i]);
  283. end;
  284. {$ifndef NEWINPUT}
  285. const
  286. current_column : longint = 1;
  287. function get_current_col : longint;
  288. begin
  289. get_current_col:=current_column;
  290. end;
  291. function get_file_col : longint;
  292. begin
  293. get_file_col:=lasttokenpos-lastlinepos;
  294. end;
  295. procedure inc_comment_level;
  296. begin
  297. inc(comment_level);
  298. if (comment_level>1) then
  299. Message1(scan_w_comment_level,tostr(comment_level));
  300. end;
  301. procedure dec_comment_level;
  302. begin
  303. if cs_tp_compatible in aktswitches then
  304. comment_level:=0
  305. else
  306. dec(comment_level);
  307. end;
  308. {$endif NEWINPUT}
  309. {*****************************************************************************
  310. TPreProcStack
  311. *****************************************************************************}
  312. constructor tpreprocstack.init(a:boolean;n:ppreprocstack);
  313. begin
  314. accept:=a;
  315. next:=n;
  316. end;
  317. destructor tpreprocstack.done;
  318. begin
  319. end;
  320. {$ifdef NEWINPUT}
  321. {****************************************************************************
  322. TSCANNERFILE
  323. ****************************************************************************}
  324. constructor tscannerfile.init(const fn:string);
  325. begin
  326. inputfile:=new(pinputfile,init(fn));
  327. current_module^.sourcefiles.register_file(inputfile);
  328. current_module^.current_index:=inputfile^.ref_index;
  329. { reset scanner }
  330. preprocstack:=nil;
  331. comment_level:=0;
  332. s_point:=false;
  333. block_type:=bt_general;
  334. { reset buf }
  335. closed:=true;
  336. filenotatend:=true;
  337. inputbufsize:=InputFileBufSize;
  338. inputbuffer:=nil;
  339. inputpointer:=nil;
  340. bufstart:=0;
  341. bufsize:=0;
  342. { line }
  343. line_no:=0;
  344. lastlinepos:=0;
  345. lasttokenpos:=0;
  346. { load block }
  347. if not open then
  348. Message(scan_f_cannot_open_input);
  349. { status.currentsource:=inputfile^.name^; }
  350. reload;
  351. end;
  352. destructor tscannerfile.done;
  353. begin
  354. { check for missing ifdefs }
  355. while assigned(preprocstack) do
  356. begin
  357. Message3(scan_e_endif_expected,'$IF(N)(DEF)',preprocstack^.name,tostr(preprocstack^.line_nb));
  358. poppreprocstack;
  359. end;
  360. { close file }
  361. if not closed then
  362. close;
  363. end;
  364. procedure tscannerfile.readbuf;
  365. {$ifdef TP}
  366. var
  367. w : word;
  368. {$endif}
  369. begin
  370. if closed then
  371. exit;
  372. inc(bufstart,bufsize);
  373. {$ifdef TP}
  374. blockread(f,inputbuffer^,inputbufsize-1,w);
  375. bufsize:=w;
  376. {$else}
  377. blockread(f,inputbuffer^,inputbufsize-1,bufsize);
  378. {$endif}
  379. inputbuffer[bufsize]:=#0;
  380. Filenotatend:=(bufsize=inputbufsize-1);
  381. end;
  382. function tscannerfile.open:boolean;
  383. var
  384. ofm : byte;
  385. begin
  386. open:=false;
  387. if not closed then
  388. exit;
  389. ofm:=filemode;
  390. filemode:=0;
  391. Assign(f,inputfile^.path^+inputfile^.name^);
  392. {$I-}
  393. reset(f,1);
  394. {$I+}
  395. filemode:=ofm;
  396. if ioresult<>0 then
  397. exit;
  398. { file }
  399. closed:=false;
  400. filenotatend:=true;
  401. Getmem(inputbuffer,inputbufsize);
  402. inputpointer:=inputbuffer;
  403. bufstart:=0;
  404. bufsize:=0;
  405. { line }
  406. line_no:=0;
  407. lastlinepos:=0;
  408. lasttokenpos:=0;
  409. open:=true;
  410. end;
  411. procedure tscannerfile.close;
  412. var
  413. i : word;
  414. begin
  415. inc(bufstart,inputpointer-inputbuffer);
  416. if not closed then
  417. begin
  418. {$I-}
  419. system.close(f);
  420. {$I+}
  421. i:=ioresult;
  422. Freemem(inputbuffer,InputFileBufSize);
  423. inputbuffer:=nil;
  424. inputpointer:=nil;
  425. closed:=true;
  426. end;
  427. end;
  428. function tscannerfile.reopen:boolean;
  429. var
  430. ofm : byte;
  431. begin
  432. reopen:=false;
  433. if not closed then
  434. exit;
  435. ofm:=filemode;
  436. filemode:=0;
  437. Assign(f,inputfile^.path^+inputfile^.name^);
  438. {$I-}
  439. reset(f,1);
  440. {$I+}
  441. filemode:=ofm;
  442. if ioresult<>0 then
  443. exit;
  444. closed:=false;
  445. { get new mem }
  446. Getmem(inputbuffer,inputbufsize);
  447. inputpointer:=inputbuffer;
  448. { restore state }
  449. seek(f,BufStart);
  450. bufsize:=0;
  451. readbuf;
  452. reopen:=true;
  453. end;
  454. procedure tscannerfile.saveinputfile;
  455. begin
  456. inputfile^.savebufstart:=bufstart;
  457. inputfile^.savebufsize:=bufsize;
  458. inputfile^.savelastlinepos:=lastlinepos;
  459. inputfile^.saveline_no:=line_no;
  460. inputfile^.saveinputbuffer:=inputbuffer;
  461. inputfile^.saveinputpointer:=inputpointer;
  462. end;
  463. procedure tscannerfile.restoreinputfile;
  464. begin
  465. bufstart:=inputfile^.savebufstart;
  466. bufsize:=inputfile^.savebufsize;
  467. lastlinepos:=inputfile^.savelastlinepos;
  468. line_no:=inputfile^.saveline_no;
  469. inputbuffer:=inputfile^.saveinputbuffer;
  470. inputpointer:=inputfile^.saveinputpointer;
  471. end;
  472. procedure tscannerfile.nextfile;
  473. begin
  474. if assigned(inputfile^.next) then
  475. begin
  476. inputfile:=inputfile^.next;
  477. restoreinputfile;
  478. end;
  479. end;
  480. procedure tscannerfile.addfile(hp:pinputfile);
  481. begin
  482. saveinputfile;
  483. { add to list }
  484. hp^.next:=inputfile;
  485. inputfile:=hp;
  486. { load new inputfile }
  487. restoreinputfile;
  488. end;
  489. procedure tscannerfile.reload;
  490. begin
  491. { still more to read, then we have an illegal char }
  492. if (inputpointer-inputbuffer)<bufsize then
  493. Message(scan_f_illegal_char);
  494. { safety check }
  495. if closed then
  496. exit;
  497. { can we read more from this file ? }
  498. if filenotatend then
  499. begin
  500. readbuf;
  501. { fixbuf; }
  502. if line_no=0 then
  503. line_no:=1;
  504. inputpointer:=inputbuffer;
  505. end
  506. else
  507. begin
  508. close;
  509. { no next module, than EOF }
  510. if not assigned(inputfile^.next) then
  511. begin
  512. c:=#26;
  513. exit;
  514. end;
  515. { load next file and reopen it }
  516. nextfile;
  517. reopen;
  518. { status }
  519. { status.currentsource:=inputfile^.name^; }
  520. Comment(V_Debug,'back in '+inputfile^.name^);
  521. { load some current_module fields }
  522. current_module^.current_index:=inputfile^.ref_index;
  523. end;
  524. { load next char }
  525. c:=inputpointer^;
  526. inc(longint(inputpointer));
  527. end;
  528. { function tscannerfile.fixbuf:boolean;
  529. var
  530. i : longint;
  531. p : pchar;
  532. c : char;
  533. begin
  534. fixbuf:=false;
  535. p:=inputbuffer;
  536. i:=0;
  537. while i<bufsize do
  538. begin
  539. c:=p^;
  540. case c of
  541. #0 : p^:=' ';
  542. #10,#13 : begin
  543. if (byte(c)+byte(p[1])=23) then
  544. begin
  545. inc(longint(p));
  546. inc(i);
  547. end;
  548. end;
  549. end;
  550. inc(i);
  551. inc(longint(p));
  552. end;
  553. if line_no=0 then
  554. line_no:=1;
  555. fixbuf:=true;
  556. end; }
  557. procedure tscannerfile.setbuf(p:pchar;l:longint);
  558. begin
  559. inputbuffer:=p;
  560. inputbufsize:=l;
  561. inputpointer:=inputbuffer;
  562. end;
  563. { function tscannerfile.setbufidx(idx:longint):longint;
  564. begin
  565. bufidx:=idx;
  566. setbufidx:=bufstart+idx;
  567. end; }
  568. { function tscannerfile.setlinebreak(idx:longint):longint;
  569. begin
  570. inc(line_no);
  571. bufidx:=idx;
  572. setlinebreak:=bufstart+idx;
  573. end; }
  574. procedure tscannerfile.gettokenpos;
  575. { load the values of tokenpos and lasttokenpos }
  576. begin
  577. lasttokenpos:=bufstart+(inputpointer-inputbuffer);
  578. tokenpos.line:=line_no;
  579. tokenpos.column:=lasttokenpos-lastlinepos;
  580. tokenpos.fileindex:=current_module^.current_index;
  581. end;
  582. procedure tscannerfile.inc_comment_level;
  583. begin
  584. inc(comment_level);
  585. if (comment_level>1) then
  586. Message1(scan_w_comment_level,tostr(comment_level));
  587. end;
  588. procedure tscannerfile.dec_comment_level;
  589. begin
  590. if cs_tp_compatible in aktswitches then
  591. comment_level:=0
  592. else
  593. dec(comment_level);
  594. end;
  595. procedure tscannerfile.linebreak;
  596. var
  597. cur : char;
  598. begin
  599. if (byte(inputpointer^)=0) and
  600. filenotatend then
  601. begin
  602. cur:=c;
  603. reload;
  604. if byte(cur)+byte(c)<>23 then
  605. dec(longint(inputpointer));
  606. end
  607. else
  608. begin
  609. { Fix linebreak to be only newline (=#10) for all types of linebreaks }
  610. if (byte(inputpointer^)+byte(c)=23) then
  611. inc(longint(inputpointer));
  612. end;
  613. c:=newline;
  614. { increase line counters }
  615. lastlinepos:=bufstart+(inputpointer-inputbuffer);
  616. inc(line_no);
  617. { update for status }
  618. inc(status.compiledlines);
  619. Comment(V_Status,'');
  620. end;
  621. {$else NEWINPUT}
  622. procedure gettokenpos;
  623. { load the values of tokenpos and lasttokenpos }
  624. begin
  625. tokenpos.line:=current_module^.current_inputfile^.true_line;
  626. tokenpos.column:=get_file_col;
  627. tokenpos.fileindex:=current_module^.current_index;
  628. end;
  629. procedure reload;
  630. var
  631. readsize : word;
  632. i,saveline : longint;
  633. begin
  634. if not assigned(current_module^.current_inputfile) then
  635. internalerror(14);
  636. if current_module^.current_inputfile^.filenotatend then
  637. begin
  638. { load the next piece of source }
  639. blockread(current_module^.current_inputfile^.f,inputbuffer^,
  640. current_module^.current_inputfile^.bufsize-1,readsize);
  641. { Scan the buffer for #0 chars, which are not alllowed }
  642. if readsize > 0 then
  643. begin
  644. { force proper line counting }
  645. saveline:=current_module^.current_inputfile^.true_line;
  646. i:=0;
  647. inputpointer:=inputbuffer;
  648. while i<readsize do
  649. begin
  650. c:=inputpointer^;
  651. case c of
  652. #0 : Message(scan_f_illegal_char);
  653. #10,#13 : begin
  654. if (byte(c)+byte(inputpointer[1])=23) then
  655. begin
  656. inc(longint(inputpointer));
  657. inc(i);
  658. end;
  659. inc(current_module^.current_inputfile^.true_line);
  660. end;
  661. end;
  662. inc(i);
  663. inc(longint(inputpointer));
  664. end;
  665. current_module^.current_inputfile^.true_line:=saveline;
  666. end;
  667. inputbuffer[readsize]:=#0;
  668. inputpointer:=inputbuffer;
  669. currlinepos:=inputpointer;
  670. { Set EOF when main source and at endoffile }
  671. if eof(current_module^.current_inputfile^.f) then
  672. begin
  673. current_module^.current_inputfile^.filenotatend:=false;
  674. if current_module^.current_inputfile^.next=nil then
  675. inputbuffer[readsize]:=#26;
  676. end;
  677. end
  678. else
  679. begin
  680. current_module^.current_inputfile^.close;
  681. { load next module }
  682. current_module^.current_inputfile:=current_module^.current_inputfile^.next;
  683. current_module^.current_index:=current_module^.current_inputfile^.ref_index;
  684. status.currentsource:=current_module^.current_inputfile^.name^+current_module^.current_inputfile^.ext^;
  685. inputbuffer:=current_module^.current_inputfile^.buf;
  686. inputpointer:=inputbuffer+current_module^.current_inputfile^.bufpos;
  687. currlinepos:=inputpointer;
  688. end;
  689. lastlinepos:=currlinepos;
  690. { load next char }
  691. c:=inputpointer^;
  692. inc(longint(inputpointer));
  693. end;
  694. procedure linebreak;
  695. var
  696. cur : char;
  697. begin
  698. if (byte(inputpointer^)=0) and
  699. current_module^.current_inputfile^.filenotatend then
  700. begin
  701. cur:=c;
  702. reload;
  703. if byte(cur)+byte(c)<>23 then
  704. dec(longint(inputpointer));
  705. end
  706. else
  707. begin
  708. { Fix linebreak to be only newline (=#10) for all types of linebreaks }
  709. if (byte(inputpointer^)+byte(c)=23) then
  710. inc(longint(inputpointer));
  711. end;
  712. c:=newline;
  713. { status }
  714. Comment(V_Status,'');
  715. { increase line counters }
  716. inc(current_module^.current_inputfile^.true_line);
  717. currlinepos:=inputpointer;
  718. inc(status.compiledlines);
  719. end;
  720. {$endif NEWINPUT}
  721. procedure {$ifdef NEWINPUT}tscannerfile.{$endif}poppreprocstack;
  722. var
  723. hp : ppreprocstack;
  724. begin
  725. if assigned(preprocstack) then
  726. begin
  727. hp:=preprocstack^.next;
  728. dispose(preprocstack,done);
  729. preprocstack:=hp;
  730. end
  731. else
  732. Message(scan_e_endif_without_if);
  733. end;
  734. procedure {$ifdef NEWINPUT}tscannerfile.{$endif}addpreprocstack(a:boolean;const s:string;w:tmsgconst);
  735. begin
  736. preprocstack:=new(ppreprocstack,init(((preprocstack=nil) or preprocstack^.accept) and a,preprocstack));
  737. preprocstack^.name:=s;
  738. preprocstack^.line_nb:={$ifndef NEWINPUT}current_module^.current_inputfile^.{$endif}line_no;
  739. if preprocstack^.accept then
  740. Message2(w,preprocstack^.name,'accepted')
  741. else
  742. Message2(w,preprocstack^.name,'rejected');
  743. end;
  744. procedure {$ifdef NEWINPUT}tscannerfile.{$endif}elsepreprocstack;
  745. begin
  746. if assigned(preprocstack) then
  747. begin
  748. if not(assigned(preprocstack^.next)) or (preprocstack^.next^.accept) then
  749. preprocstack^.accept:=not preprocstack^.accept;
  750. if preprocstack^.accept then
  751. Message2(scan_c_else_found,preprocstack^.name,'accepted')
  752. else
  753. Message2(scan_c_else_found,preprocstack^.name,'rejected');
  754. end
  755. else
  756. Message(scan_e_endif_without_if);
  757. end;
  758. procedure {$ifdef NEWINPUT}tscannerfile.{$endif}readchar;
  759. begin
  760. c:=inputpointer^;
  761. if c=#0 then
  762. reload
  763. else
  764. inc(longint(inputpointer));
  765. if c in [#10,#13] then
  766. linebreak;
  767. end;
  768. procedure {$ifdef NEWINPUT}tscannerfile.{$endif}readstring;
  769. var
  770. i : longint;
  771. begin
  772. i:=0;
  773. repeat
  774. case c of
  775. '_',
  776. '0'..'9',
  777. 'A'..'Z' : begin
  778. if i<255 then
  779. begin
  780. inc(i);
  781. orgpattern[i]:=c;
  782. pattern[i]:=c;
  783. end;
  784. c:=inputpointer^;
  785. inc(longint(inputpointer));
  786. end;
  787. 'a'..'z' : begin
  788. if i<255 then
  789. begin
  790. inc(i);
  791. orgpattern[i]:=c;
  792. pattern[i]:=chr(ord(c)-32)
  793. end;
  794. c:=inputpointer^;
  795. inc(longint(inputpointer));
  796. end;
  797. #0 : reload;
  798. #13,#10 : begin
  799. linebreak;
  800. break;
  801. end;
  802. else
  803. break;
  804. end;
  805. until false;
  806. orgpattern[0]:=chr(i);
  807. pattern[0]:=chr(i);
  808. end;
  809. procedure {$ifdef NEWINPUT}tscannerfile.{$endif}readnumber;
  810. var
  811. base,
  812. i : longint;
  813. begin
  814. case c of
  815. '%' : begin
  816. readchar;
  817. base:=2;
  818. pattern[1]:='%';
  819. i:=1;
  820. end;
  821. '$' : begin
  822. readchar;
  823. base:=16;
  824. pattern[1]:='$';
  825. i:=1;
  826. end;
  827. else
  828. begin
  829. base:=10;
  830. i:=0;
  831. end;
  832. end;
  833. while ((base>=10) and (c in ['0'..'9'])) or
  834. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  835. ((base=2) and (c in ['0'..'1'])) do
  836. begin
  837. if i<255 then
  838. begin
  839. inc(i);
  840. pattern[i]:=c;
  841. end;
  842. { get next char }
  843. c:=inputpointer^;
  844. if c=#0 then
  845. reload
  846. else
  847. inc(longint(inputpointer));
  848. end;
  849. { was the next char a linebreak ? }
  850. if c in [#10,#13] then
  851. linebreak;
  852. pattern[0]:=chr(i);
  853. end;
  854. function {$ifdef NEWINPUT}tscannerfile.{$endif}readid:string;
  855. begin
  856. readstring;
  857. readid:=pattern;
  858. end;
  859. function {$ifdef NEWINPUT}tscannerfile.{$endif}readval:longint;
  860. var
  861. l : longint;
  862. w : word;
  863. begin
  864. readnumber;
  865. valint(pattern,l,w);
  866. readval:=l;
  867. end;
  868. function {$ifdef NEWINPUT}tscannerfile.{$endif}readcomment:string;
  869. var
  870. i : longint;
  871. begin
  872. i:=0;
  873. repeat
  874. case c of
  875. '}' : begin
  876. readchar;
  877. dec_comment_level;
  878. break;
  879. end;
  880. #26 : Message(scan_f_end_of_file);
  881. else
  882. begin
  883. if (i<255) then
  884. begin
  885. inc(i);
  886. readcomment[i]:=c;
  887. end;
  888. end;
  889. end;
  890. c:=inputpointer^;
  891. if c=#0 then
  892. reload
  893. else
  894. inc(longint(inputpointer));
  895. if c in [#10,#13] then
  896. linebreak;
  897. until false;
  898. readcomment[0]:=chr(i);
  899. end;
  900. procedure {$ifdef NEWINPUT}tscannerfile.{$endif}skipspace;
  901. begin
  902. while c in [' ',#9..#13] do
  903. begin
  904. c:=inputpointer^;
  905. if c=#0 then
  906. reload
  907. else
  908. inc(longint(inputpointer));
  909. if c in [#10,#13] then
  910. linebreak;
  911. end;
  912. end;
  913. procedure {$ifdef NEWINPUT}tscannerfile.{$endif}skipuntildirective;
  914. var
  915. found : longint;
  916. begin
  917. found:=0;
  918. repeat
  919. case c of
  920. #26 : Message(scan_f_end_of_file);
  921. '{' : begin
  922. if comment_level=0 then
  923. found:=1;
  924. inc_comment_level;
  925. end;
  926. '}' : begin
  927. dec_comment_level;
  928. found:=0;
  929. end;
  930. '$' : begin
  931. if found=1 then
  932. found:=2;
  933. end;
  934. else
  935. found:=0;
  936. end;
  937. c:=inputpointer^;
  938. if c=#0 then
  939. reload
  940. else
  941. inc(longint(inputpointer));
  942. if c in [#10,#13] then
  943. linebreak;
  944. until (found=2);
  945. end;
  946. {$i scandir.inc}
  947. procedure {$ifdef NEWINPUT}tscannerfile.{$endif}skipcomment;
  948. begin
  949. readchar;
  950. inc_comment_level;
  951. { handle compiler switches }
  952. if (c='$') then
  953. handledirectives;
  954. { handle_switches can dec comment_level, }
  955. while (comment_level>0) do
  956. begin
  957. case c of
  958. '{' : inc_comment_level;
  959. '}' : dec_comment_level;
  960. #26 : Message(scan_f_end_of_file);
  961. end;
  962. c:=inputpointer^;
  963. if c=#0 then
  964. reload
  965. else
  966. inc(longint(inputpointer));
  967. if c in [#10,#13] then
  968. linebreak;
  969. end;
  970. end;
  971. procedure {$ifdef NEWINPUT}tscannerfile.{$endif}skipdelphicomment;
  972. begin
  973. inc_comment_level;
  974. readchar;
  975. { this is currently not supported }
  976. if c='$' then
  977. Message(scan_e_wrong_styled_switch);
  978. { skip comment }
  979. while c<>newline do
  980. begin
  981. if c=#26 then
  982. Message(scan_f_end_of_file);
  983. readchar;
  984. end;
  985. dec_comment_level;
  986. end;
  987. procedure {$ifdef NEWINPUT}tscannerfile.{$endif}skipoldtpcomment;
  988. var
  989. found : longint;
  990. begin
  991. inc_comment_level;
  992. readchar;
  993. { this is currently not supported }
  994. if c='$' then
  995. Message(scan_e_wrong_styled_switch);
  996. { skip comment }
  997. while (comment_level>0) do
  998. begin
  999. found:=0;
  1000. repeat
  1001. case c of
  1002. #26 : Message(scan_f_end_of_file);
  1003. '*' : begin
  1004. if found=3 then
  1005. inc_comment_level
  1006. else
  1007. found:=1;
  1008. end;
  1009. ')' : begin
  1010. if found=1 then
  1011. begin
  1012. dec_comment_level;
  1013. if comment_level=0 then
  1014. found:=2;
  1015. end;
  1016. end;
  1017. '(' : found:=3;
  1018. else
  1019. found:=0;
  1020. end;
  1021. c:=inputpointer^;
  1022. if c=#0 then
  1023. reload
  1024. else
  1025. inc(longint(inputpointer));
  1026. if c in [#10,#13] then
  1027. linebreak;
  1028. until (found=2);
  1029. end;
  1030. end;
  1031. function {$ifdef NEWINPUT}tscannerfile.{$endif}yylex : ttoken;
  1032. var
  1033. y : ttoken;
  1034. code : word;
  1035. l : longint;
  1036. mac : pmacrosym;
  1037. hp : pinputfile;
  1038. hp2 : pchar;
  1039. asciinr : string[3];
  1040. label
  1041. exit_label;
  1042. begin
  1043. { was the last character a point ? }
  1044. { this code is needed because the scanner if there is a 1. found if }
  1045. { this is a floating point number or range like 1..3 }
  1046. if s_point then
  1047. begin
  1048. gettokenpos;
  1049. s_point:=false;
  1050. if c='.' then
  1051. begin
  1052. readchar;
  1053. yylex:=POINTPOINT;
  1054. goto exit_label;
  1055. end;
  1056. yylex:=POINT;
  1057. goto exit_label;
  1058. end;
  1059. { Skip all spaces and comments }
  1060. repeat
  1061. case c of
  1062. '{' : skipcomment;
  1063. ' ',#9..#13 : skipspace;
  1064. else
  1065. break;
  1066. end;
  1067. until false;
  1068. { Save current token position }
  1069. {$ifdef NEWINPUT}
  1070. gettokenpos;
  1071. aktfilepos:=tokenpos;
  1072. {$else}
  1073. gettokenpos;
  1074. lastlinepos:=currlinepos;
  1075. lasttokenpos:=inputpointer;
  1076. {$endif}
  1077. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  1078. if c in ['_','A'..'Z','a'..'z'] then
  1079. begin
  1080. readstring;
  1081. if (length(pattern) in [2..id_len]) and is_keyword(y) then
  1082. yylex:=y
  1083. else
  1084. begin
  1085. { this takes some time ... }
  1086. if support_macros then
  1087. begin
  1088. mac:=pmacrosym(macros^.search(pattern));
  1089. if assigned(mac) and (assigned(mac^.buftext)) then
  1090. begin
  1091. { don't forget the last char }
  1092. dec(longint(inputpointer));
  1093. {$ifdef NEWINPUT}
  1094. hp:=new(pinputfile,init('Macro '+pattern));
  1095. addfile(hp);
  1096. getmem(hp2,mac^.buflen+1);
  1097. setbuf(hp2,mac^.buflen+1);
  1098. {$else}
  1099. current_module^.current_inputfile^.bufpos:=inputpointer-inputbuffer;
  1100. hp:=new(pinputfile,init('','Macro '+pattern,''));
  1101. { this isn't a proper way, but ... }
  1102. hp^.next:=current_module^.current_inputfile;
  1103. current_module^.current_inputfile:=hp;
  1104. status.currentsource:=current_module^.current_inputfile^.name^;
  1105. { I don't think that we should do that
  1106. because otherwise the file will be searched !! (PM)
  1107. but there is the problem of index !! }
  1108. current_module^.sourcefiles.register_file(hp);
  1109. current_module^.current_index:=hp^.ref_index;
  1110. { set an own buffer }
  1111. getmem(hp2,mac^.buflen+1);
  1112. current_module^.current_inputfile^.setbuf(hp2,mac^.buflen+1);
  1113. inputbuffer:=current_module^.current_inputfile^.buf;
  1114. {$endif NEWINPUT}
  1115. { copy text }
  1116. move(mac^.buftext^,inputbuffer^,mac^.buflen);
  1117. { put end sign }
  1118. inputbuffer[mac^.buflen+1]:=#0;
  1119. { load c }
  1120. c:=inputbuffer^;
  1121. inputpointer:=inputbuffer+1;
  1122. { handle empty macros }
  1123. if c=#0 then
  1124. reload;
  1125. { play it again ... }
  1126. inc(yylexcount);
  1127. if yylexcount>16 then
  1128. Message(scan_w_macro_deep_ten);
  1129. {$ifdef TP}
  1130. yylex:=yylex;
  1131. {$else}
  1132. yylex:=yylex();
  1133. {$endif}
  1134. { that's all folks }
  1135. dec(yylexcount);
  1136. exit;
  1137. end;
  1138. end;
  1139. yylex:=ID;
  1140. end;
  1141. goto exit_label;
  1142. end
  1143. else
  1144. begin
  1145. case c of
  1146. '$' : begin
  1147. readnumber;
  1148. yylex:=INTCONST;
  1149. goto exit_label;
  1150. end;
  1151. '%' : begin
  1152. readnumber;
  1153. yylex:=INTCONST;
  1154. goto exit_label;
  1155. end;
  1156. '0'..'9' : begin
  1157. readnumber;
  1158. case c of
  1159. '.' : begin
  1160. readchar;
  1161. if not(c in ['0'..'9']) then
  1162. begin
  1163. s_point:=true;
  1164. yylex:=INTCONST;
  1165. goto exit_label;
  1166. end;
  1167. pattern:=pattern+'.';
  1168. while c in ['0'..'9'] do
  1169. begin
  1170. pattern:=pattern+c;
  1171. readchar;
  1172. end;
  1173. yylex:=REALNUMBER;
  1174. goto exit_label;
  1175. end;
  1176. 'e','E' : begin
  1177. pattern:=pattern+'E';
  1178. readchar;
  1179. if c in ['-','+'] then
  1180. begin
  1181. pattern:=pattern+c;
  1182. readchar;
  1183. end;
  1184. if not(c in ['0'..'9']) then
  1185. Message(scan_f_illegal_char);
  1186. while c in ['0'..'9'] do
  1187. begin
  1188. pattern:=pattern+c;
  1189. readchar;
  1190. end;
  1191. yylex:=REALNUMBER;
  1192. goto exit_label;
  1193. end;
  1194. end;
  1195. yylex:=INTCONST;
  1196. goto exit_label;
  1197. end;
  1198. ';' : begin
  1199. readchar;
  1200. yylex:=SEMICOLON;
  1201. goto exit_label;
  1202. end;
  1203. '[' : begin
  1204. readchar;
  1205. yylex:=LECKKLAMMER;
  1206. goto exit_label;
  1207. end;
  1208. ']' : begin
  1209. readchar;
  1210. yylex:=RECKKLAMMER;
  1211. goto exit_label;
  1212. end;
  1213. '(' : begin
  1214. readchar;
  1215. if c='*' then
  1216. begin
  1217. skipoldtpcomment;
  1218. {$ifndef TP}
  1219. yylex:=yylex();
  1220. {$else}
  1221. yylex:=yylex;
  1222. {$endif}
  1223. exit;
  1224. end;
  1225. yylex:=LKLAMMER;
  1226. goto exit_label;
  1227. end;
  1228. ')' : begin
  1229. readchar;
  1230. yylex:=RKLAMMER;
  1231. goto exit_label;
  1232. end;
  1233. '+' : begin
  1234. readchar;
  1235. if (c='=') and support_c_operators then
  1236. begin
  1237. readchar;
  1238. yylex:=_PLUSASN;
  1239. goto exit_label;
  1240. end;
  1241. yylex:=PLUS;
  1242. goto exit_label;
  1243. end;
  1244. '-' : begin
  1245. readchar;
  1246. if (c='=') and support_c_operators then
  1247. begin
  1248. readchar;
  1249. yylex:=_MINUSASN;
  1250. goto exit_label;
  1251. end;
  1252. yylex:=MINUS;
  1253. goto exit_label;
  1254. end;
  1255. ':' : begin
  1256. readchar;
  1257. if c='=' then
  1258. begin
  1259. readchar;
  1260. yylex:=ASSIGNMENT;
  1261. goto exit_label;
  1262. end;
  1263. yylex:=COLON;
  1264. goto exit_label;
  1265. end;
  1266. '*' : begin
  1267. readchar;
  1268. if (c='=') and support_c_operators then
  1269. begin
  1270. readchar;
  1271. yylex:=_STARASN;
  1272. end
  1273. else
  1274. if c='*' then
  1275. begin
  1276. readchar;
  1277. yylex:=STARSTAR;
  1278. end
  1279. else
  1280. yylex:=STAR;
  1281. goto exit_label;
  1282. end;
  1283. '/' : begin
  1284. readchar;
  1285. case c of
  1286. '=' : begin
  1287. if support_c_operators then
  1288. begin
  1289. readchar;
  1290. yylex:=_SLASHASN;
  1291. goto exit_label;
  1292. end;
  1293. end;
  1294. '/' : begin
  1295. skipdelphicomment;
  1296. {$ifndef TP}
  1297. yylex:=yylex();
  1298. {$else TP}
  1299. yylex:=yylex;
  1300. {$endif TP}
  1301. exit;
  1302. end;
  1303. end;
  1304. yylex:=SLASH;
  1305. goto exit_label;
  1306. end;
  1307. '=' : begin
  1308. readchar;
  1309. yylex:=EQUAL;
  1310. goto exit_label;
  1311. end;
  1312. '.' : begin
  1313. readchar;
  1314. if c='.' then
  1315. begin
  1316. readchar;
  1317. yylex:=POINTPOINT;
  1318. goto exit_label;
  1319. end
  1320. else
  1321. yylex:=POINT;
  1322. goto exit_label;
  1323. end;
  1324. '@' : begin
  1325. readchar;
  1326. if c='@' then
  1327. begin
  1328. readchar;
  1329. yylex:=DOUBLEADDR;
  1330. end
  1331. else
  1332. yylex:=KLAMMERAFFE;
  1333. goto exit_label;
  1334. end;
  1335. ',' : begin
  1336. readchar;
  1337. yylex:=COMMA;
  1338. goto exit_label;
  1339. end;
  1340. '''','#','^' : begin
  1341. if c='^' then
  1342. begin
  1343. readchar;
  1344. c:=upcase(c);
  1345. if not(block_type=bt_type) and (c in ['A'..'Z']) then
  1346. begin
  1347. pattern:=chr(ord(c)-64);
  1348. readchar;
  1349. end
  1350. else
  1351. begin
  1352. yylex:=CARET;
  1353. goto exit_label;
  1354. end;
  1355. end
  1356. else
  1357. pattern:='';
  1358. repeat
  1359. case c of
  1360. '#' : begin
  1361. readchar; { read # }
  1362. asciinr:='';
  1363. while (c in ['0'..'9']) and (length(asciinr)<3) do
  1364. begin
  1365. asciinr:=asciinr+c;
  1366. readchar;
  1367. end;
  1368. valint(asciinr,l,code);
  1369. if (asciinr='') or (code<>0) or
  1370. (l<0) or (l>255) then
  1371. Message(scan_e_illegal_char_const);
  1372. pattern:=pattern+chr(l);
  1373. end;
  1374. '''' : begin
  1375. repeat
  1376. readchar;
  1377. case c of
  1378. #26 : Message(scan_f_end_of_file);
  1379. newline : Message(scan_f_string_exceeds_line);
  1380. '''' : begin
  1381. readchar;
  1382. if c<>'''' then
  1383. break;
  1384. end;
  1385. end;
  1386. pattern:=pattern+c;
  1387. until false;
  1388. end;
  1389. '^' : begin
  1390. readchar;
  1391. if c<#64 then
  1392. c:=chr(ord(c)+64)
  1393. else
  1394. c:=chr(ord(c)-64);
  1395. pattern:=pattern+c;
  1396. readchar;
  1397. end;
  1398. else
  1399. break;
  1400. end;
  1401. until false;
  1402. { strings with length 1 become const chars }
  1403. if length(pattern)=1 then
  1404. yylex:=CCHAR
  1405. else
  1406. yylex:=CSTRING;
  1407. goto exit_label;
  1408. end;
  1409. '>' : begin
  1410. readchar;
  1411. case c of
  1412. '=' : begin
  1413. readchar;
  1414. yylex:=GTE;
  1415. goto exit_label;
  1416. end;
  1417. '>' : begin
  1418. readchar;
  1419. yylex:=_SHR;
  1420. goto exit_label;
  1421. end;
  1422. '<' : begin { >< is for a symetric diff for sets }
  1423. readchar;
  1424. yylex:=SYMDIF;
  1425. goto exit_label;
  1426. end;
  1427. end;
  1428. yylex:=GT;
  1429. goto exit_label;
  1430. end;
  1431. '<' : begin
  1432. readchar;
  1433. case c of
  1434. '>' : begin
  1435. readchar;
  1436. yylex:=UNEQUAL;
  1437. goto exit_label;
  1438. end;
  1439. '=' : begin
  1440. readchar;
  1441. yylex:=LTE;
  1442. goto exit_label;
  1443. end;
  1444. '<' : begin
  1445. readchar;
  1446. yylex:=_SHL;
  1447. goto exit_label;
  1448. end;
  1449. end;
  1450. yylex:=LT;
  1451. goto exit_label;
  1452. end;
  1453. #26 : begin
  1454. yylex:=_EOF;
  1455. goto exit_label;
  1456. end;
  1457. else
  1458. begin
  1459. Message(scan_f_illegal_char);
  1460. end;
  1461. end;
  1462. end;
  1463. exit_label:
  1464. { don't change the file : too risky !! }
  1465. {$ifndef NEWINPUT}
  1466. if current_module^.current_index=tokenpos.fileindex then
  1467. begin
  1468. current_module^.current_inputfile^.line_no:=tokenpos.line;
  1469. current_module^.current_inputfile^.column:=tokenpos.column;
  1470. current_column:=tokenpos.column;
  1471. end;
  1472. {$endif NEWINPUT}
  1473. end;
  1474. {$ifdef NEWINPUT}
  1475. function tscannerfile.readpreproc:ttoken;
  1476. begin
  1477. skipspace;
  1478. case c of
  1479. 'A'..'Z',
  1480. 'a'..'z',
  1481. '_','0'..'9' : begin
  1482. preprocpat:=readid;
  1483. readpreproc:=ID;
  1484. end;
  1485. '(' : begin
  1486. readchar;
  1487. readpreproc:=LKLAMMER;
  1488. end;
  1489. ')' : begin
  1490. readchar;
  1491. readpreproc:=RKLAMMER;
  1492. end;
  1493. '+' : begin
  1494. readchar;
  1495. readpreproc:=PLUS;
  1496. end;
  1497. '-' : begin
  1498. readchar;
  1499. readpreproc:=MINUS;
  1500. end;
  1501. '*' : begin
  1502. readchar;
  1503. readpreproc:=STAR;
  1504. end;
  1505. '/' : begin
  1506. readchar;
  1507. readpreproc:=SLASH;
  1508. end;
  1509. '=' : begin
  1510. readchar;
  1511. readpreproc:=EQUAL;
  1512. end;
  1513. '>' : begin
  1514. readchar;
  1515. if c='=' then
  1516. begin
  1517. readchar;
  1518. readpreproc:=GTE;
  1519. end
  1520. else
  1521. readpreproc:=GT;
  1522. end;
  1523. '<' : begin
  1524. readchar;
  1525. case c of
  1526. '>' : begin
  1527. readchar;
  1528. readpreproc:=UNEQUAL;
  1529. end;
  1530. '=' : begin
  1531. readchar;
  1532. readpreproc:=LTE;
  1533. end;
  1534. else readpreproc:=LT;
  1535. end;
  1536. end;
  1537. #26 : Message(scan_f_end_of_file);
  1538. else
  1539. begin
  1540. readpreproc:=_EOF;
  1541. end;
  1542. end;
  1543. end;
  1544. {$endif}
  1545. function {$ifdef NEWINPUT}tscannerfile.{$endif}asmgetchar : char;
  1546. begin
  1547. if lastasmgetchar<>#0 then
  1548. begin
  1549. c:=lastasmgetchar;
  1550. lastasmgetchar:=#0;
  1551. end
  1552. else
  1553. readchar;
  1554. case c of
  1555. '{' : begin
  1556. skipcomment;
  1557. lastasmgetchar:=c;
  1558. asmgetchar:=';';
  1559. exit;
  1560. end;
  1561. '/' : begin
  1562. readchar;
  1563. if c='/' then
  1564. begin
  1565. skipdelphicomment;
  1566. asmgetchar:=';';
  1567. end
  1568. else
  1569. asmgetchar:='/';
  1570. lastasmgetchar:=c;
  1571. exit;
  1572. end;
  1573. '(' : begin
  1574. readchar;
  1575. if c='*' then
  1576. begin
  1577. skipoldtpcomment;
  1578. asmgetchar:=';';
  1579. end
  1580. else
  1581. asmgetchar:='(';
  1582. lastasmgetchar:=c;
  1583. exit;
  1584. end;
  1585. else
  1586. begin
  1587. asmgetchar:=c;
  1588. end;
  1589. end;
  1590. end;
  1591. {$ifdef NEWINPUT}
  1592. {$else NEWPPU}
  1593. procedure InitScanner(const fn: string);
  1594. var
  1595. d:dirstr;
  1596. n:namestr;
  1597. e:extstr;
  1598. begin
  1599. fsplit(fn,d,n,e);
  1600. current_module^.current_inputfile:=new(pinputfile,init(d,n,e));
  1601. if not current_module^.current_inputfile^.reset then
  1602. Message(scan_f_cannot_open_input);
  1603. current_module^.sourcefiles.register_file(current_module^.current_inputfile);
  1604. current_module^.current_index:=current_module^.current_inputfile^.ref_index;
  1605. status.currentsource:=current_module^.current_inputfile^.name^+current_module^.current_inputfile^.ext^;
  1606. inputbuffer:=current_module^.current_inputfile^.buf;
  1607. reload;
  1608. preprocstack:=nil;
  1609. comment_level:=0;
  1610. lasttokenpos:=inputpointer;
  1611. lastlinepos:=inputpointer;
  1612. currlinepos:=inputpointer;
  1613. s_point:=false;
  1614. block_type:=bt_general;
  1615. end;
  1616. procedure get_cur_file_pos(var fileinfo : tfileposinfo);
  1617. begin
  1618. with fileinfo do
  1619. begin
  1620. line:=current_module^.current_inputfile^.line_no;
  1621. fileindex:=current_module^.current_index;
  1622. column:=get_current_col;
  1623. end;
  1624. end;
  1625. procedure set_cur_file_pos(const fileinfo : tfileposinfo);
  1626. begin
  1627. if current_module^.current_index<>fileinfo.fileindex then
  1628. begin
  1629. current_module^.current_index:=fileinfo.fileindex;
  1630. current_module^.current_inputfile:=
  1631. pinputfile(current_module^.sourcefiles.get_file(fileinfo.fileindex));
  1632. end;
  1633. if assigned(current_module^.current_inputfile) then
  1634. begin
  1635. current_module^.current_inputfile^.line_no:=fileinfo.line;
  1636. current_module^.current_inputfile^.column:=fileinfo.column;
  1637. current_column:=fileinfo.column;
  1638. end;
  1639. end;
  1640. procedure DoneScanner(testendif:boolean);
  1641. begin
  1642. if (not testendif) then
  1643. begin
  1644. while assigned(preprocstack) do
  1645. begin
  1646. Message3(scan_e_endif_expected,'$IF(N)(DEF)',preprocstack^.name,tostr(preprocstack^.line_nb));
  1647. poppreprocstack;
  1648. end;
  1649. end;
  1650. end;
  1651. {$endif NEWINPUT}
  1652. end.
  1653. {
  1654. $Log$
  1655. Revision 1.30 1998-07-07 12:32:55 peter
  1656. * status.currentsource is now calculated in verbose (more accurated)
  1657. Revision 1.29 1998/07/07 11:20:11 peter
  1658. + NEWINPUT for a better inputfile and scanner object
  1659. Revision 1.28 1998/07/01 15:26:57 peter
  1660. * better bufferfile.reset error handling
  1661. Revision 1.27 1998/06/25 08:48:19 florian
  1662. * first version of rtti support
  1663. Revision 1.26 1998/06/16 08:56:30 peter
  1664. + targetcpu
  1665. * cleaner pmodules for newppu
  1666. Revision 1.25 1998/06/13 00:10:15 peter
  1667. * working browser and newppu
  1668. * some small fixes against crashes which occured in bp7 (but not in
  1669. fpc?!)
  1670. Revision 1.24 1998/06/12 10:32:36 pierre
  1671. * column problem hopefully solved
  1672. + C vars declaration changed
  1673. Revision 1.23 1998/06/03 22:49:02 peter
  1674. + wordbool,longbool
  1675. * rename bis,von -> high,low
  1676. * moved some systemunit loading/creating to psystem.pas
  1677. Revision 1.21 1998/05/27 00:20:32 peter
  1678. * some scanner optimizes
  1679. * automaticly aout2exe for go32v1
  1680. * fixed dynamiclinker option which was added at the wrong place
  1681. Revision 1.20 1998/05/23 01:21:30 peter
  1682. + aktasmmode, aktoptprocessor, aktoutputformat
  1683. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1684. + $LIBNAME to set the library name where the unit will be put in
  1685. * splitted cgi386 a bit (codeseg to large for bp7)
  1686. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1687. Revision 1.19 1998/05/20 09:42:37 pierre
  1688. + UseTokenInfo now default
  1689. * unit in interface uses and implementation uses gives error now
  1690. * only one error for unknown symbol (uses lastsymknown boolean)
  1691. the problem came from the label code !
  1692. + first inlined procedures and function work
  1693. (warning there might be allowed cases were the result is still wrong !!)
  1694. * UseBrower updated gives a global list of all position of all used symbols
  1695. with switch -gb
  1696. Revision 1.18 1998/05/12 10:47:00 peter
  1697. * moved printstatus to verb_def
  1698. + V_Normal which is between V_Error and V_Warning and doesn't have a
  1699. prefix like error: warning: and is included in V_Default
  1700. * fixed some messages
  1701. * first time parameter scan is only for -v and -T
  1702. - removed old style messages
  1703. Revision 1.17 1998/05/06 08:38:47 pierre
  1704. * better position info with UseTokenInfo
  1705. UseTokenInfo greatly simplified
  1706. + added check for changed tree after first time firstpass
  1707. (if we could remove all the cases were it happen
  1708. we could skip all firstpass if firstpasscount > 1)
  1709. Only with ExtDebug
  1710. Revision 1.16 1998/05/04 17:54:28 peter
  1711. + smartlinking works (only case jumptable left todo)
  1712. * redesign of systems.pas to support assemblers and linkers
  1713. + Unitname is now also in the PPU-file, increased version to 14
  1714. Revision 1.15 1998/05/01 16:38:46 florian
  1715. * handling of private and protected fixed
  1716. + change_keywords_to_tp implemented to remove
  1717. keywords which aren't supported by tp
  1718. * break and continue are now symbols of the system unit
  1719. + widestring, longstring and ansistring type released
  1720. Revision 1.14 1998/04/30 15:59:42 pierre
  1721. * GDB works again better :
  1722. correct type info in one pass
  1723. + UseTokenInfo for better source position
  1724. * fixed one remaining bug in scanner for line counts
  1725. * several little fixes
  1726. Revision 1.13 1998/04/29 13:42:27 peter
  1727. + $IOCHECKS and $ALIGN to test already, other will follow soon
  1728. * fixed the wrong linecounting with comments
  1729. Revision 1.12 1998/04/29 10:34:04 pierre
  1730. + added some code for ansistring (not complete nor working yet)
  1731. * corrected operator overloading
  1732. * corrected nasm output
  1733. + started inline procedures
  1734. + added starstarn : use ** for exponentiation (^ gave problems)
  1735. + started UseTokenInfo cond to get accurate positions
  1736. Revision 1.11 1998/04/27 23:10:29 peter
  1737. + new scanner
  1738. * $makelib -> if smartlink
  1739. * small filename fixes pmodule.setfilename
  1740. * moved import from files.pas -> import.pas
  1741. }