scanner.pas 84 KB

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