scanner.pas 80 KB

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