scanner.pas 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  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. break;
  934. end;
  935. end;
  936. until false;
  937. end;
  938. '(' :
  939. begin
  940. readchar;
  941. if c='*' then
  942. begin
  943. readchar;
  944. if c='$' then
  945. begin
  946. found:=2;
  947. inc_comment_level;
  948. aktcommentstyle:=comment_oldtp;
  949. end
  950. else
  951. begin
  952. skipoldtpcomment;
  953. aktcommentstyle:=oldcommentstyle;
  954. end;
  955. end
  956. else
  957. next_char_loaded:=true;
  958. end;
  959. else
  960. found:=0;
  961. end;
  962. if next_char_loaded then
  963. next_char_loaded:=false
  964. else
  965. begin
  966. c:=inputpointer^;
  967. if c=#0 then
  968. reload
  969. else
  970. inc(longint(inputpointer));
  971. case c of
  972. #26 : reload;
  973. #10,
  974. #13 : linebreak;
  975. end;
  976. end;
  977. until (found=2);
  978. end;
  979. {****************************************************************************
  980. Include directive scanning/parsing
  981. ****************************************************************************}
  982. {$i scandir.inc}
  983. {****************************************************************************
  984. Comment Handling
  985. ****************************************************************************}
  986. procedure tscannerfile.skipcomment;
  987. begin
  988. aktcommentstyle:=comment_tp;
  989. readchar;
  990. inc_comment_level;
  991. { handle compiler switches }
  992. if (c='$') then
  993. handledirectives;
  994. { handle_switches can dec comment_level, }
  995. while (comment_level>0) do
  996. begin
  997. case c of
  998. '{' : inc_comment_level;
  999. '}' : dec_comment_level;
  1000. #26 : end_of_file;
  1001. end;
  1002. c:=inputpointer^;
  1003. if c=#0 then
  1004. reload
  1005. else
  1006. inc(longint(inputpointer));
  1007. case c of
  1008. #26 : reload;
  1009. #10,
  1010. #13 : linebreak;
  1011. end;
  1012. end;
  1013. aktcommentstyle:=comment_none;
  1014. end;
  1015. procedure tscannerfile.skipdelphicomment;
  1016. begin
  1017. aktcommentstyle:=comment_delphi;
  1018. inc_comment_level;
  1019. readchar;
  1020. { this is currently not supported }
  1021. if c='$' then
  1022. Message(scan_e_wrong_styled_switch);
  1023. { skip comment }
  1024. while c<>newline do
  1025. begin
  1026. if c=#26 then
  1027. end_of_file;
  1028. readchar;
  1029. end;
  1030. dec_comment_level;
  1031. aktcommentstyle:=comment_none;
  1032. end;
  1033. procedure tscannerfile.skipoldtpcomment;
  1034. var
  1035. found : longint;
  1036. begin
  1037. aktcommentstyle:=comment_oldtp;
  1038. inc_comment_level;
  1039. readchar;
  1040. { this is currently not supported }
  1041. if (c='$') then
  1042. handledirectives;
  1043. { skip comment }
  1044. while (comment_level>0) do
  1045. begin
  1046. found:=0;
  1047. repeat
  1048. case c of
  1049. #26 :
  1050. end_of_file;
  1051. '*' :
  1052. begin
  1053. if found=3 then
  1054. found:=4
  1055. else
  1056. found:=1;
  1057. end;
  1058. ')' :
  1059. begin
  1060. if found in [1,4] then
  1061. begin
  1062. dec_comment_level;
  1063. if comment_level=0 then
  1064. found:=2
  1065. else
  1066. found:=0;
  1067. end;
  1068. end;
  1069. '(' :
  1070. begin
  1071. if found=4 then
  1072. inc_comment_level;
  1073. found:=3;
  1074. end;
  1075. else
  1076. begin
  1077. if found=4 then
  1078. inc_comment_level;
  1079. found:=0;
  1080. end;
  1081. end;
  1082. c:=inputpointer^;
  1083. if c=#0 then
  1084. reload
  1085. else
  1086. inc(longint(inputpointer));
  1087. case c of
  1088. #26 : reload;
  1089. #10,
  1090. #13 : linebreak;
  1091. end;
  1092. until (found=2);
  1093. end;
  1094. aktcommentstyle:=comment_none;
  1095. end;
  1096. {****************************************************************************
  1097. Token Scanner
  1098. ****************************************************************************}
  1099. procedure tscannerfile.readtoken;
  1100. var
  1101. code : integer;
  1102. low,high,mid : longint;
  1103. m : longint;
  1104. mac : pmacro;
  1105. asciinr : string[6];
  1106. iswidestring : boolean;
  1107. label
  1108. exit_label;
  1109. begin
  1110. if localswitcheschanged then
  1111. begin
  1112. aktlocalswitches:=nextaktlocalswitches;
  1113. localswitcheschanged:=false;
  1114. end;
  1115. { was there already a token read, then return that token }
  1116. if nexttoken<>NOTOKEN then
  1117. begin
  1118. token:=nexttoken;
  1119. nexttoken:=NOTOKEN;
  1120. goto exit_label;
  1121. end;
  1122. { Skip all spaces and comments }
  1123. repeat
  1124. case c of
  1125. '{' :
  1126. skipcomment;
  1127. ' ',#9..#13 :
  1128. begin
  1129. if parapreprocess then
  1130. begin
  1131. if c=#10 then
  1132. preprocfile^.eolfound:=true
  1133. else
  1134. preprocfile^.spacefound:=true;
  1135. end;
  1136. skipspace;
  1137. end
  1138. else
  1139. break;
  1140. end;
  1141. until false;
  1142. { Save current token position, for EOF its already loaded }
  1143. if c<>#26 then
  1144. gettokenpos;
  1145. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  1146. if c in ['A'..'Z','a'..'z','_'] then
  1147. begin
  1148. readstring;
  1149. token:=_ID;
  1150. idtoken:=_ID;
  1151. { keyword or any other known token,
  1152. pattern is always uppercased }
  1153. if (pattern[1]<>'_') and (length(pattern) in [2..tokenidlen]) then
  1154. begin
  1155. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  1156. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  1157. while low<high do
  1158. begin
  1159. mid:=(high+low+1) shr 1;
  1160. if pattern<tokeninfo^[ttoken(mid)].str then
  1161. high:=mid-1
  1162. else
  1163. low:=mid;
  1164. end;
  1165. if pattern=tokeninfo^[ttoken(high)].str then
  1166. begin
  1167. if tokeninfo^[ttoken(high)].keyword in aktmodeswitches then
  1168. if tokeninfo^[ttoken(high)].op=NOTOKEN then
  1169. token:=ttoken(high)
  1170. else
  1171. token:=tokeninfo^[ttoken(high)].op;
  1172. idtoken:=ttoken(high);
  1173. end;
  1174. end;
  1175. { Only process identifiers and not keywords }
  1176. if token=_ID then
  1177. begin
  1178. { this takes some time ... }
  1179. if (cs_support_macro in aktmoduleswitches) then
  1180. begin
  1181. mac:=pmacro(macros^.search(pattern));
  1182. if assigned(mac) and (assigned(mac^.buftext)) then
  1183. begin
  1184. insertmacro(pattern,mac^.buftext,mac^.buflen);
  1185. { handle empty macros }
  1186. if c=#0 then
  1187. begin
  1188. reload;
  1189. case c of
  1190. #26 : reload;
  1191. #10,
  1192. #13 : linebreak;
  1193. end;
  1194. end;
  1195. { play it again ... }
  1196. inc(yylexcount);
  1197. if yylexcount>16 then
  1198. Message(scan_w_macro_deep_ten);
  1199. readtoken;
  1200. { that's all folks }
  1201. dec(yylexcount);
  1202. exit;
  1203. end;
  1204. end;
  1205. end;
  1206. { return token }
  1207. goto exit_label;
  1208. end
  1209. else
  1210. begin
  1211. idtoken:=_NOID;
  1212. case c of
  1213. '$' :
  1214. begin
  1215. readnumber;
  1216. token:=_INTCONST;
  1217. goto exit_label;
  1218. end;
  1219. '%' :
  1220. begin
  1221. if (m_tp in aktmodeswitches) then
  1222. Illegal_Char(c)
  1223. else
  1224. begin
  1225. readnumber;
  1226. token:=_INTCONST;
  1227. goto exit_label;
  1228. end;
  1229. end;
  1230. '0'..'9' :
  1231. begin
  1232. readnumber;
  1233. if (c in ['.','e','E']) then
  1234. begin
  1235. { first check for a . }
  1236. if c='.' then
  1237. begin
  1238. readchar;
  1239. { is it a .. from a range? }
  1240. case c of
  1241. '.' :
  1242. begin
  1243. readchar;
  1244. token:=_INTCONST;
  1245. nexttoken:=_POINTPOINT;
  1246. goto exit_label;
  1247. end;
  1248. ')' :
  1249. begin
  1250. readchar;
  1251. token:=_INTCONST;
  1252. nexttoken:=_RECKKLAMMER;
  1253. goto exit_label;
  1254. end;
  1255. end;
  1256. { insert the number after the . }
  1257. pattern:=pattern+'.';
  1258. while c in ['0'..'9'] do
  1259. begin
  1260. pattern:=pattern+c;
  1261. readchar;
  1262. end;
  1263. end;
  1264. { E can also follow after a point is scanned }
  1265. if c in ['e','E'] then
  1266. begin
  1267. pattern:=pattern+'E';
  1268. readchar;
  1269. if c in ['-','+'] then
  1270. begin
  1271. pattern:=pattern+c;
  1272. readchar;
  1273. end;
  1274. if not(c in ['0'..'9']) then
  1275. Illegal_Char(c);
  1276. while c in ['0'..'9'] do
  1277. begin
  1278. pattern:=pattern+c;
  1279. readchar;
  1280. end;
  1281. end;
  1282. token:=_REALNUMBER;
  1283. goto exit_label;
  1284. end;
  1285. token:=_INTCONST;
  1286. goto exit_label;
  1287. end;
  1288. ';' :
  1289. begin
  1290. readchar;
  1291. token:=_SEMICOLON;
  1292. goto exit_label;
  1293. end;
  1294. '[' :
  1295. begin
  1296. readchar;
  1297. token:=_LECKKLAMMER;
  1298. goto exit_label;
  1299. end;
  1300. ']' :
  1301. begin
  1302. readchar;
  1303. token:=_RECKKLAMMER;
  1304. goto exit_label;
  1305. end;
  1306. '(' :
  1307. begin
  1308. readchar;
  1309. case c of
  1310. '*' :
  1311. begin
  1312. skipoldtpcomment;
  1313. readtoken;
  1314. exit;
  1315. end;
  1316. '.' :
  1317. begin
  1318. readchar;
  1319. token:=_LECKKLAMMER;
  1320. goto exit_label;
  1321. end;
  1322. end;
  1323. token:=_LKLAMMER;
  1324. goto exit_label;
  1325. end;
  1326. ')' :
  1327. begin
  1328. readchar;
  1329. token:=_RKLAMMER;
  1330. goto exit_label;
  1331. end;
  1332. '+' :
  1333. begin
  1334. readchar;
  1335. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  1336. begin
  1337. readchar;
  1338. token:=_PLUSASN;
  1339. goto exit_label;
  1340. end;
  1341. token:=_PLUS;
  1342. goto exit_label;
  1343. end;
  1344. '-' :
  1345. begin
  1346. readchar;
  1347. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  1348. begin
  1349. readchar;
  1350. token:=_MINUSASN;
  1351. goto exit_label;
  1352. end;
  1353. token:=_MINUS;
  1354. goto exit_label;
  1355. end;
  1356. ':' :
  1357. begin
  1358. readchar;
  1359. if c='=' then
  1360. begin
  1361. readchar;
  1362. token:=_ASSIGNMENT;
  1363. goto exit_label;
  1364. end;
  1365. token:=_COLON;
  1366. goto exit_label;
  1367. end;
  1368. '*' :
  1369. begin
  1370. readchar;
  1371. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  1372. begin
  1373. readchar;
  1374. token:=_STARASN;
  1375. end
  1376. else
  1377. if c='*' then
  1378. begin
  1379. readchar;
  1380. token:=_STARSTAR;
  1381. end
  1382. else
  1383. token:=_STAR;
  1384. goto exit_label;
  1385. end;
  1386. '/' :
  1387. begin
  1388. readchar;
  1389. case c of
  1390. '=' :
  1391. begin
  1392. if (cs_support_c_operators in aktmoduleswitches) then
  1393. begin
  1394. readchar;
  1395. token:=_SLASHASN;
  1396. goto exit_label;
  1397. end;
  1398. end;
  1399. '/' :
  1400. begin
  1401. skipdelphicomment;
  1402. readtoken;
  1403. exit;
  1404. end;
  1405. end;
  1406. token:=_SLASH;
  1407. goto exit_label;
  1408. end;
  1409. '=' :
  1410. begin
  1411. readchar;
  1412. token:=_EQUAL;
  1413. goto exit_label;
  1414. end;
  1415. '.' :
  1416. begin
  1417. readchar;
  1418. case c of
  1419. '.' :
  1420. begin
  1421. readchar;
  1422. token:=_POINTPOINT;
  1423. goto exit_label;
  1424. end;
  1425. ')' :
  1426. begin
  1427. readchar;
  1428. token:=_RECKKLAMMER;
  1429. goto exit_label;
  1430. end;
  1431. end;
  1432. token:=_POINT;
  1433. goto exit_label;
  1434. end;
  1435. '@' :
  1436. begin
  1437. readchar;
  1438. if c='@' then
  1439. begin
  1440. readchar;
  1441. token:=_DOUBLEADDR;
  1442. end
  1443. else
  1444. token:=_KLAMMERAFFE;
  1445. goto exit_label;
  1446. end;
  1447. ',' :
  1448. begin
  1449. readchar;
  1450. token:=_COMMA;
  1451. goto exit_label;
  1452. end;
  1453. '''','#','^' :
  1454. begin
  1455. iswidestring:=false;
  1456. if c='^' then
  1457. begin
  1458. readchar;
  1459. c:=upcase(c);
  1460. if (block_type=bt_type) or
  1461. (lasttoken=_ID) or
  1462. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  1463. begin
  1464. token:=_CARET;
  1465. goto exit_label;
  1466. end
  1467. else
  1468. begin
  1469. if c<#64 then
  1470. pattern:=chr(ord(c)+64)
  1471. else
  1472. pattern:=chr(ord(c)-64);
  1473. readchar;
  1474. end;
  1475. end
  1476. else
  1477. pattern:='';
  1478. repeat
  1479. case c of
  1480. '#' :
  1481. begin
  1482. readchar; { read # }
  1483. if c='$' then
  1484. begin
  1485. readchar; { read leading $ }
  1486. asciinr:='$';
  1487. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<6) do
  1488. begin
  1489. asciinr:=asciinr+c;
  1490. readchar;
  1491. end;
  1492. end
  1493. else
  1494. begin
  1495. asciinr:='';
  1496. while (c in ['0'..'9']) and (length(asciinr)<6) do
  1497. begin
  1498. asciinr:=asciinr+c;
  1499. readchar;
  1500. end;
  1501. end;
  1502. valint(asciinr,m,code);
  1503. if (asciinr='') or (code<>0) then
  1504. Message(scan_e_illegal_char_const)
  1505. else if (m<0) or (m>255) then
  1506. begin
  1507. if (m>=0) and (m<=65535) then
  1508. begin
  1509. ascii2unicode(pattern,patternw);
  1510. concatwidestringchar(patternw,
  1511. tcompilerwidechar(m));
  1512. iswidestring:=true;
  1513. end
  1514. else
  1515. Message(scan_e_illegal_char_const)
  1516. end
  1517. else if iswidestring then
  1518. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  1519. else
  1520. pattern:=pattern+chr(m);
  1521. end;
  1522. '''' :
  1523. begin
  1524. repeat
  1525. readchar;
  1526. case c of
  1527. #26 :
  1528. end_of_file;
  1529. newline :
  1530. Message(scan_f_string_exceeds_line);
  1531. '''' :
  1532. begin
  1533. readchar;
  1534. if c<>'''' then
  1535. break;
  1536. end;
  1537. end;
  1538. if iswidestring then
  1539. concatwidestringchar(patternw,
  1540. asciichar2unicode(c))
  1541. else
  1542. pattern:=pattern+c;
  1543. until false;
  1544. end;
  1545. '^' :
  1546. begin
  1547. readchar;
  1548. c:=upcase(c);
  1549. if c<#64 then
  1550. c:=chr(ord(c)+64)
  1551. else
  1552. c:=chr(ord(c)-64);
  1553. if iswidestring then
  1554. concatwidestringchar(patternw,
  1555. asciichar2unicode(c))
  1556. else
  1557. pattern:=pattern+c;
  1558. readchar;
  1559. end;
  1560. else
  1561. break;
  1562. end;
  1563. until false;
  1564. { strings with length 1 become const chars }
  1565. if iswidestring then
  1566. begin
  1567. if length(pattern)=1 then
  1568. token:=_CWCHAR
  1569. else
  1570. token:=_CWSTRING;
  1571. end
  1572. else
  1573. begin
  1574. if length(pattern)=1 then
  1575. token:=_CCHAR
  1576. else
  1577. token:=_CSTRING;
  1578. end;
  1579. goto exit_label;
  1580. end;
  1581. '>' :
  1582. begin
  1583. readchar;
  1584. case c of
  1585. '=' :
  1586. begin
  1587. readchar;
  1588. token:=_GTE;
  1589. goto exit_label;
  1590. end;
  1591. '>' :
  1592. begin
  1593. readchar;
  1594. token:=_OP_SHR;
  1595. goto exit_label;
  1596. end;
  1597. '<' :
  1598. begin { >< is for a symetric diff for sets }
  1599. readchar;
  1600. token:=_SYMDIF;
  1601. goto exit_label;
  1602. end;
  1603. end;
  1604. token:=_GT;
  1605. goto exit_label;
  1606. end;
  1607. '<' :
  1608. begin
  1609. readchar;
  1610. case c of
  1611. '>' :
  1612. begin
  1613. readchar;
  1614. token:=_UNEQUAL;
  1615. goto exit_label;
  1616. end;
  1617. '=' :
  1618. begin
  1619. readchar;
  1620. token:=_LTE;
  1621. goto exit_label;
  1622. end;
  1623. '<' :
  1624. begin
  1625. readchar;
  1626. token:=_OP_SHL;
  1627. goto exit_label;
  1628. end;
  1629. end;
  1630. token:=_LT;
  1631. goto exit_label;
  1632. end;
  1633. #26 :
  1634. begin
  1635. token:=_EOF;
  1636. checkpreprocstack;
  1637. goto exit_label;
  1638. end;
  1639. else
  1640. Illegal_Char(c);
  1641. end;
  1642. end;
  1643. exit_label:
  1644. lasttoken:=token;
  1645. end;
  1646. function tscannerfile.readpreproc:ttoken;
  1647. begin
  1648. skipspace;
  1649. case c of
  1650. 'A'..'Z',
  1651. 'a'..'z',
  1652. '_','0'..'9' : begin
  1653. preprocpat:=readid;
  1654. readpreproc:=_ID;
  1655. end;
  1656. '}' : begin
  1657. readpreproc:=_END;
  1658. end;
  1659. '(' : begin
  1660. readchar;
  1661. readpreproc:=_LKLAMMER;
  1662. end;
  1663. ')' : begin
  1664. readchar;
  1665. readpreproc:=_RKLAMMER;
  1666. end;
  1667. '+' : begin
  1668. readchar;
  1669. readpreproc:=_PLUS;
  1670. end;
  1671. '-' : begin
  1672. readchar;
  1673. readpreproc:=_MINUS;
  1674. end;
  1675. '*' : begin
  1676. readchar;
  1677. readpreproc:=_STAR;
  1678. end;
  1679. '/' : begin
  1680. readchar;
  1681. readpreproc:=_SLASH;
  1682. end;
  1683. '=' : begin
  1684. readchar;
  1685. readpreproc:=_EQUAL;
  1686. end;
  1687. '>' : begin
  1688. readchar;
  1689. if c='=' then
  1690. begin
  1691. readchar;
  1692. readpreproc:=_GTE;
  1693. end
  1694. else
  1695. readpreproc:=_GT;
  1696. end;
  1697. '<' : begin
  1698. readchar;
  1699. case c of
  1700. '>' : begin
  1701. readchar;
  1702. readpreproc:=_UNEQUAL;
  1703. end;
  1704. '=' : begin
  1705. readchar;
  1706. readpreproc:=_LTE;
  1707. end;
  1708. else readpreproc:=_LT;
  1709. end;
  1710. end;
  1711. #26 :
  1712. end_of_file;
  1713. else
  1714. begin
  1715. readpreproc:=_EOF;
  1716. checkpreprocstack;
  1717. end;
  1718. end;
  1719. end;
  1720. function tscannerfile.asmgetchar : char;
  1721. begin
  1722. if lastasmgetchar<>#0 then
  1723. begin
  1724. c:=lastasmgetchar;
  1725. lastasmgetchar:=#0;
  1726. end
  1727. else
  1728. readchar;
  1729. if in_asm_string then
  1730. begin
  1731. asmgetchar:=c;
  1732. exit;
  1733. end;
  1734. case c of
  1735. '{' : begin
  1736. skipcomment;
  1737. asmgetchar:=c;
  1738. exit;
  1739. end;
  1740. '/' : begin
  1741. readchar;
  1742. if c='/' then
  1743. begin
  1744. skipdelphicomment;
  1745. asmgetchar:=c;
  1746. end
  1747. else
  1748. begin
  1749. asmgetchar:='/';
  1750. lastasmgetchar:=c;
  1751. end;
  1752. exit;
  1753. end;
  1754. '(' : begin
  1755. readchar;
  1756. if c='*' then
  1757. begin
  1758. skipoldtpcomment;
  1759. asmgetchar:=c;
  1760. end
  1761. else
  1762. begin
  1763. asmgetchar:='(';
  1764. lastasmgetchar:=c;
  1765. end;
  1766. exit;
  1767. end;
  1768. else
  1769. begin
  1770. asmgetchar:=c;
  1771. end;
  1772. end;
  1773. end;
  1774. end.
  1775. {
  1776. $Log$
  1777. Revision 1.10 2000-12-16 15:36:02 peter
  1778. * fixed parsing of strings and comments in skipuntildirective
  1779. Revision 1.9 2000/11/30 20:27:51 peter
  1780. * merged fix for bug 1229
  1781. Revision 1.8 2000/11/29 00:30:40 florian
  1782. * unused units removed from uses clause
  1783. * some changes for widestrings
  1784. Revision 1.7 2000/10/31 22:02:51 peter
  1785. * symtable splitted, no real code changes
  1786. Revision 1.6 2000/09/24 15:06:28 peter
  1787. * use defines.inc
  1788. Revision 1.5 2000/08/27 16:11:53 peter
  1789. * moved some util functions from globals,cobjects to cutils
  1790. * splitted files into finput,fmodule
  1791. Revision 1.4 2000/08/12 15:30:44 peter
  1792. * IDE patch for stream reading (merged)
  1793. Revision 1.3 2000/08/08 19:28:57 peter
  1794. * memdebug/memory patches (merged)
  1795. * only once illegal directive (merged)
  1796. Revision 1.2 2000/07/13 11:32:49 michael
  1797. + removed logs
  1798. }