scanner.pas 124 KB

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