scanner.pas 171 KB

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