scanner.pas 175 KB

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