scanner.pas 99 KB

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