scanner.pas 56 KB

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