scanner.pas 187 KB

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