scanner.pas 141 KB

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