scanner.pas 99 KB

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