scanner.pas 83 KB

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