scanner.pas 84 KB

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