scanner.pas 83 KB

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