scanner.pas 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894
  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. if hs='' then
  220. Message(scan_e_error_in_preproc_expr);
  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. if hs='' then
  234. Message(scan_e_error_in_preproc_expr);
  235. mac:=tmacro(current_scanner.macros.search(hs));
  236. if assigned(mac) then
  237. mac.is_used:=true;
  238. current_scanner.addpreprocstack(pp_ifndef,not(assigned(mac) and mac.defined),hs,scan_c_ifndef_found);
  239. end;
  240. procedure dir_ifopt;
  241. var
  242. hs : string;
  243. found : boolean;
  244. state : char;
  245. begin
  246. current_scanner.skipspace;
  247. hs:=current_scanner.readid;
  248. if (length(hs)>1) then
  249. Message1(scan_w_illegal_switch,hs)
  250. else
  251. begin
  252. state:=current_scanner.ReadState;
  253. if state in ['-','+'] then
  254. found:=CheckSwitch(hs[1],state);
  255. end;
  256. current_scanner.addpreprocstack(pp_ifopt,found,hs,scan_c_ifopt_found);
  257. end;
  258. procedure dir_if;
  259. function read_expr : string; forward;
  260. procedure preproc_consume(t : ttoken);
  261. begin
  262. if t<>current_scanner.preproc_token then
  263. Message(scan_e_preproc_syntax_error);
  264. current_scanner.preproc_token:=current_scanner.readpreproc;
  265. end;
  266. function readpreproc: string;
  267. var
  268. hs: string;
  269. mac : tmacro;
  270. len : integer;
  271. begin
  272. hs := current_scanner.preproc_pattern;
  273. mac:=tmacro(current_scanner.macros.search(hs));
  274. if assigned(mac) then
  275. begin
  276. if mac.defined and assigned(mac.buftext) then
  277. begin
  278. if mac.buflen>255 then
  279. begin
  280. len:=255;
  281. Message(scan_w_macro_cut_after_255_chars);
  282. end
  283. else
  284. len:=mac.buflen;
  285. hs[0]:=char(len);
  286. move(mac.buftext^,hs[1],len);
  287. end;
  288. end;
  289. readpreproc := hs;
  290. end;
  291. function read_factor : string;
  292. var
  293. hs : string;
  294. mac: tmacro;
  295. begin
  296. if current_scanner.preproc_token=_ID then
  297. begin
  298. if readpreproc='DEFINED' then
  299. begin
  300. preproc_consume(_ID);
  301. current_scanner.skipspace;
  302. if current_scanner.preproc_token =_LKLAMMER then
  303. begin
  304. preproc_consume(_LKLAMMER);
  305. current_scanner.skipspace;
  306. end
  307. else
  308. Message(scan_e_error_in_preproc_expr);
  309. if current_scanner.preproc_token =_ID then
  310. begin
  311. hs := current_scanner.preproc_pattern;
  312. mac := tmacro(current_scanner.macros.search(hs));
  313. if assigned(mac) then
  314. hs := '1'
  315. else
  316. hs := '0';
  317. read_factor := hs;
  318. preproc_consume(_ID);
  319. current_scanner.skipspace;
  320. end
  321. else
  322. Message(scan_e_error_in_preproc_expr);
  323. if current_scanner.preproc_token =_RKLAMMER then
  324. preproc_consume(_RKLAMMER)
  325. else
  326. Message(scan_e_error_in_preproc_expr);
  327. end
  328. else
  329. if readpreproc='NOT' then
  330. begin
  331. preproc_consume(_ID);
  332. hs:=read_expr;
  333. if hs='0' then
  334. read_factor:='1'
  335. else
  336. read_factor:='0';
  337. end
  338. else
  339. begin
  340. hs:=readpreproc;
  341. preproc_consume(_ID);
  342. read_factor:=hs;
  343. end
  344. end
  345. else if current_scanner.preproc_token =_LKLAMMER then
  346. begin
  347. preproc_consume(_LKLAMMER);
  348. read_factor:=read_expr;
  349. preproc_consume(_RKLAMMER);
  350. end
  351. else
  352. Message(scan_e_error_in_preproc_expr);
  353. end;
  354. function read_term : string;
  355. var
  356. hs1,hs2 : string;
  357. l1,l2 : longint;
  358. w : integer;
  359. begin
  360. hs1:=read_factor;
  361. while true do
  362. begin
  363. if (current_scanner.preproc_token=_ID) then
  364. begin
  365. if readpreproc='AND' then
  366. begin
  367. preproc_consume(_ID);
  368. hs2:=read_expr;
  369. valint(hs1,l1,w); valint(hs2,l2,w);
  370. if (l1>0) and (l2>0) then
  371. hs1:='1'
  372. else
  373. hs1:='0';
  374. read_term := hs1;
  375. exit;
  376. end
  377. else
  378. break;
  379. end
  380. else
  381. break;
  382. end;
  383. read_term:=hs1;
  384. end;
  385. function read_simple_expr : string;
  386. var
  387. hs1,hs2 : string;
  388. l1,l2 : longint;
  389. w : integer;
  390. begin
  391. hs1:=read_term;
  392. while true do
  393. begin
  394. if (current_scanner.preproc_token=_ID) then
  395. begin
  396. if readpreproc='OR' then
  397. begin
  398. preproc_consume(_ID);
  399. hs2:=read_expr;
  400. valint(hs1,l1,w); valint(hs2,l2,w);
  401. if (l1>0) or (l2>0) then
  402. hs1:='1'
  403. else
  404. hs1:='0';
  405. read_simple_expr := hs1;
  406. exit;
  407. end
  408. else
  409. break;
  410. end
  411. else
  412. break;
  413. end;
  414. read_simple_expr:=hs1;
  415. end;
  416. function read_expr : string;
  417. var
  418. hs1,hs2 : string;
  419. b : boolean;
  420. t : ttoken;
  421. w : integer;
  422. l1,l2 : longint;
  423. begin
  424. hs1:=read_simple_expr;
  425. t:=current_scanner.preproc_token;
  426. if not(t in [_EQUAL,_UNEQUAL,_LT,_GT,_LTE,_GTE]) then
  427. begin
  428. read_expr:=hs1;
  429. exit;
  430. end;
  431. preproc_consume(t);
  432. hs2:=read_simple_expr;
  433. if is_number(hs1) and is_number(hs2) then
  434. begin
  435. valint(hs1,l1,w);
  436. valint(hs2,l2,w);
  437. case t of
  438. _EQUAL : b:=l1=l2;
  439. _UNEQUAL : b:=l1<>l2;
  440. _LT : b:=l1<l2;
  441. _GT : b:=l1>l2;
  442. _GTE : b:=l1>=l2;
  443. _LTE : b:=l1<=l2;
  444. end;
  445. end
  446. else
  447. begin
  448. case t of
  449. _EQUAL : b:=hs1=hs2;
  450. _UNEQUAL : b:=hs1<>hs2;
  451. _LT : b:=hs1<hs2;
  452. _GT : b:=hs1>hs2;
  453. _GTE : b:=hs1>=hs2;
  454. _LTE : b:=hs1<=hs2;
  455. end;
  456. end;
  457. if b then
  458. read_expr:='1'
  459. else
  460. read_expr:='0';
  461. end;
  462. var
  463. hs : string;
  464. begin
  465. current_scanner.skipspace;
  466. { start preproc expression scanner }
  467. current_scanner.preproc_token:=current_scanner.readpreproc;
  468. hs:=read_expr;
  469. current_scanner.addpreprocstack(pp_if,hs<>'0',hs,scan_c_if_found);
  470. end;
  471. procedure dir_define;
  472. var
  473. hs : string;
  474. bracketcount : longint;
  475. mac : tmacro;
  476. macropos : longint;
  477. macrobuffer : pmacrobuffer;
  478. begin
  479. current_scanner.skipspace;
  480. hs:=current_scanner.readid;
  481. mac:=tmacro(current_scanner.macros.search(hs));
  482. if not assigned(mac) then
  483. begin
  484. mac:=tmacro.create(hs);
  485. mac.defined:=true;
  486. Message1(parser_m_macro_defined,mac.name);
  487. current_scanner.macros.insert(mac);
  488. end
  489. else
  490. begin
  491. Message1(parser_m_macro_defined,mac.name);
  492. mac.defined:=true;
  493. { delete old definition }
  494. if assigned(mac.buftext) then
  495. begin
  496. freemem(mac.buftext,mac.buflen);
  497. mac.buftext:=nil;
  498. end;
  499. end;
  500. mac.is_used:=true;
  501. if (cs_support_macro in aktmoduleswitches) then
  502. begin
  503. { key words are never substituted }
  504. if is_keyword(hs) then
  505. Message(scan_e_keyword_cant_be_a_macro);
  506. { !!!!!! handle macro params, need we this? }
  507. current_scanner.skipspace;
  508. { may be a macro? }
  509. if c=':' then
  510. begin
  511. current_scanner.readchar;
  512. if c='=' then
  513. begin
  514. new(macrobuffer);
  515. macropos:=0;
  516. { parse macro, brackets are counted so it's possible
  517. to have a $ifdef etc. in the macro }
  518. bracketcount:=0;
  519. repeat
  520. current_scanner.readchar;
  521. case c of
  522. '}' :
  523. if (bracketcount=0) then
  524. break
  525. else
  526. dec(bracketcount);
  527. '{' :
  528. inc(bracketcount);
  529. #26 :
  530. current_scanner.end_of_file;
  531. end;
  532. macrobuffer^[macropos]:=c;
  533. inc(macropos);
  534. if macropos>maxmacrolen then
  535. Message(scan_f_macro_buffer_overflow);
  536. until false;
  537. { free buffer of macro ?}
  538. if assigned(mac.buftext) then
  539. freemem(mac.buftext,mac.buflen);
  540. { get new mem }
  541. getmem(mac.buftext,macropos);
  542. mac.buflen:=macropos;
  543. { copy the text }
  544. move(macrobuffer^,mac.buftext^,macropos);
  545. dispose(macrobuffer);
  546. end;
  547. end;
  548. end
  549. else
  550. begin
  551. { check if there is an assignment, then we need to give a
  552. warning }
  553. current_scanner.skipspace;
  554. if c=':' then
  555. begin
  556. current_scanner.readchar;
  557. if c='=' then
  558. Message(scan_w_macro_support_turned_off);
  559. end;
  560. end;
  561. end;
  562. procedure dir_undef;
  563. var
  564. hs : string;
  565. mac : tmacro;
  566. begin
  567. current_scanner.skipspace;
  568. hs:=current_scanner.readid;
  569. mac:=tmacro(current_scanner.macros.search(hs));
  570. if not assigned(mac) then
  571. begin
  572. mac:=tmacro.create(hs);
  573. Message1(parser_m_macro_undefined,mac.name);
  574. mac.defined:=false;
  575. current_scanner.macros.insert(mac);
  576. end
  577. else
  578. begin
  579. Message1(parser_m_macro_undefined,mac.name);
  580. mac.defined:=false;
  581. { delete old definition }
  582. if assigned(mac.buftext) then
  583. begin
  584. freemem(mac.buftext,mac.buflen);
  585. mac.buftext:=nil;
  586. end;
  587. end;
  588. mac.is_used:=true;
  589. end;
  590. procedure dir_include;
  591. function findincludefile(const path,name,ext:string;var foundfile:string):boolean;
  592. var
  593. found : boolean;
  594. hpath : string;
  595. begin
  596. { look for the include file
  597. 1. specified path,path of current inputfile,current dir
  598. 2. local includepath
  599. 3. global includepath }
  600. found:=false;
  601. foundfile:='';
  602. hpath:='';
  603. if path<>'' then
  604. begin
  605. if not path_absolute(path) then
  606. hpath:=current_scanner.inputfile.path^+path
  607. else
  608. hpath:=path+';'+current_scanner.inputfile.path^;
  609. end
  610. else
  611. hpath:=current_scanner.inputfile.path^;
  612. found:=FindFile(name+ext,hpath+';.'+source_info.DirSep,foundfile);
  613. if (not found) then
  614. found:=current_module.localincludesearchpath.FindFile(name+ext,foundfile);
  615. if (not found) then
  616. found:=includesearchpath.FindFile(name+ext,foundfile);
  617. findincludefile:=found;
  618. end;
  619. var
  620. args,
  621. foundfile,
  622. hs : string;
  623. path : dirstr;
  624. name : namestr;
  625. ext : extstr;
  626. hp : tinputfile;
  627. found : boolean;
  628. begin
  629. current_scanner.skipspace;
  630. args:=current_scanner.readcomment;
  631. hs:=GetToken(args,' ');
  632. if hs='' then
  633. exit;
  634. if (hs[1]='%') then
  635. begin
  636. { case insensitive }
  637. hs:=upper(hs);
  638. { remove %'s }
  639. Delete(hs,1,1);
  640. if hs[length(hs)]='%' then
  641. Delete(hs,length(hs),1);
  642. { save old }
  643. path:=hs;
  644. { first check for internal macros }
  645. if hs='TIME' then
  646. hs:=gettimestr
  647. else
  648. if hs='DATE' then
  649. hs:=getdatestr
  650. else
  651. if hs='FILE' then
  652. hs:=current_module.sourcefiles.get_file_name(aktfilepos.fileindex)
  653. else
  654. if hs='LINE' then
  655. hs:=tostr(aktfilepos.line)
  656. else
  657. if hs='FPCVERSION' then
  658. hs:=version_string
  659. else
  660. if hs='FPCTARGET' then
  661. hs:=target_cpu_string
  662. else
  663. if hs='FPCTARGETCPU' then
  664. hs:=target_cpu_string
  665. else
  666. if hs='FPCTARGETOS' then
  667. hs:=target_info.shortname
  668. else
  669. hs:=getenv(hs);
  670. if hs='' then
  671. Message1(scan_w_include_env_not_found,path);
  672. { make it a stringconst }
  673. hs:=''''+hs+'''';
  674. current_scanner.insertmacro(path,@hs[1],length(hs),
  675. current_scanner.line_no,current_scanner.inputfile.ref_index);
  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. fileinfo:=akttokenpos;
  724. is_used:=false;
  725. buftext:=nil;
  726. buflen:=0;
  727. end;
  728. destructor tmacro.destroy;
  729. begin
  730. if assigned(buftext) then
  731. freemem(buftext,buflen);
  732. inherited destroy;
  733. end;
  734. {*****************************************************************************
  735. Preprocessor writting
  736. *****************************************************************************}
  737. {$ifdef PREPROCWRITE}
  738. constructor tpreprocfile.create(const fn:string);
  739. begin
  740. { open outputfile }
  741. assign(f,fn);
  742. {$I-}
  743. rewrite(f);
  744. {$I+}
  745. if ioresult<>0 then
  746. Comment(V_Fatal,'can''t create file '+fn);
  747. getmem(buf,preprocbufsize);
  748. settextbuf(f,buf^,preprocbufsize);
  749. { reset }
  750. eolfound:=false;
  751. spacefound:=false;
  752. end;
  753. destructor tpreprocfile.destroy;
  754. begin
  755. close(f);
  756. freemem(buf,preprocbufsize);
  757. end;
  758. procedure tpreprocfile.add(const s:string);
  759. begin
  760. write(f,s);
  761. end;
  762. procedure tpreprocfile.addspace;
  763. begin
  764. if eolfound then
  765. begin
  766. writeln(f,'');
  767. eolfound:=false;
  768. spacefound:=false;
  769. end
  770. else
  771. if spacefound then
  772. begin
  773. write(f,' ');
  774. spacefound:=false;
  775. end;
  776. end;
  777. {$endif PREPROCWRITE}
  778. {*****************************************************************************
  779. TPreProcStack
  780. *****************************************************************************}
  781. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  782. begin
  783. accept:=a;
  784. typ:=atyp;
  785. next:=n;
  786. end;
  787. {*****************************************************************************
  788. TDirectiveItem
  789. *****************************************************************************}
  790. constructor TDirectiveItem.Create(const n:string;p:tdirectiveproc);
  791. begin
  792. inherited CreateName(n);
  793. is_conditional:=false;
  794. proc:={$ifndef FPCPROCVAR}@{$endif}p;
  795. end;
  796. constructor TDirectiveItem.CreateCond(const n:string;p:tdirectiveproc);
  797. begin
  798. inherited CreateName(n);
  799. is_conditional:=true;
  800. proc:={$ifndef FPCPROCVAR}@{$endif}p;
  801. end;
  802. {****************************************************************************
  803. TSCANNERFILE
  804. ****************************************************************************}
  805. constructor tscannerfile.create(const fn:string);
  806. begin
  807. inputfile:=do_openinputfile(fn);
  808. if assigned(current_module) then
  809. current_module.sourcefiles.register_file(inputfile);
  810. { reset localinput }
  811. inputbuffer:=nil;
  812. inputpointer:=nil;
  813. inputstart:=0;
  814. { reset scanner }
  815. preprocstack:=nil;
  816. comment_level:=0;
  817. yylexcount:=0;
  818. block_type:=bt_general;
  819. line_no:=0;
  820. lastlinepos:=0;
  821. lasttokenpos:=0;
  822. lasttoken:=NOTOKEN;
  823. nexttoken:=NOTOKEN;
  824. lastasmgetchar:=#0;
  825. ignoredirectives:=TStringList.Create;
  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 assigned(current_module) and
  845. (not current_module.in_second_load) and
  846. (status.errorcount=0) then
  847. checkpreprocstack
  848. else
  849. begin
  850. while assigned(preprocstack) do
  851. poppreprocstack;
  852. end;
  853. if not inputfile.closed then
  854. closeinputfile;
  855. ignoredirectives.free;
  856. macros.free;
  857. end;
  858. procedure tscannerfile.def_macro(const s : string);
  859. var
  860. mac : tmacro;
  861. begin
  862. mac:=tmacro(macros.search(s));
  863. if mac=nil then
  864. begin
  865. mac:=tmacro.create(s);
  866. Message1(parser_m_macro_defined,mac.name);
  867. macros.insert(mac);
  868. end;
  869. mac.defined:=true;
  870. mac.defined_at_startup:=true;
  871. end;
  872. procedure tscannerfile.set_macro(const s : string;value : string);
  873. var
  874. mac : tmacro;
  875. begin
  876. mac:=tmacro(macros.search(s));
  877. if mac=nil then
  878. begin
  879. mac:=tmacro.create(s);
  880. macros.insert(mac);
  881. end
  882. else
  883. begin
  884. if assigned(mac.buftext) then
  885. freemem(mac.buftext,mac.buflen);
  886. end;
  887. Message2(parser_m_macro_set_to,mac.name,value);
  888. mac.buflen:=length(value);
  889. getmem(mac.buftext,mac.buflen);
  890. move(value[1],mac.buftext^,mac.buflen);
  891. mac.defined:=true;
  892. mac.defined_at_startup:=true;
  893. end;
  894. function tscannerfile.openinputfile:boolean;
  895. begin
  896. openinputfile:=inputfile.open;
  897. { load buffer }
  898. inputbuffer:=inputfile.buf;
  899. inputpointer:=inputfile.buf;
  900. inputstart:=inputfile.bufstart;
  901. { line }
  902. line_no:=0;
  903. lastlinepos:=0;
  904. lasttokenpos:=0;
  905. end;
  906. procedure tscannerfile.closeinputfile;
  907. begin
  908. inputfile.close;
  909. { reset buffer }
  910. inputbuffer:=nil;
  911. inputpointer:=nil;
  912. inputstart:=0;
  913. { reset line }
  914. line_no:=0;
  915. lastlinepos:=0;
  916. lasttokenpos:=0;
  917. end;
  918. function tscannerfile.tempopeninputfile:boolean;
  919. begin
  920. tempopeninputfile:=inputfile.tempopen;
  921. { reload buffer }
  922. inputbuffer:=inputfile.buf;
  923. inputpointer:=inputfile.buf;
  924. inputstart:=inputfile.bufstart;
  925. end;
  926. procedure tscannerfile.tempcloseinputfile;
  927. begin
  928. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  929. inputfile.tempclose;
  930. { reset buffer }
  931. inputbuffer:=nil;
  932. inputpointer:=nil;
  933. inputstart:=0;
  934. end;
  935. procedure tscannerfile.saveinputfile;
  936. begin
  937. inputfile.saveinputpointer:=inputpointer;
  938. inputfile.savelastlinepos:=lastlinepos;
  939. inputfile.saveline_no:=line_no;
  940. end;
  941. procedure tscannerfile.restoreinputfile;
  942. begin
  943. inputpointer:=inputfile.saveinputpointer;
  944. lastlinepos:=inputfile.savelastlinepos;
  945. line_no:=inputfile.saveline_no;
  946. if not inputfile.is_macro then
  947. parser_current_file:=inputfile.name^;
  948. end;
  949. procedure tscannerfile.nextfile;
  950. var
  951. to_dispose : tinputfile;
  952. begin
  953. if assigned(inputfile.next) then
  954. begin
  955. if inputfile.is_macro then
  956. to_dispose:=inputfile
  957. else
  958. to_dispose:=nil;
  959. { we can allways close the file, no ? }
  960. inputfile.close;
  961. inputfile:=inputfile.next;
  962. if assigned(to_dispose) then
  963. to_dispose.free;
  964. restoreinputfile;
  965. end;
  966. end;
  967. procedure tscannerfile.addfile(hp:tinputfile);
  968. begin
  969. saveinputfile;
  970. { add to list }
  971. hp.next:=inputfile;
  972. inputfile:=hp;
  973. { load new inputfile }
  974. restoreinputfile;
  975. end;
  976. procedure tscannerfile.reload;
  977. begin
  978. with inputfile do
  979. begin
  980. { when nothing more to read then leave immediatly, so we
  981. don't change the aktfilepos and leave it point to the last
  982. char }
  983. if (c=#26) and (not assigned(next)) then
  984. exit;
  985. repeat
  986. { still more to read?, then change the #0 to a space so its seen
  987. as a seperator, this can't be used for macro's which can change
  988. the place of the #0 in the buffer with tempopen }
  989. if (c=#0) and (bufsize>0) and
  990. not(inputfile.is_macro) and
  991. (inputpointer-inputbuffer<bufsize) then
  992. begin
  993. c:=' ';
  994. inc(longint(inputpointer));
  995. exit;
  996. end;
  997. { can we read more from this file ? }
  998. if (c<>#26) and (not endoffile) then
  999. begin
  1000. readbuf;
  1001. inputpointer:=buf;
  1002. inputbuffer:=buf;
  1003. inputstart:=bufstart;
  1004. { first line? }
  1005. if line_no=0 then
  1006. begin
  1007. line_no:=1;
  1008. if cs_asm_source in aktglobalswitches then
  1009. inputfile.setline(line_no,bufstart);
  1010. end;
  1011. end
  1012. else
  1013. begin
  1014. { load eof position in tokenpos/aktfilepos }
  1015. gettokenpos;
  1016. { close file }
  1017. closeinputfile;
  1018. { no next module, than EOF }
  1019. if not assigned(inputfile.next) then
  1020. begin
  1021. c:=#26;
  1022. exit;
  1023. end;
  1024. { load next file and reopen it }
  1025. nextfile;
  1026. tempopeninputfile;
  1027. { status }
  1028. Message1(scan_t_back_in,inputfile.name^);
  1029. end;
  1030. { load next char }
  1031. c:=inputpointer^;
  1032. inc(longint(inputpointer));
  1033. until c<>#0; { if also end, then reload again }
  1034. end;
  1035. end;
  1036. procedure tscannerfile.insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  1037. var
  1038. hp : tinputfile;
  1039. begin
  1040. { save old postion and decrease linebreak }
  1041. if c=newline then
  1042. dec(line_no);
  1043. dec(longint(inputpointer));
  1044. tempcloseinputfile;
  1045. { create macro 'file' }
  1046. { use special name to dispose after !! }
  1047. hp:=do_openinputfile('_Macro_.'+macname);
  1048. addfile(hp);
  1049. with inputfile do
  1050. begin
  1051. setmacro(p,len);
  1052. { local buffer }
  1053. inputbuffer:=buf;
  1054. inputpointer:=buf;
  1055. inputstart:=bufstart;
  1056. ref_index:=fileindex;
  1057. end;
  1058. { reset line }
  1059. line_no:=line;
  1060. lastlinepos:=0;
  1061. lasttokenpos:=0;
  1062. { load new c }
  1063. c:=inputpointer^;
  1064. inc(longint(inputpointer));
  1065. end;
  1066. procedure tscannerfile.gettokenpos;
  1067. { load the values of tokenpos and lasttokenpos }
  1068. begin
  1069. lasttokenpos:=inputstart+(inputpointer-inputbuffer);
  1070. akttokenpos.line:=line_no;
  1071. akttokenpos.column:=lasttokenpos-lastlinepos;
  1072. akttokenpos.fileindex:=inputfile.ref_index;
  1073. aktfilepos:=akttokenpos;
  1074. end;
  1075. procedure tscannerfile.inc_comment_level;
  1076. var
  1077. oldaktfilepos : tfileposinfo;
  1078. begin
  1079. if (m_nested_comment in aktmodeswitches) then
  1080. inc(comment_level)
  1081. else
  1082. comment_level:=1;
  1083. if (comment_level>1) then
  1084. begin
  1085. oldaktfilepos:=aktfilepos;
  1086. gettokenpos; { update for warning }
  1087. Message1(scan_w_comment_level,tostr(comment_level));
  1088. aktfilepos:=oldaktfilepos;
  1089. end;
  1090. end;
  1091. procedure tscannerfile.dec_comment_level;
  1092. begin
  1093. if (m_nested_comment in aktmodeswitches) then
  1094. dec(comment_level)
  1095. else
  1096. comment_level:=0;
  1097. end;
  1098. procedure tscannerfile.linebreak;
  1099. var
  1100. cur : char;
  1101. oldtokenpos,
  1102. oldaktfilepos : tfileposinfo;
  1103. begin
  1104. with inputfile do
  1105. begin
  1106. if (byte(inputpointer^)=0) and not(endoffile) then
  1107. begin
  1108. cur:=c;
  1109. reload;
  1110. if byte(cur)+byte(c)<>23 then
  1111. dec(longint(inputpointer));
  1112. end
  1113. else
  1114. begin
  1115. { Fix linebreak to be only newline (=#10) for all types of linebreaks }
  1116. if (byte(inputpointer^)+byte(c)=23) then
  1117. inc(longint(inputpointer));
  1118. end;
  1119. c:=newline;
  1120. { increase line counters }
  1121. lastlinepos:=bufstart+(inputpointer-inputbuffer);
  1122. inc(line_no);
  1123. { update linebuffer }
  1124. if cs_asm_source in aktglobalswitches then
  1125. inputfile.setline(line_no,lastlinepos);
  1126. { update for status and call the show status routine,
  1127. but don't touch aktfilepos ! }
  1128. oldaktfilepos:=aktfilepos;
  1129. oldtokenpos:=akttokenpos;
  1130. gettokenpos; { update for v_status }
  1131. inc(status.compiledlines);
  1132. ShowStatus;
  1133. aktfilepos:=oldaktfilepos;
  1134. akttokenpos:=oldtokenpos;
  1135. end;
  1136. end;
  1137. procedure tscannerfile.illegal_char(c:char);
  1138. var
  1139. s : string;
  1140. begin
  1141. if c in [#32..#255] then
  1142. s:=''''+c+''''
  1143. else
  1144. s:='#'+tostr(ord(c));
  1145. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  1146. end;
  1147. procedure tscannerfile.end_of_file;
  1148. begin
  1149. checkpreprocstack;
  1150. Message(scan_f_end_of_file);
  1151. end;
  1152. {-------------------------------------------
  1153. IF Conditional Handling
  1154. -------------------------------------------}
  1155. procedure tscannerfile.checkpreprocstack;
  1156. begin
  1157. { check for missing ifdefs }
  1158. while assigned(preprocstack) do
  1159. begin
  1160. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  1161. preprocstack.owner.inputfile.name^,tostr(preprocstack.line_nb));
  1162. poppreprocstack;
  1163. end;
  1164. end;
  1165. procedure tscannerfile.poppreprocstack;
  1166. var
  1167. hp : tpreprocstack;
  1168. begin
  1169. if assigned(preprocstack) then
  1170. begin
  1171. Message1(scan_c_endif_found,preprocstack.name);
  1172. hp:=preprocstack.next;
  1173. preprocstack.free;
  1174. preprocstack:=hp;
  1175. end
  1176. else
  1177. Message(scan_e_endif_without_if);
  1178. end;
  1179. procedure tscannerfile.addpreprocstack(atyp : preproctyp;a:boolean;const s:string;w:longint);
  1180. begin
  1181. preprocstack:=tpreprocstack.create(atyp,((preprocstack=nil) or preprocstack.accept) and a,preprocstack);
  1182. preprocstack.name:=s;
  1183. preprocstack.line_nb:=line_no;
  1184. preprocstack.owner:=self;
  1185. if preprocstack.accept then
  1186. Message2(w,preprocstack.name,'accepted')
  1187. else
  1188. Message2(w,preprocstack.name,'rejected');
  1189. end;
  1190. procedure tscannerfile.elsepreprocstack;
  1191. begin
  1192. if assigned(preprocstack) then
  1193. begin
  1194. preprocstack.typ:=pp_else;
  1195. preprocstack.line_nb:=line_no;
  1196. if not(assigned(preprocstack.next)) or (preprocstack.next.accept) then
  1197. preprocstack.accept:=not preprocstack.accept;
  1198. if preprocstack.accept then
  1199. Message2(scan_c_else_found,preprocstack.name,'accepted')
  1200. else
  1201. Message2(scan_c_else_found,preprocstack.name,'rejected');
  1202. end
  1203. else
  1204. Message(scan_e_endif_without_if);
  1205. end;
  1206. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  1207. var
  1208. oldaktfilepos : tfileposinfo;
  1209. begin
  1210. oldaktfilepos:=aktfilepos;
  1211. repeat
  1212. current_scanner.gettokenpos;
  1213. p.proc{$ifdef FPCPROCVAR}(){$endif};
  1214. { accept the text ? }
  1215. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  1216. break
  1217. else
  1218. begin
  1219. current_scanner.gettokenpos;
  1220. Message(scan_c_skipping_until);
  1221. repeat
  1222. current_scanner.skipuntildirective;
  1223. p:=tdirectiveitem(scannerdirectives.search(current_scanner.readid));
  1224. until assigned(p) and (p.is_conditional);
  1225. current_scanner.gettokenpos;
  1226. Message1(scan_d_handling_switch,'$'+p.name);
  1227. end;
  1228. until false;
  1229. aktfilepos:=oldaktfilepos;
  1230. end;
  1231. procedure tscannerfile.handledirectives;
  1232. var
  1233. t : tdirectiveitem;
  1234. hs : string;
  1235. begin
  1236. gettokenpos;
  1237. readchar; {Remove the $}
  1238. hs:=readid;
  1239. {$ifdef PREPROCWRITE}
  1240. if parapreprocess then
  1241. begin
  1242. t:=Get_Directive(hs);
  1243. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  1244. begin
  1245. preprocfile^.AddSpace;
  1246. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  1247. exit;
  1248. end;
  1249. end;
  1250. {$endif PREPROCWRITE}
  1251. { skip this directive? }
  1252. if (ignoredirectives.find(hs)<>nil) then
  1253. begin
  1254. if (comment_level>0) then
  1255. readcomment;
  1256. { we've read the whole comment }
  1257. aktcommentstyle:=comment_none;
  1258. exit;
  1259. end;
  1260. if hs='' then
  1261. begin
  1262. Message1(scan_w_illegal_switch,'$'+hs);
  1263. end;
  1264. { Check for compiler switches }
  1265. while (length(hs)=1) and (c in ['-','+']) do
  1266. begin
  1267. HandleSwitch(hs[1],c);
  1268. current_scanner.readchar; {Remove + or -}
  1269. if c=',' then
  1270. begin
  1271. current_scanner.readchar; {Remove , }
  1272. { read next switch, support $v+,$+}
  1273. hs:=current_scanner.readid;
  1274. if (hs='') then
  1275. begin
  1276. if (c='$') and (m_fpc in aktmodeswitches) then
  1277. begin
  1278. current_scanner.readchar; { skip $ }
  1279. hs:=current_scanner.readid;
  1280. end;
  1281. if (hs='') then
  1282. Message1(scan_w_illegal_directive,'$'+c);
  1283. end
  1284. else
  1285. Message1(scan_d_handling_switch,'$'+hs);
  1286. end
  1287. else
  1288. hs:='';
  1289. end;
  1290. { directives may follow switches after a , }
  1291. if hs<>'' then
  1292. begin
  1293. t:=tdirectiveitem(scannerdirectives.search(hs));
  1294. if assigned(t) then
  1295. begin
  1296. if t.is_conditional then
  1297. handleconditional(t)
  1298. else
  1299. begin
  1300. Message1(scan_d_handling_switch,'$'+hs);
  1301. t.proc{$ifdef FPCPROCVAR}(){$endif};
  1302. end;
  1303. end
  1304. else
  1305. begin
  1306. current_scanner.ignoredirectives.insert(hs);
  1307. Message1(scan_w_illegal_directive,'$'+hs);
  1308. end;
  1309. { conditionals already read the comment }
  1310. if (current_scanner.comment_level>0) then
  1311. current_scanner.readcomment;
  1312. { we've read the whole comment }
  1313. aktcommentstyle:=comment_none;
  1314. end;
  1315. end;
  1316. procedure tscannerfile.readchar;
  1317. begin
  1318. c:=inputpointer^;
  1319. if c=#0 then
  1320. reload
  1321. else
  1322. inc(longint(inputpointer));
  1323. case c of
  1324. #26 : reload;
  1325. #10,
  1326. #13 : linebreak;
  1327. end;
  1328. end;
  1329. procedure tscannerfile.readstring;
  1330. var
  1331. i : longint;
  1332. begin
  1333. i:=0;
  1334. repeat
  1335. case c of
  1336. '_',
  1337. '0'..'9',
  1338. 'A'..'Z' : begin
  1339. if i<255 then
  1340. begin
  1341. inc(i);
  1342. orgpattern[i]:=c;
  1343. pattern[i]:=c;
  1344. end;
  1345. c:=inputpointer^;
  1346. inc(longint(inputpointer));
  1347. end;
  1348. 'a'..'z' : begin
  1349. if i<255 then
  1350. begin
  1351. inc(i);
  1352. orgpattern[i]:=c;
  1353. pattern[i]:=chr(ord(c)-32)
  1354. end;
  1355. c:=inputpointer^;
  1356. inc(longint(inputpointer));
  1357. end;
  1358. #0 : reload;
  1359. #26 : begin
  1360. reload;
  1361. if c=#26 then
  1362. break;
  1363. end;
  1364. #13,#10 : begin
  1365. linebreak;
  1366. break;
  1367. end;
  1368. else
  1369. break;
  1370. end;
  1371. until false;
  1372. orgpattern[0]:=chr(i);
  1373. pattern[0]:=chr(i);
  1374. end;
  1375. procedure tscannerfile.readnumber;
  1376. var
  1377. base,
  1378. i : longint;
  1379. begin
  1380. case c of
  1381. '%' : begin
  1382. readchar;
  1383. base:=2;
  1384. pattern[1]:='%';
  1385. i:=1;
  1386. end;
  1387. '&' : begin
  1388. readchar;
  1389. base:=8;
  1390. pattern[1]:='&';
  1391. i:=1;
  1392. end;
  1393. '$' : begin
  1394. readchar;
  1395. base:=16;
  1396. pattern[1]:='$';
  1397. i:=1;
  1398. end;
  1399. else
  1400. begin
  1401. base:=10;
  1402. i:=0;
  1403. end;
  1404. end;
  1405. while ((base>=10) and (c in ['0'..'9'])) or
  1406. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  1407. ((base=8) and (c in ['0'..'7'])) or
  1408. ((base=2) and (c in ['0'..'1'])) do
  1409. begin
  1410. if i<255 then
  1411. begin
  1412. inc(i);
  1413. pattern[i]:=c;
  1414. end;
  1415. { get next char }
  1416. c:=inputpointer^;
  1417. if c=#0 then
  1418. reload
  1419. else
  1420. inc(longint(inputpointer));
  1421. end;
  1422. { was the next char a linebreak ? }
  1423. case c of
  1424. #26 : reload;
  1425. #10,
  1426. #13 : linebreak;
  1427. end;
  1428. pattern[0]:=chr(i);
  1429. end;
  1430. function tscannerfile.readid:string;
  1431. begin
  1432. readstring;
  1433. readid:=pattern;
  1434. end;
  1435. function tscannerfile.readval:longint;
  1436. var
  1437. l : longint;
  1438. w : integer;
  1439. begin
  1440. readnumber;
  1441. valint(pattern,l,w);
  1442. readval:=l;
  1443. end;
  1444. function tscannerfile.readcomment:string;
  1445. var
  1446. i : longint;
  1447. begin
  1448. i:=0;
  1449. repeat
  1450. case c of
  1451. '{' :
  1452. if aktcommentstyle=comment_tp then
  1453. inc_comment_level;
  1454. '}' :
  1455. if aktcommentstyle=comment_tp then
  1456. begin
  1457. readchar;
  1458. dec_comment_level;
  1459. if comment_level=0 then
  1460. break
  1461. else
  1462. continue;
  1463. end;
  1464. '*' :
  1465. if aktcommentstyle=comment_oldtp then
  1466. begin
  1467. readchar;
  1468. if c=')' then
  1469. begin
  1470. readchar;
  1471. dec_comment_level;
  1472. break;
  1473. end
  1474. else
  1475. { Add both characters !!}
  1476. if (i<255) then
  1477. begin
  1478. inc(i);
  1479. readcomment[i]:='*';
  1480. if (i<255) then
  1481. begin
  1482. inc(i);
  1483. readcomment[i]:='*';
  1484. end;
  1485. end;
  1486. end
  1487. else
  1488. { Not old TP comment, so add...}
  1489. begin
  1490. if (i<255) then
  1491. begin
  1492. inc(i);
  1493. readcomment[i]:='*';
  1494. end;
  1495. end;
  1496. #26 :
  1497. end_of_file;
  1498. else
  1499. begin
  1500. if (i<255) then
  1501. begin
  1502. inc(i);
  1503. readcomment[i]:=c;
  1504. end;
  1505. end;
  1506. end;
  1507. c:=inputpointer^;
  1508. if c=#0 then
  1509. reload
  1510. else
  1511. inc(longint(inputpointer));
  1512. if c in [#10,#13] then
  1513. linebreak;
  1514. until false;
  1515. readcomment[0]:=chr(i);
  1516. end;
  1517. function tscannerfile.readstate:char;
  1518. var
  1519. state : char;
  1520. begin
  1521. state:=' ';
  1522. if c=' ' then
  1523. begin
  1524. current_scanner.skipspace;
  1525. current_scanner.readid;
  1526. if pattern='ON' then
  1527. state:='+'
  1528. else
  1529. if pattern='OFF' then
  1530. state:='-';
  1531. end
  1532. else
  1533. state:=c;
  1534. if not (state in ['+','-']) then
  1535. Message(scan_e_wrong_switch_toggle);
  1536. readstate:=state;
  1537. end;
  1538. procedure tscannerfile.skipspace;
  1539. begin
  1540. while c in [' ',#9..#13] do
  1541. begin
  1542. c:=inputpointer^;
  1543. if c=#0 then
  1544. reload
  1545. else
  1546. inc(longint(inputpointer));
  1547. case c of
  1548. #26 :
  1549. reload;
  1550. #10,
  1551. #13 :
  1552. linebreak;
  1553. end;
  1554. end;
  1555. end;
  1556. procedure tscannerfile.skipuntildirective;
  1557. var
  1558. incomment : boolean;
  1559. found : longint;
  1560. next_char_loaded : boolean;
  1561. oldcommentstyle : tcommentstyle;
  1562. begin
  1563. found:=0;
  1564. next_char_loaded:=false;
  1565. incomment:=true;
  1566. oldcommentstyle:=aktcommentstyle;
  1567. repeat
  1568. case c of
  1569. #26 :
  1570. end_of_file;
  1571. '{' :
  1572. begin
  1573. if not(m_nested_comment in aktmodeswitches) or
  1574. (comment_level=0) then
  1575. begin
  1576. found:=1;
  1577. aktcommentstyle:=comment_tp;
  1578. end;
  1579. inc_comment_level;
  1580. incomment:=true;
  1581. end;
  1582. '*' :
  1583. begin
  1584. if incomment then
  1585. begin
  1586. readchar;
  1587. if c=')' then
  1588. begin
  1589. dec_comment_level;
  1590. found:=0;
  1591. incomment:=false;
  1592. end
  1593. else
  1594. next_char_loaded:=true;
  1595. end
  1596. else
  1597. found := 0;
  1598. end;
  1599. '}' :
  1600. begin
  1601. dec_comment_level;
  1602. found:=0;
  1603. incomment:=false;
  1604. end;
  1605. '$' :
  1606. begin
  1607. if found=1 then
  1608. found:=2;
  1609. end;
  1610. '''' :
  1611. if not incomment then
  1612. begin
  1613. repeat
  1614. readchar;
  1615. case c of
  1616. #26 :
  1617. end_of_file;
  1618. newline :
  1619. break;
  1620. '''' :
  1621. begin
  1622. readchar;
  1623. if c<>'''' then
  1624. begin
  1625. next_char_loaded:=true;
  1626. break;
  1627. end;
  1628. end;
  1629. end;
  1630. until false;
  1631. end;
  1632. '(' :
  1633. begin
  1634. if not incomment then
  1635. begin
  1636. readchar;
  1637. if c='*' then
  1638. begin
  1639. readchar;
  1640. if c='$' then
  1641. begin
  1642. found:=2;
  1643. inc_comment_level;
  1644. aktcommentstyle:=comment_oldtp;
  1645. end
  1646. else
  1647. begin
  1648. skipoldtpcomment;
  1649. aktcommentstyle:=oldcommentstyle;
  1650. end;
  1651. end
  1652. else
  1653. next_char_loaded:=true;
  1654. end
  1655. else
  1656. found:=0;
  1657. end;
  1658. '/' :
  1659. begin
  1660. if not incomment then
  1661. begin
  1662. readchar;
  1663. if c='/' then
  1664. begin
  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.55 2002-12-27 18:05:58 peter
  2588. * use gettoken to get filename for include
  2589. Revision 1.54 2002/12/27 16:45:50 peter
  2590. * fix delphi comment parsing when skipping preproc directive
  2591. Revision 1.53 2002/12/27 15:26:43 peter
  2592. * give an error when no symbol is specified after $if(n)def
  2593. Revision 1.52 2002/12/24 23:32:02 peter
  2594. * support quotes around include filenames
  2595. Revision 1.51 2002/12/05 19:27:00 carl
  2596. * remove a stupid thing that i commited
  2597. Revision 1.50 2002/11/29 22:31:19 carl
  2598. + unimplemented hint directive added
  2599. * hint directive parsing implemented
  2600. * warning on these directives
  2601. Revision 1.49 2002/11/26 22:56:40 peter
  2602. * fix macro nesting check
  2603. Revision 1.48 2002/09/16 19:05:48 peter
  2604. * parse ^ after nil as caret
  2605. Revision 1.47 2002/09/06 14:58:42 carl
  2606. * bugfix of bug report 2072 (merged)
  2607. Revision 1.46 2002/09/05 19:27:05 peter
  2608. * fixed crash when current_module becomes nil
  2609. Revision 1.45 2002/09/05 14:17:27 pierre
  2610. * fix for bug 2004 merged
  2611. Revision 1.44 2002/08/12 16:46:04 peter
  2612. * tscannerfile is now destroyed in tmodule.reset and current_scanner
  2613. is updated accordingly. This removes all the loading and saving of
  2614. the old scanner and the invalid flag marking
  2615. Revision 1.43 2002/08/11 14:28:19 peter
  2616. * TScannerFile.SetInvalid added that will also reset inputfile
  2617. Revision 1.42 2002/08/10 14:46:31 carl
  2618. + moved target_cpu_string to cpuinfo
  2619. * renamed asmmode enum.
  2620. * assembler reader has now less ifdef's
  2621. * move from nppcmem.pas -> ncgmem.pas vec. node.
  2622. Revision 1.41 2002/08/06 21:12:16 florian
  2623. + support for octal constants, they are specified by a leading &
  2624. Revision 1.40 2002/07/20 17:35:52 florian
  2625. + char constants specified with #.. with more than 3 digits are handled as widechar
  2626. Revision 1.39 2002/05/18 13:34:17 peter
  2627. * readded missing revisions
  2628. Revision 1.38 2002/05/16 19:46:44 carl
  2629. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2630. + try to fix temp allocation (still in ifdef)
  2631. + generic constructor calls
  2632. + start of tassembler / tmodulebase class cleanup
  2633. Revision 1.36 2002/04/21 18:57:23 peter
  2634. * fixed memleaks when file can't be opened
  2635. Revision 1.35 2002/04/21 15:22:26 carl
  2636. * first check .inc file extension
  2637. Revision 1.34 2002/04/21 07:24:09 carl
  2638. - remove my fixes until Peter agrees on the fix (sorry Peter)
  2639. Revision 1.32 2002/04/19 15:42:11 peter
  2640. * default extension checking for include files
  2641. Revision 1.31 2002/03/01 14:39:44 peter
  2642. * fixed // and (* parsing to not be done when already parsing a
  2643. tp comment in skipuntildirective
  2644. Revision 1.30 2002/03/01 12:39:26 peter
  2645. * support // parsing in skipuntildirective
  2646. Revision 1.29 2002/01/27 21:44:26 peter
  2647. * FPCTARGETOS/FPCTARGETCPU added as internal environment variable
  2648. Revision 1.28 2002/01/24 18:25:50 peter
  2649. * implicit result variable generation for assembler routines
  2650. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  2651. }