scanner.pas 86 KB

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