scanner.pas 129 KB

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