scanner.pas 85 KB

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