scanner.pas 137 KB

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