2
0

scanner.pas 196 KB

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