scanner.pas 205 KB

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