scanner.pas 90 KB

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