scanner.pas 192 KB

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