scanner.pas 144 KB

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