scanner.pas 161 KB

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