scanner.pas 145 KB

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