scanner.pas 136 KB

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