scanner.pas 84 KB

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