scanner.pas 84 KB

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