scanner.pas 81 KB

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