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