scanner.pas 186 KB

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