scanner.pas 101 KB

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