scanner.pas 86 KB

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