scanner.pas 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833
  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,cpuinfo;
  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. public
  65. inputfile : tinputfile; { current inputfile list }
  66. inputbuffer, { input buffer }
  67. inputpointer : pchar;
  68. inputstart : longint;
  69. line_no, { line }
  70. lastlinepos : longint;
  71. lasttokenpos : longint; { token }
  72. lasttoken,
  73. nexttoken : ttoken;
  74. comment_level,
  75. yylexcount : longint;
  76. lastasmgetchar : char;
  77. ignoredirectives : tstringlist; { ignore directives, used to give warnings only once }
  78. preprocstack : tpreprocstack;
  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. in_asm_string:=false;
  822. macros:=tdictionary.create;
  823. end;
  824. procedure tscannerfile.firstfile;
  825. begin
  826. { load block }
  827. if not openinputfile then
  828. Message1(scan_f_cannot_open_input,inputfile.name^);
  829. reload;
  830. { process first read char }
  831. case c of
  832. #26 : reload;
  833. #10,
  834. #13 : linebreak;
  835. end;
  836. end;
  837. destructor tscannerfile.destroy;
  838. begin
  839. if (not current_module.in_second_load) and
  840. (status.errorcount=0) then
  841. checkpreprocstack
  842. else
  843. begin
  844. while assigned(preprocstack) do
  845. poppreprocstack;
  846. end;
  847. if not inputfile.closed then
  848. closeinputfile;
  849. ignoredirectives.free;
  850. macros.free;
  851. end;
  852. procedure tscannerfile.def_macro(const s : string);
  853. var
  854. mac : tmacro;
  855. begin
  856. mac:=tmacro(macros.search(s));
  857. if mac=nil then
  858. begin
  859. mac:=tmacro.create(s);
  860. Message1(parser_m_macro_defined,mac.name);
  861. macros.insert(mac);
  862. end;
  863. mac.defined:=true;
  864. mac.defined_at_startup:=true;
  865. end;
  866. procedure tscannerfile.set_macro(const s : string;value : string);
  867. var
  868. mac : tmacro;
  869. begin
  870. mac:=tmacro(macros.search(s));
  871. if mac=nil then
  872. begin
  873. mac:=tmacro.create(s);
  874. macros.insert(mac);
  875. end
  876. else
  877. begin
  878. if assigned(mac.buftext) then
  879. freemem(mac.buftext,mac.buflen);
  880. end;
  881. Message2(parser_m_macro_set_to,mac.name,value);
  882. mac.buflen:=length(value);
  883. getmem(mac.buftext,mac.buflen);
  884. move(value[1],mac.buftext^,mac.buflen);
  885. mac.defined:=true;
  886. mac.defined_at_startup:=true;
  887. end;
  888. function tscannerfile.openinputfile:boolean;
  889. begin
  890. openinputfile:=inputfile.open;
  891. { load buffer }
  892. inputbuffer:=inputfile.buf;
  893. inputpointer:=inputfile.buf;
  894. inputstart:=inputfile.bufstart;
  895. { line }
  896. line_no:=0;
  897. lastlinepos:=0;
  898. lasttokenpos:=0;
  899. end;
  900. procedure tscannerfile.closeinputfile;
  901. begin
  902. inputfile.close;
  903. { reset buffer }
  904. inputbuffer:=nil;
  905. inputpointer:=nil;
  906. inputstart:=0;
  907. { reset line }
  908. line_no:=0;
  909. lastlinepos:=0;
  910. lasttokenpos:=0;
  911. end;
  912. function tscannerfile.tempopeninputfile:boolean;
  913. begin
  914. tempopeninputfile:=inputfile.tempopen;
  915. { reload buffer }
  916. inputbuffer:=inputfile.buf;
  917. inputpointer:=inputfile.buf;
  918. inputstart:=inputfile.bufstart;
  919. end;
  920. procedure tscannerfile.tempcloseinputfile;
  921. begin
  922. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  923. inputfile.tempclose;
  924. { reset buffer }
  925. inputbuffer:=nil;
  926. inputpointer:=nil;
  927. inputstart:=0;
  928. end;
  929. procedure tscannerfile.saveinputfile;
  930. begin
  931. inputfile.saveinputpointer:=inputpointer;
  932. inputfile.savelastlinepos:=lastlinepos;
  933. inputfile.saveline_no:=line_no;
  934. end;
  935. procedure tscannerfile.restoreinputfile;
  936. begin
  937. inputpointer:=inputfile.saveinputpointer;
  938. lastlinepos:=inputfile.savelastlinepos;
  939. line_no:=inputfile.saveline_no;
  940. if not inputfile.is_macro then
  941. parser_current_file:=inputfile.name^;
  942. end;
  943. procedure tscannerfile.nextfile;
  944. var
  945. to_dispose : tinputfile;
  946. begin
  947. if assigned(inputfile.next) then
  948. begin
  949. if inputfile.is_macro then
  950. to_dispose:=inputfile
  951. else
  952. to_dispose:=nil;
  953. { we can allways close the file, no ? }
  954. inputfile.close;
  955. inputfile:=inputfile.next;
  956. if assigned(to_dispose) then
  957. to_dispose.free;
  958. restoreinputfile;
  959. end;
  960. end;
  961. procedure tscannerfile.addfile(hp:tinputfile);
  962. begin
  963. saveinputfile;
  964. { add to list }
  965. hp.next:=inputfile;
  966. inputfile:=hp;
  967. { load new inputfile }
  968. restoreinputfile;
  969. end;
  970. procedure tscannerfile.reload;
  971. begin
  972. with inputfile do
  973. begin
  974. { when nothing more to read then leave immediatly, so we
  975. don't change the aktfilepos and leave it point to the last
  976. char }
  977. if (c=#26) and (not assigned(next)) then
  978. exit;
  979. repeat
  980. { still more to read?, then change the #0 to a space so its seen
  981. as a seperator, this can't be used for macro's which can change
  982. the place of the #0 in the buffer with tempopen }
  983. if (c=#0) and (bufsize>0) and
  984. not(inputfile.is_macro) and
  985. (inputpointer-inputbuffer<bufsize) then
  986. begin
  987. c:=' ';
  988. inc(longint(inputpointer));
  989. exit;
  990. end;
  991. { can we read more from this file ? }
  992. if (c<>#26) and (not endoffile) then
  993. begin
  994. readbuf;
  995. inputpointer:=buf;
  996. inputbuffer:=buf;
  997. inputstart:=bufstart;
  998. { first line? }
  999. if line_no=0 then
  1000. begin
  1001. line_no:=1;
  1002. if cs_asm_source in aktglobalswitches then
  1003. inputfile.setline(line_no,bufstart);
  1004. end;
  1005. end
  1006. else
  1007. begin
  1008. { load eof position in tokenpos/aktfilepos }
  1009. gettokenpos;
  1010. { close file }
  1011. closeinputfile;
  1012. { no next module, than EOF }
  1013. if not assigned(inputfile.next) then
  1014. begin
  1015. c:=#26;
  1016. exit;
  1017. end;
  1018. { load next file and reopen it }
  1019. nextfile;
  1020. tempopeninputfile;
  1021. { status }
  1022. Message1(scan_t_back_in,inputfile.name^);
  1023. end;
  1024. { load next char }
  1025. c:=inputpointer^;
  1026. inc(longint(inputpointer));
  1027. until c<>#0; { if also end, then reload again }
  1028. end;
  1029. end;
  1030. procedure tscannerfile.insertmacro(const macname:string;p:pchar;len:longint);
  1031. var
  1032. hp : tinputfile;
  1033. begin
  1034. { save old postion and decrease linebreak }
  1035. if c=newline then
  1036. dec(line_no);
  1037. dec(longint(inputpointer));
  1038. tempcloseinputfile;
  1039. { create macro 'file' }
  1040. { use special name to dispose after !! }
  1041. hp:=do_openinputfile('_Macro_.'+macname);
  1042. addfile(hp);
  1043. with inputfile do
  1044. begin
  1045. setmacro(p,len);
  1046. { local buffer }
  1047. inputbuffer:=buf;
  1048. inputpointer:=buf;
  1049. inputstart:=bufstart;
  1050. end;
  1051. { reset line }
  1052. line_no:=0;
  1053. lastlinepos:=0;
  1054. lasttokenpos:=0;
  1055. { load new c }
  1056. c:=inputpointer^;
  1057. inc(longint(inputpointer));
  1058. end;
  1059. procedure tscannerfile.gettokenpos;
  1060. { load the values of tokenpos and lasttokenpos }
  1061. begin
  1062. lasttokenpos:=inputstart+(inputpointer-inputbuffer);
  1063. akttokenpos.line:=line_no;
  1064. akttokenpos.column:=lasttokenpos-lastlinepos;
  1065. akttokenpos.fileindex:=inputfile.ref_index;
  1066. aktfilepos:=akttokenpos;
  1067. end;
  1068. procedure tscannerfile.inc_comment_level;
  1069. var
  1070. oldaktfilepos : tfileposinfo;
  1071. begin
  1072. if (m_nested_comment in aktmodeswitches) then
  1073. inc(comment_level)
  1074. else
  1075. comment_level:=1;
  1076. if (comment_level>1) then
  1077. begin
  1078. oldaktfilepos:=aktfilepos;
  1079. gettokenpos; { update for warning }
  1080. Message1(scan_w_comment_level,tostr(comment_level));
  1081. aktfilepos:=oldaktfilepos;
  1082. end;
  1083. end;
  1084. procedure tscannerfile.dec_comment_level;
  1085. begin
  1086. if (m_nested_comment in aktmodeswitches) then
  1087. dec(comment_level)
  1088. else
  1089. comment_level:=0;
  1090. end;
  1091. procedure tscannerfile.linebreak;
  1092. var
  1093. cur : char;
  1094. oldtokenpos,
  1095. oldaktfilepos : tfileposinfo;
  1096. begin
  1097. with inputfile do
  1098. begin
  1099. if (byte(inputpointer^)=0) and not(endoffile) then
  1100. begin
  1101. cur:=c;
  1102. reload;
  1103. if byte(cur)+byte(c)<>23 then
  1104. dec(longint(inputpointer));
  1105. end
  1106. else
  1107. begin
  1108. { Fix linebreak to be only newline (=#10) for all types of linebreaks }
  1109. if (byte(inputpointer^)+byte(c)=23) then
  1110. inc(longint(inputpointer));
  1111. end;
  1112. c:=newline;
  1113. { increase line counters }
  1114. lastlinepos:=bufstart+(inputpointer-inputbuffer);
  1115. inc(line_no);
  1116. { update linebuffer }
  1117. if cs_asm_source in aktglobalswitches then
  1118. inputfile.setline(line_no,lastlinepos);
  1119. { update for status and call the show status routine,
  1120. but don't touch aktfilepos ! }
  1121. oldaktfilepos:=aktfilepos;
  1122. oldtokenpos:=akttokenpos;
  1123. gettokenpos; { update for v_status }
  1124. inc(status.compiledlines);
  1125. ShowStatus;
  1126. aktfilepos:=oldaktfilepos;
  1127. akttokenpos:=oldtokenpos;
  1128. end;
  1129. end;
  1130. procedure tscannerfile.illegal_char(c:char);
  1131. var
  1132. s : string;
  1133. begin
  1134. if c in [#32..#255] then
  1135. s:=''''+c+''''
  1136. else
  1137. s:='#'+tostr(ord(c));
  1138. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  1139. end;
  1140. procedure tscannerfile.end_of_file;
  1141. begin
  1142. checkpreprocstack;
  1143. Message(scan_f_end_of_file);
  1144. end;
  1145. {-------------------------------------------
  1146. IF Conditional Handling
  1147. -------------------------------------------}
  1148. procedure tscannerfile.checkpreprocstack;
  1149. begin
  1150. { check for missing ifdefs }
  1151. while assigned(preprocstack) do
  1152. begin
  1153. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  1154. preprocstack.owner.inputfile.name^,tostr(preprocstack.line_nb));
  1155. poppreprocstack;
  1156. end;
  1157. end;
  1158. procedure tscannerfile.poppreprocstack;
  1159. var
  1160. hp : tpreprocstack;
  1161. begin
  1162. if assigned(preprocstack) then
  1163. begin
  1164. Message1(scan_c_endif_found,preprocstack.name);
  1165. hp:=preprocstack.next;
  1166. preprocstack.free;
  1167. preprocstack:=hp;
  1168. end
  1169. else
  1170. Message(scan_e_endif_without_if);
  1171. end;
  1172. procedure tscannerfile.addpreprocstack(atyp : preproctyp;a:boolean;const s:string;w:longint);
  1173. begin
  1174. preprocstack:=tpreprocstack.create(atyp,((preprocstack=nil) or preprocstack.accept) and a,preprocstack);
  1175. preprocstack.name:=s;
  1176. preprocstack.line_nb:=line_no;
  1177. preprocstack.owner:=self;
  1178. if preprocstack.accept then
  1179. Message2(w,preprocstack.name,'accepted')
  1180. else
  1181. Message2(w,preprocstack.name,'rejected');
  1182. end;
  1183. procedure tscannerfile.elsepreprocstack;
  1184. begin
  1185. if assigned(preprocstack) then
  1186. begin
  1187. preprocstack.typ:=pp_else;
  1188. preprocstack.line_nb:=line_no;
  1189. if not(assigned(preprocstack.next)) or (preprocstack.next.accept) then
  1190. preprocstack.accept:=not preprocstack.accept;
  1191. if preprocstack.accept then
  1192. Message2(scan_c_else_found,preprocstack.name,'accepted')
  1193. else
  1194. Message2(scan_c_else_found,preprocstack.name,'rejected');
  1195. end
  1196. else
  1197. Message(scan_e_endif_without_if);
  1198. end;
  1199. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  1200. var
  1201. oldaktfilepos : tfileposinfo;
  1202. begin
  1203. oldaktfilepos:=aktfilepos;
  1204. repeat
  1205. current_scanner.gettokenpos;
  1206. p.proc{$ifdef FPCPROCVAR}(){$endif};
  1207. { accept the text ? }
  1208. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  1209. break
  1210. else
  1211. begin
  1212. current_scanner.gettokenpos;
  1213. Message(scan_c_skipping_until);
  1214. repeat
  1215. current_scanner.skipuntildirective;
  1216. p:=tdirectiveitem(scannerdirectives.search(current_scanner.readid));
  1217. until assigned(p) and (p.is_conditional);
  1218. current_scanner.gettokenpos;
  1219. Message1(scan_d_handling_switch,'$'+p.name);
  1220. end;
  1221. until false;
  1222. aktfilepos:=oldaktfilepos;
  1223. end;
  1224. procedure tscannerfile.handledirectives;
  1225. var
  1226. t : tdirectiveitem;
  1227. hs : string;
  1228. begin
  1229. gettokenpos;
  1230. readchar; {Remove the $}
  1231. hs:=readid;
  1232. {$ifdef PREPROCWRITE}
  1233. if parapreprocess then
  1234. begin
  1235. t:=Get_Directive(hs);
  1236. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  1237. begin
  1238. preprocfile^.AddSpace;
  1239. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  1240. exit;
  1241. end;
  1242. end;
  1243. {$endif PREPROCWRITE}
  1244. { skip this directive? }
  1245. if (ignoredirectives.find(hs)<>nil) then
  1246. begin
  1247. if (comment_level>0) then
  1248. readcomment;
  1249. { we've read the whole comment }
  1250. aktcommentstyle:=comment_none;
  1251. exit;
  1252. end;
  1253. if hs='' then
  1254. begin
  1255. Message1(scan_w_illegal_switch,'$'+hs);
  1256. end;
  1257. { Check for compiler switches }
  1258. while (length(hs)=1) and (c in ['-','+']) do
  1259. begin
  1260. HandleSwitch(hs[1],c);
  1261. current_scanner.readchar; {Remove + or -}
  1262. if c=',' then
  1263. begin
  1264. current_scanner.readchar; {Remove , }
  1265. { read next switch, support $v+,$+}
  1266. hs:=current_scanner.readid;
  1267. if (hs='') then
  1268. begin
  1269. if (c='$') and (m_fpc in aktmodeswitches) then
  1270. begin
  1271. current_scanner.readchar; { skip $ }
  1272. hs:=current_scanner.readid;
  1273. end;
  1274. if (hs='') then
  1275. Message1(scan_w_illegal_directive,'$'+c);
  1276. end
  1277. else
  1278. Message1(scan_d_handling_switch,'$'+hs);
  1279. end
  1280. else
  1281. hs:='';
  1282. end;
  1283. { directives may follow switches after a , }
  1284. if hs<>'' then
  1285. begin
  1286. t:=tdirectiveitem(scannerdirectives.search(hs));
  1287. if assigned(t) then
  1288. begin
  1289. if t.is_conditional then
  1290. handleconditional(t)
  1291. else
  1292. begin
  1293. Message1(scan_d_handling_switch,'$'+hs);
  1294. t.proc{$ifdef FPCPROCVAR}(){$endif};
  1295. end;
  1296. end
  1297. else
  1298. begin
  1299. current_scanner.ignoredirectives.insert(hs);
  1300. Message1(scan_w_illegal_directive,'$'+hs);
  1301. end;
  1302. { conditionals already read the comment }
  1303. if (current_scanner.comment_level>0) then
  1304. current_scanner.readcomment;
  1305. { we've read the whole comment }
  1306. aktcommentstyle:=comment_none;
  1307. end;
  1308. end;
  1309. procedure tscannerfile.readchar;
  1310. begin
  1311. c:=inputpointer^;
  1312. if c=#0 then
  1313. reload
  1314. else
  1315. inc(longint(inputpointer));
  1316. case c of
  1317. #26 : reload;
  1318. #10,
  1319. #13 : linebreak;
  1320. end;
  1321. end;
  1322. procedure tscannerfile.readstring;
  1323. var
  1324. i : longint;
  1325. begin
  1326. i:=0;
  1327. repeat
  1328. case c of
  1329. '_',
  1330. '0'..'9',
  1331. 'A'..'Z' : begin
  1332. if i<255 then
  1333. begin
  1334. inc(i);
  1335. orgpattern[i]:=c;
  1336. pattern[i]:=c;
  1337. end;
  1338. c:=inputpointer^;
  1339. inc(longint(inputpointer));
  1340. end;
  1341. 'a'..'z' : begin
  1342. if i<255 then
  1343. begin
  1344. inc(i);
  1345. orgpattern[i]:=c;
  1346. pattern[i]:=chr(ord(c)-32)
  1347. end;
  1348. c:=inputpointer^;
  1349. inc(longint(inputpointer));
  1350. end;
  1351. #0 : reload;
  1352. #26 : begin
  1353. reload;
  1354. if c=#26 then
  1355. break;
  1356. end;
  1357. #13,#10 : begin
  1358. linebreak;
  1359. break;
  1360. end;
  1361. else
  1362. break;
  1363. end;
  1364. until false;
  1365. orgpattern[0]:=chr(i);
  1366. pattern[0]:=chr(i);
  1367. end;
  1368. procedure tscannerfile.readnumber;
  1369. var
  1370. base,
  1371. i : longint;
  1372. begin
  1373. case c of
  1374. '%' : begin
  1375. readchar;
  1376. base:=2;
  1377. pattern[1]:='%';
  1378. i:=1;
  1379. end;
  1380. '&' : begin
  1381. readchar;
  1382. base:=8;
  1383. pattern[1]:='&';
  1384. i:=1;
  1385. end;
  1386. '$' : begin
  1387. readchar;
  1388. base:=16;
  1389. pattern[1]:='$';
  1390. i:=1;
  1391. end;
  1392. else
  1393. begin
  1394. base:=10;
  1395. i:=0;
  1396. end;
  1397. end;
  1398. while ((base>=10) and (c in ['0'..'9'])) or
  1399. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  1400. ((base=8) and (c in ['0'..'7'])) or
  1401. ((base=2) and (c in ['0'..'1'])) do
  1402. begin
  1403. if i<255 then
  1404. begin
  1405. inc(i);
  1406. pattern[i]:=c;
  1407. end;
  1408. { get next char }
  1409. c:=inputpointer^;
  1410. if c=#0 then
  1411. reload
  1412. else
  1413. inc(longint(inputpointer));
  1414. end;
  1415. { was the next char a linebreak ? }
  1416. case c of
  1417. #26 : reload;
  1418. #10,
  1419. #13 : linebreak;
  1420. end;
  1421. pattern[0]:=chr(i);
  1422. end;
  1423. function tscannerfile.readid:string;
  1424. begin
  1425. readstring;
  1426. readid:=pattern;
  1427. end;
  1428. function tscannerfile.readval:longint;
  1429. var
  1430. l : longint;
  1431. w : integer;
  1432. begin
  1433. readnumber;
  1434. valint(pattern,l,w);
  1435. readval:=l;
  1436. end;
  1437. function tscannerfile.readcomment:string;
  1438. var
  1439. i : longint;
  1440. begin
  1441. i:=0;
  1442. repeat
  1443. case c of
  1444. '{' :
  1445. if aktcommentstyle=comment_tp then
  1446. inc_comment_level;
  1447. '}' :
  1448. if aktcommentstyle=comment_tp then
  1449. begin
  1450. readchar;
  1451. dec_comment_level;
  1452. if comment_level=0 then
  1453. break
  1454. else
  1455. continue;
  1456. end;
  1457. '*' :
  1458. if aktcommentstyle=comment_oldtp then
  1459. begin
  1460. readchar;
  1461. if c=')' then
  1462. begin
  1463. readchar;
  1464. dec_comment_level;
  1465. break;
  1466. end
  1467. else
  1468. { Add both characters !!}
  1469. if (i<255) then
  1470. begin
  1471. inc(i);
  1472. readcomment[i]:='*';
  1473. if (i<255) then
  1474. begin
  1475. inc(i);
  1476. readcomment[i]:='*';
  1477. end;
  1478. end;
  1479. end
  1480. else
  1481. { Not old TP comment, so add...}
  1482. begin
  1483. if (i<255) then
  1484. begin
  1485. inc(i);
  1486. readcomment[i]:='*';
  1487. end;
  1488. end;
  1489. #26 :
  1490. end_of_file;
  1491. else
  1492. begin
  1493. if (i<255) then
  1494. begin
  1495. inc(i);
  1496. readcomment[i]:=c;
  1497. end;
  1498. end;
  1499. end;
  1500. c:=inputpointer^;
  1501. if c=#0 then
  1502. reload
  1503. else
  1504. inc(longint(inputpointer));
  1505. if c in [#10,#13] then
  1506. linebreak;
  1507. until false;
  1508. readcomment[0]:=chr(i);
  1509. end;
  1510. function tscannerfile.readstate:char;
  1511. var
  1512. state : char;
  1513. begin
  1514. state:=' ';
  1515. if c=' ' then
  1516. begin
  1517. current_scanner.skipspace;
  1518. current_scanner.readid;
  1519. if pattern='ON' then
  1520. state:='+'
  1521. else
  1522. if pattern='OFF' then
  1523. state:='-';
  1524. end
  1525. else
  1526. state:=c;
  1527. if not (state in ['+','-']) then
  1528. Message(scan_e_wrong_switch_toggle);
  1529. readstate:=state;
  1530. end;
  1531. procedure tscannerfile.skipspace;
  1532. begin
  1533. while c in [' ',#9..#13] do
  1534. begin
  1535. c:=inputpointer^;
  1536. if c=#0 then
  1537. reload
  1538. else
  1539. inc(longint(inputpointer));
  1540. case c of
  1541. #26 :
  1542. reload;
  1543. #10,
  1544. #13 :
  1545. linebreak;
  1546. end;
  1547. end;
  1548. end;
  1549. procedure tscannerfile.skipuntildirective;
  1550. var
  1551. incomment : boolean;
  1552. found : longint;
  1553. next_char_loaded : boolean;
  1554. oldcommentstyle : tcommentstyle;
  1555. begin
  1556. found:=0;
  1557. next_char_loaded:=false;
  1558. incomment:=true;
  1559. oldcommentstyle:=aktcommentstyle;
  1560. repeat
  1561. case c of
  1562. #26 :
  1563. end_of_file;
  1564. '{' :
  1565. begin
  1566. if not(m_nested_comment in aktmodeswitches) or
  1567. (comment_level=0) then
  1568. begin
  1569. found:=1;
  1570. aktcommentstyle:=comment_tp;
  1571. end;
  1572. inc_comment_level;
  1573. incomment:=true;
  1574. end;
  1575. '}' :
  1576. begin
  1577. dec_comment_level;
  1578. found:=0;
  1579. incomment:=false;
  1580. end;
  1581. '$' :
  1582. begin
  1583. if found=1 then
  1584. found:=2;
  1585. end;
  1586. '''' :
  1587. if not incomment then
  1588. begin
  1589. repeat
  1590. readchar;
  1591. case c of
  1592. #26 :
  1593. end_of_file;
  1594. newline :
  1595. break;
  1596. '''' :
  1597. begin
  1598. readchar;
  1599. if c<>'''' then
  1600. begin
  1601. next_char_loaded:=true;
  1602. break;
  1603. end;
  1604. end;
  1605. end;
  1606. until false;
  1607. end;
  1608. '(' :
  1609. begin
  1610. if not incomment then
  1611. begin
  1612. readchar;
  1613. if c='*' then
  1614. begin
  1615. readchar;
  1616. if c='$' then
  1617. begin
  1618. found:=2;
  1619. inc_comment_level;
  1620. aktcommentstyle:=comment_oldtp;
  1621. end
  1622. else
  1623. begin
  1624. skipoldtpcomment;
  1625. aktcommentstyle:=oldcommentstyle;
  1626. end;
  1627. end
  1628. else
  1629. next_char_loaded:=true;
  1630. end
  1631. else
  1632. found:=0;
  1633. end;
  1634. '/' :
  1635. begin
  1636. if not incomment then
  1637. begin
  1638. readchar;
  1639. if c='/' then
  1640. begin
  1641. readchar;
  1642. skipdelphicomment;
  1643. aktcommentstyle:=oldcommentstyle;
  1644. end
  1645. else
  1646. next_char_loaded:=true;
  1647. end
  1648. else
  1649. found:=0;
  1650. end;
  1651. else
  1652. found:=0;
  1653. end;
  1654. if next_char_loaded then
  1655. next_char_loaded:=false
  1656. else
  1657. begin
  1658. c:=inputpointer^;
  1659. if c=#0 then
  1660. reload
  1661. else
  1662. inc(longint(inputpointer));
  1663. case c of
  1664. #26 : reload;
  1665. #10,
  1666. #13 : linebreak;
  1667. end;
  1668. end;
  1669. until (found=2);
  1670. end;
  1671. {****************************************************************************
  1672. Comment Handling
  1673. ****************************************************************************}
  1674. procedure tscannerfile.skipcomment;
  1675. begin
  1676. aktcommentstyle:=comment_tp;
  1677. readchar;
  1678. inc_comment_level;
  1679. { handle compiler switches }
  1680. if (c='$') then
  1681. handledirectives;
  1682. { handle_switches can dec comment_level, }
  1683. while (comment_level>0) do
  1684. begin
  1685. case c of
  1686. '{' : inc_comment_level;
  1687. '}' : dec_comment_level;
  1688. #26 : end_of_file;
  1689. end;
  1690. c:=inputpointer^;
  1691. if c=#0 then
  1692. reload
  1693. else
  1694. inc(longint(inputpointer));
  1695. case c of
  1696. #26 : reload;
  1697. #10,
  1698. #13 : linebreak;
  1699. end;
  1700. end;
  1701. aktcommentstyle:=comment_none;
  1702. end;
  1703. procedure tscannerfile.skipdelphicomment;
  1704. begin
  1705. aktcommentstyle:=comment_delphi;
  1706. inc_comment_level;
  1707. readchar;
  1708. { this is not supported }
  1709. if c='$' then
  1710. Message(scan_e_wrong_styled_switch);
  1711. { skip comment }
  1712. while not (c in [newline,#26]) do
  1713. readchar;
  1714. dec_comment_level;
  1715. aktcommentstyle:=comment_none;
  1716. end;
  1717. procedure tscannerfile.skipoldtpcomment;
  1718. var
  1719. found : longint;
  1720. begin
  1721. aktcommentstyle:=comment_oldtp;
  1722. inc_comment_level;
  1723. { only load a char if last already processed,
  1724. was cause of bug1634 PM }
  1725. if c=#0 then
  1726. readchar;
  1727. { this is now supported }
  1728. if (c='$') then
  1729. handledirectives;
  1730. { skip comment }
  1731. while (comment_level>0) do
  1732. begin
  1733. found:=0;
  1734. repeat
  1735. case c of
  1736. #26 :
  1737. end_of_file;
  1738. '*' :
  1739. begin
  1740. if found=3 then
  1741. found:=4
  1742. else
  1743. found:=1;
  1744. end;
  1745. ')' :
  1746. begin
  1747. if found in [1,4] then
  1748. begin
  1749. dec_comment_level;
  1750. if comment_level=0 then
  1751. found:=2
  1752. else
  1753. found:=0;
  1754. end;
  1755. end;
  1756. '(' :
  1757. begin
  1758. if found=4 then
  1759. inc_comment_level;
  1760. found:=3;
  1761. end;
  1762. else
  1763. begin
  1764. if found=4 then
  1765. inc_comment_level;
  1766. found:=0;
  1767. end;
  1768. end;
  1769. c:=inputpointer^;
  1770. if c=#0 then
  1771. reload
  1772. else
  1773. inc(longint(inputpointer));
  1774. case c of
  1775. #26 : reload;
  1776. #10,
  1777. #13 : linebreak;
  1778. end;
  1779. until (found=2);
  1780. end;
  1781. aktcommentstyle:=comment_none;
  1782. end;
  1783. {****************************************************************************
  1784. Token Scanner
  1785. ****************************************************************************}
  1786. procedure tscannerfile.readtoken;
  1787. var
  1788. code : integer;
  1789. len,
  1790. low,high,mid : longint;
  1791. m : longint;
  1792. mac : tmacro;
  1793. asciinr : string[6];
  1794. msgwritten,
  1795. iswidestring : boolean;
  1796. label
  1797. exit_label;
  1798. begin
  1799. if localswitcheschanged then
  1800. begin
  1801. aktlocalswitches:=nextaktlocalswitches;
  1802. localswitcheschanged:=false;
  1803. end;
  1804. { was there already a token read, then return that token }
  1805. if nexttoken<>NOTOKEN then
  1806. begin
  1807. token:=nexttoken;
  1808. nexttoken:=NOTOKEN;
  1809. goto exit_label;
  1810. end;
  1811. { Skip all spaces and comments }
  1812. repeat
  1813. case c of
  1814. '{' :
  1815. skipcomment;
  1816. ' ',#9..#13 :
  1817. begin
  1818. {$ifdef PREPROCWRITE}
  1819. if parapreprocess then
  1820. begin
  1821. if c=#10 then
  1822. preprocfile.eolfound:=true
  1823. else
  1824. preprocfile.spacefound:=true;
  1825. end;
  1826. {$endif PREPROCWRITE}
  1827. skipspace;
  1828. end
  1829. else
  1830. break;
  1831. end;
  1832. until false;
  1833. { Save current token position, for EOF its already loaded }
  1834. if c<>#26 then
  1835. gettokenpos;
  1836. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  1837. if c in ['A'..'Z','a'..'z','_'] then
  1838. begin
  1839. readstring;
  1840. token:=_ID;
  1841. idtoken:=_ID;
  1842. { keyword or any other known token,
  1843. pattern is always uppercased }
  1844. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  1845. begin
  1846. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  1847. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  1848. while low<high do
  1849. begin
  1850. mid:=(high+low+1) shr 1;
  1851. if pattern<tokeninfo^[ttoken(mid)].str then
  1852. high:=mid-1
  1853. else
  1854. low:=mid;
  1855. end;
  1856. if pattern=tokeninfo^[ttoken(high)].str then
  1857. begin
  1858. if tokeninfo^[ttoken(high)].keyword in aktmodeswitches then
  1859. if tokeninfo^[ttoken(high)].op=NOTOKEN then
  1860. token:=ttoken(high)
  1861. else
  1862. token:=tokeninfo^[ttoken(high)].op;
  1863. idtoken:=ttoken(high);
  1864. end;
  1865. end;
  1866. { Only process identifiers and not keywords }
  1867. if token=_ID then
  1868. begin
  1869. { this takes some time ... }
  1870. if (cs_support_macro in aktmoduleswitches) then
  1871. begin
  1872. mac:=tmacro(macros.search(pattern));
  1873. if assigned(mac) and (assigned(mac.buftext)) then
  1874. begin
  1875. insertmacro(pattern,mac.buftext,mac.buflen);
  1876. { handle empty macros }
  1877. if c=#0 then
  1878. begin
  1879. reload;
  1880. case c of
  1881. #26 : reload;
  1882. #10,
  1883. #13 : linebreak;
  1884. end;
  1885. end;
  1886. { play it again ... }
  1887. inc(yylexcount);
  1888. if yylexcount>16 then
  1889. Message(scan_w_macro_deep_ten);
  1890. readtoken;
  1891. { that's all folks }
  1892. dec(yylexcount);
  1893. exit;
  1894. end;
  1895. end;
  1896. end;
  1897. { return token }
  1898. goto exit_label;
  1899. end
  1900. else
  1901. begin
  1902. idtoken:=_NOID;
  1903. case c of
  1904. '$' :
  1905. begin
  1906. readnumber;
  1907. token:=_INTCONST;
  1908. goto exit_label;
  1909. end;
  1910. '%' :
  1911. begin
  1912. if not(m_fpc in aktmodeswitches) then
  1913. Illegal_Char(c)
  1914. else
  1915. begin
  1916. readnumber;
  1917. token:=_INTCONST;
  1918. goto exit_label;
  1919. end;
  1920. end;
  1921. '&' :
  1922. begin
  1923. if not(m_fpc in aktmodeswitches) then
  1924. Illegal_Char(c)
  1925. else
  1926. begin
  1927. readnumber;
  1928. token:=_INTCONST;
  1929. goto exit_label;
  1930. end;
  1931. end;
  1932. '0'..'9' :
  1933. begin
  1934. readnumber;
  1935. if (c in ['.','e','E']) then
  1936. begin
  1937. { first check for a . }
  1938. if c='.' then
  1939. begin
  1940. readchar;
  1941. { is it a .. from a range? }
  1942. case c of
  1943. '.' :
  1944. begin
  1945. readchar;
  1946. token:=_INTCONST;
  1947. nexttoken:=_POINTPOINT;
  1948. goto exit_label;
  1949. end;
  1950. ')' :
  1951. begin
  1952. readchar;
  1953. token:=_INTCONST;
  1954. nexttoken:=_RECKKLAMMER;
  1955. goto exit_label;
  1956. end;
  1957. end;
  1958. { insert the number after the . }
  1959. pattern:=pattern+'.';
  1960. while c in ['0'..'9'] do
  1961. begin
  1962. pattern:=pattern+c;
  1963. readchar;
  1964. end;
  1965. end;
  1966. { E can also follow after a point is scanned }
  1967. if c in ['e','E'] then
  1968. begin
  1969. pattern:=pattern+'E';
  1970. readchar;
  1971. if c in ['-','+'] then
  1972. begin
  1973. pattern:=pattern+c;
  1974. readchar;
  1975. end;
  1976. if not(c in ['0'..'9']) then
  1977. Illegal_Char(c);
  1978. while c in ['0'..'9'] do
  1979. begin
  1980. pattern:=pattern+c;
  1981. readchar;
  1982. end;
  1983. end;
  1984. token:=_REALNUMBER;
  1985. goto exit_label;
  1986. end;
  1987. token:=_INTCONST;
  1988. goto exit_label;
  1989. end;
  1990. ';' :
  1991. begin
  1992. readchar;
  1993. token:=_SEMICOLON;
  1994. goto exit_label;
  1995. end;
  1996. '[' :
  1997. begin
  1998. readchar;
  1999. token:=_LECKKLAMMER;
  2000. goto exit_label;
  2001. end;
  2002. ']' :
  2003. begin
  2004. readchar;
  2005. token:=_RECKKLAMMER;
  2006. goto exit_label;
  2007. end;
  2008. '(' :
  2009. begin
  2010. readchar;
  2011. case c of
  2012. '*' :
  2013. begin
  2014. c:=#0;{Signal skipoldtpcomment to reload a char }
  2015. skipoldtpcomment;
  2016. readtoken;
  2017. exit;
  2018. end;
  2019. '.' :
  2020. begin
  2021. readchar;
  2022. token:=_LECKKLAMMER;
  2023. goto exit_label;
  2024. end;
  2025. end;
  2026. token:=_LKLAMMER;
  2027. goto exit_label;
  2028. end;
  2029. ')' :
  2030. begin
  2031. readchar;
  2032. token:=_RKLAMMER;
  2033. goto exit_label;
  2034. end;
  2035. '+' :
  2036. begin
  2037. readchar;
  2038. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2039. begin
  2040. readchar;
  2041. token:=_PLUSASN;
  2042. goto exit_label;
  2043. end;
  2044. token:=_PLUS;
  2045. goto exit_label;
  2046. end;
  2047. '-' :
  2048. begin
  2049. readchar;
  2050. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2051. begin
  2052. readchar;
  2053. token:=_MINUSASN;
  2054. goto exit_label;
  2055. end;
  2056. token:=_MINUS;
  2057. goto exit_label;
  2058. end;
  2059. ':' :
  2060. begin
  2061. readchar;
  2062. if c='=' then
  2063. begin
  2064. readchar;
  2065. token:=_ASSIGNMENT;
  2066. goto exit_label;
  2067. end;
  2068. token:=_COLON;
  2069. goto exit_label;
  2070. end;
  2071. '*' :
  2072. begin
  2073. readchar;
  2074. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2075. begin
  2076. readchar;
  2077. token:=_STARASN;
  2078. end
  2079. else
  2080. if c='*' then
  2081. begin
  2082. readchar;
  2083. token:=_STARSTAR;
  2084. end
  2085. else
  2086. token:=_STAR;
  2087. goto exit_label;
  2088. end;
  2089. '/' :
  2090. begin
  2091. readchar;
  2092. case c of
  2093. '=' :
  2094. begin
  2095. if (cs_support_c_operators in aktmoduleswitches) then
  2096. begin
  2097. readchar;
  2098. token:=_SLASHASN;
  2099. goto exit_label;
  2100. end;
  2101. end;
  2102. '/' :
  2103. begin
  2104. skipdelphicomment;
  2105. readtoken;
  2106. exit;
  2107. end;
  2108. end;
  2109. token:=_SLASH;
  2110. goto exit_label;
  2111. end;
  2112. '=' :
  2113. begin
  2114. readchar;
  2115. token:=_EQUAL;
  2116. goto exit_label;
  2117. end;
  2118. '.' :
  2119. begin
  2120. readchar;
  2121. case c of
  2122. '.' :
  2123. begin
  2124. readchar;
  2125. token:=_POINTPOINT;
  2126. goto exit_label;
  2127. end;
  2128. ')' :
  2129. begin
  2130. readchar;
  2131. token:=_RECKKLAMMER;
  2132. goto exit_label;
  2133. end;
  2134. end;
  2135. token:=_POINT;
  2136. goto exit_label;
  2137. end;
  2138. '@' :
  2139. begin
  2140. readchar;
  2141. if c='@' then
  2142. begin
  2143. readchar;
  2144. token:=_DOUBLEADDR;
  2145. end
  2146. else
  2147. token:=_KLAMMERAFFE;
  2148. goto exit_label;
  2149. end;
  2150. ',' :
  2151. begin
  2152. readchar;
  2153. token:=_COMMA;
  2154. goto exit_label;
  2155. end;
  2156. '''','#','^' :
  2157. begin
  2158. len:=0;
  2159. msgwritten:=false;
  2160. pattern:='';
  2161. iswidestring:=false;
  2162. if c='^' then
  2163. begin
  2164. readchar;
  2165. c:=upcase(c);
  2166. if (block_type=bt_type) or
  2167. (lasttoken=_ID) or
  2168. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  2169. begin
  2170. token:=_CARET;
  2171. goto exit_label;
  2172. end
  2173. else
  2174. begin
  2175. inc(len);
  2176. if c<#64 then
  2177. pattern[len]:=chr(ord(c)+64)
  2178. else
  2179. pattern[len]:=chr(ord(c)-64);
  2180. readchar;
  2181. end;
  2182. end;
  2183. repeat
  2184. case c of
  2185. '#' :
  2186. begin
  2187. readchar; { read # }
  2188. if c='$' then
  2189. begin
  2190. readchar; { read leading $ }
  2191. asciinr:='$';
  2192. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<6) do
  2193. begin
  2194. asciinr:=asciinr+c;
  2195. readchar;
  2196. end;
  2197. end
  2198. else
  2199. begin
  2200. asciinr:='';
  2201. while (c in ['0'..'9']) and (length(asciinr)<6) do
  2202. begin
  2203. asciinr:=asciinr+c;
  2204. readchar;
  2205. end;
  2206. end;
  2207. valint(asciinr,m,code);
  2208. if (asciinr='') or (code<>0) then
  2209. Message(scan_e_illegal_char_const)
  2210. else if (m<0) or (m>255) or (length(asciinr)>3) then
  2211. begin
  2212. if (m>=0) and (m<=65535) then
  2213. begin
  2214. if not iswidestring then
  2215. begin
  2216. ascii2unicode(@pattern[1],len,patternw);
  2217. iswidestring:=true;
  2218. len:=0;
  2219. end;
  2220. concatwidestringchar(patternw,tcompilerwidechar(m));
  2221. end
  2222. else
  2223. Message(scan_e_illegal_char_const)
  2224. end
  2225. else if iswidestring then
  2226. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  2227. else
  2228. begin
  2229. if len<255 then
  2230. begin
  2231. inc(len);
  2232. pattern[len]:=chr(m);
  2233. end
  2234. else
  2235. begin
  2236. if not msgwritten then
  2237. begin
  2238. Message(scan_e_string_exceeds_255_chars);
  2239. msgwritten:=true;
  2240. end;
  2241. end;
  2242. end;
  2243. end;
  2244. '''' :
  2245. begin
  2246. repeat
  2247. readchar;
  2248. case c of
  2249. #26 :
  2250. end_of_file;
  2251. newline :
  2252. Message(scan_f_string_exceeds_line);
  2253. '''' :
  2254. begin
  2255. readchar;
  2256. if c<>'''' then
  2257. break;
  2258. end;
  2259. end;
  2260. if iswidestring then
  2261. concatwidestringchar(patternw,asciichar2unicode(c))
  2262. else
  2263. begin
  2264. if len<255 then
  2265. begin
  2266. inc(len);
  2267. pattern[len]:=c;
  2268. end
  2269. else
  2270. begin
  2271. if not msgwritten then
  2272. begin
  2273. Message(scan_e_string_exceeds_255_chars);
  2274. msgwritten:=true;
  2275. end;
  2276. end;
  2277. end;
  2278. until false;
  2279. end;
  2280. '^' :
  2281. begin
  2282. readchar;
  2283. c:=upcase(c);
  2284. if c<#64 then
  2285. c:=chr(ord(c)+64)
  2286. else
  2287. c:=chr(ord(c)-64);
  2288. if iswidestring then
  2289. concatwidestringchar(patternw,asciichar2unicode(c))
  2290. else
  2291. begin
  2292. if len<255 then
  2293. begin
  2294. inc(len);
  2295. pattern[len]:=c;
  2296. end
  2297. else
  2298. begin
  2299. if not msgwritten then
  2300. begin
  2301. Message(scan_e_string_exceeds_255_chars);
  2302. msgwritten:=true;
  2303. end;
  2304. end;
  2305. end;
  2306. readchar;
  2307. end;
  2308. else
  2309. break;
  2310. end;
  2311. until false;
  2312. { strings with length 1 become const chars }
  2313. if iswidestring then
  2314. begin
  2315. if patternw^.len=1 then
  2316. token:=_CWCHAR
  2317. else
  2318. token:=_CWSTRING;
  2319. end
  2320. else
  2321. begin
  2322. pattern[0]:=chr(len);
  2323. if len=1 then
  2324. token:=_CCHAR
  2325. else
  2326. token:=_CSTRING;
  2327. end;
  2328. goto exit_label;
  2329. end;
  2330. '>' :
  2331. begin
  2332. readchar;
  2333. case c of
  2334. '=' :
  2335. begin
  2336. readchar;
  2337. token:=_GTE;
  2338. goto exit_label;
  2339. end;
  2340. '>' :
  2341. begin
  2342. readchar;
  2343. token:=_OP_SHR;
  2344. goto exit_label;
  2345. end;
  2346. '<' :
  2347. begin { >< is for a symetric diff for sets }
  2348. readchar;
  2349. token:=_SYMDIF;
  2350. goto exit_label;
  2351. end;
  2352. end;
  2353. token:=_GT;
  2354. goto exit_label;
  2355. end;
  2356. '<' :
  2357. begin
  2358. readchar;
  2359. case c of
  2360. '>' :
  2361. begin
  2362. readchar;
  2363. token:=_UNEQUAL;
  2364. goto exit_label;
  2365. end;
  2366. '=' :
  2367. begin
  2368. readchar;
  2369. token:=_LTE;
  2370. goto exit_label;
  2371. end;
  2372. '<' :
  2373. begin
  2374. readchar;
  2375. token:=_OP_SHL;
  2376. goto exit_label;
  2377. end;
  2378. end;
  2379. token:=_LT;
  2380. goto exit_label;
  2381. end;
  2382. #26 :
  2383. begin
  2384. token:=_EOF;
  2385. checkpreprocstack;
  2386. goto exit_label;
  2387. end;
  2388. else
  2389. Illegal_Char(c);
  2390. end;
  2391. end;
  2392. exit_label:
  2393. lasttoken:=token;
  2394. end;
  2395. function tscannerfile.readpreproc:ttoken;
  2396. begin
  2397. skipspace;
  2398. case c of
  2399. 'A'..'Z',
  2400. 'a'..'z',
  2401. '_','0'..'9' : begin
  2402. current_scanner.preproc_pattern:=readid;
  2403. readpreproc:=_ID;
  2404. end;
  2405. '}' : begin
  2406. readpreproc:=_END;
  2407. end;
  2408. '(' : begin
  2409. readchar;
  2410. readpreproc:=_LKLAMMER;
  2411. end;
  2412. ')' : begin
  2413. readchar;
  2414. readpreproc:=_RKLAMMER;
  2415. end;
  2416. '+' : begin
  2417. readchar;
  2418. readpreproc:=_PLUS;
  2419. end;
  2420. '-' : begin
  2421. readchar;
  2422. readpreproc:=_MINUS;
  2423. end;
  2424. '*' : begin
  2425. readchar;
  2426. readpreproc:=_STAR;
  2427. end;
  2428. '/' : begin
  2429. readchar;
  2430. readpreproc:=_SLASH;
  2431. end;
  2432. '=' : begin
  2433. readchar;
  2434. readpreproc:=_EQUAL;
  2435. end;
  2436. '>' : begin
  2437. readchar;
  2438. if c='=' then
  2439. begin
  2440. readchar;
  2441. readpreproc:=_GTE;
  2442. end
  2443. else
  2444. readpreproc:=_GT;
  2445. end;
  2446. '<' : begin
  2447. readchar;
  2448. case c of
  2449. '>' : begin
  2450. readchar;
  2451. readpreproc:=_UNEQUAL;
  2452. end;
  2453. '=' : begin
  2454. readchar;
  2455. readpreproc:=_LTE;
  2456. end;
  2457. else readpreproc:=_LT;
  2458. end;
  2459. end;
  2460. #26 :
  2461. end_of_file;
  2462. else
  2463. begin
  2464. readpreproc:=_EOF;
  2465. checkpreprocstack;
  2466. end;
  2467. end;
  2468. end;
  2469. function tscannerfile.asmgetchar : char;
  2470. begin
  2471. if lastasmgetchar<>#0 then
  2472. begin
  2473. c:=lastasmgetchar;
  2474. lastasmgetchar:=#0;
  2475. end
  2476. else
  2477. readchar;
  2478. if in_asm_string then
  2479. begin
  2480. asmgetchar:=c;
  2481. exit;
  2482. end;
  2483. repeat
  2484. case c of
  2485. '{' :
  2486. skipcomment;
  2487. '/' :
  2488. begin
  2489. readchar;
  2490. if c='/' then
  2491. skipdelphicomment
  2492. else
  2493. begin
  2494. asmgetchar:='/';
  2495. lastasmgetchar:=c;
  2496. exit;
  2497. end;
  2498. end;
  2499. '(' :
  2500. begin
  2501. readchar;
  2502. if c='*' then
  2503. begin
  2504. c:=#0;{Signal skipoldtpcomment to reload a char }
  2505. skipoldtpcomment;
  2506. end
  2507. else
  2508. begin
  2509. asmgetchar:='(';
  2510. lastasmgetchar:=c;
  2511. exit;
  2512. end;
  2513. end;
  2514. else
  2515. begin
  2516. asmgetchar:=c;
  2517. exit;
  2518. end;
  2519. end;
  2520. until false;
  2521. end;
  2522. {*****************************************************************************
  2523. Helpers
  2524. *****************************************************************************}
  2525. procedure adddirective(const s:string;p:tdirectiveproc);
  2526. begin
  2527. scannerdirectives.insert(tdirectiveitem.create(s,p));
  2528. end;
  2529. procedure addconditional(const s:string;p:tdirectiveproc);
  2530. begin
  2531. scannerdirectives.insert(tdirectiveitem.createcond(s,p));
  2532. end;
  2533. {*****************************************************************************
  2534. Initialization
  2535. *****************************************************************************}
  2536. procedure InitScanner;
  2537. begin
  2538. InitWideString(patternw);
  2539. scannerdirectives:=TDictionary.Create;
  2540. { Default directives }
  2541. AddDirective('DEFINE',{$ifdef FPCPROCVAR}@{$endif}dir_define);
  2542. AddDirective('UNDEF',{$ifdef FPCPROCVAR}@{$endif}dir_undef);
  2543. AddDirective('I',{$ifdef FPCPROCVAR}@{$endif}dir_include);
  2544. AddDirective('INCLUDE',{$ifdef FPCPROCVAR}@{$endif}dir_include);
  2545. { Default conditionals }
  2546. AddConditional('ELSE',{$ifdef FPCPROCVAR}@{$endif}dir_else);
  2547. AddConditional('ENDIF',{$ifdef FPCPROCVAR}@{$endif}dir_endif);
  2548. AddConditional('IF',{$ifdef FPCPROCVAR}@{$endif}dir_if);
  2549. AddConditional('IFDEF',{$ifdef FPCPROCVAR}@{$endif}dir_ifdef);
  2550. AddConditional('IFNDEF',{$ifdef FPCPROCVAR}@{$endif}dir_ifndef);
  2551. AddConditional('IFOPT',{$ifdef FPCPROCVAR}@{$endif}dir_ifopt);
  2552. end;
  2553. procedure DoneScanner;
  2554. begin
  2555. scannerdirectives.Free;
  2556. DoneWideString(patternw);
  2557. end;
  2558. end.
  2559. {
  2560. $Log$
  2561. Revision 1.44 2002-08-12 16:46:04 peter
  2562. * tscannerfile is now destroyed in tmodule.reset and current_scanner
  2563. is updated accordingly. This removes all the loading and saving of
  2564. the old scanner and the invalid flag marking
  2565. Revision 1.43 2002/08/11 14:28:19 peter
  2566. * TScannerFile.SetInvalid added that will also reset inputfile
  2567. Revision 1.42 2002/08/10 14:46:31 carl
  2568. + moved target_cpu_string to cpuinfo
  2569. * renamed asmmode enum.
  2570. * assembler reader has now less ifdef's
  2571. * move from nppcmem.pas -> ncgmem.pas vec. node.
  2572. Revision 1.41 2002/08/06 21:12:16 florian
  2573. + support for octal constants, they are specified by a leading &
  2574. Revision 1.40 2002/07/20 17:35:52 florian
  2575. + char constants specified with #.. with more than 3 digits are handled as widechar
  2576. Revision 1.39 2002/05/18 13:34:17 peter
  2577. * readded missing revisions
  2578. Revision 1.38 2002/05/16 19:46:44 carl
  2579. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2580. + try to fix temp allocation (still in ifdef)
  2581. + generic constructor calls
  2582. + start of tassembler / tmodulebase class cleanup
  2583. Revision 1.36 2002/04/21 18:57:23 peter
  2584. * fixed memleaks when file can't be opened
  2585. Revision 1.35 2002/04/21 15:22:26 carl
  2586. * first check .inc file extension
  2587. Revision 1.34 2002/04/21 07:24:09 carl
  2588. - remove my fixes until Peter agrees on the fix (sorry Peter)
  2589. Revision 1.32 2002/04/19 15:42:11 peter
  2590. * default extension checking for include files
  2591. Revision 1.31 2002/03/01 14:39:44 peter
  2592. * fixed // and (* parsing to not be done when already parsing a
  2593. tp comment in skipuntildirective
  2594. Revision 1.30 2002/03/01 12:39:26 peter
  2595. * support // parsing in skipuntildirective
  2596. Revision 1.29 2002/01/27 21:44:26 peter
  2597. * FPCTARGETOS/FPCTARGETCPU added as internal environment variable
  2598. Revision 1.28 2002/01/24 18:25:50 peter
  2599. * implicit result variable generation for assembler routines
  2600. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  2601. }