scanner.pas 200 KB

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