scanner.pas 154 KB

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