scanner.pas 85 KB

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