scanner.pas 78 KB

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