scanner.pas 83 KB

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