scanner.pas 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the scanner part and handling of the switches
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit scanner;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,constexp,version,tokens,
  23. verbose,comphook,
  24. finput,
  25. widestr;
  26. const
  27. max_include_nesting=32;
  28. max_macro_nesting=16;
  29. preprocbufsize=32*1024;
  30. type
  31. tcommentstyle = (comment_none,comment_tp,comment_oldtp,comment_delphi,comment_c);
  32. tscannerfile = class;
  33. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else,pp_elseif);
  34. tpreprocstack = class
  35. typ : preproctyp;
  36. accept : boolean;
  37. next : tpreprocstack;
  38. name : TIDString;
  39. line_nb : longint;
  40. owner : tscannerfile;
  41. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  42. end;
  43. tdirectiveproc=procedure;
  44. tdirectiveitem = class(TFPHashObject)
  45. public
  46. is_conditional : boolean;
  47. proc : tdirectiveproc;
  48. constructor Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  49. constructor CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  50. end;
  51. // stack for replay buffers
  52. treplaystack = class
  53. token : ttoken;
  54. settings : tsettings;
  55. tokenbuf : tdynamicarray;
  56. next : treplaystack;
  57. constructor Create(atoken: ttoken;asettings:tsettings;
  58. atokenbuf:tdynamicarray;anext:treplaystack);
  59. end;
  60. tcompile_time_predicate = function(var valuedescr: String) : Boolean;
  61. tspecialgenerictoken =
  62. (ST_LOADSETTINGS,
  63. ST_LINE,
  64. ST_COLUMN,
  65. ST_FILEINDEX,
  66. ST_LOADMESSAGES);
  67. { tscannerfile }
  68. tscannerfile = class
  69. private
  70. procedure do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  71. procedure cachenexttokenpos;
  72. procedure setnexttoken;
  73. procedure savetokenpos;
  74. procedure restoretokenpos;
  75. procedure writetoken(t: ttoken);
  76. function readtoken : ttoken;
  77. public
  78. inputfile : tinputfile; { current inputfile list }
  79. inputfilecount : longint;
  80. inputbuffer, { input buffer }
  81. inputpointer : pchar;
  82. inputstart : longint;
  83. line_no, { line }
  84. lastlinepos : longint;
  85. lasttokenpos,
  86. nexttokenpos : longint; { token }
  87. lasttoken,
  88. nexttoken : ttoken;
  89. oldlasttokenpos : longint; { temporary saving/restoring tokenpos }
  90. oldcurrent_filepos,
  91. oldcurrent_tokenpos : tfileposinfo;
  92. replaytokenbuf,
  93. recordtokenbuf : tdynamicarray;
  94. { last settings we stored }
  95. last_settings : tsettings;
  96. last_message : pmessagestaterecord;
  97. { last filepos we stored }
  98. last_filepos,
  99. { if nexttoken<>NOTOKEN, then nexttokenpos holds its filepos }
  100. next_filepos : tfileposinfo;
  101. comment_level,
  102. yylexcount : longint;
  103. lastasmgetchar : char;
  104. ignoredirectives : TFPHashList; { ignore directives, used to give warnings only once }
  105. preprocstack : tpreprocstack;
  106. replaystack : treplaystack;
  107. in_asm_string : boolean;
  108. preproc_pattern : string;
  109. preproc_token : ttoken;
  110. constructor Create(const fn:string; is_macro: boolean = false);
  111. destructor Destroy;override;
  112. { File buffer things }
  113. function openinputfile:boolean;
  114. procedure closeinputfile;
  115. function tempopeninputfile:boolean;
  116. procedure tempcloseinputfile;
  117. procedure saveinputfile;
  118. procedure restoreinputfile;
  119. procedure firstfile;
  120. procedure nextfile;
  121. procedure addfile(hp:tinputfile);
  122. procedure reload;
  123. { replaces current token with the text in p }
  124. procedure substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  125. { Scanner things }
  126. procedure gettokenpos;
  127. procedure inc_comment_level;
  128. procedure dec_comment_level;
  129. procedure illegal_char(c:char);
  130. procedure end_of_file;
  131. procedure checkpreprocstack;
  132. procedure poppreprocstack;
  133. procedure ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  134. procedure elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  135. procedure elsepreprocstack;
  136. procedure popreplaystack;
  137. procedure handleconditional(p:tdirectiveitem);
  138. procedure handledirectives;
  139. procedure linebreak;
  140. procedure recordtoken;
  141. procedure startrecordtokens(buf:tdynamicarray);
  142. procedure stoprecordtokens;
  143. procedure replaytoken;
  144. procedure startreplaytokens(buf:tdynamicarray);
  145. { bit length asizeint is target depend }
  146. procedure tokenwritesizeint(val : asizeint);
  147. procedure tokenwritelongint(val : longint);
  148. procedure tokenwritelongword(val : longword);
  149. procedure tokenwriteword(val : word);
  150. procedure tokenwriteshortint(val : shortint);
  151. procedure tokenwriteset(var b;size : longint);
  152. procedure tokenwriteenum(var b;size : longint);
  153. function tokenreadsizeint : asizeint;
  154. procedure tokenwritesettings(var asettings : tsettings; var size : asizeint);
  155. { longword/longint are 32 bits on all targets }
  156. { word/smallint are 16-bits on all targest }
  157. function tokenreadlongword : longword;
  158. function tokenreadword : word;
  159. function tokenreadlongint : longint;
  160. function tokenreadsmallint : smallint;
  161. { short int is one a signed byte }
  162. function tokenreadshortint : shortint;
  163. function tokenreadbyte : byte;
  164. { This one takes the set size as an parameter }
  165. procedure tokenreadset(var b;size : longint);
  166. function tokenreadenum(size : longint) : longword;
  167. procedure tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  168. procedure readchar;
  169. procedure readstring;
  170. procedure readnumber;
  171. function readid:string;
  172. function readval:longint;
  173. function readval_asstring:string;
  174. function readcomment:string;
  175. function readquotedstring:string;
  176. function readstate:char;
  177. function readstatedefault:char;
  178. procedure skipspace;
  179. procedure skipuntildirective;
  180. procedure skipcomment;
  181. procedure skipdelphicomment;
  182. procedure skipoldtpcomment;
  183. procedure readtoken(allowrecordtoken:boolean);
  184. function readpreproc:ttoken;
  185. function asmgetcharstart : char;
  186. function asmgetchar:char;
  187. end;
  188. {$ifdef PREPROCWRITE}
  189. tpreprocfile=class
  190. f : text;
  191. buf : pointer;
  192. spacefound,
  193. eolfound : boolean;
  194. constructor create(const fn:string);
  195. destructor destroy;
  196. procedure Add(const s:string);
  197. procedure AddSpace;
  198. end;
  199. {$endif PREPROCWRITE}
  200. var
  201. { read strings }
  202. c : char;
  203. orgpattern,
  204. pattern : string;
  205. cstringpattern : ansistring;
  206. patternw : pcompilerwidestring;
  207. { token }
  208. token, { current token being parsed }
  209. idtoken : ttoken; { holds the token if the pattern is a known word }
  210. current_scanner : tscannerfile; { current scanner in use }
  211. aktcommentstyle : tcommentstyle; { needed to use read_comment from directives }
  212. {$ifdef PREPROCWRITE}
  213. preprocfile : tpreprocfile; { used with only preprocessing }
  214. {$endif PREPROCWRITE}
  215. type
  216. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  217. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  218. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  219. procedure InitScanner;
  220. procedure DoneScanner;
  221. { To be called when the language mode is finally determined }
  222. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  223. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  224. procedure SetAppType(NewAppType:tapptype);
  225. implementation
  226. uses
  227. SysUtils,
  228. cutils,cfileutl,
  229. systems,
  230. switches,
  231. symbase,symtable,symtype,symsym,symconst,symdef,defutil,
  232. { This is needed for tcputype }
  233. cpuinfo,
  234. fmodule
  235. {$if FPC_FULLVERSION<20700}
  236. ,ccharset
  237. {$endif}
  238. ;
  239. var
  240. { dictionaries with the supported directives }
  241. turbo_scannerdirectives : TFPHashObjectList; { for other modes }
  242. mac_scannerdirectives : TFPHashObjectList; { for mode mac }
  243. {*****************************************************************************
  244. Helper routines
  245. *****************************************************************************}
  246. const
  247. { use any special name that is an invalid file name to avoid problems }
  248. preprocstring : array [preproctyp] of string[7]
  249. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE','$ELSEIF');
  250. function is_keyword(const s:string):boolean;
  251. var
  252. low,high,mid : longint;
  253. begin
  254. if not (length(s) in [tokenlenmin..tokenlenmax]) or
  255. not (s[1] in ['a'..'z','A'..'Z']) then
  256. begin
  257. is_keyword:=false;
  258. exit;
  259. end;
  260. low:=ord(tokenidx^[length(s),s[1]].first);
  261. high:=ord(tokenidx^[length(s),s[1]].last);
  262. while low<high do
  263. begin
  264. mid:=(high+low+1) shr 1;
  265. if pattern<tokeninfo^[ttoken(mid)].str then
  266. high:=mid-1
  267. else
  268. low:=mid;
  269. end;
  270. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  271. ((tokeninfo^[ttoken(high)].keyword*current_settings.modeswitches)<>[]);
  272. end;
  273. Procedure HandleModeSwitches(switch: tmodeswitch; changeInit: boolean);
  274. begin
  275. { turn ansi/unicodestrings on by default ? (only change when this
  276. particular setting is changed, so that a random modeswitch won't
  277. change the state of $h+/$h-) }
  278. if switch in [m_none,m_default_ansistring,m_default_unicodestring] then
  279. begin
  280. if ([m_default_ansistring,m_default_unicodestring]*current_settings.modeswitches)<>[] then
  281. begin
  282. { can't have both ansistring and unicodestring as default }
  283. if switch=m_default_ansistring then
  284. begin
  285. exclude(current_settings.modeswitches,m_default_unicodestring);
  286. if changeinit then
  287. exclude(init_settings.modeswitches,m_default_unicodestring);
  288. end
  289. else if switch=m_default_unicodestring then
  290. begin
  291. exclude(current_settings.modeswitches,m_default_ansistring);
  292. if changeinit then
  293. exclude(init_settings.modeswitches,m_default_ansistring);
  294. end;
  295. { enable $h+ }
  296. include(current_settings.localswitches,cs_refcountedstrings);
  297. if changeinit then
  298. include(init_settings.localswitches,cs_refcountedstrings);
  299. if m_default_unicodestring in current_settings.modeswitches then
  300. def_system_macro('FPC_UNICODESTRINGS');
  301. end
  302. else
  303. begin
  304. exclude(current_settings.localswitches,cs_refcountedstrings);
  305. if changeinit then
  306. exclude(init_settings.localswitches,cs_refcountedstrings);
  307. undef_system_macro('FPC_UNICODESTRINGS');
  308. end;
  309. end;
  310. { turn inline on by default ? }
  311. if switch in [m_none,m_default_inline] then
  312. begin
  313. if (m_default_inline in current_settings.modeswitches) then
  314. begin
  315. include(current_settings.localswitches,cs_do_inline);
  316. if changeinit then
  317. include(init_settings.localswitches,cs_do_inline);
  318. end
  319. else
  320. begin
  321. exclude(current_settings.localswitches,cs_do_inline);
  322. if changeinit then
  323. exclude(init_settings.localswitches,cs_do_inline);
  324. end;
  325. end;
  326. { turn on system codepage by default }
  327. if switch in [m_none,m_systemcodepage] then
  328. begin
  329. if m_systemcodepage in current_settings.modeswitches then
  330. begin
  331. current_settings.sourcecodepage:=DefaultSystemCodePage;
  332. if (current_settings.sourcecodepage<>CP_UTF8) and not cpavailable(current_settings.sourcecodepage) then
  333. begin
  334. Message2(scan_w_unavailable_system_codepage,IntToStr(current_settings.sourcecodepage),IntToStr(default_settings.sourcecodepage));
  335. current_settings.sourcecodepage:=default_settings.sourcecodepage;
  336. end;
  337. include(current_settings.moduleswitches,cs_explicit_codepage);
  338. if changeinit then
  339. begin
  340. init_settings.sourcecodepage:=current_settings.sourcecodepage;
  341. include(init_settings.moduleswitches,cs_explicit_codepage);
  342. end;
  343. end
  344. else
  345. begin
  346. exclude(current_settings.moduleswitches,cs_explicit_codepage);
  347. if changeinit then
  348. exclude(init_settings.moduleswitches,cs_explicit_codepage);
  349. end;
  350. end;
  351. end;
  352. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  353. var
  354. b : boolean;
  355. oldmodeswitches : tmodeswitches;
  356. begin
  357. oldmodeswitches:=current_settings.modeswitches;
  358. b:=true;
  359. if s='DEFAULT' then
  360. current_settings.modeswitches:=fpcmodeswitches
  361. else
  362. if s='DELPHI' then
  363. current_settings.modeswitches:=delphimodeswitches
  364. else
  365. if s='DELPHIUNICODE' then
  366. current_settings.modeswitches:=delphiunicodemodeswitches
  367. else
  368. if s='TP' then
  369. current_settings.modeswitches:=tpmodeswitches
  370. else
  371. if s='FPC' then begin
  372. current_settings.modeswitches:=fpcmodeswitches;
  373. { TODO: enable this for 2.3/2.9 }
  374. // include(current_settings.localswitches, cs_typed_addresses);
  375. end else
  376. if s='OBJFPC' then begin
  377. current_settings.modeswitches:=objfpcmodeswitches;
  378. { TODO: enable this for 2.3/2.9 }
  379. // include(current_settings.localswitches, cs_typed_addresses);
  380. end
  381. {$ifdef gpc_mode}
  382. else if s='GPC' then
  383. current_settings.modeswitches:=gpcmodeswitches
  384. {$endif}
  385. else
  386. if s='MACPAS' then
  387. current_settings.modeswitches:=macmodeswitches
  388. else
  389. if s='ISO' then
  390. current_settings.modeswitches:=isomodeswitches
  391. else
  392. b:=false;
  393. {$ifdef jvm}
  394. { enable final fields by default for the JVM targets }
  395. include(current_settings.modeswitches,m_final_fields);
  396. {$endif jvm}
  397. if b and changeInit then
  398. init_settings.modeswitches := current_settings.modeswitches;
  399. if b then
  400. begin
  401. { resolve all postponed switch changes }
  402. flushpendingswitchesstate;
  403. HandleModeSwitches(m_none,changeinit);
  404. { turn on bitpacking for mode macpas and iso pascal }
  405. if ([m_mac,m_iso] * current_settings.modeswitches <> []) then
  406. begin
  407. include(current_settings.localswitches,cs_bitpacking);
  408. if changeinit then
  409. include(init_settings.localswitches,cs_bitpacking);
  410. end;
  411. { support goto/label by default in delphi/tp7/mac modes }
  412. if ([m_delphi,m_tp7,m_mac,m_iso] * current_settings.modeswitches <> []) then
  413. begin
  414. include(current_settings.moduleswitches,cs_support_goto);
  415. if changeinit then
  416. include(init_settings.moduleswitches,cs_support_goto);
  417. end;
  418. { support pointer math by default in fpc/objfpc modes }
  419. if ([m_fpc,m_objfpc] * current_settings.modeswitches <> []) then
  420. begin
  421. include(current_settings.localswitches,cs_pointermath);
  422. if changeinit then
  423. include(init_settings.localswitches,cs_pointermath);
  424. end
  425. else
  426. begin
  427. exclude(current_settings.localswitches,cs_pointermath);
  428. if changeinit then
  429. exclude(init_settings.localswitches,cs_pointermath);
  430. end;
  431. { Default enum and set packing for delphi/tp7 }
  432. if (m_tp7 in current_settings.modeswitches) or
  433. (m_delphi in current_settings.modeswitches) then
  434. begin
  435. current_settings.packenum:=1;
  436. current_settings.setalloc:=1;
  437. end
  438. else if (m_mac in current_settings.modeswitches) then
  439. { compatible with Metrowerks Pascal }
  440. current_settings.packenum:=2
  441. else
  442. current_settings.packenum:=4;
  443. if changeinit then
  444. init_settings.packenum:=current_settings.packenum;
  445. {$ifdef i386}
  446. { Default to intel assembler for delphi/tp7 on i386 }
  447. if (m_delphi in current_settings.modeswitches) or
  448. (m_tp7 in current_settings.modeswitches) then
  449. current_settings.asmmode:=asmmode_i386_intel;
  450. if changeinit then
  451. init_settings.asmmode:=current_settings.asmmode;
  452. {$endif i386}
  453. { Exception support explicitly turned on (mainly for macpas, to }
  454. { compensate for lack of interprocedural goto support) }
  455. if (cs_support_exceptions in current_settings.globalswitches) then
  456. include(current_settings.modeswitches,m_except);
  457. { Default strict string var checking in TP/Delphi modes }
  458. if ([m_delphi,m_tp7] * current_settings.modeswitches <> []) then
  459. begin
  460. include(current_settings.localswitches,cs_strict_var_strings);
  461. if changeinit then
  462. include(init_settings.localswitches,cs_strict_var_strings);
  463. end;
  464. { Undefine old symbol }
  465. if (m_delphi in oldmodeswitches) then
  466. undef_system_macro('FPC_DELPHI')
  467. else if (m_tp7 in oldmodeswitches) then
  468. undef_system_macro('FPC_TP')
  469. else if (m_objfpc in oldmodeswitches) then
  470. undef_system_macro('FPC_OBJFPC')
  471. {$ifdef gpc_mode}
  472. else if (m_gpc in oldmodeswitches) then
  473. undef_system_macro('FPC_GPC')
  474. {$endif}
  475. else if (m_mac in oldmodeswitches) then
  476. undef_system_macro('FPC_MACPAS');
  477. { define new symbol in delphi,objfpc,tp,gpc,macpas mode }
  478. if (m_delphi in current_settings.modeswitches) then
  479. def_system_macro('FPC_DELPHI')
  480. else if (m_tp7 in current_settings.modeswitches) then
  481. def_system_macro('FPC_TP')
  482. else if (m_objfpc in current_settings.modeswitches) then
  483. def_system_macro('FPC_OBJFPC')
  484. {$ifdef gpc_mode}
  485. else if (m_gpc in current_settings.modeswitches) then
  486. def_system_macro('FPC_GPC')
  487. {$endif}
  488. else if (m_mac in current_settings.modeswitches) then
  489. def_system_macro('FPC_MACPAS');
  490. end;
  491. SetCompileMode:=b;
  492. end;
  493. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  494. var
  495. i : tmodeswitch;
  496. doinclude : boolean;
  497. begin
  498. s:=upper(s);
  499. { on/off? }
  500. doinclude:=true;
  501. case s[length(s)] of
  502. '+':
  503. s:=copy(s,1,length(s)-1);
  504. '-':
  505. begin
  506. s:=copy(s,1,length(s)-1);
  507. doinclude:=false;
  508. end;
  509. end;
  510. Result:=false;
  511. for i:=m_class to high(tmodeswitch) do
  512. if s=modeswitchstr[i] then
  513. begin
  514. { Objective-C is currently only supported for Darwin targets }
  515. if doinclude and
  516. (i in [m_objectivec1,m_objectivec2]) and
  517. not(target_info.system in systems_objc_supported) then
  518. begin
  519. Message1(option_unsupported_target_for_feature,'Objective-C');
  520. break;
  521. end;
  522. if changeInit then
  523. current_settings.modeswitches:=init_settings.modeswitches;
  524. Result:=true;
  525. if doinclude then
  526. begin
  527. include(current_settings.modeswitches,i);
  528. { Objective-C 2.0 support implies 1.0 support }
  529. if (i=m_objectivec2) then
  530. include(current_settings.modeswitches,m_objectivec1);
  531. if (i in [m_objectivec1,m_objectivec2]) then
  532. include(current_settings.modeswitches,m_class);
  533. end
  534. else
  535. begin
  536. exclude(current_settings.modeswitches,i);
  537. { Objective-C 2.0 support implies 1.0 support }
  538. if (i=m_objectivec2) then
  539. exclude(current_settings.modeswitches,m_objectivec1);
  540. if (i in [m_objectivec1,m_objectivec2]) and
  541. ([m_delphi,m_objfpc]*current_settings.modeswitches=[]) then
  542. exclude(current_settings.modeswitches,m_class);
  543. end;
  544. { set other switches depending on changed mode switch }
  545. HandleModeSwitches(i,changeinit);
  546. if changeInit then
  547. init_settings.modeswitches:=current_settings.modeswitches;
  548. break;
  549. end;
  550. end;
  551. procedure SetAppType(NewAppType:tapptype);
  552. begin
  553. if apptype=app_cui then
  554. undef_system_macro('CONSOLE');
  555. apptype:=NewAppType;
  556. if apptype=app_cui then
  557. def_system_macro('CONSOLE');
  558. end;
  559. {*****************************************************************************
  560. Conditional Directives
  561. *****************************************************************************}
  562. procedure dir_else;
  563. begin
  564. current_scanner.elsepreprocstack;
  565. end;
  566. procedure dir_endif;
  567. begin
  568. current_scanner.poppreprocstack;
  569. end;
  570. function isdef(var valuedescr: String): Boolean;
  571. var
  572. hs : string;
  573. begin
  574. current_scanner.skipspace;
  575. hs:=current_scanner.readid;
  576. valuedescr:= hs;
  577. if hs='' then
  578. Message(scan_e_error_in_preproc_expr);
  579. isdef:=defined_macro(hs);
  580. end;
  581. procedure dir_ifdef;
  582. begin
  583. current_scanner.ifpreprocstack(pp_ifdef,@isdef,scan_c_ifdef_found);
  584. end;
  585. function isnotdef(var valuedescr: String): Boolean;
  586. var
  587. hs : string;
  588. begin
  589. current_scanner.skipspace;
  590. hs:=current_scanner.readid;
  591. valuedescr:= hs;
  592. if hs='' then
  593. Message(scan_e_error_in_preproc_expr);
  594. isnotdef:=not defined_macro(hs);
  595. end;
  596. procedure dir_ifndef;
  597. begin
  598. current_scanner.ifpreprocstack(pp_ifndef,@isnotdef,scan_c_ifndef_found);
  599. end;
  600. function opt_check(var valuedescr: String): Boolean;
  601. var
  602. hs : string;
  603. state : char;
  604. begin
  605. opt_check:= false;
  606. current_scanner.skipspace;
  607. hs:=current_scanner.readid;
  608. valuedescr:= hs;
  609. if (length(hs)>1) then
  610. Message1(scan_w_illegal_switch,hs)
  611. else
  612. begin
  613. state:=current_scanner.ReadState;
  614. if state in ['-','+'] then
  615. opt_check:=CheckSwitch(hs[1],state)
  616. else
  617. Message(scan_e_error_in_preproc_expr);
  618. end;
  619. end;
  620. procedure dir_ifopt;
  621. begin
  622. flushpendingswitchesstate;
  623. current_scanner.ifpreprocstack(pp_ifopt,@opt_check,scan_c_ifopt_found);
  624. end;
  625. procedure dir_libprefix;
  626. var
  627. s : string;
  628. begin
  629. current_scanner.skipspace;
  630. if c <> '''' then
  631. Message2(scan_f_syn_expected, '''', c);
  632. s := current_scanner.readquotedstring;
  633. stringdispose(outputprefix);
  634. outputprefix := stringdup(s);
  635. with current_module do
  636. setfilename(paramfn, paramallowoutput);
  637. end;
  638. procedure dir_libsuffix;
  639. var
  640. s : string;
  641. begin
  642. current_scanner.skipspace;
  643. if c <> '''' then
  644. Message2(scan_f_syn_expected, '''', c);
  645. s := current_scanner.readquotedstring;
  646. stringdispose(outputsuffix);
  647. outputsuffix := stringdup(s);
  648. with current_module do
  649. setfilename(paramfn, paramallowoutput);
  650. end;
  651. procedure dir_extension;
  652. var
  653. s : string;
  654. begin
  655. current_scanner.skipspace;
  656. if c <> '''' then
  657. Message2(scan_f_syn_expected, '''', c);
  658. s := current_scanner.readquotedstring;
  659. if OutputFileName='' then
  660. OutputFileName:=InputFileName;
  661. OutputFileName:=ChangeFileExt(OutputFileName,'.'+s);
  662. with current_module do
  663. setfilename(paramfn, paramallowoutput);
  664. end;
  665. {
  666. Compile time expression type check
  667. ----------------------------------
  668. Each subexpression returns its type to the caller, which then can
  669. do type check. Since data types of compile time expressions is
  670. not well defined, the type system does a best effort. The drawback is
  671. that some errors might not be detected.
  672. Instead of returning a particular data type, a set of possible data types
  673. are returned. This way ambigouos types can be handled. For instance a
  674. value of 1 can be both a boolean and and integer.
  675. Booleans
  676. --------
  677. The following forms of boolean values are supported:
  678. * C coded, that is 0 is false, non-zero is true.
  679. * TRUE/FALSE for mac style compile time variables
  680. Thus boolean mac compile time variables are always stored as TRUE/FALSE.
  681. When a compile time expression is evaluated, they are then translated
  682. to C coded booleans (0/1), to simplify for the expression evaluator.
  683. Note that this scheme then also of support mac compile time variables which
  684. are 0/1 but with a boolean meaning.
  685. The TRUE/FALSE format is new from 22 august 2005, but the above scheme
  686. means that units which is not recompiled, and thus stores
  687. compile time variables as the old format (0/1), continue to work.
  688. Short circuit evaluation
  689. ------------------------
  690. For this to work, the part of a compile time expression which is short
  691. circuited, should not be evaluated, while it still should be parsed.
  692. Therefor there is a parameter eval, telling whether evaluation is needed.
  693. In case not, the value returned can be arbitrary.
  694. }
  695. type
  696. {Compile time expression types}
  697. TCTEType = (ctetBoolean, ctetInteger, ctetString, ctetSet);
  698. TCTETypeSet = set of TCTEType;
  699. const
  700. cteTypeNames : array[TCTEType] of string[10] = (
  701. 'BOOLEAN','INTEGER','STRING','SET');
  702. {Subset of types which can be elements in sets.}
  703. setelementdefs = [ctetBoolean, ctetInteger, ctetString];
  704. function GetCTETypeName(t: TCTETypeSet): String;
  705. var
  706. i: TCTEType;
  707. begin
  708. result:= '';
  709. for i:= Low(TCTEType) to High(TCTEType) do
  710. if i in t then
  711. if result = '' then
  712. result:= cteTypeNames[i]
  713. else
  714. result:= result + ' or ' + cteTypeNames[i];
  715. end;
  716. procedure CTEError(actType, desiredExprType: TCTETypeSet; place: String);
  717. begin
  718. Message3(scan_e_compile_time_typeerror,
  719. GetCTETypeName(desiredExprType),
  720. GetCTETypeName(actType),
  721. place
  722. );
  723. end;
  724. function parse_compiler_expr(var compileExprType: TCTETypeSet):string;
  725. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string; forward;
  726. procedure preproc_consume(t : ttoken);
  727. begin
  728. if t<>current_scanner.preproc_token then
  729. Message(scan_e_preproc_syntax_error);
  730. current_scanner.preproc_token:=current_scanner.readpreproc;
  731. end;
  732. function preproc_substitutedtoken(var macroType: TCTETypeSet; eval : Boolean): string;
  733. { Currently this parses identifiers as well as numbers.
  734. The result from this procedure can either be that the token
  735. itself is a value, or that it is a compile time variable/macro,
  736. which then is substituted for another value (for macros
  737. recursivelly substituted).}
  738. var
  739. hs: string;
  740. mac : tmacro;
  741. macrocount,
  742. len : integer;
  743. numres : longint;
  744. w: word;
  745. begin
  746. result := current_scanner.preproc_pattern;
  747. if not eval then
  748. exit;
  749. mac:= nil;
  750. { Substitue macros and compiler variables with their content/value.
  751. For real macros also do recursive substitution. }
  752. macrocount:=0;
  753. repeat
  754. mac:=tmacro(search_macro(result));
  755. inc(macrocount);
  756. if macrocount>max_macro_nesting then
  757. begin
  758. Message(scan_w_macro_too_deep);
  759. break;
  760. end;
  761. if assigned(mac) and mac.defined then
  762. if assigned(mac.buftext) then
  763. begin
  764. if mac.buflen>255 then
  765. begin
  766. len:=255;
  767. Message(scan_w_macro_cut_after_255_chars);
  768. end
  769. else
  770. len:=mac.buflen;
  771. hs[0]:=char(len);
  772. move(mac.buftext^,hs[1],len);
  773. result:=upcase(hs);
  774. mac.is_used:=true;
  775. end
  776. else
  777. begin
  778. Message1(scan_e_error_macro_lacks_value, result);
  779. break;
  780. end
  781. else
  782. begin
  783. break;
  784. end;
  785. if mac.is_compiler_var then
  786. break;
  787. until false;
  788. { At this point, result do contain the value. Do some decoding and
  789. determine the type.}
  790. val(result,numres,w);
  791. if (w=0) then {It is an integer}
  792. begin
  793. if (numres = 0) or (numres = 1) then
  794. macroType := [ctetInteger, ctetBoolean]
  795. else
  796. macroType := [ctetInteger];
  797. end
  798. else if assigned(mac) and (m_mac in current_settings.modeswitches) and (result='FALSE') then
  799. begin
  800. result:= '0';
  801. macroType:= [ctetBoolean];
  802. end
  803. else if assigned(mac) and (m_mac in current_settings.modeswitches) and (result='TRUE') then
  804. begin
  805. result:= '1';
  806. macroType:= [ctetBoolean];
  807. end
  808. else if (m_mac in current_settings.modeswitches) and
  809. (not assigned(mac) or not mac.defined) and
  810. (macrocount = 1) then
  811. begin
  812. {Errors in mode mac is issued here. For non macpas modes there is
  813. more liberty, but the error will eventually be caught at a later stage.}
  814. Message1(scan_e_error_macro_undefined, result);
  815. macroType:= [ctetString]; {Just to have something}
  816. end
  817. else
  818. macroType:= [ctetString];
  819. end;
  820. function read_factor(var factorType: TCTETypeSet; eval : Boolean) : string;
  821. var
  822. hs,countstr : string;
  823. mac: tmacro;
  824. srsym : tsym;
  825. srsymtable : TSymtable;
  826. hdef : TDef;
  827. l : longint;
  828. w : integer;
  829. hasKlammer: Boolean;
  830. setElemType : TCTETypeSet;
  831. begin
  832. read_factor:='';
  833. if current_scanner.preproc_token=_ID then
  834. begin
  835. if current_scanner.preproc_pattern='DEFINED' then
  836. begin
  837. factorType:= [ctetBoolean];
  838. preproc_consume(_ID);
  839. current_scanner.skipspace;
  840. if current_scanner.preproc_token =_LKLAMMER then
  841. begin
  842. preproc_consume(_LKLAMMER);
  843. current_scanner.skipspace;
  844. hasKlammer:= true;
  845. end
  846. else if (m_mac in current_settings.modeswitches) then
  847. hasKlammer:= false
  848. else
  849. Message(scan_e_error_in_preproc_expr);
  850. if current_scanner.preproc_token =_ID then
  851. begin
  852. hs := current_scanner.preproc_pattern;
  853. mac := tmacro(search_macro(hs));
  854. if assigned(mac) and mac.defined then
  855. begin
  856. hs := '1';
  857. mac.is_used:=true;
  858. end
  859. else
  860. hs := '0';
  861. read_factor := hs;
  862. preproc_consume(_ID);
  863. current_scanner.skipspace;
  864. end
  865. else
  866. Message(scan_e_error_in_preproc_expr);
  867. if hasKlammer then
  868. if current_scanner.preproc_token =_RKLAMMER then
  869. preproc_consume(_RKLAMMER)
  870. else
  871. Message(scan_e_error_in_preproc_expr);
  872. end
  873. else
  874. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  875. begin
  876. factorType:= [ctetBoolean];
  877. preproc_consume(_ID);
  878. current_scanner.skipspace;
  879. if current_scanner.preproc_token =_ID then
  880. begin
  881. hs := current_scanner.preproc_pattern;
  882. mac := tmacro(search_macro(hs));
  883. if assigned(mac) then
  884. begin
  885. hs := '0';
  886. mac.is_used:=true;
  887. end
  888. else
  889. hs := '1';
  890. read_factor := hs;
  891. preproc_consume(_ID);
  892. current_scanner.skipspace;
  893. end
  894. else
  895. Message(scan_e_error_in_preproc_expr);
  896. end
  897. else
  898. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  899. begin
  900. factorType:= [ctetBoolean];
  901. preproc_consume(_ID);
  902. current_scanner.skipspace;
  903. if current_scanner.preproc_token =_LKLAMMER then
  904. begin
  905. preproc_consume(_LKLAMMER);
  906. current_scanner.skipspace;
  907. end
  908. else
  909. Message(scan_e_error_in_preproc_expr);
  910. if not (current_scanner.preproc_token = _ID) then
  911. Message(scan_e_error_in_preproc_expr);
  912. hs:=current_scanner.preproc_pattern;
  913. if (length(hs) > 1) then
  914. {This is allowed in Metrowerks Pascal}
  915. Message(scan_e_error_in_preproc_expr)
  916. else
  917. begin
  918. if CheckSwitch(hs[1],'+') then
  919. read_factor := '1'
  920. else
  921. read_factor := '0';
  922. end;
  923. preproc_consume(_ID);
  924. current_scanner.skipspace;
  925. if current_scanner.preproc_token =_RKLAMMER then
  926. preproc_consume(_RKLAMMER)
  927. else
  928. Message(scan_e_error_in_preproc_expr);
  929. end
  930. else
  931. if current_scanner.preproc_pattern='SIZEOF' then
  932. begin
  933. factorType:= [ctetInteger];
  934. preproc_consume(_ID);
  935. current_scanner.skipspace;
  936. if current_scanner.preproc_token =_LKLAMMER then
  937. begin
  938. preproc_consume(_LKLAMMER);
  939. current_scanner.skipspace;
  940. end
  941. else
  942. Message(scan_e_preproc_syntax_error);
  943. if eval then
  944. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  945. begin
  946. l:=0;
  947. case srsym.typ of
  948. staticvarsym,
  949. localvarsym,
  950. paravarsym :
  951. l:=tabstractvarsym(srsym).getsize;
  952. typesym:
  953. l:=ttypesym(srsym).typedef.size;
  954. else
  955. Message(scan_e_error_in_preproc_expr);
  956. end;
  957. str(l,read_factor);
  958. end
  959. else
  960. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  961. preproc_consume(_ID);
  962. current_scanner.skipspace;
  963. if current_scanner.preproc_token =_RKLAMMER then
  964. preproc_consume(_RKLAMMER)
  965. else
  966. Message(scan_e_preproc_syntax_error);
  967. end
  968. else
  969. if current_scanner.preproc_pattern='HIGH' then
  970. begin
  971. factorType:= [ctetInteger];
  972. preproc_consume(_ID);
  973. current_scanner.skipspace;
  974. if current_scanner.preproc_token =_LKLAMMER then
  975. begin
  976. preproc_consume(_LKLAMMER);
  977. current_scanner.skipspace;
  978. end
  979. else
  980. Message(scan_e_preproc_syntax_error);
  981. if eval then
  982. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  983. begin
  984. hdef:=nil;
  985. hs:='';
  986. l:=0;
  987. case srsym.typ of
  988. staticvarsym,
  989. localvarsym,
  990. paravarsym :
  991. hdef:=tabstractvarsym(srsym).vardef;
  992. typesym:
  993. hdef:=ttypesym(srsym).typedef;
  994. else
  995. Message(scan_e_error_in_preproc_expr);
  996. end;
  997. if hdef<>nil then
  998. begin
  999. if hdef.typ=setdef then
  1000. hdef:=tsetdef(hdef).elementdef;
  1001. case hdef.typ of
  1002. orddef:
  1003. with torddef(hdef).high do
  1004. if signed then
  1005. str(svalue,hs)
  1006. else
  1007. str(uvalue,hs);
  1008. enumdef:
  1009. l:=tenumdef(hdef).maxval;
  1010. arraydef:
  1011. if is_open_array(hdef) or is_array_of_const(hdef) or is_dynamic_array(hdef) then
  1012. Message(type_e_mismatch)
  1013. else
  1014. l:=tarraydef(hdef).highrange;
  1015. stringdef:
  1016. if is_open_string(hdef) or is_ansistring(hdef) or is_wide_or_unicode_string(hdef) then
  1017. Message(type_e_mismatch)
  1018. else
  1019. l:=tstringdef(hdef).len;
  1020. else
  1021. Message(type_e_mismatch);
  1022. end;
  1023. end;
  1024. if hs='' then
  1025. str(l,read_factor)
  1026. else
  1027. read_factor:=hs;
  1028. end
  1029. else
  1030. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  1031. preproc_consume(_ID);
  1032. current_scanner.skipspace;
  1033. if current_scanner.preproc_token =_RKLAMMER then
  1034. preproc_consume(_RKLAMMER)
  1035. else
  1036. Message(scan_e_preproc_syntax_error);
  1037. end
  1038. else
  1039. if current_scanner.preproc_pattern='DECLARED' then
  1040. begin
  1041. factorType:= [ctetBoolean];
  1042. preproc_consume(_ID);
  1043. current_scanner.skipspace;
  1044. if current_scanner.preproc_token =_LKLAMMER then
  1045. begin
  1046. preproc_consume(_LKLAMMER);
  1047. current_scanner.skipspace;
  1048. end
  1049. else
  1050. Message(scan_e_error_in_preproc_expr);
  1051. if current_scanner.preproc_token =_ID then
  1052. begin
  1053. hs := upper(current_scanner.preproc_pattern);
  1054. preproc_consume(_ID);
  1055. current_scanner.skipspace;
  1056. if current_scanner.preproc_token in [_LT,_LSHARPBRACKET] then
  1057. begin
  1058. l:=1;
  1059. preproc_consume(current_scanner.preproc_token);
  1060. current_scanner.skipspace;
  1061. while current_scanner.preproc_token=_COMMA do
  1062. begin
  1063. inc(l);
  1064. preproc_consume(_COMMA);
  1065. current_scanner.skipspace;
  1066. end;
  1067. if not (current_scanner.preproc_token in [_GT,_RSHARPBRACKET]) then
  1068. Message(scan_e_error_in_preproc_expr)
  1069. else
  1070. preproc_consume(current_scanner.preproc_token);
  1071. str(l,countstr);
  1072. hs:=hs+'$'+countstr;
  1073. end
  1074. else
  1075. { special case: <> }
  1076. if current_scanner.preproc_token=_NE then
  1077. begin
  1078. hs:=hs+'$1';
  1079. preproc_consume(_NE);
  1080. end;
  1081. current_scanner.skipspace;
  1082. if searchsym(hs,srsym,srsymtable) then
  1083. begin
  1084. { TSomeGeneric<...> also adds a TSomeGeneric symbol }
  1085. if (sp_generic_dummy in srsym.symoptions) and
  1086. (srsym.typ=typesym) and
  1087. (
  1088. { mode delphi}
  1089. (ttypesym(srsym).typedef.typ in [undefineddef,errordef]) or
  1090. { non-delphi modes }
  1091. (df_generic in ttypesym(srsym).typedef.defoptions)
  1092. ) then
  1093. hs:='0'
  1094. else
  1095. hs:='1';
  1096. end
  1097. else
  1098. hs := '0';
  1099. read_factor := hs;
  1100. end
  1101. else
  1102. Message(scan_e_error_in_preproc_expr);
  1103. if current_scanner.preproc_token =_RKLAMMER then
  1104. preproc_consume(_RKLAMMER)
  1105. else
  1106. Message(scan_e_error_in_preproc_expr);
  1107. end
  1108. else
  1109. if current_scanner.preproc_pattern='NOT' then
  1110. begin
  1111. factorType:= [ctetBoolean];
  1112. preproc_consume(_ID);
  1113. hs:=read_factor(factorType, eval);
  1114. if eval then
  1115. begin
  1116. if not (ctetBoolean in factorType) then
  1117. CTEError(factorType, [ctetBoolean], 'NOT');
  1118. val(hs,l,w);
  1119. if l<>0 then
  1120. read_factor:='0'
  1121. else
  1122. read_factor:='1';
  1123. end
  1124. else
  1125. read_factor:='0'; {Just to have something}
  1126. end
  1127. else
  1128. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='TRUE') then
  1129. begin
  1130. factorType:= [ctetBoolean];
  1131. preproc_consume(_ID);
  1132. read_factor:='1';
  1133. end
  1134. else
  1135. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='FALSE') then
  1136. begin
  1137. factorType:= [ctetBoolean];
  1138. preproc_consume(_ID);
  1139. read_factor:='0';
  1140. end
  1141. else
  1142. begin
  1143. hs:=preproc_substitutedtoken(factorType, eval);
  1144. { Default is to return the original symbol }
  1145. read_factor:=hs;
  1146. if eval and ([m_delphi,m_objfpc]*current_settings.modeswitches<>[]) and (ctetString in factorType) then
  1147. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  1148. begin
  1149. case srsym.typ of
  1150. constsym :
  1151. begin
  1152. with tconstsym(srsym) do
  1153. begin
  1154. case consttyp of
  1155. constord :
  1156. begin
  1157. case constdef.typ of
  1158. orddef:
  1159. begin
  1160. if is_integer(constdef) then
  1161. begin
  1162. read_factor:=tostr(value.valueord);
  1163. factorType:= [ctetInteger];
  1164. end
  1165. else if is_boolean(constdef) then
  1166. begin
  1167. read_factor:=tostr(value.valueord);
  1168. factorType:= [ctetBoolean];
  1169. end
  1170. else if is_char(constdef) then
  1171. begin
  1172. read_factor:=char(qword(value.valueord));
  1173. factorType:= [ctetString];
  1174. end
  1175. end;
  1176. enumdef:
  1177. begin
  1178. read_factor:=tostr(value.valueord);
  1179. factorType:= [ctetInteger];
  1180. end;
  1181. end;
  1182. end;
  1183. conststring :
  1184. begin
  1185. read_factor := upper(pchar(value.valueptr));
  1186. factorType:= [ctetString];
  1187. end;
  1188. constset :
  1189. begin
  1190. hs:=',';
  1191. for l:=0 to 255 do
  1192. if l in pconstset(tconstsym(srsym).value.valueptr)^ then
  1193. hs:=hs+tostr(l)+',';
  1194. read_factor := hs;
  1195. factorType:= [ctetSet];
  1196. end;
  1197. end;
  1198. end;
  1199. end;
  1200. enumsym :
  1201. begin
  1202. read_factor:=tostr(tenumsym(srsym).value);
  1203. factorType:= [ctetInteger];
  1204. end;
  1205. end;
  1206. end;
  1207. preproc_consume(_ID);
  1208. current_scanner.skipspace;
  1209. end
  1210. end
  1211. else if current_scanner.preproc_token =_LKLAMMER then
  1212. begin
  1213. preproc_consume(_LKLAMMER);
  1214. read_factor:=read_expr(factorType, eval);
  1215. preproc_consume(_RKLAMMER);
  1216. end
  1217. else if current_scanner.preproc_token = _LECKKLAMMER then
  1218. begin
  1219. preproc_consume(_LECKKLAMMER);
  1220. read_factor := ',';
  1221. while current_scanner.preproc_token = _ID do
  1222. begin
  1223. read_factor := read_factor+read_factor(setElemType, eval)+',';
  1224. if current_scanner.preproc_token = _COMMA then
  1225. preproc_consume(_COMMA);
  1226. end;
  1227. // TODO Add check of setElemType
  1228. preproc_consume(_RECKKLAMMER);
  1229. factorType:= [ctetSet];
  1230. end
  1231. else
  1232. Message(scan_e_error_in_preproc_expr);
  1233. end;
  1234. function read_term(var termType: TCTETypeSet; eval : Boolean) : string;
  1235. var
  1236. hs1,hs2 : string;
  1237. l1,l2 : longint;
  1238. w : integer;
  1239. termType2: TCTETypeSet;
  1240. begin
  1241. hs1:=read_factor(termType, eval);
  1242. repeat
  1243. if (current_scanner.preproc_token<>_ID) then
  1244. break;
  1245. if current_scanner.preproc_pattern<>'AND' then
  1246. break;
  1247. val(hs1,l1,w);
  1248. if l1=0 then
  1249. eval:= false; {Short circuit evaluation of OR}
  1250. if eval then
  1251. begin
  1252. {Check if first expr is boolean. Must be done here, after we know
  1253. it is an AND expression.}
  1254. if not (ctetBoolean in termType) then
  1255. CTEError(termType, [ctetBoolean], 'AND');
  1256. termType:= [ctetBoolean];
  1257. end;
  1258. preproc_consume(_ID);
  1259. hs2:=read_factor(termType2, eval);
  1260. if eval then
  1261. begin
  1262. if not (ctetBoolean in termType2) then
  1263. CTEError(termType2, [ctetBoolean], 'AND');
  1264. val(hs2,l2,w);
  1265. if (l1<>0) and (l2<>0) then
  1266. hs1:='1'
  1267. else
  1268. hs1:='0';
  1269. end;
  1270. until false;
  1271. read_term:=hs1;
  1272. end;
  1273. function read_simple_expr(var simpleExprType: TCTETypeSet; eval : Boolean) : string;
  1274. var
  1275. hs1,hs2 : string;
  1276. l1,l2 : longint;
  1277. w : integer;
  1278. simpleExprType2: TCTETypeSet;
  1279. begin
  1280. hs1:=read_term(simpleExprType, eval);
  1281. repeat
  1282. if (current_scanner.preproc_token<>_ID) then
  1283. break;
  1284. if current_scanner.preproc_pattern<>'OR' then
  1285. break;
  1286. val(hs1,l1,w);
  1287. if l1<>0 then
  1288. eval:= false; {Short circuit evaluation of OR}
  1289. if eval then
  1290. begin
  1291. {Check if first expr is boolean. Must be done here, after we know
  1292. it is an OR expression.}
  1293. if not (ctetBoolean in simpleExprType) then
  1294. CTEError(simpleExprType, [ctetBoolean], 'OR');
  1295. simpleExprType:= [ctetBoolean];
  1296. end;
  1297. preproc_consume(_ID);
  1298. hs2:=read_term(simpleExprType2, eval);
  1299. if eval then
  1300. begin
  1301. if not (ctetBoolean in simpleExprType2) then
  1302. CTEError(simpleExprType2, [ctetBoolean], 'OR');
  1303. val(hs2,l2,w);
  1304. if (l1<>0) or (l2<>0) then
  1305. hs1:='1'
  1306. else
  1307. hs1:='0';
  1308. end;
  1309. until false;
  1310. read_simple_expr:=hs1;
  1311. end;
  1312. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string;
  1313. var
  1314. hs1,hs2 : string;
  1315. b : boolean;
  1316. op : ttoken;
  1317. w : integer;
  1318. l1,l2 : longint;
  1319. exprType2: TCTETypeSet;
  1320. begin
  1321. hs1:=read_simple_expr(exprType, eval);
  1322. op:=current_scanner.preproc_token;
  1323. if (op = _ID) and (current_scanner.preproc_pattern = 'IN') then
  1324. op := _IN;
  1325. if not (op in [_IN,_EQ,_NE,_LT,_GT,_LTE,_GTE]) then
  1326. begin
  1327. read_expr:=hs1;
  1328. exit;
  1329. end;
  1330. if (op = _IN) then
  1331. preproc_consume(_ID)
  1332. else
  1333. preproc_consume(op);
  1334. hs2:=read_simple_expr(exprType2, eval);
  1335. if eval then
  1336. begin
  1337. if op = _IN then
  1338. begin
  1339. if exprType2 <> [ctetSet] then
  1340. CTEError(exprType2, [ctetSet], 'IN');
  1341. if exprType = [ctetSet] then
  1342. CTEError(exprType, setelementdefs, 'IN');
  1343. if is_number(hs1) and is_number(hs2) then
  1344. Message(scan_e_preproc_syntax_error)
  1345. else if hs2[1] = ',' then
  1346. b:=pos(','+hs1+',', hs2) > 0 { TODO For integer sets, perhaps check for numeric equivalence so that 0 = 00 }
  1347. else
  1348. Message(scan_e_preproc_syntax_error);
  1349. end
  1350. else
  1351. begin
  1352. if (exprType * exprType2) = [] then
  1353. CTEError(exprType2, exprType, '"'+hs1+' '+tokeninfo^[op].str+' '+hs2+'"');
  1354. if is_number(hs1) and is_number(hs2) then
  1355. begin
  1356. val(hs1,l1,w);
  1357. val(hs2,l2,w);
  1358. case op of
  1359. _EQ :
  1360. b:=l1=l2;
  1361. _NE :
  1362. b:=l1<>l2;
  1363. _LT :
  1364. b:=l1<l2;
  1365. _GT :
  1366. b:=l1>l2;
  1367. _GTE :
  1368. b:=l1>=l2;
  1369. _LTE :
  1370. b:=l1<=l2;
  1371. end;
  1372. end
  1373. else
  1374. begin
  1375. case op of
  1376. _EQ:
  1377. b:=hs1=hs2;
  1378. _NE :
  1379. b:=hs1<>hs2;
  1380. _LT :
  1381. b:=hs1<hs2;
  1382. _GT :
  1383. b:=hs1>hs2;
  1384. _GTE :
  1385. b:=hs1>=hs2;
  1386. _LTE :
  1387. b:=hs1<=hs2;
  1388. end;
  1389. end;
  1390. end;
  1391. end
  1392. else
  1393. b:= false; {Just to have something}
  1394. if b then
  1395. read_expr:='1'
  1396. else
  1397. read_expr:='0';
  1398. exprType:= [ctetBoolean];
  1399. end;
  1400. begin
  1401. current_scanner.skipspace;
  1402. { start preproc expression scanner }
  1403. current_scanner.preproc_token:=current_scanner.readpreproc;
  1404. parse_compiler_expr:=read_expr(compileExprType, true);
  1405. end;
  1406. function boolean_compile_time_expr(var valuedescr: String): Boolean;
  1407. var
  1408. hs : string;
  1409. exprType: TCTETypeSet;
  1410. begin
  1411. hs:=parse_compiler_expr(exprType);
  1412. if (exprType * [ctetBoolean]) = [] then
  1413. CTEError(exprType, [ctetBoolean], 'IF or ELSEIF');
  1414. boolean_compile_time_expr:= hs <> '0';
  1415. valuedescr:= hs;
  1416. end;
  1417. procedure dir_if;
  1418. begin
  1419. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  1420. end;
  1421. procedure dir_elseif;
  1422. begin
  1423. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  1424. end;
  1425. procedure dir_define_impl(macstyle: boolean);
  1426. var
  1427. hs : string;
  1428. bracketcount : longint;
  1429. mac : tmacro;
  1430. macropos : longint;
  1431. macrobuffer : pmacrobuffer;
  1432. begin
  1433. current_scanner.skipspace;
  1434. hs:=current_scanner.readid;
  1435. mac:=tmacro(search_macro(hs));
  1436. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  1437. begin
  1438. mac:=tmacro.create(hs);
  1439. mac.defined:=true;
  1440. current_module.localmacrosymtable.insert(mac);
  1441. end
  1442. else
  1443. begin
  1444. mac.defined:=true;
  1445. mac.is_compiler_var:=false;
  1446. { delete old definition }
  1447. if assigned(mac.buftext) then
  1448. begin
  1449. freemem(mac.buftext,mac.buflen);
  1450. mac.buftext:=nil;
  1451. end;
  1452. end;
  1453. Message1(parser_c_macro_defined,mac.name);
  1454. mac.is_used:=true;
  1455. if (cs_support_macro in current_settings.moduleswitches) then
  1456. begin
  1457. current_scanner.skipspace;
  1458. if not macstyle then
  1459. begin
  1460. { may be a macro? }
  1461. if c <> ':' then
  1462. exit;
  1463. current_scanner.readchar;
  1464. if c <> '=' then
  1465. exit;
  1466. current_scanner.readchar;
  1467. current_scanner.skipspace;
  1468. end;
  1469. { key words are never substituted }
  1470. if is_keyword(hs) then
  1471. Message(scan_e_keyword_cant_be_a_macro);
  1472. new(macrobuffer);
  1473. macropos:=0;
  1474. { parse macro, brackets are counted so it's possible
  1475. to have a $ifdef etc. in the macro }
  1476. bracketcount:=0;
  1477. repeat
  1478. case c of
  1479. '}' :
  1480. if (bracketcount=0) then
  1481. break
  1482. else
  1483. dec(bracketcount);
  1484. '{' :
  1485. inc(bracketcount);
  1486. #10,#13 :
  1487. current_scanner.linebreak;
  1488. #26 :
  1489. current_scanner.end_of_file;
  1490. end;
  1491. macrobuffer^[macropos]:=c;
  1492. inc(macropos);
  1493. if macropos>=maxmacrolen then
  1494. Message(scan_f_macro_buffer_overflow);
  1495. current_scanner.readchar;
  1496. until false;
  1497. { free buffer of macro ?}
  1498. if assigned(mac.buftext) then
  1499. freemem(mac.buftext,mac.buflen);
  1500. { get new mem }
  1501. getmem(mac.buftext,macropos);
  1502. mac.buflen:=macropos;
  1503. { copy the text }
  1504. move(macrobuffer^,mac.buftext^,macropos);
  1505. dispose(macrobuffer);
  1506. end
  1507. else
  1508. begin
  1509. { check if there is an assignment, then we need to give a
  1510. warning }
  1511. current_scanner.skipspace;
  1512. if c=':' then
  1513. begin
  1514. current_scanner.readchar;
  1515. if c='=' then
  1516. Message(scan_w_macro_support_turned_off);
  1517. end;
  1518. end;
  1519. end;
  1520. procedure dir_define;
  1521. begin
  1522. dir_define_impl(false);
  1523. end;
  1524. procedure dir_definec;
  1525. begin
  1526. dir_define_impl(true);
  1527. end;
  1528. procedure dir_setc;
  1529. var
  1530. hs : string;
  1531. mac : tmacro;
  1532. exprType: TCTETypeSet;
  1533. l : longint;
  1534. w : integer;
  1535. begin
  1536. current_scanner.skipspace;
  1537. hs:=current_scanner.readid;
  1538. mac:=tmacro(search_macro(hs));
  1539. if not assigned(mac) or
  1540. (mac.owner <> current_module.localmacrosymtable) then
  1541. begin
  1542. mac:=tmacro.create(hs);
  1543. mac.defined:=true;
  1544. mac.is_compiler_var:=true;
  1545. current_module.localmacrosymtable.insert(mac);
  1546. end
  1547. else
  1548. begin
  1549. mac.defined:=true;
  1550. mac.is_compiler_var:=true;
  1551. { delete old definition }
  1552. if assigned(mac.buftext) then
  1553. begin
  1554. freemem(mac.buftext,mac.buflen);
  1555. mac.buftext:=nil;
  1556. end;
  1557. end;
  1558. Message1(parser_c_macro_defined,mac.name);
  1559. mac.is_used:=true;
  1560. { key words are never substituted }
  1561. if is_keyword(hs) then
  1562. Message(scan_e_keyword_cant_be_a_macro);
  1563. { macro assignment can be both := and = }
  1564. current_scanner.skipspace;
  1565. if c=':' then
  1566. current_scanner.readchar;
  1567. if c='=' then
  1568. begin
  1569. current_scanner.readchar;
  1570. hs:= parse_compiler_expr(exprType);
  1571. if (exprType * [ctetBoolean, ctetInteger]) = [] then
  1572. CTEError(exprType, [ctetBoolean, ctetInteger], 'SETC');
  1573. if length(hs) <> 0 then
  1574. begin
  1575. {If we are absolutely shure it is boolean, translate
  1576. to TRUE/FALSE to increase possibility to do future type check}
  1577. if exprType = [ctetBoolean] then
  1578. begin
  1579. val(hs,l,w);
  1580. if l<>0 then
  1581. hs:='TRUE'
  1582. else
  1583. hs:='FALSE';
  1584. end;
  1585. Message2(parser_c_macro_set_to,mac.name,hs);
  1586. { free buffer of macro ?}
  1587. if assigned(mac.buftext) then
  1588. freemem(mac.buftext,mac.buflen);
  1589. { get new mem }
  1590. getmem(mac.buftext,length(hs));
  1591. mac.buflen:=length(hs);
  1592. { copy the text }
  1593. move(hs[1],mac.buftext^,mac.buflen);
  1594. end
  1595. else
  1596. Message(scan_e_preproc_syntax_error);
  1597. end
  1598. else
  1599. Message(scan_e_preproc_syntax_error);
  1600. end;
  1601. procedure dir_undef;
  1602. var
  1603. hs : string;
  1604. mac : tmacro;
  1605. begin
  1606. current_scanner.skipspace;
  1607. hs:=current_scanner.readid;
  1608. mac:=tmacro(search_macro(hs));
  1609. if not assigned(mac) or
  1610. (mac.owner <> current_module.localmacrosymtable) then
  1611. begin
  1612. mac:=tmacro.create(hs);
  1613. mac.defined:=false;
  1614. current_module.localmacrosymtable.insert(mac);
  1615. end
  1616. else
  1617. begin
  1618. mac.defined:=false;
  1619. mac.is_compiler_var:=false;
  1620. { delete old definition }
  1621. if assigned(mac.buftext) then
  1622. begin
  1623. freemem(mac.buftext,mac.buflen);
  1624. mac.buftext:=nil;
  1625. end;
  1626. end;
  1627. Message1(parser_c_macro_undefined,mac.name);
  1628. mac.is_used:=true;
  1629. end;
  1630. procedure dir_include;
  1631. function findincludefile(const path,name:TCmdStr;var foundfile:TCmdStr):boolean;
  1632. var
  1633. found : boolean;
  1634. hpath : TCmdStr;
  1635. begin
  1636. (* look for the include file
  1637. If path was absolute and specified as part of {$I } then
  1638. 1. specified path
  1639. else
  1640. 1. path of current inputfile,current dir
  1641. 2. local includepath
  1642. 3. global includepath
  1643. -- Check mantis #13461 before changing this *)
  1644. found:=false;
  1645. foundfile:='';
  1646. hpath:='';
  1647. if path_absolute(path) then
  1648. begin
  1649. found:=FindFile(name,path,true,foundfile);
  1650. end
  1651. else
  1652. begin
  1653. hpath:=current_scanner.inputfile.path+';'+CurDirRelPath(source_info);
  1654. found:=FindFile(path+name, hpath,true,foundfile);
  1655. if not found then
  1656. found:=current_module.localincludesearchpath.FindFile(path+name,true,foundfile);
  1657. if not found then
  1658. found:=includesearchpath.FindFile(path+name,true,foundfile);
  1659. end;
  1660. result:=found;
  1661. end;
  1662. var
  1663. foundfile : TCmdStr;
  1664. path,
  1665. name,
  1666. hs : tpathstr;
  1667. args : string;
  1668. hp : tinputfile;
  1669. found : boolean;
  1670. macroIsString : boolean;
  1671. begin
  1672. current_scanner.skipspace;
  1673. args:=current_scanner.readcomment;
  1674. hs:=GetToken(args,' ');
  1675. if hs='' then
  1676. exit;
  1677. if (hs[1]='%') then
  1678. begin
  1679. { case insensitive }
  1680. hs:=upper(hs);
  1681. { remove %'s }
  1682. Delete(hs,1,1);
  1683. if hs[length(hs)]='%' then
  1684. Delete(hs,length(hs),1);
  1685. { save old }
  1686. path:=hs;
  1687. { first check for internal macros }
  1688. macroIsString:=true;
  1689. if hs='TIME' then
  1690. hs:=gettimestr
  1691. else
  1692. if hs='DATE' then
  1693. hs:=getdatestr
  1694. else
  1695. if hs='FILE' then
  1696. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex)
  1697. else
  1698. if hs='LINE' then
  1699. hs:=tostr(current_filepos.line)
  1700. else
  1701. if hs='LINENUM' then
  1702. begin
  1703. hs:=tostr(current_filepos.line);
  1704. macroIsString:=false;
  1705. end
  1706. else
  1707. if hs='FPCVERSION' then
  1708. hs:=version_string
  1709. else
  1710. if hs='FPCDATE' then
  1711. hs:=date_string
  1712. else
  1713. if hs='FPCTARGET' then
  1714. hs:=target_cpu_string
  1715. else
  1716. if hs='FPCTARGETCPU' then
  1717. hs:=target_cpu_string
  1718. else
  1719. if hs='FPCTARGETOS' then
  1720. hs:=target_info.shortname
  1721. else
  1722. hs:=GetEnvironmentVariable(hs);
  1723. if hs='' then
  1724. Message1(scan_w_include_env_not_found,path);
  1725. { make it a stringconst }
  1726. if macroIsString then
  1727. hs:=''''+hs+'''';
  1728. current_scanner.substitutemacro(path,@hs[1],length(hs),
  1729. current_scanner.line_no,current_scanner.inputfile.ref_index);
  1730. end
  1731. else
  1732. begin
  1733. hs:=FixFileName(hs);
  1734. path:=ExtractFilePath(hs);
  1735. name:=ExtractFileName(hs);
  1736. { Special case for Delphi compatibility: '*' has to be replaced
  1737. by the file name of the current source file. }
  1738. if (length(name)>=1) and
  1739. (name[1]='*') then
  1740. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  1741. { try to find the file }
  1742. found:=findincludefile(path,name,foundfile);
  1743. if (ExtractFileExt(name)='') then
  1744. begin
  1745. { try default extensions .inc , .pp and .pas }
  1746. if (not found) then
  1747. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  1748. if (not found) then
  1749. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  1750. if (not found) then
  1751. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  1752. end;
  1753. if current_scanner.inputfilecount<max_include_nesting then
  1754. begin
  1755. inc(current_scanner.inputfilecount);
  1756. { we need to reread the current char }
  1757. dec(current_scanner.inputpointer);
  1758. { shutdown current file }
  1759. current_scanner.tempcloseinputfile;
  1760. { load new file }
  1761. hp:=do_openinputfile(foundfile);
  1762. current_scanner.addfile(hp);
  1763. current_module.sourcefiles.register_file(hp);
  1764. if (not found) then
  1765. Message1(scan_f_cannot_open_includefile,hs);
  1766. if (not current_scanner.openinputfile) then
  1767. Message1(scan_f_cannot_open_includefile,hs);
  1768. Message1(scan_t_start_include_file,current_scanner.inputfile.path+current_scanner.inputfile.name);
  1769. current_scanner.reload;
  1770. end
  1771. else
  1772. Message(scan_f_include_deep_ten);
  1773. end;
  1774. end;
  1775. {*****************************************************************************
  1776. Preprocessor writing
  1777. *****************************************************************************}
  1778. {$ifdef PREPROCWRITE}
  1779. constructor tpreprocfile.create(const fn:string);
  1780. begin
  1781. { open outputfile }
  1782. assign(f,fn);
  1783. {$push}{$I-}
  1784. rewrite(f);
  1785. {$pop}
  1786. if ioresult<>0 then
  1787. Comment(V_Fatal,'can''t create file '+fn);
  1788. getmem(buf,preprocbufsize);
  1789. settextbuf(f,buf^,preprocbufsize);
  1790. { reset }
  1791. eolfound:=false;
  1792. spacefound:=false;
  1793. end;
  1794. destructor tpreprocfile.destroy;
  1795. begin
  1796. close(f);
  1797. freemem(buf,preprocbufsize);
  1798. end;
  1799. procedure tpreprocfile.add(const s:string);
  1800. begin
  1801. write(f,s);
  1802. end;
  1803. procedure tpreprocfile.addspace;
  1804. begin
  1805. if eolfound then
  1806. begin
  1807. writeln(f,'');
  1808. eolfound:=false;
  1809. spacefound:=false;
  1810. end
  1811. else
  1812. if spacefound then
  1813. begin
  1814. write(f,' ');
  1815. spacefound:=false;
  1816. end;
  1817. end;
  1818. {$endif PREPROCWRITE}
  1819. {*****************************************************************************
  1820. TPreProcStack
  1821. *****************************************************************************}
  1822. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  1823. begin
  1824. accept:=a;
  1825. typ:=atyp;
  1826. next:=n;
  1827. end;
  1828. {*****************************************************************************
  1829. TReplayStack
  1830. *****************************************************************************}
  1831. constructor treplaystack.Create(atoken:ttoken;asettings:tsettings;
  1832. atokenbuf:tdynamicarray;anext:treplaystack);
  1833. begin
  1834. token:=atoken;
  1835. settings:=asettings;
  1836. tokenbuf:=atokenbuf;
  1837. next:=anext;
  1838. end;
  1839. {*****************************************************************************
  1840. TDirectiveItem
  1841. *****************************************************************************}
  1842. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  1843. begin
  1844. inherited Create(AList,n);
  1845. is_conditional:=false;
  1846. proc:=p;
  1847. end;
  1848. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  1849. begin
  1850. inherited Create(AList,n);
  1851. is_conditional:=true;
  1852. proc:=p;
  1853. end;
  1854. {****************************************************************************
  1855. TSCANNERFILE
  1856. ****************************************************************************}
  1857. constructor tscannerfile.create(const fn:string; is_macro: boolean = false);
  1858. begin
  1859. inputfile:=do_openinputfile(fn);
  1860. if is_macro then
  1861. inputfile.is_macro:=true;
  1862. if assigned(current_module) then
  1863. current_module.sourcefiles.register_file(inputfile);
  1864. { reset localinput }
  1865. c:=#0;
  1866. inputbuffer:=nil;
  1867. inputpointer:=nil;
  1868. inputstart:=0;
  1869. { reset scanner }
  1870. preprocstack:=nil;
  1871. replaystack:=nil;
  1872. comment_level:=0;
  1873. yylexcount:=0;
  1874. block_type:=bt_general;
  1875. line_no:=0;
  1876. lastlinepos:=0;
  1877. lasttokenpos:=0;
  1878. nexttokenpos:=0;
  1879. lasttoken:=NOTOKEN;
  1880. nexttoken:=NOTOKEN;
  1881. lastasmgetchar:=#0;
  1882. ignoredirectives:=TFPHashList.Create;
  1883. in_asm_string:=false;
  1884. end;
  1885. procedure tscannerfile.firstfile;
  1886. begin
  1887. { load block }
  1888. if not openinputfile then
  1889. Message1(scan_f_cannot_open_input,inputfile.name);
  1890. reload;
  1891. end;
  1892. destructor tscannerfile.destroy;
  1893. begin
  1894. if assigned(current_module) and
  1895. (current_module.state=ms_compiled) and
  1896. (status.errorcount=0) then
  1897. checkpreprocstack
  1898. else
  1899. begin
  1900. while assigned(preprocstack) do
  1901. poppreprocstack;
  1902. end;
  1903. while assigned(replaystack) do
  1904. popreplaystack;
  1905. if not inputfile.closed then
  1906. closeinputfile;
  1907. if inputfile.is_macro then
  1908. inputfile.free;
  1909. ignoredirectives.free;
  1910. end;
  1911. function tscannerfile.openinputfile:boolean;
  1912. begin
  1913. openinputfile:=inputfile.open;
  1914. { load buffer }
  1915. inputbuffer:=inputfile.buf;
  1916. inputpointer:=inputfile.buf;
  1917. inputstart:=inputfile.bufstart;
  1918. { line }
  1919. line_no:=0;
  1920. lastlinepos:=0;
  1921. lasttokenpos:=0;
  1922. nexttokenpos:=0;
  1923. end;
  1924. procedure tscannerfile.closeinputfile;
  1925. begin
  1926. inputfile.close;
  1927. { reset buffer }
  1928. inputbuffer:=nil;
  1929. inputpointer:=nil;
  1930. inputstart:=0;
  1931. { reset line }
  1932. line_no:=0;
  1933. lastlinepos:=0;
  1934. lasttokenpos:=0;
  1935. nexttokenpos:=0;
  1936. end;
  1937. function tscannerfile.tempopeninputfile:boolean;
  1938. begin
  1939. if inputfile.is_macro then
  1940. exit;
  1941. tempopeninputfile:=inputfile.tempopen;
  1942. { reload buffer }
  1943. inputbuffer:=inputfile.buf;
  1944. inputpointer:=inputfile.buf;
  1945. inputstart:=inputfile.bufstart;
  1946. end;
  1947. procedure tscannerfile.tempcloseinputfile;
  1948. begin
  1949. if inputfile.closed or inputfile.is_macro then
  1950. exit;
  1951. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  1952. inputfile.tempclose;
  1953. { reset buffer }
  1954. inputbuffer:=nil;
  1955. inputpointer:=nil;
  1956. inputstart:=0;
  1957. end;
  1958. procedure tscannerfile.saveinputfile;
  1959. begin
  1960. inputfile.saveinputpointer:=inputpointer;
  1961. inputfile.savelastlinepos:=lastlinepos;
  1962. inputfile.saveline_no:=line_no;
  1963. end;
  1964. procedure tscannerfile.restoreinputfile;
  1965. begin
  1966. inputbuffer:=inputfile.buf;
  1967. inputpointer:=inputfile.saveinputpointer;
  1968. lastlinepos:=inputfile.savelastlinepos;
  1969. line_no:=inputfile.saveline_no;
  1970. if not inputfile.is_macro then
  1971. parser_current_file:=inputfile.name;
  1972. end;
  1973. procedure tscannerfile.nextfile;
  1974. var
  1975. to_dispose : tinputfile;
  1976. begin
  1977. if assigned(inputfile.next) then
  1978. begin
  1979. if inputfile.is_macro then
  1980. to_dispose:=inputfile
  1981. else
  1982. begin
  1983. to_dispose:=nil;
  1984. dec(inputfilecount);
  1985. end;
  1986. { we can allways close the file, no ? }
  1987. inputfile.close;
  1988. inputfile:=inputfile.next;
  1989. if assigned(to_dispose) then
  1990. to_dispose.free;
  1991. restoreinputfile;
  1992. end;
  1993. end;
  1994. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  1995. begin
  1996. if not assigned(buf) then
  1997. internalerror(200511172);
  1998. if assigned(recordtokenbuf) then
  1999. internalerror(200511173);
  2000. recordtokenbuf:=buf;
  2001. fillchar(last_settings,sizeof(last_settings),0);
  2002. last_message:=nil;
  2003. fillchar(last_filepos,sizeof(last_filepos),0);
  2004. end;
  2005. procedure tscannerfile.stoprecordtokens;
  2006. begin
  2007. if not assigned(recordtokenbuf) then
  2008. internalerror(200511174);
  2009. recordtokenbuf:=nil;
  2010. end;
  2011. procedure tscannerfile.writetoken(t : ttoken);
  2012. var
  2013. b : byte;
  2014. begin
  2015. if ord(t)>$7f then
  2016. begin
  2017. b:=(ord(t) shr 8) or $80;
  2018. recordtokenbuf.write(b,1);
  2019. end;
  2020. b:=ord(t) and $ff;
  2021. recordtokenbuf.write(b,1);
  2022. end;
  2023. procedure tscannerfile.tokenwritesizeint(val : asizeint);
  2024. begin
  2025. {$ifdef FPC_BIG_ENDIAN}
  2026. val:=swapendian(val);
  2027. {$endif}
  2028. recordtokenbuf.write(val,sizeof(asizeint));
  2029. end;
  2030. procedure tscannerfile.tokenwritelongint(val : longint);
  2031. begin
  2032. {$ifdef FPC_BIG_ENDIAN}
  2033. val:=swapendian(val);
  2034. {$endif}
  2035. recordtokenbuf.write(val,sizeof(longint));
  2036. end;
  2037. procedure tscannerfile.tokenwriteshortint(val : shortint);
  2038. begin
  2039. {$ifdef FPC_BIG_ENDIAN}
  2040. val:=swapendian(val);
  2041. {$endif}
  2042. recordtokenbuf.write(val,sizeof(shortint));
  2043. end;
  2044. procedure tscannerfile.tokenwriteword(val : word);
  2045. begin
  2046. {$ifdef FPC_BIG_ENDIAN}
  2047. val:=swapendian(val);
  2048. {$endif}
  2049. recordtokenbuf.write(val,sizeof(word));
  2050. end;
  2051. procedure tscannerfile.tokenwritelongword(val : longword);
  2052. begin
  2053. {$ifdef FPC_BIG_ENDIAN}
  2054. val:=swapendian(val);
  2055. {$endif}
  2056. recordtokenbuf.write(val,sizeof(longword));
  2057. end;
  2058. function tscannerfile.tokenreadsizeint : asizeint;
  2059. var
  2060. val : asizeint;
  2061. begin
  2062. replaytokenbuf.read(val,sizeof(asizeint));
  2063. {$ifdef FPC_BIG_ENDIAN}
  2064. val:=swapendian(val);
  2065. {$endif}
  2066. result:=val;
  2067. end;
  2068. function tscannerfile.tokenreadlongword : longword;
  2069. var
  2070. val : longword;
  2071. begin
  2072. replaytokenbuf.read(val,sizeof(longword));
  2073. {$ifdef FPC_BIG_ENDIAN}
  2074. val:=swapendian(val);
  2075. {$endif}
  2076. result:=val;
  2077. end;
  2078. function tscannerfile.tokenreadlongint : longint;
  2079. var
  2080. val : longint;
  2081. begin
  2082. replaytokenbuf.read(val,sizeof(longint));
  2083. {$ifdef FPC_BIG_ENDIAN}
  2084. val:=swapendian(val);
  2085. {$endif}
  2086. result:=val;
  2087. end;
  2088. function tscannerfile.tokenreadshortint : shortint;
  2089. var
  2090. val : shortint;
  2091. begin
  2092. replaytokenbuf.read(val,sizeof(shortint));
  2093. result:=val;
  2094. end;
  2095. function tscannerfile.tokenreadbyte : byte;
  2096. var
  2097. val : byte;
  2098. begin
  2099. replaytokenbuf.read(val,sizeof(byte));
  2100. result:=val;
  2101. end;
  2102. function tscannerfile.tokenreadsmallint : smallint;
  2103. var
  2104. val : smallint;
  2105. begin
  2106. replaytokenbuf.read(val,sizeof(smallint));
  2107. {$ifdef FPC_BIG_ENDIAN}
  2108. val:=swapendian(val);
  2109. {$endif}
  2110. result:=val;
  2111. end;
  2112. function tscannerfile.tokenreadword : word;
  2113. var
  2114. val : word;
  2115. begin
  2116. replaytokenbuf.read(val,sizeof(word));
  2117. {$ifdef FPC_BIG_ENDIAN}
  2118. val:=swapendian(val);
  2119. {$endif}
  2120. result:=val;
  2121. end;
  2122. function tscannerfile.tokenreadenum(size : longint) : longword;
  2123. begin
  2124. if size=1 then
  2125. result:=tokenreadbyte
  2126. else if size=2 then
  2127. result:=tokenreadword
  2128. else if size=4 then
  2129. result:=tokenreadlongword;
  2130. end;
  2131. procedure tscannerfile.tokenreadset(var b;size : longint);
  2132. {$ifdef FPC_BIG_ENDIAN}
  2133. var
  2134. i : longint;
  2135. {$endif}
  2136. begin
  2137. replaytokenbuf.read(b,size);
  2138. {$ifdef FPC_BIG_ENDIAN}
  2139. for i:=0 to size-1 do
  2140. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  2141. {$endif}
  2142. end;
  2143. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  2144. begin
  2145. recordtokenbuf.write(b,size);
  2146. end;
  2147. procedure tscannerfile.tokenwriteset(var b;size : longint);
  2148. {$ifdef FPC_BIG_ENDIAN}
  2149. var
  2150. i: longint;
  2151. tmpset: array[0..31] of byte;
  2152. {$endif}
  2153. begin
  2154. {$ifdef FPC_BIG_ENDIAN}
  2155. for i:=0 to size-1 do
  2156. tmpset[i]:=reverse_byte(Pbyte(@b)[i]);
  2157. recordtokenbuf.write(tmpset,size);
  2158. {$else}
  2159. recordtokenbuf.write(b,size);
  2160. {$endif}
  2161. end;
  2162. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  2163. { This procedure
  2164. needs to be changed whenever
  2165. globals.tsettings type is changed,
  2166. the problem is that no error will appear
  2167. before tests with generics are tested. PM }
  2168. var
  2169. startpos, endpos : longword;
  2170. begin
  2171. { WARNING all those fields need to be in the correct
  2172. order otherwise cross_endian PPU reading will fail }
  2173. startpos:=replaytokenbuf.pos;
  2174. with asettings do
  2175. begin
  2176. alignment.procalign:=tokenreadlongint;
  2177. alignment.loopalign:=tokenreadlongint;
  2178. alignment.jumpalign:=tokenreadlongint;
  2179. alignment.constalignmin:=tokenreadlongint;
  2180. alignment.constalignmax:=tokenreadlongint;
  2181. alignment.varalignmin:=tokenreadlongint;
  2182. alignment.varalignmax:=tokenreadlongint;
  2183. alignment.localalignmin:=tokenreadlongint;
  2184. alignment.localalignmax:=tokenreadlongint;
  2185. alignment.recordalignmin:=tokenreadlongint;
  2186. alignment.recordalignmax:=tokenreadlongint;
  2187. alignment.maxCrecordalign:=tokenreadlongint;
  2188. tokenreadset(globalswitches,sizeof(globalswitches));
  2189. tokenreadset(targetswitches,sizeof(targetswitches));
  2190. tokenreadset(moduleswitches,sizeof(moduleswitches));
  2191. tokenreadset(localswitches,sizeof(localswitches));
  2192. tokenreadset(modeswitches,sizeof(modeswitches));
  2193. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  2194. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2195. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2196. tokenreadset(debugswitches,sizeof(debugswitches));
  2197. { 0: old behaviour for sets <=256 elements
  2198. >0: round to this size }
  2199. setalloc:=tokenreadshortint;
  2200. packenum:=tokenreadshortint;
  2201. packrecords:=tokenreadshortint;
  2202. maxfpuregisters:=tokenreadshortint;
  2203. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2204. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2205. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  2206. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  2207. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  2208. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  2209. { tstringencoding is word type,
  2210. thus this should be OK here }
  2211. sourcecodepage:=tstringEncoding(tokenreadword);
  2212. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  2213. disabledircache:=boolean(tokenreadbyte);
  2214. {$if defined(ARM) or defined(AVR)}
  2215. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)));
  2216. {$endif defined(ARM) or defined(AVR)}
  2217. endpos:=replaytokenbuf.pos;
  2218. if endpos-startpos<>expected_size then
  2219. Comment(V_Error,'Wrong size of Settings read-in');
  2220. end;
  2221. end;
  2222. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  2223. { This procedure
  2224. needs to be changed whenever
  2225. globals.tsettings type is changed,
  2226. the problem is that no error will appear
  2227. before tests with generics are tested. PM }
  2228. var
  2229. sizepos, startpos, endpos : longword;
  2230. begin
  2231. { WARNING all those fields need to be in the correct
  2232. order otherwise cross_endian PPU reading will fail }
  2233. sizepos:=recordtokenbuf.pos;
  2234. size:=0;
  2235. tokenwritesizeint(size);
  2236. startpos:=recordtokenbuf.pos;
  2237. with asettings do
  2238. begin
  2239. tokenwritelongint(alignment.procalign);
  2240. tokenwritelongint(alignment.loopalign);
  2241. tokenwritelongint(alignment.jumpalign);
  2242. tokenwritelongint(alignment.constalignmin);
  2243. tokenwritelongint(alignment.constalignmax);
  2244. tokenwritelongint(alignment.varalignmin);
  2245. tokenwritelongint(alignment.varalignmax);
  2246. tokenwritelongint(alignment.localalignmin);
  2247. tokenwritelongint(alignment.localalignmax);
  2248. tokenwritelongint(alignment.recordalignmin);
  2249. tokenwritelongint(alignment.recordalignmax);
  2250. tokenwritelongint(alignment.maxCrecordalign);
  2251. tokenwriteset(globalswitches,sizeof(globalswitches));
  2252. tokenwriteset(targetswitches,sizeof(targetswitches));
  2253. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  2254. tokenwriteset(localswitches,sizeof(localswitches));
  2255. tokenwriteset(modeswitches,sizeof(modeswitches));
  2256. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  2257. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2258. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2259. tokenwriteset(debugswitches,sizeof(debugswitches));
  2260. { 0: old behaviour for sets <=256 elements
  2261. >0: round to this size }
  2262. tokenwriteshortint(setalloc);
  2263. tokenwriteshortint(packenum);
  2264. tokenwriteshortint(packrecords);
  2265. tokenwriteshortint(maxfpuregisters);
  2266. tokenwriteenum(cputype,sizeof(tcputype));
  2267. tokenwriteenum(optimizecputype,sizeof(tcputype));
  2268. tokenwriteenum(fputype,sizeof(tfputype));
  2269. tokenwriteenum(asmmode,sizeof(tasmmode));
  2270. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  2271. tokenwriteenum(defproccall,sizeof(tproccalloption));
  2272. { tstringencoding is word type,
  2273. thus this should be OK here }
  2274. tokenwriteword(sourcecodepage);
  2275. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  2276. recordtokenbuf.write(byte(disabledircache),1);
  2277. {$if defined(ARM) or defined(AVR)}
  2278. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  2279. {$endif defined(ARM) or defined(AVR)}
  2280. endpos:=recordtokenbuf.pos;
  2281. size:=endpos-startpos;
  2282. recordtokenbuf.seek(sizepos);
  2283. tokenwritesizeint(size);
  2284. recordtokenbuf.seek(endpos);
  2285. end;
  2286. end;
  2287. procedure tscannerfile.recordtoken;
  2288. var
  2289. t : ttoken;
  2290. s : tspecialgenerictoken;
  2291. len,msgnb,copy_size : asizeint;
  2292. val : longint;
  2293. b : byte;
  2294. pmsg : pmessagestaterecord;
  2295. begin
  2296. if not assigned(recordtokenbuf) then
  2297. internalerror(200511176);
  2298. t:=_GENERICSPECIALTOKEN;
  2299. { settings changed? }
  2300. { last field pmessage is handled separately below in
  2301. ST_LOADMESSAGES }
  2302. if CompareByte(current_settings,last_settings,
  2303. sizeof(current_settings)-sizeof(pointer))<>0 then
  2304. begin
  2305. { use a special token to record it }
  2306. s:=ST_LOADSETTINGS;
  2307. writetoken(t);
  2308. recordtokenbuf.write(s,1);
  2309. copy_size:=sizeof(current_settings)-sizeof(pointer);
  2310. tokenwritesettings(current_settings,copy_size);
  2311. last_settings:=current_settings;
  2312. end;
  2313. if current_settings.pmessage<>last_message then
  2314. begin
  2315. { use a special token to record it }
  2316. s:=ST_LOADMESSAGES;
  2317. writetoken(t);
  2318. recordtokenbuf.write(s,1);
  2319. msgnb:=0;
  2320. pmsg:=current_settings.pmessage;
  2321. while assigned(pmsg) do
  2322. begin
  2323. if msgnb=high(asizeint) then
  2324. { Too many messages }
  2325. internalerror(2011090401);
  2326. inc(msgnb);
  2327. pmsg:=pmsg^.next;
  2328. end;
  2329. tokenwritesizeint(msgnb);
  2330. pmsg:=current_settings.pmessage;
  2331. while assigned(pmsg) do
  2332. begin
  2333. { What about endianess here?}
  2334. { SB: this is handled by tokenreadlongint }
  2335. val:=pmsg^.value;
  2336. tokenwritelongint(val);
  2337. val:=ord(pmsg^.state);
  2338. tokenwritelongint(val);
  2339. pmsg:=pmsg^.next;
  2340. end;
  2341. last_message:=current_settings.pmessage;
  2342. end;
  2343. { file pos changes? }
  2344. if current_tokenpos.line<>last_filepos.line then
  2345. begin
  2346. s:=ST_LINE;
  2347. writetoken(t);
  2348. recordtokenbuf.write(s,1);
  2349. recordtokenbuf.write(current_tokenpos.line,sizeof(current_tokenpos.line));
  2350. last_filepos.line:=current_tokenpos.line;
  2351. end;
  2352. if current_tokenpos.column<>last_filepos.column then
  2353. begin
  2354. s:=ST_COLUMN;
  2355. writetoken(t);
  2356. { can the column be written packed? }
  2357. if current_tokenpos.column<$80 then
  2358. begin
  2359. b:=$80 or current_tokenpos.column;
  2360. recordtokenbuf.write(b,1);
  2361. end
  2362. else
  2363. begin
  2364. recordtokenbuf.write(s,1);
  2365. recordtokenbuf.write(current_tokenpos.column,sizeof(current_tokenpos.column));
  2366. end;
  2367. last_filepos.column:=current_tokenpos.column;
  2368. end;
  2369. if current_tokenpos.fileindex<>last_filepos.fileindex then
  2370. begin
  2371. s:=ST_FILEINDEX;
  2372. writetoken(t);
  2373. recordtokenbuf.write(s,1);
  2374. recordtokenbuf.write(current_tokenpos.fileindex,sizeof(current_tokenpos.fileindex));
  2375. last_filepos.fileindex:=current_tokenpos.fileindex;
  2376. end;
  2377. writetoken(token);
  2378. if token<>_GENERICSPECIALTOKEN then
  2379. writetoken(idtoken);
  2380. case token of
  2381. _CWCHAR,
  2382. _CWSTRING :
  2383. begin
  2384. tokenwritesizeint(patternw^.len);
  2385. if patternw^.len>0 then
  2386. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2387. end;
  2388. _CSTRING:
  2389. begin
  2390. len:=length(cstringpattern);
  2391. tokenwritesizeint(len);
  2392. if len>0 then
  2393. recordtokenbuf.write(cstringpattern[1],len);
  2394. end;
  2395. _CCHAR,
  2396. _INTCONST,
  2397. _REALNUMBER :
  2398. begin
  2399. { pexpr.pas messes with pattern in case of negative integer consts,
  2400. see around line 2562 the comment of JM; remove the - before recording it
  2401. (FK)
  2402. }
  2403. if (token=_INTCONST) and (pattern[1]='-') then
  2404. delete(pattern,1,1);
  2405. recordtokenbuf.write(pattern[0],1);
  2406. recordtokenbuf.write(pattern[1],length(pattern));
  2407. end;
  2408. _ID :
  2409. begin
  2410. recordtokenbuf.write(orgpattern[0],1);
  2411. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  2412. end;
  2413. end;
  2414. end;
  2415. procedure tscannerfile.startreplaytokens(buf:tdynamicarray);
  2416. begin
  2417. if not assigned(buf) then
  2418. internalerror(200511175);
  2419. { save current token }
  2420. if token in [_CWCHAR,_CWSTRING,_CCHAR,_CSTRING,_INTCONST,_REALNUMBER,_ID] then
  2421. internalerror(200511178);
  2422. replaystack:=treplaystack.create(token,current_settings,
  2423. replaytokenbuf,replaystack);
  2424. if assigned(inputpointer) then
  2425. dec(inputpointer);
  2426. { install buffer }
  2427. replaytokenbuf:=buf;
  2428. { reload next token }
  2429. replaytokenbuf.seek(0);
  2430. replaytoken;
  2431. end;
  2432. function tscannerfile.readtoken: ttoken;
  2433. var
  2434. b,b2 : byte;
  2435. begin
  2436. replaytokenbuf.read(b,1);
  2437. if (b and $80)<>0 then
  2438. begin
  2439. replaytokenbuf.read(b2,1);
  2440. result:=ttoken(((b and $7f) shl 8) or b2);
  2441. end
  2442. else
  2443. result:=ttoken(b);
  2444. end;
  2445. procedure tscannerfile.replaytoken;
  2446. var
  2447. wlen,mesgnb,copy_size : asizeint;
  2448. specialtoken : tspecialgenerictoken;
  2449. i : byte;
  2450. pmsg,prevmsg : pmessagestaterecord;
  2451. begin
  2452. if not assigned(replaytokenbuf) then
  2453. internalerror(200511177);
  2454. { End of replay buffer? Then load the next char from the file again }
  2455. if replaytokenbuf.pos>=replaytokenbuf.size then
  2456. begin
  2457. token:=replaystack.token;
  2458. replaytokenbuf:=replaystack.tokenbuf;
  2459. { restore compiler settings }
  2460. current_settings:=replaystack.settings;
  2461. popreplaystack;
  2462. if assigned(inputpointer) then
  2463. begin
  2464. c:=inputpointer^;
  2465. inc(inputpointer);
  2466. end;
  2467. exit;
  2468. end;
  2469. repeat
  2470. { load token from the buffer }
  2471. token:=readtoken;
  2472. if token<>_GENERICSPECIALTOKEN then
  2473. idtoken:=readtoken
  2474. else
  2475. idtoken:=_NOID;
  2476. case token of
  2477. _CWCHAR,
  2478. _CWSTRING :
  2479. begin
  2480. wlen:=tokenreadsizeint;
  2481. setlengthwidestring(patternw,wlen);
  2482. if wlen>0 then
  2483. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2484. orgpattern:='';
  2485. pattern:='';
  2486. cstringpattern:='';
  2487. end;
  2488. _CSTRING:
  2489. begin
  2490. wlen:=tokenreadsizeint;
  2491. if wlen>0 then
  2492. begin
  2493. setlength(cstringpattern,wlen);
  2494. replaytokenbuf.read(cstringpattern[1],wlen);
  2495. end
  2496. else
  2497. cstringpattern:='';
  2498. orgpattern:='';
  2499. pattern:='';
  2500. end;
  2501. _CCHAR,
  2502. _INTCONST,
  2503. _REALNUMBER :
  2504. begin
  2505. replaytokenbuf.read(pattern[0],1);
  2506. replaytokenbuf.read(pattern[1],length(pattern));
  2507. orgpattern:='';
  2508. end;
  2509. _ID :
  2510. begin
  2511. replaytokenbuf.read(orgpattern[0],1);
  2512. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  2513. pattern:=upper(orgpattern);
  2514. end;
  2515. _GENERICSPECIALTOKEN:
  2516. begin
  2517. replaytokenbuf.read(specialtoken,1);
  2518. { packed column? }
  2519. if (ord(specialtoken) and $80)<>0 then
  2520. begin
  2521. current_tokenpos.column:=ord(specialtoken) and $7f;
  2522. current_filepos:=current_tokenpos;
  2523. end
  2524. else
  2525. case specialtoken of
  2526. ST_LOADSETTINGS:
  2527. begin
  2528. copy_size:=tokenreadsizeint;
  2529. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  2530. // internalerror(2011090501);
  2531. {
  2532. replaytokenbuf.read(current_settings,copy_size);
  2533. }
  2534. tokenreadsettings(current_settings,copy_size);
  2535. end;
  2536. ST_LOADMESSAGES:
  2537. begin
  2538. current_settings.pmessage:=nil;
  2539. mesgnb:=tokenreadsizeint;
  2540. if mesgnb>0 then
  2541. Comment(V_Error,'Message recordind not yet supported');
  2542. for i:=1 to mesgnb do
  2543. begin
  2544. new(pmsg);
  2545. if i=1 then
  2546. begin
  2547. current_settings.pmessage:=pmsg;
  2548. prevmsg:=nil;
  2549. end
  2550. else
  2551. prevmsg^.next:=pmsg;
  2552. pmsg^.value:=tokenreadlongint;
  2553. pmsg^.state:=tmsgstate(tokenreadlongint);
  2554. pmsg^.next:=nil;
  2555. prevmsg:=pmsg;
  2556. end;
  2557. end;
  2558. ST_LINE:
  2559. begin
  2560. current_tokenpos.line:=tokenreadlongint;
  2561. current_filepos:=current_tokenpos;
  2562. end;
  2563. ST_COLUMN:
  2564. begin
  2565. current_tokenpos.column:=tokenreadword;
  2566. current_filepos:=current_tokenpos;
  2567. end;
  2568. ST_FILEINDEX:
  2569. begin
  2570. current_tokenpos.fileindex:=tokenreadword;
  2571. current_filepos:=current_tokenpos;
  2572. end;
  2573. else
  2574. internalerror(2006103010);
  2575. end;
  2576. continue;
  2577. end;
  2578. end;
  2579. break;
  2580. until false;
  2581. end;
  2582. procedure tscannerfile.addfile(hp:tinputfile);
  2583. begin
  2584. saveinputfile;
  2585. { add to list }
  2586. hp.next:=inputfile;
  2587. inputfile:=hp;
  2588. { load new inputfile }
  2589. restoreinputfile;
  2590. end;
  2591. procedure tscannerfile.reload;
  2592. begin
  2593. with inputfile do
  2594. begin
  2595. { when nothing more to read then leave immediatly, so we
  2596. don't change the current_filepos and leave it point to the last
  2597. char }
  2598. if (c=#26) and (not assigned(next)) then
  2599. exit;
  2600. repeat
  2601. { still more to read?, then change the #0 to a space so its seen
  2602. as a seperator, this can't be used for macro's which can change
  2603. the place of the #0 in the buffer with tempopen }
  2604. if (c=#0) and (bufsize>0) and
  2605. not(inputfile.is_macro) and
  2606. (inputpointer-inputbuffer<bufsize) then
  2607. begin
  2608. c:=' ';
  2609. inc(inputpointer);
  2610. exit;
  2611. end;
  2612. { can we read more from this file ? }
  2613. if (c<>#26) and (not endoffile) then
  2614. begin
  2615. readbuf;
  2616. inputpointer:=buf;
  2617. inputbuffer:=buf;
  2618. inputstart:=bufstart;
  2619. { first line? }
  2620. if line_no=0 then
  2621. begin
  2622. c:=inputpointer^;
  2623. { eat utf-8 signature? }
  2624. if (ord(inputpointer^)=$ef) and
  2625. (ord((inputpointer+1)^)=$bb) and
  2626. (ord((inputpointer+2)^)=$bf) then
  2627. begin
  2628. (* we don't support including files with an UTF-8 bom
  2629. inside another file that wasn't encoded as UTF-8
  2630. already (we don't support {$codepage xxx} switches in
  2631. the middle of a file either) *)
  2632. if (current_settings.sourcecodepage<>CP_UTF8) and
  2633. not current_module.in_global then
  2634. Message(scanner_f_illegal_utf8_bom);
  2635. inc(inputpointer,3);
  2636. message(scan_c_switching_to_utf8);
  2637. current_settings.sourcecodepage:=CP_UTF8;
  2638. include(current_settings.moduleswitches,cs_explicit_codepage);
  2639. end;
  2640. line_no:=1;
  2641. if cs_asm_source in current_settings.globalswitches then
  2642. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  2643. end;
  2644. end
  2645. else
  2646. begin
  2647. { load eof position in tokenpos/current_filepos }
  2648. gettokenpos;
  2649. { close file }
  2650. closeinputfile;
  2651. { no next module, than EOF }
  2652. if not assigned(inputfile.next) then
  2653. begin
  2654. c:=#26;
  2655. exit;
  2656. end;
  2657. { load next file and reopen it }
  2658. nextfile;
  2659. tempopeninputfile;
  2660. { status }
  2661. Message1(scan_t_back_in,inputfile.name);
  2662. end;
  2663. { load next char }
  2664. c:=inputpointer^;
  2665. inc(inputpointer);
  2666. until c<>#0; { if also end, then reload again }
  2667. end;
  2668. end;
  2669. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  2670. var
  2671. hp : tinputfile;
  2672. begin
  2673. { save old postion }
  2674. dec(inputpointer);
  2675. tempcloseinputfile;
  2676. { create macro 'file' }
  2677. { use special name to dispose after !! }
  2678. hp:=do_openinputfile('_Macro_.'+macname);
  2679. addfile(hp);
  2680. with inputfile do
  2681. begin
  2682. setmacro(p,len);
  2683. { local buffer }
  2684. inputbuffer:=buf;
  2685. inputpointer:=buf;
  2686. inputstart:=bufstart;
  2687. ref_index:=fileindex;
  2688. end;
  2689. { reset line }
  2690. line_no:=line;
  2691. lastlinepos:=0;
  2692. lasttokenpos:=0;
  2693. nexttokenpos:=0;
  2694. { load new c }
  2695. c:=inputpointer^;
  2696. inc(inputpointer);
  2697. end;
  2698. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  2699. begin
  2700. tokenpos:=inputstart+(inputpointer-inputbuffer);
  2701. filepos.line:=line_no;
  2702. filepos.column:=tokenpos-lastlinepos;
  2703. filepos.fileindex:=inputfile.ref_index;
  2704. filepos.moduleindex:=current_module.unit_index;
  2705. end;
  2706. procedure tscannerfile.gettokenpos;
  2707. { load the values of tokenpos and lasttokenpos }
  2708. begin
  2709. do_gettokenpos(lasttokenpos,current_tokenpos);
  2710. current_filepos:=current_tokenpos;
  2711. end;
  2712. procedure tscannerfile.cachenexttokenpos;
  2713. begin
  2714. do_gettokenpos(nexttokenpos,next_filepos);
  2715. end;
  2716. procedure tscannerfile.setnexttoken;
  2717. begin
  2718. token:=nexttoken;
  2719. nexttoken:=NOTOKEN;
  2720. lasttokenpos:=nexttokenpos;
  2721. current_tokenpos:=next_filepos;
  2722. current_filepos:=current_tokenpos;
  2723. nexttokenpos:=0;
  2724. end;
  2725. procedure tscannerfile.savetokenpos;
  2726. begin
  2727. oldlasttokenpos:=lasttokenpos;
  2728. oldcurrent_filepos:=current_filepos;
  2729. oldcurrent_tokenpos:=current_tokenpos;
  2730. end;
  2731. procedure tscannerfile.restoretokenpos;
  2732. begin
  2733. lasttokenpos:=oldlasttokenpos;
  2734. current_filepos:=oldcurrent_filepos;
  2735. current_tokenpos:=oldcurrent_tokenpos;
  2736. end;
  2737. procedure tscannerfile.inc_comment_level;
  2738. begin
  2739. if (m_nested_comment in current_settings.modeswitches) then
  2740. inc(comment_level)
  2741. else
  2742. comment_level:=1;
  2743. if (comment_level>1) then
  2744. begin
  2745. savetokenpos;
  2746. gettokenpos; { update for warning }
  2747. Message1(scan_w_comment_level,tostr(comment_level));
  2748. restoretokenpos;
  2749. end;
  2750. end;
  2751. procedure tscannerfile.dec_comment_level;
  2752. begin
  2753. if (m_nested_comment in current_settings.modeswitches) then
  2754. dec(comment_level)
  2755. else
  2756. comment_level:=0;
  2757. end;
  2758. procedure tscannerfile.linebreak;
  2759. var
  2760. cur : char;
  2761. begin
  2762. with inputfile do
  2763. begin
  2764. if (byte(inputpointer^)=0) and not(endoffile) then
  2765. begin
  2766. cur:=c;
  2767. reload;
  2768. if byte(cur)+byte(c)<>23 then
  2769. dec(inputpointer);
  2770. end
  2771. else
  2772. begin
  2773. { Support all combination of #10 and #13 as line break }
  2774. if (byte(inputpointer^)+byte(c)=23) then
  2775. inc(inputpointer);
  2776. end;
  2777. { Always return #10 as line break }
  2778. c:=#10;
  2779. { increase line counters }
  2780. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  2781. inc(line_no);
  2782. { update linebuffer }
  2783. if cs_asm_source in current_settings.globalswitches then
  2784. inputfile.setline(line_no,lastlinepos);
  2785. { update for status and call the show status routine,
  2786. but don't touch current_filepos ! }
  2787. savetokenpos;
  2788. gettokenpos; { update for v_status }
  2789. inc(status.compiledlines);
  2790. ShowStatus;
  2791. restoretokenpos;
  2792. end;
  2793. end;
  2794. procedure tscannerfile.illegal_char(c:char);
  2795. var
  2796. s : string;
  2797. begin
  2798. if c in [#32..#255] then
  2799. s:=''''+c+''''
  2800. else
  2801. s:='#'+tostr(ord(c));
  2802. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  2803. end;
  2804. procedure tscannerfile.end_of_file;
  2805. begin
  2806. checkpreprocstack;
  2807. Message(scan_f_end_of_file);
  2808. end;
  2809. {-------------------------------------------
  2810. IF Conditional Handling
  2811. -------------------------------------------}
  2812. procedure tscannerfile.checkpreprocstack;
  2813. begin
  2814. { check for missing ifdefs }
  2815. while assigned(preprocstack) do
  2816. begin
  2817. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  2818. preprocstack.owner.inputfile.name,tostr(preprocstack.line_nb));
  2819. poppreprocstack;
  2820. end;
  2821. end;
  2822. procedure tscannerfile.poppreprocstack;
  2823. var
  2824. hp : tpreprocstack;
  2825. begin
  2826. if assigned(preprocstack) then
  2827. begin
  2828. Message1(scan_c_endif_found,preprocstack.name);
  2829. hp:=preprocstack.next;
  2830. preprocstack.free;
  2831. preprocstack:=hp;
  2832. end
  2833. else
  2834. Message(scan_e_endif_without_if);
  2835. end;
  2836. procedure tscannerfile.ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  2837. var
  2838. condition: Boolean;
  2839. valuedescr: String;
  2840. begin
  2841. if (preprocstack=nil) or preprocstack.accept then
  2842. condition:= compile_time_predicate(valuedescr)
  2843. else
  2844. begin
  2845. condition:= false;
  2846. valuedescr:= '';
  2847. end;
  2848. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  2849. preprocstack.name:=valuedescr;
  2850. preprocstack.line_nb:=line_no;
  2851. preprocstack.owner:=self;
  2852. if preprocstack.accept then
  2853. Message2(messid,preprocstack.name,'accepted')
  2854. else
  2855. Message2(messid,preprocstack.name,'rejected');
  2856. end;
  2857. procedure tscannerfile.elsepreprocstack;
  2858. begin
  2859. if assigned(preprocstack) and
  2860. (preprocstack.typ<>pp_else) then
  2861. begin
  2862. if (preprocstack.typ=pp_elseif) then
  2863. preprocstack.accept:=false
  2864. else
  2865. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  2866. preprocstack.accept:=not preprocstack.accept;
  2867. preprocstack.typ:=pp_else;
  2868. preprocstack.line_nb:=line_no;
  2869. if preprocstack.accept then
  2870. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2871. else
  2872. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2873. end
  2874. else
  2875. Message(scan_e_endif_without_if);
  2876. end;
  2877. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  2878. var
  2879. valuedescr: String;
  2880. begin
  2881. if assigned(preprocstack) and
  2882. (preprocstack.typ in [pp_if,pp_elseif]) then
  2883. begin
  2884. { when the branch is accepted we use pp_elseif so we know that
  2885. all the next branches need to be rejected. when this branch is still
  2886. not accepted then leave it at pp_if }
  2887. if (preprocstack.typ=pp_elseif) then
  2888. preprocstack.accept:=false
  2889. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  2890. begin
  2891. preprocstack.accept:=false;
  2892. preprocstack.typ:=pp_elseif;
  2893. end
  2894. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  2895. and compile_time_predicate(valuedescr) then
  2896. begin
  2897. preprocstack.name:=valuedescr;
  2898. preprocstack.accept:=true;
  2899. preprocstack.typ:=pp_elseif;
  2900. end;
  2901. preprocstack.line_nb:=line_no;
  2902. if preprocstack.accept then
  2903. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2904. else
  2905. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2906. end
  2907. else
  2908. Message(scan_e_endif_without_if);
  2909. end;
  2910. procedure tscannerfile.popreplaystack;
  2911. var
  2912. hp : treplaystack;
  2913. begin
  2914. if assigned(replaystack) then
  2915. begin
  2916. hp:=replaystack.next;
  2917. replaystack.free;
  2918. replaystack:=hp;
  2919. end;
  2920. end;
  2921. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  2922. begin
  2923. savetokenpos;
  2924. repeat
  2925. current_scanner.gettokenpos;
  2926. Message1(scan_d_handling_switch,'$'+p.name);
  2927. p.proc();
  2928. { accept the text ? }
  2929. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  2930. break
  2931. else
  2932. begin
  2933. current_scanner.gettokenpos;
  2934. Message(scan_c_skipping_until);
  2935. repeat
  2936. current_scanner.skipuntildirective;
  2937. if not (m_mac in current_settings.modeswitches) then
  2938. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  2939. else
  2940. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  2941. until assigned(p) and (p.is_conditional);
  2942. current_scanner.gettokenpos;
  2943. end;
  2944. until false;
  2945. restoretokenpos;
  2946. end;
  2947. procedure tscannerfile.handledirectives;
  2948. var
  2949. t : tdirectiveitem;
  2950. hs : string;
  2951. begin
  2952. gettokenpos;
  2953. readchar; {Remove the $}
  2954. hs:=readid;
  2955. { handle empty directive }
  2956. if hs='' then
  2957. begin
  2958. Message1(scan_w_illegal_switch,'$');
  2959. exit;
  2960. end;
  2961. {$ifdef PREPROCWRITE}
  2962. if parapreprocess then
  2963. begin
  2964. t:=Get_Directive(hs);
  2965. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  2966. begin
  2967. preprocfile^.AddSpace;
  2968. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  2969. exit;
  2970. end;
  2971. end;
  2972. {$endif PREPROCWRITE}
  2973. { skip this directive? }
  2974. if (ignoredirectives.find(hs)<>nil) then
  2975. begin
  2976. if (comment_level>0) then
  2977. readcomment;
  2978. { we've read the whole comment }
  2979. aktcommentstyle:=comment_none;
  2980. exit;
  2981. end;
  2982. { Check for compiler switches }
  2983. while (length(hs)=1) and (c in ['-','+']) do
  2984. begin
  2985. Message1(scan_d_handling_switch,'$'+hs+c);
  2986. HandleSwitch(hs[1],c);
  2987. current_scanner.readchar; {Remove + or -}
  2988. if c=',' then
  2989. begin
  2990. current_scanner.readchar; {Remove , }
  2991. { read next switch, support $v+,$+}
  2992. hs:=current_scanner.readid;
  2993. if (hs='') then
  2994. begin
  2995. if (c='$') and (m_fpc in current_settings.modeswitches) then
  2996. begin
  2997. current_scanner.readchar; { skip $ }
  2998. hs:=current_scanner.readid;
  2999. end;
  3000. if (hs='') then
  3001. Message1(scan_w_illegal_directive,'$'+c);
  3002. end;
  3003. end
  3004. else
  3005. hs:='';
  3006. end;
  3007. { directives may follow switches after a , }
  3008. if hs<>'' then
  3009. begin
  3010. if not (m_mac in current_settings.modeswitches) then
  3011. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  3012. else
  3013. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  3014. if assigned(t) then
  3015. begin
  3016. if t.is_conditional then
  3017. handleconditional(t)
  3018. else
  3019. begin
  3020. Message1(scan_d_handling_switch,'$'+hs);
  3021. t.proc();
  3022. end;
  3023. end
  3024. else
  3025. begin
  3026. current_scanner.ignoredirectives.Add(hs,nil);
  3027. Message1(scan_w_illegal_directive,'$'+hs);
  3028. end;
  3029. { conditionals already read the comment }
  3030. if (current_scanner.comment_level>0) then
  3031. current_scanner.readcomment;
  3032. { we've read the whole comment }
  3033. aktcommentstyle:=comment_none;
  3034. end;
  3035. end;
  3036. procedure tscannerfile.readchar;
  3037. begin
  3038. c:=inputpointer^;
  3039. if c=#0 then
  3040. reload
  3041. else
  3042. inc(inputpointer);
  3043. end;
  3044. procedure tscannerfile.readstring;
  3045. var
  3046. i : longint;
  3047. err : boolean;
  3048. begin
  3049. err:=false;
  3050. i:=0;
  3051. repeat
  3052. case c of
  3053. '_',
  3054. '0'..'9',
  3055. 'A'..'Z' :
  3056. begin
  3057. if i<255 then
  3058. begin
  3059. inc(i);
  3060. orgpattern[i]:=c;
  3061. pattern[i]:=c;
  3062. end
  3063. else
  3064. begin
  3065. if not err then
  3066. begin
  3067. Message(scan_e_string_exceeds_255_chars);
  3068. err:=true;
  3069. end;
  3070. end;
  3071. c:=inputpointer^;
  3072. inc(inputpointer);
  3073. end;
  3074. 'a'..'z' :
  3075. begin
  3076. if i<255 then
  3077. begin
  3078. inc(i);
  3079. orgpattern[i]:=c;
  3080. pattern[i]:=chr(ord(c)-32)
  3081. end
  3082. else
  3083. begin
  3084. if not err then
  3085. begin
  3086. Message(scan_e_string_exceeds_255_chars);
  3087. err:=true;
  3088. end;
  3089. end;
  3090. c:=inputpointer^;
  3091. inc(inputpointer);
  3092. end;
  3093. #0 :
  3094. reload;
  3095. else
  3096. break;
  3097. end;
  3098. until false;
  3099. orgpattern[0]:=chr(i);
  3100. pattern[0]:=chr(i);
  3101. end;
  3102. procedure tscannerfile.readnumber;
  3103. var
  3104. base,
  3105. i : longint;
  3106. begin
  3107. case c of
  3108. '%' :
  3109. begin
  3110. readchar;
  3111. base:=2;
  3112. pattern[1]:='%';
  3113. i:=1;
  3114. end;
  3115. '&' :
  3116. begin
  3117. readchar;
  3118. base:=8;
  3119. pattern[1]:='&';
  3120. i:=1;
  3121. end;
  3122. '$' :
  3123. begin
  3124. readchar;
  3125. base:=16;
  3126. pattern[1]:='$';
  3127. i:=1;
  3128. end;
  3129. else
  3130. begin
  3131. base:=10;
  3132. i:=0;
  3133. end;
  3134. end;
  3135. while ((base>=10) and (c in ['0'..'9'])) or
  3136. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  3137. ((base=8) and (c in ['0'..'7'])) or
  3138. ((base=2) and (c in ['0'..'1'])) do
  3139. begin
  3140. if i<255 then
  3141. begin
  3142. inc(i);
  3143. pattern[i]:=c;
  3144. end;
  3145. readchar;
  3146. end;
  3147. pattern[0]:=chr(i);
  3148. end;
  3149. function tscannerfile.readid:string;
  3150. begin
  3151. readstring;
  3152. readid:=pattern;
  3153. end;
  3154. function tscannerfile.readval:longint;
  3155. var
  3156. l : longint;
  3157. w : integer;
  3158. begin
  3159. readnumber;
  3160. val(pattern,l,w);
  3161. readval:=l;
  3162. end;
  3163. function tscannerfile.readval_asstring:string;
  3164. begin
  3165. readnumber;
  3166. readval_asstring:=pattern;
  3167. end;
  3168. function tscannerfile.readcomment:string;
  3169. var
  3170. i : longint;
  3171. begin
  3172. i:=0;
  3173. repeat
  3174. case c of
  3175. '{' :
  3176. begin
  3177. if aktcommentstyle=comment_tp then
  3178. inc_comment_level;
  3179. end;
  3180. '}' :
  3181. begin
  3182. if aktcommentstyle=comment_tp then
  3183. begin
  3184. readchar;
  3185. dec_comment_level;
  3186. if comment_level=0 then
  3187. break
  3188. else
  3189. continue;
  3190. end;
  3191. end;
  3192. '*' :
  3193. begin
  3194. if aktcommentstyle=comment_oldtp then
  3195. begin
  3196. readchar;
  3197. if c=')' then
  3198. begin
  3199. readchar;
  3200. dec_comment_level;
  3201. break;
  3202. end
  3203. else
  3204. { Add both characters !!}
  3205. if (i<255) then
  3206. begin
  3207. inc(i);
  3208. readcomment[i]:='*';
  3209. if (i<255) then
  3210. begin
  3211. inc(i);
  3212. readcomment[i]:=c;
  3213. end;
  3214. end;
  3215. end
  3216. else
  3217. { Not old TP comment, so add...}
  3218. begin
  3219. if (i<255) then
  3220. begin
  3221. inc(i);
  3222. readcomment[i]:='*';
  3223. end;
  3224. end;
  3225. end;
  3226. #10,#13 :
  3227. linebreak;
  3228. #26 :
  3229. end_of_file;
  3230. else
  3231. begin
  3232. if (i<255) then
  3233. begin
  3234. inc(i);
  3235. readcomment[i]:=c;
  3236. end;
  3237. end;
  3238. end;
  3239. readchar;
  3240. until false;
  3241. readcomment[0]:=chr(i);
  3242. end;
  3243. function tscannerfile.readquotedstring:string;
  3244. var
  3245. i : longint;
  3246. msgwritten : boolean;
  3247. begin
  3248. i:=0;
  3249. msgwritten:=false;
  3250. if (c='''') then
  3251. begin
  3252. repeat
  3253. readchar;
  3254. case c of
  3255. #26 :
  3256. end_of_file;
  3257. #10,#13 :
  3258. Message(scan_f_string_exceeds_line);
  3259. '''' :
  3260. begin
  3261. readchar;
  3262. if c<>'''' then
  3263. break;
  3264. end;
  3265. end;
  3266. if i<255 then
  3267. begin
  3268. inc(i);
  3269. result[i]:=c;
  3270. end
  3271. else
  3272. begin
  3273. if not msgwritten then
  3274. begin
  3275. Message(scan_e_string_exceeds_255_chars);
  3276. msgwritten:=true;
  3277. end;
  3278. end;
  3279. until false;
  3280. end;
  3281. result[0]:=chr(i);
  3282. end;
  3283. function tscannerfile.readstate:char;
  3284. var
  3285. state : char;
  3286. begin
  3287. state:=' ';
  3288. if c=' ' then
  3289. begin
  3290. current_scanner.skipspace;
  3291. current_scanner.readid;
  3292. if pattern='ON' then
  3293. state:='+'
  3294. else
  3295. if pattern='OFF' then
  3296. state:='-';
  3297. end
  3298. else
  3299. state:=c;
  3300. if not (state in ['+','-']) then
  3301. Message(scan_e_wrong_switch_toggle);
  3302. readstate:=state;
  3303. end;
  3304. function tscannerfile.readstatedefault:char;
  3305. var
  3306. state : char;
  3307. begin
  3308. state:=' ';
  3309. if c=' ' then
  3310. begin
  3311. current_scanner.skipspace;
  3312. current_scanner.readid;
  3313. if pattern='ON' then
  3314. state:='+'
  3315. else
  3316. if pattern='OFF' then
  3317. state:='-'
  3318. else
  3319. if pattern='DEFAULT' then
  3320. state:='*';
  3321. end
  3322. else
  3323. state:=c;
  3324. if not (state in ['+','-','*']) then
  3325. Message(scan_e_wrong_switch_toggle_default);
  3326. readstatedefault:=state;
  3327. end;
  3328. procedure tscannerfile.skipspace;
  3329. begin
  3330. repeat
  3331. case c of
  3332. #26 :
  3333. begin
  3334. reload;
  3335. if (c=#26) and not assigned(inputfile.next) then
  3336. break;
  3337. continue;
  3338. end;
  3339. #10,
  3340. #13 :
  3341. linebreak;
  3342. #9,#11,#12,' ' :
  3343. ;
  3344. else
  3345. break;
  3346. end;
  3347. readchar;
  3348. until false;
  3349. end;
  3350. procedure tscannerfile.skipuntildirective;
  3351. var
  3352. found : longint;
  3353. next_char_loaded : boolean;
  3354. begin
  3355. found:=0;
  3356. next_char_loaded:=false;
  3357. repeat
  3358. case c of
  3359. #10,
  3360. #13 :
  3361. linebreak;
  3362. #26 :
  3363. begin
  3364. reload;
  3365. if (c=#26) and not assigned(inputfile.next) then
  3366. end_of_file;
  3367. continue;
  3368. end;
  3369. '{' :
  3370. begin
  3371. if (aktcommentstyle in [comment_tp,comment_none]) then
  3372. begin
  3373. aktcommentstyle:=comment_tp;
  3374. if (comment_level=0) then
  3375. found:=1;
  3376. inc_comment_level;
  3377. end;
  3378. end;
  3379. '*' :
  3380. begin
  3381. if (aktcommentstyle=comment_oldtp) then
  3382. begin
  3383. readchar;
  3384. if c=')' then
  3385. begin
  3386. dec_comment_level;
  3387. found:=0;
  3388. aktcommentstyle:=comment_none;
  3389. end
  3390. else
  3391. next_char_loaded:=true;
  3392. end
  3393. else
  3394. found := 0;
  3395. end;
  3396. '}' :
  3397. begin
  3398. if (aktcommentstyle=comment_tp) then
  3399. begin
  3400. dec_comment_level;
  3401. if (comment_level=0) then
  3402. aktcommentstyle:=comment_none;
  3403. found:=0;
  3404. end;
  3405. end;
  3406. '$' :
  3407. begin
  3408. if found=1 then
  3409. found:=2;
  3410. end;
  3411. '''' :
  3412. if (aktcommentstyle=comment_none) then
  3413. begin
  3414. repeat
  3415. readchar;
  3416. case c of
  3417. #26 :
  3418. end_of_file;
  3419. #10,#13 :
  3420. break;
  3421. '''' :
  3422. begin
  3423. readchar;
  3424. if c<>'''' then
  3425. begin
  3426. next_char_loaded:=true;
  3427. break;
  3428. end;
  3429. end;
  3430. end;
  3431. until false;
  3432. end;
  3433. '(' :
  3434. begin
  3435. if (aktcommentstyle=comment_none) then
  3436. begin
  3437. readchar;
  3438. if c='*' then
  3439. begin
  3440. readchar;
  3441. if c='$' then
  3442. begin
  3443. found:=2;
  3444. inc_comment_level;
  3445. aktcommentstyle:=comment_oldtp;
  3446. end
  3447. else
  3448. begin
  3449. skipoldtpcomment;
  3450. next_char_loaded:=true;
  3451. end;
  3452. end
  3453. else
  3454. next_char_loaded:=true;
  3455. end
  3456. else
  3457. found:=0;
  3458. end;
  3459. '/' :
  3460. begin
  3461. if (aktcommentstyle=comment_none) then
  3462. begin
  3463. readchar;
  3464. if c='/' then
  3465. skipdelphicomment;
  3466. next_char_loaded:=true;
  3467. end
  3468. else
  3469. found:=0;
  3470. end;
  3471. else
  3472. found:=0;
  3473. end;
  3474. if next_char_loaded then
  3475. next_char_loaded:=false
  3476. else
  3477. readchar;
  3478. until (found=2);
  3479. end;
  3480. {****************************************************************************
  3481. Comment Handling
  3482. ****************************************************************************}
  3483. procedure tscannerfile.skipcomment;
  3484. begin
  3485. aktcommentstyle:=comment_tp;
  3486. readchar;
  3487. inc_comment_level;
  3488. { handle compiler switches }
  3489. if (c='$') then
  3490. handledirectives;
  3491. { handle_switches can dec comment_level, }
  3492. while (comment_level>0) do
  3493. begin
  3494. case c of
  3495. '{' :
  3496. inc_comment_level;
  3497. '}' :
  3498. dec_comment_level;
  3499. #10,#13 :
  3500. linebreak;
  3501. #26 :
  3502. begin
  3503. reload;
  3504. if (c=#26) and not assigned(inputfile.next) then
  3505. end_of_file;
  3506. continue;
  3507. end;
  3508. end;
  3509. readchar;
  3510. end;
  3511. aktcommentstyle:=comment_none;
  3512. end;
  3513. procedure tscannerfile.skipdelphicomment;
  3514. begin
  3515. aktcommentstyle:=comment_delphi;
  3516. inc_comment_level;
  3517. readchar;
  3518. { this is not supported }
  3519. if c='$' then
  3520. Message(scan_w_wrong_styled_switch);
  3521. { skip comment }
  3522. while not (c in [#10,#13,#26]) do
  3523. readchar;
  3524. dec_comment_level;
  3525. aktcommentstyle:=comment_none;
  3526. end;
  3527. procedure tscannerfile.skipoldtpcomment;
  3528. var
  3529. found : longint;
  3530. begin
  3531. aktcommentstyle:=comment_oldtp;
  3532. inc_comment_level;
  3533. { only load a char if last already processed,
  3534. was cause of bug1634 PM }
  3535. if c=#0 then
  3536. readchar;
  3537. { this is now supported }
  3538. if (c='$') then
  3539. handledirectives;
  3540. { skip comment }
  3541. while (comment_level>0) do
  3542. begin
  3543. found:=0;
  3544. repeat
  3545. case c of
  3546. #26 :
  3547. begin
  3548. reload;
  3549. if (c=#26) and not assigned(inputfile.next) then
  3550. end_of_file;
  3551. continue;
  3552. end;
  3553. #10,#13 :
  3554. begin
  3555. if found=4 then
  3556. inc_comment_level;
  3557. linebreak;
  3558. found:=0;
  3559. end;
  3560. '*' :
  3561. begin
  3562. if found=3 then
  3563. found:=4
  3564. else
  3565. found:=1;
  3566. end;
  3567. ')' :
  3568. begin
  3569. if found in [1,4] then
  3570. begin
  3571. dec_comment_level;
  3572. if comment_level=0 then
  3573. found:=2
  3574. else
  3575. found:=0;
  3576. end
  3577. else
  3578. found:=0;
  3579. end;
  3580. '(' :
  3581. begin
  3582. if found=4 then
  3583. inc_comment_level;
  3584. found:=3;
  3585. end;
  3586. else
  3587. begin
  3588. if found=4 then
  3589. inc_comment_level;
  3590. found:=0;
  3591. end;
  3592. end;
  3593. readchar;
  3594. until (found=2);
  3595. end;
  3596. aktcommentstyle:=comment_none;
  3597. end;
  3598. {****************************************************************************
  3599. Token Scanner
  3600. ****************************************************************************}
  3601. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  3602. var
  3603. code : integer;
  3604. len,
  3605. low,high,mid : longint;
  3606. w : word;
  3607. m : longint;
  3608. mac : tmacro;
  3609. asciinr : string[33];
  3610. iswidestring : boolean;
  3611. label
  3612. exit_label;
  3613. begin
  3614. flushpendingswitchesstate;
  3615. { record tokens? }
  3616. if allowrecordtoken and
  3617. assigned(recordtokenbuf) then
  3618. recordtoken;
  3619. { replay tokens? }
  3620. if assigned(replaytokenbuf) then
  3621. begin
  3622. replaytoken;
  3623. goto exit_label;
  3624. end;
  3625. { was there already a token read, then return that token }
  3626. if nexttoken<>NOTOKEN then
  3627. begin
  3628. setnexttoken;
  3629. goto exit_label;
  3630. end;
  3631. { Skip all spaces and comments }
  3632. repeat
  3633. case c of
  3634. '{' :
  3635. skipcomment;
  3636. #26 :
  3637. begin
  3638. reload;
  3639. if (c=#26) and not assigned(inputfile.next) then
  3640. break;
  3641. end;
  3642. ' ',#9..#13 :
  3643. begin
  3644. {$ifdef PREPROCWRITE}
  3645. if parapreprocess then
  3646. begin
  3647. if c=#10 then
  3648. preprocfile.eolfound:=true
  3649. else
  3650. preprocfile.spacefound:=true;
  3651. end;
  3652. {$endif PREPROCWRITE}
  3653. skipspace;
  3654. end
  3655. else
  3656. break;
  3657. end;
  3658. until false;
  3659. { Save current token position, for EOF its already loaded }
  3660. if c<>#26 then
  3661. gettokenpos;
  3662. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  3663. if c in ['A'..'Z','a'..'z','_'] then
  3664. begin
  3665. readstring;
  3666. token:=_ID;
  3667. idtoken:=_ID;
  3668. { keyword or any other known token,
  3669. pattern is always uppercased }
  3670. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  3671. begin
  3672. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  3673. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  3674. while low<high do
  3675. begin
  3676. mid:=(high+low+1) shr 1;
  3677. if pattern<tokeninfo^[ttoken(mid)].str then
  3678. high:=mid-1
  3679. else
  3680. low:=mid;
  3681. end;
  3682. with tokeninfo^[ttoken(high)] do
  3683. if pattern=str then
  3684. begin
  3685. if (keyword*current_settings.modeswitches)<>[] then
  3686. if op=NOTOKEN then
  3687. token:=ttoken(high)
  3688. else
  3689. token:=op;
  3690. idtoken:=ttoken(high);
  3691. end;
  3692. end;
  3693. { Only process identifiers and not keywords }
  3694. if token=_ID then
  3695. begin
  3696. { this takes some time ... }
  3697. if (cs_support_macro in current_settings.moduleswitches) then
  3698. begin
  3699. mac:=tmacro(search_macro(pattern));
  3700. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  3701. begin
  3702. if yylexcount<max_macro_nesting then
  3703. begin
  3704. mac.is_used:=true;
  3705. inc(yylexcount);
  3706. substitutemacro(pattern,mac.buftext,mac.buflen,
  3707. mac.fileinfo.line,mac.fileinfo.fileindex);
  3708. { handle empty macros }
  3709. if c=#0 then
  3710. reload;
  3711. readtoken(false);
  3712. { that's all folks }
  3713. dec(yylexcount);
  3714. exit;
  3715. end
  3716. else
  3717. Message(scan_w_macro_too_deep);
  3718. end;
  3719. end;
  3720. end;
  3721. { return token }
  3722. goto exit_label;
  3723. end
  3724. else
  3725. begin
  3726. idtoken:=_NOID;
  3727. case c of
  3728. '$' :
  3729. begin
  3730. readnumber;
  3731. token:=_INTCONST;
  3732. goto exit_label;
  3733. end;
  3734. '%' :
  3735. begin
  3736. if not(m_fpc in current_settings.modeswitches) then
  3737. Illegal_Char(c)
  3738. else
  3739. begin
  3740. readnumber;
  3741. token:=_INTCONST;
  3742. goto exit_label;
  3743. end;
  3744. end;
  3745. '&' :
  3746. begin
  3747. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  3748. begin
  3749. readnumber;
  3750. if length(pattern)=1 then
  3751. begin
  3752. readstring;
  3753. token:=_ID;
  3754. idtoken:=_ID;
  3755. end
  3756. else
  3757. token:=_INTCONST;
  3758. goto exit_label;
  3759. end
  3760. else if m_mac in current_settings.modeswitches then
  3761. begin
  3762. readchar;
  3763. token:=_AMPERSAND;
  3764. goto exit_label;
  3765. end
  3766. else
  3767. Illegal_Char(c);
  3768. end;
  3769. '0'..'9' :
  3770. begin
  3771. readnumber;
  3772. if (c in ['.','e','E']) then
  3773. begin
  3774. { first check for a . }
  3775. if c='.' then
  3776. begin
  3777. cachenexttokenpos;
  3778. readchar;
  3779. { is it a .. from a range? }
  3780. case c of
  3781. '.' :
  3782. begin
  3783. readchar;
  3784. token:=_INTCONST;
  3785. nexttoken:=_POINTPOINT;
  3786. goto exit_label;
  3787. end;
  3788. ')' :
  3789. begin
  3790. readchar;
  3791. token:=_INTCONST;
  3792. nexttoken:=_RECKKLAMMER;
  3793. goto exit_label;
  3794. end;
  3795. '0'..'9' :
  3796. begin
  3797. { insert the number after the . }
  3798. pattern:=pattern+'.';
  3799. while c in ['0'..'9'] do
  3800. begin
  3801. pattern:=pattern+c;
  3802. readchar;
  3803. end;
  3804. end;
  3805. else
  3806. begin
  3807. token:=_INTCONST;
  3808. nexttoken:=_POINT;
  3809. goto exit_label;
  3810. end;
  3811. end;
  3812. end;
  3813. { E can also follow after a point is scanned }
  3814. if c in ['e','E'] then
  3815. begin
  3816. pattern:=pattern+'E';
  3817. readchar;
  3818. if c in ['-','+'] then
  3819. begin
  3820. pattern:=pattern+c;
  3821. readchar;
  3822. end;
  3823. if not(c in ['0'..'9']) then
  3824. Illegal_Char(c);
  3825. while c in ['0'..'9'] do
  3826. begin
  3827. pattern:=pattern+c;
  3828. readchar;
  3829. end;
  3830. end;
  3831. token:=_REALNUMBER;
  3832. goto exit_label;
  3833. end;
  3834. token:=_INTCONST;
  3835. goto exit_label;
  3836. end;
  3837. ';' :
  3838. begin
  3839. readchar;
  3840. token:=_SEMICOLON;
  3841. goto exit_label;
  3842. end;
  3843. '[' :
  3844. begin
  3845. readchar;
  3846. token:=_LECKKLAMMER;
  3847. goto exit_label;
  3848. end;
  3849. ']' :
  3850. begin
  3851. readchar;
  3852. token:=_RECKKLAMMER;
  3853. goto exit_label;
  3854. end;
  3855. '(' :
  3856. begin
  3857. readchar;
  3858. case c of
  3859. '*' :
  3860. begin
  3861. c:=#0;{Signal skipoldtpcomment to reload a char }
  3862. skipoldtpcomment;
  3863. readtoken(false);
  3864. exit;
  3865. end;
  3866. '.' :
  3867. begin
  3868. readchar;
  3869. token:=_LECKKLAMMER;
  3870. goto exit_label;
  3871. end;
  3872. end;
  3873. token:=_LKLAMMER;
  3874. goto exit_label;
  3875. end;
  3876. ')' :
  3877. begin
  3878. readchar;
  3879. token:=_RKLAMMER;
  3880. goto exit_label;
  3881. end;
  3882. '+' :
  3883. begin
  3884. readchar;
  3885. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3886. begin
  3887. readchar;
  3888. token:=_PLUSASN;
  3889. goto exit_label;
  3890. end;
  3891. token:=_PLUS;
  3892. goto exit_label;
  3893. end;
  3894. '-' :
  3895. begin
  3896. readchar;
  3897. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3898. begin
  3899. readchar;
  3900. token:=_MINUSASN;
  3901. goto exit_label;
  3902. end;
  3903. token:=_MINUS;
  3904. goto exit_label;
  3905. end;
  3906. ':' :
  3907. begin
  3908. readchar;
  3909. if c='=' then
  3910. begin
  3911. readchar;
  3912. token:=_ASSIGNMENT;
  3913. goto exit_label;
  3914. end;
  3915. token:=_COLON;
  3916. goto exit_label;
  3917. end;
  3918. '*' :
  3919. begin
  3920. readchar;
  3921. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3922. begin
  3923. readchar;
  3924. token:=_STARASN;
  3925. end
  3926. else
  3927. if c='*' then
  3928. begin
  3929. readchar;
  3930. token:=_STARSTAR;
  3931. end
  3932. else
  3933. token:=_STAR;
  3934. goto exit_label;
  3935. end;
  3936. '/' :
  3937. begin
  3938. readchar;
  3939. case c of
  3940. '=' :
  3941. begin
  3942. if (cs_support_c_operators in current_settings.moduleswitches) then
  3943. begin
  3944. readchar;
  3945. token:=_SLASHASN;
  3946. goto exit_label;
  3947. end;
  3948. end;
  3949. '/' :
  3950. begin
  3951. skipdelphicomment;
  3952. readtoken(false);
  3953. exit;
  3954. end;
  3955. end;
  3956. token:=_SLASH;
  3957. goto exit_label;
  3958. end;
  3959. '|' :
  3960. if m_mac in current_settings.modeswitches then
  3961. begin
  3962. readchar;
  3963. token:=_PIPE;
  3964. goto exit_label;
  3965. end
  3966. else
  3967. Illegal_Char(c);
  3968. '=' :
  3969. begin
  3970. readchar;
  3971. token:=_EQ;
  3972. goto exit_label;
  3973. end;
  3974. '.' :
  3975. begin
  3976. readchar;
  3977. case c of
  3978. '.' :
  3979. begin
  3980. readchar;
  3981. case c of
  3982. '.' :
  3983. begin
  3984. readchar;
  3985. token:=_POINTPOINTPOINT;
  3986. goto exit_label;
  3987. end;
  3988. else
  3989. begin
  3990. token:=_POINTPOINT;
  3991. goto exit_label;
  3992. end;
  3993. end;
  3994. end;
  3995. ')' :
  3996. begin
  3997. readchar;
  3998. token:=_RECKKLAMMER;
  3999. goto exit_label;
  4000. end;
  4001. end;
  4002. token:=_POINT;
  4003. goto exit_label;
  4004. end;
  4005. '@' :
  4006. begin
  4007. readchar;
  4008. token:=_KLAMMERAFFE;
  4009. goto exit_label;
  4010. end;
  4011. ',' :
  4012. begin
  4013. readchar;
  4014. token:=_COMMA;
  4015. goto exit_label;
  4016. end;
  4017. '''','#','^' :
  4018. begin
  4019. len:=0;
  4020. cstringpattern:='';
  4021. iswidestring:=false;
  4022. if c='^' then
  4023. begin
  4024. readchar;
  4025. c:=upcase(c);
  4026. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  4027. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  4028. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  4029. begin
  4030. token:=_CARET;
  4031. goto exit_label;
  4032. end
  4033. else
  4034. begin
  4035. inc(len);
  4036. setlength(cstringpattern,256);
  4037. if c<#64 then
  4038. cstringpattern[len]:=chr(ord(c)+64)
  4039. else
  4040. cstringpattern[len]:=chr(ord(c)-64);
  4041. readchar;
  4042. end;
  4043. end;
  4044. repeat
  4045. case c of
  4046. '#' :
  4047. begin
  4048. readchar; { read # }
  4049. case c of
  4050. '$':
  4051. begin
  4052. readchar; { read leading $ }
  4053. asciinr:='$';
  4054. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=5) do
  4055. begin
  4056. asciinr:=asciinr+c;
  4057. readchar;
  4058. end;
  4059. end;
  4060. '&':
  4061. begin
  4062. readchar; { read leading $ }
  4063. asciinr:='&';
  4064. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=7) do
  4065. begin
  4066. asciinr:=asciinr+c;
  4067. readchar;
  4068. end;
  4069. end;
  4070. '%':
  4071. begin
  4072. readchar; { read leading $ }
  4073. asciinr:='%';
  4074. while (upcase(c) in ['0','1']) and (length(asciinr)<=17) do
  4075. begin
  4076. asciinr:=asciinr+c;
  4077. readchar;
  4078. end;
  4079. end;
  4080. else
  4081. begin
  4082. asciinr:='';
  4083. while (c in ['0'..'9']) and (length(asciinr)<=5) do
  4084. begin
  4085. asciinr:=asciinr+c;
  4086. readchar;
  4087. end;
  4088. end;
  4089. end;
  4090. val(asciinr,m,code);
  4091. if (asciinr='') or (code<>0) then
  4092. Message(scan_e_illegal_char_const)
  4093. else if (m<0) or (m>255) or (length(asciinr)>3) then
  4094. begin
  4095. if (m>=0) and (m<=65535) then
  4096. begin
  4097. if not iswidestring then
  4098. begin
  4099. if len>0 then
  4100. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4101. else
  4102. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4103. iswidestring:=true;
  4104. len:=0;
  4105. end;
  4106. concatwidestringchar(patternw,tcompilerwidechar(m));
  4107. end
  4108. else
  4109. Message(scan_e_illegal_char_const)
  4110. end
  4111. else if iswidestring then
  4112. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  4113. else
  4114. begin
  4115. if len>=length(cstringpattern) then
  4116. setlength(cstringpattern,length(cstringpattern)+256);
  4117. inc(len);
  4118. cstringpattern[len]:=chr(m);
  4119. end;
  4120. end;
  4121. '''' :
  4122. begin
  4123. repeat
  4124. readchar;
  4125. case c of
  4126. #26 :
  4127. end_of_file;
  4128. #10,#13 :
  4129. Message(scan_f_string_exceeds_line);
  4130. '''' :
  4131. begin
  4132. readchar;
  4133. if c<>'''' then
  4134. break;
  4135. end;
  4136. end;
  4137. { interpret as utf-8 string? }
  4138. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  4139. begin
  4140. { convert existing string to an utf-8 string }
  4141. if not iswidestring then
  4142. begin
  4143. if len>0 then
  4144. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4145. else
  4146. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4147. iswidestring:=true;
  4148. len:=0;
  4149. end;
  4150. { four or more chars aren't handled }
  4151. if (ord(c) and $f0)=$f0 then
  4152. message(scan_e_utf8_bigger_than_65535)
  4153. { three chars }
  4154. else if (ord(c) and $e0)=$e0 then
  4155. begin
  4156. w:=ord(c) and $f;
  4157. readchar;
  4158. if (ord(c) and $c0)<>$80 then
  4159. message(scan_e_utf8_malformed);
  4160. w:=(w shl 6) or (ord(c) and $3f);
  4161. readchar;
  4162. if (ord(c) and $c0)<>$80 then
  4163. message(scan_e_utf8_malformed);
  4164. w:=(w shl 6) or (ord(c) and $3f);
  4165. concatwidestringchar(patternw,w);
  4166. end
  4167. { two chars }
  4168. else if (ord(c) and $c0)<>0 then
  4169. begin
  4170. w:=ord(c) and $1f;
  4171. readchar;
  4172. if (ord(c) and $c0)<>$80 then
  4173. message(scan_e_utf8_malformed);
  4174. w:=(w shl 6) or (ord(c) and $3f);
  4175. concatwidestringchar(patternw,w);
  4176. end
  4177. { illegal }
  4178. else if (ord(c) and $80)<>0 then
  4179. message(scan_e_utf8_malformed)
  4180. else
  4181. concatwidestringchar(patternw,tcompilerwidechar(c))
  4182. end
  4183. else if iswidestring then
  4184. begin
  4185. if current_settings.sourcecodepage=CP_UTF8 then
  4186. concatwidestringchar(patternw,ord(c))
  4187. else
  4188. concatwidestringchar(patternw,asciichar2unicode(c))
  4189. end
  4190. else
  4191. begin
  4192. if len>=length(cstringpattern) then
  4193. setlength(cstringpattern,length(cstringpattern)+256);
  4194. inc(len);
  4195. cstringpattern[len]:=c;
  4196. end;
  4197. until false;
  4198. end;
  4199. '^' :
  4200. begin
  4201. readchar;
  4202. c:=upcase(c);
  4203. if c<#64 then
  4204. c:=chr(ord(c)+64)
  4205. else
  4206. c:=chr(ord(c)-64);
  4207. if iswidestring then
  4208. concatwidestringchar(patternw,asciichar2unicode(c))
  4209. else
  4210. begin
  4211. if len>=length(cstringpattern) then
  4212. setlength(cstringpattern,length(cstringpattern)+256);
  4213. inc(len);
  4214. cstringpattern[len]:=c;
  4215. end;
  4216. readchar;
  4217. end;
  4218. else
  4219. break;
  4220. end;
  4221. until false;
  4222. { strings with length 1 become const chars }
  4223. if iswidestring then
  4224. begin
  4225. if patternw^.len=1 then
  4226. token:=_CWCHAR
  4227. else
  4228. token:=_CWSTRING;
  4229. end
  4230. else
  4231. begin
  4232. setlength(cstringpattern,len);
  4233. if length(cstringpattern)=1 then
  4234. begin
  4235. token:=_CCHAR;
  4236. pattern:=cstringpattern;
  4237. end
  4238. else
  4239. token:=_CSTRING;
  4240. end;
  4241. goto exit_label;
  4242. end;
  4243. '>' :
  4244. begin
  4245. readchar;
  4246. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4247. token:=_RSHARPBRACKET
  4248. else
  4249. begin
  4250. case c of
  4251. '=' :
  4252. begin
  4253. readchar;
  4254. token:=_GTE;
  4255. goto exit_label;
  4256. end;
  4257. '>' :
  4258. begin
  4259. readchar;
  4260. token:=_OP_SHR;
  4261. goto exit_label;
  4262. end;
  4263. '<' :
  4264. begin { >< is for a symetric diff for sets }
  4265. readchar;
  4266. token:=_SYMDIF;
  4267. goto exit_label;
  4268. end;
  4269. end;
  4270. token:=_GT;
  4271. end;
  4272. goto exit_label;
  4273. end;
  4274. '<' :
  4275. begin
  4276. readchar;
  4277. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4278. token:=_LSHARPBRACKET
  4279. else
  4280. begin
  4281. case c of
  4282. '>' :
  4283. begin
  4284. readchar;
  4285. token:=_NE;
  4286. goto exit_label;
  4287. end;
  4288. '=' :
  4289. begin
  4290. readchar;
  4291. token:=_LTE;
  4292. goto exit_label;
  4293. end;
  4294. '<' :
  4295. begin
  4296. readchar;
  4297. token:=_OP_SHL;
  4298. goto exit_label;
  4299. end;
  4300. end;
  4301. token:=_LT;
  4302. end;
  4303. goto exit_label;
  4304. end;
  4305. #26 :
  4306. begin
  4307. token:=_EOF;
  4308. checkpreprocstack;
  4309. goto exit_label;
  4310. end;
  4311. else
  4312. Illegal_Char(c);
  4313. end;
  4314. end;
  4315. exit_label:
  4316. lasttoken:=token;
  4317. end;
  4318. function tscannerfile.readpreproc:ttoken;
  4319. begin
  4320. skipspace;
  4321. case c of
  4322. '_',
  4323. 'A'..'Z',
  4324. 'a'..'z' :
  4325. begin
  4326. current_scanner.preproc_pattern:=readid;
  4327. readpreproc:=_ID;
  4328. end;
  4329. '0'..'9' :
  4330. begin
  4331. current_scanner.preproc_pattern:=readval_asstring;
  4332. { realnumber? }
  4333. if c='.' then
  4334. begin
  4335. readchar;
  4336. while c in ['0'..'9'] do
  4337. begin
  4338. current_scanner.preproc_pattern:=current_scanner.preproc_pattern+c;
  4339. readchar;
  4340. end;
  4341. end;
  4342. readpreproc:=_ID;
  4343. end;
  4344. '$','%','&' :
  4345. begin
  4346. current_scanner.preproc_pattern:=readval_asstring;
  4347. readpreproc:=_ID;
  4348. end;
  4349. ',' :
  4350. begin
  4351. readchar;
  4352. readpreproc:=_COMMA;
  4353. end;
  4354. '}' :
  4355. begin
  4356. readpreproc:=_END;
  4357. end;
  4358. '(' :
  4359. begin
  4360. readchar;
  4361. readpreproc:=_LKLAMMER;
  4362. end;
  4363. ')' :
  4364. begin
  4365. readchar;
  4366. readpreproc:=_RKLAMMER;
  4367. end;
  4368. '[' :
  4369. begin
  4370. readchar;
  4371. readpreproc:=_LECKKLAMMER;
  4372. end;
  4373. ']' :
  4374. begin
  4375. readchar;
  4376. readpreproc:=_RECKKLAMMER;
  4377. end;
  4378. '+' :
  4379. begin
  4380. readchar;
  4381. readpreproc:=_PLUS;
  4382. end;
  4383. '-' :
  4384. begin
  4385. readchar;
  4386. readpreproc:=_MINUS;
  4387. end;
  4388. '*' :
  4389. begin
  4390. readchar;
  4391. readpreproc:=_STAR;
  4392. end;
  4393. '/' :
  4394. begin
  4395. readchar;
  4396. readpreproc:=_SLASH;
  4397. end;
  4398. '=' :
  4399. begin
  4400. readchar;
  4401. readpreproc:=_EQ;
  4402. end;
  4403. '>' :
  4404. begin
  4405. readchar;
  4406. if c='=' then
  4407. begin
  4408. readchar;
  4409. readpreproc:=_GTE;
  4410. end
  4411. else
  4412. readpreproc:=_GT;
  4413. end;
  4414. '<' :
  4415. begin
  4416. readchar;
  4417. case c of
  4418. '>' :
  4419. begin
  4420. readchar;
  4421. readpreproc:=_NE;
  4422. end;
  4423. '=' :
  4424. begin
  4425. readchar;
  4426. readpreproc:=_LTE;
  4427. end;
  4428. else
  4429. readpreproc:=_LT;
  4430. end;
  4431. end;
  4432. #26 :
  4433. begin
  4434. readpreproc:=_EOF;
  4435. checkpreprocstack;
  4436. end;
  4437. else
  4438. Illegal_Char(c);
  4439. end;
  4440. end;
  4441. function tscannerfile.asmgetcharstart : char;
  4442. begin
  4443. { return first the character already
  4444. available in c }
  4445. lastasmgetchar:=c;
  4446. result:=asmgetchar;
  4447. end;
  4448. function tscannerfile.asmgetchar : char;
  4449. begin
  4450. if lastasmgetchar<>#0 then
  4451. begin
  4452. c:=lastasmgetchar;
  4453. lastasmgetchar:=#0;
  4454. end
  4455. else
  4456. readchar;
  4457. if in_asm_string then
  4458. begin
  4459. asmgetchar:=c;
  4460. exit;
  4461. end;
  4462. repeat
  4463. case c of
  4464. // the { ... } is used in ARM assembler to define register sets, so we can't used
  4465. // it as comment, either (* ... *), /* ... */ or // ... should be used instead.
  4466. // But compiler directives {$...} are allowed in ARM assembler.
  4467. '{' :
  4468. begin
  4469. {$ifdef arm}
  4470. readchar;
  4471. dec(inputpointer);
  4472. if c<>'$' then
  4473. begin
  4474. asmgetchar:='{';
  4475. exit;
  4476. end
  4477. else
  4478. {$endif arm}
  4479. skipcomment;
  4480. end;
  4481. #10,#13 :
  4482. begin
  4483. linebreak;
  4484. asmgetchar:=c;
  4485. exit;
  4486. end;
  4487. #26 :
  4488. begin
  4489. reload;
  4490. if (c=#26) and not assigned(inputfile.next) then
  4491. end_of_file;
  4492. continue;
  4493. end;
  4494. '/' :
  4495. begin
  4496. readchar;
  4497. if c='/' then
  4498. skipdelphicomment
  4499. else
  4500. begin
  4501. asmgetchar:='/';
  4502. lastasmgetchar:=c;
  4503. exit;
  4504. end;
  4505. end;
  4506. '(' :
  4507. begin
  4508. readchar;
  4509. if c='*' then
  4510. begin
  4511. c:=#0;{Signal skipoldtpcomment to reload a char }
  4512. skipoldtpcomment;
  4513. end
  4514. else
  4515. begin
  4516. asmgetchar:='(';
  4517. lastasmgetchar:=c;
  4518. exit;
  4519. end;
  4520. end;
  4521. else
  4522. begin
  4523. asmgetchar:=c;
  4524. exit;
  4525. end;
  4526. end;
  4527. until false;
  4528. end;
  4529. {*****************************************************************************
  4530. Helpers
  4531. *****************************************************************************}
  4532. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  4533. begin
  4534. if dm in [directive_all, directive_turbo] then
  4535. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  4536. if dm in [directive_all, directive_mac] then
  4537. tdirectiveitem.create(mac_scannerdirectives,s,p);
  4538. end;
  4539. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  4540. begin
  4541. if dm in [directive_all, directive_turbo] then
  4542. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  4543. if dm in [directive_all, directive_mac] then
  4544. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  4545. end;
  4546. {*****************************************************************************
  4547. Initialization
  4548. *****************************************************************************}
  4549. procedure InitScanner;
  4550. begin
  4551. InitWideString(patternw);
  4552. turbo_scannerdirectives:=TFPHashObjectList.Create;
  4553. mac_scannerdirectives:=TFPHashObjectList.Create;
  4554. { Common directives and conditionals }
  4555. AddDirective('I',directive_all, @dir_include);
  4556. AddDirective('DEFINE',directive_all, @dir_define);
  4557. AddDirective('UNDEF',directive_all, @dir_undef);
  4558. AddConditional('IF',directive_all, @dir_if);
  4559. AddConditional('IFDEF',directive_all, @dir_ifdef);
  4560. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  4561. AddConditional('ELSE',directive_all, @dir_else);
  4562. AddConditional('ELSEIF',directive_all, @dir_elseif);
  4563. AddConditional('ENDIF',directive_all, @dir_endif);
  4564. { Directives and conditionals for all modes except mode macpas}
  4565. AddDirective('INCLUDE',directive_turbo, @dir_include);
  4566. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  4567. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  4568. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  4569. AddConditional('IFEND',directive_turbo, @dir_endif);
  4570. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  4571. { Directives and conditionals for mode macpas: }
  4572. AddDirective('SETC',directive_mac, @dir_setc);
  4573. AddDirective('DEFINEC',directive_mac, @dir_definec);
  4574. AddDirective('UNDEFC',directive_mac, @dir_undef);
  4575. AddConditional('IFC',directive_mac, @dir_if);
  4576. AddConditional('ELSEC',directive_mac, @dir_else);
  4577. AddConditional('ELIFC',directive_mac, @dir_elseif);
  4578. AddConditional('ENDC',directive_mac, @dir_endif);
  4579. end;
  4580. procedure DoneScanner;
  4581. begin
  4582. turbo_scannerdirectives.Free;
  4583. mac_scannerdirectives.Free;
  4584. DoneWideString(patternw);
  4585. end;
  4586. end.