scanner.pas 123 KB

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