scanner.pas 76 KB

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