scanner.pas 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the scanner part and handling of the switches
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit scanner;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. globtype,globals,version,tokens,
  24. verbose,comphook,
  25. finput,
  26. widestr,cpuinfo;
  27. const
  28. max_include_nesting=32;
  29. max_macro_nesting=16;
  30. maxmacrolen=16*1024;
  31. preprocbufsize=32*1024;
  32. type
  33. tcommentstyle = (comment_none,comment_tp,comment_oldtp,comment_delphi,comment_c);
  34. pmacrobuffer = ^tmacrobuffer;
  35. tmacrobuffer = array[0..maxmacrolen-1] of char;
  36. tscannerfile = class;
  37. tmacro = class(TNamedIndexItem)
  38. defined : boolean; { normally true, but false when the macro is undef-ed}
  39. defined_at_startup : boolean;
  40. is_used : boolean;
  41. is_compiler_var : boolean; { if this is a mac style compiler variable, in
  42. which case no macro substitutions shall be done.}
  43. buftext : pchar;
  44. buflen : longint;
  45. fileinfo : tfileposinfo;
  46. constructor Create(const n : string);
  47. destructor destroy;override;
  48. end;
  49. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else,pp_elseif);
  50. tpreprocstack = class
  51. typ : preproctyp;
  52. accept : boolean;
  53. next : tpreprocstack;
  54. name : stringid;
  55. line_nb : longint;
  56. owner : tscannerfile;
  57. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  58. end;
  59. tdirectiveproc=procedure;
  60. tdirectiveitem = class(TNamedIndexItem)
  61. public
  62. is_conditional : boolean;
  63. proc : tdirectiveproc;
  64. constructor Create(const n:string;p:tdirectiveproc);
  65. constructor CreateCond(const n:string;p:tdirectiveproc);
  66. end;
  67. tscannerfile = class
  68. public
  69. inputfile : tinputfile; { current inputfile list }
  70. inputfilecount : longint;
  71. inputbuffer, { input buffer }
  72. inputpointer : pchar;
  73. inputstart : longint;
  74. line_no, { line }
  75. lastlinepos : longint;
  76. lasttokenpos : longint; { token }
  77. lasttoken,
  78. nexttoken : ttoken;
  79. comment_level,
  80. yylexcount : longint;
  81. lastasmgetchar : char;
  82. ignoredirectives : tstringlist; { ignore directives, used to give warnings only once }
  83. preprocstack : tpreprocstack;
  84. macros : Tdictionary;
  85. in_asm_string : boolean;
  86. preproc_pattern : string;
  87. preproc_token : ttoken;
  88. constructor Create(const fn:string);
  89. destructor Destroy;override;
  90. { File buffer things }
  91. function openinputfile:boolean;
  92. procedure closeinputfile;
  93. function tempopeninputfile:boolean;
  94. procedure tempcloseinputfile;
  95. procedure saveinputfile;
  96. procedure restoreinputfile;
  97. procedure firstfile;
  98. procedure nextfile;
  99. procedure addfile(hp:tinputfile);
  100. procedure reload;
  101. procedure insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  102. { Scanner things }
  103. procedure def_macro(const s : string);
  104. procedure set_macro(const s : string;value : string);
  105. procedure gettokenpos;
  106. procedure inc_comment_level;
  107. procedure dec_comment_level;
  108. procedure illegal_char(c:char);
  109. procedure end_of_file;
  110. procedure checkpreprocstack;
  111. procedure poppreprocstack;
  112. procedure addpreprocstack(atyp : preproctyp;a:boolean;const s:string;w:longint);
  113. procedure elsepreprocstack;
  114. procedure elseifpreprocstack(accept:boolean);
  115. procedure handleconditional(p:tdirectiveitem);
  116. procedure handledirectives;
  117. procedure linebreak;
  118. procedure readchar;
  119. procedure readstring;
  120. procedure readnumber;
  121. function readid:string;
  122. function readval:longint;
  123. function readval_asstring:string;
  124. function readcomment:string;
  125. function readquotedstring:string;
  126. function readstate:char;
  127. procedure skipspace;
  128. procedure skipuntildirective;
  129. procedure skipcomment;
  130. procedure skipdelphicomment;
  131. procedure skipoldtpcomment;
  132. procedure readtoken;
  133. function readpreproc:ttoken;
  134. function asmgetcharstart : char;
  135. function asmgetchar:char;
  136. end;
  137. {$ifdef PREPROCWRITE}
  138. tpreprocfile=class
  139. f : text;
  140. buf : pointer;
  141. spacefound,
  142. eolfound : boolean;
  143. constructor create(const fn:string);
  144. destructor destroy;
  145. procedure Add(const s:string);
  146. procedure AddSpace;
  147. end;
  148. {$endif PREPROCWRITE}
  149. var
  150. { read strings }
  151. c : char;
  152. orgpattern,
  153. pattern : string;
  154. patternw : pcompilerwidestring;
  155. { token }
  156. token, { current token being parsed }
  157. idtoken : ttoken; { holds the token if the pattern is a known word }
  158. current_scanner : tscannerfile; { current scanner in use }
  159. aktcommentstyle : tcommentstyle; { needed to use read_comment from directives }
  160. {$ifdef PREPROCWRITE}
  161. preprocfile : tpreprocfile; { used with only preprocessing }
  162. {$endif PREPROCWRITE}
  163. type
  164. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  165. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  166. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  167. procedure InitScanner;
  168. procedure DoneScanner;
  169. implementation
  170. uses
  171. SysUtils,
  172. cutils,
  173. systems,
  174. switches,
  175. symbase,symtable,symtype,symsym,symconst,
  176. fmodule;
  177. var
  178. { dictionaries with the supported directives }
  179. turbo_scannerdirectives : tdictionary; { for other modes }
  180. mac_scannerdirectives : tdictionary; { for mode mac }
  181. {*****************************************************************************
  182. Helper routines
  183. *****************************************************************************}
  184. const
  185. { use any special name that is an invalid file name to avoid problems }
  186. preprocstring : array [preproctyp] of string[7]
  187. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE','$ELSEIF');
  188. function is_keyword(const s:string):boolean;
  189. var
  190. low,high,mid : longint;
  191. begin
  192. if not (length(s) in [tokenlenmin..tokenlenmax]) then
  193. begin
  194. is_keyword:=false;
  195. exit;
  196. end;
  197. low:=ord(tokenidx^[length(s),s[1]].first);
  198. high:=ord(tokenidx^[length(s),s[1]].last);
  199. while low<high do
  200. begin
  201. mid:=(high+low+1) shr 1;
  202. if pattern<tokeninfo^[ttoken(mid)].str then
  203. high:=mid-1
  204. else
  205. low:=mid;
  206. end;
  207. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  208. (tokeninfo^[ttoken(high)].keyword in aktmodeswitches);
  209. end;
  210. {*****************************************************************************
  211. Conditional Directives
  212. *****************************************************************************}
  213. procedure dir_else;
  214. begin
  215. current_scanner.elsepreprocstack;
  216. end;
  217. procedure dir_endif;
  218. begin
  219. current_scanner.poppreprocstack;
  220. end;
  221. procedure dir_ifdef;
  222. var
  223. hs : string;
  224. mac : tmacro;
  225. begin
  226. current_scanner.skipspace;
  227. hs:=current_scanner.readid;
  228. if hs='' then
  229. Message(scan_e_error_in_preproc_expr);
  230. mac:=tmacro(current_scanner.macros.search(hs));
  231. if assigned(mac) then
  232. mac.is_used:=true;
  233. current_scanner.addpreprocstack(pp_ifdef,assigned(mac) and mac.defined,hs,scan_c_ifdef_found);
  234. end;
  235. procedure dir_ifndef;
  236. var
  237. hs : string;
  238. mac : tmacro;
  239. begin
  240. current_scanner.skipspace;
  241. hs:=current_scanner.readid;
  242. if hs='' then
  243. Message(scan_e_error_in_preproc_expr);
  244. mac:=tmacro(current_scanner.macros.search(hs));
  245. if assigned(mac) then
  246. mac.is_used:=true;
  247. current_scanner.addpreprocstack(pp_ifndef,not(assigned(mac) and mac.defined),hs,scan_c_ifndef_found);
  248. end;
  249. procedure dir_ifopt;
  250. var
  251. hs : string;
  252. found : boolean;
  253. state : char;
  254. begin
  255. found:= false;
  256. current_scanner.skipspace;
  257. hs:=current_scanner.readid;
  258. if (length(hs)>1) then
  259. Message1(scan_w_illegal_switch,hs)
  260. else
  261. begin
  262. state:=current_scanner.ReadState;
  263. if state in ['-','+'] then
  264. found:=CheckSwitch(hs[1],state);
  265. end;
  266. current_scanner.addpreprocstack(pp_ifopt,found,hs,scan_c_ifopt_found);
  267. end;
  268. procedure dir_libprefix;
  269. var
  270. s : string;
  271. begin
  272. current_scanner.skipspace;
  273. if c <> '''' then
  274. Message2(scan_f_syn_expected, '''', c);
  275. s := current_scanner.readquotedstring;
  276. stringdispose(outputprefix);
  277. outputprefix := stringdup(s);
  278. with current_module do
  279. setfilename(paramfn^, paramallowoutput);
  280. end;
  281. procedure dir_libsuffix;
  282. var
  283. s : string;
  284. begin
  285. current_scanner.skipspace;
  286. if c <> '''' then
  287. Message2(scan_f_syn_expected, '''', c);
  288. s := current_scanner.readquotedstring;
  289. stringdispose(outputsuffix);
  290. outputsuffix := stringdup(s);
  291. with current_module do
  292. setfilename(paramfn^, paramallowoutput);
  293. end;
  294. procedure dir_extension;
  295. var
  296. s : string;
  297. begin
  298. current_scanner.skipspace;
  299. if c <> '''' then
  300. Message2(scan_f_syn_expected, '''', c);
  301. s := current_scanner.readquotedstring;
  302. outputextension := '.'+s;
  303. with current_module do
  304. setfilename(paramfn^, paramallowoutput);
  305. end;
  306. function parse_compiler_expr:string;
  307. function read_expr : string; forward;
  308. procedure preproc_consume(t : ttoken);
  309. begin
  310. if t<>current_scanner.preproc_token then
  311. Message(scan_e_preproc_syntax_error);
  312. current_scanner.preproc_token:=current_scanner.readpreproc;
  313. end;
  314. function preproc_substitutedtoken: string;
  315. var
  316. hs: string;
  317. mac : tmacro;
  318. macrocount,
  319. len : integer;
  320. begin
  321. result := current_scanner.preproc_pattern;
  322. { Substitue macros and compiler variables with their content/value.
  323. For real macros also do recursive substitution. }
  324. macrocount:=0;
  325. repeat
  326. mac:=tmacro(current_scanner.macros.search(result));
  327. inc(macrocount);
  328. if macrocount>max_macro_nesting then
  329. begin
  330. Message(scan_w_macro_deep_ten);
  331. break;
  332. end;
  333. if assigned(mac) and mac.defined then
  334. if assigned(mac.buftext) then
  335. begin
  336. if mac.buflen>255 then
  337. begin
  338. len:=255;
  339. Message(scan_w_macro_cut_after_255_chars);
  340. end
  341. else
  342. len:=mac.buflen;
  343. hs[0]:=char(len);
  344. move(mac.buftext^,hs[1],len);
  345. result:=upcase(hs);
  346. end
  347. else
  348. begin
  349. Message1(scan_e_error_macro_lacks_value, result);
  350. break;
  351. end
  352. else
  353. break;
  354. if mac.is_compiler_var then
  355. break;
  356. until false;
  357. end;
  358. function read_factor : string;
  359. var
  360. hs : string;
  361. mac: tmacro;
  362. srsym : tsym;
  363. srsymtable : tsymtable;
  364. l : longint;
  365. w : integer;
  366. begin
  367. if current_scanner.preproc_token=_ID then
  368. begin
  369. if current_scanner.preproc_pattern='DEFINED' then
  370. begin
  371. preproc_consume(_ID);
  372. current_scanner.skipspace;
  373. if current_scanner.preproc_token =_LKLAMMER then
  374. begin
  375. preproc_consume(_LKLAMMER);
  376. current_scanner.skipspace;
  377. end
  378. else
  379. Message(scan_e_error_in_preproc_expr);
  380. if current_scanner.preproc_token =_ID then
  381. begin
  382. hs := current_scanner.preproc_pattern;
  383. mac := tmacro(current_scanner.macros.search(hs));
  384. if assigned(mac) then
  385. hs := '1'
  386. else
  387. hs := '0';
  388. read_factor := hs;
  389. preproc_consume(_ID);
  390. current_scanner.skipspace;
  391. end
  392. else
  393. Message(scan_e_error_in_preproc_expr);
  394. if current_scanner.preproc_token =_RKLAMMER then
  395. preproc_consume(_RKLAMMER)
  396. else
  397. Message(scan_e_error_in_preproc_expr);
  398. end
  399. else
  400. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  401. begin
  402. preproc_consume(_ID);
  403. current_scanner.skipspace;
  404. if current_scanner.preproc_token =_ID then
  405. begin
  406. hs := current_scanner.preproc_pattern;
  407. mac := tmacro(current_scanner.macros.search(hs));
  408. if assigned(mac) then
  409. hs := '0'
  410. else
  411. hs := '1';
  412. read_factor := hs;
  413. preproc_consume(_ID);
  414. current_scanner.skipspace;
  415. end
  416. else
  417. Message(scan_e_error_in_preproc_expr);
  418. end
  419. else
  420. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='OPTION') then
  421. begin
  422. preproc_consume(_ID);
  423. current_scanner.skipspace;
  424. if current_scanner.preproc_token =_LKLAMMER then
  425. begin
  426. preproc_consume(_LKLAMMER);
  427. current_scanner.skipspace;
  428. end
  429. else
  430. Message(scan_e_error_in_preproc_expr);
  431. if not (current_scanner.preproc_token = _ID) then
  432. Message(scan_e_error_in_preproc_expr);
  433. hs:=current_scanner.preproc_pattern;
  434. if (length(hs) > 1) then
  435. {This is allowed in Metrowerks Pascal}
  436. Message(scan_e_error_in_preproc_expr)
  437. else
  438. begin
  439. if CheckSwitch(hs[1],'+') then
  440. read_factor := '1'
  441. else
  442. read_factor := '0';
  443. end;
  444. preproc_consume(_ID);
  445. current_scanner.skipspace;
  446. if current_scanner.preproc_token =_RKLAMMER then
  447. preproc_consume(_RKLAMMER)
  448. else
  449. Message(scan_e_error_in_preproc_expr);
  450. end
  451. else
  452. if current_scanner.preproc_pattern='SIZEOF' then
  453. begin
  454. preproc_consume(_ID);
  455. current_scanner.skipspace;
  456. if current_scanner.preproc_token =_LKLAMMER then
  457. begin
  458. preproc_consume(_LKLAMMER);
  459. current_scanner.skipspace;
  460. end
  461. else
  462. Message(scan_e_error_in_preproc_expr);
  463. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  464. begin
  465. l:=0;
  466. case srsym.typ of
  467. varsym :
  468. l:=tvarsym(srsym).getsize;
  469. typedconstsym :
  470. l:=ttypedconstsym(srsym).getsize;
  471. typesym:
  472. l:=ttypesym(srsym).restype.def.size;
  473. else
  474. Message(scan_e_error_in_preproc_expr);
  475. end;
  476. str(l,read_factor);
  477. preproc_consume(_ID);
  478. current_scanner.skipspace;
  479. end
  480. else
  481. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  482. if current_scanner.preproc_token =_RKLAMMER then
  483. preproc_consume(_RKLAMMER)
  484. else
  485. Message(scan_e_error_in_preproc_expr);
  486. end
  487. else
  488. if current_scanner.preproc_pattern='DECLARED' then
  489. begin
  490. preproc_consume(_ID);
  491. current_scanner.skipspace;
  492. if current_scanner.preproc_token =_LKLAMMER then
  493. begin
  494. preproc_consume(_LKLAMMER);
  495. current_scanner.skipspace;
  496. end
  497. else
  498. Message(scan_e_error_in_preproc_expr);
  499. if current_scanner.preproc_token =_ID then
  500. begin
  501. hs := upper(current_scanner.preproc_pattern);
  502. if searchsym(hs,srsym,srsymtable) then
  503. hs := '1'
  504. else
  505. hs := '0';
  506. read_factor := hs;
  507. preproc_consume(_ID);
  508. current_scanner.skipspace;
  509. end
  510. else
  511. Message(scan_e_error_in_preproc_expr);
  512. if current_scanner.preproc_token =_RKLAMMER then
  513. preproc_consume(_RKLAMMER)
  514. else
  515. Message(scan_e_error_in_preproc_expr);
  516. end
  517. else
  518. if current_scanner.preproc_pattern='NOT' then
  519. begin
  520. preproc_consume(_ID);
  521. hs:=read_factor();
  522. valint(hs,l,w);
  523. if l<>0 then
  524. read_factor:='0'
  525. else
  526. read_factor:='1';
  527. end
  528. else
  529. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='TRUE') then
  530. begin
  531. preproc_consume(_ID);
  532. read_factor:='1';
  533. end
  534. else
  535. if (m_mac in aktmodeswitches) and (current_scanner.preproc_pattern='FALSE') then
  536. begin
  537. preproc_consume(_ID);
  538. read_factor:='0';
  539. end
  540. else
  541. begin
  542. hs:=preproc_substitutedtoken;
  543. preproc_consume(_ID);
  544. read_factor:=hs;
  545. end
  546. end
  547. else if current_scanner.preproc_token =_LKLAMMER then
  548. begin
  549. preproc_consume(_LKLAMMER);
  550. read_factor:=read_expr;
  551. preproc_consume(_RKLAMMER);
  552. end
  553. else
  554. Message(scan_e_error_in_preproc_expr);
  555. end;
  556. function read_term : string;
  557. var
  558. hs1,hs2 : string;
  559. l1,l2 : longint;
  560. w : integer;
  561. begin
  562. hs1:=read_factor;
  563. repeat
  564. if (current_scanner.preproc_token<>_ID) then
  565. break;
  566. if current_scanner.preproc_pattern<>'AND' then
  567. break;
  568. preproc_consume(_ID);
  569. hs2:=read_factor;
  570. valint(hs1,l1,w);
  571. valint(hs2,l2,w);
  572. if (l1<>0) and (l2<>0) then
  573. hs1:='1'
  574. else
  575. hs1:='0';
  576. until false;
  577. read_term:=hs1;
  578. end;
  579. function read_simple_expr : string;
  580. var
  581. hs1,hs2 : string;
  582. l1,l2 : longint;
  583. w : integer;
  584. begin
  585. hs1:=read_term;
  586. repeat
  587. if (current_scanner.preproc_token<>_ID) then
  588. break;
  589. if current_scanner.preproc_pattern<>'OR' then
  590. break;
  591. preproc_consume(_ID);
  592. hs2:=read_term;
  593. valint(hs1,l1,w);
  594. valint(hs2,l2,w);
  595. if (l1<>0) or (l2<>0) then
  596. hs1:='1'
  597. else
  598. hs1:='0';
  599. until false;
  600. read_simple_expr:=hs1;
  601. end;
  602. function read_expr : string;
  603. var
  604. hs1,hs2 : string;
  605. b : boolean;
  606. t : ttoken;
  607. w : integer;
  608. l1,l2 : longint;
  609. begin
  610. hs1:=read_simple_expr;
  611. t:=current_scanner.preproc_token;
  612. if not(t in [_EQUAL,_UNEQUAL,_LT,_GT,_LTE,_GTE]) then
  613. begin
  614. read_expr:=hs1;
  615. exit;
  616. end;
  617. preproc_consume(t);
  618. hs2:=read_simple_expr;
  619. if is_number(hs1) and is_number(hs2) then
  620. begin
  621. valint(hs1,l1,w);
  622. valint(hs2,l2,w);
  623. case t of
  624. _EQUAL : b:=l1=l2;
  625. _UNEQUAL : b:=l1<>l2;
  626. _LT : b:=l1<l2;
  627. _GT : b:=l1>l2;
  628. _GTE : b:=l1>=l2;
  629. _LTE : b:=l1<=l2;
  630. end;
  631. end
  632. else
  633. begin
  634. case t of
  635. _EQUAL : b:=hs1=hs2;
  636. _UNEQUAL : b:=hs1<>hs2;
  637. _LT : b:=hs1<hs2;
  638. _GT : b:=hs1>hs2;
  639. _GTE : b:=hs1>=hs2;
  640. _LTE : b:=hs1<=hs2;
  641. end;
  642. end;
  643. if b then
  644. read_expr:='1'
  645. else
  646. read_expr:='0';
  647. end;
  648. begin
  649. current_scanner.skipspace;
  650. { start preproc expression scanner }
  651. current_scanner.preproc_token:=current_scanner.readpreproc;
  652. parse_compiler_expr:=read_expr;
  653. end;
  654. procedure dir_if;
  655. var
  656. hs : string;
  657. begin
  658. hs:=parse_compiler_expr;
  659. current_scanner.addpreprocstack(pp_if,hs<>'0',hs,scan_c_if_found);
  660. end;
  661. procedure dir_elseif;
  662. var
  663. hs : string;
  664. begin
  665. hs:=parse_compiler_expr;
  666. current_scanner.elseifpreprocstack(hs<>'0');
  667. end;
  668. procedure dir_define;
  669. var
  670. hs : string;
  671. bracketcount : longint;
  672. mac : tmacro;
  673. macropos : longint;
  674. macrobuffer : pmacrobuffer;
  675. begin
  676. current_scanner.skipspace;
  677. hs:=current_scanner.readid;
  678. mac:=tmacro(current_scanner.macros.search(hs));
  679. if not assigned(mac) then
  680. begin
  681. mac:=tmacro.create(hs);
  682. mac.defined:=true;
  683. Message1(parser_c_macro_defined,mac.name);
  684. current_scanner.macros.insert(mac);
  685. end
  686. else
  687. begin
  688. Message1(parser_c_macro_defined,mac.name);
  689. mac.defined:=true;
  690. mac.is_compiler_var:=false;
  691. { delete old definition }
  692. if assigned(mac.buftext) then
  693. begin
  694. freemem(mac.buftext,mac.buflen);
  695. mac.buftext:=nil;
  696. end;
  697. end;
  698. mac.is_used:=true;
  699. if (cs_support_macro in aktmoduleswitches) then
  700. begin
  701. { key words are never substituted }
  702. if is_keyword(hs) then
  703. Message(scan_e_keyword_cant_be_a_macro);
  704. { !!!!!! handle macro params, need we this? }
  705. current_scanner.skipspace;
  706. if not (m_mac in aktmodeswitches) then
  707. begin
  708. { may be a macro? }
  709. if c <> ':' then
  710. exit;
  711. current_scanner.readchar;
  712. if c <> '=' then
  713. exit;
  714. current_scanner.readchar;
  715. current_scanner.skipspace;
  716. end;
  717. new(macrobuffer);
  718. macropos:=0;
  719. { parse macro, brackets are counted so it's possible
  720. to have a $ifdef etc. in the macro }
  721. bracketcount:=0;
  722. repeat
  723. case c of
  724. '}' :
  725. if (bracketcount=0) then
  726. break
  727. else
  728. dec(bracketcount);
  729. '{' :
  730. inc(bracketcount);
  731. #10,#13 :
  732. current_scanner.linebreak;
  733. #26 :
  734. current_scanner.end_of_file;
  735. end;
  736. macrobuffer^[macropos]:=c;
  737. inc(macropos);
  738. if macropos>maxmacrolen then
  739. Message(scan_f_macro_buffer_overflow);
  740. current_scanner.readchar;
  741. until false;
  742. { free buffer of macro ?}
  743. if assigned(mac.buftext) then
  744. freemem(mac.buftext,mac.buflen);
  745. { get new mem }
  746. getmem(mac.buftext,macropos);
  747. mac.buflen:=macropos;
  748. { copy the text }
  749. move(macrobuffer^,mac.buftext^,macropos);
  750. dispose(macrobuffer);
  751. end
  752. else
  753. begin
  754. { check if there is an assignment, then we need to give a
  755. warning }
  756. current_scanner.skipspace;
  757. if c=':' then
  758. begin
  759. current_scanner.readchar;
  760. if c='=' then
  761. Message(scan_w_macro_support_turned_off);
  762. end;
  763. end;
  764. end;
  765. procedure dir_setc;
  766. var
  767. hs : string;
  768. mac : tmacro;
  769. begin
  770. current_scanner.skipspace;
  771. hs:=current_scanner.readid;
  772. mac:=tmacro(current_scanner.macros.search(hs));
  773. if not assigned(mac) then
  774. begin
  775. mac:=tmacro.create(hs);
  776. mac.defined:=true;
  777. mac.is_compiler_var:=true;
  778. Message1(parser_c_macro_defined,mac.name);
  779. current_scanner.macros.insert(mac);
  780. end
  781. else
  782. begin
  783. mac.defined:=true;
  784. mac.is_compiler_var:=true;
  785. { delete old definition }
  786. if assigned(mac.buftext) then
  787. begin
  788. freemem(mac.buftext,mac.buflen);
  789. mac.buftext:=nil;
  790. end;
  791. end;
  792. mac.is_used:=true;
  793. { key words are never substituted }
  794. if is_keyword(hs) then
  795. Message(scan_e_keyword_cant_be_a_macro);
  796. { !!!!!! handle macro params, need we this? }
  797. current_scanner.skipspace;
  798. { may be a macro? }
  799. { assignment can be both := and = }
  800. if c=':' then
  801. current_scanner.readchar;
  802. if c='=' then
  803. begin
  804. current_scanner.readchar;
  805. hs:= parse_compiler_expr;
  806. if length(hs) <> 0 then
  807. begin
  808. Message2(parser_c_macro_set_to,mac.name,hs);
  809. { free buffer of macro ?}
  810. if assigned(mac.buftext) then
  811. freemem(mac.buftext,mac.buflen);
  812. { get new mem }
  813. getmem(mac.buftext,length(hs));
  814. mac.buflen:=length(hs);
  815. { copy the text }
  816. move(hs[1],mac.buftext^,mac.buflen);
  817. end
  818. else
  819. Message(scan_e_preproc_syntax_error);
  820. end
  821. else
  822. Message(scan_e_preproc_syntax_error);
  823. end;
  824. procedure dir_undef;
  825. var
  826. hs : string;
  827. mac : tmacro;
  828. begin
  829. current_scanner.skipspace;
  830. hs:=current_scanner.readid;
  831. mac:=tmacro(current_scanner.macros.search(hs));
  832. if not assigned(mac) then
  833. begin
  834. mac:=tmacro.create(hs);
  835. Message1(parser_c_macro_undefined,mac.name);
  836. mac.defined:=false;
  837. current_scanner.macros.insert(mac);
  838. end
  839. else
  840. begin
  841. Message1(parser_c_macro_undefined,mac.name);
  842. mac.defined:=false;
  843. mac.is_compiler_var:=false;
  844. { delete old definition }
  845. if assigned(mac.buftext) then
  846. begin
  847. freemem(mac.buftext,mac.buflen);
  848. mac.buftext:=nil;
  849. end;
  850. end;
  851. mac.is_used:=true;
  852. end;
  853. procedure dir_include;
  854. function findincludefile(const path,name,ext:string;var foundfile:string):boolean;
  855. var
  856. found : boolean;
  857. hpath : string;
  858. begin
  859. { look for the include file
  860. 1. specified path,path of current inputfile,current dir
  861. 2. local includepath
  862. 3. global includepath }
  863. found:=false;
  864. foundfile:='';
  865. hpath:='';
  866. if path<>'' then
  867. begin
  868. if not path_absolute(path) then
  869. hpath:=current_scanner.inputfile.path^+path
  870. else
  871. hpath:=path+';'+current_scanner.inputfile.path^;
  872. end
  873. else
  874. hpath:=current_scanner.inputfile.path^;
  875. found:=FindFile(name+ext,hpath+';.'+source_info.DirSep,foundfile);
  876. if (not found) then
  877. found:=current_module.localincludesearchpath.FindFile(name+ext,foundfile);
  878. if (not found) then
  879. found:=includesearchpath.FindFile(name+ext,foundfile);
  880. findincludefile:=found;
  881. end;
  882. var
  883. args,
  884. foundfile,
  885. hs : string;
  886. path : String;
  887. name : String;
  888. ext : String;
  889. hp : tinputfile;
  890. found : boolean;
  891. begin
  892. current_scanner.skipspace;
  893. args:=current_scanner.readcomment;
  894. hs:=GetToken(args,' ');
  895. if hs='' then
  896. exit;
  897. if (hs[1]='%') then
  898. begin
  899. { case insensitive }
  900. hs:=upper(hs);
  901. { remove %'s }
  902. Delete(hs,1,1);
  903. if hs[length(hs)]='%' then
  904. Delete(hs,length(hs),1);
  905. { save old }
  906. path:=hs;
  907. { first check for internal macros }
  908. if hs='TIME' then
  909. hs:=gettimestr
  910. else
  911. if hs='DATE' then
  912. hs:=getdatestr
  913. else
  914. if hs='FILE' then
  915. hs:=current_module.sourcefiles.get_file_name(aktfilepos.fileindex)
  916. else
  917. if hs='LINE' then
  918. hs:=tostr(aktfilepos.line)
  919. else
  920. if hs='FPCVERSION' then
  921. hs:=version_string
  922. else
  923. if hs='FPCTARGET' then
  924. hs:=target_cpu_string
  925. else
  926. if hs='FPCTARGETCPU' then
  927. hs:=target_cpu_string
  928. else
  929. if hs='FPCTARGETOS' then
  930. hs:=target_info.shortname
  931. else
  932. hs:=GetEnvironmentVariable(hs);
  933. if hs='' then
  934. Message1(scan_w_include_env_not_found,path);
  935. { make it a stringconst }
  936. hs:=''''+hs+'''';
  937. current_scanner.insertmacro(path,@hs[1],length(hs),
  938. current_scanner.line_no,current_scanner.inputfile.ref_index);
  939. end
  940. else
  941. begin
  942. hs:=FixFileName(hs);
  943. path := SplitPath(hs);
  944. name := SplitName(hs);
  945. ext := SplitExtension(hs);
  946. { try to find the file }
  947. found:=findincludefile(path,name,ext,foundfile);
  948. if (ext='') then
  949. begin
  950. { try default extensions .inc , .pp and .pas }
  951. if (not found) then
  952. found:=findincludefile(path,name,'.inc',foundfile);
  953. if (not found) then
  954. found:=findincludefile(path,name,target_info.sourceext,foundfile);
  955. if (not found) then
  956. found:=findincludefile(path,name,target_info.pasext,foundfile);
  957. end;
  958. if current_scanner.inputfilecount<max_include_nesting then
  959. begin
  960. inc(current_scanner.inputfilecount);
  961. { we need to reread the current char }
  962. dec(current_scanner.inputpointer);
  963. { shutdown current file }
  964. current_scanner.tempcloseinputfile;
  965. { load new file }
  966. hp:=do_openinputfile(foundfile);
  967. current_scanner.addfile(hp);
  968. current_module.sourcefiles.register_file(hp);
  969. if not current_scanner.openinputfile then
  970. Message1(scan_f_cannot_open_includefile,hs);
  971. Message1(scan_t_start_include_file,current_scanner.inputfile.path^+current_scanner.inputfile.name^);
  972. current_scanner.reload;
  973. end
  974. else
  975. Message(scan_f_include_deep_ten);
  976. end;
  977. end;
  978. {*****************************************************************************
  979. TMacro
  980. *****************************************************************************}
  981. constructor tmacro.create(const n : string);
  982. begin
  983. inherited createname(n);
  984. defined:=true;
  985. defined_at_startup:=false;
  986. fileinfo:=akttokenpos;
  987. is_used:=false;
  988. is_compiler_var:= false;
  989. buftext:=nil;
  990. buflen:=0;
  991. end;
  992. destructor tmacro.destroy;
  993. begin
  994. if assigned(buftext) then
  995. freemem(buftext,buflen);
  996. inherited destroy;
  997. end;
  998. {*****************************************************************************
  999. Preprocessor writting
  1000. *****************************************************************************}
  1001. {$ifdef PREPROCWRITE}
  1002. constructor tpreprocfile.create(const fn:string);
  1003. begin
  1004. { open outputfile }
  1005. assign(f,fn);
  1006. {$I-}
  1007. rewrite(f);
  1008. {$I+}
  1009. if ioresult<>0 then
  1010. Comment(V_Fatal,'can''t create file '+fn);
  1011. getmem(buf,preprocbufsize);
  1012. settextbuf(f,buf^,preprocbufsize);
  1013. { reset }
  1014. eolfound:=false;
  1015. spacefound:=false;
  1016. end;
  1017. destructor tpreprocfile.destroy;
  1018. begin
  1019. close(f);
  1020. freemem(buf,preprocbufsize);
  1021. end;
  1022. procedure tpreprocfile.add(const s:string);
  1023. begin
  1024. write(f,s);
  1025. end;
  1026. procedure tpreprocfile.addspace;
  1027. begin
  1028. if eolfound then
  1029. begin
  1030. writeln(f,'');
  1031. eolfound:=false;
  1032. spacefound:=false;
  1033. end
  1034. else
  1035. if spacefound then
  1036. begin
  1037. write(f,' ');
  1038. spacefound:=false;
  1039. end;
  1040. end;
  1041. {$endif PREPROCWRITE}
  1042. {*****************************************************************************
  1043. TPreProcStack
  1044. *****************************************************************************}
  1045. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  1046. begin
  1047. accept:=a;
  1048. typ:=atyp;
  1049. next:=n;
  1050. end;
  1051. {*****************************************************************************
  1052. TDirectiveItem
  1053. *****************************************************************************}
  1054. constructor TDirectiveItem.Create(const n:string;p:tdirectiveproc);
  1055. begin
  1056. inherited CreateName(n);
  1057. is_conditional:=false;
  1058. proc:={$ifndef FPCPROCVAR}@{$endif}p;
  1059. end;
  1060. constructor TDirectiveItem.CreateCond(const n:string;p:tdirectiveproc);
  1061. begin
  1062. inherited CreateName(n);
  1063. is_conditional:=true;
  1064. proc:={$ifndef FPCPROCVAR}@{$endif}p;
  1065. end;
  1066. {****************************************************************************
  1067. TSCANNERFILE
  1068. ****************************************************************************}
  1069. constructor tscannerfile.create(const fn:string);
  1070. begin
  1071. inputfile:=do_openinputfile(fn);
  1072. if assigned(current_module) then
  1073. current_module.sourcefiles.register_file(inputfile);
  1074. { reset localinput }
  1075. inputbuffer:=nil;
  1076. inputpointer:=nil;
  1077. inputstart:=0;
  1078. { reset scanner }
  1079. preprocstack:=nil;
  1080. comment_level:=0;
  1081. yylexcount:=0;
  1082. block_type:=bt_general;
  1083. line_no:=0;
  1084. lastlinepos:=0;
  1085. lasttokenpos:=0;
  1086. lasttoken:=NOTOKEN;
  1087. nexttoken:=NOTOKEN;
  1088. lastasmgetchar:=#0;
  1089. ignoredirectives:=TStringList.Create;
  1090. in_asm_string:=false;
  1091. macros:=tdictionary.create;
  1092. end;
  1093. procedure tscannerfile.firstfile;
  1094. begin
  1095. { load block }
  1096. if not openinputfile then
  1097. Message1(scan_f_cannot_open_input,inputfile.name^);
  1098. reload;
  1099. end;
  1100. destructor tscannerfile.destroy;
  1101. begin
  1102. if assigned(current_module) and
  1103. (current_module.state=ms_compiled) and
  1104. (status.errorcount=0) then
  1105. checkpreprocstack
  1106. else
  1107. begin
  1108. while assigned(preprocstack) do
  1109. poppreprocstack;
  1110. end;
  1111. if not inputfile.closed then
  1112. closeinputfile;
  1113. ignoredirectives.free;
  1114. macros.free;
  1115. end;
  1116. procedure tscannerfile.def_macro(const s : string);
  1117. var
  1118. mac : tmacro;
  1119. begin
  1120. mac:=tmacro(macros.search(s));
  1121. if mac=nil then
  1122. begin
  1123. mac:=tmacro.create(s);
  1124. Message1(parser_c_macro_defined,mac.name);
  1125. macros.insert(mac);
  1126. end;
  1127. mac.defined:=true;
  1128. mac.defined_at_startup:=true;
  1129. end;
  1130. procedure tscannerfile.set_macro(const s : string;value : string);
  1131. var
  1132. mac : tmacro;
  1133. begin
  1134. mac:=tmacro(macros.search(s));
  1135. if mac=nil then
  1136. begin
  1137. mac:=tmacro.create(s);
  1138. macros.insert(mac);
  1139. end
  1140. else
  1141. begin
  1142. mac.is_compiler_var:=false;
  1143. if assigned(mac.buftext) then
  1144. freemem(mac.buftext,mac.buflen);
  1145. end;
  1146. Message2(parser_c_macro_set_to,mac.name,value);
  1147. mac.buflen:=length(value);
  1148. getmem(mac.buftext,mac.buflen);
  1149. move(value[1],mac.buftext^,mac.buflen);
  1150. mac.defined:=true;
  1151. mac.defined_at_startup:=true;
  1152. end;
  1153. function tscannerfile.openinputfile:boolean;
  1154. begin
  1155. openinputfile:=inputfile.open;
  1156. { load buffer }
  1157. inputbuffer:=inputfile.buf;
  1158. inputpointer:=inputfile.buf;
  1159. inputstart:=inputfile.bufstart;
  1160. { line }
  1161. line_no:=0;
  1162. lastlinepos:=0;
  1163. lasttokenpos:=0;
  1164. end;
  1165. procedure tscannerfile.closeinputfile;
  1166. begin
  1167. inputfile.close;
  1168. { reset buffer }
  1169. inputbuffer:=nil;
  1170. inputpointer:=nil;
  1171. inputstart:=0;
  1172. { reset line }
  1173. line_no:=0;
  1174. lastlinepos:=0;
  1175. lasttokenpos:=0;
  1176. end;
  1177. function tscannerfile.tempopeninputfile:boolean;
  1178. begin
  1179. if inputfile.is_macro then
  1180. exit;
  1181. tempopeninputfile:=inputfile.tempopen;
  1182. { reload buffer }
  1183. inputbuffer:=inputfile.buf;
  1184. inputpointer:=inputfile.buf;
  1185. inputstart:=inputfile.bufstart;
  1186. end;
  1187. procedure tscannerfile.tempcloseinputfile;
  1188. begin
  1189. if inputfile.closed or inputfile.is_macro then
  1190. exit;
  1191. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  1192. inputfile.tempclose;
  1193. { reset buffer }
  1194. inputbuffer:=nil;
  1195. inputpointer:=nil;
  1196. inputstart:=0;
  1197. end;
  1198. procedure tscannerfile.saveinputfile;
  1199. begin
  1200. inputfile.saveinputpointer:=inputpointer;
  1201. inputfile.savelastlinepos:=lastlinepos;
  1202. inputfile.saveline_no:=line_no;
  1203. end;
  1204. procedure tscannerfile.restoreinputfile;
  1205. begin
  1206. inputpointer:=inputfile.saveinputpointer;
  1207. lastlinepos:=inputfile.savelastlinepos;
  1208. line_no:=inputfile.saveline_no;
  1209. if not inputfile.is_macro then
  1210. parser_current_file:=inputfile.name^;
  1211. end;
  1212. procedure tscannerfile.nextfile;
  1213. var
  1214. to_dispose : tinputfile;
  1215. begin
  1216. if assigned(inputfile.next) then
  1217. begin
  1218. if inputfile.is_macro then
  1219. to_dispose:=inputfile
  1220. else
  1221. begin
  1222. to_dispose:=nil;
  1223. dec(inputfilecount);
  1224. end;
  1225. { we can allways close the file, no ? }
  1226. inputfile.close;
  1227. inputfile:=inputfile.next;
  1228. if assigned(to_dispose) then
  1229. to_dispose.free;
  1230. restoreinputfile;
  1231. end;
  1232. end;
  1233. procedure tscannerfile.addfile(hp:tinputfile);
  1234. begin
  1235. saveinputfile;
  1236. { add to list }
  1237. hp.next:=inputfile;
  1238. inputfile:=hp;
  1239. { load new inputfile }
  1240. restoreinputfile;
  1241. end;
  1242. procedure tscannerfile.reload;
  1243. begin
  1244. with inputfile do
  1245. begin
  1246. { when nothing more to read then leave immediatly, so we
  1247. don't change the aktfilepos and leave it point to the last
  1248. char }
  1249. if (c=#26) and (not assigned(next)) then
  1250. exit;
  1251. repeat
  1252. { still more to read?, then change the #0 to a space so its seen
  1253. as a seperator, this can't be used for macro's which can change
  1254. the place of the #0 in the buffer with tempopen }
  1255. if (c=#0) and (bufsize>0) and
  1256. not(inputfile.is_macro) and
  1257. (inputpointer-inputbuffer<bufsize) then
  1258. begin
  1259. c:=' ';
  1260. inc(inputpointer);
  1261. exit;
  1262. end;
  1263. { can we read more from this file ? }
  1264. if (c<>#26) and (not endoffile) then
  1265. begin
  1266. readbuf;
  1267. inputpointer:=buf;
  1268. inputbuffer:=buf;
  1269. inputstart:=bufstart;
  1270. { first line? }
  1271. if line_no=0 then
  1272. begin
  1273. line_no:=1;
  1274. if cs_asm_source in aktglobalswitches then
  1275. inputfile.setline(line_no,bufstart);
  1276. end;
  1277. end
  1278. else
  1279. begin
  1280. { load eof position in tokenpos/aktfilepos }
  1281. gettokenpos;
  1282. { close file }
  1283. closeinputfile;
  1284. { no next module, than EOF }
  1285. if not assigned(inputfile.next) then
  1286. begin
  1287. c:=#26;
  1288. exit;
  1289. end;
  1290. { load next file and reopen it }
  1291. nextfile;
  1292. tempopeninputfile;
  1293. { status }
  1294. Message1(scan_t_back_in,inputfile.name^);
  1295. end;
  1296. { load next char }
  1297. c:=inputpointer^;
  1298. inc(inputpointer);
  1299. until c<>#0; { if also end, then reload again }
  1300. end;
  1301. end;
  1302. procedure tscannerfile.insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  1303. var
  1304. hp : tinputfile;
  1305. begin
  1306. { save old postion }
  1307. dec(inputpointer);
  1308. tempcloseinputfile;
  1309. { create macro 'file' }
  1310. { use special name to dispose after !! }
  1311. hp:=do_openinputfile('_Macro_.'+macname);
  1312. addfile(hp);
  1313. with inputfile do
  1314. begin
  1315. setmacro(p,len);
  1316. { local buffer }
  1317. inputbuffer:=buf;
  1318. inputpointer:=buf;
  1319. inputstart:=bufstart;
  1320. ref_index:=fileindex;
  1321. end;
  1322. { reset line }
  1323. line_no:=line;
  1324. lastlinepos:=0;
  1325. lasttokenpos:=0;
  1326. { load new c }
  1327. c:=inputpointer^;
  1328. inc(inputpointer);
  1329. end;
  1330. procedure tscannerfile.gettokenpos;
  1331. { load the values of tokenpos and lasttokenpos }
  1332. begin
  1333. lasttokenpos:=inputstart+(inputpointer-inputbuffer);
  1334. akttokenpos.line:=line_no;
  1335. akttokenpos.column:=lasttokenpos-lastlinepos;
  1336. akttokenpos.fileindex:=inputfile.ref_index;
  1337. aktfilepos:=akttokenpos;
  1338. end;
  1339. procedure tscannerfile.inc_comment_level;
  1340. var
  1341. oldaktfilepos : tfileposinfo;
  1342. begin
  1343. if (m_nested_comment in aktmodeswitches) then
  1344. inc(comment_level)
  1345. else
  1346. comment_level:=1;
  1347. if (comment_level>1) then
  1348. begin
  1349. oldaktfilepos:=aktfilepos;
  1350. gettokenpos; { update for warning }
  1351. Message1(scan_w_comment_level,tostr(comment_level));
  1352. aktfilepos:=oldaktfilepos;
  1353. end;
  1354. end;
  1355. procedure tscannerfile.dec_comment_level;
  1356. begin
  1357. if (m_nested_comment in aktmodeswitches) then
  1358. dec(comment_level)
  1359. else
  1360. comment_level:=0;
  1361. end;
  1362. procedure tscannerfile.linebreak;
  1363. var
  1364. cur : char;
  1365. oldtokenpos,
  1366. oldaktfilepos : tfileposinfo;
  1367. begin
  1368. with inputfile do
  1369. begin
  1370. if (byte(inputpointer^)=0) and not(endoffile) then
  1371. begin
  1372. cur:=c;
  1373. reload;
  1374. if byte(cur)+byte(c)<>23 then
  1375. dec(inputpointer);
  1376. end
  1377. else
  1378. begin
  1379. { Support all combination of #10 and #13 as line break }
  1380. if (byte(inputpointer^)+byte(c)=23) then
  1381. inc(inputpointer);
  1382. end;
  1383. { Always return #10 as line break }
  1384. c:=#10;
  1385. { increase line counters }
  1386. lastlinepos:=bufstart+(inputpointer-inputbuffer);
  1387. inc(line_no);
  1388. { update linebuffer }
  1389. if cs_asm_source in aktglobalswitches then
  1390. inputfile.setline(line_no,lastlinepos);
  1391. { update for status and call the show status routine,
  1392. but don't touch aktfilepos ! }
  1393. oldaktfilepos:=aktfilepos;
  1394. oldtokenpos:=akttokenpos;
  1395. gettokenpos; { update for v_status }
  1396. inc(status.compiledlines);
  1397. ShowStatus;
  1398. aktfilepos:=oldaktfilepos;
  1399. akttokenpos:=oldtokenpos;
  1400. end;
  1401. end;
  1402. procedure tscannerfile.illegal_char(c:char);
  1403. var
  1404. s : string;
  1405. begin
  1406. if c in [#32..#255] then
  1407. s:=''''+c+''''
  1408. else
  1409. s:='#'+tostr(ord(c));
  1410. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  1411. end;
  1412. procedure tscannerfile.end_of_file;
  1413. begin
  1414. checkpreprocstack;
  1415. Message(scan_f_end_of_file);
  1416. end;
  1417. {-------------------------------------------
  1418. IF Conditional Handling
  1419. -------------------------------------------}
  1420. procedure tscannerfile.checkpreprocstack;
  1421. begin
  1422. { check for missing ifdefs }
  1423. while assigned(preprocstack) do
  1424. begin
  1425. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  1426. preprocstack.owner.inputfile.name^,tostr(preprocstack.line_nb));
  1427. poppreprocstack;
  1428. end;
  1429. end;
  1430. procedure tscannerfile.poppreprocstack;
  1431. var
  1432. hp : tpreprocstack;
  1433. begin
  1434. if assigned(preprocstack) then
  1435. begin
  1436. Message1(scan_c_endif_found,preprocstack.name);
  1437. hp:=preprocstack.next;
  1438. preprocstack.free;
  1439. preprocstack:=hp;
  1440. end
  1441. else
  1442. Message(scan_e_endif_without_if);
  1443. end;
  1444. procedure tscannerfile.addpreprocstack(atyp : preproctyp;a:boolean;const s:string;w:longint);
  1445. begin
  1446. preprocstack:=tpreprocstack.create(atyp,((preprocstack=nil) or preprocstack.accept) and a,preprocstack);
  1447. preprocstack.name:=s;
  1448. preprocstack.line_nb:=line_no;
  1449. preprocstack.owner:=self;
  1450. if preprocstack.accept then
  1451. Message2(w,preprocstack.name,'accepted')
  1452. else
  1453. Message2(w,preprocstack.name,'rejected');
  1454. end;
  1455. procedure tscannerfile.elsepreprocstack;
  1456. begin
  1457. if assigned(preprocstack) and
  1458. (preprocstack.typ<>pp_else) then
  1459. begin
  1460. if (preprocstack.typ=pp_elseif) then
  1461. preprocstack.accept:=false
  1462. else
  1463. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  1464. preprocstack.accept:=not preprocstack.accept;
  1465. preprocstack.typ:=pp_else;
  1466. preprocstack.line_nb:=line_no;
  1467. if preprocstack.accept then
  1468. Message2(scan_c_else_found,preprocstack.name,'accepted')
  1469. else
  1470. Message2(scan_c_else_found,preprocstack.name,'rejected');
  1471. end
  1472. else
  1473. Message(scan_e_endif_without_if);
  1474. end;
  1475. procedure tscannerfile.elseifpreprocstack(accept:boolean);
  1476. begin
  1477. if assigned(preprocstack) and
  1478. (preprocstack.typ in [pp_if,pp_elseif]) then
  1479. begin
  1480. { when the branch is accepted we use pp_elseif so we know that
  1481. all the next branches need to be rejected. when this branch is still
  1482. not accepted then leave it at pp_if }
  1483. if (preprocstack.typ=pp_elseif) then
  1484. preprocstack.accept:=false
  1485. else
  1486. if (preprocstack.typ=pp_if) and preprocstack.accept then
  1487. begin
  1488. preprocstack.accept:=false;
  1489. preprocstack.typ:=pp_elseif;
  1490. end
  1491. else
  1492. if accept and
  1493. (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  1494. begin
  1495. preprocstack.accept:=true;
  1496. preprocstack.typ:=pp_elseif;
  1497. end;
  1498. preprocstack.line_nb:=line_no;
  1499. if preprocstack.accept then
  1500. Message2(scan_c_else_found,preprocstack.name,'accepted')
  1501. else
  1502. Message2(scan_c_else_found,preprocstack.name,'rejected');
  1503. end
  1504. else
  1505. Message(scan_e_endif_without_if);
  1506. end;
  1507. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  1508. var
  1509. oldaktfilepos : tfileposinfo;
  1510. begin
  1511. oldaktfilepos:=aktfilepos;
  1512. repeat
  1513. current_scanner.gettokenpos;
  1514. p.proc{$ifdef FPCPROCVAR}(){$endif};
  1515. { accept the text ? }
  1516. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  1517. break
  1518. else
  1519. begin
  1520. current_scanner.gettokenpos;
  1521. Message(scan_c_skipping_until);
  1522. repeat
  1523. current_scanner.skipuntildirective;
  1524. if not (m_mac in aktmodeswitches) then
  1525. p:=tdirectiveitem(turbo_scannerdirectives.search(current_scanner.readid))
  1526. else
  1527. p:=tdirectiveitem(mac_scannerdirectives.search(current_scanner.readid));
  1528. until assigned(p) and (p.is_conditional);
  1529. current_scanner.gettokenpos;
  1530. Message1(scan_d_handling_switch,'$'+p.name);
  1531. end;
  1532. until false;
  1533. aktfilepos:=oldaktfilepos;
  1534. end;
  1535. procedure tscannerfile.handledirectives;
  1536. var
  1537. t : tdirectiveitem;
  1538. hs : string;
  1539. begin
  1540. gettokenpos;
  1541. readchar; {Remove the $}
  1542. hs:=readid;
  1543. {$ifdef PREPROCWRITE}
  1544. if parapreprocess then
  1545. begin
  1546. t:=Get_Directive(hs);
  1547. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  1548. begin
  1549. preprocfile^.AddSpace;
  1550. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  1551. exit;
  1552. end;
  1553. end;
  1554. {$endif PREPROCWRITE}
  1555. { skip this directive? }
  1556. if (ignoredirectives.find(hs)<>nil) then
  1557. begin
  1558. if (comment_level>0) then
  1559. readcomment;
  1560. { we've read the whole comment }
  1561. aktcommentstyle:=comment_none;
  1562. exit;
  1563. end;
  1564. if hs='' then
  1565. begin
  1566. Message1(scan_w_illegal_switch,'$'+hs);
  1567. end;
  1568. { Check for compiler switches }
  1569. while (length(hs)=1) and (c in ['-','+']) do
  1570. begin
  1571. HandleSwitch(hs[1],c);
  1572. current_scanner.readchar; {Remove + or -}
  1573. if c=',' then
  1574. begin
  1575. current_scanner.readchar; {Remove , }
  1576. { read next switch, support $v+,$+}
  1577. hs:=current_scanner.readid;
  1578. if (hs='') then
  1579. begin
  1580. if (c='$') and (m_fpc in aktmodeswitches) then
  1581. begin
  1582. current_scanner.readchar; { skip $ }
  1583. hs:=current_scanner.readid;
  1584. end;
  1585. if (hs='') then
  1586. Message1(scan_w_illegal_directive,'$'+c);
  1587. end
  1588. else
  1589. Message1(scan_d_handling_switch,'$'+hs);
  1590. end
  1591. else
  1592. hs:='';
  1593. end;
  1594. { directives may follow switches after a , }
  1595. if hs<>'' then
  1596. begin
  1597. if not (m_mac in aktmodeswitches) then
  1598. t:=tdirectiveitem(turbo_scannerdirectives.search(hs))
  1599. else
  1600. t:=tdirectiveitem(mac_scannerdirectives.search(hs));
  1601. if assigned(t) then
  1602. begin
  1603. if t.is_conditional then
  1604. handleconditional(t)
  1605. else
  1606. begin
  1607. Message1(scan_d_handling_switch,'$'+hs);
  1608. t.proc{$ifdef FPCPROCVAR}(){$endif};
  1609. end;
  1610. end
  1611. else
  1612. begin
  1613. current_scanner.ignoredirectives.insert(hs);
  1614. Message1(scan_w_illegal_directive,'$'+hs);
  1615. end;
  1616. { conditionals already read the comment }
  1617. if (current_scanner.comment_level>0) then
  1618. current_scanner.readcomment;
  1619. { we've read the whole comment }
  1620. aktcommentstyle:=comment_none;
  1621. end;
  1622. end;
  1623. procedure tscannerfile.readchar;
  1624. begin
  1625. c:=inputpointer^;
  1626. if c=#0 then
  1627. reload
  1628. else
  1629. inc(inputpointer);
  1630. end;
  1631. procedure tscannerfile.readstring;
  1632. var
  1633. i : longint;
  1634. begin
  1635. i:=0;
  1636. repeat
  1637. case c of
  1638. '_',
  1639. '0'..'9',
  1640. 'A'..'Z' :
  1641. begin
  1642. if i<255 then
  1643. begin
  1644. inc(i);
  1645. orgpattern[i]:=c;
  1646. pattern[i]:=c;
  1647. end;
  1648. c:=inputpointer^;
  1649. inc(inputpointer);
  1650. end;
  1651. 'a'..'z' :
  1652. begin
  1653. if i<255 then
  1654. begin
  1655. inc(i);
  1656. orgpattern[i]:=c;
  1657. pattern[i]:=chr(ord(c)-32)
  1658. end;
  1659. c:=inputpointer^;
  1660. inc(inputpointer);
  1661. end;
  1662. #0 :
  1663. reload;
  1664. else
  1665. break;
  1666. end;
  1667. until false;
  1668. orgpattern[0]:=chr(i);
  1669. pattern[0]:=chr(i);
  1670. end;
  1671. procedure tscannerfile.readnumber;
  1672. var
  1673. base,
  1674. i : longint;
  1675. begin
  1676. case c of
  1677. '%' :
  1678. begin
  1679. readchar;
  1680. base:=2;
  1681. pattern[1]:='%';
  1682. i:=1;
  1683. end;
  1684. '&' :
  1685. begin
  1686. readchar;
  1687. base:=8;
  1688. pattern[1]:='&';
  1689. i:=1;
  1690. end;
  1691. '$' :
  1692. begin
  1693. readchar;
  1694. base:=16;
  1695. pattern[1]:='$';
  1696. i:=1;
  1697. end;
  1698. else
  1699. begin
  1700. base:=10;
  1701. i:=0;
  1702. end;
  1703. end;
  1704. while ((base>=10) and (c in ['0'..'9'])) or
  1705. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  1706. ((base=8) and (c in ['0'..'7'])) or
  1707. ((base=2) and (c in ['0'..'1'])) do
  1708. begin
  1709. if i<255 then
  1710. begin
  1711. inc(i);
  1712. pattern[i]:=c;
  1713. end;
  1714. readchar;
  1715. end;
  1716. pattern[0]:=chr(i);
  1717. end;
  1718. function tscannerfile.readid:string;
  1719. begin
  1720. readstring;
  1721. readid:=pattern;
  1722. end;
  1723. function tscannerfile.readval:longint;
  1724. var
  1725. l : longint;
  1726. w : integer;
  1727. begin
  1728. readnumber;
  1729. valint(pattern,l,w);
  1730. readval:=l;
  1731. end;
  1732. function tscannerfile.readval_asstring:string;
  1733. begin
  1734. readnumber;
  1735. readval_asstring:=pattern;
  1736. end;
  1737. function tscannerfile.readcomment:string;
  1738. var
  1739. i : longint;
  1740. begin
  1741. i:=0;
  1742. repeat
  1743. case c of
  1744. '{' :
  1745. begin
  1746. if aktcommentstyle=comment_tp then
  1747. inc_comment_level;
  1748. end;
  1749. '}' :
  1750. begin
  1751. if aktcommentstyle=comment_tp then
  1752. begin
  1753. readchar;
  1754. dec_comment_level;
  1755. if comment_level=0 then
  1756. break
  1757. else
  1758. continue;
  1759. end;
  1760. end;
  1761. '*' :
  1762. begin
  1763. if aktcommentstyle=comment_oldtp then
  1764. begin
  1765. readchar;
  1766. if c=')' then
  1767. begin
  1768. readchar;
  1769. dec_comment_level;
  1770. break;
  1771. end
  1772. else
  1773. { Add both characters !!}
  1774. if (i<255) then
  1775. begin
  1776. inc(i);
  1777. readcomment[i]:='*';
  1778. if (i<255) then
  1779. begin
  1780. inc(i);
  1781. readcomment[i]:='*';
  1782. end;
  1783. end;
  1784. end
  1785. else
  1786. { Not old TP comment, so add...}
  1787. begin
  1788. if (i<255) then
  1789. begin
  1790. inc(i);
  1791. readcomment[i]:='*';
  1792. end;
  1793. end;
  1794. end;
  1795. #10,#13 :
  1796. linebreak;
  1797. #26 :
  1798. end_of_file;
  1799. else
  1800. begin
  1801. if (i<255) then
  1802. begin
  1803. inc(i);
  1804. readcomment[i]:=c;
  1805. end;
  1806. end;
  1807. end;
  1808. readchar;
  1809. until false;
  1810. readcomment[0]:=chr(i);
  1811. end;
  1812. function tscannerfile.readquotedstring:string;
  1813. var
  1814. i : longint;
  1815. msgwritten : boolean;
  1816. begin
  1817. i:=0;
  1818. msgwritten:=false;
  1819. if (c='''') then
  1820. begin
  1821. repeat
  1822. readchar;
  1823. case c of
  1824. #26 :
  1825. end_of_file;
  1826. #10,#13 :
  1827. Message(scan_f_string_exceeds_line);
  1828. '''' :
  1829. begin
  1830. readchar;
  1831. if c<>'''' then
  1832. break;
  1833. end;
  1834. end;
  1835. if i<255 then
  1836. begin
  1837. inc(i);
  1838. result[i]:=c;
  1839. end
  1840. else
  1841. begin
  1842. if not msgwritten then
  1843. begin
  1844. Message(scan_e_string_exceeds_255_chars);
  1845. msgwritten:=true;
  1846. end;
  1847. end;
  1848. until false;
  1849. end;
  1850. result[0]:=chr(i);
  1851. end;
  1852. function tscannerfile.readstate:char;
  1853. var
  1854. state : char;
  1855. begin
  1856. state:=' ';
  1857. if c=' ' then
  1858. begin
  1859. current_scanner.skipspace;
  1860. current_scanner.readid;
  1861. if pattern='ON' then
  1862. state:='+'
  1863. else
  1864. if pattern='OFF' then
  1865. state:='-';
  1866. end
  1867. else
  1868. state:=c;
  1869. if not (state in ['+','-']) then
  1870. Message(scan_e_wrong_switch_toggle);
  1871. readstate:=state;
  1872. end;
  1873. procedure tscannerfile.skipspace;
  1874. begin
  1875. repeat
  1876. case c of
  1877. #26 :
  1878. begin
  1879. reload;
  1880. if (c=#26) and not assigned(inputfile.next) then
  1881. break;
  1882. continue;
  1883. end;
  1884. #10,
  1885. #13 :
  1886. linebreak;
  1887. #9,#11,#12,' ' :
  1888. ;
  1889. else
  1890. break;
  1891. end;
  1892. readchar;
  1893. until false;
  1894. end;
  1895. procedure tscannerfile.skipuntildirective;
  1896. var
  1897. found : longint;
  1898. next_char_loaded : boolean;
  1899. begin
  1900. found:=0;
  1901. next_char_loaded:=false;
  1902. repeat
  1903. case c of
  1904. #10,
  1905. #13 :
  1906. linebreak;
  1907. #26 :
  1908. begin
  1909. reload;
  1910. if (c=#26) and not assigned(inputfile.next) then
  1911. end_of_file;
  1912. continue;
  1913. end;
  1914. '{' :
  1915. begin
  1916. if (aktcommentstyle in [comment_tp,comment_none]) then
  1917. begin
  1918. aktcommentstyle:=comment_tp;
  1919. if (comment_level=0) then
  1920. found:=1;
  1921. inc_comment_level;
  1922. end;
  1923. end;
  1924. '*' :
  1925. begin
  1926. if (aktcommentstyle=comment_oldtp) then
  1927. begin
  1928. readchar;
  1929. if c=')' then
  1930. begin
  1931. dec_comment_level;
  1932. found:=0;
  1933. aktcommentstyle:=comment_none;
  1934. end
  1935. else
  1936. next_char_loaded:=true;
  1937. end
  1938. else
  1939. found := 0;
  1940. end;
  1941. '}' :
  1942. begin
  1943. if (aktcommentstyle=comment_tp) then
  1944. begin
  1945. dec_comment_level;
  1946. if (comment_level=0) then
  1947. aktcommentstyle:=comment_none;
  1948. found:=0;
  1949. end;
  1950. end;
  1951. '$' :
  1952. begin
  1953. if found=1 then
  1954. found:=2;
  1955. end;
  1956. '''' :
  1957. if (aktcommentstyle=comment_none) then
  1958. begin
  1959. repeat
  1960. readchar;
  1961. case c of
  1962. #26 :
  1963. end_of_file;
  1964. #10,#13 :
  1965. break;
  1966. '''' :
  1967. begin
  1968. readchar;
  1969. if c<>'''' then
  1970. begin
  1971. next_char_loaded:=true;
  1972. break;
  1973. end;
  1974. end;
  1975. end;
  1976. until false;
  1977. end;
  1978. '(' :
  1979. begin
  1980. if (aktcommentstyle=comment_none) then
  1981. begin
  1982. readchar;
  1983. if c='*' then
  1984. begin
  1985. readchar;
  1986. if c='$' then
  1987. begin
  1988. found:=2;
  1989. inc_comment_level;
  1990. aktcommentstyle:=comment_oldtp;
  1991. end
  1992. else
  1993. begin
  1994. skipoldtpcomment;
  1995. next_char_loaded:=true;
  1996. end;
  1997. end
  1998. else
  1999. next_char_loaded:=true;
  2000. end
  2001. else
  2002. found:=0;
  2003. end;
  2004. '/' :
  2005. begin
  2006. if (aktcommentstyle=comment_none) then
  2007. begin
  2008. readchar;
  2009. if c='/' then
  2010. skipdelphicomment;
  2011. next_char_loaded:=true;
  2012. end
  2013. else
  2014. found:=0;
  2015. end;
  2016. else
  2017. found:=0;
  2018. end;
  2019. if next_char_loaded then
  2020. next_char_loaded:=false
  2021. else
  2022. readchar;
  2023. until (found=2);
  2024. end;
  2025. {****************************************************************************
  2026. Comment Handling
  2027. ****************************************************************************}
  2028. procedure tscannerfile.skipcomment;
  2029. begin
  2030. aktcommentstyle:=comment_tp;
  2031. readchar;
  2032. inc_comment_level;
  2033. { handle compiler switches }
  2034. if (c='$') then
  2035. handledirectives;
  2036. { handle_switches can dec comment_level, }
  2037. while (comment_level>0) do
  2038. begin
  2039. case c of
  2040. '{' :
  2041. inc_comment_level;
  2042. '}' :
  2043. dec_comment_level;
  2044. #10,#13 :
  2045. linebreak;
  2046. #26 :
  2047. begin
  2048. reload;
  2049. if (c=#26) and not assigned(inputfile.next) then
  2050. end_of_file;
  2051. continue;
  2052. end;
  2053. end;
  2054. readchar;
  2055. end;
  2056. aktcommentstyle:=comment_none;
  2057. end;
  2058. procedure tscannerfile.skipdelphicomment;
  2059. begin
  2060. aktcommentstyle:=comment_delphi;
  2061. inc_comment_level;
  2062. readchar;
  2063. { this is not supported }
  2064. if c='$' then
  2065. Message(scan_w_wrong_styled_switch);
  2066. { skip comment }
  2067. while not (c in [#10,#13,#26]) do
  2068. readchar;
  2069. dec_comment_level;
  2070. aktcommentstyle:=comment_none;
  2071. end;
  2072. procedure tscannerfile.skipoldtpcomment;
  2073. var
  2074. found : longint;
  2075. begin
  2076. aktcommentstyle:=comment_oldtp;
  2077. inc_comment_level;
  2078. { only load a char if last already processed,
  2079. was cause of bug1634 PM }
  2080. if c=#0 then
  2081. readchar;
  2082. { this is now supported }
  2083. if (c='$') then
  2084. handledirectives;
  2085. { skip comment }
  2086. while (comment_level>0) do
  2087. begin
  2088. found:=0;
  2089. repeat
  2090. case c of
  2091. #26 :
  2092. begin
  2093. reload;
  2094. if (c=#26) and not assigned(inputfile.next) then
  2095. end_of_file;
  2096. continue;
  2097. end;
  2098. #10,#13 :
  2099. linebreak;
  2100. '*' :
  2101. begin
  2102. if found=3 then
  2103. found:=4
  2104. else
  2105. found:=1;
  2106. end;
  2107. ')' :
  2108. begin
  2109. if found in [1,4] then
  2110. begin
  2111. dec_comment_level;
  2112. if comment_level=0 then
  2113. found:=2
  2114. else
  2115. found:=0;
  2116. end;
  2117. end;
  2118. '(' :
  2119. begin
  2120. if found=4 then
  2121. inc_comment_level;
  2122. found:=3;
  2123. end;
  2124. else
  2125. begin
  2126. if found=4 then
  2127. inc_comment_level;
  2128. found:=0;
  2129. end;
  2130. end;
  2131. readchar;
  2132. until (found=2);
  2133. end;
  2134. aktcommentstyle:=comment_none;
  2135. end;
  2136. {****************************************************************************
  2137. Token Scanner
  2138. ****************************************************************************}
  2139. procedure tscannerfile.readtoken;
  2140. var
  2141. code : integer;
  2142. len,
  2143. low,high,mid : longint;
  2144. m : longint;
  2145. mac : tmacro;
  2146. asciinr : string[6];
  2147. msgwritten,
  2148. iswidestring : boolean;
  2149. label
  2150. exit_label;
  2151. begin
  2152. if localswitcheschanged then
  2153. begin
  2154. aktlocalswitches:=nextaktlocalswitches;
  2155. localswitcheschanged:=false;
  2156. end;
  2157. { was there already a token read, then return that token }
  2158. if nexttoken<>NOTOKEN then
  2159. begin
  2160. token:=nexttoken;
  2161. nexttoken:=NOTOKEN;
  2162. goto exit_label;
  2163. end;
  2164. { Skip all spaces and comments }
  2165. repeat
  2166. case c of
  2167. '{' :
  2168. skipcomment;
  2169. #26 :
  2170. begin
  2171. reload;
  2172. if (c=#26) and not assigned(inputfile.next) then
  2173. break;
  2174. end;
  2175. ' ',#9..#13 :
  2176. begin
  2177. {$ifdef PREPROCWRITE}
  2178. if parapreprocess then
  2179. begin
  2180. if c=#10 then
  2181. preprocfile.eolfound:=true
  2182. else
  2183. preprocfile.spacefound:=true;
  2184. end;
  2185. {$endif PREPROCWRITE}
  2186. skipspace;
  2187. end
  2188. else
  2189. break;
  2190. end;
  2191. until false;
  2192. { Save current token position, for EOF its already loaded }
  2193. if c<>#26 then
  2194. gettokenpos;
  2195. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  2196. if c in ['A'..'Z','a'..'z','_'] then
  2197. begin
  2198. readstring;
  2199. token:=_ID;
  2200. idtoken:=_ID;
  2201. { keyword or any other known token,
  2202. pattern is always uppercased }
  2203. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  2204. begin
  2205. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  2206. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  2207. while low<high do
  2208. begin
  2209. mid:=(high+low+1) shr 1;
  2210. if pattern<tokeninfo^[ttoken(mid)].str then
  2211. high:=mid-1
  2212. else
  2213. low:=mid;
  2214. end;
  2215. with tokeninfo^[ttoken(high)] do
  2216. if pattern=str then
  2217. begin
  2218. if keyword in aktmodeswitches then
  2219. if op=NOTOKEN then
  2220. token:=ttoken(high)
  2221. else
  2222. token:=op;
  2223. idtoken:=ttoken(high);
  2224. end;
  2225. end;
  2226. { Only process identifiers and not keywords }
  2227. if token=_ID then
  2228. begin
  2229. { this takes some time ... }
  2230. if (cs_support_macro in aktmoduleswitches) then
  2231. begin
  2232. mac:=tmacro(macros.search(pattern));
  2233. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  2234. begin
  2235. if yylexcount<max_macro_nesting then
  2236. begin
  2237. inc(yylexcount);
  2238. insertmacro(pattern,mac.buftext,mac.buflen,
  2239. mac.fileinfo.line,mac.fileinfo.fileindex);
  2240. { handle empty macros }
  2241. if c=#0 then
  2242. reload;
  2243. readtoken;
  2244. { that's all folks }
  2245. dec(yylexcount);
  2246. exit;
  2247. end
  2248. else
  2249. Message(scan_w_macro_deep_ten);
  2250. end;
  2251. end;
  2252. end;
  2253. { return token }
  2254. goto exit_label;
  2255. end
  2256. else
  2257. begin
  2258. idtoken:=_NOID;
  2259. case c of
  2260. '$' :
  2261. begin
  2262. readnumber;
  2263. token:=_INTCONST;
  2264. goto exit_label;
  2265. end;
  2266. '%' :
  2267. begin
  2268. if not(m_fpc in aktmodeswitches) then
  2269. Illegal_Char(c)
  2270. else
  2271. begin
  2272. readnumber;
  2273. token:=_INTCONST;
  2274. goto exit_label;
  2275. end;
  2276. end;
  2277. '&' :
  2278. begin
  2279. if m_fpc in aktmodeswitches then
  2280. begin
  2281. readnumber;
  2282. token:=_INTCONST;
  2283. goto exit_label;
  2284. end
  2285. else if m_mac in aktmodeswitches then
  2286. begin
  2287. readchar;
  2288. token:=_AMPERSAND;
  2289. goto exit_label;
  2290. end
  2291. else
  2292. Illegal_Char(c);
  2293. end;
  2294. '0'..'9' :
  2295. begin
  2296. readnumber;
  2297. if (c in ['.','e','E']) then
  2298. begin
  2299. { first check for a . }
  2300. if c='.' then
  2301. begin
  2302. readchar;
  2303. { is it a .. from a range? }
  2304. case c of
  2305. '.' :
  2306. begin
  2307. readchar;
  2308. token:=_INTCONST;
  2309. nexttoken:=_POINTPOINT;
  2310. goto exit_label;
  2311. end;
  2312. ')' :
  2313. begin
  2314. readchar;
  2315. token:=_INTCONST;
  2316. nexttoken:=_RECKKLAMMER;
  2317. goto exit_label;
  2318. end;
  2319. end;
  2320. { insert the number after the . }
  2321. pattern:=pattern+'.';
  2322. while c in ['0'..'9'] do
  2323. begin
  2324. pattern:=pattern+c;
  2325. readchar;
  2326. end;
  2327. end;
  2328. { E can also follow after a point is scanned }
  2329. if c in ['e','E'] then
  2330. begin
  2331. pattern:=pattern+'E';
  2332. readchar;
  2333. if c in ['-','+'] then
  2334. begin
  2335. pattern:=pattern+c;
  2336. readchar;
  2337. end;
  2338. if not(c in ['0'..'9']) then
  2339. Illegal_Char(c);
  2340. while c in ['0'..'9'] do
  2341. begin
  2342. pattern:=pattern+c;
  2343. readchar;
  2344. end;
  2345. end;
  2346. token:=_REALNUMBER;
  2347. goto exit_label;
  2348. end;
  2349. token:=_INTCONST;
  2350. goto exit_label;
  2351. end;
  2352. ';' :
  2353. begin
  2354. readchar;
  2355. token:=_SEMICOLON;
  2356. goto exit_label;
  2357. end;
  2358. '[' :
  2359. begin
  2360. readchar;
  2361. token:=_LECKKLAMMER;
  2362. goto exit_label;
  2363. end;
  2364. ']' :
  2365. begin
  2366. readchar;
  2367. token:=_RECKKLAMMER;
  2368. goto exit_label;
  2369. end;
  2370. '(' :
  2371. begin
  2372. readchar;
  2373. case c of
  2374. '*' :
  2375. begin
  2376. c:=#0;{Signal skipoldtpcomment to reload a char }
  2377. skipoldtpcomment;
  2378. readtoken;
  2379. exit;
  2380. end;
  2381. '.' :
  2382. begin
  2383. readchar;
  2384. token:=_LECKKLAMMER;
  2385. goto exit_label;
  2386. end;
  2387. end;
  2388. token:=_LKLAMMER;
  2389. goto exit_label;
  2390. end;
  2391. ')' :
  2392. begin
  2393. readchar;
  2394. token:=_RKLAMMER;
  2395. goto exit_label;
  2396. end;
  2397. '+' :
  2398. begin
  2399. readchar;
  2400. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2401. begin
  2402. readchar;
  2403. token:=_PLUSASN;
  2404. goto exit_label;
  2405. end;
  2406. token:=_PLUS;
  2407. goto exit_label;
  2408. end;
  2409. '-' :
  2410. begin
  2411. readchar;
  2412. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2413. begin
  2414. readchar;
  2415. token:=_MINUSASN;
  2416. goto exit_label;
  2417. end;
  2418. token:=_MINUS;
  2419. goto exit_label;
  2420. end;
  2421. ':' :
  2422. begin
  2423. readchar;
  2424. if c='=' then
  2425. begin
  2426. readchar;
  2427. token:=_ASSIGNMENT;
  2428. goto exit_label;
  2429. end;
  2430. token:=_COLON;
  2431. goto exit_label;
  2432. end;
  2433. '*' :
  2434. begin
  2435. readchar;
  2436. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2437. begin
  2438. readchar;
  2439. token:=_STARASN;
  2440. end
  2441. else
  2442. if c='*' then
  2443. begin
  2444. readchar;
  2445. token:=_STARSTAR;
  2446. end
  2447. else
  2448. token:=_STAR;
  2449. goto exit_label;
  2450. end;
  2451. '/' :
  2452. begin
  2453. readchar;
  2454. case c of
  2455. '=' :
  2456. begin
  2457. if (cs_support_c_operators in aktmoduleswitches) then
  2458. begin
  2459. readchar;
  2460. token:=_SLASHASN;
  2461. goto exit_label;
  2462. end;
  2463. end;
  2464. '/' :
  2465. begin
  2466. skipdelphicomment;
  2467. readtoken;
  2468. exit;
  2469. end;
  2470. end;
  2471. token:=_SLASH;
  2472. goto exit_label;
  2473. end;
  2474. '|' :
  2475. if m_mac in aktmodeswitches then
  2476. begin
  2477. readchar;
  2478. token:=_PIPE;
  2479. goto exit_label;
  2480. end
  2481. else
  2482. Illegal_Char(c);
  2483. '=' :
  2484. begin
  2485. readchar;
  2486. token:=_EQUAL;
  2487. goto exit_label;
  2488. end;
  2489. '.' :
  2490. begin
  2491. readchar;
  2492. case c of
  2493. '.' :
  2494. begin
  2495. readchar;
  2496. case c of
  2497. '.' :
  2498. begin
  2499. readchar;
  2500. token:=_POINTPOINTPOINT;
  2501. goto exit_label;
  2502. end;
  2503. else
  2504. begin
  2505. token:=_POINTPOINT;
  2506. goto exit_label;
  2507. end;
  2508. end;
  2509. end;
  2510. ')' :
  2511. begin
  2512. readchar;
  2513. token:=_RECKKLAMMER;
  2514. goto exit_label;
  2515. end;
  2516. end;
  2517. token:=_POINT;
  2518. goto exit_label;
  2519. end;
  2520. '@' :
  2521. begin
  2522. readchar;
  2523. token:=_KLAMMERAFFE;
  2524. goto exit_label;
  2525. end;
  2526. ',' :
  2527. begin
  2528. readchar;
  2529. token:=_COMMA;
  2530. goto exit_label;
  2531. end;
  2532. '''','#','^' :
  2533. begin
  2534. len:=0;
  2535. msgwritten:=false;
  2536. pattern:='';
  2537. iswidestring:=false;
  2538. if c='^' then
  2539. begin
  2540. readchar;
  2541. c:=upcase(c);
  2542. if (block_type=bt_type) or
  2543. (lasttoken=_ID) or (lasttoken=_NIL) or
  2544. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  2545. begin
  2546. token:=_CARET;
  2547. goto exit_label;
  2548. end
  2549. else
  2550. begin
  2551. inc(len);
  2552. if c<#64 then
  2553. pattern[len]:=chr(ord(c)+64)
  2554. else
  2555. pattern[len]:=chr(ord(c)-64);
  2556. readchar;
  2557. end;
  2558. end;
  2559. repeat
  2560. case c of
  2561. '#' :
  2562. begin
  2563. readchar; { read # }
  2564. if c='$' then
  2565. begin
  2566. readchar; { read leading $ }
  2567. asciinr:='$';
  2568. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<6) do
  2569. begin
  2570. asciinr:=asciinr+c;
  2571. readchar;
  2572. end;
  2573. end
  2574. else
  2575. begin
  2576. asciinr:='';
  2577. while (c in ['0'..'9']) and (length(asciinr)<6) do
  2578. begin
  2579. asciinr:=asciinr+c;
  2580. readchar;
  2581. end;
  2582. end;
  2583. valint(asciinr,m,code);
  2584. if (asciinr='') or (code<>0) then
  2585. Message(scan_e_illegal_char_const)
  2586. else if (m<0) or (m>255) or (length(asciinr)>3) then
  2587. begin
  2588. if (m>=0) and (m<=65535) then
  2589. begin
  2590. if not iswidestring then
  2591. begin
  2592. ascii2unicode(@pattern[1],len,patternw);
  2593. iswidestring:=true;
  2594. len:=0;
  2595. end;
  2596. concatwidestringchar(patternw,tcompilerwidechar(m));
  2597. end
  2598. else
  2599. Message(scan_e_illegal_char_const)
  2600. end
  2601. else if iswidestring then
  2602. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  2603. else
  2604. begin
  2605. if len<255 then
  2606. begin
  2607. inc(len);
  2608. pattern[len]:=chr(m);
  2609. end
  2610. else
  2611. begin
  2612. if not msgwritten then
  2613. begin
  2614. Message(scan_e_string_exceeds_255_chars);
  2615. msgwritten:=true;
  2616. end;
  2617. end;
  2618. end;
  2619. end;
  2620. '''' :
  2621. begin
  2622. repeat
  2623. readchar;
  2624. case c of
  2625. #26 :
  2626. end_of_file;
  2627. #10,#13 :
  2628. Message(scan_f_string_exceeds_line);
  2629. '''' :
  2630. begin
  2631. readchar;
  2632. if c<>'''' then
  2633. break;
  2634. end;
  2635. end;
  2636. if iswidestring then
  2637. concatwidestringchar(patternw,asciichar2unicode(c))
  2638. else
  2639. begin
  2640. if len<255 then
  2641. begin
  2642. inc(len);
  2643. pattern[len]:=c;
  2644. end
  2645. else
  2646. begin
  2647. if not msgwritten then
  2648. begin
  2649. Message(scan_e_string_exceeds_255_chars);
  2650. msgwritten:=true;
  2651. end;
  2652. end;
  2653. end;
  2654. until false;
  2655. end;
  2656. '^' :
  2657. begin
  2658. readchar;
  2659. c:=upcase(c);
  2660. if c<#64 then
  2661. c:=chr(ord(c)+64)
  2662. else
  2663. c:=chr(ord(c)-64);
  2664. if iswidestring then
  2665. concatwidestringchar(patternw,asciichar2unicode(c))
  2666. else
  2667. begin
  2668. if len<255 then
  2669. begin
  2670. inc(len);
  2671. pattern[len]:=c;
  2672. end
  2673. else
  2674. begin
  2675. if not msgwritten then
  2676. begin
  2677. Message(scan_e_string_exceeds_255_chars);
  2678. msgwritten:=true;
  2679. end;
  2680. end;
  2681. end;
  2682. readchar;
  2683. end;
  2684. else
  2685. break;
  2686. end;
  2687. until false;
  2688. { strings with length 1 become const chars }
  2689. if iswidestring then
  2690. begin
  2691. if patternw^.len=1 then
  2692. token:=_CWCHAR
  2693. else
  2694. token:=_CWSTRING;
  2695. end
  2696. else
  2697. begin
  2698. pattern[0]:=chr(len);
  2699. if len=1 then
  2700. token:=_CCHAR
  2701. else
  2702. token:=_CSTRING;
  2703. end;
  2704. goto exit_label;
  2705. end;
  2706. '>' :
  2707. begin
  2708. readchar;
  2709. case c of
  2710. '=' :
  2711. begin
  2712. readchar;
  2713. token:=_GTE;
  2714. goto exit_label;
  2715. end;
  2716. '>' :
  2717. begin
  2718. readchar;
  2719. token:=_OP_SHR;
  2720. goto exit_label;
  2721. end;
  2722. '<' :
  2723. begin { >< is for a symetric diff for sets }
  2724. readchar;
  2725. token:=_SYMDIF;
  2726. goto exit_label;
  2727. end;
  2728. end;
  2729. token:=_GT;
  2730. goto exit_label;
  2731. end;
  2732. '<' :
  2733. begin
  2734. readchar;
  2735. case c of
  2736. '>' :
  2737. begin
  2738. readchar;
  2739. token:=_UNEQUAL;
  2740. goto exit_label;
  2741. end;
  2742. '=' :
  2743. begin
  2744. readchar;
  2745. token:=_LTE;
  2746. goto exit_label;
  2747. end;
  2748. '<' :
  2749. begin
  2750. readchar;
  2751. token:=_OP_SHL;
  2752. goto exit_label;
  2753. end;
  2754. end;
  2755. token:=_LT;
  2756. goto exit_label;
  2757. end;
  2758. #26 :
  2759. begin
  2760. token:=_EOF;
  2761. checkpreprocstack;
  2762. goto exit_label;
  2763. end;
  2764. else
  2765. Illegal_Char(c);
  2766. end;
  2767. end;
  2768. exit_label:
  2769. lasttoken:=token;
  2770. end;
  2771. function tscannerfile.readpreproc:ttoken;
  2772. begin
  2773. skipspace;
  2774. case c of
  2775. '_',
  2776. 'A'..'Z',
  2777. 'a'..'z' :
  2778. begin
  2779. current_scanner.preproc_pattern:=readid;
  2780. readpreproc:=_ID;
  2781. end;
  2782. '0'..'9' :
  2783. begin
  2784. current_scanner.preproc_pattern:=readval_asstring;
  2785. { realnumber? }
  2786. if c='.' then
  2787. begin
  2788. readchar;
  2789. while c in ['0'..'9'] do
  2790. begin
  2791. current_scanner.preproc_pattern:=current_scanner.preproc_pattern+c;
  2792. readchar;
  2793. end;
  2794. end;
  2795. readpreproc:=_ID;
  2796. end;
  2797. '$','%','&' :
  2798. begin
  2799. current_scanner.preproc_pattern:=readval_asstring;
  2800. readpreproc:=_ID;
  2801. end;
  2802. '}' :
  2803. begin
  2804. readpreproc:=_END;
  2805. end;
  2806. '(' :
  2807. begin
  2808. readchar;
  2809. readpreproc:=_LKLAMMER;
  2810. end;
  2811. ')' :
  2812. begin
  2813. readchar;
  2814. readpreproc:=_RKLAMMER;
  2815. end;
  2816. '+' :
  2817. begin
  2818. readchar;
  2819. readpreproc:=_PLUS;
  2820. end;
  2821. '-' :
  2822. begin
  2823. readchar;
  2824. readpreproc:=_MINUS;
  2825. end;
  2826. '*' :
  2827. begin
  2828. readchar;
  2829. readpreproc:=_STAR;
  2830. end;
  2831. '/' :
  2832. begin
  2833. readchar;
  2834. readpreproc:=_SLASH;
  2835. end;
  2836. '=' :
  2837. begin
  2838. readchar;
  2839. readpreproc:=_EQUAL;
  2840. end;
  2841. '>' :
  2842. begin
  2843. readchar;
  2844. if c='=' then
  2845. begin
  2846. readchar;
  2847. readpreproc:=_GTE;
  2848. end
  2849. else
  2850. readpreproc:=_GT;
  2851. end;
  2852. '<' :
  2853. begin
  2854. readchar;
  2855. case c of
  2856. '>' :
  2857. begin
  2858. readchar;
  2859. readpreproc:=_UNEQUAL;
  2860. end;
  2861. '=' :
  2862. begin
  2863. readchar;
  2864. readpreproc:=_LTE;
  2865. end;
  2866. else
  2867. readpreproc:=_LT;
  2868. end;
  2869. end;
  2870. #26 :
  2871. begin
  2872. readpreproc:=_EOF;
  2873. checkpreprocstack;
  2874. end;
  2875. else
  2876. Illegal_Char(c);
  2877. end;
  2878. end;
  2879. function tscannerfile.asmgetcharstart : char;
  2880. begin
  2881. { return first the character already
  2882. available in c }
  2883. lastasmgetchar:=c;
  2884. result:=asmgetchar;
  2885. end;
  2886. function tscannerfile.asmgetchar : char;
  2887. begin
  2888. if lastasmgetchar<>#0 then
  2889. begin
  2890. c:=lastasmgetchar;
  2891. lastasmgetchar:=#0;
  2892. end
  2893. else
  2894. readchar;
  2895. if in_asm_string then
  2896. begin
  2897. asmgetchar:=c;
  2898. exit;
  2899. end;
  2900. repeat
  2901. case c of
  2902. {$ifndef arm}
  2903. // the { ... } is used in ARM assembler to define register sets, so we can't used
  2904. // it as comment, either (* ... *), /* ... */ or // ... should be used instead
  2905. '{' :
  2906. skipcomment;
  2907. {$endif arm}
  2908. #10,#13 :
  2909. begin
  2910. linebreak;
  2911. asmgetchar:=c;
  2912. exit;
  2913. end;
  2914. #26 :
  2915. begin
  2916. reload;
  2917. if (c=#26) and not assigned(inputfile.next) then
  2918. end_of_file;
  2919. continue;
  2920. end;
  2921. '/' :
  2922. begin
  2923. readchar;
  2924. if c='/' then
  2925. skipdelphicomment
  2926. else
  2927. begin
  2928. asmgetchar:='/';
  2929. lastasmgetchar:=c;
  2930. exit;
  2931. end;
  2932. end;
  2933. '(' :
  2934. begin
  2935. readchar;
  2936. if c='*' then
  2937. begin
  2938. c:=#0;{Signal skipoldtpcomment to reload a char }
  2939. skipoldtpcomment;
  2940. end
  2941. else
  2942. begin
  2943. asmgetchar:='(';
  2944. lastasmgetchar:=c;
  2945. exit;
  2946. end;
  2947. end;
  2948. else
  2949. begin
  2950. asmgetchar:=c;
  2951. exit;
  2952. end;
  2953. end;
  2954. until false;
  2955. end;
  2956. {*****************************************************************************
  2957. Helpers
  2958. *****************************************************************************}
  2959. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  2960. begin
  2961. if dm in [directive_all, directive_turbo] then
  2962. turbo_scannerdirectives.insert(tdirectiveitem.create(s,p));
  2963. if dm in [directive_all, directive_mac] then
  2964. mac_scannerdirectives.insert(tdirectiveitem.create(s,p));
  2965. end;
  2966. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  2967. begin
  2968. if dm in [directive_all, directive_turbo] then
  2969. turbo_scannerdirectives.insert(tdirectiveitem.createcond(s,p));
  2970. if dm in [directive_all, directive_mac] then
  2971. mac_scannerdirectives.insert(tdirectiveitem.createcond(s,p));
  2972. end;
  2973. {*****************************************************************************
  2974. Initialization
  2975. *****************************************************************************}
  2976. procedure InitScanner;
  2977. begin
  2978. InitWideString(patternw);
  2979. turbo_scannerdirectives:=TDictionary.Create;
  2980. mac_scannerdirectives:=TDictionary.Create;
  2981. { Default directives and conditionals for all modes }
  2982. AddDirective('I',directive_all, {$ifdef FPCPROCVAR}@{$endif}dir_include);
  2983. { Common directives and conditionals }
  2984. AddDirective('DEFINE',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_define);
  2985. AddDirective('UNDEF',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_undef);
  2986. AddDirective('INCLUDE',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_include);
  2987. AddDirective('LIBPREFIX',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_libprefix);
  2988. AddDirective('LIBSUFFIX',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_libsuffix);
  2989. AddDirective('EXTENSION',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_extension);
  2990. AddConditional('ELSE',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_else);
  2991. AddConditional('ELSEIF',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_elseif);
  2992. AddConditional('ENDIF',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_endif);
  2993. AddConditional('IFEND',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_endif);
  2994. AddConditional('IF',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_if);
  2995. AddConditional('IFDEF',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_ifdef);
  2996. AddConditional('IFNDEF',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_ifndef);
  2997. AddConditional('IFOPT',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_ifopt);
  2998. { Default Mac directives and conditionals: }
  2999. AddDirective('SETC',directive_mac, {$ifdef FPCPROCVAR}@{$endif}dir_setc);
  3000. AddDirective('DEFINEC',directive_mac, {$ifdef FPCPROCVAR}@{$endif}dir_define);
  3001. AddConditional('IFC',directive_mac, {$ifdef FPCPROCVAR}@{$endif}dir_if);
  3002. AddConditional('ELSEC',directive_mac, {$ifdef FPCPROCVAR}@{$endif}dir_else);
  3003. AddConditional('ENDC',directive_mac, {$ifdef FPCPROCVAR}@{$endif}dir_endif);
  3004. end;
  3005. procedure DoneScanner;
  3006. begin
  3007. turbo_scannerdirectives.Free;
  3008. mac_scannerdirectives.Free;
  3009. DoneWideString(patternw);
  3010. end;
  3011. end.
  3012. {
  3013. $Log$
  3014. Revision 1.89 2004-09-28 16:00:52 mazen
  3015. - remove Dos unit dependency
  3016. Revision 1.88 2004/09/12 20:46:58 olle
  3017. * Minor bugfix in $IFOPT
  3018. Revision 1.87 2004/08/22 23:16:06 olle
  3019. + added flag to TMacro denoting mac style compiler variable
  3020. * fixed $DEFINEC
  3021. * improved robustness of macro facility
  3022. Revision 1.85 2004/08/02 20:45:40 florian
  3023. * sizeof in the preprocessor handles types now as well
  3024. Revision 1.84 2004/08/02 07:15:54 michael
  3025. + Patch from Christian Iversen to implement LIBPREFIX/SUFFIX/EXTENSION directives
  3026. Revision 1.83 2004/07/23 00:25:05 olle
  3027. + Added compile time function OPTION(x) for MacPas
  3028. * Saved file as text and not binary (I hope)
  3029. Revision 1.81 2004/07/05 23:25:34 olle
  3030. + adding operators "|" and "&" for macpas
  3031. Revision 1.80 2004/06/20 08:55:30 florian
  3032. * logs truncated
  3033. Revision 1.79 2004/05/23 20:55:38 peter
  3034. * support sizeof() in preprocessor
  3035. Revision 1.78 2004/05/19 23:29:56 peter
  3036. * $message directive compatible with delphi
  3037. Revision 1.77 2004/05/16 13:55:26 peter
  3038. * report about illegal chars in preproctoken instead of end of
  3039. expression
  3040. * support realnumbers in preproctoken parser
  3041. Revision 1.76 2004/05/03 10:06:38 olle
  3042. + added language constructs UNIV, C, ... for mode mac
  3043. * consolidated macro expression to conform to Pascal
  3044. * macro true is defined as <> 0
  3045. Revision 1.75 2004/03/04 17:23:10 peter
  3046. * $elseif support
  3047. * conditiotnal in // returns warning isntead of error
  3048. }