scanner.pas 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650
  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. 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. tscannerfile = class;
  36. tmacro = class(TNamedIndexItem)
  37. defined,
  38. defined_at_startup,
  39. is_used : boolean;
  40. buftext : pchar;
  41. buflen : longint;
  42. constructor Create(const n : string);
  43. destructor destroy;override;
  44. end;
  45. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else);
  46. tpreprocstack = class
  47. typ : preproctyp;
  48. accept : boolean;
  49. next : tpreprocstack;
  50. name : stringid;
  51. line_nb : longint;
  52. owner : tscannerfile;
  53. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  54. end;
  55. tdirectiveproc=procedure;
  56. tdirectiveitem = class(TNamedIndexItem)
  57. public
  58. is_conditional : boolean;
  59. proc : tdirectiveproc;
  60. constructor Create(const n:string;p:tdirectiveproc);
  61. constructor CreateCond(const n:string;p:tdirectiveproc);
  62. end;
  63. tscannerfile = class
  64. inputfile : tinputfile; { current inputfile list }
  65. inputbuffer, { input buffer }
  66. inputpointer : pchar;
  67. inputstart : longint;
  68. line_no, { line }
  69. lastlinepos : longint;
  70. lasttokenpos : longint; { token }
  71. lasttoken,
  72. nexttoken : ttoken;
  73. comment_level,
  74. yylexcount : longint;
  75. lastasmgetchar : char;
  76. ignoredirectives : tstringlist; { ignore directives, used to give warnings only once }
  77. preprocstack : tpreprocstack;
  78. invalid : boolean; { flag if sourcefiles have been destroyed ! }
  79. macros : Tdictionary;
  80. in_asm_string : boolean;
  81. preproc_pattern : string;
  82. preproc_token : ttoken;
  83. constructor Create(const fn:string);
  84. destructor Destroy;override;
  85. { File buffer things }
  86. function openinputfile:boolean;
  87. procedure closeinputfile;
  88. function tempopeninputfile:boolean;
  89. procedure tempcloseinputfile;
  90. procedure saveinputfile;
  91. procedure restoreinputfile;
  92. procedure nextfile;
  93. procedure addfile(hp:tinputfile);
  94. procedure reload;
  95. procedure insertmacro(const macname:string;p:pchar;len:longint);
  96. { Scanner things }
  97. procedure def_macro(const s : string);
  98. procedure set_macro(const s : string;value : string);
  99. procedure gettokenpos;
  100. procedure inc_comment_level;
  101. procedure dec_comment_level;
  102. procedure illegal_char(c:char);
  103. procedure end_of_file;
  104. procedure checkpreprocstack;
  105. procedure poppreprocstack;
  106. procedure addpreprocstack(atyp : preproctyp;a:boolean;const s:string;w:longint);
  107. procedure elsepreprocstack;
  108. procedure handleconditional(p:tdirectiveitem);
  109. procedure handledirectives;
  110. procedure linebreak;
  111. procedure readchar;
  112. procedure readstring;
  113. procedure readnumber;
  114. function readid:string;
  115. function readval:longint;
  116. function readcomment:string;
  117. function readstate:char;
  118. procedure skipspace;
  119. procedure skipuntildirective;
  120. procedure skipcomment;
  121. procedure skipdelphicomment;
  122. procedure skipoldtpcomment;
  123. procedure readtoken;
  124. function readpreproc:ttoken;
  125. function asmgetchar:char;
  126. end;
  127. {$ifdef PREPROCWRITE}
  128. tpreprocfile=class
  129. f : text;
  130. buf : pointer;
  131. spacefound,
  132. eolfound : boolean;
  133. constructor create(const fn:string);
  134. destructor destroy;
  135. procedure Add(const s:string);
  136. procedure AddSpace;
  137. end;
  138. {$endif PREPROCWRITE}
  139. var
  140. { read strings }
  141. c : char;
  142. orgpattern,
  143. pattern : string;
  144. patternw : tcompilerwidestring;
  145. { token }
  146. token, { current token being parsed }
  147. idtoken : ttoken; { holds the token if the pattern is a known word }
  148. current_scanner : tscannerfile; { current scanner in use }
  149. scannerdirectives : tdictionary; { dictionary with the supported directives }
  150. aktcommentstyle : tcommentstyle; { needed to use read_comment from directives }
  151. {$ifdef PREPROCWRITE}
  152. preprocfile : tpreprocfile; { used with only preprocessing }
  153. {$endif PREPROCWRITE}
  154. procedure adddirective(const s:string;p:tdirectiveproc);
  155. procedure addconditional(const s:string;p:tdirectiveproc);
  156. procedure InitScanner;
  157. procedure DoneScanner;
  158. implementation
  159. uses
  160. {$ifdef delphi}
  161. dmisc,
  162. {$else}
  163. dos,
  164. {$endif delphi}
  165. cutils,
  166. systems,
  167. switches,
  168. fmodule;
  169. {*****************************************************************************
  170. Helper routines
  171. *****************************************************************************}
  172. const
  173. { use any special name that is an invalid file name to avoid problems }
  174. preprocstring : array [preproctyp] of string[7]
  175. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE');
  176. function is_keyword(const s:string):boolean;
  177. var
  178. low,high,mid : longint;
  179. begin
  180. if not (length(s) in [2..tokenidlen]) then
  181. begin
  182. is_keyword:=false;
  183. exit;
  184. end;
  185. low:=ord(tokenidx^[length(s),s[1]].first);
  186. high:=ord(tokenidx^[length(s),s[1]].last);
  187. while low<high do
  188. begin
  189. mid:=(high+low+1) shr 1;
  190. if pattern<tokeninfo^[ttoken(mid)].str then
  191. high:=mid-1
  192. else
  193. low:=mid;
  194. end;
  195. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  196. (tokeninfo^[ttoken(high)].keyword in aktmodeswitches);
  197. end;
  198. {*****************************************************************************
  199. Conditional Directives
  200. *****************************************************************************}
  201. procedure dir_else;
  202. begin
  203. current_scanner.elsepreprocstack;
  204. end;
  205. procedure dir_endif;
  206. begin
  207. current_scanner.poppreprocstack;
  208. end;
  209. procedure dir_ifdef;
  210. var
  211. hs : string;
  212. mac : tmacro;
  213. begin
  214. current_scanner.skipspace;
  215. hs:=current_scanner.readid;
  216. mac:=tmacro(current_scanner.macros.search(hs));
  217. if assigned(mac) then
  218. mac.is_used:=true;
  219. current_scanner.addpreprocstack(pp_ifdef,assigned(mac) and mac.defined,hs,scan_c_ifdef_found);
  220. end;
  221. procedure dir_ifndef;
  222. var
  223. hs : string;
  224. mac : tmacro;
  225. begin
  226. current_scanner.skipspace;
  227. hs:=current_scanner.readid;
  228. mac:=tmacro(current_scanner.macros.search(hs));
  229. if assigned(mac) then
  230. mac.is_used:=true;
  231. current_scanner.addpreprocstack(pp_ifndef,not(assigned(mac) and mac.defined),hs,scan_c_ifndef_found);
  232. end;
  233. procedure dir_ifopt;
  234. var
  235. hs : string;
  236. found : boolean;
  237. state : char;
  238. begin
  239. current_scanner.skipspace;
  240. hs:=current_scanner.readid;
  241. if (length(hs)>1) then
  242. Message1(scan_w_illegal_switch,hs)
  243. else
  244. begin
  245. state:=current_scanner.ReadState;
  246. if state in ['-','+'] then
  247. found:=CheckSwitch(hs[1],state);
  248. end;
  249. current_scanner.addpreprocstack(pp_ifopt,found,hs,scan_c_ifopt_found);
  250. end;
  251. procedure dir_if;
  252. function read_expr : string; forward;
  253. procedure preproc_consume(t : ttoken);
  254. begin
  255. if t<>current_scanner.preproc_token then
  256. Message(scan_e_preproc_syntax_error);
  257. current_scanner.preproc_token:=current_scanner.readpreproc;
  258. end;
  259. function read_factor : string;
  260. var
  261. hs : string;
  262. mac : tmacro;
  263. len : byte;
  264. begin
  265. if current_scanner.preproc_token=_ID then
  266. begin
  267. if current_scanner.preproc_pattern='NOT' then
  268. begin
  269. preproc_consume(_ID);
  270. hs:=read_expr;
  271. if hs='0' then
  272. read_factor:='1'
  273. else
  274. read_factor:='0';
  275. end
  276. else
  277. begin
  278. mac:=tmacro(current_scanner.macros.search(hs));
  279. hs:=current_scanner.preproc_pattern;
  280. preproc_consume(_ID);
  281. if assigned(mac) then
  282. begin
  283. if mac.defined and assigned(mac.buftext) then
  284. begin
  285. if mac.buflen>255 then
  286. begin
  287. len:=255;
  288. Message(scan_w_macro_cut_after_255_chars);
  289. end
  290. else
  291. len:=mac.buflen;
  292. hs[0]:=char(len);
  293. move(mac.buftext^,hs[1],len);
  294. end
  295. else
  296. read_factor:='';
  297. end
  298. else
  299. read_factor:=hs;
  300. end
  301. end
  302. else if current_scanner.preproc_token=_LKLAMMER then
  303. begin
  304. preproc_consume(_LKLAMMER);
  305. read_factor:=read_expr;
  306. preproc_consume(_RKLAMMER);
  307. end
  308. else
  309. Message(scan_e_error_in_preproc_expr);
  310. end;
  311. function read_term : string;
  312. var
  313. hs1,hs2 : string;
  314. begin
  315. hs1:=read_factor;
  316. while true do
  317. begin
  318. if (current_scanner.preproc_token=_ID) then
  319. begin
  320. if current_scanner.preproc_pattern='AND' then
  321. begin
  322. preproc_consume(_ID);
  323. hs2:=read_factor;
  324. if (hs1<>'0') and (hs2<>'0') then
  325. hs1:='1';
  326. end
  327. else
  328. break;
  329. end
  330. else
  331. break;
  332. end;
  333. read_term:=hs1;
  334. end;
  335. function read_simple_expr : string;
  336. var
  337. hs1,hs2 : string;
  338. begin
  339. hs1:=read_term;
  340. while true do
  341. begin
  342. if (current_scanner.preproc_token=_ID) then
  343. begin
  344. if current_scanner.preproc_pattern='OR' then
  345. begin
  346. preproc_consume(_ID);
  347. hs2:=read_term;
  348. if (hs1<>'0') or (hs2<>'0') then
  349. hs1:='1';
  350. end
  351. else
  352. break;
  353. end
  354. else
  355. break;
  356. end;
  357. read_simple_expr:=hs1;
  358. end;
  359. function read_expr : string;
  360. var
  361. hs1,hs2 : string;
  362. b : boolean;
  363. t : ttoken;
  364. w : integer;
  365. l1,l2 : longint;
  366. begin
  367. hs1:=read_simple_expr;
  368. t:=current_scanner.preproc_token;
  369. if not(t in [_EQUAL,_UNEQUAL,_LT,_GT,_LTE,_GTE]) then
  370. begin
  371. read_expr:=hs1;
  372. exit;
  373. end;
  374. preproc_consume(t);
  375. hs2:=read_simple_expr;
  376. if is_number(hs1) and is_number(hs2) then
  377. begin
  378. valint(hs1,l1,w);
  379. valint(hs2,l2,w);
  380. case t of
  381. _EQUAL : b:=l1=l2;
  382. _UNEQUAL : b:=l1<>l2;
  383. _LT : b:=l1<l2;
  384. _GT : b:=l1>l2;
  385. _GTE : b:=l1>=l2;
  386. _LTE : b:=l1<=l2;
  387. end;
  388. end
  389. else
  390. begin
  391. case t of
  392. _EQUAL : b:=hs1=hs2;
  393. _UNEQUAL : b:=hs1<>hs2;
  394. _LT : b:=hs1<hs2;
  395. _GT : b:=hs1>hs2;
  396. _GTE : b:=hs1>=hs2;
  397. _LTE : b:=hs1<=hs2;
  398. end;
  399. end;
  400. if b then
  401. read_expr:='1'
  402. else
  403. read_expr:='0';
  404. end;
  405. var
  406. hs : string;
  407. begin
  408. current_scanner.skipspace;
  409. { start preproc expression scanner }
  410. current_scanner.preproc_token:=current_scanner.readpreproc;
  411. hs:=read_expr;
  412. current_scanner.addpreprocstack(pp_if,hs<>'0',hs,scan_c_if_found);
  413. end;
  414. procedure dir_define;
  415. var
  416. hs : string;
  417. bracketcount : longint;
  418. mac : tmacro;
  419. macropos : longint;
  420. macrobuffer : pmacrobuffer;
  421. begin
  422. current_scanner.skipspace;
  423. hs:=current_scanner.readid;
  424. mac:=tmacro(current_scanner.macros.search(hs));
  425. if not assigned(mac) then
  426. begin
  427. mac:=tmacro.create(hs);
  428. mac.defined:=true;
  429. Message1(parser_m_macro_defined,mac.name);
  430. current_scanner.macros.insert(mac);
  431. end
  432. else
  433. begin
  434. Message1(parser_m_macro_defined,mac.name);
  435. mac.defined:=true;
  436. { delete old definition }
  437. if assigned(mac.buftext) then
  438. begin
  439. freemem(mac.buftext,mac.buflen);
  440. mac.buftext:=nil;
  441. end;
  442. end;
  443. mac.is_used:=true;
  444. if (cs_support_macro in aktmoduleswitches) then
  445. begin
  446. { key words are never substituted }
  447. if is_keyword(hs) then
  448. Message(scan_e_keyword_cant_be_a_macro);
  449. { !!!!!! handle macro params, need we this? }
  450. current_scanner.skipspace;
  451. { may be a macro? }
  452. if c=':' then
  453. begin
  454. current_scanner.readchar;
  455. if c='=' then
  456. begin
  457. new(macrobuffer);
  458. macropos:=0;
  459. { parse macro, brackets are counted so it's possible
  460. to have a $ifdef etc. in the macro }
  461. bracketcount:=0;
  462. repeat
  463. current_scanner.readchar;
  464. case c of
  465. '}' :
  466. if (bracketcount=0) then
  467. break
  468. else
  469. dec(bracketcount);
  470. '{' :
  471. inc(bracketcount);
  472. #26 :
  473. current_scanner.end_of_file;
  474. end;
  475. macrobuffer^[macropos]:=c;
  476. inc(macropos);
  477. if macropos>maxmacrolen then
  478. Message(scan_f_macro_buffer_overflow);
  479. until false;
  480. { free buffer of macro ?}
  481. if assigned(mac.buftext) then
  482. freemem(mac.buftext,mac.buflen);
  483. { get new mem }
  484. getmem(mac.buftext,macropos);
  485. mac.buflen:=macropos;
  486. { copy the text }
  487. move(macrobuffer^,mac.buftext^,macropos);
  488. dispose(macrobuffer);
  489. end;
  490. end;
  491. end
  492. else
  493. begin
  494. { check if there is an assignment, then we need to give a
  495. warning }
  496. current_scanner.skipspace;
  497. if c=':' then
  498. begin
  499. current_scanner.readchar;
  500. if c='=' then
  501. Message(scan_w_macro_support_turned_off);
  502. end;
  503. end;
  504. end;
  505. procedure dir_undef;
  506. var
  507. hs : string;
  508. mac : tmacro;
  509. begin
  510. current_scanner.skipspace;
  511. hs:=current_scanner.readid;
  512. mac:=tmacro(current_scanner.macros.search(hs));
  513. if not assigned(mac) then
  514. begin
  515. mac:=tmacro.create(hs);
  516. Message1(parser_m_macro_undefined,mac.name);
  517. mac.defined:=false;
  518. current_scanner.macros.insert(mac);
  519. end
  520. else
  521. begin
  522. Message1(parser_m_macro_undefined,mac.name);
  523. mac.defined:=false;
  524. { delete old definition }
  525. if assigned(mac.buftext) then
  526. begin
  527. freemem(mac.buftext,mac.buflen);
  528. mac.buftext:=nil;
  529. end;
  530. end;
  531. mac.is_used:=true;
  532. end;
  533. procedure dir_include;
  534. var
  535. foundfile,
  536. hs : string;
  537. path : dirstr;
  538. name : namestr;
  539. ext : extstr;
  540. hp : tinputfile;
  541. i : longint;
  542. found : boolean;
  543. begin
  544. current_scanner.skipspace;
  545. hs:=current_scanner.readcomment;
  546. i:=length(hs);
  547. while (i>0) and (hs[i]=' ') do
  548. dec(i);
  549. Delete(hs,i+1,length(hs)-i);
  550. if hs='' then
  551. exit;
  552. if (hs[1]='%') then
  553. begin
  554. { case insensitive }
  555. hs:=upper(hs);
  556. { remove %'s }
  557. Delete(hs,1,1);
  558. if hs[length(hs)]='%' then
  559. Delete(hs,length(hs),1);
  560. { save old }
  561. path:=hs;
  562. { first check for internal macros }
  563. if hs='TIME' then
  564. hs:=gettimestr
  565. else
  566. if hs='DATE' then
  567. hs:=getdatestr
  568. else
  569. if hs='FILE' then
  570. hs:=current_module.sourcefiles.get_file_name(aktfilepos.fileindex)
  571. else
  572. if hs='LINE' then
  573. hs:=tostr(aktfilepos.line)
  574. else
  575. if hs='FPCVERSION' then
  576. hs:=version_string
  577. else
  578. if hs='FPCTARGET' then
  579. hs:=target_cpu_string
  580. else
  581. hs:=getenv(hs);
  582. if hs='' then
  583. Message1(scan_w_include_env_not_found,path);
  584. { make it a stringconst }
  585. hs:=''''+hs+'''';
  586. current_scanner.insertmacro(path,@hs[1],length(hs));
  587. end
  588. else
  589. begin
  590. hs:=FixFileName(hs);
  591. fsplit(hs,path,name,ext);
  592. { look for the include file
  593. 1. specified path,path of current inputfile,current dir
  594. 2. local includepath
  595. 3. global includepath }
  596. found:=false;
  597. foundfile:='';
  598. if path<>'' then
  599. path:=path+';';
  600. found:=FindFile(name+ext,path+current_scanner.inputfile.path^+';.'+DirSep,foundfile);
  601. if (not found) then
  602. found:=current_module.localincludesearchpath.FindFile(name+ext,foundfile);
  603. if (not found) then
  604. found:=includesearchpath.FindFile(name+ext,foundfile);
  605. { save old postion and decrease linebreak }
  606. if c=newline then
  607. dec(current_scanner.line_no);
  608. dec(longint(current_scanner.inputpointer));
  609. { shutdown current file }
  610. current_scanner.tempcloseinputfile;
  611. { load new file }
  612. hp:=do_openinputfile(foundfile);
  613. current_scanner.addfile(hp);
  614. current_module.sourcefiles.register_file(hp);
  615. if not current_scanner.openinputfile then
  616. Message1(scan_f_cannot_open_includefile,hs);
  617. Message1(scan_t_start_include_file,current_scanner.inputfile.path^+current_scanner.inputfile.name^);
  618. current_scanner.reload;
  619. { process first read char }
  620. case c of
  621. #26 : current_scanner.reload;
  622. #10,
  623. #13 : current_scanner.linebreak;
  624. end;
  625. end;
  626. end;
  627. {*****************************************************************************
  628. TMacro
  629. *****************************************************************************}
  630. constructor tmacro.create(const n : string);
  631. begin
  632. inherited createname(n);
  633. defined:=true;
  634. defined_at_startup:=false;
  635. is_used:=false;
  636. buftext:=nil;
  637. buflen:=0;
  638. end;
  639. destructor tmacro.destroy;
  640. begin
  641. if assigned(buftext) then
  642. freemem(buftext,buflen);
  643. inherited destroy;
  644. end;
  645. {*****************************************************************************
  646. Preprocessor writting
  647. *****************************************************************************}
  648. {$ifdef PREPROCWRITE}
  649. constructor tpreprocfile.create(const fn:string);
  650. begin
  651. { open outputfile }
  652. assign(f,fn);
  653. {$I-}
  654. rewrite(f);
  655. {$I+}
  656. if ioresult<>0 then
  657. Comment(V_Fatal,'can''t create file '+fn);
  658. getmem(buf,preprocbufsize);
  659. settextbuf(f,buf^,preprocbufsize);
  660. { reset }
  661. eolfound:=false;
  662. spacefound:=false;
  663. end;
  664. destructor tpreprocfile.destroy;
  665. begin
  666. close(f);
  667. freemem(buf,preprocbufsize);
  668. end;
  669. procedure tpreprocfile.add(const s:string);
  670. begin
  671. write(f,s);
  672. end;
  673. procedure tpreprocfile.addspace;
  674. begin
  675. if eolfound then
  676. begin
  677. writeln(f,'');
  678. eolfound:=false;
  679. spacefound:=false;
  680. end
  681. else
  682. if spacefound then
  683. begin
  684. write(f,' ');
  685. spacefound:=false;
  686. end;
  687. end;
  688. {$endif PREPROCWRITE}
  689. {*****************************************************************************
  690. TPreProcStack
  691. *****************************************************************************}
  692. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  693. begin
  694. accept:=a;
  695. typ:=atyp;
  696. next:=n;
  697. end;
  698. {*****************************************************************************
  699. TDirectiveItem
  700. *****************************************************************************}
  701. constructor TDirectiveItem.Create(const n:string;p:tdirectiveproc);
  702. begin
  703. inherited CreateName(n);
  704. is_conditional:=false;
  705. proc:={$ifndef FPCPROCVAR}@{$endif}p;
  706. end;
  707. constructor TDirectiveItem.CreateCond(const n:string;p:tdirectiveproc);
  708. begin
  709. inherited CreateName(n);
  710. is_conditional:=true;
  711. proc:={$ifndef FPCPROCVAR}@{$endif}p;
  712. end;
  713. {****************************************************************************
  714. TSCANNERFILE
  715. ****************************************************************************}
  716. constructor tscannerfile.create(const fn:string);
  717. begin
  718. inputfile:=do_openinputfile(fn);
  719. if assigned(current_module) then
  720. current_module.sourcefiles.register_file(inputfile);
  721. { reset localinput }
  722. inputbuffer:=nil;
  723. inputpointer:=nil;
  724. inputstart:=0;
  725. { reset scanner }
  726. preprocstack:=nil;
  727. comment_level:=0;
  728. yylexcount:=0;
  729. block_type:=bt_general;
  730. line_no:=0;
  731. lastlinepos:=0;
  732. lasttokenpos:=0;
  733. lasttoken:=NOTOKEN;
  734. nexttoken:=NOTOKEN;
  735. lastasmgetchar:=#0;
  736. ignoredirectives:=TStringList.Create;
  737. invalid:=false;
  738. in_asm_string:=false;
  739. macros:=tdictionary.create;
  740. { load block }
  741. if not openinputfile then
  742. Message1(scan_f_cannot_open_input,fn);
  743. reload;
  744. { process first read char }
  745. case c of
  746. #26 : reload;
  747. #10,
  748. #13 : linebreak;
  749. end;
  750. end;
  751. destructor tscannerfile.destroy;
  752. begin
  753. if not invalid then
  754. begin
  755. if status.errorcount=0 then
  756. checkpreprocstack
  757. else
  758. begin
  759. while assigned(preprocstack) do
  760. poppreprocstack;
  761. end;
  762. { close file, but only if we are the first compile }
  763. { probably not necessary anymore with invalid flag PM }
  764. if not current_module.in_second_compile then
  765. begin
  766. if not inputfile.closed then
  767. closeinputfile;
  768. end;
  769. end;
  770. ignoredirectives.free;
  771. macros.free;
  772. end;
  773. procedure tscannerfile.def_macro(const s : string);
  774. var
  775. mac : tmacro;
  776. begin
  777. mac:=tmacro(macros.search(s));
  778. if mac=nil then
  779. begin
  780. mac:=tmacro.create(s);
  781. Message1(parser_m_macro_defined,mac.name);
  782. macros.insert(mac);
  783. end;
  784. mac.defined:=true;
  785. mac.defined_at_startup:=true;
  786. end;
  787. procedure tscannerfile.set_macro(const s : string;value : string);
  788. var
  789. mac : tmacro;
  790. begin
  791. mac:=tmacro(macros.search(s));
  792. if mac=nil then
  793. begin
  794. mac:=tmacro.create(s);
  795. macros.insert(mac);
  796. end
  797. else
  798. begin
  799. if assigned(mac.buftext) then
  800. freemem(mac.buftext,mac.buflen);
  801. end;
  802. Message2(parser_m_macro_set_to,mac.name,value);
  803. mac.buflen:=length(value);
  804. getmem(mac.buftext,mac.buflen);
  805. move(value[1],mac.buftext^,mac.buflen);
  806. mac.defined:=true;
  807. mac.defined_at_startup:=true;
  808. end;
  809. function tscannerfile.openinputfile:boolean;
  810. begin
  811. openinputfile:=inputfile.open;
  812. { load buffer }
  813. inputbuffer:=inputfile.buf;
  814. inputpointer:=inputfile.buf;
  815. inputstart:=inputfile.bufstart;
  816. { line }
  817. line_no:=0;
  818. lastlinepos:=0;
  819. lasttokenpos:=0;
  820. end;
  821. procedure tscannerfile.closeinputfile;
  822. begin
  823. inputfile.close;
  824. { reset buffer }
  825. inputbuffer:=nil;
  826. inputpointer:=nil;
  827. inputstart:=0;
  828. { reset line }
  829. line_no:=0;
  830. lastlinepos:=0;
  831. lasttokenpos:=0;
  832. end;
  833. function tscannerfile.tempopeninputfile:boolean;
  834. begin
  835. tempopeninputfile:=inputfile.tempopen;
  836. { reload buffer }
  837. inputbuffer:=inputfile.buf;
  838. inputpointer:=inputfile.buf;
  839. inputstart:=inputfile.bufstart;
  840. end;
  841. procedure tscannerfile.tempcloseinputfile;
  842. begin
  843. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  844. inputfile.tempclose;
  845. { reset buffer }
  846. inputbuffer:=nil;
  847. inputpointer:=nil;
  848. inputstart:=0;
  849. end;
  850. procedure tscannerfile.saveinputfile;
  851. begin
  852. inputfile.saveinputpointer:=inputpointer;
  853. inputfile.savelastlinepos:=lastlinepos;
  854. inputfile.saveline_no:=line_no;
  855. end;
  856. procedure tscannerfile.restoreinputfile;
  857. begin
  858. inputpointer:=inputfile.saveinputpointer;
  859. lastlinepos:=inputfile.savelastlinepos;
  860. line_no:=inputfile.saveline_no;
  861. if not inputfile.is_macro then
  862. parser_current_file:=inputfile.name^;
  863. end;
  864. procedure tscannerfile.nextfile;
  865. var
  866. to_dispose : tinputfile;
  867. begin
  868. if assigned(inputfile.next) then
  869. begin
  870. if inputfile.is_macro then
  871. to_dispose:=inputfile
  872. else
  873. to_dispose:=nil;
  874. { we can allways close the file, no ? }
  875. inputfile.close;
  876. inputfile:=inputfile.next;
  877. if assigned(to_dispose) then
  878. to_dispose.free;
  879. restoreinputfile;
  880. end;
  881. end;
  882. procedure tscannerfile.addfile(hp:tinputfile);
  883. begin
  884. saveinputfile;
  885. { add to list }
  886. hp.next:=inputfile;
  887. inputfile:=hp;
  888. { load new inputfile }
  889. restoreinputfile;
  890. end;
  891. procedure tscannerfile.reload;
  892. begin
  893. with inputfile do
  894. begin
  895. { when nothing more to read then leave immediatly, so we
  896. don't change the aktfilepos and leave it point to the last
  897. char }
  898. if (c=#26) and (not assigned(next)) then
  899. exit;
  900. repeat
  901. { still more to read?, then change the #0 to a space so its seen
  902. as a seperator, this can't be used for macro's which can change
  903. the place of the #0 in the buffer with tempopen }
  904. if (c=#0) and (bufsize>0) and
  905. not(inputfile.is_macro) and
  906. (inputpointer-inputbuffer<bufsize) then
  907. begin
  908. c:=' ';
  909. inc(longint(inputpointer));
  910. exit;
  911. end;
  912. { can we read more from this file ? }
  913. if (c<>#26) and (not endoffile) then
  914. begin
  915. readbuf;
  916. inputpointer:=buf;
  917. inputbuffer:=buf;
  918. inputstart:=bufstart;
  919. { first line? }
  920. if line_no=0 then
  921. begin
  922. line_no:=1;
  923. if cs_asm_source in aktglobalswitches then
  924. inputfile.setline(line_no,bufstart);
  925. end;
  926. end
  927. else
  928. begin
  929. { load eof position in tokenpos/aktfilepos }
  930. gettokenpos;
  931. { close file }
  932. closeinputfile;
  933. { no next module, than EOF }
  934. if not assigned(inputfile.next) then
  935. begin
  936. c:=#26;
  937. exit;
  938. end;
  939. { load next file and reopen it }
  940. nextfile;
  941. tempopeninputfile;
  942. { status }
  943. Message1(scan_t_back_in,inputfile.name^);
  944. end;
  945. { load next char }
  946. c:=inputpointer^;
  947. inc(longint(inputpointer));
  948. until c<>#0; { if also end, then reload again }
  949. end;
  950. end;
  951. procedure tscannerfile.insertmacro(const macname:string;p:pchar;len:longint);
  952. var
  953. hp : tinputfile;
  954. begin
  955. { save old postion and decrease linebreak }
  956. if c=newline then
  957. dec(line_no);
  958. dec(longint(inputpointer));
  959. tempcloseinputfile;
  960. { create macro 'file' }
  961. { use special name to dispose after !! }
  962. hp:=do_openinputfile('_Macro_.'+macname);
  963. addfile(hp);
  964. with inputfile do
  965. begin
  966. setmacro(p,len);
  967. { local buffer }
  968. inputbuffer:=buf;
  969. inputpointer:=buf;
  970. inputstart:=bufstart;
  971. end;
  972. { reset line }
  973. line_no:=0;
  974. lastlinepos:=0;
  975. lasttokenpos:=0;
  976. { load new c }
  977. c:=inputpointer^;
  978. inc(longint(inputpointer));
  979. end;
  980. procedure tscannerfile.gettokenpos;
  981. { load the values of tokenpos and lasttokenpos }
  982. begin
  983. lasttokenpos:=inputstart+(inputpointer-inputbuffer);
  984. akttokenpos.line:=line_no;
  985. akttokenpos.column:=lasttokenpos-lastlinepos;
  986. akttokenpos.fileindex:=inputfile.ref_index;
  987. aktfilepos:=akttokenpos;
  988. end;
  989. procedure tscannerfile.inc_comment_level;
  990. var
  991. oldaktfilepos : tfileposinfo;
  992. begin
  993. if (m_nested_comment in aktmodeswitches) then
  994. inc(comment_level)
  995. else
  996. comment_level:=1;
  997. if (comment_level>1) then
  998. begin
  999. oldaktfilepos:=aktfilepos;
  1000. gettokenpos; { update for warning }
  1001. Message1(scan_w_comment_level,tostr(comment_level));
  1002. aktfilepos:=oldaktfilepos;
  1003. end;
  1004. end;
  1005. procedure tscannerfile.dec_comment_level;
  1006. begin
  1007. if (m_nested_comment in aktmodeswitches) then
  1008. dec(comment_level)
  1009. else
  1010. comment_level:=0;
  1011. end;
  1012. procedure tscannerfile.linebreak;
  1013. var
  1014. cur : char;
  1015. oldtokenpos,
  1016. oldaktfilepos : tfileposinfo;
  1017. begin
  1018. with inputfile do
  1019. begin
  1020. if (byte(inputpointer^)=0) and not(endoffile) then
  1021. begin
  1022. cur:=c;
  1023. reload;
  1024. if byte(cur)+byte(c)<>23 then
  1025. dec(longint(inputpointer));
  1026. end
  1027. else
  1028. begin
  1029. { Fix linebreak to be only newline (=#10) for all types of linebreaks }
  1030. if (byte(inputpointer^)+byte(c)=23) then
  1031. inc(longint(inputpointer));
  1032. end;
  1033. c:=newline;
  1034. { increase line counters }
  1035. lastlinepos:=bufstart+(inputpointer-inputbuffer);
  1036. inc(line_no);
  1037. { update linebuffer }
  1038. if cs_asm_source in aktglobalswitches then
  1039. inputfile.setline(line_no,lastlinepos);
  1040. { update for status and call the show status routine,
  1041. but don't touch aktfilepos ! }
  1042. oldaktfilepos:=aktfilepos;
  1043. oldtokenpos:=akttokenpos;
  1044. gettokenpos; { update for v_status }
  1045. inc(status.compiledlines);
  1046. ShowStatus;
  1047. aktfilepos:=oldaktfilepos;
  1048. akttokenpos:=oldtokenpos;
  1049. end;
  1050. end;
  1051. procedure tscannerfile.illegal_char(c:char);
  1052. var
  1053. s : string;
  1054. begin
  1055. if c in [#32..#255] then
  1056. s:=''''+c+''''
  1057. else
  1058. s:='#'+tostr(ord(c));
  1059. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  1060. end;
  1061. procedure tscannerfile.end_of_file;
  1062. begin
  1063. checkpreprocstack;
  1064. Message(scan_f_end_of_file);
  1065. end;
  1066. {-------------------------------------------
  1067. IF Conditional Handling
  1068. -------------------------------------------}
  1069. procedure tscannerfile.checkpreprocstack;
  1070. begin
  1071. { check for missing ifdefs }
  1072. while assigned(preprocstack) do
  1073. begin
  1074. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  1075. preprocstack.owner.inputfile.name^,tostr(preprocstack.line_nb));
  1076. poppreprocstack;
  1077. end;
  1078. end;
  1079. procedure tscannerfile.poppreprocstack;
  1080. var
  1081. hp : tpreprocstack;
  1082. begin
  1083. if assigned(preprocstack) then
  1084. begin
  1085. Message1(scan_c_endif_found,preprocstack.name);
  1086. hp:=preprocstack.next;
  1087. preprocstack.free;
  1088. preprocstack:=hp;
  1089. end
  1090. else
  1091. Message(scan_e_endif_without_if);
  1092. end;
  1093. procedure tscannerfile.addpreprocstack(atyp : preproctyp;a:boolean;const s:string;w:longint);
  1094. begin
  1095. preprocstack:=tpreprocstack.create(atyp,((preprocstack=nil) or preprocstack.accept) and a,preprocstack);
  1096. preprocstack.name:=s;
  1097. preprocstack.line_nb:=line_no;
  1098. preprocstack.owner:=self;
  1099. if preprocstack.accept then
  1100. Message2(w,preprocstack.name,'accepted')
  1101. else
  1102. Message2(w,preprocstack.name,'rejected');
  1103. end;
  1104. procedure tscannerfile.elsepreprocstack;
  1105. begin
  1106. if assigned(preprocstack) then
  1107. begin
  1108. preprocstack.typ:=pp_else;
  1109. preprocstack.line_nb:=line_no;
  1110. if not(assigned(preprocstack.next)) or (preprocstack.next.accept) then
  1111. preprocstack.accept:=not preprocstack.accept;
  1112. if preprocstack.accept then
  1113. Message2(scan_c_else_found,preprocstack.name,'accepted')
  1114. else
  1115. Message2(scan_c_else_found,preprocstack.name,'rejected');
  1116. end
  1117. else
  1118. Message(scan_e_endif_without_if);
  1119. end;
  1120. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  1121. var
  1122. oldaktfilepos : tfileposinfo;
  1123. begin
  1124. oldaktfilepos:=aktfilepos;
  1125. repeat
  1126. current_scanner.gettokenpos;
  1127. p.proc{$ifdef FPCPROCVAR}(){$endif};
  1128. { accept the text ? }
  1129. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  1130. break
  1131. else
  1132. begin
  1133. current_scanner.gettokenpos;
  1134. Message(scan_c_skipping_until);
  1135. repeat
  1136. current_scanner.skipuntildirective;
  1137. p:=tdirectiveitem(scannerdirectives.search(current_scanner.readid));
  1138. until assigned(p) and (p.is_conditional);
  1139. current_scanner.gettokenpos;
  1140. Message1(scan_d_handling_switch,'$'+p.name);
  1141. end;
  1142. until false;
  1143. aktfilepos:=oldaktfilepos;
  1144. end;
  1145. procedure tscannerfile.handledirectives;
  1146. var
  1147. t : tdirectiveitem;
  1148. hs : string;
  1149. begin
  1150. gettokenpos;
  1151. readchar; {Remove the $}
  1152. hs:=readid;
  1153. {$ifdef PREPROCWRITE}
  1154. if parapreprocess then
  1155. begin
  1156. t:=Get_Directive(hs);
  1157. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  1158. begin
  1159. preprocfile^.AddSpace;
  1160. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  1161. exit;
  1162. end;
  1163. end;
  1164. {$endif PREPROCWRITE}
  1165. { skip this directive? }
  1166. if (ignoredirectives.find(hs)<>nil) then
  1167. begin
  1168. if (comment_level>0) then
  1169. readcomment;
  1170. { we've read the whole comment }
  1171. aktcommentstyle:=comment_none;
  1172. exit;
  1173. end;
  1174. if hs='' then
  1175. begin
  1176. Message1(scan_w_illegal_switch,'$'+hs);
  1177. end;
  1178. { Check for compiler switches }
  1179. while (length(hs)=1) and (c in ['-','+']) do
  1180. begin
  1181. HandleSwitch(hs[1],c);
  1182. current_scanner.readchar; {Remove + or -}
  1183. if c=',' then
  1184. begin
  1185. current_scanner.readchar; {Remove , }
  1186. { read next switch, support $v+,$+}
  1187. hs:=current_scanner.readid;
  1188. if (hs='') then
  1189. begin
  1190. if (c='$') and (m_fpc in aktmodeswitches) then
  1191. begin
  1192. current_scanner.readchar; { skip $ }
  1193. hs:=current_scanner.readid;
  1194. end;
  1195. if (hs='') then
  1196. Message1(scan_w_illegal_directive,'$'+c);
  1197. end
  1198. else
  1199. Message1(scan_d_handling_switch,'$'+hs);
  1200. end
  1201. else
  1202. hs:='';
  1203. end;
  1204. { directives may follow switches after a , }
  1205. if hs<>'' then
  1206. begin
  1207. t:=tdirectiveitem(scannerdirectives.search(hs));
  1208. if assigned(t) then
  1209. begin
  1210. if t.is_conditional then
  1211. handleconditional(t)
  1212. else
  1213. t.proc{$ifdef FPCPROCVAR}(){$endif};
  1214. end
  1215. else
  1216. begin
  1217. current_scanner.ignoredirectives.insert(hs);
  1218. Message1(scan_w_illegal_directive,'$'+hs);
  1219. end;
  1220. { conditionals already read the comment }
  1221. if (current_scanner.comment_level>0) then
  1222. current_scanner.readcomment;
  1223. { we've read the whole comment }
  1224. aktcommentstyle:=comment_none;
  1225. end;
  1226. end;
  1227. procedure tscannerfile.readchar;
  1228. begin
  1229. c:=inputpointer^;
  1230. if c=#0 then
  1231. reload
  1232. else
  1233. inc(longint(inputpointer));
  1234. case c of
  1235. #26 : reload;
  1236. #10,
  1237. #13 : linebreak;
  1238. end;
  1239. end;
  1240. procedure tscannerfile.readstring;
  1241. var
  1242. i : longint;
  1243. begin
  1244. i:=0;
  1245. repeat
  1246. case c of
  1247. '_',
  1248. '0'..'9',
  1249. 'A'..'Z' : begin
  1250. if i<255 then
  1251. begin
  1252. inc(i);
  1253. orgpattern[i]:=c;
  1254. pattern[i]:=c;
  1255. end;
  1256. c:=inputpointer^;
  1257. inc(longint(inputpointer));
  1258. end;
  1259. 'a'..'z' : begin
  1260. if i<255 then
  1261. begin
  1262. inc(i);
  1263. orgpattern[i]:=c;
  1264. pattern[i]:=chr(ord(c)-32)
  1265. end;
  1266. c:=inputpointer^;
  1267. inc(longint(inputpointer));
  1268. end;
  1269. #0 : reload;
  1270. #26 : begin
  1271. reload;
  1272. if c=#26 then
  1273. break;
  1274. end;
  1275. #13,#10 : begin
  1276. linebreak;
  1277. break;
  1278. end;
  1279. else
  1280. break;
  1281. end;
  1282. until false;
  1283. orgpattern[0]:=chr(i);
  1284. pattern[0]:=chr(i);
  1285. end;
  1286. procedure tscannerfile.readnumber;
  1287. var
  1288. base,
  1289. i : longint;
  1290. begin
  1291. case c of
  1292. '%' : begin
  1293. readchar;
  1294. base:=2;
  1295. pattern[1]:='%';
  1296. i:=1;
  1297. end;
  1298. '$' : begin
  1299. readchar;
  1300. base:=16;
  1301. pattern[1]:='$';
  1302. i:=1;
  1303. end;
  1304. else
  1305. begin
  1306. base:=10;
  1307. i:=0;
  1308. end;
  1309. end;
  1310. while ((base>=10) and (c in ['0'..'9'])) or
  1311. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  1312. ((base=2) and (c in ['0'..'1'])) do
  1313. begin
  1314. if i<255 then
  1315. begin
  1316. inc(i);
  1317. pattern[i]:=c;
  1318. end;
  1319. { get next char }
  1320. c:=inputpointer^;
  1321. if c=#0 then
  1322. reload
  1323. else
  1324. inc(longint(inputpointer));
  1325. end;
  1326. { was the next char a linebreak ? }
  1327. case c of
  1328. #26 : reload;
  1329. #10,
  1330. #13 : linebreak;
  1331. end;
  1332. pattern[0]:=chr(i);
  1333. end;
  1334. function tscannerfile.readid:string;
  1335. begin
  1336. readstring;
  1337. readid:=pattern;
  1338. end;
  1339. function tscannerfile.readval:longint;
  1340. var
  1341. l : longint;
  1342. w : integer;
  1343. begin
  1344. readnumber;
  1345. valint(pattern,l,w);
  1346. readval:=l;
  1347. end;
  1348. function tscannerfile.readcomment:string;
  1349. var
  1350. i : longint;
  1351. begin
  1352. i:=0;
  1353. repeat
  1354. case c of
  1355. '{' :
  1356. if aktcommentstyle=comment_tp then
  1357. inc_comment_level;
  1358. '}' :
  1359. if aktcommentstyle=comment_tp then
  1360. begin
  1361. readchar;
  1362. dec_comment_level;
  1363. if comment_level=0 then
  1364. break
  1365. else
  1366. continue;
  1367. end;
  1368. '*' :
  1369. if aktcommentstyle=comment_oldtp then
  1370. begin
  1371. readchar;
  1372. if c=')' then
  1373. begin
  1374. readchar;
  1375. dec_comment_level;
  1376. break;
  1377. end
  1378. else
  1379. { Add both characters !!}
  1380. if (i<255) then
  1381. begin
  1382. inc(i);
  1383. readcomment[i]:='*';
  1384. if (i<255) then
  1385. begin
  1386. inc(i);
  1387. readcomment[i]:='*';
  1388. end;
  1389. end;
  1390. end
  1391. else
  1392. { Not old TP comment, so add...}
  1393. begin
  1394. if (i<255) then
  1395. begin
  1396. inc(i);
  1397. readcomment[i]:='*';
  1398. end;
  1399. end;
  1400. #26 :
  1401. end_of_file;
  1402. else
  1403. begin
  1404. if (i<255) then
  1405. begin
  1406. inc(i);
  1407. readcomment[i]:=c;
  1408. end;
  1409. end;
  1410. end;
  1411. c:=inputpointer^;
  1412. if c=#0 then
  1413. reload
  1414. else
  1415. inc(longint(inputpointer));
  1416. if c in [#10,#13] then
  1417. linebreak;
  1418. until false;
  1419. readcomment[0]:=chr(i);
  1420. end;
  1421. function tscannerfile.readstate:char;
  1422. var
  1423. state : char;
  1424. begin
  1425. state:=' ';
  1426. if c=' ' then
  1427. begin
  1428. current_scanner.skipspace;
  1429. current_scanner.readid;
  1430. if pattern='ON' then
  1431. state:='+'
  1432. else
  1433. if pattern='OFF' then
  1434. state:='-';
  1435. end
  1436. else
  1437. state:=c;
  1438. if not (state in ['+','-']) then
  1439. Message(scan_e_wrong_switch_toggle);
  1440. readstate:=state;
  1441. end;
  1442. procedure tscannerfile.skipspace;
  1443. begin
  1444. while c in [' ',#9..#13] do
  1445. begin
  1446. c:=inputpointer^;
  1447. if c=#0 then
  1448. reload
  1449. else
  1450. inc(longint(inputpointer));
  1451. case c of
  1452. #26 :
  1453. reload;
  1454. #10,
  1455. #13 :
  1456. linebreak;
  1457. end;
  1458. end;
  1459. end;
  1460. procedure tscannerfile.skipuntildirective;
  1461. var
  1462. incomment : boolean;
  1463. found : longint;
  1464. next_char_loaded : boolean;
  1465. oldcommentstyle : tcommentstyle;
  1466. begin
  1467. found:=0;
  1468. next_char_loaded:=false;
  1469. incomment:=true;
  1470. oldcommentstyle:=aktcommentstyle;
  1471. repeat
  1472. case c of
  1473. #26 :
  1474. end_of_file;
  1475. '{' :
  1476. begin
  1477. if not(m_nested_comment in aktmodeswitches) or
  1478. (comment_level=0) then
  1479. begin
  1480. found:=1;
  1481. aktcommentstyle:=comment_tp;
  1482. end;
  1483. inc_comment_level;
  1484. incomment:=true;
  1485. end;
  1486. '}' :
  1487. begin
  1488. dec_comment_level;
  1489. found:=0;
  1490. incomment:=false;
  1491. end;
  1492. '$' :
  1493. begin
  1494. if found=1 then
  1495. found:=2;
  1496. end;
  1497. '''' :
  1498. if not incomment then
  1499. begin
  1500. repeat
  1501. readchar;
  1502. case c of
  1503. #26 :
  1504. end_of_file;
  1505. newline :
  1506. break;
  1507. '''' :
  1508. begin
  1509. readchar;
  1510. if c<>'''' then
  1511. begin
  1512. next_char_loaded:=true;
  1513. break;
  1514. end;
  1515. end;
  1516. end;
  1517. until false;
  1518. end;
  1519. '(' :
  1520. begin
  1521. readchar;
  1522. if c='*' then
  1523. begin
  1524. readchar;
  1525. if c='$' then
  1526. begin
  1527. found:=2;
  1528. inc_comment_level;
  1529. aktcommentstyle:=comment_oldtp;
  1530. end
  1531. else
  1532. begin
  1533. skipoldtpcomment;
  1534. aktcommentstyle:=oldcommentstyle;
  1535. end;
  1536. end
  1537. else
  1538. next_char_loaded:=true;
  1539. end;
  1540. else
  1541. found:=0;
  1542. end;
  1543. if next_char_loaded then
  1544. next_char_loaded:=false
  1545. else
  1546. begin
  1547. c:=inputpointer^;
  1548. if c=#0 then
  1549. reload
  1550. else
  1551. inc(longint(inputpointer));
  1552. case c of
  1553. #26 : reload;
  1554. #10,
  1555. #13 : linebreak;
  1556. end;
  1557. end;
  1558. until (found=2);
  1559. end;
  1560. {****************************************************************************
  1561. Comment Handling
  1562. ****************************************************************************}
  1563. procedure tscannerfile.skipcomment;
  1564. begin
  1565. aktcommentstyle:=comment_tp;
  1566. readchar;
  1567. inc_comment_level;
  1568. { handle compiler switches }
  1569. if (c='$') then
  1570. handledirectives;
  1571. { handle_switches can dec comment_level, }
  1572. while (comment_level>0) do
  1573. begin
  1574. case c of
  1575. '{' : inc_comment_level;
  1576. '}' : dec_comment_level;
  1577. #26 : end_of_file;
  1578. end;
  1579. c:=inputpointer^;
  1580. if c=#0 then
  1581. reload
  1582. else
  1583. inc(longint(inputpointer));
  1584. case c of
  1585. #26 : reload;
  1586. #10,
  1587. #13 : linebreak;
  1588. end;
  1589. end;
  1590. aktcommentstyle:=comment_none;
  1591. end;
  1592. procedure tscannerfile.skipdelphicomment;
  1593. begin
  1594. aktcommentstyle:=comment_delphi;
  1595. inc_comment_level;
  1596. readchar;
  1597. { this is currently not supported }
  1598. if c='$' then
  1599. Message(scan_e_wrong_styled_switch);
  1600. { skip comment }
  1601. while c<>newline do
  1602. begin
  1603. if c=#26 then
  1604. end_of_file;
  1605. readchar;
  1606. end;
  1607. dec_comment_level;
  1608. aktcommentstyle:=comment_none;
  1609. end;
  1610. procedure tscannerfile.skipoldtpcomment;
  1611. var
  1612. found : longint;
  1613. begin
  1614. aktcommentstyle:=comment_oldtp;
  1615. inc_comment_level;
  1616. readchar;
  1617. { this is currently not supported }
  1618. if (c='$') then
  1619. handledirectives;
  1620. { skip comment }
  1621. while (comment_level>0) do
  1622. begin
  1623. found:=0;
  1624. repeat
  1625. case c of
  1626. #26 :
  1627. end_of_file;
  1628. '*' :
  1629. begin
  1630. if found=3 then
  1631. found:=4
  1632. else
  1633. found:=1;
  1634. end;
  1635. ')' :
  1636. begin
  1637. if found in [1,4] then
  1638. begin
  1639. dec_comment_level;
  1640. if comment_level=0 then
  1641. found:=2
  1642. else
  1643. found:=0;
  1644. end;
  1645. end;
  1646. '(' :
  1647. begin
  1648. if found=4 then
  1649. inc_comment_level;
  1650. found:=3;
  1651. end;
  1652. else
  1653. begin
  1654. if found=4 then
  1655. inc_comment_level;
  1656. found:=0;
  1657. end;
  1658. end;
  1659. c:=inputpointer^;
  1660. if c=#0 then
  1661. reload
  1662. else
  1663. inc(longint(inputpointer));
  1664. case c of
  1665. #26 : reload;
  1666. #10,
  1667. #13 : linebreak;
  1668. end;
  1669. until (found=2);
  1670. end;
  1671. aktcommentstyle:=comment_none;
  1672. end;
  1673. {****************************************************************************
  1674. Token Scanner
  1675. ****************************************************************************}
  1676. procedure tscannerfile.readtoken;
  1677. var
  1678. code : integer;
  1679. low,high,mid : longint;
  1680. m : longint;
  1681. mac : tmacro;
  1682. asciinr : string[6];
  1683. iswidestring : boolean;
  1684. label
  1685. exit_label;
  1686. begin
  1687. if localswitcheschanged then
  1688. begin
  1689. aktlocalswitches:=nextaktlocalswitches;
  1690. localswitcheschanged:=false;
  1691. end;
  1692. { was there already a token read, then return that token }
  1693. if nexttoken<>NOTOKEN then
  1694. begin
  1695. token:=nexttoken;
  1696. nexttoken:=NOTOKEN;
  1697. goto exit_label;
  1698. end;
  1699. { Skip all spaces and comments }
  1700. repeat
  1701. case c of
  1702. '{' :
  1703. skipcomment;
  1704. ' ',#9..#13 :
  1705. begin
  1706. {$ifdef PREPROCWRITE}
  1707. if parapreprocess then
  1708. begin
  1709. if c=#10 then
  1710. preprocfile.eolfound:=true
  1711. else
  1712. preprocfile.spacefound:=true;
  1713. end;
  1714. {$endif PREPROCWRITE}
  1715. skipspace;
  1716. end
  1717. else
  1718. break;
  1719. end;
  1720. until false;
  1721. { Save current token position, for EOF its already loaded }
  1722. if c<>#26 then
  1723. gettokenpos;
  1724. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  1725. if c in ['A'..'Z','a'..'z','_'] then
  1726. begin
  1727. readstring;
  1728. token:=_ID;
  1729. idtoken:=_ID;
  1730. { keyword or any other known token,
  1731. pattern is always uppercased }
  1732. if (pattern[1]<>'_') and (length(pattern) in [2..tokenidlen]) then
  1733. begin
  1734. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  1735. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  1736. while low<high do
  1737. begin
  1738. mid:=(high+low+1) shr 1;
  1739. if pattern<tokeninfo^[ttoken(mid)].str then
  1740. high:=mid-1
  1741. else
  1742. low:=mid;
  1743. end;
  1744. if pattern=tokeninfo^[ttoken(high)].str then
  1745. begin
  1746. if tokeninfo^[ttoken(high)].keyword in aktmodeswitches then
  1747. if tokeninfo^[ttoken(high)].op=NOTOKEN then
  1748. token:=ttoken(high)
  1749. else
  1750. token:=tokeninfo^[ttoken(high)].op;
  1751. idtoken:=ttoken(high);
  1752. end;
  1753. end;
  1754. { Only process identifiers and not keywords }
  1755. if token=_ID then
  1756. begin
  1757. { this takes some time ... }
  1758. if (cs_support_macro in aktmoduleswitches) then
  1759. begin
  1760. mac:=tmacro(macros.search(pattern));
  1761. if assigned(mac) and (assigned(mac.buftext)) then
  1762. begin
  1763. insertmacro(pattern,mac.buftext,mac.buflen);
  1764. { handle empty macros }
  1765. if c=#0 then
  1766. begin
  1767. reload;
  1768. case c of
  1769. #26 : reload;
  1770. #10,
  1771. #13 : linebreak;
  1772. end;
  1773. end;
  1774. { play it again ... }
  1775. inc(yylexcount);
  1776. if yylexcount>16 then
  1777. Message(scan_w_macro_deep_ten);
  1778. readtoken;
  1779. { that's all folks }
  1780. dec(yylexcount);
  1781. exit;
  1782. end;
  1783. end;
  1784. end;
  1785. { return token }
  1786. goto exit_label;
  1787. end
  1788. else
  1789. begin
  1790. idtoken:=_NOID;
  1791. case c of
  1792. '$' :
  1793. begin
  1794. readnumber;
  1795. token:=_INTCONST;
  1796. goto exit_label;
  1797. end;
  1798. '%' :
  1799. begin
  1800. if (m_tp in aktmodeswitches) then
  1801. Illegal_Char(c)
  1802. else
  1803. begin
  1804. readnumber;
  1805. token:=_INTCONST;
  1806. goto exit_label;
  1807. end;
  1808. end;
  1809. '0'..'9' :
  1810. begin
  1811. readnumber;
  1812. if (c in ['.','e','E']) then
  1813. begin
  1814. { first check for a . }
  1815. if c='.' then
  1816. begin
  1817. readchar;
  1818. { is it a .. from a range? }
  1819. case c of
  1820. '.' :
  1821. begin
  1822. readchar;
  1823. token:=_INTCONST;
  1824. nexttoken:=_POINTPOINT;
  1825. goto exit_label;
  1826. end;
  1827. ')' :
  1828. begin
  1829. readchar;
  1830. token:=_INTCONST;
  1831. nexttoken:=_RECKKLAMMER;
  1832. goto exit_label;
  1833. end;
  1834. end;
  1835. { insert the number after the . }
  1836. pattern:=pattern+'.';
  1837. while c in ['0'..'9'] do
  1838. begin
  1839. pattern:=pattern+c;
  1840. readchar;
  1841. end;
  1842. end;
  1843. { E can also follow after a point is scanned }
  1844. if c in ['e','E'] then
  1845. begin
  1846. pattern:=pattern+'E';
  1847. readchar;
  1848. if c in ['-','+'] then
  1849. begin
  1850. pattern:=pattern+c;
  1851. readchar;
  1852. end;
  1853. if not(c in ['0'..'9']) then
  1854. Illegal_Char(c);
  1855. while c in ['0'..'9'] do
  1856. begin
  1857. pattern:=pattern+c;
  1858. readchar;
  1859. end;
  1860. end;
  1861. token:=_REALNUMBER;
  1862. goto exit_label;
  1863. end;
  1864. token:=_INTCONST;
  1865. goto exit_label;
  1866. end;
  1867. ';' :
  1868. begin
  1869. readchar;
  1870. token:=_SEMICOLON;
  1871. goto exit_label;
  1872. end;
  1873. '[' :
  1874. begin
  1875. readchar;
  1876. token:=_LECKKLAMMER;
  1877. goto exit_label;
  1878. end;
  1879. ']' :
  1880. begin
  1881. readchar;
  1882. token:=_RECKKLAMMER;
  1883. goto exit_label;
  1884. end;
  1885. '(' :
  1886. begin
  1887. readchar;
  1888. case c of
  1889. '*' :
  1890. begin
  1891. skipoldtpcomment;
  1892. readtoken;
  1893. exit;
  1894. end;
  1895. '.' :
  1896. begin
  1897. readchar;
  1898. token:=_LECKKLAMMER;
  1899. goto exit_label;
  1900. end;
  1901. end;
  1902. token:=_LKLAMMER;
  1903. goto exit_label;
  1904. end;
  1905. ')' :
  1906. begin
  1907. readchar;
  1908. token:=_RKLAMMER;
  1909. goto exit_label;
  1910. end;
  1911. '+' :
  1912. begin
  1913. readchar;
  1914. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  1915. begin
  1916. readchar;
  1917. token:=_PLUSASN;
  1918. goto exit_label;
  1919. end;
  1920. token:=_PLUS;
  1921. goto exit_label;
  1922. end;
  1923. '-' :
  1924. begin
  1925. readchar;
  1926. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  1927. begin
  1928. readchar;
  1929. token:=_MINUSASN;
  1930. goto exit_label;
  1931. end;
  1932. token:=_MINUS;
  1933. goto exit_label;
  1934. end;
  1935. ':' :
  1936. begin
  1937. readchar;
  1938. if c='=' then
  1939. begin
  1940. readchar;
  1941. token:=_ASSIGNMENT;
  1942. goto exit_label;
  1943. end;
  1944. token:=_COLON;
  1945. goto exit_label;
  1946. end;
  1947. '*' :
  1948. begin
  1949. readchar;
  1950. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  1951. begin
  1952. readchar;
  1953. token:=_STARASN;
  1954. end
  1955. else
  1956. if c='*' then
  1957. begin
  1958. readchar;
  1959. token:=_STARSTAR;
  1960. end
  1961. else
  1962. token:=_STAR;
  1963. goto exit_label;
  1964. end;
  1965. '/' :
  1966. begin
  1967. readchar;
  1968. case c of
  1969. '=' :
  1970. begin
  1971. if (cs_support_c_operators in aktmoduleswitches) then
  1972. begin
  1973. readchar;
  1974. token:=_SLASHASN;
  1975. goto exit_label;
  1976. end;
  1977. end;
  1978. '/' :
  1979. begin
  1980. skipdelphicomment;
  1981. readtoken;
  1982. exit;
  1983. end;
  1984. end;
  1985. token:=_SLASH;
  1986. goto exit_label;
  1987. end;
  1988. '=' :
  1989. begin
  1990. readchar;
  1991. token:=_EQUAL;
  1992. goto exit_label;
  1993. end;
  1994. '.' :
  1995. begin
  1996. readchar;
  1997. case c of
  1998. '.' :
  1999. begin
  2000. readchar;
  2001. token:=_POINTPOINT;
  2002. goto exit_label;
  2003. end;
  2004. ')' :
  2005. begin
  2006. readchar;
  2007. token:=_RECKKLAMMER;
  2008. goto exit_label;
  2009. end;
  2010. end;
  2011. token:=_POINT;
  2012. goto exit_label;
  2013. end;
  2014. '@' :
  2015. begin
  2016. readchar;
  2017. if c='@' then
  2018. begin
  2019. readchar;
  2020. token:=_DOUBLEADDR;
  2021. end
  2022. else
  2023. token:=_KLAMMERAFFE;
  2024. goto exit_label;
  2025. end;
  2026. ',' :
  2027. begin
  2028. readchar;
  2029. token:=_COMMA;
  2030. goto exit_label;
  2031. end;
  2032. '''','#','^' :
  2033. begin
  2034. iswidestring:=false;
  2035. if c='^' then
  2036. begin
  2037. readchar;
  2038. c:=upcase(c);
  2039. if (block_type=bt_type) or
  2040. (lasttoken=_ID) or
  2041. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  2042. begin
  2043. token:=_CARET;
  2044. goto exit_label;
  2045. end
  2046. else
  2047. begin
  2048. if c<#64 then
  2049. pattern:=chr(ord(c)+64)
  2050. else
  2051. pattern:=chr(ord(c)-64);
  2052. readchar;
  2053. end;
  2054. end
  2055. else
  2056. pattern:='';
  2057. repeat
  2058. case c of
  2059. '#' :
  2060. begin
  2061. readchar; { read # }
  2062. if c='$' then
  2063. begin
  2064. readchar; { read leading $ }
  2065. asciinr:='$';
  2066. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<6) do
  2067. begin
  2068. asciinr:=asciinr+c;
  2069. readchar;
  2070. end;
  2071. end
  2072. else
  2073. begin
  2074. asciinr:='';
  2075. while (c in ['0'..'9']) and (length(asciinr)<6) do
  2076. begin
  2077. asciinr:=asciinr+c;
  2078. readchar;
  2079. end;
  2080. end;
  2081. valint(asciinr,m,code);
  2082. if (asciinr='') or (code<>0) then
  2083. Message(scan_e_illegal_char_const)
  2084. else if (m<0) or (m>255) then
  2085. begin
  2086. if (m>=0) and (m<=65535) then
  2087. begin
  2088. ascii2unicode(pattern,patternw);
  2089. concatwidestringchar(patternw,
  2090. tcompilerwidechar(m));
  2091. iswidestring:=true;
  2092. end
  2093. else
  2094. Message(scan_e_illegal_char_const)
  2095. end
  2096. else if iswidestring then
  2097. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  2098. else
  2099. pattern:=pattern+chr(m);
  2100. end;
  2101. '''' :
  2102. begin
  2103. repeat
  2104. readchar;
  2105. case c of
  2106. #26 :
  2107. end_of_file;
  2108. newline :
  2109. Message(scan_f_string_exceeds_line);
  2110. '''' :
  2111. begin
  2112. readchar;
  2113. if c<>'''' then
  2114. break;
  2115. end;
  2116. end;
  2117. if iswidestring then
  2118. concatwidestringchar(patternw,
  2119. asciichar2unicode(c))
  2120. else
  2121. pattern:=pattern+c;
  2122. until false;
  2123. end;
  2124. '^' :
  2125. begin
  2126. readchar;
  2127. c:=upcase(c);
  2128. if c<#64 then
  2129. c:=chr(ord(c)+64)
  2130. else
  2131. c:=chr(ord(c)-64);
  2132. if iswidestring then
  2133. concatwidestringchar(patternw,
  2134. asciichar2unicode(c))
  2135. else
  2136. pattern:=pattern+c;
  2137. readchar;
  2138. end;
  2139. else
  2140. break;
  2141. end;
  2142. until false;
  2143. { strings with length 1 become const chars }
  2144. if iswidestring then
  2145. begin
  2146. if length(pattern)=1 then
  2147. token:=_CWCHAR
  2148. else
  2149. token:=_CWSTRING;
  2150. end
  2151. else
  2152. begin
  2153. if length(pattern)=1 then
  2154. token:=_CCHAR
  2155. else
  2156. token:=_CSTRING;
  2157. end;
  2158. goto exit_label;
  2159. end;
  2160. '>' :
  2161. begin
  2162. readchar;
  2163. case c of
  2164. '=' :
  2165. begin
  2166. readchar;
  2167. token:=_GTE;
  2168. goto exit_label;
  2169. end;
  2170. '>' :
  2171. begin
  2172. readchar;
  2173. token:=_OP_SHR;
  2174. goto exit_label;
  2175. end;
  2176. '<' :
  2177. begin { >< is for a symetric diff for sets }
  2178. readchar;
  2179. token:=_SYMDIF;
  2180. goto exit_label;
  2181. end;
  2182. end;
  2183. token:=_GT;
  2184. goto exit_label;
  2185. end;
  2186. '<' :
  2187. begin
  2188. readchar;
  2189. case c of
  2190. '>' :
  2191. begin
  2192. readchar;
  2193. token:=_UNEQUAL;
  2194. goto exit_label;
  2195. end;
  2196. '=' :
  2197. begin
  2198. readchar;
  2199. token:=_LTE;
  2200. goto exit_label;
  2201. end;
  2202. '<' :
  2203. begin
  2204. readchar;
  2205. token:=_OP_SHL;
  2206. goto exit_label;
  2207. end;
  2208. end;
  2209. token:=_LT;
  2210. goto exit_label;
  2211. end;
  2212. #26 :
  2213. begin
  2214. token:=_EOF;
  2215. checkpreprocstack;
  2216. goto exit_label;
  2217. end;
  2218. else
  2219. Illegal_Char(c);
  2220. end;
  2221. end;
  2222. exit_label:
  2223. lasttoken:=token;
  2224. end;
  2225. function tscannerfile.readpreproc:ttoken;
  2226. begin
  2227. skipspace;
  2228. case c of
  2229. 'A'..'Z',
  2230. 'a'..'z',
  2231. '_','0'..'9' : begin
  2232. current_scanner.preproc_pattern:=readid;
  2233. readpreproc:=_ID;
  2234. end;
  2235. '}' : begin
  2236. readpreproc:=_END;
  2237. end;
  2238. '(' : begin
  2239. readchar;
  2240. readpreproc:=_LKLAMMER;
  2241. end;
  2242. ')' : begin
  2243. readchar;
  2244. readpreproc:=_RKLAMMER;
  2245. end;
  2246. '+' : begin
  2247. readchar;
  2248. readpreproc:=_PLUS;
  2249. end;
  2250. '-' : begin
  2251. readchar;
  2252. readpreproc:=_MINUS;
  2253. end;
  2254. '*' : begin
  2255. readchar;
  2256. readpreproc:=_STAR;
  2257. end;
  2258. '/' : begin
  2259. readchar;
  2260. readpreproc:=_SLASH;
  2261. end;
  2262. '=' : begin
  2263. readchar;
  2264. readpreproc:=_EQUAL;
  2265. end;
  2266. '>' : begin
  2267. readchar;
  2268. if c='=' then
  2269. begin
  2270. readchar;
  2271. readpreproc:=_GTE;
  2272. end
  2273. else
  2274. readpreproc:=_GT;
  2275. end;
  2276. '<' : begin
  2277. readchar;
  2278. case c of
  2279. '>' : begin
  2280. readchar;
  2281. readpreproc:=_UNEQUAL;
  2282. end;
  2283. '=' : begin
  2284. readchar;
  2285. readpreproc:=_LTE;
  2286. end;
  2287. else readpreproc:=_LT;
  2288. end;
  2289. end;
  2290. #26 :
  2291. end_of_file;
  2292. else
  2293. begin
  2294. readpreproc:=_EOF;
  2295. checkpreprocstack;
  2296. end;
  2297. end;
  2298. end;
  2299. function tscannerfile.asmgetchar : char;
  2300. begin
  2301. if lastasmgetchar<>#0 then
  2302. begin
  2303. c:=lastasmgetchar;
  2304. lastasmgetchar:=#0;
  2305. end
  2306. else
  2307. readchar;
  2308. if in_asm_string then
  2309. begin
  2310. asmgetchar:=c;
  2311. exit;
  2312. end;
  2313. repeat
  2314. case c of
  2315. '{' :
  2316. skipcomment;
  2317. '/' :
  2318. begin
  2319. readchar;
  2320. if c='/' then
  2321. skipdelphicomment
  2322. else
  2323. begin
  2324. asmgetchar:='/';
  2325. lastasmgetchar:=c;
  2326. exit;
  2327. end;
  2328. end;
  2329. '(' :
  2330. begin
  2331. readchar;
  2332. if c='*' then
  2333. skipoldtpcomment
  2334. else
  2335. begin
  2336. asmgetchar:='(';
  2337. lastasmgetchar:=c;
  2338. exit;
  2339. end;
  2340. end;
  2341. else
  2342. begin
  2343. asmgetchar:=c;
  2344. exit;
  2345. end;
  2346. end;
  2347. until false;
  2348. end;
  2349. {*****************************************************************************
  2350. Helpers
  2351. *****************************************************************************}
  2352. procedure adddirective(const s:string;p:tdirectiveproc);
  2353. begin
  2354. scannerdirectives.insert(tdirectiveitem.create(s,p));
  2355. end;
  2356. procedure addconditional(const s:string;p:tdirectiveproc);
  2357. begin
  2358. scannerdirectives.insert(tdirectiveitem.createcond(s,p));
  2359. end;
  2360. {*****************************************************************************
  2361. Initialization
  2362. *****************************************************************************}
  2363. procedure InitScanner;
  2364. begin
  2365. scannerdirectives:=TDictionary.Create;
  2366. { Default directives }
  2367. AddDirective('DEFINE',{$ifdef FPCPROCVAR}@{$endif}dir_define);
  2368. AddDirective('UNDEF',{$ifdef FPCPROCVAR}@{$endif}dir_undef);
  2369. AddDirective('I',{$ifdef FPCPROCVAR}@{$endif}dir_include);
  2370. AddDirective('INCLUDE',{$ifdef FPCPROCVAR}@{$endif}dir_include);
  2371. { Default conditionals }
  2372. AddConditional('ELSE',{$ifdef FPCPROCVAR}@{$endif}dir_else);
  2373. AddConditional('ENDIF',{$ifdef FPCPROCVAR}@{$endif}dir_endif);
  2374. AddConditional('IF',{$ifdef FPCPROCVAR}@{$endif}dir_if);
  2375. AddConditional('IFDEF',{$ifdef FPCPROCVAR}@{$endif}dir_ifdef);
  2376. AddConditional('IFNDEF',{$ifdef FPCPROCVAR}@{$endif}dir_ifndef);
  2377. AddConditional('IFOPT',{$ifdef FPCPROCVAR}@{$endif}dir_ifopt);
  2378. end;
  2379. procedure DoneScanner;
  2380. begin
  2381. scannerdirectives.Free;
  2382. end;
  2383. end.
  2384. {
  2385. $Log$
  2386. Revision 1.17 2001-05-27 14:30:55 florian
  2387. + some widestring stuff added
  2388. Revision 1.16 2001/04/13 22:12:34 peter
  2389. * fixed comment after comment parsing in assembler blocks
  2390. Revision 1.15 2001/04/13 18:00:36 peter
  2391. * easier registration of directives
  2392. Revision 1.14 2001/04/13 01:22:13 peter
  2393. * symtable change to classes
  2394. * range check generation and errors fixed, make cycle DEBUG=1 works
  2395. * memory leaks fixed
  2396. Revision 1.13 2000/12/25 00:07:28 peter
  2397. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  2398. tlinkedlist objects)
  2399. Revision 1.12 2000/12/24 12:24:38 peter
  2400. * moved preprocessfile into a conditional
  2401. Revision 1.11 2000/12/18 17:59:01 peter
  2402. * fixed skipuntildirective
  2403. Revision 1.10 2000/12/16 15:36:02 peter
  2404. * fixed parsing of strings and comments in skipuntildirective
  2405. Revision 1.9 2000/11/30 20:27:51 peter
  2406. * merged fix for bug 1229
  2407. Revision 1.8 2000/11/29 00:30:40 florian
  2408. * unused units removed from uses clause
  2409. * some changes for widestrings
  2410. Revision 1.7 2000/10/31 22:02:51 peter
  2411. * symtable splitted, no real code changes
  2412. Revision 1.6 2000/09/24 15:06:28 peter
  2413. * use defines.inc
  2414. Revision 1.5 2000/08/27 16:11:53 peter
  2415. * moved some util functions from globals,cobjects to cutils
  2416. * splitted files into finput,fmodule
  2417. Revision 1.4 2000/08/12 15:30:44 peter
  2418. * IDE patch for stream reading (merged)
  2419. Revision 1.3 2000/08/08 19:28:57 peter
  2420. * memdebug/memory patches (merged)
  2421. * only once illegal directive (merged)
  2422. Revision 1.2 2000/07/13 11:32:49 michael
  2423. + removed logs
  2424. }