scanner.pas 160 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the scanner part and handling of the switches
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit scanner;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,constexp,version,tokens,
  23. verbose,comphook,
  24. finput,
  25. widestr;
  26. const
  27. max_include_nesting=32;
  28. max_macro_nesting=16;
  29. preprocbufsize=32*1024;
  30. type
  31. tcommentstyle = (comment_none,comment_tp,comment_oldtp,comment_delphi,comment_c);
  32. tscannerfile = class;
  33. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else,pp_elseif);
  34. tpreprocstack = class
  35. typ : preproctyp;
  36. accept : boolean;
  37. next : tpreprocstack;
  38. name : TIDString;
  39. line_nb : longint;
  40. owner : tscannerfile;
  41. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  42. end;
  43. tdirectiveproc=procedure;
  44. tdirectiveitem = class(TFPHashObject)
  45. public
  46. is_conditional : boolean;
  47. proc : tdirectiveproc;
  48. constructor Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  49. constructor CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  50. end;
  51. // stack for replay buffers
  52. treplaystack = class
  53. token : ttoken;
  54. settings : tsettings;
  55. tokenbuf : tdynamicarray;
  56. next : treplaystack;
  57. change_endian : boolean;
  58. constructor Create(atoken: ttoken;asettings:tsettings;
  59. atokenbuf:tdynamicarray;anext:treplaystack; achange_endian : boolean);
  60. end;
  61. tcompile_time_predicate = function(var valuedescr: String) : Boolean;
  62. tspecialgenerictoken =
  63. (ST_LOADSETTINGS,
  64. ST_LINE,
  65. ST_COLUMN,
  66. ST_FILEINDEX,
  67. ST_LOADMESSAGES);
  68. { tscannerfile }
  69. tscannerfile = class
  70. private
  71. procedure do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  72. procedure cachenexttokenpos;
  73. procedure setnexttoken;
  74. procedure savetokenpos;
  75. procedure restoretokenpos;
  76. procedure writetoken(t: ttoken);
  77. function readtoken : ttoken;
  78. public
  79. inputfile : tinputfile; { current inputfile list }
  80. inputfilecount : longint;
  81. inputbuffer, { input buffer }
  82. inputpointer : pchar;
  83. inputstart : longint;
  84. line_no, { line }
  85. lastlinepos : longint;
  86. lasttokenpos,
  87. nexttokenpos : longint; { token }
  88. lasttoken,
  89. nexttoken : ttoken;
  90. oldlasttokenpos : longint; { temporary saving/restoring tokenpos }
  91. oldcurrent_filepos,
  92. oldcurrent_tokenpos : tfileposinfo;
  93. replaytokenbuf,
  94. recordtokenbuf : tdynamicarray;
  95. tokenbuf_change_endian : boolean;
  96. { last settings we stored }
  97. last_settings : tsettings;
  98. last_message : pmessagestaterecord;
  99. { last filepos we stored }
  100. last_filepos,
  101. { if nexttoken<>NOTOKEN, then nexttokenpos holds its filepos }
  102. next_filepos : tfileposinfo;
  103. comment_level,
  104. yylexcount : longint;
  105. lastasmgetchar : char;
  106. ignoredirectives : TFPHashList; { ignore directives, used to give warnings only once }
  107. preprocstack : tpreprocstack;
  108. replaystack : treplaystack;
  109. in_asm_string : boolean;
  110. preproc_pattern : string;
  111. preproc_token : ttoken;
  112. constructor Create(const fn:string; is_macro: boolean = false);
  113. destructor Destroy;override;
  114. { File buffer things }
  115. function openinputfile:boolean;
  116. procedure closeinputfile;
  117. function tempopeninputfile:boolean;
  118. procedure tempcloseinputfile;
  119. procedure saveinputfile;
  120. procedure restoreinputfile;
  121. procedure firstfile;
  122. procedure nextfile;
  123. procedure addfile(hp:tinputfile);
  124. procedure reload;
  125. { replaces current token with the text in p }
  126. procedure substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  127. { Scanner things }
  128. procedure gettokenpos;
  129. procedure inc_comment_level;
  130. procedure dec_comment_level;
  131. procedure illegal_char(c:char);
  132. procedure end_of_file;
  133. procedure checkpreprocstack;
  134. procedure poppreprocstack;
  135. procedure ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  136. procedure elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  137. procedure elsepreprocstack;
  138. procedure popreplaystack;
  139. procedure handleconditional(p:tdirectiveitem);
  140. procedure handledirectives;
  141. procedure linebreak;
  142. procedure recordtoken;
  143. procedure startrecordtokens(buf:tdynamicarray);
  144. procedure stoprecordtokens;
  145. procedure replaytoken;
  146. procedure startreplaytokens(buf:tdynamicarray; achange_endian : boolean);
  147. { bit length asizeint is target depend }
  148. procedure tokenwritesizeint(val : asizeint);
  149. procedure tokenwritelongint(val : longint);
  150. procedure tokenwritelongword(val : longword);
  151. procedure tokenwriteword(val : word);
  152. procedure tokenwriteshortint(val : shortint);
  153. procedure tokenwriteset(var b;size : longint);
  154. procedure tokenwriteenum(var b;size : longint);
  155. function tokenreadsizeint : asizeint;
  156. procedure tokenwritesettings(var asettings : tsettings; var size : asizeint);
  157. { longword/longint are 32 bits on all targets }
  158. { word/smallint are 16-bits on all targest }
  159. function tokenreadlongword : longword;
  160. function tokenreadword : word;
  161. function tokenreadlongint : longint;
  162. function tokenreadsmallint : smallint;
  163. { short int is one a signed byte }
  164. function tokenreadshortint : shortint;
  165. function tokenreadbyte : byte;
  166. { This one takes the set size as an parameter }
  167. procedure tokenreadset(var b;size : longint);
  168. function tokenreadenum(size : longint) : longword;
  169. procedure tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  170. procedure readchar;
  171. procedure readstring;
  172. procedure readnumber;
  173. function readid:string;
  174. function readval:longint;
  175. function readval_asstring:string;
  176. function readcomment:string;
  177. function readquotedstring:string;
  178. function readstate:char;
  179. function readstatedefault:char;
  180. procedure skipspace;
  181. procedure skipuntildirective;
  182. procedure skipcomment;
  183. procedure skipdelphicomment;
  184. procedure skipoldtpcomment;
  185. procedure readtoken(allowrecordtoken:boolean);
  186. function readpreproc:ttoken;
  187. function asmgetcharstart : char;
  188. function asmgetchar:char;
  189. end;
  190. {$ifdef PREPROCWRITE}
  191. tpreprocfile=class
  192. f : text;
  193. buf : pointer;
  194. spacefound,
  195. eolfound : boolean;
  196. constructor create(const fn:string);
  197. destructor destroy;
  198. procedure Add(const s:string);
  199. procedure AddSpace;
  200. end;
  201. {$endif PREPROCWRITE}
  202. var
  203. { read strings }
  204. c : char;
  205. orgpattern,
  206. pattern : string;
  207. cstringpattern : ansistring;
  208. patternw : pcompilerwidestring;
  209. { token }
  210. token, { current token being parsed }
  211. idtoken : ttoken; { holds the token if the pattern is a known word }
  212. current_scanner : tscannerfile; { current scanner in use }
  213. aktcommentstyle : tcommentstyle; { needed to use read_comment from directives }
  214. {$ifdef PREPROCWRITE}
  215. preprocfile : tpreprocfile; { used with only preprocessing }
  216. {$endif PREPROCWRITE}
  217. type
  218. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  219. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  220. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  221. procedure InitScanner;
  222. procedure DoneScanner;
  223. { To be called when the language mode is finally determined }
  224. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  225. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  226. implementation
  227. uses
  228. SysUtils,
  229. cutils,cfileutl,
  230. systems,
  231. switches,
  232. symbase,symtable,symtype,symsym,symconst,symdef,defutil,
  233. { This is needed for tcputype }
  234. cpuinfo,
  235. fmodule
  236. {$if FPC_FULLVERSION<20700}
  237. ,ccharset
  238. {$endif}
  239. ;
  240. var
  241. { dictionaries with the supported directives }
  242. turbo_scannerdirectives : TFPHashObjectList; { for other modes }
  243. mac_scannerdirectives : TFPHashObjectList; { for mode mac }
  244. {*****************************************************************************
  245. Helper routines
  246. *****************************************************************************}
  247. const
  248. { use any special name that is an invalid file name to avoid problems }
  249. preprocstring : array [preproctyp] of string[7]
  250. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE','$ELSEIF');
  251. function is_keyword(const s:string):boolean;
  252. var
  253. low,high,mid : longint;
  254. begin
  255. if not (length(s) in [tokenlenmin..tokenlenmax]) or
  256. not (s[1] in ['a'..'z','A'..'Z']) then
  257. begin
  258. is_keyword:=false;
  259. exit;
  260. end;
  261. low:=ord(tokenidx^[length(s),s[1]].first);
  262. high:=ord(tokenidx^[length(s),s[1]].last);
  263. while low<high do
  264. begin
  265. mid:=(high+low+1) shr 1;
  266. if pattern<tokeninfo^[ttoken(mid)].str then
  267. high:=mid-1
  268. else
  269. low:=mid;
  270. end;
  271. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  272. (tokeninfo^[ttoken(high)].keyword in current_settings.modeswitches);
  273. end;
  274. Procedure HandleModeSwitches(switch: tmodeswitch; changeInit: boolean);
  275. begin
  276. { turn ansi/unicodestrings on by default ? (only change when this
  277. particular setting is changed, so that a random modeswitch won't
  278. change the state of $h+/$h-) }
  279. if switch in [m_all,m_default_ansistring,m_default_unicodestring] then
  280. begin
  281. if ([m_default_ansistring,m_default_unicodestring]*current_settings.modeswitches)<>[] then
  282. begin
  283. { can't have both ansistring and unicodestring as default }
  284. if switch=m_default_ansistring then
  285. begin
  286. exclude(current_settings.modeswitches,m_default_unicodestring);
  287. if changeinit then
  288. exclude(init_settings.modeswitches,m_default_unicodestring);
  289. end
  290. else if switch=m_default_unicodestring then
  291. begin
  292. exclude(current_settings.modeswitches,m_default_ansistring);
  293. if changeinit then
  294. exclude(init_settings.modeswitches,m_default_ansistring);
  295. end;
  296. { enable $h+ }
  297. include(current_settings.localswitches,cs_refcountedstrings);
  298. if changeinit then
  299. include(init_settings.localswitches,cs_refcountedstrings);
  300. end
  301. else
  302. begin
  303. exclude(current_settings.localswitches,cs_refcountedstrings);
  304. if changeinit then
  305. exclude(init_settings.localswitches,cs_refcountedstrings);
  306. end;
  307. end;
  308. { turn inline on by default ? }
  309. if switch in [m_all,m_default_inline] then
  310. begin
  311. if (m_default_inline in current_settings.modeswitches) then
  312. begin
  313. include(current_settings.localswitches,cs_do_inline);
  314. if changeinit then
  315. include(init_settings.localswitches,cs_do_inline);
  316. end
  317. else
  318. begin
  319. exclude(current_settings.localswitches,cs_do_inline);
  320. if changeinit then
  321. exclude(init_settings.localswitches,cs_do_inline);
  322. end;
  323. end;
  324. { turn on system codepage by default }
  325. if switch in [m_all,m_systemcodepage] then
  326. begin
  327. if m_systemcodepage in current_settings.modeswitches then
  328. begin
  329. current_settings.sourcecodepage:=DefaultSystemCodePage;
  330. if not cpavailable(current_settings.sourcecodepage) then
  331. begin
  332. Message2(scan_w_unavailable_system_codepage,IntToStr(current_settings.sourcecodepage),IntToStr(default_settings.sourcecodepage));
  333. current_settings.sourcecodepage:=default_settings.sourcecodepage;
  334. end;
  335. include(current_settings.moduleswitches,cs_explicit_codepage);
  336. if changeinit then
  337. begin
  338. init_settings.sourcecodepage:=current_settings.sourcecodepage;
  339. include(init_settings.moduleswitches,cs_explicit_codepage);
  340. end;
  341. end
  342. else
  343. begin
  344. exclude(current_settings.moduleswitches,cs_explicit_codepage);
  345. if changeinit then
  346. exclude(init_settings.moduleswitches,cs_explicit_codepage);
  347. end;
  348. end;
  349. end;
  350. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  351. var
  352. b : boolean;
  353. oldmodeswitches : tmodeswitches;
  354. begin
  355. oldmodeswitches:=current_settings.modeswitches;
  356. b:=true;
  357. if s='DEFAULT' then
  358. current_settings.modeswitches:=fpcmodeswitches
  359. else
  360. if s='DELPHI' then
  361. current_settings.modeswitches:=delphimodeswitches
  362. else
  363. if s='DELPHIUNICODE' then
  364. current_settings.modeswitches:=delphiunicodemodeswitches
  365. else
  366. if s='TP' then
  367. current_settings.modeswitches:=tpmodeswitches
  368. else
  369. if s='FPC' then begin
  370. current_settings.modeswitches:=fpcmodeswitches;
  371. { TODO: enable this for 2.3/2.9 }
  372. // include(current_settings.localswitches, cs_typed_addresses);
  373. end else
  374. if s='OBJFPC' then begin
  375. current_settings.modeswitches:=objfpcmodeswitches;
  376. { TODO: enable this for 2.3/2.9 }
  377. // include(current_settings.localswitches, cs_typed_addresses);
  378. end
  379. {$ifdef gpc_mode}
  380. else if s='GPC' then
  381. current_settings.modeswitches:=gpcmodeswitches
  382. {$endif}
  383. else
  384. if s='MACPAS' then
  385. current_settings.modeswitches:=macmodeswitches
  386. else
  387. if s='ISO' then
  388. current_settings.modeswitches:=isomodeswitches
  389. else
  390. b:=false;
  391. {$ifdef jvm}
  392. { enable final fields by default for the JVM targets }
  393. include(current_settings.modeswitches,m_final_fields);
  394. {$endif jvm}
  395. if b and changeInit then
  396. init_settings.modeswitches := current_settings.modeswitches;
  397. if b then
  398. begin
  399. { resolve all postponed switch changes }
  400. flushpendingswitchesstate;
  401. HandleModeSwitches(m_all,changeinit);
  402. { turn on bitpacking for mode macpas and iso pascal }
  403. if ([m_mac,m_iso] * current_settings.modeswitches <> []) then
  404. begin
  405. include(current_settings.localswitches,cs_bitpacking);
  406. if changeinit then
  407. include(init_settings.localswitches,cs_bitpacking);
  408. end;
  409. { support goto/label by default in delphi/tp7/mac modes }
  410. if ([m_delphi,m_tp7,m_mac,m_iso] * current_settings.modeswitches <> []) then
  411. begin
  412. include(current_settings.moduleswitches,cs_support_goto);
  413. if changeinit then
  414. include(init_settings.moduleswitches,cs_support_goto);
  415. end;
  416. { support pointer math by default in fpc/objfpc modes }
  417. if ([m_fpc,m_objfpc] * current_settings.modeswitches <> []) then
  418. begin
  419. include(current_settings.localswitches,cs_pointermath);
  420. if changeinit then
  421. include(init_settings.localswitches,cs_pointermath);
  422. end
  423. else
  424. begin
  425. exclude(current_settings.localswitches,cs_pointermath);
  426. if changeinit then
  427. exclude(init_settings.localswitches,cs_pointermath);
  428. end;
  429. { Default enum and set packing for delphi/tp7 }
  430. if (m_tp7 in current_settings.modeswitches) or
  431. (m_delphi in current_settings.modeswitches) then
  432. begin
  433. current_settings.packenum:=1;
  434. current_settings.setalloc:=1;
  435. end
  436. else if (m_mac in current_settings.modeswitches) then
  437. { compatible with Metrowerks Pascal }
  438. current_settings.packenum:=2
  439. else
  440. current_settings.packenum:=4;
  441. if changeinit then
  442. init_settings.packenum:=current_settings.packenum;
  443. {$ifdef i386}
  444. { Default to intel assembler for delphi/tp7 on i386 }
  445. if (m_delphi in current_settings.modeswitches) or
  446. (m_tp7 in current_settings.modeswitches) then
  447. current_settings.asmmode:=asmmode_i386_intel;
  448. if changeinit then
  449. init_settings.asmmode:=current_settings.asmmode;
  450. {$endif i386}
  451. { Exception support explicitly turned on (mainly for macpas, to }
  452. { compensate for lack of interprocedural goto support) }
  453. if (cs_support_exceptions in current_settings.globalswitches) then
  454. include(current_settings.modeswitches,m_except);
  455. { Default strict string var checking in TP/Delphi modes }
  456. if ([m_delphi,m_tp7] * current_settings.modeswitches <> []) then
  457. begin
  458. include(current_settings.localswitches,cs_strict_var_strings);
  459. if changeinit then
  460. include(init_settings.localswitches,cs_strict_var_strings);
  461. end;
  462. { Undefine old symbol }
  463. if (m_delphi in oldmodeswitches) then
  464. undef_system_macro('FPC_DELPHI')
  465. else if (m_tp7 in oldmodeswitches) then
  466. undef_system_macro('FPC_TP')
  467. else if (m_objfpc in oldmodeswitches) then
  468. undef_system_macro('FPC_OBJFPC')
  469. {$ifdef gpc_mode}
  470. else if (m_gpc in oldmodeswitches) then
  471. undef_system_macro('FPC_GPC')
  472. {$endif}
  473. else if (m_mac in oldmodeswitches) then
  474. undef_system_macro('FPC_MACPAS');
  475. { define new symbol in delphi,objfpc,tp,gpc,macpas mode }
  476. if (m_delphi in current_settings.modeswitches) then
  477. def_system_macro('FPC_DELPHI')
  478. else if (m_tp7 in current_settings.modeswitches) then
  479. def_system_macro('FPC_TP')
  480. else if (m_objfpc in current_settings.modeswitches) then
  481. def_system_macro('FPC_OBJFPC')
  482. {$ifdef gpc_mode}
  483. else if (m_gpc in current_settings.modeswitches) then
  484. def_system_macro('FPC_GPC')
  485. {$endif}
  486. else if (m_mac in current_settings.modeswitches) then
  487. def_system_macro('FPC_MACPAS');
  488. end;
  489. SetCompileMode:=b;
  490. end;
  491. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  492. var
  493. i : tmodeswitch;
  494. doinclude : boolean;
  495. begin
  496. s:=upper(s);
  497. { on/off? }
  498. doinclude:=true;
  499. case s[length(s)] of
  500. '+':
  501. s:=copy(s,1,length(s)-1);
  502. '-':
  503. begin
  504. s:=copy(s,1,length(s)-1);
  505. doinclude:=false;
  506. end;
  507. end;
  508. Result:=false;
  509. for i:=m_class to high(tmodeswitch) do
  510. if s=modeswitchstr[i] then
  511. begin
  512. { Objective-C is currently only supported for Darwin targets }
  513. if doinclude and
  514. (i in [m_objectivec1,m_objectivec2]) and
  515. not(target_info.system in systems_objc_supported) then
  516. begin
  517. Message1(option_unsupported_target_for_feature,'Objective-C');
  518. break;
  519. end;
  520. if changeInit then
  521. current_settings.modeswitches:=init_settings.modeswitches;
  522. Result:=true;
  523. if doinclude then
  524. begin
  525. include(current_settings.modeswitches,i);
  526. { Objective-C 2.0 support implies 1.0 support }
  527. if (i=m_objectivec2) then
  528. include(current_settings.modeswitches,m_objectivec1);
  529. if (i in [m_objectivec1,m_objectivec2]) then
  530. include(current_settings.modeswitches,m_class);
  531. end
  532. else
  533. begin
  534. exclude(current_settings.modeswitches,i);
  535. { Objective-C 2.0 support implies 1.0 support }
  536. if (i=m_objectivec2) then
  537. exclude(current_settings.modeswitches,m_objectivec1);
  538. if (i in [m_objectivec1,m_objectivec2]) and
  539. ([m_delphi,m_objfpc]*current_settings.modeswitches=[]) then
  540. exclude(current_settings.modeswitches,m_class);
  541. end;
  542. { set other switches depending on changed mode switch }
  543. HandleModeSwitches(i,changeinit);
  544. if changeInit then
  545. init_settings.modeswitches:=current_settings.modeswitches;
  546. break;
  547. end;
  548. end;
  549. {*****************************************************************************
  550. Conditional Directives
  551. *****************************************************************************}
  552. procedure dir_else;
  553. begin
  554. current_scanner.elsepreprocstack;
  555. end;
  556. procedure dir_endif;
  557. begin
  558. current_scanner.poppreprocstack;
  559. end;
  560. function isdef(var valuedescr: String): Boolean;
  561. var
  562. hs : string;
  563. begin
  564. current_scanner.skipspace;
  565. hs:=current_scanner.readid;
  566. valuedescr:= hs;
  567. if hs='' then
  568. Message(scan_e_error_in_preproc_expr);
  569. isdef:=defined_macro(hs);
  570. end;
  571. procedure dir_ifdef;
  572. begin
  573. current_scanner.ifpreprocstack(pp_ifdef,@isdef,scan_c_ifdef_found);
  574. end;
  575. function isnotdef(var valuedescr: String): Boolean;
  576. var
  577. hs : string;
  578. begin
  579. current_scanner.skipspace;
  580. hs:=current_scanner.readid;
  581. valuedescr:= hs;
  582. if hs='' then
  583. Message(scan_e_error_in_preproc_expr);
  584. isnotdef:=not defined_macro(hs);
  585. end;
  586. procedure dir_ifndef;
  587. begin
  588. current_scanner.ifpreprocstack(pp_ifndef,@isnotdef,scan_c_ifndef_found);
  589. end;
  590. function opt_check(var valuedescr: String): Boolean;
  591. var
  592. hs : string;
  593. state : char;
  594. begin
  595. opt_check:= false;
  596. current_scanner.skipspace;
  597. hs:=current_scanner.readid;
  598. valuedescr:= hs;
  599. if (length(hs)>1) then
  600. Message1(scan_w_illegal_switch,hs)
  601. else
  602. begin
  603. state:=current_scanner.ReadState;
  604. if state in ['-','+'] then
  605. opt_check:=CheckSwitch(hs[1],state)
  606. else
  607. Message(scan_e_error_in_preproc_expr);
  608. end;
  609. end;
  610. procedure dir_ifopt;
  611. begin
  612. flushpendingswitchesstate;
  613. current_scanner.ifpreprocstack(pp_ifopt,@opt_check,scan_c_ifopt_found);
  614. end;
  615. procedure dir_libprefix;
  616. var
  617. s : string;
  618. begin
  619. current_scanner.skipspace;
  620. if c <> '''' then
  621. Message2(scan_f_syn_expected, '''', c);
  622. s := current_scanner.readquotedstring;
  623. stringdispose(outputprefix);
  624. outputprefix := stringdup(s);
  625. with current_module do
  626. setfilename(paramfn, paramallowoutput);
  627. end;
  628. procedure dir_libsuffix;
  629. var
  630. s : string;
  631. begin
  632. current_scanner.skipspace;
  633. if c <> '''' then
  634. Message2(scan_f_syn_expected, '''', c);
  635. s := current_scanner.readquotedstring;
  636. stringdispose(outputsuffix);
  637. outputsuffix := stringdup(s);
  638. with current_module do
  639. setfilename(paramfn, paramallowoutput);
  640. end;
  641. procedure dir_extension;
  642. var
  643. s : string;
  644. begin
  645. current_scanner.skipspace;
  646. if c <> '''' then
  647. Message2(scan_f_syn_expected, '''', c);
  648. s := current_scanner.readquotedstring;
  649. if OutputFileName='' then
  650. OutputFileName:=InputFileName;
  651. OutputFileName:=ChangeFileExt(OutputFileName,'.'+s);
  652. with current_module do
  653. setfilename(paramfn, paramallowoutput);
  654. end;
  655. {
  656. Compile time expression type check
  657. ----------------------------------
  658. Each subexpression returns its type to the caller, which then can
  659. do type check. Since data types of compile time expressions is
  660. not well defined, the type system does a best effort. The drawback is
  661. that some errors might not be detected.
  662. Instead of returning a particular data type, a set of possible data types
  663. are returned. This way ambigouos types can be handled. For instance a
  664. value of 1 can be both a boolean and and integer.
  665. Booleans
  666. --------
  667. The following forms of boolean values are supported:
  668. * C coded, that is 0 is false, non-zero is true.
  669. * TRUE/FALSE for mac style compile time variables
  670. Thus boolean mac compile time variables are always stored as TRUE/FALSE.
  671. When a compile time expression is evaluated, they are then translated
  672. to C coded booleans (0/1), to simplify for the expression evaluator.
  673. Note that this scheme then also of support mac compile time variables which
  674. are 0/1 but with a boolean meaning.
  675. The TRUE/FALSE format is new from 22 august 2005, but the above scheme
  676. means that units which is not recompiled, and thus stores
  677. compile time variables as the old format (0/1), continue to work.
  678. Short circuit evaluation
  679. ------------------------
  680. For this to work, the part of a compile time expression which is short
  681. circuited, should not be evaluated, while it still should be parsed.
  682. Therefor there is a parameter eval, telling whether evaluation is needed.
  683. In case not, the value returned can be arbitrary.
  684. }
  685. type
  686. {Compile time expression types}
  687. TCTEType = (ctetBoolean, ctetInteger, ctetString, ctetSet);
  688. TCTETypeSet = set of TCTEType;
  689. const
  690. cteTypeNames : array[TCTEType] of string[10] = (
  691. 'BOOLEAN','INTEGER','STRING','SET');
  692. {Subset of types which can be elements in sets.}
  693. setelementdefs = [ctetBoolean, ctetInteger, ctetString];
  694. function GetCTETypeName(t: TCTETypeSet): String;
  695. var
  696. i: TCTEType;
  697. begin
  698. result:= '';
  699. for i:= Low(TCTEType) to High(TCTEType) do
  700. if i in t then
  701. if result = '' then
  702. result:= cteTypeNames[i]
  703. else
  704. result:= result + ' or ' + cteTypeNames[i];
  705. end;
  706. procedure CTEError(actType, desiredExprType: TCTETypeSet; place: String);
  707. begin
  708. Message3(scan_e_compile_time_typeerror,
  709. GetCTETypeName(desiredExprType),
  710. GetCTETypeName(actType),
  711. place
  712. );
  713. end;
  714. function parse_compiler_expr(var compileExprType: TCTETypeSet):string;
  715. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string; forward;
  716. procedure preproc_consume(t : ttoken);
  717. begin
  718. if t<>current_scanner.preproc_token then
  719. Message(scan_e_preproc_syntax_error);
  720. current_scanner.preproc_token:=current_scanner.readpreproc;
  721. end;
  722. function preproc_substitutedtoken(var macroType: TCTETypeSet; eval : Boolean): string;
  723. { Currently this parses identifiers as well as numbers.
  724. The result from this procedure can either be that the token
  725. itself is a value, or that it is a compile time variable/macro,
  726. which then is substituted for another value (for macros
  727. recursivelly substituted).}
  728. var
  729. hs: string;
  730. mac : tmacro;
  731. macrocount,
  732. len : integer;
  733. numres : longint;
  734. w: word;
  735. begin
  736. result := current_scanner.preproc_pattern;
  737. if not eval then
  738. exit;
  739. mac:= nil;
  740. { Substitue macros and compiler variables with their content/value.
  741. For real macros also do recursive substitution. }
  742. macrocount:=0;
  743. repeat
  744. mac:=tmacro(search_macro(result));
  745. inc(macrocount);
  746. if macrocount>max_macro_nesting then
  747. begin
  748. Message(scan_w_macro_too_deep);
  749. break;
  750. end;
  751. if assigned(mac) and mac.defined then
  752. if assigned(mac.buftext) then
  753. begin
  754. if mac.buflen>255 then
  755. begin
  756. len:=255;
  757. Message(scan_w_macro_cut_after_255_chars);
  758. end
  759. else
  760. len:=mac.buflen;
  761. hs[0]:=char(len);
  762. move(mac.buftext^,hs[1],len);
  763. result:=upcase(hs);
  764. mac.is_used:=true;
  765. end
  766. else
  767. begin
  768. Message1(scan_e_error_macro_lacks_value, result);
  769. break;
  770. end
  771. else
  772. begin
  773. break;
  774. end;
  775. if mac.is_compiler_var then
  776. break;
  777. until false;
  778. { At this point, result do contain the value. Do some decoding and
  779. determine the type.}
  780. val(result,numres,w);
  781. if (w=0) then {It is an integer}
  782. begin
  783. if (numres = 0) or (numres = 1) then
  784. macroType := [ctetInteger, ctetBoolean]
  785. else
  786. macroType := [ctetInteger];
  787. end
  788. else if assigned(mac) and (m_mac in current_settings.modeswitches) and (result='FALSE') then
  789. begin
  790. result:= '0';
  791. macroType:= [ctetBoolean];
  792. end
  793. else if assigned(mac) and (m_mac in current_settings.modeswitches) and (result='TRUE') then
  794. begin
  795. result:= '1';
  796. macroType:= [ctetBoolean];
  797. end
  798. else if (m_mac in current_settings.modeswitches) and
  799. (not assigned(mac) or not mac.defined) and
  800. (macrocount = 1) then
  801. begin
  802. {Errors in mode mac is issued here. For non macpas modes there is
  803. more liberty, but the error will eventually be caught at a later stage.}
  804. Message1(scan_e_error_macro_undefined, result);
  805. macroType:= [ctetString]; {Just to have something}
  806. end
  807. else
  808. macroType:= [ctetString];
  809. end;
  810. function read_factor(var factorType: TCTETypeSet; eval : Boolean) : string;
  811. var
  812. hs : string;
  813. mac: tmacro;
  814. srsym : tsym;
  815. srsymtable : TSymtable;
  816. hdef : TDef;
  817. l : longint;
  818. w : integer;
  819. hasKlammer: Boolean;
  820. setElemType : TCTETypeSet;
  821. begin
  822. if current_scanner.preproc_token=_ID then
  823. begin
  824. if current_scanner.preproc_pattern='DEFINED' then
  825. begin
  826. factorType:= [ctetBoolean];
  827. preproc_consume(_ID);
  828. current_scanner.skipspace;
  829. if current_scanner.preproc_token =_LKLAMMER then
  830. begin
  831. preproc_consume(_LKLAMMER);
  832. current_scanner.skipspace;
  833. hasKlammer:= true;
  834. end
  835. else if (m_mac in current_settings.modeswitches) then
  836. hasKlammer:= false
  837. else
  838. Message(scan_e_error_in_preproc_expr);
  839. if current_scanner.preproc_token =_ID then
  840. begin
  841. hs := current_scanner.preproc_pattern;
  842. mac := tmacro(search_macro(hs));
  843. if assigned(mac) and mac.defined then
  844. begin
  845. hs := '1';
  846. mac.is_used:=true;
  847. end
  848. else
  849. hs := '0';
  850. read_factor := hs;
  851. preproc_consume(_ID);
  852. current_scanner.skipspace;
  853. end
  854. else
  855. Message(scan_e_error_in_preproc_expr);
  856. if hasKlammer then
  857. if current_scanner.preproc_token =_RKLAMMER then
  858. preproc_consume(_RKLAMMER)
  859. else
  860. Message(scan_e_error_in_preproc_expr);
  861. end
  862. else
  863. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  864. begin
  865. factorType:= [ctetBoolean];
  866. preproc_consume(_ID);
  867. current_scanner.skipspace;
  868. if current_scanner.preproc_token =_ID then
  869. begin
  870. hs := current_scanner.preproc_pattern;
  871. mac := tmacro(search_macro(hs));
  872. if assigned(mac) then
  873. begin
  874. hs := '0';
  875. mac.is_used:=true;
  876. end
  877. else
  878. hs := '1';
  879. read_factor := hs;
  880. preproc_consume(_ID);
  881. current_scanner.skipspace;
  882. end
  883. else
  884. Message(scan_e_error_in_preproc_expr);
  885. end
  886. else
  887. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  888. begin
  889. factorType:= [ctetBoolean];
  890. preproc_consume(_ID);
  891. current_scanner.skipspace;
  892. if current_scanner.preproc_token =_LKLAMMER then
  893. begin
  894. preproc_consume(_LKLAMMER);
  895. current_scanner.skipspace;
  896. end
  897. else
  898. Message(scan_e_error_in_preproc_expr);
  899. if not (current_scanner.preproc_token = _ID) then
  900. Message(scan_e_error_in_preproc_expr);
  901. hs:=current_scanner.preproc_pattern;
  902. if (length(hs) > 1) then
  903. {This is allowed in Metrowerks Pascal}
  904. Message(scan_e_error_in_preproc_expr)
  905. else
  906. begin
  907. if CheckSwitch(hs[1],'+') then
  908. read_factor := '1'
  909. else
  910. read_factor := '0';
  911. end;
  912. preproc_consume(_ID);
  913. current_scanner.skipspace;
  914. if current_scanner.preproc_token =_RKLAMMER then
  915. preproc_consume(_RKLAMMER)
  916. else
  917. Message(scan_e_error_in_preproc_expr);
  918. end
  919. else
  920. if current_scanner.preproc_pattern='SIZEOF' then
  921. begin
  922. factorType:= [ctetInteger];
  923. preproc_consume(_ID);
  924. current_scanner.skipspace;
  925. if current_scanner.preproc_token =_LKLAMMER then
  926. begin
  927. preproc_consume(_LKLAMMER);
  928. current_scanner.skipspace;
  929. end
  930. else
  931. Message(scan_e_preproc_syntax_error);
  932. if eval then
  933. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  934. begin
  935. l:=0;
  936. case srsym.typ of
  937. staticvarsym,
  938. localvarsym,
  939. paravarsym :
  940. l:=tabstractvarsym(srsym).getsize;
  941. typesym:
  942. l:=ttypesym(srsym).typedef.size;
  943. else
  944. Message(scan_e_error_in_preproc_expr);
  945. end;
  946. str(l,read_factor);
  947. end
  948. else
  949. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  950. preproc_consume(_ID);
  951. current_scanner.skipspace;
  952. if current_scanner.preproc_token =_RKLAMMER then
  953. preproc_consume(_RKLAMMER)
  954. else
  955. Message(scan_e_preproc_syntax_error);
  956. end
  957. else
  958. if current_scanner.preproc_pattern='HIGH' then
  959. begin
  960. factorType:= [ctetInteger];
  961. preproc_consume(_ID);
  962. current_scanner.skipspace;
  963. if current_scanner.preproc_token =_LKLAMMER then
  964. begin
  965. preproc_consume(_LKLAMMER);
  966. current_scanner.skipspace;
  967. end
  968. else
  969. Message(scan_e_preproc_syntax_error);
  970. if eval then
  971. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  972. begin
  973. hdef:=nil;
  974. hs:='';
  975. l:=0;
  976. case srsym.typ of
  977. staticvarsym,
  978. localvarsym,
  979. paravarsym :
  980. hdef:=tabstractvarsym(srsym).vardef;
  981. typesym:
  982. hdef:=ttypesym(srsym).typedef;
  983. else
  984. Message(scan_e_error_in_preproc_expr);
  985. end;
  986. if hdef<>nil then
  987. begin
  988. if hdef.typ=setdef then
  989. hdef:=tsetdef(hdef).elementdef;
  990. case hdef.typ of
  991. orddef:
  992. with torddef(hdef).high do
  993. if signed then
  994. str(svalue,hs)
  995. else
  996. str(uvalue,hs);
  997. enumdef:
  998. l:=tenumdef(hdef).maxval;
  999. arraydef:
  1000. if is_open_array(hdef) or is_array_of_const(hdef) or is_dynamic_array(hdef) then
  1001. Message(type_e_mismatch)
  1002. else
  1003. l:=tarraydef(hdef).highrange;
  1004. stringdef:
  1005. if is_open_string(hdef) or is_ansistring(hdef) or is_wide_or_unicode_string(hdef) then
  1006. Message(type_e_mismatch)
  1007. else
  1008. l:=tstringdef(hdef).len;
  1009. else
  1010. Message(type_e_mismatch);
  1011. end;
  1012. end;
  1013. if hs='' then
  1014. str(l,read_factor)
  1015. else
  1016. read_factor:=hs;
  1017. end
  1018. else
  1019. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  1020. preproc_consume(_ID);
  1021. current_scanner.skipspace;
  1022. if current_scanner.preproc_token =_RKLAMMER then
  1023. preproc_consume(_RKLAMMER)
  1024. else
  1025. Message(scan_e_preproc_syntax_error);
  1026. end
  1027. else
  1028. if current_scanner.preproc_pattern='DECLARED' then
  1029. begin
  1030. factorType:= [ctetBoolean];
  1031. preproc_consume(_ID);
  1032. current_scanner.skipspace;
  1033. if current_scanner.preproc_token =_LKLAMMER then
  1034. begin
  1035. preproc_consume(_LKLAMMER);
  1036. current_scanner.skipspace;
  1037. end
  1038. else
  1039. Message(scan_e_error_in_preproc_expr);
  1040. if current_scanner.preproc_token =_ID then
  1041. begin
  1042. hs := upper(current_scanner.preproc_pattern);
  1043. if searchsym(hs,srsym,srsymtable) then
  1044. hs := '1'
  1045. else
  1046. hs := '0';
  1047. read_factor := hs;
  1048. preproc_consume(_ID);
  1049. current_scanner.skipspace;
  1050. end
  1051. else
  1052. Message(scan_e_error_in_preproc_expr);
  1053. if current_scanner.preproc_token =_RKLAMMER then
  1054. preproc_consume(_RKLAMMER)
  1055. else
  1056. Message(scan_e_error_in_preproc_expr);
  1057. end
  1058. else
  1059. if current_scanner.preproc_pattern='NOT' then
  1060. begin
  1061. factorType:= [ctetBoolean];
  1062. preproc_consume(_ID);
  1063. hs:=read_factor(factorType, eval);
  1064. if eval then
  1065. begin
  1066. if not (ctetBoolean in factorType) then
  1067. CTEError(factorType, [ctetBoolean], 'NOT');
  1068. val(hs,l,w);
  1069. if l<>0 then
  1070. read_factor:='0'
  1071. else
  1072. read_factor:='1';
  1073. end
  1074. else
  1075. read_factor:='0'; {Just to have something}
  1076. end
  1077. else
  1078. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='TRUE') then
  1079. begin
  1080. factorType:= [ctetBoolean];
  1081. preproc_consume(_ID);
  1082. read_factor:='1';
  1083. end
  1084. else
  1085. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='FALSE') then
  1086. begin
  1087. factorType:= [ctetBoolean];
  1088. preproc_consume(_ID);
  1089. read_factor:='0';
  1090. end
  1091. else
  1092. begin
  1093. hs:=preproc_substitutedtoken(factorType, eval);
  1094. { Default is to return the original symbol }
  1095. read_factor:=hs;
  1096. if eval and ([m_delphi,m_objfpc]*current_settings.modeswitches<>[]) and (ctetString in factorType) then
  1097. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  1098. begin
  1099. case srsym.typ of
  1100. constsym :
  1101. begin
  1102. with tconstsym(srsym) do
  1103. begin
  1104. case consttyp of
  1105. constord :
  1106. begin
  1107. case constdef.typ of
  1108. orddef:
  1109. begin
  1110. if is_integer(constdef) then
  1111. begin
  1112. read_factor:=tostr(value.valueord);
  1113. factorType:= [ctetInteger];
  1114. end
  1115. else if is_boolean(constdef) then
  1116. begin
  1117. read_factor:=tostr(value.valueord);
  1118. factorType:= [ctetBoolean];
  1119. end
  1120. else if is_char(constdef) then
  1121. begin
  1122. read_factor:=char(qword(value.valueord));
  1123. factorType:= [ctetString];
  1124. end
  1125. end;
  1126. enumdef:
  1127. begin
  1128. read_factor:=tostr(value.valueord);
  1129. factorType:= [ctetInteger];
  1130. end;
  1131. end;
  1132. end;
  1133. conststring :
  1134. begin
  1135. read_factor := upper(pchar(value.valueptr));
  1136. factorType:= [ctetString];
  1137. end;
  1138. constset :
  1139. begin
  1140. hs:=',';
  1141. for l:=0 to 255 do
  1142. if l in pconstset(tconstsym(srsym).value.valueptr)^ then
  1143. hs:=hs+tostr(l)+',';
  1144. read_factor := hs;
  1145. factorType:= [ctetSet];
  1146. end;
  1147. end;
  1148. end;
  1149. end;
  1150. enumsym :
  1151. begin
  1152. read_factor:=tostr(tenumsym(srsym).value);
  1153. factorType:= [ctetInteger];
  1154. end;
  1155. end;
  1156. end;
  1157. preproc_consume(_ID);
  1158. current_scanner.skipspace;
  1159. end
  1160. end
  1161. else if current_scanner.preproc_token =_LKLAMMER then
  1162. begin
  1163. preproc_consume(_LKLAMMER);
  1164. read_factor:=read_expr(factorType, eval);
  1165. preproc_consume(_RKLAMMER);
  1166. end
  1167. else if current_scanner.preproc_token = _LECKKLAMMER then
  1168. begin
  1169. preproc_consume(_LECKKLAMMER);
  1170. read_factor := ',';
  1171. while current_scanner.preproc_token = _ID do
  1172. begin
  1173. read_factor := read_factor+read_factor(setElemType, eval)+',';
  1174. if current_scanner.preproc_token = _COMMA then
  1175. preproc_consume(_COMMA);
  1176. end;
  1177. // TODO Add check of setElemType
  1178. preproc_consume(_RECKKLAMMER);
  1179. factorType:= [ctetSet];
  1180. end
  1181. else
  1182. Message(scan_e_error_in_preproc_expr);
  1183. end;
  1184. function read_term(var termType: TCTETypeSet; eval : Boolean) : string;
  1185. var
  1186. hs1,hs2 : string;
  1187. l1,l2 : longint;
  1188. w : integer;
  1189. termType2: TCTETypeSet;
  1190. begin
  1191. hs1:=read_factor(termType, eval);
  1192. repeat
  1193. if (current_scanner.preproc_token<>_ID) then
  1194. break;
  1195. if current_scanner.preproc_pattern<>'AND' then
  1196. break;
  1197. val(hs1,l1,w);
  1198. if l1=0 then
  1199. eval:= false; {Short circuit evaluation of OR}
  1200. if eval then
  1201. begin
  1202. {Check if first expr is boolean. Must be done here, after we know
  1203. it is an AND expression.}
  1204. if not (ctetBoolean in termType) then
  1205. CTEError(termType, [ctetBoolean], 'AND');
  1206. termType:= [ctetBoolean];
  1207. end;
  1208. preproc_consume(_ID);
  1209. hs2:=read_factor(termType2, eval);
  1210. if eval then
  1211. begin
  1212. if not (ctetBoolean in termType2) then
  1213. CTEError(termType2, [ctetBoolean], 'AND');
  1214. val(hs2,l2,w);
  1215. if (l1<>0) and (l2<>0) then
  1216. hs1:='1'
  1217. else
  1218. hs1:='0';
  1219. end;
  1220. until false;
  1221. read_term:=hs1;
  1222. end;
  1223. function read_simple_expr(var simpleExprType: TCTETypeSet; eval : Boolean) : string;
  1224. var
  1225. hs1,hs2 : string;
  1226. l1,l2 : longint;
  1227. w : integer;
  1228. simpleExprType2: TCTETypeSet;
  1229. begin
  1230. hs1:=read_term(simpleExprType, eval);
  1231. repeat
  1232. if (current_scanner.preproc_token<>_ID) then
  1233. break;
  1234. if current_scanner.preproc_pattern<>'OR' then
  1235. break;
  1236. val(hs1,l1,w);
  1237. if l1<>0 then
  1238. eval:= false; {Short circuit evaluation of OR}
  1239. if eval then
  1240. begin
  1241. {Check if first expr is boolean. Must be done here, after we know
  1242. it is an OR expression.}
  1243. if not (ctetBoolean in simpleExprType) then
  1244. CTEError(simpleExprType, [ctetBoolean], 'OR');
  1245. simpleExprType:= [ctetBoolean];
  1246. end;
  1247. preproc_consume(_ID);
  1248. hs2:=read_term(simpleExprType2, eval);
  1249. if eval then
  1250. begin
  1251. if not (ctetBoolean in simpleExprType2) then
  1252. CTEError(simpleExprType2, [ctetBoolean], 'OR');
  1253. val(hs2,l2,w);
  1254. if (l1<>0) or (l2<>0) then
  1255. hs1:='1'
  1256. else
  1257. hs1:='0';
  1258. end;
  1259. until false;
  1260. read_simple_expr:=hs1;
  1261. end;
  1262. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string;
  1263. var
  1264. hs1,hs2 : string;
  1265. b : boolean;
  1266. op : ttoken;
  1267. w : integer;
  1268. l1,l2 : longint;
  1269. exprType2: TCTETypeSet;
  1270. begin
  1271. hs1:=read_simple_expr(exprType, eval);
  1272. op:=current_scanner.preproc_token;
  1273. if (op = _ID) and (current_scanner.preproc_pattern = 'IN') then
  1274. op := _IN;
  1275. if not (op in [_IN,_EQ,_NE,_LT,_GT,_LTE,_GTE]) then
  1276. begin
  1277. read_expr:=hs1;
  1278. exit;
  1279. end;
  1280. if (op = _IN) then
  1281. preproc_consume(_ID)
  1282. else
  1283. preproc_consume(op);
  1284. hs2:=read_simple_expr(exprType2, eval);
  1285. if eval then
  1286. begin
  1287. if op = _IN then
  1288. begin
  1289. if exprType2 <> [ctetSet] then
  1290. CTEError(exprType2, [ctetSet], 'IN');
  1291. if exprType = [ctetSet] then
  1292. CTEError(exprType, setelementdefs, 'IN');
  1293. if is_number(hs1) and is_number(hs2) then
  1294. Message(scan_e_preproc_syntax_error)
  1295. else if hs2[1] = ',' then
  1296. b:=pos(','+hs1+',', hs2) > 0 { TODO For integer sets, perhaps check for numeric equivalence so that 0 = 00 }
  1297. else
  1298. Message(scan_e_preproc_syntax_error);
  1299. end
  1300. else
  1301. begin
  1302. if (exprType * exprType2) = [] then
  1303. CTEError(exprType2, exprType, '"'+hs1+' '+tokeninfo^[op].str+' '+hs2+'"');
  1304. if is_number(hs1) and is_number(hs2) then
  1305. begin
  1306. val(hs1,l1,w);
  1307. val(hs2,l2,w);
  1308. case op of
  1309. _EQ :
  1310. b:=l1=l2;
  1311. _NE :
  1312. b:=l1<>l2;
  1313. _LT :
  1314. b:=l1<l2;
  1315. _GT :
  1316. b:=l1>l2;
  1317. _GTE :
  1318. b:=l1>=l2;
  1319. _LTE :
  1320. b:=l1<=l2;
  1321. end;
  1322. end
  1323. else
  1324. begin
  1325. case op of
  1326. _EQ:
  1327. b:=hs1=hs2;
  1328. _NE :
  1329. b:=hs1<>hs2;
  1330. _LT :
  1331. b:=hs1<hs2;
  1332. _GT :
  1333. b:=hs1>hs2;
  1334. _GTE :
  1335. b:=hs1>=hs2;
  1336. _LTE :
  1337. b:=hs1<=hs2;
  1338. end;
  1339. end;
  1340. end;
  1341. end
  1342. else
  1343. b:= false; {Just to have something}
  1344. if b then
  1345. read_expr:='1'
  1346. else
  1347. read_expr:='0';
  1348. exprType:= [ctetBoolean];
  1349. end;
  1350. begin
  1351. current_scanner.skipspace;
  1352. { start preproc expression scanner }
  1353. current_scanner.preproc_token:=current_scanner.readpreproc;
  1354. parse_compiler_expr:=read_expr(compileExprType, true);
  1355. end;
  1356. function boolean_compile_time_expr(var valuedescr: String): Boolean;
  1357. var
  1358. hs : string;
  1359. exprType: TCTETypeSet;
  1360. begin
  1361. hs:=parse_compiler_expr(exprType);
  1362. if (exprType * [ctetBoolean]) = [] then
  1363. CTEError(exprType, [ctetBoolean], 'IF or ELSEIF');
  1364. boolean_compile_time_expr:= hs <> '0';
  1365. valuedescr:= hs;
  1366. end;
  1367. procedure dir_if;
  1368. begin
  1369. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  1370. end;
  1371. procedure dir_elseif;
  1372. begin
  1373. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  1374. end;
  1375. procedure dir_define_impl(macstyle: boolean);
  1376. var
  1377. hs : string;
  1378. bracketcount : longint;
  1379. mac : tmacro;
  1380. macropos : longint;
  1381. macrobuffer : pmacrobuffer;
  1382. begin
  1383. current_scanner.skipspace;
  1384. hs:=current_scanner.readid;
  1385. mac:=tmacro(search_macro(hs));
  1386. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  1387. begin
  1388. mac:=tmacro.create(hs);
  1389. mac.defined:=true;
  1390. current_module.localmacrosymtable.insert(mac);
  1391. end
  1392. else
  1393. begin
  1394. mac.defined:=true;
  1395. mac.is_compiler_var:=false;
  1396. { delete old definition }
  1397. if assigned(mac.buftext) then
  1398. begin
  1399. freemem(mac.buftext,mac.buflen);
  1400. mac.buftext:=nil;
  1401. end;
  1402. end;
  1403. Message1(parser_c_macro_defined,mac.name);
  1404. mac.is_used:=true;
  1405. if (cs_support_macro in current_settings.moduleswitches) then
  1406. begin
  1407. current_scanner.skipspace;
  1408. if not macstyle then
  1409. begin
  1410. { may be a macro? }
  1411. if c <> ':' then
  1412. exit;
  1413. current_scanner.readchar;
  1414. if c <> '=' then
  1415. exit;
  1416. current_scanner.readchar;
  1417. current_scanner.skipspace;
  1418. end;
  1419. { key words are never substituted }
  1420. if is_keyword(hs) then
  1421. Message(scan_e_keyword_cant_be_a_macro);
  1422. new(macrobuffer);
  1423. macropos:=0;
  1424. { parse macro, brackets are counted so it's possible
  1425. to have a $ifdef etc. in the macro }
  1426. bracketcount:=0;
  1427. repeat
  1428. case c of
  1429. '}' :
  1430. if (bracketcount=0) then
  1431. break
  1432. else
  1433. dec(bracketcount);
  1434. '{' :
  1435. inc(bracketcount);
  1436. #10,#13 :
  1437. current_scanner.linebreak;
  1438. #26 :
  1439. current_scanner.end_of_file;
  1440. end;
  1441. macrobuffer^[macropos]:=c;
  1442. inc(macropos);
  1443. if macropos>=maxmacrolen then
  1444. Message(scan_f_macro_buffer_overflow);
  1445. current_scanner.readchar;
  1446. until false;
  1447. { free buffer of macro ?}
  1448. if assigned(mac.buftext) then
  1449. freemem(mac.buftext,mac.buflen);
  1450. { get new mem }
  1451. getmem(mac.buftext,macropos);
  1452. mac.buflen:=macropos;
  1453. { copy the text }
  1454. move(macrobuffer^,mac.buftext^,macropos);
  1455. dispose(macrobuffer);
  1456. end
  1457. else
  1458. begin
  1459. { check if there is an assignment, then we need to give a
  1460. warning }
  1461. current_scanner.skipspace;
  1462. if c=':' then
  1463. begin
  1464. current_scanner.readchar;
  1465. if c='=' then
  1466. Message(scan_w_macro_support_turned_off);
  1467. end;
  1468. end;
  1469. end;
  1470. procedure dir_define;
  1471. begin
  1472. dir_define_impl(false);
  1473. end;
  1474. procedure dir_definec;
  1475. begin
  1476. dir_define_impl(true);
  1477. end;
  1478. procedure dir_setc;
  1479. var
  1480. hs : string;
  1481. mac : tmacro;
  1482. exprType: TCTETypeSet;
  1483. l : longint;
  1484. w : integer;
  1485. begin
  1486. current_scanner.skipspace;
  1487. hs:=current_scanner.readid;
  1488. mac:=tmacro(search_macro(hs));
  1489. if not assigned(mac) or
  1490. (mac.owner <> current_module.localmacrosymtable) then
  1491. begin
  1492. mac:=tmacro.create(hs);
  1493. mac.defined:=true;
  1494. mac.is_compiler_var:=true;
  1495. current_module.localmacrosymtable.insert(mac);
  1496. end
  1497. else
  1498. begin
  1499. mac.defined:=true;
  1500. mac.is_compiler_var:=true;
  1501. { delete old definition }
  1502. if assigned(mac.buftext) then
  1503. begin
  1504. freemem(mac.buftext,mac.buflen);
  1505. mac.buftext:=nil;
  1506. end;
  1507. end;
  1508. Message1(parser_c_macro_defined,mac.name);
  1509. mac.is_used:=true;
  1510. { key words are never substituted }
  1511. if is_keyword(hs) then
  1512. Message(scan_e_keyword_cant_be_a_macro);
  1513. { macro assignment can be both := and = }
  1514. current_scanner.skipspace;
  1515. if c=':' then
  1516. current_scanner.readchar;
  1517. if c='=' then
  1518. begin
  1519. current_scanner.readchar;
  1520. hs:= parse_compiler_expr(exprType);
  1521. if (exprType * [ctetBoolean, ctetInteger]) = [] then
  1522. CTEError(exprType, [ctetBoolean, ctetInteger], 'SETC');
  1523. if length(hs) <> 0 then
  1524. begin
  1525. {If we are absolutely shure it is boolean, translate
  1526. to TRUE/FALSE to increase possibility to do future type check}
  1527. if exprType = [ctetBoolean] then
  1528. begin
  1529. val(hs,l,w);
  1530. if l<>0 then
  1531. hs:='TRUE'
  1532. else
  1533. hs:='FALSE';
  1534. end;
  1535. Message2(parser_c_macro_set_to,mac.name,hs);
  1536. { free buffer of macro ?}
  1537. if assigned(mac.buftext) then
  1538. freemem(mac.buftext,mac.buflen);
  1539. { get new mem }
  1540. getmem(mac.buftext,length(hs));
  1541. mac.buflen:=length(hs);
  1542. { copy the text }
  1543. move(hs[1],mac.buftext^,mac.buflen);
  1544. end
  1545. else
  1546. Message(scan_e_preproc_syntax_error);
  1547. end
  1548. else
  1549. Message(scan_e_preproc_syntax_error);
  1550. end;
  1551. procedure dir_undef;
  1552. var
  1553. hs : string;
  1554. mac : tmacro;
  1555. begin
  1556. current_scanner.skipspace;
  1557. hs:=current_scanner.readid;
  1558. mac:=tmacro(search_macro(hs));
  1559. if not assigned(mac) or
  1560. (mac.owner <> current_module.localmacrosymtable) then
  1561. begin
  1562. mac:=tmacro.create(hs);
  1563. mac.defined:=false;
  1564. current_module.localmacrosymtable.insert(mac);
  1565. end
  1566. else
  1567. begin
  1568. mac.defined:=false;
  1569. mac.is_compiler_var:=false;
  1570. { delete old definition }
  1571. if assigned(mac.buftext) then
  1572. begin
  1573. freemem(mac.buftext,mac.buflen);
  1574. mac.buftext:=nil;
  1575. end;
  1576. end;
  1577. Message1(parser_c_macro_undefined,mac.name);
  1578. mac.is_used:=true;
  1579. end;
  1580. procedure dir_include;
  1581. function findincludefile(const path,name:TCmdStr;var foundfile:TCmdStr):boolean;
  1582. var
  1583. found : boolean;
  1584. hpath : TCmdStr;
  1585. begin
  1586. (* look for the include file
  1587. If path was absolute and specified as part of {$I } then
  1588. 1. specified path
  1589. else
  1590. 1. path of current inputfile,current dir
  1591. 2. local includepath
  1592. 3. global includepath
  1593. -- Check mantis #13461 before changing this *)
  1594. found:=false;
  1595. foundfile:='';
  1596. hpath:='';
  1597. if path_absolute(path) then
  1598. begin
  1599. found:=FindFile(name,path,true,foundfile);
  1600. end
  1601. else
  1602. begin
  1603. hpath:=current_scanner.inputfile.path+';'+CurDirRelPath(source_info);
  1604. found:=FindFile(path+name, hpath,true,foundfile);
  1605. if not found then
  1606. found:=current_module.localincludesearchpath.FindFile(path+name,true,foundfile);
  1607. if not found then
  1608. found:=includesearchpath.FindFile(path+name,true,foundfile);
  1609. end;
  1610. result:=found;
  1611. end;
  1612. var
  1613. foundfile : TCmdStr;
  1614. path,
  1615. name,
  1616. hs : tpathstr;
  1617. args : string;
  1618. hp : tinputfile;
  1619. found : boolean;
  1620. macroIsString : boolean;
  1621. begin
  1622. current_scanner.skipspace;
  1623. args:=current_scanner.readcomment;
  1624. hs:=GetToken(args,' ');
  1625. if hs='' then
  1626. exit;
  1627. if (hs[1]='%') then
  1628. begin
  1629. { case insensitive }
  1630. hs:=upper(hs);
  1631. { remove %'s }
  1632. Delete(hs,1,1);
  1633. if hs[length(hs)]='%' then
  1634. Delete(hs,length(hs),1);
  1635. { save old }
  1636. path:=hs;
  1637. { first check for internal macros }
  1638. macroIsString:=true;
  1639. if hs='TIME' then
  1640. hs:=gettimestr
  1641. else
  1642. if hs='DATE' then
  1643. hs:=getdatestr
  1644. else
  1645. if hs='FILE' then
  1646. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex)
  1647. else
  1648. if hs='LINE' then
  1649. hs:=tostr(current_filepos.line)
  1650. else
  1651. if hs='LINENUM' then
  1652. begin
  1653. hs:=tostr(current_filepos.line);
  1654. macroIsString:=false;
  1655. end
  1656. else
  1657. if hs='FPCVERSION' then
  1658. hs:=version_string
  1659. else
  1660. if hs='FPCDATE' then
  1661. hs:=date_string
  1662. else
  1663. if hs='FPCTARGET' then
  1664. hs:=target_cpu_string
  1665. else
  1666. if hs='FPCTARGETCPU' then
  1667. hs:=target_cpu_string
  1668. else
  1669. if hs='FPCTARGETOS' then
  1670. hs:=target_info.shortname
  1671. else
  1672. hs:=GetEnvironmentVariable(hs);
  1673. if hs='' then
  1674. Message1(scan_w_include_env_not_found,path);
  1675. { make it a stringconst }
  1676. if macroIsString then
  1677. hs:=''''+hs+'''';
  1678. current_scanner.substitutemacro(path,@hs[1],length(hs),
  1679. current_scanner.line_no,current_scanner.inputfile.ref_index);
  1680. end
  1681. else
  1682. begin
  1683. hs:=FixFileName(hs);
  1684. path:=ExtractFilePath(hs);
  1685. name:=ExtractFileName(hs);
  1686. { Special case for Delphi compatibility: '*' has to be replaced
  1687. by the file name of the current source file. }
  1688. if (length(name)>=1) and
  1689. (name[1]='*') then
  1690. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  1691. { try to find the file }
  1692. found:=findincludefile(path,name,foundfile);
  1693. if (ExtractFileExt(name)='') then
  1694. begin
  1695. { try default extensions .inc , .pp and .pas }
  1696. if (not found) then
  1697. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  1698. if (not found) then
  1699. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  1700. if (not found) then
  1701. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  1702. end;
  1703. if current_scanner.inputfilecount<max_include_nesting then
  1704. begin
  1705. inc(current_scanner.inputfilecount);
  1706. { we need to reread the current char }
  1707. dec(current_scanner.inputpointer);
  1708. { shutdown current file }
  1709. current_scanner.tempcloseinputfile;
  1710. { load new file }
  1711. hp:=do_openinputfile(foundfile);
  1712. current_scanner.addfile(hp);
  1713. current_module.sourcefiles.register_file(hp);
  1714. if (not found) then
  1715. Message1(scan_f_cannot_open_includefile,hs);
  1716. if (not current_scanner.openinputfile) then
  1717. Message1(scan_f_cannot_open_includefile,hs);
  1718. Message1(scan_t_start_include_file,current_scanner.inputfile.path+current_scanner.inputfile.name);
  1719. current_scanner.reload;
  1720. end
  1721. else
  1722. Message(scan_f_include_deep_ten);
  1723. end;
  1724. end;
  1725. {*****************************************************************************
  1726. Preprocessor writing
  1727. *****************************************************************************}
  1728. {$ifdef PREPROCWRITE}
  1729. constructor tpreprocfile.create(const fn:string);
  1730. begin
  1731. { open outputfile }
  1732. assign(f,fn);
  1733. {$push}{$I-}
  1734. rewrite(f);
  1735. {$pop}
  1736. if ioresult<>0 then
  1737. Comment(V_Fatal,'can''t create file '+fn);
  1738. getmem(buf,preprocbufsize);
  1739. settextbuf(f,buf^,preprocbufsize);
  1740. { reset }
  1741. eolfound:=false;
  1742. spacefound:=false;
  1743. end;
  1744. destructor tpreprocfile.destroy;
  1745. begin
  1746. close(f);
  1747. freemem(buf,preprocbufsize);
  1748. end;
  1749. procedure tpreprocfile.add(const s:string);
  1750. begin
  1751. write(f,s);
  1752. end;
  1753. procedure tpreprocfile.addspace;
  1754. begin
  1755. if eolfound then
  1756. begin
  1757. writeln(f,'');
  1758. eolfound:=false;
  1759. spacefound:=false;
  1760. end
  1761. else
  1762. if spacefound then
  1763. begin
  1764. write(f,' ');
  1765. spacefound:=false;
  1766. end;
  1767. end;
  1768. {$endif PREPROCWRITE}
  1769. {*****************************************************************************
  1770. TPreProcStack
  1771. *****************************************************************************}
  1772. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  1773. begin
  1774. accept:=a;
  1775. typ:=atyp;
  1776. next:=n;
  1777. end;
  1778. {*****************************************************************************
  1779. TReplayStack
  1780. *****************************************************************************}
  1781. constructor treplaystack.Create(atoken:ttoken;asettings:tsettings;
  1782. atokenbuf:tdynamicarray;anext:treplaystack;achange_endian : boolean);
  1783. begin
  1784. token:=atoken;
  1785. settings:=asettings;
  1786. tokenbuf:=atokenbuf;
  1787. change_endian:=achange_endian;
  1788. next:=anext;
  1789. end;
  1790. {*****************************************************************************
  1791. TDirectiveItem
  1792. *****************************************************************************}
  1793. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  1794. begin
  1795. inherited Create(AList,n);
  1796. is_conditional:=false;
  1797. proc:=p;
  1798. end;
  1799. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  1800. begin
  1801. inherited Create(AList,n);
  1802. is_conditional:=true;
  1803. proc:=p;
  1804. end;
  1805. {****************************************************************************
  1806. TSCANNERFILE
  1807. ****************************************************************************}
  1808. constructor tscannerfile.create(const fn:string; is_macro: boolean = false);
  1809. begin
  1810. inputfile:=do_openinputfile(fn);
  1811. if is_macro then
  1812. inputfile.is_macro:=true;
  1813. if assigned(current_module) then
  1814. current_module.sourcefiles.register_file(inputfile);
  1815. { reset localinput }
  1816. c:=#0;
  1817. inputbuffer:=nil;
  1818. inputpointer:=nil;
  1819. inputstart:=0;
  1820. { reset scanner }
  1821. preprocstack:=nil;
  1822. replaystack:=nil;
  1823. tokenbuf_change_endian:=false;
  1824. comment_level:=0;
  1825. yylexcount:=0;
  1826. block_type:=bt_general;
  1827. line_no:=0;
  1828. lastlinepos:=0;
  1829. lasttokenpos:=0;
  1830. nexttokenpos:=0;
  1831. lasttoken:=NOTOKEN;
  1832. nexttoken:=NOTOKEN;
  1833. lastasmgetchar:=#0;
  1834. ignoredirectives:=TFPHashList.Create;
  1835. in_asm_string:=false;
  1836. end;
  1837. procedure tscannerfile.firstfile;
  1838. begin
  1839. { load block }
  1840. if not openinputfile then
  1841. Message1(scan_f_cannot_open_input,inputfile.name);
  1842. reload;
  1843. end;
  1844. destructor tscannerfile.destroy;
  1845. begin
  1846. if assigned(current_module) and
  1847. (current_module.state=ms_compiled) and
  1848. (status.errorcount=0) then
  1849. checkpreprocstack
  1850. else
  1851. begin
  1852. while assigned(preprocstack) do
  1853. poppreprocstack;
  1854. end;
  1855. while assigned(replaystack) do
  1856. popreplaystack;
  1857. if not inputfile.closed then
  1858. closeinputfile;
  1859. if inputfile.is_macro then
  1860. inputfile.free;
  1861. ignoredirectives.free;
  1862. end;
  1863. function tscannerfile.openinputfile:boolean;
  1864. begin
  1865. openinputfile:=inputfile.open;
  1866. { load buffer }
  1867. inputbuffer:=inputfile.buf;
  1868. inputpointer:=inputfile.buf;
  1869. inputstart:=inputfile.bufstart;
  1870. { line }
  1871. line_no:=0;
  1872. lastlinepos:=0;
  1873. lasttokenpos:=0;
  1874. nexttokenpos:=0;
  1875. end;
  1876. procedure tscannerfile.closeinputfile;
  1877. begin
  1878. inputfile.close;
  1879. { reset buffer }
  1880. inputbuffer:=nil;
  1881. inputpointer:=nil;
  1882. inputstart:=0;
  1883. { reset line }
  1884. line_no:=0;
  1885. lastlinepos:=0;
  1886. lasttokenpos:=0;
  1887. nexttokenpos:=0;
  1888. end;
  1889. function tscannerfile.tempopeninputfile:boolean;
  1890. begin
  1891. if inputfile.is_macro then
  1892. exit;
  1893. tempopeninputfile:=inputfile.tempopen;
  1894. { reload buffer }
  1895. inputbuffer:=inputfile.buf;
  1896. inputpointer:=inputfile.buf;
  1897. inputstart:=inputfile.bufstart;
  1898. end;
  1899. procedure tscannerfile.tempcloseinputfile;
  1900. begin
  1901. if inputfile.closed or inputfile.is_macro then
  1902. exit;
  1903. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  1904. inputfile.tempclose;
  1905. { reset buffer }
  1906. inputbuffer:=nil;
  1907. inputpointer:=nil;
  1908. inputstart:=0;
  1909. end;
  1910. procedure tscannerfile.saveinputfile;
  1911. begin
  1912. inputfile.saveinputpointer:=inputpointer;
  1913. inputfile.savelastlinepos:=lastlinepos;
  1914. inputfile.saveline_no:=line_no;
  1915. end;
  1916. procedure tscannerfile.restoreinputfile;
  1917. begin
  1918. inputbuffer:=inputfile.buf;
  1919. inputpointer:=inputfile.saveinputpointer;
  1920. lastlinepos:=inputfile.savelastlinepos;
  1921. line_no:=inputfile.saveline_no;
  1922. if not inputfile.is_macro then
  1923. parser_current_file:=inputfile.name;
  1924. end;
  1925. procedure tscannerfile.nextfile;
  1926. var
  1927. to_dispose : tinputfile;
  1928. begin
  1929. if assigned(inputfile.next) then
  1930. begin
  1931. if inputfile.is_macro then
  1932. to_dispose:=inputfile
  1933. else
  1934. begin
  1935. to_dispose:=nil;
  1936. dec(inputfilecount);
  1937. end;
  1938. { we can allways close the file, no ? }
  1939. inputfile.close;
  1940. inputfile:=inputfile.next;
  1941. if assigned(to_dispose) then
  1942. to_dispose.free;
  1943. restoreinputfile;
  1944. end;
  1945. end;
  1946. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  1947. begin
  1948. if not assigned(buf) then
  1949. internalerror(200511172);
  1950. if assigned(recordtokenbuf) then
  1951. internalerror(200511173);
  1952. recordtokenbuf:=buf;
  1953. fillchar(last_settings,sizeof(last_settings),0);
  1954. last_message:=nil;
  1955. fillchar(last_filepos,sizeof(last_filepos),0);
  1956. end;
  1957. procedure tscannerfile.stoprecordtokens;
  1958. begin
  1959. if not assigned(recordtokenbuf) then
  1960. internalerror(200511174);
  1961. recordtokenbuf:=nil;
  1962. end;
  1963. procedure tscannerfile.writetoken(t : ttoken);
  1964. var
  1965. b : byte;
  1966. begin
  1967. if ord(t)>$7f then
  1968. begin
  1969. b:=(ord(t) shr 8) or $80;
  1970. recordtokenbuf.write(b,1);
  1971. end;
  1972. b:=ord(t) and $ff;
  1973. recordtokenbuf.write(b,1);
  1974. end;
  1975. procedure tscannerfile.tokenwritesizeint(val : asizeint);
  1976. begin
  1977. recordtokenbuf.write(val,sizeof(asizeint));
  1978. end;
  1979. procedure tscannerfile.tokenwritelongint(val : longint);
  1980. begin
  1981. recordtokenbuf.write(val,sizeof(longint));
  1982. end;
  1983. procedure tscannerfile.tokenwriteshortint(val : shortint);
  1984. begin
  1985. recordtokenbuf.write(val,sizeof(shortint));
  1986. end;
  1987. procedure tscannerfile.tokenwriteword(val : word);
  1988. begin
  1989. recordtokenbuf.write(val,sizeof(word));
  1990. end;
  1991. procedure tscannerfile.tokenwritelongword(val : longword);
  1992. begin
  1993. recordtokenbuf.write(val,sizeof(longword));
  1994. end;
  1995. function tscannerfile.tokenreadsizeint : asizeint;
  1996. var
  1997. val : asizeint;
  1998. begin
  1999. replaytokenbuf.read(val,sizeof(asizeint));
  2000. if tokenbuf_change_endian then
  2001. val:=swapendian(val);
  2002. result:=val;
  2003. end;
  2004. function tscannerfile.tokenreadlongword : longword;
  2005. var
  2006. val : longword;
  2007. begin
  2008. replaytokenbuf.read(val,sizeof(longword));
  2009. if tokenbuf_change_endian then
  2010. val:=swapendian(val);
  2011. result:=val;
  2012. end;
  2013. function tscannerfile.tokenreadlongint : longint;
  2014. var
  2015. val : longint;
  2016. begin
  2017. replaytokenbuf.read(val,sizeof(longint));
  2018. if tokenbuf_change_endian then
  2019. val:=swapendian(val);
  2020. result:=val;
  2021. end;
  2022. function tscannerfile.tokenreadshortint : shortint;
  2023. var
  2024. val : shortint;
  2025. begin
  2026. replaytokenbuf.read(val,sizeof(shortint));
  2027. result:=val;
  2028. end;
  2029. function tscannerfile.tokenreadbyte : byte;
  2030. var
  2031. val : byte;
  2032. begin
  2033. replaytokenbuf.read(val,sizeof(byte));
  2034. result:=val;
  2035. end;
  2036. function tscannerfile.tokenreadsmallint : smallint;
  2037. var
  2038. val : smallint;
  2039. begin
  2040. replaytokenbuf.read(val,sizeof(smallint));
  2041. if tokenbuf_change_endian then
  2042. val:=swapendian(val);
  2043. result:=val;
  2044. end;
  2045. function tscannerfile.tokenreadword : word;
  2046. var
  2047. val : word;
  2048. begin
  2049. replaytokenbuf.read(val,sizeof(word));
  2050. if tokenbuf_change_endian then
  2051. val:=swapendian(val);
  2052. result:=val;
  2053. end;
  2054. function tscannerfile.tokenreadenum(size : longint) : longword;
  2055. begin
  2056. if size=1 then
  2057. result:=tokenreadbyte
  2058. else if size=2 then
  2059. result:=tokenreadword
  2060. else if size=4 then
  2061. result:=tokenreadlongword;
  2062. end;
  2063. procedure tscannerfile.tokenreadset(var b;size : longint);
  2064. var
  2065. i : longint;
  2066. begin
  2067. replaytokenbuf.read(b,size);
  2068. if tokenbuf_change_endian then
  2069. for i:=0 to size-1 do
  2070. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  2071. end;
  2072. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  2073. begin
  2074. recordtokenbuf.write(b,size);
  2075. end;
  2076. procedure tscannerfile.tokenwriteset(var b;size : longint);
  2077. begin
  2078. recordtokenbuf.write(b,size);
  2079. end;
  2080. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  2081. { This procedure
  2082. needs to be changed whenever
  2083. globals.tsettings type is changed,
  2084. the problem is that no error will appear
  2085. before tests with generics are tested. PM }
  2086. var
  2087. startpos, endpos : longword;
  2088. begin
  2089. { WARNING all those fields need to be in the correct
  2090. order otherwise cross_endian PPU reading will fail }
  2091. startpos:=replaytokenbuf.pos;
  2092. with asettings do
  2093. begin
  2094. alignment.procalign:=tokenreadlongint;
  2095. alignment.loopalign:=tokenreadlongint;
  2096. alignment.jumpalign:=tokenreadlongint;
  2097. alignment.constalignmin:=tokenreadlongint;
  2098. alignment.constalignmax:=tokenreadlongint;
  2099. alignment.varalignmin:=tokenreadlongint;
  2100. alignment.varalignmax:=tokenreadlongint;
  2101. alignment.localalignmin:=tokenreadlongint;
  2102. alignment.localalignmax:=tokenreadlongint;
  2103. alignment.recordalignmin:=tokenreadlongint;
  2104. alignment.recordalignmax:=tokenreadlongint;
  2105. alignment.maxCrecordalign:=tokenreadlongint;
  2106. tokenreadset(globalswitches,sizeof(globalswitches));
  2107. tokenreadset(targetswitches,sizeof(targetswitches));
  2108. tokenreadset(moduleswitches,sizeof(moduleswitches));
  2109. tokenreadset(localswitches,sizeof(localswitches));
  2110. tokenreadset(modeswitches,sizeof(modeswitches));
  2111. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  2112. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2113. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2114. tokenreadset(debugswitches,sizeof(debugswitches));
  2115. { 0: old behaviour for sets <=256 elements
  2116. >0: round to this size }
  2117. setalloc:=tokenreadshortint;
  2118. packenum:=tokenreadshortint;
  2119. packrecords:=tokenreadshortint;
  2120. maxfpuregisters:=tokenreadshortint;
  2121. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2122. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2123. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  2124. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  2125. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  2126. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  2127. { tstringencoding is word type,
  2128. thus this should be OK here }
  2129. sourcecodepage:=tstringEncoding(tokenreadword);
  2130. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  2131. disabledircache:=boolean(tokenreadbyte);
  2132. {$if defined(ARM) or defined(AVR)}
  2133. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)));
  2134. {$endif defined(ARM) or defined(AVR)}
  2135. endpos:=replaytokenbuf.pos;
  2136. if endpos-startpos<>expected_size then
  2137. Comment(V_Error,'Wrong size of Settings read-in');
  2138. end;
  2139. end;
  2140. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  2141. { This procedure
  2142. needs to be changed whenever
  2143. globals.tsettings type is changed,
  2144. the problem is that no error will appear
  2145. before tests with generics are tested. PM }
  2146. var
  2147. sizepos, startpos, endpos : longword;
  2148. begin
  2149. { WARNING all those fields need to be in the correct
  2150. order otherwise cross_endian PPU reading will fail }
  2151. sizepos:=recordtokenbuf.pos;
  2152. size:=0;
  2153. tokenwritesizeint(size);
  2154. startpos:=recordtokenbuf.pos;
  2155. with asettings do
  2156. begin
  2157. tokenwritelongint(alignment.procalign);
  2158. tokenwritelongint(alignment.loopalign);
  2159. tokenwritelongint(alignment.jumpalign);
  2160. tokenwritelongint(alignment.constalignmin);
  2161. tokenwritelongint(alignment.constalignmax);
  2162. tokenwritelongint(alignment.varalignmin);
  2163. tokenwritelongint(alignment.varalignmax);
  2164. tokenwritelongint(alignment.localalignmin);
  2165. tokenwritelongint(alignment.localalignmax);
  2166. tokenwritelongint(alignment.recordalignmin);
  2167. tokenwritelongint(alignment.recordalignmax);
  2168. tokenwritelongint(alignment.maxCrecordalign);
  2169. tokenwriteset(globalswitches,sizeof(globalswitches));
  2170. tokenwriteset(targetswitches,sizeof(targetswitches));
  2171. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  2172. tokenwriteset(localswitches,sizeof(localswitches));
  2173. tokenwriteset(modeswitches,sizeof(modeswitches));
  2174. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  2175. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2176. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2177. tokenwriteset(debugswitches,sizeof(debugswitches));
  2178. { 0: old behaviour for sets <=256 elements
  2179. >0: round to this size }
  2180. tokenwriteshortint(setalloc);
  2181. tokenwriteshortint(packenum);
  2182. tokenwriteshortint(packrecords);
  2183. tokenwriteshortint(maxfpuregisters);
  2184. tokenwriteenum(cputype,sizeof(tcputype));
  2185. tokenwriteenum(optimizecputype,sizeof(tcputype));
  2186. tokenwriteenum(fputype,sizeof(tfputype));
  2187. tokenwriteenum(asmmode,sizeof(tasmmode));
  2188. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  2189. tokenwriteenum(defproccall,sizeof(tproccalloption));
  2190. { tstringencoding is word type,
  2191. thus this should be OK here }
  2192. tokenwriteword(sourcecodepage);
  2193. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  2194. recordtokenbuf.write(byte(disabledircache),1);
  2195. {$if defined(ARM) or defined(AVR)}
  2196. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  2197. {$endif defined(ARM) or defined(AVR)}
  2198. endpos:=recordtokenbuf.pos;
  2199. size:=endpos-startpos;
  2200. recordtokenbuf.seek(sizepos);
  2201. tokenwritesizeint(size);
  2202. recordtokenbuf.seek(endpos);
  2203. end;
  2204. end;
  2205. procedure tscannerfile.recordtoken;
  2206. var
  2207. t : ttoken;
  2208. s : tspecialgenerictoken;
  2209. len,msgnb,copy_size : asizeint;
  2210. val : longint;
  2211. b : byte;
  2212. pmsg : pmessagestaterecord;
  2213. begin
  2214. if not assigned(recordtokenbuf) then
  2215. internalerror(200511176);
  2216. t:=_GENERICSPECIALTOKEN;
  2217. { settings changed? }
  2218. { last field pmessage is handled separately below in
  2219. ST_LOADMESSAGES }
  2220. if CompareByte(current_settings,last_settings,
  2221. sizeof(current_settings)-sizeof(pointer))<>0 then
  2222. begin
  2223. { use a special token to record it }
  2224. s:=ST_LOADSETTINGS;
  2225. writetoken(t);
  2226. recordtokenbuf.write(s,1);
  2227. copy_size:=sizeof(current_settings)-sizeof(pointer);
  2228. tokenwritesettings(current_settings,copy_size);
  2229. last_settings:=current_settings;
  2230. end;
  2231. if current_settings.pmessage<>last_message then
  2232. begin
  2233. { use a special token to record it }
  2234. s:=ST_LOADMESSAGES;
  2235. writetoken(t);
  2236. recordtokenbuf.write(s,1);
  2237. msgnb:=0;
  2238. pmsg:=current_settings.pmessage;
  2239. while assigned(pmsg) do
  2240. begin
  2241. if msgnb=high(asizeint) then
  2242. { Too many messages }
  2243. internalerror(2011090401);
  2244. inc(msgnb);
  2245. pmsg:=pmsg^.next;
  2246. end;
  2247. tokenwritesizeint(msgnb);
  2248. pmsg:=current_settings.pmessage;
  2249. while assigned(pmsg) do
  2250. begin
  2251. { What about endianess here?}
  2252. { SB: this is handled by tokenreadlongint }
  2253. val:=pmsg^.value;
  2254. tokenwritelongint(val);
  2255. val:=ord(pmsg^.state);
  2256. tokenwritelongint(val);
  2257. pmsg:=pmsg^.next;
  2258. end;
  2259. last_message:=current_settings.pmessage;
  2260. end;
  2261. { file pos changes? }
  2262. if current_tokenpos.line<>last_filepos.line then
  2263. begin
  2264. s:=ST_LINE;
  2265. writetoken(t);
  2266. recordtokenbuf.write(s,1);
  2267. recordtokenbuf.write(current_tokenpos.line,sizeof(current_tokenpos.line));
  2268. last_filepos.line:=current_tokenpos.line;
  2269. end;
  2270. if current_tokenpos.column<>last_filepos.column then
  2271. begin
  2272. s:=ST_COLUMN;
  2273. writetoken(t);
  2274. { can the column be written packed? }
  2275. if current_tokenpos.column<$80 then
  2276. begin
  2277. b:=$80 or current_tokenpos.column;
  2278. recordtokenbuf.write(b,1);
  2279. end
  2280. else
  2281. begin
  2282. recordtokenbuf.write(s,1);
  2283. recordtokenbuf.write(current_tokenpos.column,sizeof(current_tokenpos.column));
  2284. end;
  2285. last_filepos.column:=current_tokenpos.column;
  2286. end;
  2287. if current_tokenpos.fileindex<>last_filepos.fileindex then
  2288. begin
  2289. s:=ST_FILEINDEX;
  2290. writetoken(t);
  2291. recordtokenbuf.write(s,1);
  2292. recordtokenbuf.write(current_tokenpos.fileindex,sizeof(current_tokenpos.fileindex));
  2293. last_filepos.fileindex:=current_tokenpos.fileindex;
  2294. end;
  2295. writetoken(token);
  2296. if token<>_GENERICSPECIALTOKEN then
  2297. writetoken(idtoken);
  2298. case token of
  2299. _CWCHAR,
  2300. _CWSTRING :
  2301. begin
  2302. tokenwritesizeint(patternw^.len);
  2303. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2304. end;
  2305. _CSTRING:
  2306. begin
  2307. len:=length(cstringpattern);
  2308. tokenwritesizeint(len);
  2309. recordtokenbuf.write(cstringpattern[1],length(cstringpattern));
  2310. end;
  2311. _CCHAR,
  2312. _INTCONST,
  2313. _REALNUMBER :
  2314. begin
  2315. { pexpr.pas messes with pattern in case of negative integer consts,
  2316. see around line 2562 the comment of JM; remove the - before recording it
  2317. (FK)
  2318. }
  2319. if (token=_INTCONST) and (pattern[1]='-') then
  2320. delete(pattern,1,1);
  2321. recordtokenbuf.write(pattern[0],1);
  2322. recordtokenbuf.write(pattern[1],length(pattern));
  2323. end;
  2324. _ID :
  2325. begin
  2326. recordtokenbuf.write(orgpattern[0],1);
  2327. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  2328. end;
  2329. end;
  2330. end;
  2331. procedure tscannerfile.startreplaytokens(buf:tdynamicarray; achange_endian : boolean);
  2332. begin
  2333. if not assigned(buf) then
  2334. internalerror(200511175);
  2335. { save current token }
  2336. if token in [_CWCHAR,_CWSTRING,_CCHAR,_CSTRING,_INTCONST,_REALNUMBER,_ID] then
  2337. internalerror(200511178);
  2338. replaystack:=treplaystack.create(token,current_settings,
  2339. replaytokenbuf,replaystack,tokenbuf_change_endian);
  2340. if assigned(inputpointer) then
  2341. dec(inputpointer);
  2342. { install buffer }
  2343. replaytokenbuf:=buf;
  2344. tokenbuf_change_endian:=achange_endian;
  2345. { reload next token }
  2346. replaytokenbuf.seek(0);
  2347. replaytoken;
  2348. end;
  2349. function tscannerfile.readtoken: ttoken;
  2350. var
  2351. b,b2 : byte;
  2352. begin
  2353. replaytokenbuf.read(b,1);
  2354. if (b and $80)<>0 then
  2355. begin
  2356. replaytokenbuf.read(b2,1);
  2357. result:=ttoken(((b and $7f) shl 8) or b2);
  2358. end
  2359. else
  2360. result:=ttoken(b);
  2361. end;
  2362. procedure tscannerfile.replaytoken;
  2363. var
  2364. wlen,mesgnb,copy_size : asizeint;
  2365. specialtoken : tspecialgenerictoken;
  2366. i : byte;
  2367. pmsg,prevmsg : pmessagestaterecord;
  2368. begin
  2369. if not assigned(replaytokenbuf) then
  2370. internalerror(200511177);
  2371. { End of replay buffer? Then load the next char from the file again }
  2372. if replaytokenbuf.pos>=replaytokenbuf.size then
  2373. begin
  2374. token:=replaystack.token;
  2375. replaytokenbuf:=replaystack.tokenbuf;
  2376. { restore compiler settings }
  2377. current_settings:=replaystack.settings;
  2378. popreplaystack;
  2379. if assigned(inputpointer) then
  2380. begin
  2381. c:=inputpointer^;
  2382. inc(inputpointer);
  2383. end;
  2384. exit;
  2385. end;
  2386. repeat
  2387. { load token from the buffer }
  2388. token:=readtoken;
  2389. if token<>_GENERICSPECIALTOKEN then
  2390. idtoken:=readtoken
  2391. else
  2392. idtoken:=_NOID;
  2393. case token of
  2394. _CWCHAR,
  2395. _CWSTRING :
  2396. begin
  2397. wlen:=tokenreadsizeint;
  2398. setlengthwidestring(patternw,wlen);
  2399. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2400. orgpattern:='';
  2401. pattern:='';
  2402. cstringpattern:='';
  2403. end;
  2404. _CSTRING:
  2405. begin
  2406. wlen:=tokenreadsizeint;
  2407. setlength(cstringpattern,wlen);
  2408. replaytokenbuf.read(cstringpattern[1],wlen);
  2409. orgpattern:='';
  2410. pattern:='';
  2411. end;
  2412. _CCHAR,
  2413. _INTCONST,
  2414. _REALNUMBER :
  2415. begin
  2416. replaytokenbuf.read(pattern[0],1);
  2417. replaytokenbuf.read(pattern[1],length(pattern));
  2418. orgpattern:='';
  2419. end;
  2420. _ID :
  2421. begin
  2422. replaytokenbuf.read(orgpattern[0],1);
  2423. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  2424. pattern:=upper(orgpattern);
  2425. end;
  2426. _GENERICSPECIALTOKEN:
  2427. begin
  2428. replaytokenbuf.read(specialtoken,1);
  2429. { packed column? }
  2430. if (ord(specialtoken) and $80)<>0 then
  2431. begin
  2432. current_tokenpos.column:=ord(specialtoken) and $7f;
  2433. current_filepos:=current_tokenpos;
  2434. end
  2435. else
  2436. case specialtoken of
  2437. ST_LOADSETTINGS:
  2438. begin
  2439. copy_size:=tokenreadsizeint;
  2440. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  2441. // internalerror(2011090501);
  2442. {
  2443. replaytokenbuf.read(current_settings,copy_size);
  2444. }
  2445. tokenreadsettings(current_settings,copy_size);
  2446. end;
  2447. ST_LOADMESSAGES:
  2448. begin
  2449. current_settings.pmessage:=nil;
  2450. mesgnb:=tokenreadsizeint;
  2451. if mesgnb>0 then
  2452. Comment(V_Error,'Message recordind not yet supported');
  2453. for i:=1 to mesgnb do
  2454. begin
  2455. new(pmsg);
  2456. if i=1 then
  2457. begin
  2458. current_settings.pmessage:=pmsg;
  2459. prevmsg:=nil;
  2460. end
  2461. else
  2462. prevmsg^.next:=pmsg;
  2463. pmsg^.value:=tokenreadlongint;
  2464. pmsg^.state:=tmsgstate(tokenreadlongint);
  2465. pmsg^.next:=nil;
  2466. prevmsg:=pmsg;
  2467. end;
  2468. end;
  2469. ST_LINE:
  2470. begin
  2471. current_tokenpos.line:=tokenreadlongint;
  2472. current_filepos:=current_tokenpos;
  2473. end;
  2474. ST_COLUMN:
  2475. begin
  2476. current_tokenpos.column:=tokenreadword;
  2477. current_filepos:=current_tokenpos;
  2478. end;
  2479. ST_FILEINDEX:
  2480. begin
  2481. current_tokenpos.fileindex:=tokenreadword;
  2482. current_filepos:=current_tokenpos;
  2483. end;
  2484. else
  2485. internalerror(2006103010);
  2486. end;
  2487. continue;
  2488. end;
  2489. end;
  2490. break;
  2491. until false;
  2492. end;
  2493. procedure tscannerfile.addfile(hp:tinputfile);
  2494. begin
  2495. saveinputfile;
  2496. { add to list }
  2497. hp.next:=inputfile;
  2498. inputfile:=hp;
  2499. { load new inputfile }
  2500. restoreinputfile;
  2501. end;
  2502. procedure tscannerfile.reload;
  2503. begin
  2504. with inputfile do
  2505. begin
  2506. { when nothing more to read then leave immediatly, so we
  2507. don't change the current_filepos and leave it point to the last
  2508. char }
  2509. if (c=#26) and (not assigned(next)) then
  2510. exit;
  2511. repeat
  2512. { still more to read?, then change the #0 to a space so its seen
  2513. as a seperator, this can't be used for macro's which can change
  2514. the place of the #0 in the buffer with tempopen }
  2515. if (c=#0) and (bufsize>0) and
  2516. not(inputfile.is_macro) and
  2517. (inputpointer-inputbuffer<bufsize) then
  2518. begin
  2519. c:=' ';
  2520. inc(inputpointer);
  2521. exit;
  2522. end;
  2523. { can we read more from this file ? }
  2524. if (c<>#26) and (not endoffile) then
  2525. begin
  2526. readbuf;
  2527. inputpointer:=buf;
  2528. inputbuffer:=buf;
  2529. inputstart:=bufstart;
  2530. { first line? }
  2531. if line_no=0 then
  2532. begin
  2533. c:=inputpointer^;
  2534. { eat utf-8 signature? }
  2535. if (ord(inputpointer^)=$ef) and
  2536. (ord((inputpointer+1)^)=$bb) and
  2537. (ord((inputpointer+2)^)=$bf) then
  2538. begin
  2539. (* we don't support including files with an UTF-8 bom
  2540. inside another file that wasn't encoded as UTF-8
  2541. already (we don't support {$codepage xxx} switches in
  2542. the middle of a file either) *)
  2543. if (current_settings.sourcecodepage<>CP_UTF8) and
  2544. not current_module.in_global then
  2545. Message(scanner_f_illegal_utf8_bom);
  2546. inc(inputpointer,3);
  2547. message(scan_c_switching_to_utf8);
  2548. current_settings.sourcecodepage:=CP_UTF8;
  2549. include(current_settings.moduleswitches,cs_explicit_codepage);
  2550. end;
  2551. line_no:=1;
  2552. if cs_asm_source in current_settings.globalswitches then
  2553. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  2554. end;
  2555. end
  2556. else
  2557. begin
  2558. { load eof position in tokenpos/current_filepos }
  2559. gettokenpos;
  2560. { close file }
  2561. closeinputfile;
  2562. { no next module, than EOF }
  2563. if not assigned(inputfile.next) then
  2564. begin
  2565. c:=#26;
  2566. exit;
  2567. end;
  2568. { load next file and reopen it }
  2569. nextfile;
  2570. tempopeninputfile;
  2571. { status }
  2572. Message1(scan_t_back_in,inputfile.name);
  2573. end;
  2574. { load next char }
  2575. c:=inputpointer^;
  2576. inc(inputpointer);
  2577. until c<>#0; { if also end, then reload again }
  2578. end;
  2579. end;
  2580. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  2581. var
  2582. hp : tinputfile;
  2583. begin
  2584. { save old postion }
  2585. dec(inputpointer);
  2586. tempcloseinputfile;
  2587. { create macro 'file' }
  2588. { use special name to dispose after !! }
  2589. hp:=do_openinputfile('_Macro_.'+macname);
  2590. addfile(hp);
  2591. with inputfile do
  2592. begin
  2593. setmacro(p,len);
  2594. { local buffer }
  2595. inputbuffer:=buf;
  2596. inputpointer:=buf;
  2597. inputstart:=bufstart;
  2598. ref_index:=fileindex;
  2599. end;
  2600. { reset line }
  2601. line_no:=line;
  2602. lastlinepos:=0;
  2603. lasttokenpos:=0;
  2604. nexttokenpos:=0;
  2605. { load new c }
  2606. c:=inputpointer^;
  2607. inc(inputpointer);
  2608. end;
  2609. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  2610. begin
  2611. tokenpos:=inputstart+(inputpointer-inputbuffer);
  2612. filepos.line:=line_no;
  2613. filepos.column:=tokenpos-lastlinepos;
  2614. filepos.fileindex:=inputfile.ref_index;
  2615. filepos.moduleindex:=current_module.unit_index;
  2616. end;
  2617. procedure tscannerfile.gettokenpos;
  2618. { load the values of tokenpos and lasttokenpos }
  2619. begin
  2620. do_gettokenpos(lasttokenpos,current_tokenpos);
  2621. current_filepos:=current_tokenpos;
  2622. end;
  2623. procedure tscannerfile.cachenexttokenpos;
  2624. begin
  2625. do_gettokenpos(nexttokenpos,next_filepos);
  2626. end;
  2627. procedure tscannerfile.setnexttoken;
  2628. begin
  2629. token:=nexttoken;
  2630. nexttoken:=NOTOKEN;
  2631. lasttokenpos:=nexttokenpos;
  2632. current_tokenpos:=next_filepos;
  2633. current_filepos:=current_tokenpos;
  2634. nexttokenpos:=0;
  2635. end;
  2636. procedure tscannerfile.savetokenpos;
  2637. begin
  2638. oldlasttokenpos:=lasttokenpos;
  2639. oldcurrent_filepos:=current_filepos;
  2640. oldcurrent_tokenpos:=current_tokenpos;
  2641. end;
  2642. procedure tscannerfile.restoretokenpos;
  2643. begin
  2644. lasttokenpos:=oldlasttokenpos;
  2645. current_filepos:=oldcurrent_filepos;
  2646. current_tokenpos:=oldcurrent_tokenpos;
  2647. end;
  2648. procedure tscannerfile.inc_comment_level;
  2649. begin
  2650. if (m_nested_comment in current_settings.modeswitches) then
  2651. inc(comment_level)
  2652. else
  2653. comment_level:=1;
  2654. if (comment_level>1) then
  2655. begin
  2656. savetokenpos;
  2657. gettokenpos; { update for warning }
  2658. Message1(scan_w_comment_level,tostr(comment_level));
  2659. restoretokenpos;
  2660. end;
  2661. end;
  2662. procedure tscannerfile.dec_comment_level;
  2663. begin
  2664. if (m_nested_comment in current_settings.modeswitches) then
  2665. dec(comment_level)
  2666. else
  2667. comment_level:=0;
  2668. end;
  2669. procedure tscannerfile.linebreak;
  2670. var
  2671. cur : char;
  2672. begin
  2673. with inputfile do
  2674. begin
  2675. if (byte(inputpointer^)=0) and not(endoffile) then
  2676. begin
  2677. cur:=c;
  2678. reload;
  2679. if byte(cur)+byte(c)<>23 then
  2680. dec(inputpointer);
  2681. end
  2682. else
  2683. begin
  2684. { Support all combination of #10 and #13 as line break }
  2685. if (byte(inputpointer^)+byte(c)=23) then
  2686. inc(inputpointer);
  2687. end;
  2688. { Always return #10 as line break }
  2689. c:=#10;
  2690. { increase line counters }
  2691. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  2692. inc(line_no);
  2693. { update linebuffer }
  2694. if cs_asm_source in current_settings.globalswitches then
  2695. inputfile.setline(line_no,lastlinepos);
  2696. { update for status and call the show status routine,
  2697. but don't touch current_filepos ! }
  2698. savetokenpos;
  2699. gettokenpos; { update for v_status }
  2700. inc(status.compiledlines);
  2701. ShowStatus;
  2702. restoretokenpos;
  2703. end;
  2704. end;
  2705. procedure tscannerfile.illegal_char(c:char);
  2706. var
  2707. s : string;
  2708. begin
  2709. if c in [#32..#255] then
  2710. s:=''''+c+''''
  2711. else
  2712. s:='#'+tostr(ord(c));
  2713. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  2714. end;
  2715. procedure tscannerfile.end_of_file;
  2716. begin
  2717. checkpreprocstack;
  2718. Message(scan_f_end_of_file);
  2719. end;
  2720. {-------------------------------------------
  2721. IF Conditional Handling
  2722. -------------------------------------------}
  2723. procedure tscannerfile.checkpreprocstack;
  2724. begin
  2725. { check for missing ifdefs }
  2726. while assigned(preprocstack) do
  2727. begin
  2728. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  2729. preprocstack.owner.inputfile.name,tostr(preprocstack.line_nb));
  2730. poppreprocstack;
  2731. end;
  2732. end;
  2733. procedure tscannerfile.poppreprocstack;
  2734. var
  2735. hp : tpreprocstack;
  2736. begin
  2737. if assigned(preprocstack) then
  2738. begin
  2739. Message1(scan_c_endif_found,preprocstack.name);
  2740. hp:=preprocstack.next;
  2741. preprocstack.free;
  2742. preprocstack:=hp;
  2743. end
  2744. else
  2745. Message(scan_e_endif_without_if);
  2746. end;
  2747. procedure tscannerfile.ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  2748. var
  2749. condition: Boolean;
  2750. valuedescr: String;
  2751. begin
  2752. if (preprocstack=nil) or preprocstack.accept then
  2753. condition:= compile_time_predicate(valuedescr)
  2754. else
  2755. begin
  2756. condition:= false;
  2757. valuedescr:= '';
  2758. end;
  2759. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  2760. preprocstack.name:=valuedescr;
  2761. preprocstack.line_nb:=line_no;
  2762. preprocstack.owner:=self;
  2763. if preprocstack.accept then
  2764. Message2(messid,preprocstack.name,'accepted')
  2765. else
  2766. Message2(messid,preprocstack.name,'rejected');
  2767. end;
  2768. procedure tscannerfile.elsepreprocstack;
  2769. begin
  2770. if assigned(preprocstack) and
  2771. (preprocstack.typ<>pp_else) then
  2772. begin
  2773. if (preprocstack.typ=pp_elseif) then
  2774. preprocstack.accept:=false
  2775. else
  2776. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  2777. preprocstack.accept:=not preprocstack.accept;
  2778. preprocstack.typ:=pp_else;
  2779. preprocstack.line_nb:=line_no;
  2780. if preprocstack.accept then
  2781. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2782. else
  2783. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2784. end
  2785. else
  2786. Message(scan_e_endif_without_if);
  2787. end;
  2788. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  2789. var
  2790. valuedescr: String;
  2791. begin
  2792. if assigned(preprocstack) and
  2793. (preprocstack.typ in [pp_if,pp_elseif]) then
  2794. begin
  2795. { when the branch is accepted we use pp_elseif so we know that
  2796. all the next branches need to be rejected. when this branch is still
  2797. not accepted then leave it at pp_if }
  2798. if (preprocstack.typ=pp_elseif) then
  2799. preprocstack.accept:=false
  2800. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  2801. begin
  2802. preprocstack.accept:=false;
  2803. preprocstack.typ:=pp_elseif;
  2804. end
  2805. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  2806. and compile_time_predicate(valuedescr) then
  2807. begin
  2808. preprocstack.name:=valuedescr;
  2809. preprocstack.accept:=true;
  2810. preprocstack.typ:=pp_elseif;
  2811. end;
  2812. preprocstack.line_nb:=line_no;
  2813. if preprocstack.accept then
  2814. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2815. else
  2816. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2817. end
  2818. else
  2819. Message(scan_e_endif_without_if);
  2820. end;
  2821. procedure tscannerfile.popreplaystack;
  2822. var
  2823. hp : treplaystack;
  2824. begin
  2825. if assigned(replaystack) then
  2826. begin
  2827. hp:=replaystack.next;
  2828. replaystack.free;
  2829. replaystack:=hp;
  2830. if assigned (replaystack) then
  2831. tokenbuf_change_endian:=replaystack.change_endian
  2832. else
  2833. tokenbuf_change_endian:=false;
  2834. end;
  2835. end;
  2836. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  2837. begin
  2838. savetokenpos;
  2839. repeat
  2840. current_scanner.gettokenpos;
  2841. Message1(scan_d_handling_switch,'$'+p.name);
  2842. p.proc();
  2843. { accept the text ? }
  2844. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  2845. break
  2846. else
  2847. begin
  2848. current_scanner.gettokenpos;
  2849. Message(scan_c_skipping_until);
  2850. repeat
  2851. current_scanner.skipuntildirective;
  2852. if not (m_mac in current_settings.modeswitches) then
  2853. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  2854. else
  2855. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  2856. until assigned(p) and (p.is_conditional);
  2857. current_scanner.gettokenpos;
  2858. end;
  2859. until false;
  2860. restoretokenpos;
  2861. end;
  2862. procedure tscannerfile.handledirectives;
  2863. var
  2864. t : tdirectiveitem;
  2865. hs : string;
  2866. begin
  2867. gettokenpos;
  2868. readchar; {Remove the $}
  2869. hs:=readid;
  2870. { handle empty directive }
  2871. if hs='' then
  2872. begin
  2873. Message1(scan_w_illegal_switch,'$');
  2874. exit;
  2875. end;
  2876. {$ifdef PREPROCWRITE}
  2877. if parapreprocess then
  2878. begin
  2879. t:=Get_Directive(hs);
  2880. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  2881. begin
  2882. preprocfile^.AddSpace;
  2883. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  2884. exit;
  2885. end;
  2886. end;
  2887. {$endif PREPROCWRITE}
  2888. { skip this directive? }
  2889. if (ignoredirectives.find(hs)<>nil) then
  2890. begin
  2891. if (comment_level>0) then
  2892. readcomment;
  2893. { we've read the whole comment }
  2894. aktcommentstyle:=comment_none;
  2895. exit;
  2896. end;
  2897. { Check for compiler switches }
  2898. while (length(hs)=1) and (c in ['-','+']) do
  2899. begin
  2900. Message1(scan_d_handling_switch,'$'+hs+c);
  2901. HandleSwitch(hs[1],c);
  2902. current_scanner.readchar; {Remove + or -}
  2903. if c=',' then
  2904. begin
  2905. current_scanner.readchar; {Remove , }
  2906. { read next switch, support $v+,$+}
  2907. hs:=current_scanner.readid;
  2908. if (hs='') then
  2909. begin
  2910. if (c='$') and (m_fpc in current_settings.modeswitches) then
  2911. begin
  2912. current_scanner.readchar; { skip $ }
  2913. hs:=current_scanner.readid;
  2914. end;
  2915. if (hs='') then
  2916. Message1(scan_w_illegal_directive,'$'+c);
  2917. end;
  2918. end
  2919. else
  2920. hs:='';
  2921. end;
  2922. { directives may follow switches after a , }
  2923. if hs<>'' then
  2924. begin
  2925. if not (m_mac in current_settings.modeswitches) then
  2926. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  2927. else
  2928. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  2929. if assigned(t) then
  2930. begin
  2931. if t.is_conditional then
  2932. handleconditional(t)
  2933. else
  2934. begin
  2935. Message1(scan_d_handling_switch,'$'+hs);
  2936. t.proc();
  2937. end;
  2938. end
  2939. else
  2940. begin
  2941. current_scanner.ignoredirectives.Add(hs,nil);
  2942. Message1(scan_w_illegal_directive,'$'+hs);
  2943. end;
  2944. { conditionals already read the comment }
  2945. if (current_scanner.comment_level>0) then
  2946. current_scanner.readcomment;
  2947. { we've read the whole comment }
  2948. aktcommentstyle:=comment_none;
  2949. end;
  2950. end;
  2951. procedure tscannerfile.readchar;
  2952. begin
  2953. c:=inputpointer^;
  2954. if c=#0 then
  2955. reload
  2956. else
  2957. inc(inputpointer);
  2958. end;
  2959. procedure tscannerfile.readstring;
  2960. var
  2961. i : longint;
  2962. err : boolean;
  2963. begin
  2964. err:=false;
  2965. i:=0;
  2966. repeat
  2967. case c of
  2968. '_',
  2969. '0'..'9',
  2970. 'A'..'Z' :
  2971. begin
  2972. if i<255 then
  2973. begin
  2974. inc(i);
  2975. orgpattern[i]:=c;
  2976. pattern[i]:=c;
  2977. end
  2978. else
  2979. begin
  2980. if not err then
  2981. begin
  2982. Message(scan_e_string_exceeds_255_chars);
  2983. err:=true;
  2984. end;
  2985. end;
  2986. c:=inputpointer^;
  2987. inc(inputpointer);
  2988. end;
  2989. 'a'..'z' :
  2990. begin
  2991. if i<255 then
  2992. begin
  2993. inc(i);
  2994. orgpattern[i]:=c;
  2995. pattern[i]:=chr(ord(c)-32)
  2996. end
  2997. else
  2998. begin
  2999. if not err then
  3000. begin
  3001. Message(scan_e_string_exceeds_255_chars);
  3002. err:=true;
  3003. end;
  3004. end;
  3005. c:=inputpointer^;
  3006. inc(inputpointer);
  3007. end;
  3008. #0 :
  3009. reload;
  3010. else
  3011. break;
  3012. end;
  3013. until false;
  3014. orgpattern[0]:=chr(i);
  3015. pattern[0]:=chr(i);
  3016. end;
  3017. procedure tscannerfile.readnumber;
  3018. var
  3019. base,
  3020. i : longint;
  3021. begin
  3022. case c of
  3023. '%' :
  3024. begin
  3025. readchar;
  3026. base:=2;
  3027. pattern[1]:='%';
  3028. i:=1;
  3029. end;
  3030. '&' :
  3031. begin
  3032. readchar;
  3033. base:=8;
  3034. pattern[1]:='&';
  3035. i:=1;
  3036. end;
  3037. '$' :
  3038. begin
  3039. readchar;
  3040. base:=16;
  3041. pattern[1]:='$';
  3042. i:=1;
  3043. end;
  3044. else
  3045. begin
  3046. base:=10;
  3047. i:=0;
  3048. end;
  3049. end;
  3050. while ((base>=10) and (c in ['0'..'9'])) or
  3051. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  3052. ((base=8) and (c in ['0'..'7'])) or
  3053. ((base=2) and (c in ['0'..'1'])) do
  3054. begin
  3055. if i<255 then
  3056. begin
  3057. inc(i);
  3058. pattern[i]:=c;
  3059. end;
  3060. readchar;
  3061. end;
  3062. pattern[0]:=chr(i);
  3063. end;
  3064. function tscannerfile.readid:string;
  3065. begin
  3066. readstring;
  3067. readid:=pattern;
  3068. end;
  3069. function tscannerfile.readval:longint;
  3070. var
  3071. l : longint;
  3072. w : integer;
  3073. begin
  3074. readnumber;
  3075. val(pattern,l,w);
  3076. readval:=l;
  3077. end;
  3078. function tscannerfile.readval_asstring:string;
  3079. begin
  3080. readnumber;
  3081. readval_asstring:=pattern;
  3082. end;
  3083. function tscannerfile.readcomment:string;
  3084. var
  3085. i : longint;
  3086. begin
  3087. i:=0;
  3088. repeat
  3089. case c of
  3090. '{' :
  3091. begin
  3092. if aktcommentstyle=comment_tp then
  3093. inc_comment_level;
  3094. end;
  3095. '}' :
  3096. begin
  3097. if aktcommentstyle=comment_tp then
  3098. begin
  3099. readchar;
  3100. dec_comment_level;
  3101. if comment_level=0 then
  3102. break
  3103. else
  3104. continue;
  3105. end;
  3106. end;
  3107. '*' :
  3108. begin
  3109. if aktcommentstyle=comment_oldtp then
  3110. begin
  3111. readchar;
  3112. if c=')' then
  3113. begin
  3114. readchar;
  3115. dec_comment_level;
  3116. break;
  3117. end
  3118. else
  3119. { Add both characters !!}
  3120. if (i<255) then
  3121. begin
  3122. inc(i);
  3123. readcomment[i]:='*';
  3124. if (i<255) then
  3125. begin
  3126. inc(i);
  3127. readcomment[i]:=c;
  3128. end;
  3129. end;
  3130. end
  3131. else
  3132. { Not old TP comment, so add...}
  3133. begin
  3134. if (i<255) then
  3135. begin
  3136. inc(i);
  3137. readcomment[i]:='*';
  3138. end;
  3139. end;
  3140. end;
  3141. #10,#13 :
  3142. linebreak;
  3143. #26 :
  3144. end_of_file;
  3145. else
  3146. begin
  3147. if (i<255) then
  3148. begin
  3149. inc(i);
  3150. readcomment[i]:=c;
  3151. end;
  3152. end;
  3153. end;
  3154. readchar;
  3155. until false;
  3156. readcomment[0]:=chr(i);
  3157. end;
  3158. function tscannerfile.readquotedstring:string;
  3159. var
  3160. i : longint;
  3161. msgwritten : boolean;
  3162. begin
  3163. i:=0;
  3164. msgwritten:=false;
  3165. if (c='''') then
  3166. begin
  3167. repeat
  3168. readchar;
  3169. case c of
  3170. #26 :
  3171. end_of_file;
  3172. #10,#13 :
  3173. Message(scan_f_string_exceeds_line);
  3174. '''' :
  3175. begin
  3176. readchar;
  3177. if c<>'''' then
  3178. break;
  3179. end;
  3180. end;
  3181. if i<255 then
  3182. begin
  3183. inc(i);
  3184. result[i]:=c;
  3185. end
  3186. else
  3187. begin
  3188. if not msgwritten then
  3189. begin
  3190. Message(scan_e_string_exceeds_255_chars);
  3191. msgwritten:=true;
  3192. end;
  3193. end;
  3194. until false;
  3195. end;
  3196. result[0]:=chr(i);
  3197. end;
  3198. function tscannerfile.readstate:char;
  3199. var
  3200. state : char;
  3201. begin
  3202. state:=' ';
  3203. if c=' ' then
  3204. begin
  3205. current_scanner.skipspace;
  3206. current_scanner.readid;
  3207. if pattern='ON' then
  3208. state:='+'
  3209. else
  3210. if pattern='OFF' then
  3211. state:='-';
  3212. end
  3213. else
  3214. state:=c;
  3215. if not (state in ['+','-']) then
  3216. Message(scan_e_wrong_switch_toggle);
  3217. readstate:=state;
  3218. end;
  3219. function tscannerfile.readstatedefault:char;
  3220. var
  3221. state : char;
  3222. begin
  3223. state:=' ';
  3224. if c=' ' then
  3225. begin
  3226. current_scanner.skipspace;
  3227. current_scanner.readid;
  3228. if pattern='ON' then
  3229. state:='+'
  3230. else
  3231. if pattern='OFF' then
  3232. state:='-'
  3233. else
  3234. if pattern='DEFAULT' then
  3235. state:='*';
  3236. end
  3237. else
  3238. state:=c;
  3239. if not (state in ['+','-','*']) then
  3240. Message(scan_e_wrong_switch_toggle_default);
  3241. readstatedefault:=state;
  3242. end;
  3243. procedure tscannerfile.skipspace;
  3244. begin
  3245. repeat
  3246. case c of
  3247. #26 :
  3248. begin
  3249. reload;
  3250. if (c=#26) and not assigned(inputfile.next) then
  3251. break;
  3252. continue;
  3253. end;
  3254. #10,
  3255. #13 :
  3256. linebreak;
  3257. #9,#11,#12,' ' :
  3258. ;
  3259. else
  3260. break;
  3261. end;
  3262. readchar;
  3263. until false;
  3264. end;
  3265. procedure tscannerfile.skipuntildirective;
  3266. var
  3267. found : longint;
  3268. next_char_loaded : boolean;
  3269. begin
  3270. found:=0;
  3271. next_char_loaded:=false;
  3272. repeat
  3273. case c of
  3274. #10,
  3275. #13 :
  3276. linebreak;
  3277. #26 :
  3278. begin
  3279. reload;
  3280. if (c=#26) and not assigned(inputfile.next) then
  3281. end_of_file;
  3282. continue;
  3283. end;
  3284. '{' :
  3285. begin
  3286. if (aktcommentstyle in [comment_tp,comment_none]) then
  3287. begin
  3288. aktcommentstyle:=comment_tp;
  3289. if (comment_level=0) then
  3290. found:=1;
  3291. inc_comment_level;
  3292. end;
  3293. end;
  3294. '*' :
  3295. begin
  3296. if (aktcommentstyle=comment_oldtp) then
  3297. begin
  3298. readchar;
  3299. if c=')' then
  3300. begin
  3301. dec_comment_level;
  3302. found:=0;
  3303. aktcommentstyle:=comment_none;
  3304. end
  3305. else
  3306. next_char_loaded:=true;
  3307. end
  3308. else
  3309. found := 0;
  3310. end;
  3311. '}' :
  3312. begin
  3313. if (aktcommentstyle=comment_tp) then
  3314. begin
  3315. dec_comment_level;
  3316. if (comment_level=0) then
  3317. aktcommentstyle:=comment_none;
  3318. found:=0;
  3319. end;
  3320. end;
  3321. '$' :
  3322. begin
  3323. if found=1 then
  3324. found:=2;
  3325. end;
  3326. '''' :
  3327. if (aktcommentstyle=comment_none) then
  3328. begin
  3329. repeat
  3330. readchar;
  3331. case c of
  3332. #26 :
  3333. end_of_file;
  3334. #10,#13 :
  3335. break;
  3336. '''' :
  3337. begin
  3338. readchar;
  3339. if c<>'''' then
  3340. begin
  3341. next_char_loaded:=true;
  3342. break;
  3343. end;
  3344. end;
  3345. end;
  3346. until false;
  3347. end;
  3348. '(' :
  3349. begin
  3350. if (aktcommentstyle=comment_none) then
  3351. begin
  3352. readchar;
  3353. if c='*' then
  3354. begin
  3355. readchar;
  3356. if c='$' then
  3357. begin
  3358. found:=2;
  3359. inc_comment_level;
  3360. aktcommentstyle:=comment_oldtp;
  3361. end
  3362. else
  3363. begin
  3364. skipoldtpcomment;
  3365. next_char_loaded:=true;
  3366. end;
  3367. end
  3368. else
  3369. next_char_loaded:=true;
  3370. end
  3371. else
  3372. found:=0;
  3373. end;
  3374. '/' :
  3375. begin
  3376. if (aktcommentstyle=comment_none) then
  3377. begin
  3378. readchar;
  3379. if c='/' then
  3380. skipdelphicomment;
  3381. next_char_loaded:=true;
  3382. end
  3383. else
  3384. found:=0;
  3385. end;
  3386. else
  3387. found:=0;
  3388. end;
  3389. if next_char_loaded then
  3390. next_char_loaded:=false
  3391. else
  3392. readchar;
  3393. until (found=2);
  3394. end;
  3395. {****************************************************************************
  3396. Comment Handling
  3397. ****************************************************************************}
  3398. procedure tscannerfile.skipcomment;
  3399. begin
  3400. aktcommentstyle:=comment_tp;
  3401. readchar;
  3402. inc_comment_level;
  3403. { handle compiler switches }
  3404. if (c='$') then
  3405. handledirectives;
  3406. { handle_switches can dec comment_level, }
  3407. while (comment_level>0) do
  3408. begin
  3409. case c of
  3410. '{' :
  3411. inc_comment_level;
  3412. '}' :
  3413. dec_comment_level;
  3414. #10,#13 :
  3415. linebreak;
  3416. #26 :
  3417. begin
  3418. reload;
  3419. if (c=#26) and not assigned(inputfile.next) then
  3420. end_of_file;
  3421. continue;
  3422. end;
  3423. end;
  3424. readchar;
  3425. end;
  3426. aktcommentstyle:=comment_none;
  3427. end;
  3428. procedure tscannerfile.skipdelphicomment;
  3429. begin
  3430. aktcommentstyle:=comment_delphi;
  3431. inc_comment_level;
  3432. readchar;
  3433. { this is not supported }
  3434. if c='$' then
  3435. Message(scan_w_wrong_styled_switch);
  3436. { skip comment }
  3437. while not (c in [#10,#13,#26]) do
  3438. readchar;
  3439. dec_comment_level;
  3440. aktcommentstyle:=comment_none;
  3441. end;
  3442. procedure tscannerfile.skipoldtpcomment;
  3443. var
  3444. found : longint;
  3445. begin
  3446. aktcommentstyle:=comment_oldtp;
  3447. inc_comment_level;
  3448. { only load a char if last already processed,
  3449. was cause of bug1634 PM }
  3450. if c=#0 then
  3451. readchar;
  3452. { this is now supported }
  3453. if (c='$') then
  3454. handledirectives;
  3455. { skip comment }
  3456. while (comment_level>0) do
  3457. begin
  3458. found:=0;
  3459. repeat
  3460. case c of
  3461. #26 :
  3462. begin
  3463. reload;
  3464. if (c=#26) and not assigned(inputfile.next) then
  3465. end_of_file;
  3466. continue;
  3467. end;
  3468. #10,#13 :
  3469. begin
  3470. if found=4 then
  3471. inc_comment_level;
  3472. linebreak;
  3473. found:=0;
  3474. end;
  3475. '*' :
  3476. begin
  3477. if found=3 then
  3478. found:=4
  3479. else
  3480. found:=1;
  3481. end;
  3482. ')' :
  3483. begin
  3484. if found in [1,4] then
  3485. begin
  3486. dec_comment_level;
  3487. if comment_level=0 then
  3488. found:=2
  3489. else
  3490. found:=0;
  3491. end
  3492. else
  3493. found:=0;
  3494. end;
  3495. '(' :
  3496. begin
  3497. if found=4 then
  3498. inc_comment_level;
  3499. found:=3;
  3500. end;
  3501. else
  3502. begin
  3503. if found=4 then
  3504. inc_comment_level;
  3505. found:=0;
  3506. end;
  3507. end;
  3508. readchar;
  3509. until (found=2);
  3510. end;
  3511. aktcommentstyle:=comment_none;
  3512. end;
  3513. {****************************************************************************
  3514. Token Scanner
  3515. ****************************************************************************}
  3516. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  3517. var
  3518. code : integer;
  3519. len,
  3520. low,high,mid : longint;
  3521. w : word;
  3522. m : longint;
  3523. mac : tmacro;
  3524. asciinr : string[33];
  3525. iswidestring : boolean;
  3526. label
  3527. exit_label;
  3528. begin
  3529. flushpendingswitchesstate;
  3530. { record tokens? }
  3531. if allowrecordtoken and
  3532. assigned(recordtokenbuf) then
  3533. recordtoken;
  3534. { replay tokens? }
  3535. if assigned(replaytokenbuf) then
  3536. begin
  3537. replaytoken;
  3538. goto exit_label;
  3539. end;
  3540. { was there already a token read, then return that token }
  3541. if nexttoken<>NOTOKEN then
  3542. begin
  3543. setnexttoken;
  3544. goto exit_label;
  3545. end;
  3546. { Skip all spaces and comments }
  3547. repeat
  3548. case c of
  3549. '{' :
  3550. skipcomment;
  3551. #26 :
  3552. begin
  3553. reload;
  3554. if (c=#26) and not assigned(inputfile.next) then
  3555. break;
  3556. end;
  3557. ' ',#9..#13 :
  3558. begin
  3559. {$ifdef PREPROCWRITE}
  3560. if parapreprocess then
  3561. begin
  3562. if c=#10 then
  3563. preprocfile.eolfound:=true
  3564. else
  3565. preprocfile.spacefound:=true;
  3566. end;
  3567. {$endif PREPROCWRITE}
  3568. skipspace;
  3569. end
  3570. else
  3571. break;
  3572. end;
  3573. until false;
  3574. { Save current token position, for EOF its already loaded }
  3575. if c<>#26 then
  3576. gettokenpos;
  3577. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  3578. if c in ['A'..'Z','a'..'z','_'] then
  3579. begin
  3580. readstring;
  3581. token:=_ID;
  3582. idtoken:=_ID;
  3583. { keyword or any other known token,
  3584. pattern is always uppercased }
  3585. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  3586. begin
  3587. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  3588. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  3589. while low<high do
  3590. begin
  3591. mid:=(high+low+1) shr 1;
  3592. if pattern<tokeninfo^[ttoken(mid)].str then
  3593. high:=mid-1
  3594. else
  3595. low:=mid;
  3596. end;
  3597. with tokeninfo^[ttoken(high)] do
  3598. if pattern=str then
  3599. begin
  3600. if keyword in current_settings.modeswitches then
  3601. if op=NOTOKEN then
  3602. token:=ttoken(high)
  3603. else
  3604. token:=op;
  3605. idtoken:=ttoken(high);
  3606. end;
  3607. end;
  3608. { Only process identifiers and not keywords }
  3609. if token=_ID then
  3610. begin
  3611. { this takes some time ... }
  3612. if (cs_support_macro in current_settings.moduleswitches) then
  3613. begin
  3614. mac:=tmacro(search_macro(pattern));
  3615. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  3616. begin
  3617. if yylexcount<max_macro_nesting then
  3618. begin
  3619. mac.is_used:=true;
  3620. inc(yylexcount);
  3621. substitutemacro(pattern,mac.buftext,mac.buflen,
  3622. mac.fileinfo.line,mac.fileinfo.fileindex);
  3623. { handle empty macros }
  3624. if c=#0 then
  3625. reload;
  3626. readtoken(false);
  3627. { that's all folks }
  3628. dec(yylexcount);
  3629. exit;
  3630. end
  3631. else
  3632. Message(scan_w_macro_too_deep);
  3633. end;
  3634. end;
  3635. end;
  3636. { return token }
  3637. goto exit_label;
  3638. end
  3639. else
  3640. begin
  3641. idtoken:=_NOID;
  3642. case c of
  3643. '$' :
  3644. begin
  3645. readnumber;
  3646. token:=_INTCONST;
  3647. goto exit_label;
  3648. end;
  3649. '%' :
  3650. begin
  3651. if not(m_fpc in current_settings.modeswitches) then
  3652. Illegal_Char(c)
  3653. else
  3654. begin
  3655. readnumber;
  3656. token:=_INTCONST;
  3657. goto exit_label;
  3658. end;
  3659. end;
  3660. '&' :
  3661. begin
  3662. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  3663. begin
  3664. readnumber;
  3665. if length(pattern)=1 then
  3666. begin
  3667. readstring;
  3668. token:=_ID;
  3669. idtoken:=_ID;
  3670. end
  3671. else
  3672. token:=_INTCONST;
  3673. goto exit_label;
  3674. end
  3675. else if m_mac in current_settings.modeswitches then
  3676. begin
  3677. readchar;
  3678. token:=_AMPERSAND;
  3679. goto exit_label;
  3680. end
  3681. else
  3682. Illegal_Char(c);
  3683. end;
  3684. '0'..'9' :
  3685. begin
  3686. readnumber;
  3687. if (c in ['.','e','E']) then
  3688. begin
  3689. { first check for a . }
  3690. if c='.' then
  3691. begin
  3692. cachenexttokenpos;
  3693. readchar;
  3694. { is it a .. from a range? }
  3695. case c of
  3696. '.' :
  3697. begin
  3698. readchar;
  3699. token:=_INTCONST;
  3700. nexttoken:=_POINTPOINT;
  3701. goto exit_label;
  3702. end;
  3703. ')' :
  3704. begin
  3705. readchar;
  3706. token:=_INTCONST;
  3707. nexttoken:=_RECKKLAMMER;
  3708. goto exit_label;
  3709. end;
  3710. end;
  3711. { insert the number after the . }
  3712. pattern:=pattern+'.';
  3713. while c in ['0'..'9'] do
  3714. begin
  3715. pattern:=pattern+c;
  3716. readchar;
  3717. end;
  3718. end;
  3719. { E can also follow after a point is scanned }
  3720. if c in ['e','E'] then
  3721. begin
  3722. pattern:=pattern+'E';
  3723. readchar;
  3724. if c in ['-','+'] then
  3725. begin
  3726. pattern:=pattern+c;
  3727. readchar;
  3728. end;
  3729. if not(c in ['0'..'9']) then
  3730. Illegal_Char(c);
  3731. while c in ['0'..'9'] do
  3732. begin
  3733. pattern:=pattern+c;
  3734. readchar;
  3735. end;
  3736. end;
  3737. token:=_REALNUMBER;
  3738. goto exit_label;
  3739. end;
  3740. token:=_INTCONST;
  3741. goto exit_label;
  3742. end;
  3743. ';' :
  3744. begin
  3745. readchar;
  3746. token:=_SEMICOLON;
  3747. goto exit_label;
  3748. end;
  3749. '[' :
  3750. begin
  3751. readchar;
  3752. token:=_LECKKLAMMER;
  3753. goto exit_label;
  3754. end;
  3755. ']' :
  3756. begin
  3757. readchar;
  3758. token:=_RECKKLAMMER;
  3759. goto exit_label;
  3760. end;
  3761. '(' :
  3762. begin
  3763. readchar;
  3764. case c of
  3765. '*' :
  3766. begin
  3767. c:=#0;{Signal skipoldtpcomment to reload a char }
  3768. skipoldtpcomment;
  3769. readtoken(false);
  3770. exit;
  3771. end;
  3772. '.' :
  3773. begin
  3774. readchar;
  3775. token:=_LECKKLAMMER;
  3776. goto exit_label;
  3777. end;
  3778. end;
  3779. token:=_LKLAMMER;
  3780. goto exit_label;
  3781. end;
  3782. ')' :
  3783. begin
  3784. readchar;
  3785. token:=_RKLAMMER;
  3786. goto exit_label;
  3787. end;
  3788. '+' :
  3789. begin
  3790. readchar;
  3791. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3792. begin
  3793. readchar;
  3794. token:=_PLUSASN;
  3795. goto exit_label;
  3796. end;
  3797. token:=_PLUS;
  3798. goto exit_label;
  3799. end;
  3800. '-' :
  3801. begin
  3802. readchar;
  3803. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3804. begin
  3805. readchar;
  3806. token:=_MINUSASN;
  3807. goto exit_label;
  3808. end;
  3809. token:=_MINUS;
  3810. goto exit_label;
  3811. end;
  3812. ':' :
  3813. begin
  3814. readchar;
  3815. if c='=' then
  3816. begin
  3817. readchar;
  3818. token:=_ASSIGNMENT;
  3819. goto exit_label;
  3820. end;
  3821. token:=_COLON;
  3822. goto exit_label;
  3823. end;
  3824. '*' :
  3825. begin
  3826. readchar;
  3827. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3828. begin
  3829. readchar;
  3830. token:=_STARASN;
  3831. end
  3832. else
  3833. if c='*' then
  3834. begin
  3835. readchar;
  3836. token:=_STARSTAR;
  3837. end
  3838. else
  3839. token:=_STAR;
  3840. goto exit_label;
  3841. end;
  3842. '/' :
  3843. begin
  3844. readchar;
  3845. case c of
  3846. '=' :
  3847. begin
  3848. if (cs_support_c_operators in current_settings.moduleswitches) then
  3849. begin
  3850. readchar;
  3851. token:=_SLASHASN;
  3852. goto exit_label;
  3853. end;
  3854. end;
  3855. '/' :
  3856. begin
  3857. skipdelphicomment;
  3858. readtoken(false);
  3859. exit;
  3860. end;
  3861. end;
  3862. token:=_SLASH;
  3863. goto exit_label;
  3864. end;
  3865. '|' :
  3866. if m_mac in current_settings.modeswitches then
  3867. begin
  3868. readchar;
  3869. token:=_PIPE;
  3870. goto exit_label;
  3871. end
  3872. else
  3873. Illegal_Char(c);
  3874. '=' :
  3875. begin
  3876. readchar;
  3877. token:=_EQ;
  3878. goto exit_label;
  3879. end;
  3880. '.' :
  3881. begin
  3882. readchar;
  3883. case c of
  3884. '.' :
  3885. begin
  3886. readchar;
  3887. case c of
  3888. '.' :
  3889. begin
  3890. readchar;
  3891. token:=_POINTPOINTPOINT;
  3892. goto exit_label;
  3893. end;
  3894. else
  3895. begin
  3896. token:=_POINTPOINT;
  3897. goto exit_label;
  3898. end;
  3899. end;
  3900. end;
  3901. ')' :
  3902. begin
  3903. readchar;
  3904. token:=_RECKKLAMMER;
  3905. goto exit_label;
  3906. end;
  3907. end;
  3908. token:=_POINT;
  3909. goto exit_label;
  3910. end;
  3911. '@' :
  3912. begin
  3913. readchar;
  3914. token:=_KLAMMERAFFE;
  3915. goto exit_label;
  3916. end;
  3917. ',' :
  3918. begin
  3919. readchar;
  3920. token:=_COMMA;
  3921. goto exit_label;
  3922. end;
  3923. '''','#','^' :
  3924. begin
  3925. len:=0;
  3926. cstringpattern:='';
  3927. iswidestring:=false;
  3928. if c='^' then
  3929. begin
  3930. readchar;
  3931. c:=upcase(c);
  3932. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  3933. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  3934. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  3935. begin
  3936. token:=_CARET;
  3937. goto exit_label;
  3938. end
  3939. else
  3940. begin
  3941. inc(len);
  3942. setlength(cstringpattern,256);
  3943. if c<#64 then
  3944. cstringpattern[len]:=chr(ord(c)+64)
  3945. else
  3946. cstringpattern[len]:=chr(ord(c)-64);
  3947. readchar;
  3948. end;
  3949. end;
  3950. repeat
  3951. case c of
  3952. '#' :
  3953. begin
  3954. readchar; { read # }
  3955. case c of
  3956. '$':
  3957. begin
  3958. readchar; { read leading $ }
  3959. asciinr:='$';
  3960. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=5) do
  3961. begin
  3962. asciinr:=asciinr+c;
  3963. readchar;
  3964. end;
  3965. end;
  3966. '&':
  3967. begin
  3968. readchar; { read leading $ }
  3969. asciinr:='&';
  3970. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=7) do
  3971. begin
  3972. asciinr:=asciinr+c;
  3973. readchar;
  3974. end;
  3975. end;
  3976. '%':
  3977. begin
  3978. readchar; { read leading $ }
  3979. asciinr:='%';
  3980. while (upcase(c) in ['0','1']) and (length(asciinr)<=17) do
  3981. begin
  3982. asciinr:=asciinr+c;
  3983. readchar;
  3984. end;
  3985. end;
  3986. else
  3987. begin
  3988. asciinr:='';
  3989. while (c in ['0'..'9']) and (length(asciinr)<=5) do
  3990. begin
  3991. asciinr:=asciinr+c;
  3992. readchar;
  3993. end;
  3994. end;
  3995. end;
  3996. val(asciinr,m,code);
  3997. if (asciinr='') or (code<>0) then
  3998. Message(scan_e_illegal_char_const)
  3999. else if (m<0) or (m>255) or (length(asciinr)>3) then
  4000. begin
  4001. if (m>=0) and (m<=65535) then
  4002. begin
  4003. if not iswidestring then
  4004. begin
  4005. if len>0 then
  4006. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4007. else
  4008. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4009. iswidestring:=true;
  4010. len:=0;
  4011. end;
  4012. concatwidestringchar(patternw,tcompilerwidechar(m));
  4013. end
  4014. else
  4015. Message(scan_e_illegal_char_const)
  4016. end
  4017. else if iswidestring then
  4018. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  4019. else
  4020. begin
  4021. if len>=length(cstringpattern) then
  4022. setlength(cstringpattern,length(cstringpattern)+256);
  4023. inc(len);
  4024. cstringpattern[len]:=chr(m);
  4025. end;
  4026. end;
  4027. '''' :
  4028. begin
  4029. repeat
  4030. readchar;
  4031. case c of
  4032. #26 :
  4033. end_of_file;
  4034. #10,#13 :
  4035. Message(scan_f_string_exceeds_line);
  4036. '''' :
  4037. begin
  4038. readchar;
  4039. if c<>'''' then
  4040. break;
  4041. end;
  4042. end;
  4043. { interpret as utf-8 string? }
  4044. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  4045. begin
  4046. { convert existing string to an utf-8 string }
  4047. if not iswidestring then
  4048. begin
  4049. if len>0 then
  4050. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4051. else
  4052. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4053. iswidestring:=true;
  4054. len:=0;
  4055. end;
  4056. { four or more chars aren't handled }
  4057. if (ord(c) and $f0)=$f0 then
  4058. message(scan_e_utf8_bigger_than_65535)
  4059. { three chars }
  4060. else if (ord(c) and $e0)=$e0 then
  4061. begin
  4062. w:=ord(c) and $f;
  4063. readchar;
  4064. if (ord(c) and $c0)<>$80 then
  4065. message(scan_e_utf8_malformed);
  4066. w:=(w shl 6) or (ord(c) and $3f);
  4067. readchar;
  4068. if (ord(c) and $c0)<>$80 then
  4069. message(scan_e_utf8_malformed);
  4070. w:=(w shl 6) or (ord(c) and $3f);
  4071. concatwidestringchar(patternw,w);
  4072. end
  4073. { two chars }
  4074. else if (ord(c) and $c0)<>0 then
  4075. begin
  4076. w:=ord(c) and $1f;
  4077. readchar;
  4078. if (ord(c) and $c0)<>$80 then
  4079. message(scan_e_utf8_malformed);
  4080. w:=(w shl 6) or (ord(c) and $3f);
  4081. concatwidestringchar(patternw,w);
  4082. end
  4083. { illegal }
  4084. else if (ord(c) and $80)<>0 then
  4085. message(scan_e_utf8_malformed)
  4086. else
  4087. concatwidestringchar(patternw,tcompilerwidechar(c))
  4088. end
  4089. else if iswidestring then
  4090. begin
  4091. if current_settings.sourcecodepage=CP_UTF8 then
  4092. concatwidestringchar(patternw,ord(c))
  4093. else
  4094. concatwidestringchar(patternw,asciichar2unicode(c))
  4095. end
  4096. else
  4097. begin
  4098. if len>=length(cstringpattern) then
  4099. setlength(cstringpattern,length(cstringpattern)+256);
  4100. inc(len);
  4101. cstringpattern[len]:=c;
  4102. end;
  4103. until false;
  4104. end;
  4105. '^' :
  4106. begin
  4107. readchar;
  4108. c:=upcase(c);
  4109. if c<#64 then
  4110. c:=chr(ord(c)+64)
  4111. else
  4112. c:=chr(ord(c)-64);
  4113. if iswidestring then
  4114. concatwidestringchar(patternw,asciichar2unicode(c))
  4115. else
  4116. begin
  4117. if len>=length(cstringpattern) then
  4118. setlength(cstringpattern,length(cstringpattern)+256);
  4119. inc(len);
  4120. cstringpattern[len]:=c;
  4121. end;
  4122. readchar;
  4123. end;
  4124. else
  4125. break;
  4126. end;
  4127. until false;
  4128. { strings with length 1 become const chars }
  4129. if iswidestring then
  4130. begin
  4131. if patternw^.len=1 then
  4132. token:=_CWCHAR
  4133. else
  4134. token:=_CWSTRING;
  4135. end
  4136. else
  4137. begin
  4138. setlength(cstringpattern,len);
  4139. if length(cstringpattern)=1 then
  4140. begin
  4141. token:=_CCHAR;
  4142. pattern:=cstringpattern;
  4143. end
  4144. else
  4145. token:=_CSTRING;
  4146. end;
  4147. goto exit_label;
  4148. end;
  4149. '>' :
  4150. begin
  4151. readchar;
  4152. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4153. token:=_RSHARPBRACKET
  4154. else
  4155. begin
  4156. case c of
  4157. '=' :
  4158. begin
  4159. readchar;
  4160. token:=_GTE;
  4161. goto exit_label;
  4162. end;
  4163. '>' :
  4164. begin
  4165. readchar;
  4166. token:=_OP_SHR;
  4167. goto exit_label;
  4168. end;
  4169. '<' :
  4170. begin { >< is for a symetric diff for sets }
  4171. readchar;
  4172. token:=_SYMDIF;
  4173. goto exit_label;
  4174. end;
  4175. end;
  4176. token:=_GT;
  4177. end;
  4178. goto exit_label;
  4179. end;
  4180. '<' :
  4181. begin
  4182. readchar;
  4183. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4184. token:=_LSHARPBRACKET
  4185. else
  4186. begin
  4187. case c of
  4188. '>' :
  4189. begin
  4190. readchar;
  4191. token:=_NE;
  4192. goto exit_label;
  4193. end;
  4194. '=' :
  4195. begin
  4196. readchar;
  4197. token:=_LTE;
  4198. goto exit_label;
  4199. end;
  4200. '<' :
  4201. begin
  4202. readchar;
  4203. token:=_OP_SHL;
  4204. goto exit_label;
  4205. end;
  4206. end;
  4207. token:=_LT;
  4208. end;
  4209. goto exit_label;
  4210. end;
  4211. #26 :
  4212. begin
  4213. token:=_EOF;
  4214. checkpreprocstack;
  4215. goto exit_label;
  4216. end;
  4217. else
  4218. Illegal_Char(c);
  4219. end;
  4220. end;
  4221. exit_label:
  4222. lasttoken:=token;
  4223. end;
  4224. function tscannerfile.readpreproc:ttoken;
  4225. begin
  4226. skipspace;
  4227. case c of
  4228. '_',
  4229. 'A'..'Z',
  4230. 'a'..'z' :
  4231. begin
  4232. current_scanner.preproc_pattern:=readid;
  4233. readpreproc:=_ID;
  4234. end;
  4235. '0'..'9' :
  4236. begin
  4237. current_scanner.preproc_pattern:=readval_asstring;
  4238. { realnumber? }
  4239. if c='.' then
  4240. begin
  4241. readchar;
  4242. while c in ['0'..'9'] do
  4243. begin
  4244. current_scanner.preproc_pattern:=current_scanner.preproc_pattern+c;
  4245. readchar;
  4246. end;
  4247. end;
  4248. readpreproc:=_ID;
  4249. end;
  4250. '$','%','&' :
  4251. begin
  4252. current_scanner.preproc_pattern:=readval_asstring;
  4253. readpreproc:=_ID;
  4254. end;
  4255. ',' :
  4256. begin
  4257. readchar;
  4258. readpreproc:=_COMMA;
  4259. end;
  4260. '}' :
  4261. begin
  4262. readpreproc:=_END;
  4263. end;
  4264. '(' :
  4265. begin
  4266. readchar;
  4267. readpreproc:=_LKLAMMER;
  4268. end;
  4269. ')' :
  4270. begin
  4271. readchar;
  4272. readpreproc:=_RKLAMMER;
  4273. end;
  4274. '[' :
  4275. begin
  4276. readchar;
  4277. readpreproc:=_LECKKLAMMER;
  4278. end;
  4279. ']' :
  4280. begin
  4281. readchar;
  4282. readpreproc:=_RECKKLAMMER;
  4283. end;
  4284. '+' :
  4285. begin
  4286. readchar;
  4287. readpreproc:=_PLUS;
  4288. end;
  4289. '-' :
  4290. begin
  4291. readchar;
  4292. readpreproc:=_MINUS;
  4293. end;
  4294. '*' :
  4295. begin
  4296. readchar;
  4297. readpreproc:=_STAR;
  4298. end;
  4299. '/' :
  4300. begin
  4301. readchar;
  4302. readpreproc:=_SLASH;
  4303. end;
  4304. '=' :
  4305. begin
  4306. readchar;
  4307. readpreproc:=_EQ;
  4308. end;
  4309. '>' :
  4310. begin
  4311. readchar;
  4312. if c='=' then
  4313. begin
  4314. readchar;
  4315. readpreproc:=_GTE;
  4316. end
  4317. else
  4318. readpreproc:=_GT;
  4319. end;
  4320. '<' :
  4321. begin
  4322. readchar;
  4323. case c of
  4324. '>' :
  4325. begin
  4326. readchar;
  4327. readpreproc:=_NE;
  4328. end;
  4329. '=' :
  4330. begin
  4331. readchar;
  4332. readpreproc:=_LTE;
  4333. end;
  4334. else
  4335. readpreproc:=_LT;
  4336. end;
  4337. end;
  4338. #26 :
  4339. begin
  4340. readpreproc:=_EOF;
  4341. checkpreprocstack;
  4342. end;
  4343. else
  4344. Illegal_Char(c);
  4345. end;
  4346. end;
  4347. function tscannerfile.asmgetcharstart : char;
  4348. begin
  4349. { return first the character already
  4350. available in c }
  4351. lastasmgetchar:=c;
  4352. result:=asmgetchar;
  4353. end;
  4354. function tscannerfile.asmgetchar : char;
  4355. begin
  4356. if lastasmgetchar<>#0 then
  4357. begin
  4358. c:=lastasmgetchar;
  4359. lastasmgetchar:=#0;
  4360. end
  4361. else
  4362. readchar;
  4363. if in_asm_string then
  4364. begin
  4365. asmgetchar:=c;
  4366. exit;
  4367. end;
  4368. repeat
  4369. case c of
  4370. // the { ... } is used in ARM assembler to define register sets, so we can't used
  4371. // it as comment, either (* ... *), /* ... */ or // ... should be used instead.
  4372. // But compiler directives {$...} are allowed in ARM assembler.
  4373. '{' :
  4374. begin
  4375. {$ifdef arm}
  4376. readchar;
  4377. dec(inputpointer);
  4378. if c<>'$' then
  4379. begin
  4380. asmgetchar:='{';
  4381. exit;
  4382. end
  4383. else
  4384. {$endif arm}
  4385. skipcomment;
  4386. end;
  4387. #10,#13 :
  4388. begin
  4389. linebreak;
  4390. asmgetchar:=c;
  4391. exit;
  4392. end;
  4393. #26 :
  4394. begin
  4395. reload;
  4396. if (c=#26) and not assigned(inputfile.next) then
  4397. end_of_file;
  4398. continue;
  4399. end;
  4400. '/' :
  4401. begin
  4402. readchar;
  4403. if c='/' then
  4404. skipdelphicomment
  4405. else
  4406. begin
  4407. asmgetchar:='/';
  4408. lastasmgetchar:=c;
  4409. exit;
  4410. end;
  4411. end;
  4412. '(' :
  4413. begin
  4414. readchar;
  4415. if c='*' then
  4416. begin
  4417. c:=#0;{Signal skipoldtpcomment to reload a char }
  4418. skipoldtpcomment;
  4419. end
  4420. else
  4421. begin
  4422. asmgetchar:='(';
  4423. lastasmgetchar:=c;
  4424. exit;
  4425. end;
  4426. end;
  4427. else
  4428. begin
  4429. asmgetchar:=c;
  4430. exit;
  4431. end;
  4432. end;
  4433. until false;
  4434. end;
  4435. {*****************************************************************************
  4436. Helpers
  4437. *****************************************************************************}
  4438. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  4439. begin
  4440. if dm in [directive_all, directive_turbo] then
  4441. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  4442. if dm in [directive_all, directive_mac] then
  4443. tdirectiveitem.create(mac_scannerdirectives,s,p);
  4444. end;
  4445. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  4446. begin
  4447. if dm in [directive_all, directive_turbo] then
  4448. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  4449. if dm in [directive_all, directive_mac] then
  4450. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  4451. end;
  4452. {*****************************************************************************
  4453. Initialization
  4454. *****************************************************************************}
  4455. procedure InitScanner;
  4456. begin
  4457. InitWideString(patternw);
  4458. turbo_scannerdirectives:=TFPHashObjectList.Create;
  4459. mac_scannerdirectives:=TFPHashObjectList.Create;
  4460. { Common directives and conditionals }
  4461. AddDirective('I',directive_all, @dir_include);
  4462. AddDirective('DEFINE',directive_all, @dir_define);
  4463. AddDirective('UNDEF',directive_all, @dir_undef);
  4464. AddConditional('IF',directive_all, @dir_if);
  4465. AddConditional('IFDEF',directive_all, @dir_ifdef);
  4466. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  4467. AddConditional('ELSE',directive_all, @dir_else);
  4468. AddConditional('ELSEIF',directive_all, @dir_elseif);
  4469. AddConditional('ENDIF',directive_all, @dir_endif);
  4470. { Directives and conditionals for all modes except mode macpas}
  4471. AddDirective('INCLUDE',directive_turbo, @dir_include);
  4472. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  4473. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  4474. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  4475. AddConditional('IFEND',directive_turbo, @dir_endif);
  4476. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  4477. { Directives and conditionals for mode macpas: }
  4478. AddDirective('SETC',directive_mac, @dir_setc);
  4479. AddDirective('DEFINEC',directive_mac, @dir_definec);
  4480. AddDirective('UNDEFC',directive_mac, @dir_undef);
  4481. AddConditional('IFC',directive_mac, @dir_if);
  4482. AddConditional('ELSEC',directive_mac, @dir_else);
  4483. AddConditional('ELIFC',directive_mac, @dir_elseif);
  4484. AddConditional('ENDC',directive_mac, @dir_endif);
  4485. end;
  4486. procedure DoneScanner;
  4487. begin
  4488. turbo_scannerdirectives.Free;
  4489. mac_scannerdirectives.Free;
  4490. DoneWideString(patternw);
  4491. end;
  4492. end.