scanner.pas 86 KB

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