2
0

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. isdef:=defined_macro(hs);
  370. end;
  371. procedure dir_ifdef;
  372. begin
  373. current_scanner.ifpreprocstack(pp_ifdef,@isdef,scan_c_ifdef_found);
  374. end;
  375. function isnotdef(var valuedescr: String): Boolean;
  376. var
  377. hs : string;
  378. mac : tmacro;
  379. begin
  380. current_scanner.skipspace;
  381. hs:=current_scanner.readid;
  382. valuedescr:= hs;
  383. if hs='' then
  384. Message(scan_e_error_in_preproc_expr);
  385. isnotdef:=not defined_macro(hs);
  386. end;
  387. procedure dir_ifndef;
  388. begin
  389. current_scanner.ifpreprocstack(pp_ifndef,@isnotdef,scan_c_ifndef_found);
  390. end;
  391. function opt_check(var valuedescr: String): Boolean;
  392. var
  393. hs : string;
  394. state : char;
  395. begin
  396. opt_check:= false;
  397. current_scanner.skipspace;
  398. hs:=current_scanner.readid;
  399. valuedescr:= hs;
  400. if (length(hs)>1) then
  401. Message1(scan_w_illegal_switch,hs)
  402. else
  403. begin
  404. state:=current_scanner.ReadState;
  405. if state in ['-','+'] then
  406. opt_check:=CheckSwitch(hs[1],state)
  407. else
  408. Message(scan_e_error_in_preproc_expr);
  409. end;
  410. end;
  411. procedure dir_ifopt;
  412. begin
  413. if localswitcheschanged then
  414. begin
  415. current_settings.localswitches:=nextlocalswitches;
  416. localswitcheschanged:=false;
  417. end;
  418. current_scanner.ifpreprocstack(pp_ifopt,@opt_check,scan_c_ifopt_found);
  419. end;
  420. procedure dir_libprefix;
  421. var
  422. s : string;
  423. begin
  424. current_scanner.skipspace;
  425. if c <> '''' then
  426. Message2(scan_f_syn_expected, '''', c);
  427. s := current_scanner.readquotedstring;
  428. stringdispose(outputprefix);
  429. outputprefix := stringdup(s);
  430. with current_module do
  431. setfilename(paramfn^, paramallowoutput);
  432. end;
  433. procedure dir_libsuffix;
  434. var
  435. s : string;
  436. begin
  437. current_scanner.skipspace;
  438. if c <> '''' then
  439. Message2(scan_f_syn_expected, '''', c);
  440. s := current_scanner.readquotedstring;
  441. stringdispose(outputsuffix);
  442. outputsuffix := stringdup(s);
  443. with current_module do
  444. setfilename(paramfn^, paramallowoutput);
  445. end;
  446. procedure dir_extension;
  447. var
  448. s : string;
  449. begin
  450. current_scanner.skipspace;
  451. if c <> '''' then
  452. Message2(scan_f_syn_expected, '''', c);
  453. s := current_scanner.readquotedstring;
  454. if OutputFileName='' then
  455. OutputFileName:=InputFileName;
  456. OutputFileName:=ChangeFileExt(OutputFileName,'.'+s);
  457. with current_module do
  458. setfilename(paramfn^, paramallowoutput);
  459. end;
  460. {
  461. Compile time expression type check
  462. ----------------------------------
  463. Each subexpression returns its type to the caller, which then can
  464. do type check. Since data types of compile time expressions is
  465. not well defined, the type system does a best effort. The drawback is
  466. that some errors might not be detected.
  467. Instead of returning a particular data type, a set of possible data types
  468. are returned. This way ambigouos types can be handled. For instance a
  469. value of 1 can be both a boolean and and integer.
  470. Booleans
  471. --------
  472. The following forms of boolean values are supported:
  473. * C coded, that is 0 is false, non-zero is true.
  474. * TRUE/FALSE for mac style compile time variables
  475. Thus boolean mac compile time variables are always stored as TRUE/FALSE.
  476. When a compile time expression is evaluated, they are then translated
  477. to C coded booleans (0/1), to simplify for the expression evaluator.
  478. Note that this scheme then also of support mac compile time variables which
  479. are 0/1 but with a boolean meaning.
  480. The TRUE/FALSE format is new from 22 august 2005, but the above scheme
  481. means that units which is not recompiled, and thus stores
  482. compile time variables as the old format (0/1), continue to work.
  483. Short circuit evaluation
  484. ------------------------
  485. For this to work, the part of a compile time expression which is short
  486. circuited, should not be evaluated, while it still should be parsed.
  487. Therefor there is a parameter eval, telling whether evaluation is needed.
  488. In case not, the value returned can be arbitrary.
  489. }
  490. type
  491. {Compile time expression types}
  492. TCTEType = (ctetBoolean, ctetInteger, ctetString, ctetSet);
  493. TCTETypeSet = set of TCTEType;
  494. const
  495. cteTypeNames : array[TCTEType] of string[10] = (
  496. 'BOOLEAN','INTEGER','STRING','SET');
  497. {Subset of types which can be elements in sets.}
  498. setelementdefs = [ctetBoolean, ctetInteger, ctetString];
  499. function GetCTETypeName(t: TCTETypeSet): String;
  500. var
  501. i: TCTEType;
  502. begin
  503. result:= '';
  504. for i:= Low(TCTEType) to High(TCTEType) do
  505. if i in t then
  506. if result = '' then
  507. result:= cteTypeNames[i]
  508. else
  509. result:= result + ' or ' + cteTypeNames[i];
  510. end;
  511. procedure CTEError(actType, desiredExprType: TCTETypeSet; place: String);
  512. begin
  513. Message3(scan_e_compile_time_typeerror,
  514. GetCTETypeName(desiredExprType),
  515. GetCTETypeName(actType),
  516. place
  517. );
  518. end;
  519. function parse_compiler_expr(var compileExprType: TCTETypeSet):string;
  520. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string; forward;
  521. procedure preproc_consume(t : ttoken);
  522. begin
  523. if t<>current_scanner.preproc_token then
  524. Message(scan_e_preproc_syntax_error);
  525. current_scanner.preproc_token:=current_scanner.readpreproc;
  526. end;
  527. function preproc_substitutedtoken(var macroType: TCTETypeSet; eval : Boolean): string;
  528. { Currently this parses identifiers as well as numbers.
  529. The result from this procedure can either be that the token
  530. itself is a value, or that it is a compile time variable/macro,
  531. which then is substituted for another value (for macros
  532. recursivelly substituted).}
  533. var
  534. hs: string;
  535. mac : tmacro;
  536. macrocount,
  537. len : integer;
  538. numres : longint;
  539. w: word;
  540. begin
  541. result := current_scanner.preproc_pattern;
  542. if not eval then
  543. exit;
  544. mac:= nil;
  545. { Substitue macros and compiler variables with their content/value.
  546. For real macros also do recursive substitution. }
  547. macrocount:=0;
  548. repeat
  549. mac:=tmacro(search_macro(result));
  550. inc(macrocount);
  551. if macrocount>max_macro_nesting then
  552. begin
  553. Message(scan_w_macro_too_deep);
  554. break;
  555. end;
  556. if assigned(mac) and mac.defined then
  557. if assigned(mac.buftext) then
  558. begin
  559. if mac.buflen>255 then
  560. begin
  561. len:=255;
  562. Message(scan_w_macro_cut_after_255_chars);
  563. end
  564. else
  565. len:=mac.buflen;
  566. hs[0]:=char(len);
  567. move(mac.buftext^,hs[1],len);
  568. result:=upcase(hs);
  569. mac.is_used:=true;
  570. end
  571. else
  572. begin
  573. Message1(scan_e_error_macro_lacks_value, result);
  574. break;
  575. end
  576. else
  577. begin
  578. break;
  579. end;
  580. if mac.is_compiler_var then
  581. break;
  582. until false;
  583. { At this point, result do contain the value. Do some decoding and
  584. determine the type.}
  585. val(result,numres,w);
  586. if (w=0) then {It is an integer}
  587. begin
  588. if (numres = 0) or (numres = 1) then
  589. macroType := [ctetInteger, ctetBoolean]
  590. else
  591. macroType := [ctetInteger];
  592. end
  593. else if assigned(mac) and (m_mac in current_settings.modeswitches) and (result='FALSE') then
  594. begin
  595. result:= '0';
  596. macroType:= [ctetBoolean];
  597. end
  598. else if assigned(mac) and (m_mac in current_settings.modeswitches) and (result='TRUE') then
  599. begin
  600. result:= '1';
  601. macroType:= [ctetBoolean];
  602. end
  603. else if (m_mac in current_settings.modeswitches) and
  604. (not assigned(mac) or not mac.defined) and
  605. (macrocount = 1) then
  606. begin
  607. {Errors in mode mac is issued here. For non macpas modes there is
  608. more liberty, but the error will eventually be caught at a later stage.}
  609. Message1(scan_e_error_macro_undefined, result);
  610. macroType:= [ctetString]; {Just to have something}
  611. end
  612. else
  613. macroType:= [ctetString];
  614. end;
  615. function read_factor(var factorType: TCTETypeSet; eval : Boolean) : string;
  616. var
  617. hs : string;
  618. mac: tmacro;
  619. srsym : tsym;
  620. srsymtable : TSymtable;
  621. hdef : TDef;
  622. l : longint;
  623. w : integer;
  624. hasKlammer: Boolean;
  625. setElemType : TCTETypeSet;
  626. begin
  627. if current_scanner.preproc_token=_ID then
  628. begin
  629. if current_scanner.preproc_pattern='DEFINED' then
  630. begin
  631. factorType:= [ctetBoolean];
  632. preproc_consume(_ID);
  633. current_scanner.skipspace;
  634. if current_scanner.preproc_token =_LKLAMMER then
  635. begin
  636. preproc_consume(_LKLAMMER);
  637. current_scanner.skipspace;
  638. hasKlammer:= true;
  639. end
  640. else if (m_mac in current_settings.modeswitches) then
  641. hasKlammer:= false
  642. else
  643. Message(scan_e_error_in_preproc_expr);
  644. if current_scanner.preproc_token =_ID then
  645. begin
  646. hs := current_scanner.preproc_pattern;
  647. mac := tmacro(search_macro(hs));
  648. if assigned(mac) and mac.defined then
  649. begin
  650. hs := '1';
  651. mac.is_used:=true;
  652. end
  653. else
  654. hs := '0';
  655. read_factor := hs;
  656. preproc_consume(_ID);
  657. current_scanner.skipspace;
  658. end
  659. else
  660. Message(scan_e_error_in_preproc_expr);
  661. if hasKlammer then
  662. if current_scanner.preproc_token =_RKLAMMER then
  663. preproc_consume(_RKLAMMER)
  664. else
  665. Message(scan_e_error_in_preproc_expr);
  666. end
  667. else
  668. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  669. begin
  670. factorType:= [ctetBoolean];
  671. preproc_consume(_ID);
  672. current_scanner.skipspace;
  673. if current_scanner.preproc_token =_ID then
  674. begin
  675. hs := current_scanner.preproc_pattern;
  676. mac := tmacro(search_macro(hs));
  677. if assigned(mac) then
  678. begin
  679. hs := '0';
  680. mac.is_used:=true;
  681. end
  682. else
  683. hs := '1';
  684. read_factor := hs;
  685. preproc_consume(_ID);
  686. current_scanner.skipspace;
  687. end
  688. else
  689. Message(scan_e_error_in_preproc_expr);
  690. end
  691. else
  692. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  693. begin
  694. factorType:= [ctetBoolean];
  695. preproc_consume(_ID);
  696. current_scanner.skipspace;
  697. if current_scanner.preproc_token =_LKLAMMER then
  698. begin
  699. preproc_consume(_LKLAMMER);
  700. current_scanner.skipspace;
  701. end
  702. else
  703. Message(scan_e_error_in_preproc_expr);
  704. if not (current_scanner.preproc_token = _ID) then
  705. Message(scan_e_error_in_preproc_expr);
  706. hs:=current_scanner.preproc_pattern;
  707. if (length(hs) > 1) then
  708. {This is allowed in Metrowerks Pascal}
  709. Message(scan_e_error_in_preproc_expr)
  710. else
  711. begin
  712. if CheckSwitch(hs[1],'+') then
  713. read_factor := '1'
  714. else
  715. read_factor := '0';
  716. end;
  717. preproc_consume(_ID);
  718. current_scanner.skipspace;
  719. if current_scanner.preproc_token =_RKLAMMER then
  720. preproc_consume(_RKLAMMER)
  721. else
  722. Message(scan_e_error_in_preproc_expr);
  723. end
  724. else
  725. if current_scanner.preproc_pattern='SIZEOF' then
  726. begin
  727. factorType:= [ctetInteger];
  728. preproc_consume(_ID);
  729. current_scanner.skipspace;
  730. if current_scanner.preproc_token =_LKLAMMER then
  731. begin
  732. preproc_consume(_LKLAMMER);
  733. current_scanner.skipspace;
  734. end
  735. else
  736. Message(scan_e_preproc_syntax_error);
  737. if eval then
  738. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  739. begin
  740. l:=0;
  741. case srsym.typ of
  742. staticvarsym,
  743. localvarsym,
  744. paravarsym :
  745. l:=tabstractvarsym(srsym).getsize;
  746. typesym:
  747. l:=ttypesym(srsym).typedef.size;
  748. else
  749. Message(scan_e_error_in_preproc_expr);
  750. end;
  751. str(l,read_factor);
  752. end
  753. else
  754. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  755. preproc_consume(_ID);
  756. current_scanner.skipspace;
  757. if current_scanner.preproc_token =_RKLAMMER then
  758. preproc_consume(_RKLAMMER)
  759. else
  760. Message(scan_e_preproc_syntax_error);
  761. end
  762. else
  763. if current_scanner.preproc_pattern='HIGH' then
  764. begin
  765. factorType:= [ctetInteger];
  766. preproc_consume(_ID);
  767. current_scanner.skipspace;
  768. if current_scanner.preproc_token =_LKLAMMER then
  769. begin
  770. preproc_consume(_LKLAMMER);
  771. current_scanner.skipspace;
  772. end
  773. else
  774. Message(scan_e_preproc_syntax_error);
  775. if eval then
  776. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  777. begin
  778. hdef:=nil;
  779. hs:='';
  780. l:=0;
  781. case srsym.typ of
  782. staticvarsym,
  783. localvarsym,
  784. paravarsym :
  785. hdef:=tabstractvarsym(srsym).vardef;
  786. typesym:
  787. hdef:=ttypesym(srsym).typedef;
  788. else
  789. Message(scan_e_error_in_preproc_expr);
  790. end;
  791. if hdef<>nil then
  792. begin
  793. if hdef.typ=setdef then
  794. hdef:=tsetdef(hdef).elementdef;
  795. case hdef.typ of
  796. orddef:
  797. with torddef(hdef).high do
  798. if signed then
  799. str(svalue,hs)
  800. else
  801. str(uvalue,hs);
  802. enumdef:
  803. l:=tenumdef(hdef).maxval;
  804. arraydef:
  805. if is_open_array(hdef) or is_array_of_const(hdef) or is_dynamic_array(hdef) then
  806. Message(type_e_mismatch)
  807. else
  808. l:=tarraydef(hdef).highrange;
  809. stringdef:
  810. if is_open_string(hdef) or is_ansistring(hdef) or is_widestring(hdef) then
  811. Message(type_e_mismatch)
  812. else
  813. l:=tstringdef(hdef).len;
  814. else
  815. Message(type_e_mismatch);
  816. end;
  817. end;
  818. if hs='' then
  819. str(l,read_factor)
  820. else
  821. read_factor:=hs;
  822. end
  823. else
  824. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  825. preproc_consume(_ID);
  826. current_scanner.skipspace;
  827. if current_scanner.preproc_token =_RKLAMMER then
  828. preproc_consume(_RKLAMMER)
  829. else
  830. Message(scan_e_preproc_syntax_error);
  831. end
  832. else
  833. if current_scanner.preproc_pattern='DECLARED' then
  834. begin
  835. factorType:= [ctetBoolean];
  836. preproc_consume(_ID);
  837. current_scanner.skipspace;
  838. if current_scanner.preproc_token =_LKLAMMER then
  839. begin
  840. preproc_consume(_LKLAMMER);
  841. current_scanner.skipspace;
  842. end
  843. else
  844. Message(scan_e_error_in_preproc_expr);
  845. if current_scanner.preproc_token =_ID then
  846. begin
  847. hs := upper(current_scanner.preproc_pattern);
  848. if searchsym(hs,srsym,srsymtable) then
  849. hs := '1'
  850. else
  851. hs := '0';
  852. read_factor := hs;
  853. preproc_consume(_ID);
  854. current_scanner.skipspace;
  855. end
  856. else
  857. Message(scan_e_error_in_preproc_expr);
  858. if current_scanner.preproc_token =_RKLAMMER then
  859. preproc_consume(_RKLAMMER)
  860. else
  861. Message(scan_e_error_in_preproc_expr);
  862. end
  863. else
  864. if current_scanner.preproc_pattern='NOT' then
  865. begin
  866. factorType:= [ctetBoolean];
  867. preproc_consume(_ID);
  868. hs:=read_factor(factorType, eval);
  869. if eval then
  870. begin
  871. if not (ctetBoolean in factorType) then
  872. CTEError(factorType, [ctetBoolean], 'NOT');
  873. val(hs,l,w);
  874. if l<>0 then
  875. read_factor:='0'
  876. else
  877. read_factor:='1';
  878. end
  879. else
  880. read_factor:='0'; {Just to have something}
  881. end
  882. else
  883. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='TRUE') then
  884. begin
  885. factorType:= [ctetBoolean];
  886. preproc_consume(_ID);
  887. read_factor:='1';
  888. end
  889. else
  890. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='FALSE') then
  891. begin
  892. factorType:= [ctetBoolean];
  893. preproc_consume(_ID);
  894. read_factor:='0';
  895. end
  896. else
  897. begin
  898. hs:=preproc_substitutedtoken(factorType, eval);
  899. { Default is to return the original symbol }
  900. read_factor:=hs;
  901. if eval and ([m_delphi,m_objfpc]*current_settings.modeswitches<>[]) and (ctetString in factorType) then
  902. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  903. begin
  904. case srsym.typ of
  905. constsym :
  906. begin
  907. with tconstsym(srsym) do
  908. begin
  909. case consttyp of
  910. constord :
  911. begin
  912. case constdef.typ of
  913. orddef:
  914. begin
  915. if is_integer(constdef) then
  916. begin
  917. read_factor:=tostr(value.valueord);
  918. factorType:= [ctetInteger];
  919. end
  920. else if is_boolean(constdef) then
  921. begin
  922. read_factor:=tostr(value.valueord);
  923. factorType:= [ctetBoolean];
  924. end
  925. else if is_char(constdef) then
  926. begin
  927. read_factor:=char(qword(value.valueord));
  928. factorType:= [ctetString];
  929. end
  930. end;
  931. enumdef:
  932. begin
  933. read_factor:=tostr(value.valueord);
  934. factorType:= [ctetInteger];
  935. end;
  936. end;
  937. end;
  938. conststring :
  939. begin
  940. read_factor := upper(pchar(value.valueptr));
  941. factorType:= [ctetString];
  942. end;
  943. constset :
  944. begin
  945. hs:=',';
  946. for l:=0 to 255 do
  947. if l in pconstset(tconstsym(srsym).value.valueptr)^ then
  948. hs:=hs+tostr(l)+',';
  949. read_factor := hs;
  950. factorType:= [ctetSet];
  951. end;
  952. end;
  953. end;
  954. end;
  955. enumsym :
  956. begin
  957. read_factor:=tostr(tenumsym(srsym).value);
  958. factorType:= [ctetInteger];
  959. end;
  960. end;
  961. end;
  962. preproc_consume(_ID);
  963. current_scanner.skipspace;
  964. end
  965. end
  966. else if current_scanner.preproc_token =_LKLAMMER then
  967. begin
  968. preproc_consume(_LKLAMMER);
  969. read_factor:=read_expr(factorType, eval);
  970. preproc_consume(_RKLAMMER);
  971. end
  972. else if current_scanner.preproc_token = _LECKKLAMMER then
  973. begin
  974. preproc_consume(_LECKKLAMMER);
  975. read_factor := ',';
  976. while current_scanner.preproc_token = _ID do
  977. begin
  978. read_factor := read_factor+read_factor(setElemType, eval)+',';
  979. if current_scanner.preproc_token = _COMMA then
  980. preproc_consume(_COMMA);
  981. end;
  982. // TODO Add check of setElemType
  983. preproc_consume(_RECKKLAMMER);
  984. factorType:= [ctetSet];
  985. end
  986. else
  987. Message(scan_e_error_in_preproc_expr);
  988. end;
  989. function read_term(var termType: TCTETypeSet; eval : Boolean) : string;
  990. var
  991. hs1,hs2 : string;
  992. l1,l2 : longint;
  993. w : integer;
  994. termType2: TCTETypeSet;
  995. begin
  996. hs1:=read_factor(termType, eval);
  997. repeat
  998. if (current_scanner.preproc_token<>_ID) then
  999. break;
  1000. if current_scanner.preproc_pattern<>'AND' then
  1001. break;
  1002. val(hs1,l1,w);
  1003. if l1=0 then
  1004. eval:= false; {Short circuit evaluation of OR}
  1005. if eval then
  1006. begin
  1007. {Check if first expr is boolean. Must be done here, after we know
  1008. it is an AND expression.}
  1009. if not (ctetBoolean in termType) then
  1010. CTEError(termType, [ctetBoolean], 'AND');
  1011. termType:= [ctetBoolean];
  1012. end;
  1013. preproc_consume(_ID);
  1014. hs2:=read_factor(termType2, eval);
  1015. if eval then
  1016. begin
  1017. if not (ctetBoolean in termType2) then
  1018. CTEError(termType2, [ctetBoolean], 'AND');
  1019. val(hs2,l2,w);
  1020. if (l1<>0) and (l2<>0) then
  1021. hs1:='1'
  1022. else
  1023. hs1:='0';
  1024. end;
  1025. until false;
  1026. read_term:=hs1;
  1027. end;
  1028. function read_simple_expr(var simpleExprType: TCTETypeSet; eval : Boolean) : string;
  1029. var
  1030. hs1,hs2 : string;
  1031. l1,l2 : longint;
  1032. w : integer;
  1033. simpleExprType2: TCTETypeSet;
  1034. begin
  1035. hs1:=read_term(simpleExprType, eval);
  1036. repeat
  1037. if (current_scanner.preproc_token<>_ID) then
  1038. break;
  1039. if current_scanner.preproc_pattern<>'OR' then
  1040. break;
  1041. val(hs1,l1,w);
  1042. if l1<>0 then
  1043. eval:= false; {Short circuit evaluation of OR}
  1044. if eval then
  1045. begin
  1046. {Check if first expr is boolean. Must be done here, after we know
  1047. it is an OR expression.}
  1048. if not (ctetBoolean in simpleExprType) then
  1049. CTEError(simpleExprType, [ctetBoolean], 'OR');
  1050. simpleExprType:= [ctetBoolean];
  1051. end;
  1052. preproc_consume(_ID);
  1053. hs2:=read_term(simpleExprType2, eval);
  1054. if eval then
  1055. begin
  1056. if not (ctetBoolean in simpleExprType2) then
  1057. CTEError(simpleExprType2, [ctetBoolean], 'OR');
  1058. val(hs2,l2,w);
  1059. if (l1<>0) or (l2<>0) then
  1060. hs1:='1'
  1061. else
  1062. hs1:='0';
  1063. end;
  1064. until false;
  1065. read_simple_expr:=hs1;
  1066. end;
  1067. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string;
  1068. var
  1069. hs1,hs2 : string;
  1070. b : boolean;
  1071. op : ttoken;
  1072. w : integer;
  1073. l1,l2 : longint;
  1074. exprType2: TCTETypeSet;
  1075. begin
  1076. hs1:=read_simple_expr(exprType, eval);
  1077. op:=current_scanner.preproc_token;
  1078. if (op = _ID) and (current_scanner.preproc_pattern = 'IN') then
  1079. op := _IN;
  1080. if not (op in [_IN,_EQUAL,_UNEQUAL,_LT,_GT,_LTE,_GTE]) then
  1081. begin
  1082. read_expr:=hs1;
  1083. exit;
  1084. end;
  1085. if (op = _IN) then
  1086. preproc_consume(_ID)
  1087. else
  1088. preproc_consume(op);
  1089. hs2:=read_simple_expr(exprType2, eval);
  1090. if eval then
  1091. begin
  1092. if op = _IN then
  1093. begin
  1094. if exprType2 <> [ctetSet] then
  1095. CTEError(exprType2, [ctetSet], 'IN');
  1096. if exprType = [ctetSet] then
  1097. CTEError(exprType, setelementdefs, 'IN');
  1098. if is_number(hs1) and is_number(hs2) then
  1099. Message(scan_e_preproc_syntax_error)
  1100. else if hs2[1] = ',' then
  1101. b:=pos(','+hs1+',', hs2) > 0 { TODO For integer sets, perhaps check for numeric equivalence so that 0 = 00 }
  1102. else
  1103. Message(scan_e_preproc_syntax_error);
  1104. end
  1105. else
  1106. begin
  1107. if (exprType * exprType2) = [] then
  1108. CTEError(exprType2, exprType, tokeninfo^[op].str);
  1109. if is_number(hs1) and is_number(hs2) then
  1110. begin
  1111. val(hs1,l1,w);
  1112. val(hs2,l2,w);
  1113. case op of
  1114. _EQUAL :
  1115. b:=l1=l2;
  1116. _UNEQUAL :
  1117. b:=l1<>l2;
  1118. _LT :
  1119. b:=l1<l2;
  1120. _GT :
  1121. b:=l1>l2;
  1122. _GTE :
  1123. b:=l1>=l2;
  1124. _LTE :
  1125. b:=l1<=l2;
  1126. end;
  1127. end
  1128. else
  1129. begin
  1130. case op of
  1131. _EQUAL :
  1132. b:=hs1=hs2;
  1133. _UNEQUAL :
  1134. b:=hs1<>hs2;
  1135. _LT :
  1136. b:=hs1<hs2;
  1137. _GT :
  1138. b:=hs1>hs2;
  1139. _GTE :
  1140. b:=hs1>=hs2;
  1141. _LTE :
  1142. b:=hs1<=hs2;
  1143. end;
  1144. end;
  1145. end;
  1146. end
  1147. else
  1148. b:= false; {Just to have something}
  1149. if b then
  1150. read_expr:='1'
  1151. else
  1152. read_expr:='0';
  1153. exprType:= [ctetBoolean];
  1154. end;
  1155. begin
  1156. current_scanner.skipspace;
  1157. { start preproc expression scanner }
  1158. current_scanner.preproc_token:=current_scanner.readpreproc;
  1159. parse_compiler_expr:=read_expr(compileExprType, true);
  1160. end;
  1161. function boolean_compile_time_expr(var valuedescr: String): Boolean;
  1162. var
  1163. hs : string;
  1164. exprType: TCTETypeSet;
  1165. begin
  1166. hs:=parse_compiler_expr(exprType);
  1167. if (exprType * [ctetBoolean]) = [] then
  1168. CTEError(exprType, [ctetBoolean], 'IF or ELSEIF');
  1169. boolean_compile_time_expr:= hs <> '0';
  1170. valuedescr:= hs;
  1171. end;
  1172. procedure dir_if;
  1173. begin
  1174. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  1175. end;
  1176. procedure dir_elseif;
  1177. begin
  1178. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  1179. end;
  1180. procedure dir_define_impl(macstyle: boolean);
  1181. var
  1182. hs : string;
  1183. bracketcount : longint;
  1184. mac : tmacro;
  1185. macropos : longint;
  1186. macrobuffer : pmacrobuffer;
  1187. begin
  1188. current_scanner.skipspace;
  1189. hs:=current_scanner.readid;
  1190. mac:=tmacro(search_macro(hs));
  1191. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  1192. begin
  1193. mac:=tmacro.create(hs);
  1194. mac.defined:=true;
  1195. current_module.localmacrosymtable.insert(mac);
  1196. end
  1197. else
  1198. begin
  1199. mac.defined:=true;
  1200. mac.is_compiler_var:=false;
  1201. { delete old definition }
  1202. if assigned(mac.buftext) then
  1203. begin
  1204. freemem(mac.buftext,mac.buflen);
  1205. mac.buftext:=nil;
  1206. end;
  1207. end;
  1208. Message1(parser_c_macro_defined,mac.name);
  1209. mac.is_used:=true;
  1210. if (cs_support_macro in current_settings.moduleswitches) then
  1211. begin
  1212. current_scanner.skipspace;
  1213. if not macstyle then
  1214. begin
  1215. { may be a macro? }
  1216. if c <> ':' then
  1217. exit;
  1218. current_scanner.readchar;
  1219. if c <> '=' then
  1220. exit;
  1221. current_scanner.readchar;
  1222. current_scanner.skipspace;
  1223. end;
  1224. { key words are never substituted }
  1225. if is_keyword(hs) then
  1226. Message(scan_e_keyword_cant_be_a_macro);
  1227. new(macrobuffer);
  1228. macropos:=0;
  1229. { parse macro, brackets are counted so it's possible
  1230. to have a $ifdef etc. in the macro }
  1231. bracketcount:=0;
  1232. repeat
  1233. case c of
  1234. '}' :
  1235. if (bracketcount=0) then
  1236. break
  1237. else
  1238. dec(bracketcount);
  1239. '{' :
  1240. inc(bracketcount);
  1241. #10,#13 :
  1242. current_scanner.linebreak;
  1243. #26 :
  1244. current_scanner.end_of_file;
  1245. end;
  1246. macrobuffer^[macropos]:=c;
  1247. inc(macropos);
  1248. if macropos>=maxmacrolen then
  1249. Message(scan_f_macro_buffer_overflow);
  1250. current_scanner.readchar;
  1251. until false;
  1252. { free buffer of macro ?}
  1253. if assigned(mac.buftext) then
  1254. freemem(mac.buftext,mac.buflen);
  1255. { get new mem }
  1256. getmem(mac.buftext,macropos);
  1257. mac.buflen:=macropos;
  1258. { copy the text }
  1259. move(macrobuffer^,mac.buftext^,macropos);
  1260. dispose(macrobuffer);
  1261. end
  1262. else
  1263. begin
  1264. { check if there is an assignment, then we need to give a
  1265. warning }
  1266. current_scanner.skipspace;
  1267. if c=':' then
  1268. begin
  1269. current_scanner.readchar;
  1270. if c='=' then
  1271. Message(scan_w_macro_support_turned_off);
  1272. end;
  1273. end;
  1274. end;
  1275. procedure dir_define;
  1276. begin
  1277. dir_define_impl(false);
  1278. end;
  1279. procedure dir_definec;
  1280. begin
  1281. dir_define_impl(true);
  1282. end;
  1283. procedure dir_setc;
  1284. var
  1285. hs : string;
  1286. mac : tmacro;
  1287. exprType: TCTETypeSet;
  1288. l : longint;
  1289. w : integer;
  1290. begin
  1291. current_scanner.skipspace;
  1292. hs:=current_scanner.readid;
  1293. mac:=tmacro(search_macro(hs));
  1294. if not assigned(mac) or
  1295. (mac.owner <> current_module.localmacrosymtable) then
  1296. begin
  1297. mac:=tmacro.create(hs);
  1298. mac.defined:=true;
  1299. mac.is_compiler_var:=true;
  1300. current_module.localmacrosymtable.insert(mac);
  1301. end
  1302. else
  1303. begin
  1304. mac.defined:=true;
  1305. mac.is_compiler_var:=true;
  1306. { delete old definition }
  1307. if assigned(mac.buftext) then
  1308. begin
  1309. freemem(mac.buftext,mac.buflen);
  1310. mac.buftext:=nil;
  1311. end;
  1312. end;
  1313. Message1(parser_c_macro_defined,mac.name);
  1314. mac.is_used:=true;
  1315. { key words are never substituted }
  1316. if is_keyword(hs) then
  1317. Message(scan_e_keyword_cant_be_a_macro);
  1318. { macro assignment can be both := and = }
  1319. current_scanner.skipspace;
  1320. if c=':' then
  1321. current_scanner.readchar;
  1322. if c='=' then
  1323. begin
  1324. current_scanner.readchar;
  1325. hs:= parse_compiler_expr(exprType);
  1326. if (exprType * [ctetBoolean, ctetInteger]) = [] then
  1327. CTEError(exprType, [ctetBoolean, ctetInteger], 'SETC');
  1328. if length(hs) <> 0 then
  1329. begin
  1330. {If we are absolutely shure it is boolean, translate
  1331. to TRUE/FALSE to increase possibility to do future type check}
  1332. if exprType = [ctetBoolean] then
  1333. begin
  1334. val(hs,l,w);
  1335. if l<>0 then
  1336. hs:='TRUE'
  1337. else
  1338. hs:='FALSE';
  1339. end;
  1340. Message2(parser_c_macro_set_to,mac.name,hs);
  1341. { free buffer of macro ?}
  1342. if assigned(mac.buftext) then
  1343. freemem(mac.buftext,mac.buflen);
  1344. { get new mem }
  1345. getmem(mac.buftext,length(hs));
  1346. mac.buflen:=length(hs);
  1347. { copy the text }
  1348. move(hs[1],mac.buftext^,mac.buflen);
  1349. end
  1350. else
  1351. Message(scan_e_preproc_syntax_error);
  1352. end
  1353. else
  1354. Message(scan_e_preproc_syntax_error);
  1355. end;
  1356. procedure dir_undef;
  1357. var
  1358. hs : string;
  1359. mac : tmacro;
  1360. begin
  1361. current_scanner.skipspace;
  1362. hs:=current_scanner.readid;
  1363. mac:=tmacro(search_macro(hs));
  1364. if not assigned(mac) or
  1365. (mac.owner <> current_module.localmacrosymtable) then
  1366. begin
  1367. mac:=tmacro.create(hs);
  1368. mac.defined:=false;
  1369. current_module.localmacrosymtable.insert(mac);
  1370. end
  1371. else
  1372. begin
  1373. mac.defined:=false;
  1374. mac.is_compiler_var:=false;
  1375. { delete old definition }
  1376. if assigned(mac.buftext) then
  1377. begin
  1378. freemem(mac.buftext,mac.buflen);
  1379. mac.buftext:=nil;
  1380. end;
  1381. end;
  1382. Message1(parser_c_macro_undefined,mac.name);
  1383. mac.is_used:=true;
  1384. end;
  1385. procedure dir_include;
  1386. function findincludefile(const path,name:TCmdStr;var foundfile:TCmdStr):boolean;
  1387. var
  1388. found : boolean;
  1389. hpath : TCmdStr;
  1390. begin
  1391. (* look for the include file
  1392. If path was specified as part of {$I } then
  1393. 1. specified path (expanded with path of inputfile if relative)
  1394. else
  1395. 1. path of current inputfile,current dir
  1396. 2. local includepath
  1397. 3. global includepath *)
  1398. found:=false;
  1399. foundfile:='';
  1400. hpath:='';
  1401. if path<>'' then
  1402. begin
  1403. if not path_absolute(path) then
  1404. hpath:=current_scanner.inputfile.path^+path
  1405. else
  1406. hpath:=path;
  1407. found:=FindFile(name, hpath,true,foundfile);
  1408. end
  1409. else
  1410. begin
  1411. hpath:=current_scanner.inputfile.path^+';'+CurDirRelPath(source_info);
  1412. found:=FindFile(name, hpath,true,foundfile);
  1413. if not found then
  1414. found:=current_module.localincludesearchpath.FindFile(name,true,foundfile);
  1415. if not found then
  1416. found:=includesearchpath.FindFile(name,true,foundfile);
  1417. end;
  1418. result:=found;
  1419. end;
  1420. var
  1421. foundfile : TCmdStr;
  1422. path,
  1423. name,
  1424. args,
  1425. hs : tpathstr;
  1426. hp : tinputfile;
  1427. found : boolean;
  1428. begin
  1429. current_scanner.skipspace;
  1430. args:=current_scanner.readcomment;
  1431. hs:=GetToken(args,' ');
  1432. if hs='' then
  1433. exit;
  1434. if (hs[1]='%') then
  1435. begin
  1436. { case insensitive }
  1437. hs:=upper(hs);
  1438. { remove %'s }
  1439. Delete(hs,1,1);
  1440. if hs[length(hs)]='%' then
  1441. Delete(hs,length(hs),1);
  1442. { save old }
  1443. path:=hs;
  1444. { first check for internal macros }
  1445. if hs='TIME' then
  1446. hs:=gettimestr
  1447. else
  1448. if hs='DATE' then
  1449. hs:=getdatestr
  1450. else
  1451. if hs='FILE' then
  1452. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex)
  1453. else
  1454. if hs='LINE' then
  1455. hs:=tostr(current_filepos.line)
  1456. else
  1457. if hs='FPCVERSION' then
  1458. hs:=version_string
  1459. else
  1460. if hs='FPCDATE' then
  1461. hs:=date_string
  1462. else
  1463. if hs='FPCTARGET' then
  1464. hs:=target_cpu_string
  1465. else
  1466. if hs='FPCTARGETCPU' then
  1467. hs:=target_cpu_string
  1468. else
  1469. if hs='FPCTARGETOS' then
  1470. hs:=target_info.shortname
  1471. else
  1472. hs:=GetEnvironmentVariable(hs);
  1473. if hs='' then
  1474. Message1(scan_w_include_env_not_found,path);
  1475. { make it a stringconst }
  1476. hs:=''''+hs+'''';
  1477. current_scanner.insertmacro(path,@hs[1],length(hs),
  1478. current_scanner.line_no,current_scanner.inputfile.ref_index);
  1479. end
  1480. else
  1481. begin
  1482. hs:=FixFileName(hs);
  1483. path:=ExtractFilePath(hs);
  1484. name:=ExtractFileName(hs);
  1485. { try to find the file }
  1486. found:=findincludefile(path,name,foundfile);
  1487. if (ExtractFileExt(name)='') then
  1488. begin
  1489. { try default extensions .inc , .pp and .pas }
  1490. if (not found) then
  1491. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  1492. if (not found) then
  1493. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  1494. if (not found) then
  1495. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  1496. end;
  1497. if current_scanner.inputfilecount<max_include_nesting then
  1498. begin
  1499. inc(current_scanner.inputfilecount);
  1500. { we need to reread the current char }
  1501. dec(current_scanner.inputpointer);
  1502. { shutdown current file }
  1503. current_scanner.tempcloseinputfile;
  1504. { load new file }
  1505. hp:=do_openinputfile(foundfile);
  1506. current_scanner.addfile(hp);
  1507. current_module.sourcefiles.register_file(hp);
  1508. if (not found) then
  1509. Message1(scan_f_cannot_open_includefile,hs);
  1510. if (not current_scanner.openinputfile) then
  1511. Message1(scan_f_cannot_open_includefile,hs);
  1512. Message1(scan_t_start_include_file,current_scanner.inputfile.path^+current_scanner.inputfile.name^);
  1513. current_scanner.reload;
  1514. end
  1515. else
  1516. Message(scan_f_include_deep_ten);
  1517. end;
  1518. end;
  1519. {*****************************************************************************
  1520. Preprocessor writing
  1521. *****************************************************************************}
  1522. {$ifdef PREPROCWRITE}
  1523. constructor tpreprocfile.create(const fn:string);
  1524. begin
  1525. { open outputfile }
  1526. assign(f,fn);
  1527. {$I-}
  1528. rewrite(f);
  1529. {$I+}
  1530. if ioresult<>0 then
  1531. Comment(V_Fatal,'can''t create file '+fn);
  1532. getmem(buf,preprocbufsize);
  1533. settextbuf(f,buf^,preprocbufsize);
  1534. { reset }
  1535. eolfound:=false;
  1536. spacefound:=false;
  1537. end;
  1538. destructor tpreprocfile.destroy;
  1539. begin
  1540. close(f);
  1541. freemem(buf,preprocbufsize);
  1542. end;
  1543. procedure tpreprocfile.add(const s:string);
  1544. begin
  1545. write(f,s);
  1546. end;
  1547. procedure tpreprocfile.addspace;
  1548. begin
  1549. if eolfound then
  1550. begin
  1551. writeln(f,'');
  1552. eolfound:=false;
  1553. spacefound:=false;
  1554. end
  1555. else
  1556. if spacefound then
  1557. begin
  1558. write(f,' ');
  1559. spacefound:=false;
  1560. end;
  1561. end;
  1562. {$endif PREPROCWRITE}
  1563. {*****************************************************************************
  1564. TPreProcStack
  1565. *****************************************************************************}
  1566. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  1567. begin
  1568. accept:=a;
  1569. typ:=atyp;
  1570. next:=n;
  1571. end;
  1572. {*****************************************************************************
  1573. TDirectiveItem
  1574. *****************************************************************************}
  1575. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  1576. begin
  1577. inherited Create(AList,n);
  1578. is_conditional:=false;
  1579. proc:=p;
  1580. end;
  1581. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  1582. begin
  1583. inherited Create(AList,n);
  1584. is_conditional:=true;
  1585. proc:=p;
  1586. end;
  1587. {****************************************************************************
  1588. TSCANNERFILE
  1589. ****************************************************************************}
  1590. constructor tscannerfile.create(const fn:string);
  1591. begin
  1592. inputfile:=do_openinputfile(fn);
  1593. if assigned(current_module) then
  1594. current_module.sourcefiles.register_file(inputfile);
  1595. { reset localinput }
  1596. inputbuffer:=nil;
  1597. inputpointer:=nil;
  1598. inputstart:=0;
  1599. { reset scanner }
  1600. preprocstack:=nil;
  1601. comment_level:=0;
  1602. yylexcount:=0;
  1603. block_type:=bt_general;
  1604. line_no:=0;
  1605. lastlinepos:=0;
  1606. lasttokenpos:=0;
  1607. lasttoken:=NOTOKEN;
  1608. nexttoken:=NOTOKEN;
  1609. lastasmgetchar:=#0;
  1610. ignoredirectives:=TFPHashList.Create;
  1611. in_asm_string:=false;
  1612. end;
  1613. procedure tscannerfile.firstfile;
  1614. begin
  1615. { load block }
  1616. if not openinputfile then
  1617. Message1(scan_f_cannot_open_input,inputfile.name^);
  1618. reload;
  1619. end;
  1620. destructor tscannerfile.destroy;
  1621. begin
  1622. if assigned(current_module) and
  1623. (current_module.state=ms_compiled) and
  1624. (status.errorcount=0) then
  1625. checkpreprocstack
  1626. else
  1627. begin
  1628. while assigned(preprocstack) do
  1629. poppreprocstack;
  1630. end;
  1631. if not inputfile.closed then
  1632. closeinputfile;
  1633. ignoredirectives.free;
  1634. end;
  1635. function tscannerfile.openinputfile:boolean;
  1636. begin
  1637. openinputfile:=inputfile.open;
  1638. { load buffer }
  1639. inputbuffer:=inputfile.buf;
  1640. inputpointer:=inputfile.buf;
  1641. inputstart:=inputfile.bufstart;
  1642. { line }
  1643. line_no:=0;
  1644. lastlinepos:=0;
  1645. lasttokenpos:=0;
  1646. end;
  1647. procedure tscannerfile.closeinputfile;
  1648. begin
  1649. inputfile.close;
  1650. { reset buffer }
  1651. inputbuffer:=nil;
  1652. inputpointer:=nil;
  1653. inputstart:=0;
  1654. { reset line }
  1655. line_no:=0;
  1656. lastlinepos:=0;
  1657. lasttokenpos:=0;
  1658. end;
  1659. function tscannerfile.tempopeninputfile:boolean;
  1660. begin
  1661. if inputfile.is_macro then
  1662. exit;
  1663. tempopeninputfile:=inputfile.tempopen;
  1664. { reload buffer }
  1665. inputbuffer:=inputfile.buf;
  1666. inputpointer:=inputfile.buf;
  1667. inputstart:=inputfile.bufstart;
  1668. end;
  1669. procedure tscannerfile.tempcloseinputfile;
  1670. begin
  1671. if inputfile.closed or inputfile.is_macro then
  1672. exit;
  1673. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  1674. inputfile.tempclose;
  1675. { reset buffer }
  1676. inputbuffer:=nil;
  1677. inputpointer:=nil;
  1678. inputstart:=0;
  1679. end;
  1680. procedure tscannerfile.saveinputfile;
  1681. begin
  1682. inputfile.saveinputpointer:=inputpointer;
  1683. inputfile.savelastlinepos:=lastlinepos;
  1684. inputfile.saveline_no:=line_no;
  1685. end;
  1686. procedure tscannerfile.restoreinputfile;
  1687. begin
  1688. inputbuffer:=inputfile.buf;
  1689. inputpointer:=inputfile.saveinputpointer;
  1690. lastlinepos:=inputfile.savelastlinepos;
  1691. line_no:=inputfile.saveline_no;
  1692. if not inputfile.is_macro then
  1693. parser_current_file:=inputfile.name^;
  1694. end;
  1695. procedure tscannerfile.nextfile;
  1696. var
  1697. to_dispose : tinputfile;
  1698. begin
  1699. if assigned(inputfile.next) then
  1700. begin
  1701. if inputfile.is_macro then
  1702. to_dispose:=inputfile
  1703. else
  1704. begin
  1705. to_dispose:=nil;
  1706. dec(inputfilecount);
  1707. end;
  1708. { we can allways close the file, no ? }
  1709. inputfile.close;
  1710. inputfile:=inputfile.next;
  1711. if assigned(to_dispose) then
  1712. to_dispose.free;
  1713. restoreinputfile;
  1714. end;
  1715. end;
  1716. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  1717. begin
  1718. if not assigned(buf) then
  1719. internalerror(200511172);
  1720. if assigned(recordtokenbuf) then
  1721. internalerror(200511173);
  1722. recordtokenbuf:=buf;
  1723. fillchar(last_settings,sizeof(last_settings),0);
  1724. fillchar(last_filepos,sizeof(last_filepos),0);
  1725. end;
  1726. procedure tscannerfile.stoprecordtokens;
  1727. begin
  1728. if not assigned(recordtokenbuf) then
  1729. internalerror(200511174);
  1730. recordtokenbuf:=nil;
  1731. end;
  1732. procedure tscannerfile.recordtoken;
  1733. var
  1734. a : array[0..1] of byte;
  1735. begin
  1736. if not assigned(recordtokenbuf) then
  1737. internalerror(200511176);
  1738. { settings changed? }
  1739. if CompareByte(current_settings,last_settings,sizeof(current_settings))<>0 then
  1740. begin
  1741. { use a special token to record it }
  1742. a[0]:=byte(_GENERICSPECIALTOKEN);
  1743. a[1]:=byte(ST_LOADSETTINGS);
  1744. recordtokenbuf.write(a,2);
  1745. recordtokenbuf.write(current_settings,sizeof(current_settings));
  1746. last_settings:=current_settings;
  1747. end;
  1748. { file pos changes? }
  1749. if current_tokenpos.line<>last_filepos.line then
  1750. begin
  1751. a[0]:=byte(_GENERICSPECIALTOKEN);
  1752. a[1]:=byte(ST_LINE);
  1753. recordtokenbuf.write(a,2);
  1754. recordtokenbuf.write(current_tokenpos.line,sizeof(current_tokenpos.line));
  1755. last_filepos.line:=current_tokenpos.line;
  1756. end;
  1757. if current_tokenpos.column<>last_filepos.column then
  1758. begin
  1759. a[0]:=byte(_GENERICSPECIALTOKEN);
  1760. a[1]:=byte(ST_COLUMN);
  1761. recordtokenbuf.write(a,2);
  1762. recordtokenbuf.write(current_tokenpos.column,sizeof(current_tokenpos.column));
  1763. last_filepos.column:=current_tokenpos.column;
  1764. end;
  1765. if current_tokenpos.fileindex<>last_filepos.fileindex then
  1766. begin
  1767. a[0]:=byte(_GENERICSPECIALTOKEN);
  1768. a[1]:=byte(ST_FILEINDEX);
  1769. recordtokenbuf.write(a,2);
  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.