scanner.pas 100 KB

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