scanner.pas 146 KB

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