scanner.pas 98 KB

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