scanner.pas 136 KB

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