scanner.pas 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. unit scanner;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cobjects,
  23. globtype,globals,version,tokens,
  24. verbose,comphook,
  25. finput,
  26. widestr;
  27. const
  28. maxmacrolen=16*1024;
  29. preprocbufsize=32*1024;
  30. Newline = #10;
  31. type
  32. tcommentstyle = (comment_none,comment_tp,comment_oldtp,comment_delphi,comment_c);
  33. pmacrobuffer = ^tmacrobuffer;
  34. tmacrobuffer = array[0..maxmacrolen-1] of char;
  35. pmacro = ^tmacro;
  36. tmacro = object(tnamedindexobject)
  37. defined,
  38. defined_at_startup,
  39. is_used : boolean;
  40. buftext : pchar;
  41. buflen : longint;
  42. constructor init(const n : string);
  43. destructor done;virtual;
  44. end;
  45. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else);
  46. ppreprocstack = ^tpreprocstack;
  47. tpreprocstack = object
  48. typ : preproctyp;
  49. accept : boolean;
  50. next : ppreprocstack;
  51. name : stringid;
  52. line_nb : longint;
  53. constructor init(atyp:preproctyp;a:boolean;n:ppreprocstack);
  54. destructor done;
  55. end;
  56. pscannerfile = ^tscannerfile;
  57. tscannerfile = object
  58. inputfile : pinputfile; { current inputfile list }
  59. inputbuffer, { input buffer }
  60. inputpointer : pchar;
  61. inputstart : longint;
  62. line_no, { line }
  63. lastlinepos : longint;
  64. lasttokenpos : longint; { token }
  65. lasttoken,
  66. nexttoken : ttoken;
  67. comment_level,
  68. yylexcount : longint;
  69. lastasmgetchar : char;
  70. ignoredirectives : tstringcontainer; { ignore directives, used to give warnings only once }
  71. preprocstack : ppreprocstack;
  72. invalid : boolean; { flag if sourcefiles have been destroyed ! }
  73. macros : pdictionary;
  74. in_asm_string : boolean;
  75. constructor init(const fn:string);
  76. destructor done;
  77. { File buffer things }
  78. function openinputfile:boolean;
  79. procedure closeinputfile;
  80. function tempopeninputfile:boolean;
  81. procedure tempcloseinputfile;
  82. procedure saveinputfile;
  83. procedure restoreinputfile;
  84. procedure nextfile;
  85. procedure addfile(hp:pinputfile);
  86. procedure reload;
  87. procedure insertmacro(const macname:string;p:pchar;len:longint);
  88. { Scanner things }
  89. procedure def_macro(const s : string);
  90. procedure set_macro(const s : string;value : string);
  91. procedure gettokenpos;
  92. procedure inc_comment_level;
  93. procedure dec_comment_level;
  94. procedure illegal_char(c:char);
  95. procedure end_of_file;
  96. procedure checkpreprocstack;
  97. procedure poppreprocstack;
  98. procedure addpreprocstack(atyp : preproctyp;a:boolean;const s:string;w:longint);
  99. procedure elsepreprocstack;
  100. procedure linebreak;
  101. procedure readchar;
  102. procedure readstring;
  103. procedure readnumber;
  104. function readid:string;
  105. function readval:longint;
  106. function readcomment:string;
  107. function readstate:char;
  108. procedure skipspace;
  109. procedure skipuntildirective;
  110. procedure skipcomment;
  111. procedure skipdelphicomment;
  112. procedure skipoldtpcomment;
  113. procedure readtoken;
  114. function readpreproc:ttoken;
  115. function asmgetchar:char;
  116. end;
  117. ppreprocfile=^tpreprocfile;
  118. tpreprocfile=object
  119. f : text;
  120. buf : pointer;
  121. spacefound,
  122. eolfound : boolean;
  123. constructor init(const fn:string);
  124. destructor done;
  125. procedure Add(const s:string);
  126. procedure AddSpace;
  127. end;
  128. var
  129. { read strings }
  130. c : char;
  131. orgpattern,
  132. pattern : string;
  133. patternw : tcompilerwidestring;
  134. { token }
  135. token, { current token being parsed }
  136. idtoken : ttoken; { holds the token if the pattern is a known word }
  137. current_scanner : pscannerfile;
  138. aktcommentstyle : tcommentstyle; { needed to use read_comment from directives }
  139. preprocfile : ppreprocfile; { used with only preprocessing }
  140. implementation
  141. uses
  142. {$ifdef delphi}
  143. dmisc,
  144. {$else}
  145. dos,
  146. {$endif delphi}
  147. cutils,
  148. systems,
  149. switches,
  150. fmodule;
  151. {*****************************************************************************
  152. Helper routines
  153. *****************************************************************************}
  154. const
  155. { use any special name that is an invalid file name to avoid problems }
  156. preprocstring : array [preproctyp] of string[7]
  157. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE');
  158. function is_keyword(const s:string):boolean;
  159. var
  160. low,high,mid : longint;
  161. begin
  162. if not (length(s) in [2..tokenidlen]) then
  163. begin
  164. is_keyword:=false;
  165. exit;
  166. end;
  167. low:=ord(tokenidx^[length(s),s[1]].first);
  168. high:=ord(tokenidx^[length(s),s[1]].last);
  169. while low<high do
  170. begin
  171. mid:=(high+low+1) shr 1;
  172. if pattern<tokeninfo^[ttoken(mid)].str then
  173. high:=mid-1
  174. else
  175. low:=mid;
  176. end;
  177. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  178. (tokeninfo^[ttoken(high)].keyword in aktmodeswitches);
  179. end;
  180. {*****************************************************************************
  181. TMacro
  182. *****************************************************************************}
  183. constructor tmacro.init(const n : string);
  184. begin
  185. inherited initname(n);
  186. defined:=true;
  187. defined_at_startup:=false;
  188. is_used:=false;
  189. buftext:=nil;
  190. buflen:=0;
  191. end;
  192. destructor tmacro.done;
  193. begin
  194. if assigned(buftext) then
  195. freemem(buftext,buflen);
  196. inherited done;
  197. end;
  198. {*****************************************************************************
  199. Preprocessor writting
  200. *****************************************************************************}
  201. constructor tpreprocfile.init(const fn:string);
  202. begin
  203. { open outputfile }
  204. assign(f,fn);
  205. {$I-}
  206. rewrite(f);
  207. {$I+}
  208. if ioresult<>0 then
  209. Comment(V_Fatal,'can''t create file '+fn);
  210. getmem(buf,preprocbufsize);
  211. settextbuf(f,buf^,preprocbufsize);
  212. { reset }
  213. eolfound:=false;
  214. spacefound:=false;
  215. end;
  216. destructor tpreprocfile.done;
  217. begin
  218. close(f);
  219. freemem(buf,preprocbufsize);
  220. end;
  221. procedure tpreprocfile.add(const s:string);
  222. begin
  223. write(f,s);
  224. end;
  225. procedure tpreprocfile.addspace;
  226. begin
  227. if eolfound then
  228. begin
  229. writeln(f,'');
  230. eolfound:=false;
  231. spacefound:=false;
  232. end
  233. else
  234. if spacefound then
  235. begin
  236. write(f,' ');
  237. spacefound:=false;
  238. end;
  239. end;
  240. {*****************************************************************************
  241. TPreProcStack
  242. *****************************************************************************}
  243. constructor tpreprocstack.init(atyp : preproctyp;a:boolean;n:ppreprocstack);
  244. begin
  245. accept:=a;
  246. typ:=atyp;
  247. next:=n;
  248. end;
  249. destructor tpreprocstack.done;
  250. begin
  251. end;
  252. {****************************************************************************
  253. TSCANNERFILE
  254. ****************************************************************************}
  255. constructor tscannerfile.init(const fn:string);
  256. begin
  257. inputfile:=do_openinputfile(fn);
  258. if assigned(current_module) then
  259. current_module^.sourcefiles^.register_file(inputfile);
  260. { reset localinput }
  261. inputbuffer:=nil;
  262. inputpointer:=nil;
  263. inputstart:=0;
  264. { reset scanner }
  265. preprocstack:=nil;
  266. comment_level:=0;
  267. yylexcount:=0;
  268. block_type:=bt_general;
  269. line_no:=0;
  270. lastlinepos:=0;
  271. lasttokenpos:=0;
  272. lasttoken:=NOTOKEN;
  273. nexttoken:=NOTOKEN;
  274. lastasmgetchar:=#0;
  275. ignoredirectives.init;
  276. invalid:=false;
  277. in_asm_string:=false;
  278. new(macros,init);
  279. { load block }
  280. if not openinputfile then
  281. Message1(scan_f_cannot_open_input,fn);
  282. reload;
  283. { process first read char }
  284. case c of
  285. #26 : reload;
  286. #10,
  287. #13 : linebreak;
  288. end;
  289. end;
  290. destructor tscannerfile.done;
  291. begin
  292. if not invalid then
  293. begin
  294. if status.errorcount=0 then
  295. checkpreprocstack;
  296. { close file, but only if we are the first compile }
  297. { probably not necessary anymore with invalid flag PM }
  298. if not current_module^.in_second_compile then
  299. begin
  300. if not inputfile^.closed then
  301. closeinputfile;
  302. end;
  303. end;
  304. ignoredirectives.done;
  305. dispose(macros,done);
  306. end;
  307. procedure tscannerfile.def_macro(const s : string);
  308. var
  309. mac : pmacro;
  310. begin
  311. mac:=pmacro(macros^.search(s));
  312. if mac=nil then
  313. begin
  314. mac:=new(pmacro,init(s));
  315. Message1(parser_m_macro_defined,mac^.name);
  316. macros^.insert(mac);
  317. end;
  318. mac^.defined:=true;
  319. mac^.defined_at_startup:=true;
  320. end;
  321. procedure tscannerfile.set_macro(const s : string;value : string);
  322. var
  323. mac : pmacro;
  324. begin
  325. mac:=pmacro(macros^.search(s));
  326. if mac=nil then
  327. begin
  328. mac:=new(pmacro,init(s));
  329. macros^.insert(mac);
  330. end
  331. else
  332. begin
  333. if assigned(mac^.buftext) then
  334. freemem(mac^.buftext,mac^.buflen);
  335. end;
  336. Message2(parser_m_macro_set_to,mac^.name,value);
  337. mac^.buflen:=length(value);
  338. getmem(mac^.buftext,mac^.buflen);
  339. move(value[1],mac^.buftext^,mac^.buflen);
  340. mac^.defined:=true;
  341. mac^.defined_at_startup:=true;
  342. end;
  343. function tscannerfile.openinputfile:boolean;
  344. begin
  345. openinputfile:=inputfile^.open;
  346. { load buffer }
  347. inputbuffer:=inputfile^.buf;
  348. inputpointer:=inputfile^.buf;
  349. inputstart:=inputfile^.bufstart;
  350. { line }
  351. line_no:=0;
  352. lastlinepos:=0;
  353. lasttokenpos:=0;
  354. end;
  355. procedure tscannerfile.closeinputfile;
  356. begin
  357. inputfile^.close;
  358. { reset buffer }
  359. inputbuffer:=nil;
  360. inputpointer:=nil;
  361. inputstart:=0;
  362. { reset line }
  363. line_no:=0;
  364. lastlinepos:=0;
  365. lasttokenpos:=0;
  366. end;
  367. function tscannerfile.tempopeninputfile:boolean;
  368. begin
  369. tempopeninputfile:=inputfile^.tempopen;
  370. { reload buffer }
  371. inputbuffer:=inputfile^.buf;
  372. inputpointer:=inputfile^.buf;
  373. inputstart:=inputfile^.bufstart;
  374. end;
  375. procedure tscannerfile.tempcloseinputfile;
  376. begin
  377. inputfile^.setpos(inputstart+(inputpointer-inputbuffer));
  378. inputfile^.tempclose;
  379. { reset buffer }
  380. inputbuffer:=nil;
  381. inputpointer:=nil;
  382. inputstart:=0;
  383. end;
  384. procedure tscannerfile.saveinputfile;
  385. begin
  386. inputfile^.saveinputpointer:=inputpointer;
  387. inputfile^.savelastlinepos:=lastlinepos;
  388. inputfile^.saveline_no:=line_no;
  389. end;
  390. procedure tscannerfile.restoreinputfile;
  391. begin
  392. inputpointer:=inputfile^.saveinputpointer;
  393. lastlinepos:=inputfile^.savelastlinepos;
  394. line_no:=inputfile^.saveline_no;
  395. if not inputfile^.is_macro then
  396. parser_current_file:=inputfile^.name^;
  397. end;
  398. procedure tscannerfile.nextfile;
  399. var
  400. to_dispose : pinputfile;
  401. begin
  402. if assigned(inputfile^.next) then
  403. begin
  404. if inputfile^.is_macro then
  405. to_dispose:=inputfile
  406. else
  407. to_dispose:=nil;
  408. { we can allways close the file, no ? }
  409. inputfile^.close;
  410. inputfile:=inputfile^.next;
  411. if assigned(to_dispose) then
  412. dispose(to_dispose,done);
  413. restoreinputfile;
  414. end;
  415. end;
  416. procedure tscannerfile.addfile(hp:pinputfile);
  417. begin
  418. saveinputfile;
  419. { add to list }
  420. hp^.next:=inputfile;
  421. inputfile:=hp;
  422. { load new inputfile }
  423. restoreinputfile;
  424. end;
  425. procedure tscannerfile.reload;
  426. begin
  427. with inputfile^ do
  428. begin
  429. { when nothing more to read then leave immediatly, so we
  430. don't change the aktfilepos and leave it point to the last
  431. char }
  432. if (c=#26) and (not assigned(next)) then
  433. exit;
  434. repeat
  435. { still more to read?, then change the #0 to a space so its seen
  436. as a seperator, this can't be used for macro's which can change
  437. the place of the #0 in the buffer with tempopen }
  438. if (c=#0) and (bufsize>0) and
  439. not(inputfile^.is_macro) and
  440. (inputpointer-inputbuffer<bufsize) then
  441. begin
  442. c:=' ';
  443. inc(longint(inputpointer));
  444. exit;
  445. end;
  446. { can we read more from this file ? }
  447. if (c<>#26) and (not endoffile) then
  448. begin
  449. readbuf;
  450. inputpointer:=buf;
  451. inputbuffer:=buf;
  452. inputstart:=bufstart;
  453. { first line? }
  454. if line_no=0 then
  455. begin
  456. line_no:=1;
  457. if cs_asm_source in aktglobalswitches then
  458. inputfile^.setline(line_no,bufstart);
  459. end;
  460. end
  461. else
  462. begin
  463. { load eof position in tokenpos/aktfilepos }
  464. gettokenpos;
  465. { close file }
  466. closeinputfile;
  467. { no next module, than EOF }
  468. if not assigned(inputfile^.next) then
  469. begin
  470. c:=#26;
  471. exit;
  472. end;
  473. { load next file and reopen it }
  474. nextfile;
  475. tempopeninputfile;
  476. { status }
  477. Message1(scan_t_back_in,inputfile^.name^);
  478. end;
  479. { load next char }
  480. c:=inputpointer^;
  481. inc(longint(inputpointer));
  482. until c<>#0; { if also end, then reload again }
  483. end;
  484. end;
  485. procedure tscannerfile.insertmacro(const macname:string;p:pchar;len:longint);
  486. var
  487. hp : pinputfile;
  488. begin
  489. { save old postion and decrease linebreak }
  490. if c=newline then
  491. dec(line_no);
  492. dec(longint(inputpointer));
  493. tempcloseinputfile;
  494. { create macro 'file' }
  495. { use special name to dispose after !! }
  496. hp:=do_openinputfile('_Macro_.'+macname);
  497. addfile(hp);
  498. with inputfile^ do
  499. begin
  500. setmacro(p,len);
  501. { local buffer }
  502. inputbuffer:=buf;
  503. inputpointer:=buf;
  504. inputstart:=bufstart;
  505. end;
  506. { reset line }
  507. line_no:=0;
  508. lastlinepos:=0;
  509. lasttokenpos:=0;
  510. { load new c }
  511. c:=inputpointer^;
  512. inc(longint(inputpointer));
  513. end;
  514. procedure tscannerfile.gettokenpos;
  515. { load the values of tokenpos and lasttokenpos }
  516. begin
  517. lasttokenpos:=inputstart+(inputpointer-inputbuffer);
  518. akttokenpos.line:=line_no;
  519. akttokenpos.column:=lasttokenpos-lastlinepos;
  520. akttokenpos.fileindex:=inputfile^.ref_index;
  521. aktfilepos:=akttokenpos;
  522. end;
  523. procedure tscannerfile.inc_comment_level;
  524. var
  525. oldaktfilepos : tfileposinfo;
  526. begin
  527. if (m_nested_comment in aktmodeswitches) then
  528. inc(comment_level)
  529. else
  530. comment_level:=1;
  531. if (comment_level>1) then
  532. begin
  533. oldaktfilepos:=aktfilepos;
  534. gettokenpos; { update for warning }
  535. Message1(scan_w_comment_level,tostr(comment_level));
  536. aktfilepos:=oldaktfilepos;
  537. end;
  538. end;
  539. procedure tscannerfile.dec_comment_level;
  540. begin
  541. if (m_nested_comment in aktmodeswitches) then
  542. dec(comment_level)
  543. else
  544. comment_level:=0;
  545. end;
  546. procedure tscannerfile.linebreak;
  547. var
  548. cur : char;
  549. oldtokenpos,
  550. oldaktfilepos : tfileposinfo;
  551. begin
  552. with inputfile^ do
  553. begin
  554. if (byte(inputpointer^)=0) and not(endoffile) then
  555. begin
  556. cur:=c;
  557. reload;
  558. if byte(cur)+byte(c)<>23 then
  559. dec(longint(inputpointer));
  560. end
  561. else
  562. begin
  563. { Fix linebreak to be only newline (=#10) for all types of linebreaks }
  564. if (byte(inputpointer^)+byte(c)=23) then
  565. inc(longint(inputpointer));
  566. end;
  567. c:=newline;
  568. { increase line counters }
  569. lastlinepos:=bufstart+(inputpointer-inputbuffer);
  570. inc(line_no);
  571. { update linebuffer }
  572. if cs_asm_source in aktglobalswitches then
  573. inputfile^.setline(line_no,lastlinepos);
  574. { update for status and call the show status routine,
  575. but don't touch aktfilepos ! }
  576. oldaktfilepos:=aktfilepos;
  577. oldtokenpos:=akttokenpos;
  578. gettokenpos; { update for v_status }
  579. inc(status.compiledlines);
  580. ShowStatus;
  581. aktfilepos:=oldaktfilepos;
  582. akttokenpos:=oldtokenpos;
  583. end;
  584. end;
  585. procedure tscannerfile.illegal_char(c:char);
  586. var
  587. s : string;
  588. begin
  589. if c in [#32..#255] then
  590. s:=''''+c+''''
  591. else
  592. s:='#'+tostr(ord(c));
  593. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  594. end;
  595. procedure tscannerfile.end_of_file;
  596. begin
  597. checkpreprocstack;
  598. Message(scan_f_end_of_file);
  599. end;
  600. procedure tscannerfile.checkpreprocstack;
  601. begin
  602. { check for missing ifdefs }
  603. while assigned(preprocstack) do
  604. begin
  605. Message3(scan_e_endif_expected,preprocstring[preprocstack^.typ],preprocstack^.name,tostr(preprocstack^.line_nb));
  606. poppreprocstack;
  607. end;
  608. end;
  609. procedure tscannerfile.poppreprocstack;
  610. var
  611. hp : ppreprocstack;
  612. begin
  613. if assigned(preprocstack) then
  614. begin
  615. Message1(scan_c_endif_found,preprocstack^.name);
  616. hp:=preprocstack^.next;
  617. dispose(preprocstack,done);
  618. preprocstack:=hp;
  619. end
  620. else
  621. Message(scan_e_endif_without_if);
  622. end;
  623. procedure tscannerfile.addpreprocstack(atyp : preproctyp;a:boolean;const s:string;w:longint);
  624. begin
  625. preprocstack:=new(ppreprocstack,init(atyp,((preprocstack=nil) or preprocstack^.accept) and a,preprocstack));
  626. preprocstack^.name:=s;
  627. preprocstack^.line_nb:=line_no;
  628. if preprocstack^.accept then
  629. Message2(w,preprocstack^.name,'accepted')
  630. else
  631. Message2(w,preprocstack^.name,'rejected');
  632. end;
  633. procedure tscannerfile.elsepreprocstack;
  634. begin
  635. if assigned(preprocstack) then
  636. begin
  637. preprocstack^.typ:=pp_else;
  638. preprocstack^.line_nb:=line_no;
  639. if not(assigned(preprocstack^.next)) or (preprocstack^.next^.accept) then
  640. preprocstack^.accept:=not preprocstack^.accept;
  641. if preprocstack^.accept then
  642. Message2(scan_c_else_found,preprocstack^.name,'accepted')
  643. else
  644. Message2(scan_c_else_found,preprocstack^.name,'rejected');
  645. end
  646. else
  647. Message(scan_e_endif_without_if);
  648. end;
  649. procedure tscannerfile.readchar;
  650. begin
  651. c:=inputpointer^;
  652. if c=#0 then
  653. reload
  654. else
  655. inc(longint(inputpointer));
  656. case c of
  657. #26 : reload;
  658. #10,
  659. #13 : linebreak;
  660. end;
  661. end;
  662. procedure tscannerfile.readstring;
  663. var
  664. i : longint;
  665. begin
  666. i:=0;
  667. repeat
  668. case c of
  669. '_',
  670. '0'..'9',
  671. 'A'..'Z' : begin
  672. if i<255 then
  673. begin
  674. inc(i);
  675. orgpattern[i]:=c;
  676. pattern[i]:=c;
  677. end;
  678. c:=inputpointer^;
  679. inc(longint(inputpointer));
  680. end;
  681. 'a'..'z' : begin
  682. if i<255 then
  683. begin
  684. inc(i);
  685. orgpattern[i]:=c;
  686. pattern[i]:=chr(ord(c)-32)
  687. end;
  688. c:=inputpointer^;
  689. inc(longint(inputpointer));
  690. end;
  691. #0 : reload;
  692. #26 : begin
  693. reload;
  694. if c=#26 then
  695. break;
  696. end;
  697. #13,#10 : begin
  698. linebreak;
  699. break;
  700. end;
  701. else
  702. break;
  703. end;
  704. until false;
  705. orgpattern[0]:=chr(i);
  706. pattern[0]:=chr(i);
  707. end;
  708. procedure tscannerfile.readnumber;
  709. var
  710. base,
  711. i : longint;
  712. begin
  713. case c of
  714. '%' : begin
  715. readchar;
  716. base:=2;
  717. pattern[1]:='%';
  718. i:=1;
  719. end;
  720. '$' : begin
  721. readchar;
  722. base:=16;
  723. pattern[1]:='$';
  724. i:=1;
  725. end;
  726. else
  727. begin
  728. base:=10;
  729. i:=0;
  730. end;
  731. end;
  732. while ((base>=10) and (c in ['0'..'9'])) or
  733. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  734. ((base=2) and (c in ['0'..'1'])) do
  735. begin
  736. if i<255 then
  737. begin
  738. inc(i);
  739. pattern[i]:=c;
  740. end;
  741. { get next char }
  742. c:=inputpointer^;
  743. if c=#0 then
  744. reload
  745. else
  746. inc(longint(inputpointer));
  747. end;
  748. { was the next char a linebreak ? }
  749. case c of
  750. #26 : reload;
  751. #10,
  752. #13 : linebreak;
  753. end;
  754. pattern[0]:=chr(i);
  755. end;
  756. function tscannerfile.readid:string;
  757. begin
  758. readstring;
  759. readid:=pattern;
  760. end;
  761. function tscannerfile.readval:longint;
  762. var
  763. l : longint;
  764. w : integer;
  765. begin
  766. readnumber;
  767. valint(pattern,l,w);
  768. readval:=l;
  769. end;
  770. function tscannerfile.readcomment:string;
  771. var
  772. i : longint;
  773. begin
  774. i:=0;
  775. repeat
  776. case c of
  777. '{' :
  778. if aktcommentstyle=comment_tp then
  779. inc_comment_level;
  780. '}' :
  781. if aktcommentstyle=comment_tp then
  782. begin
  783. readchar;
  784. dec_comment_level;
  785. if comment_level=0 then
  786. break
  787. else
  788. continue;
  789. end;
  790. '*' :
  791. if aktcommentstyle=comment_oldtp then
  792. begin
  793. readchar;
  794. if c=')' then
  795. begin
  796. readchar;
  797. dec_comment_level;
  798. break;
  799. end
  800. else
  801. { Add both characters !!}
  802. if (i<255) then
  803. begin
  804. inc(i);
  805. readcomment[i]:='*';
  806. if (i<255) then
  807. begin
  808. inc(i);
  809. readcomment[i]:='*';
  810. end;
  811. end;
  812. end
  813. else
  814. { Not old TP comment, so add...}
  815. begin
  816. if (i<255) then
  817. begin
  818. inc(i);
  819. readcomment[i]:='*';
  820. end;
  821. end;
  822. #26 :
  823. end_of_file;
  824. else
  825. begin
  826. if (i<255) then
  827. begin
  828. inc(i);
  829. readcomment[i]:=c;
  830. end;
  831. end;
  832. end;
  833. c:=inputpointer^;
  834. if c=#0 then
  835. reload
  836. else
  837. inc(longint(inputpointer));
  838. if c in [#10,#13] then
  839. linebreak;
  840. until false;
  841. readcomment[0]:=chr(i);
  842. end;
  843. function tscannerfile.readstate:char;
  844. var
  845. state : char;
  846. begin
  847. state:=' ';
  848. if c=' ' then
  849. begin
  850. current_scanner^.skipspace;
  851. current_scanner^.readid;
  852. if pattern='ON' then
  853. state:='+'
  854. else
  855. if pattern='OFF' then
  856. state:='-';
  857. end
  858. else
  859. state:=c;
  860. if not (state in ['+','-']) then
  861. Message(scan_e_wrong_switch_toggle);
  862. readstate:=state;
  863. end;
  864. procedure tscannerfile.skipspace;
  865. begin
  866. while c in [' ',#9..#13] do
  867. begin
  868. c:=inputpointer^;
  869. if c=#0 then
  870. reload
  871. else
  872. inc(longint(inputpointer));
  873. case c of
  874. #26 :
  875. reload;
  876. #10,
  877. #13 :
  878. linebreak;
  879. end;
  880. end;
  881. end;
  882. procedure tscannerfile.skipuntildirective;
  883. var
  884. incomment : boolean;
  885. found : longint;
  886. next_char_loaded : boolean;
  887. oldcommentstyle : tcommentstyle;
  888. begin
  889. found:=0;
  890. next_char_loaded:=false;
  891. incomment:=true;
  892. oldcommentstyle:=aktcommentstyle;
  893. repeat
  894. case c of
  895. #26 :
  896. end_of_file;
  897. '{' :
  898. begin
  899. if not(m_nested_comment in aktmodeswitches) or
  900. (comment_level=0) then
  901. begin
  902. found:=1;
  903. aktcommentstyle:=comment_tp;
  904. end;
  905. inc_comment_level;
  906. incomment:=true;
  907. end;
  908. '}' :
  909. begin
  910. dec_comment_level;
  911. found:=0;
  912. incomment:=false;
  913. end;
  914. '$' :
  915. begin
  916. if found=1 then
  917. found:=2;
  918. end;
  919. '''' :
  920. if not incomment then
  921. begin
  922. repeat
  923. readchar;
  924. case c of
  925. #26 :
  926. end_of_file;
  927. newline :
  928. break;
  929. '''' :
  930. begin
  931. readchar;
  932. if c<>'''' then
  933. begin
  934. next_char_loaded:=true;
  935. break;
  936. end;
  937. end;
  938. end;
  939. until false;
  940. end;
  941. '(' :
  942. begin
  943. readchar;
  944. if c='*' then
  945. begin
  946. readchar;
  947. if c='$' then
  948. begin
  949. found:=2;
  950. inc_comment_level;
  951. aktcommentstyle:=comment_oldtp;
  952. end
  953. else
  954. begin
  955. skipoldtpcomment;
  956. aktcommentstyle:=oldcommentstyle;
  957. end;
  958. end
  959. else
  960. next_char_loaded:=true;
  961. end;
  962. else
  963. found:=0;
  964. end;
  965. if next_char_loaded then
  966. next_char_loaded:=false
  967. else
  968. begin
  969. c:=inputpointer^;
  970. if c=#0 then
  971. reload
  972. else
  973. inc(longint(inputpointer));
  974. case c of
  975. #26 : reload;
  976. #10,
  977. #13 : linebreak;
  978. end;
  979. end;
  980. until (found=2);
  981. end;
  982. {****************************************************************************
  983. Include directive scanning/parsing
  984. ****************************************************************************}
  985. {$i scandir.inc}
  986. {****************************************************************************
  987. Comment Handling
  988. ****************************************************************************}
  989. procedure tscannerfile.skipcomment;
  990. begin
  991. aktcommentstyle:=comment_tp;
  992. readchar;
  993. inc_comment_level;
  994. { handle compiler switches }
  995. if (c='$') then
  996. handledirectives;
  997. { handle_switches can dec comment_level, }
  998. while (comment_level>0) do
  999. begin
  1000. case c of
  1001. '{' : inc_comment_level;
  1002. '}' : dec_comment_level;
  1003. #26 : end_of_file;
  1004. end;
  1005. c:=inputpointer^;
  1006. if c=#0 then
  1007. reload
  1008. else
  1009. inc(longint(inputpointer));
  1010. case c of
  1011. #26 : reload;
  1012. #10,
  1013. #13 : linebreak;
  1014. end;
  1015. end;
  1016. aktcommentstyle:=comment_none;
  1017. end;
  1018. procedure tscannerfile.skipdelphicomment;
  1019. begin
  1020. aktcommentstyle:=comment_delphi;
  1021. inc_comment_level;
  1022. readchar;
  1023. { this is currently not supported }
  1024. if c='$' then
  1025. Message(scan_e_wrong_styled_switch);
  1026. { skip comment }
  1027. while c<>newline do
  1028. begin
  1029. if c=#26 then
  1030. end_of_file;
  1031. readchar;
  1032. end;
  1033. dec_comment_level;
  1034. aktcommentstyle:=comment_none;
  1035. end;
  1036. procedure tscannerfile.skipoldtpcomment;
  1037. var
  1038. found : longint;
  1039. begin
  1040. aktcommentstyle:=comment_oldtp;
  1041. inc_comment_level;
  1042. readchar;
  1043. { this is currently not supported }
  1044. if (c='$') then
  1045. handledirectives;
  1046. { skip comment }
  1047. while (comment_level>0) do
  1048. begin
  1049. found:=0;
  1050. repeat
  1051. case c of
  1052. #26 :
  1053. end_of_file;
  1054. '*' :
  1055. begin
  1056. if found=3 then
  1057. found:=4
  1058. else
  1059. found:=1;
  1060. end;
  1061. ')' :
  1062. begin
  1063. if found in [1,4] then
  1064. begin
  1065. dec_comment_level;
  1066. if comment_level=0 then
  1067. found:=2
  1068. else
  1069. found:=0;
  1070. end;
  1071. end;
  1072. '(' :
  1073. begin
  1074. if found=4 then
  1075. inc_comment_level;
  1076. found:=3;
  1077. end;
  1078. else
  1079. begin
  1080. if found=4 then
  1081. inc_comment_level;
  1082. found:=0;
  1083. end;
  1084. end;
  1085. c:=inputpointer^;
  1086. if c=#0 then
  1087. reload
  1088. else
  1089. inc(longint(inputpointer));
  1090. case c of
  1091. #26 : reload;
  1092. #10,
  1093. #13 : linebreak;
  1094. end;
  1095. until (found=2);
  1096. end;
  1097. aktcommentstyle:=comment_none;
  1098. end;
  1099. {****************************************************************************
  1100. Token Scanner
  1101. ****************************************************************************}
  1102. procedure tscannerfile.readtoken;
  1103. var
  1104. code : integer;
  1105. low,high,mid : longint;
  1106. m : longint;
  1107. mac : pmacro;
  1108. asciinr : string[6];
  1109. iswidestring : boolean;
  1110. label
  1111. exit_label;
  1112. begin
  1113. if localswitcheschanged then
  1114. begin
  1115. aktlocalswitches:=nextaktlocalswitches;
  1116. localswitcheschanged:=false;
  1117. end;
  1118. { was there already a token read, then return that token }
  1119. if nexttoken<>NOTOKEN then
  1120. begin
  1121. token:=nexttoken;
  1122. nexttoken:=NOTOKEN;
  1123. goto exit_label;
  1124. end;
  1125. { Skip all spaces and comments }
  1126. repeat
  1127. case c of
  1128. '{' :
  1129. skipcomment;
  1130. ' ',#9..#13 :
  1131. begin
  1132. if parapreprocess then
  1133. begin
  1134. if c=#10 then
  1135. preprocfile^.eolfound:=true
  1136. else
  1137. preprocfile^.spacefound:=true;
  1138. end;
  1139. skipspace;
  1140. end
  1141. else
  1142. break;
  1143. end;
  1144. until false;
  1145. { Save current token position, for EOF its already loaded }
  1146. if c<>#26 then
  1147. gettokenpos;
  1148. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  1149. if c in ['A'..'Z','a'..'z','_'] then
  1150. begin
  1151. readstring;
  1152. token:=_ID;
  1153. idtoken:=_ID;
  1154. { keyword or any other known token,
  1155. pattern is always uppercased }
  1156. if (pattern[1]<>'_') and (length(pattern) in [2..tokenidlen]) then
  1157. begin
  1158. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  1159. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  1160. while low<high do
  1161. begin
  1162. mid:=(high+low+1) shr 1;
  1163. if pattern<tokeninfo^[ttoken(mid)].str then
  1164. high:=mid-1
  1165. else
  1166. low:=mid;
  1167. end;
  1168. if pattern=tokeninfo^[ttoken(high)].str then
  1169. begin
  1170. if tokeninfo^[ttoken(high)].keyword in aktmodeswitches then
  1171. if tokeninfo^[ttoken(high)].op=NOTOKEN then
  1172. token:=ttoken(high)
  1173. else
  1174. token:=tokeninfo^[ttoken(high)].op;
  1175. idtoken:=ttoken(high);
  1176. end;
  1177. end;
  1178. { Only process identifiers and not keywords }
  1179. if token=_ID then
  1180. begin
  1181. { this takes some time ... }
  1182. if (cs_support_macro in aktmoduleswitches) then
  1183. begin
  1184. mac:=pmacro(macros^.search(pattern));
  1185. if assigned(mac) and (assigned(mac^.buftext)) then
  1186. begin
  1187. insertmacro(pattern,mac^.buftext,mac^.buflen);
  1188. { handle empty macros }
  1189. if c=#0 then
  1190. begin
  1191. reload;
  1192. case c of
  1193. #26 : reload;
  1194. #10,
  1195. #13 : linebreak;
  1196. end;
  1197. end;
  1198. { play it again ... }
  1199. inc(yylexcount);
  1200. if yylexcount>16 then
  1201. Message(scan_w_macro_deep_ten);
  1202. readtoken;
  1203. { that's all folks }
  1204. dec(yylexcount);
  1205. exit;
  1206. end;
  1207. end;
  1208. end;
  1209. { return token }
  1210. goto exit_label;
  1211. end
  1212. else
  1213. begin
  1214. idtoken:=_NOID;
  1215. case c of
  1216. '$' :
  1217. begin
  1218. readnumber;
  1219. token:=_INTCONST;
  1220. goto exit_label;
  1221. end;
  1222. '%' :
  1223. begin
  1224. if (m_tp in aktmodeswitches) then
  1225. Illegal_Char(c)
  1226. else
  1227. begin
  1228. readnumber;
  1229. token:=_INTCONST;
  1230. goto exit_label;
  1231. end;
  1232. end;
  1233. '0'..'9' :
  1234. begin
  1235. readnumber;
  1236. if (c in ['.','e','E']) then
  1237. begin
  1238. { first check for a . }
  1239. if c='.' then
  1240. begin
  1241. readchar;
  1242. { is it a .. from a range? }
  1243. case c of
  1244. '.' :
  1245. begin
  1246. readchar;
  1247. token:=_INTCONST;
  1248. nexttoken:=_POINTPOINT;
  1249. goto exit_label;
  1250. end;
  1251. ')' :
  1252. begin
  1253. readchar;
  1254. token:=_INTCONST;
  1255. nexttoken:=_RECKKLAMMER;
  1256. goto exit_label;
  1257. end;
  1258. end;
  1259. { insert the number after the . }
  1260. pattern:=pattern+'.';
  1261. while c in ['0'..'9'] do
  1262. begin
  1263. pattern:=pattern+c;
  1264. readchar;
  1265. end;
  1266. end;
  1267. { E can also follow after a point is scanned }
  1268. if c in ['e','E'] then
  1269. begin
  1270. pattern:=pattern+'E';
  1271. readchar;
  1272. if c in ['-','+'] then
  1273. begin
  1274. pattern:=pattern+c;
  1275. readchar;
  1276. end;
  1277. if not(c in ['0'..'9']) then
  1278. Illegal_Char(c);
  1279. while c in ['0'..'9'] do
  1280. begin
  1281. pattern:=pattern+c;
  1282. readchar;
  1283. end;
  1284. end;
  1285. token:=_REALNUMBER;
  1286. goto exit_label;
  1287. end;
  1288. token:=_INTCONST;
  1289. goto exit_label;
  1290. end;
  1291. ';' :
  1292. begin
  1293. readchar;
  1294. token:=_SEMICOLON;
  1295. goto exit_label;
  1296. end;
  1297. '[' :
  1298. begin
  1299. readchar;
  1300. token:=_LECKKLAMMER;
  1301. goto exit_label;
  1302. end;
  1303. ']' :
  1304. begin
  1305. readchar;
  1306. token:=_RECKKLAMMER;
  1307. goto exit_label;
  1308. end;
  1309. '(' :
  1310. begin
  1311. readchar;
  1312. case c of
  1313. '*' :
  1314. begin
  1315. skipoldtpcomment;
  1316. readtoken;
  1317. exit;
  1318. end;
  1319. '.' :
  1320. begin
  1321. readchar;
  1322. token:=_LECKKLAMMER;
  1323. goto exit_label;
  1324. end;
  1325. end;
  1326. token:=_LKLAMMER;
  1327. goto exit_label;
  1328. end;
  1329. ')' :
  1330. begin
  1331. readchar;
  1332. token:=_RKLAMMER;
  1333. goto exit_label;
  1334. end;
  1335. '+' :
  1336. begin
  1337. readchar;
  1338. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  1339. begin
  1340. readchar;
  1341. token:=_PLUSASN;
  1342. goto exit_label;
  1343. end;
  1344. token:=_PLUS;
  1345. goto exit_label;
  1346. end;
  1347. '-' :
  1348. begin
  1349. readchar;
  1350. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  1351. begin
  1352. readchar;
  1353. token:=_MINUSASN;
  1354. goto exit_label;
  1355. end;
  1356. token:=_MINUS;
  1357. goto exit_label;
  1358. end;
  1359. ':' :
  1360. begin
  1361. readchar;
  1362. if c='=' then
  1363. begin
  1364. readchar;
  1365. token:=_ASSIGNMENT;
  1366. goto exit_label;
  1367. end;
  1368. token:=_COLON;
  1369. goto exit_label;
  1370. end;
  1371. '*' :
  1372. begin
  1373. readchar;
  1374. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  1375. begin
  1376. readchar;
  1377. token:=_STARASN;
  1378. end
  1379. else
  1380. if c='*' then
  1381. begin
  1382. readchar;
  1383. token:=_STARSTAR;
  1384. end
  1385. else
  1386. token:=_STAR;
  1387. goto exit_label;
  1388. end;
  1389. '/' :
  1390. begin
  1391. readchar;
  1392. case c of
  1393. '=' :
  1394. begin
  1395. if (cs_support_c_operators in aktmoduleswitches) then
  1396. begin
  1397. readchar;
  1398. token:=_SLASHASN;
  1399. goto exit_label;
  1400. end;
  1401. end;
  1402. '/' :
  1403. begin
  1404. skipdelphicomment;
  1405. readtoken;
  1406. exit;
  1407. end;
  1408. end;
  1409. token:=_SLASH;
  1410. goto exit_label;
  1411. end;
  1412. '=' :
  1413. begin
  1414. readchar;
  1415. token:=_EQUAL;
  1416. goto exit_label;
  1417. end;
  1418. '.' :
  1419. begin
  1420. readchar;
  1421. case c of
  1422. '.' :
  1423. begin
  1424. readchar;
  1425. token:=_POINTPOINT;
  1426. goto exit_label;
  1427. end;
  1428. ')' :
  1429. begin
  1430. readchar;
  1431. token:=_RECKKLAMMER;
  1432. goto exit_label;
  1433. end;
  1434. end;
  1435. token:=_POINT;
  1436. goto exit_label;
  1437. end;
  1438. '@' :
  1439. begin
  1440. readchar;
  1441. if c='@' then
  1442. begin
  1443. readchar;
  1444. token:=_DOUBLEADDR;
  1445. end
  1446. else
  1447. token:=_KLAMMERAFFE;
  1448. goto exit_label;
  1449. end;
  1450. ',' :
  1451. begin
  1452. readchar;
  1453. token:=_COMMA;
  1454. goto exit_label;
  1455. end;
  1456. '''','#','^' :
  1457. begin
  1458. iswidestring:=false;
  1459. if c='^' then
  1460. begin
  1461. readchar;
  1462. c:=upcase(c);
  1463. if (block_type=bt_type) or
  1464. (lasttoken=_ID) or
  1465. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  1466. begin
  1467. token:=_CARET;
  1468. goto exit_label;
  1469. end
  1470. else
  1471. begin
  1472. if c<#64 then
  1473. pattern:=chr(ord(c)+64)
  1474. else
  1475. pattern:=chr(ord(c)-64);
  1476. readchar;
  1477. end;
  1478. end
  1479. else
  1480. pattern:='';
  1481. repeat
  1482. case c of
  1483. '#' :
  1484. begin
  1485. readchar; { read # }
  1486. if c='$' then
  1487. begin
  1488. readchar; { read leading $ }
  1489. asciinr:='$';
  1490. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<6) do
  1491. begin
  1492. asciinr:=asciinr+c;
  1493. readchar;
  1494. end;
  1495. end
  1496. else
  1497. begin
  1498. asciinr:='';
  1499. while (c in ['0'..'9']) and (length(asciinr)<6) do
  1500. begin
  1501. asciinr:=asciinr+c;
  1502. readchar;
  1503. end;
  1504. end;
  1505. valint(asciinr,m,code);
  1506. if (asciinr='') or (code<>0) then
  1507. Message(scan_e_illegal_char_const)
  1508. else if (m<0) or (m>255) then
  1509. begin
  1510. if (m>=0) and (m<=65535) then
  1511. begin
  1512. ascii2unicode(pattern,patternw);
  1513. concatwidestringchar(patternw,
  1514. tcompilerwidechar(m));
  1515. iswidestring:=true;
  1516. end
  1517. else
  1518. Message(scan_e_illegal_char_const)
  1519. end
  1520. else if iswidestring then
  1521. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  1522. else
  1523. pattern:=pattern+chr(m);
  1524. end;
  1525. '''' :
  1526. begin
  1527. repeat
  1528. readchar;
  1529. case c of
  1530. #26 :
  1531. end_of_file;
  1532. newline :
  1533. Message(scan_f_string_exceeds_line);
  1534. '''' :
  1535. begin
  1536. readchar;
  1537. if c<>'''' then
  1538. break;
  1539. end;
  1540. end;
  1541. if iswidestring then
  1542. concatwidestringchar(patternw,
  1543. asciichar2unicode(c))
  1544. else
  1545. pattern:=pattern+c;
  1546. until false;
  1547. end;
  1548. '^' :
  1549. begin
  1550. readchar;
  1551. c:=upcase(c);
  1552. if c<#64 then
  1553. c:=chr(ord(c)+64)
  1554. else
  1555. c:=chr(ord(c)-64);
  1556. if iswidestring then
  1557. concatwidestringchar(patternw,
  1558. asciichar2unicode(c))
  1559. else
  1560. pattern:=pattern+c;
  1561. readchar;
  1562. end;
  1563. else
  1564. break;
  1565. end;
  1566. until false;
  1567. { strings with length 1 become const chars }
  1568. if iswidestring then
  1569. begin
  1570. if length(pattern)=1 then
  1571. token:=_CWCHAR
  1572. else
  1573. token:=_CWSTRING;
  1574. end
  1575. else
  1576. begin
  1577. if length(pattern)=1 then
  1578. token:=_CCHAR
  1579. else
  1580. token:=_CSTRING;
  1581. end;
  1582. goto exit_label;
  1583. end;
  1584. '>' :
  1585. begin
  1586. readchar;
  1587. case c of
  1588. '=' :
  1589. begin
  1590. readchar;
  1591. token:=_GTE;
  1592. goto exit_label;
  1593. end;
  1594. '>' :
  1595. begin
  1596. readchar;
  1597. token:=_OP_SHR;
  1598. goto exit_label;
  1599. end;
  1600. '<' :
  1601. begin { >< is for a symetric diff for sets }
  1602. readchar;
  1603. token:=_SYMDIF;
  1604. goto exit_label;
  1605. end;
  1606. end;
  1607. token:=_GT;
  1608. goto exit_label;
  1609. end;
  1610. '<' :
  1611. begin
  1612. readchar;
  1613. case c of
  1614. '>' :
  1615. begin
  1616. readchar;
  1617. token:=_UNEQUAL;
  1618. goto exit_label;
  1619. end;
  1620. '=' :
  1621. begin
  1622. readchar;
  1623. token:=_LTE;
  1624. goto exit_label;
  1625. end;
  1626. '<' :
  1627. begin
  1628. readchar;
  1629. token:=_OP_SHL;
  1630. goto exit_label;
  1631. end;
  1632. end;
  1633. token:=_LT;
  1634. goto exit_label;
  1635. end;
  1636. #26 :
  1637. begin
  1638. token:=_EOF;
  1639. checkpreprocstack;
  1640. goto exit_label;
  1641. end;
  1642. else
  1643. Illegal_Char(c);
  1644. end;
  1645. end;
  1646. exit_label:
  1647. lasttoken:=token;
  1648. end;
  1649. function tscannerfile.readpreproc:ttoken;
  1650. begin
  1651. skipspace;
  1652. case c of
  1653. 'A'..'Z',
  1654. 'a'..'z',
  1655. '_','0'..'9' : begin
  1656. preprocpat:=readid;
  1657. readpreproc:=_ID;
  1658. end;
  1659. '}' : begin
  1660. readpreproc:=_END;
  1661. end;
  1662. '(' : begin
  1663. readchar;
  1664. readpreproc:=_LKLAMMER;
  1665. end;
  1666. ')' : begin
  1667. readchar;
  1668. readpreproc:=_RKLAMMER;
  1669. end;
  1670. '+' : begin
  1671. readchar;
  1672. readpreproc:=_PLUS;
  1673. end;
  1674. '-' : begin
  1675. readchar;
  1676. readpreproc:=_MINUS;
  1677. end;
  1678. '*' : begin
  1679. readchar;
  1680. readpreproc:=_STAR;
  1681. end;
  1682. '/' : begin
  1683. readchar;
  1684. readpreproc:=_SLASH;
  1685. end;
  1686. '=' : begin
  1687. readchar;
  1688. readpreproc:=_EQUAL;
  1689. end;
  1690. '>' : begin
  1691. readchar;
  1692. if c='=' then
  1693. begin
  1694. readchar;
  1695. readpreproc:=_GTE;
  1696. end
  1697. else
  1698. readpreproc:=_GT;
  1699. end;
  1700. '<' : begin
  1701. readchar;
  1702. case c of
  1703. '>' : begin
  1704. readchar;
  1705. readpreproc:=_UNEQUAL;
  1706. end;
  1707. '=' : begin
  1708. readchar;
  1709. readpreproc:=_LTE;
  1710. end;
  1711. else readpreproc:=_LT;
  1712. end;
  1713. end;
  1714. #26 :
  1715. end_of_file;
  1716. else
  1717. begin
  1718. readpreproc:=_EOF;
  1719. checkpreprocstack;
  1720. end;
  1721. end;
  1722. end;
  1723. function tscannerfile.asmgetchar : char;
  1724. begin
  1725. if lastasmgetchar<>#0 then
  1726. begin
  1727. c:=lastasmgetchar;
  1728. lastasmgetchar:=#0;
  1729. end
  1730. else
  1731. readchar;
  1732. if in_asm_string then
  1733. begin
  1734. asmgetchar:=c;
  1735. exit;
  1736. end;
  1737. case c of
  1738. '{' : begin
  1739. skipcomment;
  1740. asmgetchar:=c;
  1741. exit;
  1742. end;
  1743. '/' : begin
  1744. readchar;
  1745. if c='/' then
  1746. begin
  1747. skipdelphicomment;
  1748. asmgetchar:=c;
  1749. end
  1750. else
  1751. begin
  1752. asmgetchar:='/';
  1753. lastasmgetchar:=c;
  1754. end;
  1755. exit;
  1756. end;
  1757. '(' : begin
  1758. readchar;
  1759. if c='*' then
  1760. begin
  1761. skipoldtpcomment;
  1762. asmgetchar:=c;
  1763. end
  1764. else
  1765. begin
  1766. asmgetchar:='(';
  1767. lastasmgetchar:=c;
  1768. end;
  1769. exit;
  1770. end;
  1771. else
  1772. begin
  1773. asmgetchar:=c;
  1774. end;
  1775. end;
  1776. end;
  1777. end.
  1778. {
  1779. $Log$
  1780. Revision 1.11 2000-12-18 17:59:01 peter
  1781. * fixed skipuntildirective
  1782. Revision 1.10 2000/12/16 15:36:02 peter
  1783. * fixed parsing of strings and comments in skipuntildirective
  1784. Revision 1.9 2000/11/30 20:27:51 peter
  1785. * merged fix for bug 1229
  1786. Revision 1.8 2000/11/29 00:30:40 florian
  1787. * unused units removed from uses clause
  1788. * some changes for widestrings
  1789. Revision 1.7 2000/10/31 22:02:51 peter
  1790. * symtable splitted, no real code changes
  1791. Revision 1.6 2000/09/24 15:06:28 peter
  1792. * use defines.inc
  1793. Revision 1.5 2000/08/27 16:11:53 peter
  1794. * moved some util functions from globals,cobjects to cutils
  1795. * splitted files into finput,fmodule
  1796. Revision 1.4 2000/08/12 15:30:44 peter
  1797. * IDE patch for stream reading (merged)
  1798. Revision 1.3 2000/08/08 19:28:57 peter
  1799. * memdebug/memory patches (merged)
  1800. * only once illegal directive (merged)
  1801. Revision 1.2 2000/07/13 11:32:49 michael
  1802. + removed logs
  1803. }