scanner.pas 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841
  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 (not current_module.in_second_load) and
  843. (status.errorcount=0) then
  844. checkpreprocstack
  845. else
  846. begin
  847. while assigned(preprocstack) do
  848. poppreprocstack;
  849. end;
  850. if not inputfile.closed then
  851. closeinputfile;
  852. ignoredirectives.free;
  853. macros.free;
  854. end;
  855. procedure tscannerfile.def_macro(const s : string);
  856. var
  857. mac : tmacro;
  858. begin
  859. mac:=tmacro(macros.search(s));
  860. if mac=nil then
  861. begin
  862. mac:=tmacro.create(s);
  863. Message1(parser_m_macro_defined,mac.name);
  864. macros.insert(mac);
  865. end;
  866. mac.defined:=true;
  867. mac.defined_at_startup:=true;
  868. end;
  869. procedure tscannerfile.set_macro(const s : string;value : string);
  870. var
  871. mac : tmacro;
  872. begin
  873. mac:=tmacro(macros.search(s));
  874. if mac=nil then
  875. begin
  876. mac:=tmacro.create(s);
  877. macros.insert(mac);
  878. end
  879. else
  880. begin
  881. if assigned(mac.buftext) then
  882. freemem(mac.buftext,mac.buflen);
  883. end;
  884. Message2(parser_m_macro_set_to,mac.name,value);
  885. mac.buflen:=length(value);
  886. getmem(mac.buftext,mac.buflen);
  887. move(value[1],mac.buftext^,mac.buflen);
  888. mac.defined:=true;
  889. mac.defined_at_startup:=true;
  890. end;
  891. function tscannerfile.openinputfile:boolean;
  892. begin
  893. openinputfile:=inputfile.open;
  894. { load buffer }
  895. inputbuffer:=inputfile.buf;
  896. inputpointer:=inputfile.buf;
  897. inputstart:=inputfile.bufstart;
  898. { line }
  899. line_no:=0;
  900. lastlinepos:=0;
  901. lasttokenpos:=0;
  902. end;
  903. procedure tscannerfile.closeinputfile;
  904. begin
  905. inputfile.close;
  906. { reset buffer }
  907. inputbuffer:=nil;
  908. inputpointer:=nil;
  909. inputstart:=0;
  910. { reset line }
  911. line_no:=0;
  912. lastlinepos:=0;
  913. lasttokenpos:=0;
  914. end;
  915. function tscannerfile.tempopeninputfile:boolean;
  916. begin
  917. tempopeninputfile:=inputfile.tempopen;
  918. { reload buffer }
  919. inputbuffer:=inputfile.buf;
  920. inputpointer:=inputfile.buf;
  921. inputstart:=inputfile.bufstart;
  922. end;
  923. procedure tscannerfile.tempcloseinputfile;
  924. begin
  925. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  926. inputfile.tempclose;
  927. { reset buffer }
  928. inputbuffer:=nil;
  929. inputpointer:=nil;
  930. inputstart:=0;
  931. end;
  932. procedure tscannerfile.saveinputfile;
  933. begin
  934. inputfile.saveinputpointer:=inputpointer;
  935. inputfile.savelastlinepos:=lastlinepos;
  936. inputfile.saveline_no:=line_no;
  937. end;
  938. procedure tscannerfile.restoreinputfile;
  939. begin
  940. inputpointer:=inputfile.saveinputpointer;
  941. lastlinepos:=inputfile.savelastlinepos;
  942. line_no:=inputfile.saveline_no;
  943. if not inputfile.is_macro then
  944. parser_current_file:=inputfile.name^;
  945. end;
  946. procedure tscannerfile.nextfile;
  947. var
  948. to_dispose : tinputfile;
  949. begin
  950. if assigned(inputfile.next) then
  951. begin
  952. if inputfile.is_macro then
  953. to_dispose:=inputfile
  954. else
  955. to_dispose:=nil;
  956. { we can allways close the file, no ? }
  957. inputfile.close;
  958. inputfile:=inputfile.next;
  959. if assigned(to_dispose) then
  960. to_dispose.free;
  961. restoreinputfile;
  962. end;
  963. end;
  964. procedure tscannerfile.addfile(hp:tinputfile);
  965. begin
  966. saveinputfile;
  967. { add to list }
  968. hp.next:=inputfile;
  969. inputfile:=hp;
  970. { load new inputfile }
  971. restoreinputfile;
  972. end;
  973. procedure tscannerfile.reload;
  974. begin
  975. with inputfile do
  976. begin
  977. { when nothing more to read then leave immediatly, so we
  978. don't change the aktfilepos and leave it point to the last
  979. char }
  980. if (c=#26) and (not assigned(next)) then
  981. exit;
  982. repeat
  983. { still more to read?, then change the #0 to a space so its seen
  984. as a seperator, this can't be used for macro's which can change
  985. the place of the #0 in the buffer with tempopen }
  986. if (c=#0) and (bufsize>0) and
  987. not(inputfile.is_macro) and
  988. (inputpointer-inputbuffer<bufsize) then
  989. begin
  990. c:=' ';
  991. inc(longint(inputpointer));
  992. exit;
  993. end;
  994. { can we read more from this file ? }
  995. if (c<>#26) and (not endoffile) then
  996. begin
  997. readbuf;
  998. inputpointer:=buf;
  999. inputbuffer:=buf;
  1000. inputstart:=bufstart;
  1001. { first line? }
  1002. if line_no=0 then
  1003. begin
  1004. line_no:=1;
  1005. if cs_asm_source in aktglobalswitches then
  1006. inputfile.setline(line_no,bufstart);
  1007. end;
  1008. end
  1009. else
  1010. begin
  1011. { load eof position in tokenpos/aktfilepos }
  1012. gettokenpos;
  1013. { close file }
  1014. closeinputfile;
  1015. { no next module, than EOF }
  1016. if not assigned(inputfile.next) then
  1017. begin
  1018. c:=#26;
  1019. exit;
  1020. end;
  1021. { load next file and reopen it }
  1022. nextfile;
  1023. tempopeninputfile;
  1024. { status }
  1025. Message1(scan_t_back_in,inputfile.name^);
  1026. end;
  1027. { load next char }
  1028. c:=inputpointer^;
  1029. inc(longint(inputpointer));
  1030. until c<>#0; { if also end, then reload again }
  1031. end;
  1032. end;
  1033. procedure tscannerfile.insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  1034. var
  1035. hp : tinputfile;
  1036. begin
  1037. { save old postion and decrease linebreak }
  1038. if c=newline then
  1039. dec(line_no);
  1040. dec(longint(inputpointer));
  1041. tempcloseinputfile;
  1042. { create macro 'file' }
  1043. { use special name to dispose after !! }
  1044. hp:=do_openinputfile('_Macro_.'+macname);
  1045. addfile(hp);
  1046. with inputfile do
  1047. begin
  1048. setmacro(p,len);
  1049. { local buffer }
  1050. inputbuffer:=buf;
  1051. inputpointer:=buf;
  1052. inputstart:=bufstart;
  1053. ref_index:=fileindex;
  1054. end;
  1055. { reset line }
  1056. line_no:=line;
  1057. lastlinepos:=0;
  1058. lasttokenpos:=0;
  1059. { load new c }
  1060. c:=inputpointer^;
  1061. inc(longint(inputpointer));
  1062. end;
  1063. procedure tscannerfile.gettokenpos;
  1064. { load the values of tokenpos and lasttokenpos }
  1065. begin
  1066. lasttokenpos:=inputstart+(inputpointer-inputbuffer);
  1067. akttokenpos.line:=line_no;
  1068. akttokenpos.column:=lasttokenpos-lastlinepos;
  1069. akttokenpos.fileindex:=inputfile.ref_index;
  1070. aktfilepos:=akttokenpos;
  1071. end;
  1072. procedure tscannerfile.inc_comment_level;
  1073. var
  1074. oldaktfilepos : tfileposinfo;
  1075. begin
  1076. if (m_nested_comment in aktmodeswitches) then
  1077. inc(comment_level)
  1078. else
  1079. comment_level:=1;
  1080. if (comment_level>1) then
  1081. begin
  1082. oldaktfilepos:=aktfilepos;
  1083. gettokenpos; { update for warning }
  1084. Message1(scan_w_comment_level,tostr(comment_level));
  1085. aktfilepos:=oldaktfilepos;
  1086. end;
  1087. end;
  1088. procedure tscannerfile.dec_comment_level;
  1089. begin
  1090. if (m_nested_comment in aktmodeswitches) then
  1091. dec(comment_level)
  1092. else
  1093. comment_level:=0;
  1094. end;
  1095. procedure tscannerfile.linebreak;
  1096. var
  1097. cur : char;
  1098. oldtokenpos,
  1099. oldaktfilepos : tfileposinfo;
  1100. begin
  1101. with inputfile do
  1102. begin
  1103. if (byte(inputpointer^)=0) and not(endoffile) then
  1104. begin
  1105. cur:=c;
  1106. reload;
  1107. if byte(cur)+byte(c)<>23 then
  1108. dec(longint(inputpointer));
  1109. end
  1110. else
  1111. begin
  1112. { Fix linebreak to be only newline (=#10) for all types of linebreaks }
  1113. if (byte(inputpointer^)+byte(c)=23) then
  1114. inc(longint(inputpointer));
  1115. end;
  1116. c:=newline;
  1117. { increase line counters }
  1118. lastlinepos:=bufstart+(inputpointer-inputbuffer);
  1119. inc(line_no);
  1120. { update linebuffer }
  1121. if cs_asm_source in aktglobalswitches then
  1122. inputfile.setline(line_no,lastlinepos);
  1123. { update for status and call the show status routine,
  1124. but don't touch aktfilepos ! }
  1125. oldaktfilepos:=aktfilepos;
  1126. oldtokenpos:=akttokenpos;
  1127. gettokenpos; { update for v_status }
  1128. inc(status.compiledlines);
  1129. ShowStatus;
  1130. aktfilepos:=oldaktfilepos;
  1131. akttokenpos:=oldtokenpos;
  1132. end;
  1133. end;
  1134. procedure tscannerfile.illegal_char(c:char);
  1135. var
  1136. s : string;
  1137. begin
  1138. if c in [#32..#255] then
  1139. s:=''''+c+''''
  1140. else
  1141. s:='#'+tostr(ord(c));
  1142. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  1143. end;
  1144. procedure tscannerfile.end_of_file;
  1145. begin
  1146. checkpreprocstack;
  1147. Message(scan_f_end_of_file);
  1148. end;
  1149. {-------------------------------------------
  1150. IF Conditional Handling
  1151. -------------------------------------------}
  1152. procedure tscannerfile.checkpreprocstack;
  1153. begin
  1154. { check for missing ifdefs }
  1155. while assigned(preprocstack) do
  1156. begin
  1157. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  1158. preprocstack.owner.inputfile.name^,tostr(preprocstack.line_nb));
  1159. poppreprocstack;
  1160. end;
  1161. end;
  1162. procedure tscannerfile.poppreprocstack;
  1163. var
  1164. hp : tpreprocstack;
  1165. begin
  1166. if assigned(preprocstack) then
  1167. begin
  1168. Message1(scan_c_endif_found,preprocstack.name);
  1169. hp:=preprocstack.next;
  1170. preprocstack.free;
  1171. preprocstack:=hp;
  1172. end
  1173. else
  1174. Message(scan_e_endif_without_if);
  1175. end;
  1176. procedure tscannerfile.addpreprocstack(atyp : preproctyp;a:boolean;const s:string;w:longint);
  1177. begin
  1178. preprocstack:=tpreprocstack.create(atyp,((preprocstack=nil) or preprocstack.accept) and a,preprocstack);
  1179. preprocstack.name:=s;
  1180. preprocstack.line_nb:=line_no;
  1181. preprocstack.owner:=self;
  1182. if preprocstack.accept then
  1183. Message2(w,preprocstack.name,'accepted')
  1184. else
  1185. Message2(w,preprocstack.name,'rejected');
  1186. end;
  1187. procedure tscannerfile.elsepreprocstack;
  1188. begin
  1189. if assigned(preprocstack) then
  1190. begin
  1191. preprocstack.typ:=pp_else;
  1192. preprocstack.line_nb:=line_no;
  1193. if not(assigned(preprocstack.next)) or (preprocstack.next.accept) then
  1194. preprocstack.accept:=not preprocstack.accept;
  1195. if preprocstack.accept then
  1196. Message2(scan_c_else_found,preprocstack.name,'accepted')
  1197. else
  1198. Message2(scan_c_else_found,preprocstack.name,'rejected');
  1199. end
  1200. else
  1201. Message(scan_e_endif_without_if);
  1202. end;
  1203. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  1204. var
  1205. oldaktfilepos : tfileposinfo;
  1206. begin
  1207. oldaktfilepos:=aktfilepos;
  1208. repeat
  1209. current_scanner.gettokenpos;
  1210. p.proc{$ifdef FPCPROCVAR}(){$endif};
  1211. { accept the text ? }
  1212. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  1213. break
  1214. else
  1215. begin
  1216. current_scanner.gettokenpos;
  1217. Message(scan_c_skipping_until);
  1218. repeat
  1219. current_scanner.skipuntildirective;
  1220. p:=tdirectiveitem(scannerdirectives.search(current_scanner.readid));
  1221. until assigned(p) and (p.is_conditional);
  1222. current_scanner.gettokenpos;
  1223. Message1(scan_d_handling_switch,'$'+p.name);
  1224. end;
  1225. until false;
  1226. aktfilepos:=oldaktfilepos;
  1227. end;
  1228. procedure tscannerfile.handledirectives;
  1229. var
  1230. t : tdirectiveitem;
  1231. hs : string;
  1232. begin
  1233. gettokenpos;
  1234. readchar; {Remove the $}
  1235. hs:=readid;
  1236. {$ifdef PREPROCWRITE}
  1237. if parapreprocess then
  1238. begin
  1239. t:=Get_Directive(hs);
  1240. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  1241. begin
  1242. preprocfile^.AddSpace;
  1243. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  1244. exit;
  1245. end;
  1246. end;
  1247. {$endif PREPROCWRITE}
  1248. { skip this directive? }
  1249. if (ignoredirectives.find(hs)<>nil) then
  1250. begin
  1251. if (comment_level>0) then
  1252. readcomment;
  1253. { we've read the whole comment }
  1254. aktcommentstyle:=comment_none;
  1255. exit;
  1256. end;
  1257. if hs='' then
  1258. begin
  1259. Message1(scan_w_illegal_switch,'$'+hs);
  1260. end;
  1261. { Check for compiler switches }
  1262. while (length(hs)=1) and (c in ['-','+']) do
  1263. begin
  1264. HandleSwitch(hs[1],c);
  1265. current_scanner.readchar; {Remove + or -}
  1266. if c=',' then
  1267. begin
  1268. current_scanner.readchar; {Remove , }
  1269. { read next switch, support $v+,$+}
  1270. hs:=current_scanner.readid;
  1271. if (hs='') then
  1272. begin
  1273. if (c='$') and (m_fpc in aktmodeswitches) then
  1274. begin
  1275. current_scanner.readchar; { skip $ }
  1276. hs:=current_scanner.readid;
  1277. end;
  1278. if (hs='') then
  1279. Message1(scan_w_illegal_directive,'$'+c);
  1280. end
  1281. else
  1282. Message1(scan_d_handling_switch,'$'+hs);
  1283. end
  1284. else
  1285. hs:='';
  1286. end;
  1287. { directives may follow switches after a , }
  1288. if hs<>'' then
  1289. begin
  1290. t:=tdirectiveitem(scannerdirectives.search(hs));
  1291. if assigned(t) then
  1292. begin
  1293. if t.is_conditional then
  1294. handleconditional(t)
  1295. else
  1296. begin
  1297. Message1(scan_d_handling_switch,'$'+hs);
  1298. t.proc{$ifdef FPCPROCVAR}(){$endif};
  1299. end;
  1300. end
  1301. else
  1302. begin
  1303. current_scanner.ignoredirectives.insert(hs);
  1304. Message1(scan_w_illegal_directive,'$'+hs);
  1305. end;
  1306. { conditionals already read the comment }
  1307. if (current_scanner.comment_level>0) then
  1308. current_scanner.readcomment;
  1309. { we've read the whole comment }
  1310. aktcommentstyle:=comment_none;
  1311. end;
  1312. end;
  1313. procedure tscannerfile.readchar;
  1314. begin
  1315. c:=inputpointer^;
  1316. if c=#0 then
  1317. reload
  1318. else
  1319. inc(longint(inputpointer));
  1320. case c of
  1321. #26 : reload;
  1322. #10,
  1323. #13 : linebreak;
  1324. end;
  1325. end;
  1326. procedure tscannerfile.readstring;
  1327. var
  1328. i : longint;
  1329. begin
  1330. i:=0;
  1331. repeat
  1332. case c of
  1333. '_',
  1334. '0'..'9',
  1335. 'A'..'Z' : begin
  1336. if i<255 then
  1337. begin
  1338. inc(i);
  1339. orgpattern[i]:=c;
  1340. pattern[i]:=c;
  1341. end;
  1342. c:=inputpointer^;
  1343. inc(longint(inputpointer));
  1344. end;
  1345. 'a'..'z' : begin
  1346. if i<255 then
  1347. begin
  1348. inc(i);
  1349. orgpattern[i]:=c;
  1350. pattern[i]:=chr(ord(c)-32)
  1351. end;
  1352. c:=inputpointer^;
  1353. inc(longint(inputpointer));
  1354. end;
  1355. #0 : reload;
  1356. #26 : begin
  1357. reload;
  1358. if c=#26 then
  1359. break;
  1360. end;
  1361. #13,#10 : begin
  1362. linebreak;
  1363. break;
  1364. end;
  1365. else
  1366. break;
  1367. end;
  1368. until false;
  1369. orgpattern[0]:=chr(i);
  1370. pattern[0]:=chr(i);
  1371. end;
  1372. procedure tscannerfile.readnumber;
  1373. var
  1374. base,
  1375. i : longint;
  1376. begin
  1377. case c of
  1378. '%' : begin
  1379. readchar;
  1380. base:=2;
  1381. pattern[1]:='%';
  1382. i:=1;
  1383. end;
  1384. '&' : begin
  1385. readchar;
  1386. base:=8;
  1387. pattern[1]:='&';
  1388. i:=1;
  1389. end;
  1390. '$' : begin
  1391. readchar;
  1392. base:=16;
  1393. pattern[1]:='$';
  1394. i:=1;
  1395. end;
  1396. else
  1397. begin
  1398. base:=10;
  1399. i:=0;
  1400. end;
  1401. end;
  1402. while ((base>=10) and (c in ['0'..'9'])) or
  1403. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  1404. ((base=8) and (c in ['0'..'7'])) or
  1405. ((base=2) and (c in ['0'..'1'])) do
  1406. begin
  1407. if i<255 then
  1408. begin
  1409. inc(i);
  1410. pattern[i]:=c;
  1411. end;
  1412. { get next char }
  1413. c:=inputpointer^;
  1414. if c=#0 then
  1415. reload
  1416. else
  1417. inc(longint(inputpointer));
  1418. end;
  1419. { was the next char a linebreak ? }
  1420. case c of
  1421. #26 : reload;
  1422. #10,
  1423. #13 : linebreak;
  1424. end;
  1425. pattern[0]:=chr(i);
  1426. end;
  1427. function tscannerfile.readid:string;
  1428. begin
  1429. readstring;
  1430. readid:=pattern;
  1431. end;
  1432. function tscannerfile.readval:longint;
  1433. var
  1434. l : longint;
  1435. w : integer;
  1436. begin
  1437. readnumber;
  1438. valint(pattern,l,w);
  1439. readval:=l;
  1440. end;
  1441. function tscannerfile.readcomment:string;
  1442. var
  1443. i : longint;
  1444. begin
  1445. i:=0;
  1446. repeat
  1447. case c of
  1448. '{' :
  1449. if aktcommentstyle=comment_tp then
  1450. inc_comment_level;
  1451. '}' :
  1452. if aktcommentstyle=comment_tp then
  1453. begin
  1454. readchar;
  1455. dec_comment_level;
  1456. if comment_level=0 then
  1457. break
  1458. else
  1459. continue;
  1460. end;
  1461. '*' :
  1462. if aktcommentstyle=comment_oldtp then
  1463. begin
  1464. readchar;
  1465. if c=')' then
  1466. begin
  1467. readchar;
  1468. dec_comment_level;
  1469. break;
  1470. end
  1471. else
  1472. { Add both characters !!}
  1473. if (i<255) then
  1474. begin
  1475. inc(i);
  1476. readcomment[i]:='*';
  1477. if (i<255) then
  1478. begin
  1479. inc(i);
  1480. readcomment[i]:='*';
  1481. end;
  1482. end;
  1483. end
  1484. else
  1485. { Not old TP comment, so add...}
  1486. begin
  1487. if (i<255) then
  1488. begin
  1489. inc(i);
  1490. readcomment[i]:='*';
  1491. end;
  1492. end;
  1493. #26 :
  1494. end_of_file;
  1495. else
  1496. begin
  1497. if (i<255) then
  1498. begin
  1499. inc(i);
  1500. readcomment[i]:=c;
  1501. end;
  1502. end;
  1503. end;
  1504. c:=inputpointer^;
  1505. if c=#0 then
  1506. reload
  1507. else
  1508. inc(longint(inputpointer));
  1509. if c in [#10,#13] then
  1510. linebreak;
  1511. until false;
  1512. readcomment[0]:=chr(i);
  1513. end;
  1514. function tscannerfile.readstate:char;
  1515. var
  1516. state : char;
  1517. begin
  1518. state:=' ';
  1519. if c=' ' then
  1520. begin
  1521. current_scanner.skipspace;
  1522. current_scanner.readid;
  1523. if pattern='ON' then
  1524. state:='+'
  1525. else
  1526. if pattern='OFF' then
  1527. state:='-';
  1528. end
  1529. else
  1530. state:=c;
  1531. if not (state in ['+','-']) then
  1532. Message(scan_e_wrong_switch_toggle);
  1533. readstate:=state;
  1534. end;
  1535. procedure tscannerfile.skipspace;
  1536. begin
  1537. while c in [' ',#9..#13] do
  1538. begin
  1539. c:=inputpointer^;
  1540. if c=#0 then
  1541. reload
  1542. else
  1543. inc(longint(inputpointer));
  1544. case c of
  1545. #26 :
  1546. reload;
  1547. #10,
  1548. #13 :
  1549. linebreak;
  1550. end;
  1551. end;
  1552. end;
  1553. procedure tscannerfile.skipuntildirective;
  1554. var
  1555. incomment : boolean;
  1556. found : longint;
  1557. next_char_loaded : boolean;
  1558. oldcommentstyle : tcommentstyle;
  1559. begin
  1560. found:=0;
  1561. next_char_loaded:=false;
  1562. incomment:=true;
  1563. oldcommentstyle:=aktcommentstyle;
  1564. repeat
  1565. case c of
  1566. #26 :
  1567. end_of_file;
  1568. '{' :
  1569. begin
  1570. if not(m_nested_comment in aktmodeswitches) or
  1571. (comment_level=0) then
  1572. begin
  1573. found:=1;
  1574. aktcommentstyle:=comment_tp;
  1575. end;
  1576. inc_comment_level;
  1577. incomment:=true;
  1578. end;
  1579. '}' :
  1580. begin
  1581. dec_comment_level;
  1582. found:=0;
  1583. incomment:=false;
  1584. end;
  1585. '$' :
  1586. begin
  1587. if found=1 then
  1588. found:=2;
  1589. end;
  1590. '''' :
  1591. if not incomment then
  1592. begin
  1593. repeat
  1594. readchar;
  1595. case c of
  1596. #26 :
  1597. end_of_file;
  1598. newline :
  1599. break;
  1600. '''' :
  1601. begin
  1602. readchar;
  1603. if c<>'''' then
  1604. begin
  1605. next_char_loaded:=true;
  1606. break;
  1607. end;
  1608. end;
  1609. end;
  1610. until false;
  1611. end;
  1612. '(' :
  1613. begin
  1614. if not incomment then
  1615. begin
  1616. readchar;
  1617. if c='*' then
  1618. begin
  1619. readchar;
  1620. if c='$' then
  1621. begin
  1622. found:=2;
  1623. inc_comment_level;
  1624. aktcommentstyle:=comment_oldtp;
  1625. end
  1626. else
  1627. begin
  1628. skipoldtpcomment;
  1629. aktcommentstyle:=oldcommentstyle;
  1630. end;
  1631. end
  1632. else
  1633. next_char_loaded:=true;
  1634. end
  1635. else
  1636. found:=0;
  1637. end;
  1638. '/' :
  1639. begin
  1640. if not incomment then
  1641. begin
  1642. readchar;
  1643. if c='/' then
  1644. begin
  1645. readchar;
  1646. skipdelphicomment;
  1647. aktcommentstyle:=oldcommentstyle;
  1648. end
  1649. else
  1650. next_char_loaded:=true;
  1651. end
  1652. else
  1653. found:=0;
  1654. end;
  1655. else
  1656. found:=0;
  1657. end;
  1658. if next_char_loaded then
  1659. next_char_loaded:=false
  1660. else
  1661. begin
  1662. c:=inputpointer^;
  1663. if c=#0 then
  1664. reload
  1665. else
  1666. inc(longint(inputpointer));
  1667. case c of
  1668. #26 : reload;
  1669. #10,
  1670. #13 : linebreak;
  1671. end;
  1672. end;
  1673. until (found=2);
  1674. end;
  1675. {****************************************************************************
  1676. Comment Handling
  1677. ****************************************************************************}
  1678. procedure tscannerfile.skipcomment;
  1679. begin
  1680. aktcommentstyle:=comment_tp;
  1681. readchar;
  1682. inc_comment_level;
  1683. { handle compiler switches }
  1684. if (c='$') then
  1685. handledirectives;
  1686. { handle_switches can dec comment_level, }
  1687. while (comment_level>0) do
  1688. begin
  1689. case c of
  1690. '{' : inc_comment_level;
  1691. '}' : dec_comment_level;
  1692. #26 : end_of_file;
  1693. end;
  1694. c:=inputpointer^;
  1695. if c=#0 then
  1696. reload
  1697. else
  1698. inc(longint(inputpointer));
  1699. case c of
  1700. #26 : reload;
  1701. #10,
  1702. #13 : linebreak;
  1703. end;
  1704. end;
  1705. aktcommentstyle:=comment_none;
  1706. end;
  1707. procedure tscannerfile.skipdelphicomment;
  1708. begin
  1709. aktcommentstyle:=comment_delphi;
  1710. inc_comment_level;
  1711. readchar;
  1712. { this is not supported }
  1713. if c='$' then
  1714. Message(scan_e_wrong_styled_switch);
  1715. { skip comment }
  1716. while not (c in [newline,#26]) do
  1717. readchar;
  1718. dec_comment_level;
  1719. aktcommentstyle:=comment_none;
  1720. end;
  1721. procedure tscannerfile.skipoldtpcomment;
  1722. var
  1723. found : longint;
  1724. begin
  1725. aktcommentstyle:=comment_oldtp;
  1726. inc_comment_level;
  1727. { only load a char if last already processed,
  1728. was cause of bug1634 PM }
  1729. if c=#0 then
  1730. readchar;
  1731. { this is now supported }
  1732. if (c='$') then
  1733. handledirectives;
  1734. { skip comment }
  1735. while (comment_level>0) do
  1736. begin
  1737. found:=0;
  1738. repeat
  1739. case c of
  1740. #26 :
  1741. end_of_file;
  1742. '*' :
  1743. begin
  1744. if found=3 then
  1745. found:=4
  1746. else
  1747. found:=1;
  1748. end;
  1749. ')' :
  1750. begin
  1751. if found in [1,4] then
  1752. begin
  1753. dec_comment_level;
  1754. if comment_level=0 then
  1755. found:=2
  1756. else
  1757. found:=0;
  1758. end;
  1759. end;
  1760. '(' :
  1761. begin
  1762. if found=4 then
  1763. inc_comment_level;
  1764. found:=3;
  1765. end;
  1766. else
  1767. begin
  1768. if found=4 then
  1769. inc_comment_level;
  1770. found:=0;
  1771. end;
  1772. end;
  1773. c:=inputpointer^;
  1774. if c=#0 then
  1775. reload
  1776. else
  1777. inc(longint(inputpointer));
  1778. case c of
  1779. #26 : reload;
  1780. #10,
  1781. #13 : linebreak;
  1782. end;
  1783. until (found=2);
  1784. end;
  1785. aktcommentstyle:=comment_none;
  1786. end;
  1787. {****************************************************************************
  1788. Token Scanner
  1789. ****************************************************************************}
  1790. procedure tscannerfile.readtoken;
  1791. var
  1792. code : integer;
  1793. len,
  1794. low,high,mid : longint;
  1795. m : longint;
  1796. mac : tmacro;
  1797. asciinr : string[6];
  1798. msgwritten,
  1799. iswidestring : boolean;
  1800. label
  1801. exit_label;
  1802. begin
  1803. if localswitcheschanged then
  1804. begin
  1805. aktlocalswitches:=nextaktlocalswitches;
  1806. localswitcheschanged:=false;
  1807. end;
  1808. { was there already a token read, then return that token }
  1809. if nexttoken<>NOTOKEN then
  1810. begin
  1811. token:=nexttoken;
  1812. nexttoken:=NOTOKEN;
  1813. goto exit_label;
  1814. end;
  1815. { Skip all spaces and comments }
  1816. repeat
  1817. case c of
  1818. '{' :
  1819. skipcomment;
  1820. ' ',#9..#13 :
  1821. begin
  1822. {$ifdef PREPROCWRITE}
  1823. if parapreprocess then
  1824. begin
  1825. if c=#10 then
  1826. preprocfile.eolfound:=true
  1827. else
  1828. preprocfile.spacefound:=true;
  1829. end;
  1830. {$endif PREPROCWRITE}
  1831. skipspace;
  1832. end
  1833. else
  1834. break;
  1835. end;
  1836. until false;
  1837. { Save current token position, for EOF its already loaded }
  1838. if c<>#26 then
  1839. gettokenpos;
  1840. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  1841. if c in ['A'..'Z','a'..'z','_'] then
  1842. begin
  1843. readstring;
  1844. token:=_ID;
  1845. idtoken:=_ID;
  1846. { keyword or any other known token,
  1847. pattern is always uppercased }
  1848. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  1849. begin
  1850. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  1851. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  1852. while low<high do
  1853. begin
  1854. mid:=(high+low+1) shr 1;
  1855. if pattern<tokeninfo^[ttoken(mid)].str then
  1856. high:=mid-1
  1857. else
  1858. low:=mid;
  1859. end;
  1860. if pattern=tokeninfo^[ttoken(high)].str then
  1861. begin
  1862. if tokeninfo^[ttoken(high)].keyword in aktmodeswitches then
  1863. if tokeninfo^[ttoken(high)].op=NOTOKEN then
  1864. token:=ttoken(high)
  1865. else
  1866. token:=tokeninfo^[ttoken(high)].op;
  1867. idtoken:=ttoken(high);
  1868. end;
  1869. end;
  1870. { Only process identifiers and not keywords }
  1871. if token=_ID then
  1872. begin
  1873. { this takes some time ... }
  1874. if (cs_support_macro in aktmoduleswitches) then
  1875. begin
  1876. mac:=tmacro(macros.search(pattern));
  1877. if assigned(mac) and (assigned(mac.buftext)) then
  1878. begin
  1879. insertmacro(pattern,mac.buftext,mac.buflen,
  1880. mac.fileinfo.line,mac.fileinfo.fileindex);
  1881. { handle empty macros }
  1882. if c=#0 then
  1883. begin
  1884. reload;
  1885. case c of
  1886. #26 : reload;
  1887. #10,
  1888. #13 : linebreak;
  1889. end;
  1890. end;
  1891. { play it again ... }
  1892. inc(yylexcount);
  1893. if yylexcount>16 then
  1894. Message(scan_w_macro_deep_ten);
  1895. readtoken;
  1896. { that's all folks }
  1897. dec(yylexcount);
  1898. exit;
  1899. end;
  1900. end;
  1901. end;
  1902. { return token }
  1903. goto exit_label;
  1904. end
  1905. else
  1906. begin
  1907. idtoken:=_NOID;
  1908. case c of
  1909. '$' :
  1910. begin
  1911. readnumber;
  1912. token:=_INTCONST;
  1913. goto exit_label;
  1914. end;
  1915. '%' :
  1916. begin
  1917. if not(m_fpc in aktmodeswitches) then
  1918. Illegal_Char(c)
  1919. else
  1920. begin
  1921. readnumber;
  1922. token:=_INTCONST;
  1923. goto exit_label;
  1924. end;
  1925. end;
  1926. '&' :
  1927. begin
  1928. if not(m_fpc in aktmodeswitches) then
  1929. Illegal_Char(c)
  1930. else
  1931. begin
  1932. readnumber;
  1933. token:=_INTCONST;
  1934. goto exit_label;
  1935. end;
  1936. end;
  1937. '0'..'9' :
  1938. begin
  1939. readnumber;
  1940. if (c in ['.','e','E']) then
  1941. begin
  1942. { first check for a . }
  1943. if c='.' then
  1944. begin
  1945. readchar;
  1946. { is it a .. from a range? }
  1947. case c of
  1948. '.' :
  1949. begin
  1950. readchar;
  1951. token:=_INTCONST;
  1952. nexttoken:=_POINTPOINT;
  1953. goto exit_label;
  1954. end;
  1955. ')' :
  1956. begin
  1957. readchar;
  1958. token:=_INTCONST;
  1959. nexttoken:=_RECKKLAMMER;
  1960. goto exit_label;
  1961. end;
  1962. end;
  1963. { insert the number after the . }
  1964. pattern:=pattern+'.';
  1965. while c in ['0'..'9'] do
  1966. begin
  1967. pattern:=pattern+c;
  1968. readchar;
  1969. end;
  1970. end;
  1971. { E can also follow after a point is scanned }
  1972. if c in ['e','E'] then
  1973. begin
  1974. pattern:=pattern+'E';
  1975. readchar;
  1976. if c in ['-','+'] then
  1977. begin
  1978. pattern:=pattern+c;
  1979. readchar;
  1980. end;
  1981. if not(c in ['0'..'9']) then
  1982. Illegal_Char(c);
  1983. while c in ['0'..'9'] do
  1984. begin
  1985. pattern:=pattern+c;
  1986. readchar;
  1987. end;
  1988. end;
  1989. token:=_REALNUMBER;
  1990. goto exit_label;
  1991. end;
  1992. token:=_INTCONST;
  1993. goto exit_label;
  1994. end;
  1995. ';' :
  1996. begin
  1997. readchar;
  1998. token:=_SEMICOLON;
  1999. goto exit_label;
  2000. end;
  2001. '[' :
  2002. begin
  2003. readchar;
  2004. token:=_LECKKLAMMER;
  2005. goto exit_label;
  2006. end;
  2007. ']' :
  2008. begin
  2009. readchar;
  2010. token:=_RECKKLAMMER;
  2011. goto exit_label;
  2012. end;
  2013. '(' :
  2014. begin
  2015. readchar;
  2016. case c of
  2017. '*' :
  2018. begin
  2019. c:=#0;{Signal skipoldtpcomment to reload a char }
  2020. skipoldtpcomment;
  2021. readtoken;
  2022. exit;
  2023. end;
  2024. '.' :
  2025. begin
  2026. readchar;
  2027. token:=_LECKKLAMMER;
  2028. goto exit_label;
  2029. end;
  2030. end;
  2031. token:=_LKLAMMER;
  2032. goto exit_label;
  2033. end;
  2034. ')' :
  2035. begin
  2036. readchar;
  2037. token:=_RKLAMMER;
  2038. goto exit_label;
  2039. end;
  2040. '+' :
  2041. begin
  2042. readchar;
  2043. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2044. begin
  2045. readchar;
  2046. token:=_PLUSASN;
  2047. goto exit_label;
  2048. end;
  2049. token:=_PLUS;
  2050. goto exit_label;
  2051. end;
  2052. '-' :
  2053. begin
  2054. readchar;
  2055. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2056. begin
  2057. readchar;
  2058. token:=_MINUSASN;
  2059. goto exit_label;
  2060. end;
  2061. token:=_MINUS;
  2062. goto exit_label;
  2063. end;
  2064. ':' :
  2065. begin
  2066. readchar;
  2067. if c='=' then
  2068. begin
  2069. readchar;
  2070. token:=_ASSIGNMENT;
  2071. goto exit_label;
  2072. end;
  2073. token:=_COLON;
  2074. goto exit_label;
  2075. end;
  2076. '*' :
  2077. begin
  2078. readchar;
  2079. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2080. begin
  2081. readchar;
  2082. token:=_STARASN;
  2083. end
  2084. else
  2085. if c='*' then
  2086. begin
  2087. readchar;
  2088. token:=_STARSTAR;
  2089. end
  2090. else
  2091. token:=_STAR;
  2092. goto exit_label;
  2093. end;
  2094. '/' :
  2095. begin
  2096. readchar;
  2097. case c of
  2098. '=' :
  2099. begin
  2100. if (cs_support_c_operators in aktmoduleswitches) then
  2101. begin
  2102. readchar;
  2103. token:=_SLASHASN;
  2104. goto exit_label;
  2105. end;
  2106. end;
  2107. '/' :
  2108. begin
  2109. skipdelphicomment;
  2110. readtoken;
  2111. exit;
  2112. end;
  2113. end;
  2114. token:=_SLASH;
  2115. goto exit_label;
  2116. end;
  2117. '=' :
  2118. begin
  2119. readchar;
  2120. token:=_EQUAL;
  2121. goto exit_label;
  2122. end;
  2123. '.' :
  2124. begin
  2125. readchar;
  2126. case c of
  2127. '.' :
  2128. begin
  2129. readchar;
  2130. token:=_POINTPOINT;
  2131. goto exit_label;
  2132. end;
  2133. ')' :
  2134. begin
  2135. readchar;
  2136. token:=_RECKKLAMMER;
  2137. goto exit_label;
  2138. end;
  2139. end;
  2140. token:=_POINT;
  2141. goto exit_label;
  2142. end;
  2143. '@' :
  2144. begin
  2145. readchar;
  2146. if c='@' then
  2147. begin
  2148. readchar;
  2149. token:=_DOUBLEADDR;
  2150. end
  2151. else
  2152. token:=_KLAMMERAFFE;
  2153. goto exit_label;
  2154. end;
  2155. ',' :
  2156. begin
  2157. readchar;
  2158. token:=_COMMA;
  2159. goto exit_label;
  2160. end;
  2161. '''','#','^' :
  2162. begin
  2163. len:=0;
  2164. msgwritten:=false;
  2165. pattern:='';
  2166. iswidestring:=false;
  2167. if c='^' then
  2168. begin
  2169. readchar;
  2170. c:=upcase(c);
  2171. if (block_type=bt_type) or
  2172. (lasttoken=_ID) or
  2173. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  2174. begin
  2175. token:=_CARET;
  2176. goto exit_label;
  2177. end
  2178. else
  2179. begin
  2180. inc(len);
  2181. if c<#64 then
  2182. pattern[len]:=chr(ord(c)+64)
  2183. else
  2184. pattern[len]:=chr(ord(c)-64);
  2185. readchar;
  2186. end;
  2187. end;
  2188. repeat
  2189. case c of
  2190. '#' :
  2191. begin
  2192. readchar; { read # }
  2193. if c='$' then
  2194. begin
  2195. readchar; { read leading $ }
  2196. asciinr:='$';
  2197. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<6) do
  2198. begin
  2199. asciinr:=asciinr+c;
  2200. readchar;
  2201. end;
  2202. end
  2203. else
  2204. begin
  2205. asciinr:='';
  2206. while (c in ['0'..'9']) and (length(asciinr)<6) do
  2207. begin
  2208. asciinr:=asciinr+c;
  2209. readchar;
  2210. end;
  2211. end;
  2212. valint(asciinr,m,code);
  2213. if (asciinr='') or (code<>0) then
  2214. Message(scan_e_illegal_char_const)
  2215. else if (m<0) or (m>255) or (length(asciinr)>3) then
  2216. begin
  2217. if (m>=0) and (m<=65535) then
  2218. begin
  2219. if not iswidestring then
  2220. begin
  2221. ascii2unicode(@pattern[1],len,patternw);
  2222. iswidestring:=true;
  2223. len:=0;
  2224. end;
  2225. concatwidestringchar(patternw,tcompilerwidechar(m));
  2226. end
  2227. else
  2228. Message(scan_e_illegal_char_const)
  2229. end
  2230. else if iswidestring then
  2231. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  2232. else
  2233. begin
  2234. if len<255 then
  2235. begin
  2236. inc(len);
  2237. pattern[len]:=chr(m);
  2238. end
  2239. else
  2240. begin
  2241. if not msgwritten then
  2242. begin
  2243. Message(scan_e_string_exceeds_255_chars);
  2244. msgwritten:=true;
  2245. end;
  2246. end;
  2247. end;
  2248. end;
  2249. '''' :
  2250. begin
  2251. repeat
  2252. readchar;
  2253. case c of
  2254. #26 :
  2255. end_of_file;
  2256. newline :
  2257. Message(scan_f_string_exceeds_line);
  2258. '''' :
  2259. begin
  2260. readchar;
  2261. if c<>'''' then
  2262. break;
  2263. end;
  2264. end;
  2265. if iswidestring then
  2266. concatwidestringchar(patternw,asciichar2unicode(c))
  2267. else
  2268. begin
  2269. if len<255 then
  2270. begin
  2271. inc(len);
  2272. pattern[len]:=c;
  2273. end
  2274. else
  2275. begin
  2276. if not msgwritten then
  2277. begin
  2278. Message(scan_e_string_exceeds_255_chars);
  2279. msgwritten:=true;
  2280. end;
  2281. end;
  2282. end;
  2283. until false;
  2284. end;
  2285. '^' :
  2286. begin
  2287. readchar;
  2288. c:=upcase(c);
  2289. if c<#64 then
  2290. c:=chr(ord(c)+64)
  2291. else
  2292. c:=chr(ord(c)-64);
  2293. if iswidestring then
  2294. concatwidestringchar(patternw,asciichar2unicode(c))
  2295. else
  2296. begin
  2297. if len<255 then
  2298. begin
  2299. inc(len);
  2300. pattern[len]:=c;
  2301. end
  2302. else
  2303. begin
  2304. if not msgwritten then
  2305. begin
  2306. Message(scan_e_string_exceeds_255_chars);
  2307. msgwritten:=true;
  2308. end;
  2309. end;
  2310. end;
  2311. readchar;
  2312. end;
  2313. else
  2314. break;
  2315. end;
  2316. until false;
  2317. { strings with length 1 become const chars }
  2318. if iswidestring then
  2319. begin
  2320. if patternw^.len=1 then
  2321. token:=_CWCHAR
  2322. else
  2323. token:=_CWSTRING;
  2324. end
  2325. else
  2326. begin
  2327. pattern[0]:=chr(len);
  2328. if len=1 then
  2329. token:=_CCHAR
  2330. else
  2331. token:=_CSTRING;
  2332. end;
  2333. goto exit_label;
  2334. end;
  2335. '>' :
  2336. begin
  2337. readchar;
  2338. case c of
  2339. '=' :
  2340. begin
  2341. readchar;
  2342. token:=_GTE;
  2343. goto exit_label;
  2344. end;
  2345. '>' :
  2346. begin
  2347. readchar;
  2348. token:=_OP_SHR;
  2349. goto exit_label;
  2350. end;
  2351. '<' :
  2352. begin { >< is for a symetric diff for sets }
  2353. readchar;
  2354. token:=_SYMDIF;
  2355. goto exit_label;
  2356. end;
  2357. end;
  2358. token:=_GT;
  2359. goto exit_label;
  2360. end;
  2361. '<' :
  2362. begin
  2363. readchar;
  2364. case c of
  2365. '>' :
  2366. begin
  2367. readchar;
  2368. token:=_UNEQUAL;
  2369. goto exit_label;
  2370. end;
  2371. '=' :
  2372. begin
  2373. readchar;
  2374. token:=_LTE;
  2375. goto exit_label;
  2376. end;
  2377. '<' :
  2378. begin
  2379. readchar;
  2380. token:=_OP_SHL;
  2381. goto exit_label;
  2382. end;
  2383. end;
  2384. token:=_LT;
  2385. goto exit_label;
  2386. end;
  2387. #26 :
  2388. begin
  2389. token:=_EOF;
  2390. checkpreprocstack;
  2391. goto exit_label;
  2392. end;
  2393. else
  2394. Illegal_Char(c);
  2395. end;
  2396. end;
  2397. exit_label:
  2398. lasttoken:=token;
  2399. end;
  2400. function tscannerfile.readpreproc:ttoken;
  2401. begin
  2402. skipspace;
  2403. case c of
  2404. 'A'..'Z',
  2405. 'a'..'z',
  2406. '_','0'..'9' : begin
  2407. current_scanner.preproc_pattern:=readid;
  2408. readpreproc:=_ID;
  2409. end;
  2410. '}' : begin
  2411. readpreproc:=_END;
  2412. end;
  2413. '(' : begin
  2414. readchar;
  2415. readpreproc:=_LKLAMMER;
  2416. end;
  2417. ')' : begin
  2418. readchar;
  2419. readpreproc:=_RKLAMMER;
  2420. end;
  2421. '+' : begin
  2422. readchar;
  2423. readpreproc:=_PLUS;
  2424. end;
  2425. '-' : begin
  2426. readchar;
  2427. readpreproc:=_MINUS;
  2428. end;
  2429. '*' : begin
  2430. readchar;
  2431. readpreproc:=_STAR;
  2432. end;
  2433. '/' : begin
  2434. readchar;
  2435. readpreproc:=_SLASH;
  2436. end;
  2437. '=' : begin
  2438. readchar;
  2439. readpreproc:=_EQUAL;
  2440. end;
  2441. '>' : begin
  2442. readchar;
  2443. if c='=' then
  2444. begin
  2445. readchar;
  2446. readpreproc:=_GTE;
  2447. end
  2448. else
  2449. readpreproc:=_GT;
  2450. end;
  2451. '<' : begin
  2452. readchar;
  2453. case c of
  2454. '>' : begin
  2455. readchar;
  2456. readpreproc:=_UNEQUAL;
  2457. end;
  2458. '=' : begin
  2459. readchar;
  2460. readpreproc:=_LTE;
  2461. end;
  2462. else readpreproc:=_LT;
  2463. end;
  2464. end;
  2465. #26 :
  2466. end_of_file;
  2467. else
  2468. begin
  2469. readpreproc:=_EOF;
  2470. checkpreprocstack;
  2471. end;
  2472. end;
  2473. end;
  2474. function tscannerfile.asmgetchar : char;
  2475. begin
  2476. if lastasmgetchar<>#0 then
  2477. begin
  2478. c:=lastasmgetchar;
  2479. lastasmgetchar:=#0;
  2480. end
  2481. else
  2482. readchar;
  2483. if in_asm_string then
  2484. begin
  2485. asmgetchar:=c;
  2486. exit;
  2487. end;
  2488. repeat
  2489. case c of
  2490. '{' :
  2491. skipcomment;
  2492. '/' :
  2493. begin
  2494. readchar;
  2495. if c='/' then
  2496. skipdelphicomment
  2497. else
  2498. begin
  2499. asmgetchar:='/';
  2500. lastasmgetchar:=c;
  2501. exit;
  2502. end;
  2503. end;
  2504. '(' :
  2505. begin
  2506. readchar;
  2507. if c='*' then
  2508. begin
  2509. c:=#0;{Signal skipoldtpcomment to reload a char }
  2510. skipoldtpcomment;
  2511. end
  2512. else
  2513. begin
  2514. asmgetchar:='(';
  2515. lastasmgetchar:=c;
  2516. exit;
  2517. end;
  2518. end;
  2519. else
  2520. begin
  2521. asmgetchar:=c;
  2522. exit;
  2523. end;
  2524. end;
  2525. until false;
  2526. end;
  2527. {*****************************************************************************
  2528. Helpers
  2529. *****************************************************************************}
  2530. procedure adddirective(const s:string;p:tdirectiveproc);
  2531. begin
  2532. scannerdirectives.insert(tdirectiveitem.create(s,p));
  2533. end;
  2534. procedure addconditional(const s:string;p:tdirectiveproc);
  2535. begin
  2536. scannerdirectives.insert(tdirectiveitem.createcond(s,p));
  2537. end;
  2538. {*****************************************************************************
  2539. Initialization
  2540. *****************************************************************************}
  2541. procedure InitScanner;
  2542. begin
  2543. InitWideString(patternw);
  2544. scannerdirectives:=TDictionary.Create;
  2545. { Default directives }
  2546. AddDirective('DEFINE',{$ifdef FPCPROCVAR}@{$endif}dir_define);
  2547. AddDirective('UNDEF',{$ifdef FPCPROCVAR}@{$endif}dir_undef);
  2548. AddDirective('I',{$ifdef FPCPROCVAR}@{$endif}dir_include);
  2549. AddDirective('INCLUDE',{$ifdef FPCPROCVAR}@{$endif}dir_include);
  2550. { Default conditionals }
  2551. AddConditional('ELSE',{$ifdef FPCPROCVAR}@{$endif}dir_else);
  2552. AddConditional('ENDIF',{$ifdef FPCPROCVAR}@{$endif}dir_endif);
  2553. AddConditional('IF',{$ifdef FPCPROCVAR}@{$endif}dir_if);
  2554. AddConditional('IFDEF',{$ifdef FPCPROCVAR}@{$endif}dir_ifdef);
  2555. AddConditional('IFNDEF',{$ifdef FPCPROCVAR}@{$endif}dir_ifndef);
  2556. AddConditional('IFOPT',{$ifdef FPCPROCVAR}@{$endif}dir_ifopt);
  2557. end;
  2558. procedure DoneScanner;
  2559. begin
  2560. scannerdirectives.Free;
  2561. DoneWideString(patternw);
  2562. end;
  2563. end.
  2564. {
  2565. $Log$
  2566. Revision 1.45 2002-09-05 14:17:27 pierre
  2567. * fix for bug 2004 merged
  2568. Revision 1.44 2002/08/12 16:46:04 peter
  2569. * tscannerfile is now destroyed in tmodule.reset and current_scanner
  2570. is updated accordingly. This removes all the loading and saving of
  2571. the old scanner and the invalid flag marking
  2572. Revision 1.43 2002/08/11 14:28:19 peter
  2573. * TScannerFile.SetInvalid added that will also reset inputfile
  2574. Revision 1.42 2002/08/10 14:46:31 carl
  2575. + moved target_cpu_string to cpuinfo
  2576. * renamed asmmode enum.
  2577. * assembler reader has now less ifdef's
  2578. * move from nppcmem.pas -> ncgmem.pas vec. node.
  2579. Revision 1.41 2002/08/06 21:12:16 florian
  2580. + support for octal constants, they are specified by a leading &
  2581. Revision 1.40 2002/07/20 17:35:52 florian
  2582. + char constants specified with #.. with more than 3 digits are handled as widechar
  2583. Revision 1.39 2002/05/18 13:34:17 peter
  2584. * readded missing revisions
  2585. Revision 1.38 2002/05/16 19:46:44 carl
  2586. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2587. + try to fix temp allocation (still in ifdef)
  2588. + generic constructor calls
  2589. + start of tassembler / tmodulebase class cleanup
  2590. Revision 1.36 2002/04/21 18:57:23 peter
  2591. * fixed memleaks when file can't be opened
  2592. Revision 1.35 2002/04/21 15:22:26 carl
  2593. * first check .inc file extension
  2594. Revision 1.34 2002/04/21 07:24:09 carl
  2595. - remove my fixes until Peter agrees on the fix (sorry Peter)
  2596. Revision 1.32 2002/04/19 15:42:11 peter
  2597. * default extension checking for include files
  2598. Revision 1.31 2002/03/01 14:39:44 peter
  2599. * fixed // and (* parsing to not be done when already parsing a
  2600. tp comment in skipuntildirective
  2601. Revision 1.30 2002/03/01 12:39:26 peter
  2602. * support // parsing in skipuntildirective
  2603. Revision 1.29 2002/01/27 21:44:26 peter
  2604. * FPCTARGETOS/FPCTARGETCPU added as internal environment variable
  2605. Revision 1.28 2002/01/24 18:25:50 peter
  2606. * implicit result variable generation for assembler routines
  2607. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  2608. }