scanner.pas 86 KB

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