scanner.pas 95 KB

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