scanner.pas 184 KB

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