scanner.pas 134 KB

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