scanner.pas 154 KB

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