scanner.pas 153 KB

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