scanner.pas 84 KB

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