scanner.pas 100 KB

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