scanner.pas 215 KB

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