scanner.pas 134 KB

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