scanner.pas 137 KB

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