scanner.pas 153 KB

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