scanner.pas 179 KB

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