scanner.pas 192 KB

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