scanner.pas 192 KB

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