2
0

scanner.pas 98 KB

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