scanner.pas 82 KB

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