scanner.pas 85 KB

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