scanner.pas 161 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898
  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. { don't generate invalid line info if no sources are available for the current module }
  2434. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2435. current_tokenpos.column:=0;
  2436. current_filepos:=current_tokenpos;
  2437. end
  2438. else
  2439. case specialtoken of
  2440. ST_LOADSETTINGS:
  2441. begin
  2442. copy_size:=tokenreadsizeint;
  2443. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  2444. // internalerror(2011090501);
  2445. {
  2446. replaytokenbuf.read(current_settings,copy_size);
  2447. }
  2448. tokenreadsettings(current_settings,copy_size);
  2449. end;
  2450. ST_LOADMESSAGES:
  2451. begin
  2452. current_settings.pmessage:=nil;
  2453. mesgnb:=tokenreadsizeint;
  2454. if mesgnb>0 then
  2455. Comment(V_Error,'Message recordind not yet supported');
  2456. for i:=1 to mesgnb do
  2457. begin
  2458. new(pmsg);
  2459. if i=1 then
  2460. begin
  2461. current_settings.pmessage:=pmsg;
  2462. prevmsg:=nil;
  2463. end
  2464. else
  2465. prevmsg^.next:=pmsg;
  2466. pmsg^.value:=tokenreadlongint;
  2467. pmsg^.state:=tmsgstate(tokenreadlongint);
  2468. pmsg^.next:=nil;
  2469. prevmsg:=pmsg;
  2470. end;
  2471. end;
  2472. ST_LINE:
  2473. begin
  2474. current_tokenpos.line:=tokenreadlongint;
  2475. { don't generate invalid line info if no sources are available for the current module }
  2476. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2477. current_tokenpos.line:=0;
  2478. current_filepos:=current_tokenpos;
  2479. end;
  2480. ST_COLUMN:
  2481. begin
  2482. current_tokenpos.column:=tokenreadword;
  2483. { don't generate invalid line info if no sources are available for the current module }
  2484. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2485. current_tokenpos.column:=0;
  2486. current_filepos:=current_tokenpos;
  2487. end;
  2488. ST_FILEINDEX:
  2489. begin
  2490. current_tokenpos.fileindex:=tokenreadword;
  2491. { don't generate invalid line info if no sources are available for the current module }
  2492. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2493. begin
  2494. current_tokenpos.column:=0;
  2495. current_tokenpos.line:=0;
  2496. end;
  2497. current_filepos:=current_tokenpos;
  2498. end;
  2499. else
  2500. internalerror(2006103010);
  2501. end;
  2502. continue;
  2503. end;
  2504. end;
  2505. break;
  2506. until false;
  2507. end;
  2508. procedure tscannerfile.addfile(hp:tinputfile);
  2509. begin
  2510. saveinputfile;
  2511. { add to list }
  2512. hp.next:=inputfile;
  2513. inputfile:=hp;
  2514. { load new inputfile }
  2515. restoreinputfile;
  2516. end;
  2517. procedure tscannerfile.reload;
  2518. begin
  2519. with inputfile do
  2520. begin
  2521. { when nothing more to read then leave immediatly, so we
  2522. don't change the current_filepos and leave it point to the last
  2523. char }
  2524. if (c=#26) and (not assigned(next)) then
  2525. exit;
  2526. repeat
  2527. { still more to read?, then change the #0 to a space so its seen
  2528. as a seperator, this can't be used for macro's which can change
  2529. the place of the #0 in the buffer with tempopen }
  2530. if (c=#0) and (bufsize>0) and
  2531. not(inputfile.is_macro) and
  2532. (inputpointer-inputbuffer<bufsize) then
  2533. begin
  2534. c:=' ';
  2535. inc(inputpointer);
  2536. exit;
  2537. end;
  2538. { can we read more from this file ? }
  2539. if (c<>#26) and (not endoffile) then
  2540. begin
  2541. readbuf;
  2542. inputpointer:=buf;
  2543. inputbuffer:=buf;
  2544. inputstart:=bufstart;
  2545. { first line? }
  2546. if line_no=0 then
  2547. begin
  2548. c:=inputpointer^;
  2549. { eat utf-8 signature? }
  2550. if (ord(inputpointer^)=$ef) and
  2551. (ord((inputpointer+1)^)=$bb) and
  2552. (ord((inputpointer+2)^)=$bf) then
  2553. begin
  2554. (* we don't support including files with an UTF-8 bom
  2555. inside another file that wasn't encoded as UTF-8
  2556. already (we don't support {$codepage xxx} switches in
  2557. the middle of a file either) *)
  2558. if (current_settings.sourcecodepage<>CP_UTF8) and
  2559. not current_module.in_global then
  2560. Message(scanner_f_illegal_utf8_bom);
  2561. inc(inputpointer,3);
  2562. message(scan_c_switching_to_utf8);
  2563. current_settings.sourcecodepage:=CP_UTF8;
  2564. include(current_settings.moduleswitches,cs_explicit_codepage);
  2565. end;
  2566. line_no:=1;
  2567. if cs_asm_source in current_settings.globalswitches then
  2568. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  2569. end;
  2570. end
  2571. else
  2572. begin
  2573. { load eof position in tokenpos/current_filepos }
  2574. gettokenpos;
  2575. { close file }
  2576. closeinputfile;
  2577. { no next module, than EOF }
  2578. if not assigned(inputfile.next) then
  2579. begin
  2580. c:=#26;
  2581. exit;
  2582. end;
  2583. { load next file and reopen it }
  2584. nextfile;
  2585. tempopeninputfile;
  2586. { status }
  2587. Message1(scan_t_back_in,inputfile.name);
  2588. end;
  2589. { load next char }
  2590. c:=inputpointer^;
  2591. inc(inputpointer);
  2592. until c<>#0; { if also end, then reload again }
  2593. end;
  2594. end;
  2595. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  2596. var
  2597. hp : tinputfile;
  2598. begin
  2599. { save old postion }
  2600. dec(inputpointer);
  2601. tempcloseinputfile;
  2602. { create macro 'file' }
  2603. { use special name to dispose after !! }
  2604. hp:=do_openinputfile('_Macro_.'+macname);
  2605. addfile(hp);
  2606. with inputfile do
  2607. begin
  2608. setmacro(p,len);
  2609. { local buffer }
  2610. inputbuffer:=buf;
  2611. inputpointer:=buf;
  2612. inputstart:=bufstart;
  2613. ref_index:=fileindex;
  2614. end;
  2615. { reset line }
  2616. line_no:=line;
  2617. lastlinepos:=0;
  2618. lasttokenpos:=0;
  2619. nexttokenpos:=0;
  2620. { load new c }
  2621. c:=inputpointer^;
  2622. inc(inputpointer);
  2623. end;
  2624. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  2625. begin
  2626. tokenpos:=inputstart+(inputpointer-inputbuffer);
  2627. filepos.line:=line_no;
  2628. filepos.column:=tokenpos-lastlinepos;
  2629. filepos.fileindex:=inputfile.ref_index;
  2630. filepos.moduleindex:=current_module.unit_index;
  2631. end;
  2632. procedure tscannerfile.gettokenpos;
  2633. { load the values of tokenpos and lasttokenpos }
  2634. begin
  2635. do_gettokenpos(lasttokenpos,current_tokenpos);
  2636. current_filepos:=current_tokenpos;
  2637. end;
  2638. procedure tscannerfile.cachenexttokenpos;
  2639. begin
  2640. do_gettokenpos(nexttokenpos,next_filepos);
  2641. end;
  2642. procedure tscannerfile.setnexttoken;
  2643. begin
  2644. token:=nexttoken;
  2645. nexttoken:=NOTOKEN;
  2646. lasttokenpos:=nexttokenpos;
  2647. current_tokenpos:=next_filepos;
  2648. current_filepos:=current_tokenpos;
  2649. nexttokenpos:=0;
  2650. end;
  2651. procedure tscannerfile.savetokenpos;
  2652. begin
  2653. oldlasttokenpos:=lasttokenpos;
  2654. oldcurrent_filepos:=current_filepos;
  2655. oldcurrent_tokenpos:=current_tokenpos;
  2656. end;
  2657. procedure tscannerfile.restoretokenpos;
  2658. begin
  2659. lasttokenpos:=oldlasttokenpos;
  2660. current_filepos:=oldcurrent_filepos;
  2661. current_tokenpos:=oldcurrent_tokenpos;
  2662. end;
  2663. procedure tscannerfile.inc_comment_level;
  2664. begin
  2665. if (m_nested_comment in current_settings.modeswitches) then
  2666. inc(comment_level)
  2667. else
  2668. comment_level:=1;
  2669. if (comment_level>1) then
  2670. begin
  2671. savetokenpos;
  2672. gettokenpos; { update for warning }
  2673. Message1(scan_w_comment_level,tostr(comment_level));
  2674. restoretokenpos;
  2675. end;
  2676. end;
  2677. procedure tscannerfile.dec_comment_level;
  2678. begin
  2679. if (m_nested_comment in current_settings.modeswitches) then
  2680. dec(comment_level)
  2681. else
  2682. comment_level:=0;
  2683. end;
  2684. procedure tscannerfile.linebreak;
  2685. var
  2686. cur : char;
  2687. begin
  2688. with inputfile do
  2689. begin
  2690. if (byte(inputpointer^)=0) and not(endoffile) then
  2691. begin
  2692. cur:=c;
  2693. reload;
  2694. if byte(cur)+byte(c)<>23 then
  2695. dec(inputpointer);
  2696. end
  2697. else
  2698. begin
  2699. { Support all combination of #10 and #13 as line break }
  2700. if (byte(inputpointer^)+byte(c)=23) then
  2701. inc(inputpointer);
  2702. end;
  2703. { Always return #10 as line break }
  2704. c:=#10;
  2705. { increase line counters }
  2706. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  2707. inc(line_no);
  2708. { update linebuffer }
  2709. if cs_asm_source in current_settings.globalswitches then
  2710. inputfile.setline(line_no,lastlinepos);
  2711. { update for status and call the show status routine,
  2712. but don't touch current_filepos ! }
  2713. savetokenpos;
  2714. gettokenpos; { update for v_status }
  2715. inc(status.compiledlines);
  2716. ShowStatus;
  2717. restoretokenpos;
  2718. end;
  2719. end;
  2720. procedure tscannerfile.illegal_char(c:char);
  2721. var
  2722. s : string;
  2723. begin
  2724. if c in [#32..#255] then
  2725. s:=''''+c+''''
  2726. else
  2727. s:='#'+tostr(ord(c));
  2728. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  2729. end;
  2730. procedure tscannerfile.end_of_file;
  2731. begin
  2732. checkpreprocstack;
  2733. Message(scan_f_end_of_file);
  2734. end;
  2735. {-------------------------------------------
  2736. IF Conditional Handling
  2737. -------------------------------------------}
  2738. procedure tscannerfile.checkpreprocstack;
  2739. begin
  2740. { check for missing ifdefs }
  2741. while assigned(preprocstack) do
  2742. begin
  2743. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  2744. preprocstack.owner.inputfile.name,tostr(preprocstack.line_nb));
  2745. poppreprocstack;
  2746. end;
  2747. end;
  2748. procedure tscannerfile.poppreprocstack;
  2749. var
  2750. hp : tpreprocstack;
  2751. begin
  2752. if assigned(preprocstack) then
  2753. begin
  2754. Message1(scan_c_endif_found,preprocstack.name);
  2755. hp:=preprocstack.next;
  2756. preprocstack.free;
  2757. preprocstack:=hp;
  2758. end
  2759. else
  2760. Message(scan_e_endif_without_if);
  2761. end;
  2762. procedure tscannerfile.ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  2763. var
  2764. condition: Boolean;
  2765. valuedescr: String;
  2766. begin
  2767. if (preprocstack=nil) or preprocstack.accept then
  2768. condition:= compile_time_predicate(valuedescr)
  2769. else
  2770. begin
  2771. condition:= false;
  2772. valuedescr:= '';
  2773. end;
  2774. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  2775. preprocstack.name:=valuedescr;
  2776. preprocstack.line_nb:=line_no;
  2777. preprocstack.owner:=self;
  2778. if preprocstack.accept then
  2779. Message2(messid,preprocstack.name,'accepted')
  2780. else
  2781. Message2(messid,preprocstack.name,'rejected');
  2782. end;
  2783. procedure tscannerfile.elsepreprocstack;
  2784. begin
  2785. if assigned(preprocstack) and
  2786. (preprocstack.typ<>pp_else) then
  2787. begin
  2788. if (preprocstack.typ=pp_elseif) then
  2789. preprocstack.accept:=false
  2790. else
  2791. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  2792. preprocstack.accept:=not preprocstack.accept;
  2793. preprocstack.typ:=pp_else;
  2794. preprocstack.line_nb:=line_no;
  2795. if preprocstack.accept then
  2796. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2797. else
  2798. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2799. end
  2800. else
  2801. Message(scan_e_endif_without_if);
  2802. end;
  2803. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  2804. var
  2805. valuedescr: String;
  2806. begin
  2807. if assigned(preprocstack) and
  2808. (preprocstack.typ in [pp_if,pp_elseif]) then
  2809. begin
  2810. { when the branch is accepted we use pp_elseif so we know that
  2811. all the next branches need to be rejected. when this branch is still
  2812. not accepted then leave it at pp_if }
  2813. if (preprocstack.typ=pp_elseif) then
  2814. preprocstack.accept:=false
  2815. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  2816. begin
  2817. preprocstack.accept:=false;
  2818. preprocstack.typ:=pp_elseif;
  2819. end
  2820. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  2821. and compile_time_predicate(valuedescr) then
  2822. begin
  2823. preprocstack.name:=valuedescr;
  2824. preprocstack.accept:=true;
  2825. preprocstack.typ:=pp_elseif;
  2826. end;
  2827. preprocstack.line_nb:=line_no;
  2828. if preprocstack.accept then
  2829. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2830. else
  2831. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2832. end
  2833. else
  2834. Message(scan_e_endif_without_if);
  2835. end;
  2836. procedure tscannerfile.popreplaystack;
  2837. var
  2838. hp : treplaystack;
  2839. begin
  2840. if assigned(replaystack) then
  2841. begin
  2842. hp:=replaystack.next;
  2843. replaystack.free;
  2844. replaystack:=hp;
  2845. if assigned (replaystack) then
  2846. tokenbuf_change_endian:=replaystack.change_endian
  2847. else
  2848. tokenbuf_change_endian:=false;
  2849. end;
  2850. end;
  2851. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  2852. begin
  2853. savetokenpos;
  2854. repeat
  2855. current_scanner.gettokenpos;
  2856. Message1(scan_d_handling_switch,'$'+p.name);
  2857. p.proc();
  2858. { accept the text ? }
  2859. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  2860. break
  2861. else
  2862. begin
  2863. current_scanner.gettokenpos;
  2864. Message(scan_c_skipping_until);
  2865. repeat
  2866. current_scanner.skipuntildirective;
  2867. if not (m_mac in current_settings.modeswitches) then
  2868. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  2869. else
  2870. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  2871. until assigned(p) and (p.is_conditional);
  2872. current_scanner.gettokenpos;
  2873. end;
  2874. until false;
  2875. restoretokenpos;
  2876. end;
  2877. procedure tscannerfile.handledirectives;
  2878. var
  2879. t : tdirectiveitem;
  2880. hs : string;
  2881. begin
  2882. gettokenpos;
  2883. readchar; {Remove the $}
  2884. hs:=readid;
  2885. { handle empty directive }
  2886. if hs='' then
  2887. begin
  2888. Message1(scan_w_illegal_switch,'$');
  2889. exit;
  2890. end;
  2891. {$ifdef PREPROCWRITE}
  2892. if parapreprocess then
  2893. begin
  2894. t:=Get_Directive(hs);
  2895. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  2896. begin
  2897. preprocfile^.AddSpace;
  2898. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  2899. exit;
  2900. end;
  2901. end;
  2902. {$endif PREPROCWRITE}
  2903. { skip this directive? }
  2904. if (ignoredirectives.find(hs)<>nil) then
  2905. begin
  2906. if (comment_level>0) then
  2907. readcomment;
  2908. { we've read the whole comment }
  2909. aktcommentstyle:=comment_none;
  2910. exit;
  2911. end;
  2912. { Check for compiler switches }
  2913. while (length(hs)=1) and (c in ['-','+']) do
  2914. begin
  2915. Message1(scan_d_handling_switch,'$'+hs+c);
  2916. HandleSwitch(hs[1],c);
  2917. current_scanner.readchar; {Remove + or -}
  2918. if c=',' then
  2919. begin
  2920. current_scanner.readchar; {Remove , }
  2921. { read next switch, support $v+,$+}
  2922. hs:=current_scanner.readid;
  2923. if (hs='') then
  2924. begin
  2925. if (c='$') and (m_fpc in current_settings.modeswitches) then
  2926. begin
  2927. current_scanner.readchar; { skip $ }
  2928. hs:=current_scanner.readid;
  2929. end;
  2930. if (hs='') then
  2931. Message1(scan_w_illegal_directive,'$'+c);
  2932. end;
  2933. end
  2934. else
  2935. hs:='';
  2936. end;
  2937. { directives may follow switches after a , }
  2938. if hs<>'' then
  2939. begin
  2940. if not (m_mac in current_settings.modeswitches) then
  2941. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  2942. else
  2943. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  2944. if assigned(t) then
  2945. begin
  2946. if t.is_conditional then
  2947. handleconditional(t)
  2948. else
  2949. begin
  2950. Message1(scan_d_handling_switch,'$'+hs);
  2951. t.proc();
  2952. end;
  2953. end
  2954. else
  2955. begin
  2956. current_scanner.ignoredirectives.Add(hs,nil);
  2957. Message1(scan_w_illegal_directive,'$'+hs);
  2958. end;
  2959. { conditionals already read the comment }
  2960. if (current_scanner.comment_level>0) then
  2961. current_scanner.readcomment;
  2962. { we've read the whole comment }
  2963. aktcommentstyle:=comment_none;
  2964. end;
  2965. end;
  2966. procedure tscannerfile.readchar;
  2967. begin
  2968. c:=inputpointer^;
  2969. if c=#0 then
  2970. reload
  2971. else
  2972. inc(inputpointer);
  2973. end;
  2974. procedure tscannerfile.readstring;
  2975. var
  2976. i : longint;
  2977. err : boolean;
  2978. begin
  2979. err:=false;
  2980. i:=0;
  2981. repeat
  2982. case c of
  2983. '_',
  2984. '0'..'9',
  2985. 'A'..'Z' :
  2986. begin
  2987. if i<255 then
  2988. begin
  2989. inc(i);
  2990. orgpattern[i]:=c;
  2991. pattern[i]:=c;
  2992. end
  2993. else
  2994. begin
  2995. if not err then
  2996. begin
  2997. Message(scan_e_string_exceeds_255_chars);
  2998. err:=true;
  2999. end;
  3000. end;
  3001. c:=inputpointer^;
  3002. inc(inputpointer);
  3003. end;
  3004. 'a'..'z' :
  3005. begin
  3006. if i<255 then
  3007. begin
  3008. inc(i);
  3009. orgpattern[i]:=c;
  3010. pattern[i]:=chr(ord(c)-32)
  3011. end
  3012. else
  3013. begin
  3014. if not err then
  3015. begin
  3016. Message(scan_e_string_exceeds_255_chars);
  3017. err:=true;
  3018. end;
  3019. end;
  3020. c:=inputpointer^;
  3021. inc(inputpointer);
  3022. end;
  3023. #0 :
  3024. reload;
  3025. else
  3026. break;
  3027. end;
  3028. until false;
  3029. orgpattern[0]:=chr(i);
  3030. pattern[0]:=chr(i);
  3031. end;
  3032. procedure tscannerfile.readnumber;
  3033. var
  3034. base,
  3035. i : longint;
  3036. begin
  3037. case c of
  3038. '%' :
  3039. begin
  3040. readchar;
  3041. base:=2;
  3042. pattern[1]:='%';
  3043. i:=1;
  3044. end;
  3045. '&' :
  3046. begin
  3047. readchar;
  3048. base:=8;
  3049. pattern[1]:='&';
  3050. i:=1;
  3051. end;
  3052. '$' :
  3053. begin
  3054. readchar;
  3055. base:=16;
  3056. pattern[1]:='$';
  3057. i:=1;
  3058. end;
  3059. else
  3060. begin
  3061. base:=10;
  3062. i:=0;
  3063. end;
  3064. end;
  3065. while ((base>=10) and (c in ['0'..'9'])) or
  3066. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  3067. ((base=8) and (c in ['0'..'7'])) or
  3068. ((base=2) and (c in ['0'..'1'])) do
  3069. begin
  3070. if i<255 then
  3071. begin
  3072. inc(i);
  3073. pattern[i]:=c;
  3074. end;
  3075. readchar;
  3076. end;
  3077. pattern[0]:=chr(i);
  3078. end;
  3079. function tscannerfile.readid:string;
  3080. begin
  3081. readstring;
  3082. readid:=pattern;
  3083. end;
  3084. function tscannerfile.readval:longint;
  3085. var
  3086. l : longint;
  3087. w : integer;
  3088. begin
  3089. readnumber;
  3090. val(pattern,l,w);
  3091. readval:=l;
  3092. end;
  3093. function tscannerfile.readval_asstring:string;
  3094. begin
  3095. readnumber;
  3096. readval_asstring:=pattern;
  3097. end;
  3098. function tscannerfile.readcomment:string;
  3099. var
  3100. i : longint;
  3101. begin
  3102. i:=0;
  3103. repeat
  3104. case c of
  3105. '{' :
  3106. begin
  3107. if aktcommentstyle=comment_tp then
  3108. inc_comment_level;
  3109. end;
  3110. '}' :
  3111. begin
  3112. if aktcommentstyle=comment_tp then
  3113. begin
  3114. readchar;
  3115. dec_comment_level;
  3116. if comment_level=0 then
  3117. break
  3118. else
  3119. continue;
  3120. end;
  3121. end;
  3122. '*' :
  3123. begin
  3124. if aktcommentstyle=comment_oldtp then
  3125. begin
  3126. readchar;
  3127. if c=')' then
  3128. begin
  3129. readchar;
  3130. dec_comment_level;
  3131. break;
  3132. end
  3133. else
  3134. { Add both characters !!}
  3135. if (i<255) then
  3136. begin
  3137. inc(i);
  3138. readcomment[i]:='*';
  3139. if (i<255) then
  3140. begin
  3141. inc(i);
  3142. readcomment[i]:=c;
  3143. end;
  3144. end;
  3145. end
  3146. else
  3147. { Not old TP comment, so add...}
  3148. begin
  3149. if (i<255) then
  3150. begin
  3151. inc(i);
  3152. readcomment[i]:='*';
  3153. end;
  3154. end;
  3155. end;
  3156. #10,#13 :
  3157. linebreak;
  3158. #26 :
  3159. end_of_file;
  3160. else
  3161. begin
  3162. if (i<255) then
  3163. begin
  3164. inc(i);
  3165. readcomment[i]:=c;
  3166. end;
  3167. end;
  3168. end;
  3169. readchar;
  3170. until false;
  3171. readcomment[0]:=chr(i);
  3172. end;
  3173. function tscannerfile.readquotedstring:string;
  3174. var
  3175. i : longint;
  3176. msgwritten : boolean;
  3177. begin
  3178. i:=0;
  3179. msgwritten:=false;
  3180. if (c='''') then
  3181. begin
  3182. repeat
  3183. readchar;
  3184. case c of
  3185. #26 :
  3186. end_of_file;
  3187. #10,#13 :
  3188. Message(scan_f_string_exceeds_line);
  3189. '''' :
  3190. begin
  3191. readchar;
  3192. if c<>'''' then
  3193. break;
  3194. end;
  3195. end;
  3196. if i<255 then
  3197. begin
  3198. inc(i);
  3199. result[i]:=c;
  3200. end
  3201. else
  3202. begin
  3203. if not msgwritten then
  3204. begin
  3205. Message(scan_e_string_exceeds_255_chars);
  3206. msgwritten:=true;
  3207. end;
  3208. end;
  3209. until false;
  3210. end;
  3211. result[0]:=chr(i);
  3212. end;
  3213. function tscannerfile.readstate:char;
  3214. var
  3215. state : char;
  3216. begin
  3217. state:=' ';
  3218. if c=' ' then
  3219. begin
  3220. current_scanner.skipspace;
  3221. current_scanner.readid;
  3222. if pattern='ON' then
  3223. state:='+'
  3224. else
  3225. if pattern='OFF' then
  3226. state:='-';
  3227. end
  3228. else
  3229. state:=c;
  3230. if not (state in ['+','-']) then
  3231. Message(scan_e_wrong_switch_toggle);
  3232. readstate:=state;
  3233. end;
  3234. function tscannerfile.readstatedefault:char;
  3235. var
  3236. state : char;
  3237. begin
  3238. state:=' ';
  3239. if c=' ' then
  3240. begin
  3241. current_scanner.skipspace;
  3242. current_scanner.readid;
  3243. if pattern='ON' then
  3244. state:='+'
  3245. else
  3246. if pattern='OFF' then
  3247. state:='-'
  3248. else
  3249. if pattern='DEFAULT' then
  3250. state:='*';
  3251. end
  3252. else
  3253. state:=c;
  3254. if not (state in ['+','-','*']) then
  3255. Message(scan_e_wrong_switch_toggle_default);
  3256. readstatedefault:=state;
  3257. end;
  3258. procedure tscannerfile.skipspace;
  3259. begin
  3260. repeat
  3261. case c of
  3262. #26 :
  3263. begin
  3264. reload;
  3265. if (c=#26) and not assigned(inputfile.next) then
  3266. break;
  3267. continue;
  3268. end;
  3269. #10,
  3270. #13 :
  3271. linebreak;
  3272. #9,#11,#12,' ' :
  3273. ;
  3274. else
  3275. break;
  3276. end;
  3277. readchar;
  3278. until false;
  3279. end;
  3280. procedure tscannerfile.skipuntildirective;
  3281. var
  3282. found : longint;
  3283. next_char_loaded : boolean;
  3284. begin
  3285. found:=0;
  3286. next_char_loaded:=false;
  3287. repeat
  3288. case c of
  3289. #10,
  3290. #13 :
  3291. linebreak;
  3292. #26 :
  3293. begin
  3294. reload;
  3295. if (c=#26) and not assigned(inputfile.next) then
  3296. end_of_file;
  3297. continue;
  3298. end;
  3299. '{' :
  3300. begin
  3301. if (aktcommentstyle in [comment_tp,comment_none]) then
  3302. begin
  3303. aktcommentstyle:=comment_tp;
  3304. if (comment_level=0) then
  3305. found:=1;
  3306. inc_comment_level;
  3307. end;
  3308. end;
  3309. '*' :
  3310. begin
  3311. if (aktcommentstyle=comment_oldtp) then
  3312. begin
  3313. readchar;
  3314. if c=')' then
  3315. begin
  3316. dec_comment_level;
  3317. found:=0;
  3318. aktcommentstyle:=comment_none;
  3319. end
  3320. else
  3321. next_char_loaded:=true;
  3322. end
  3323. else
  3324. found := 0;
  3325. end;
  3326. '}' :
  3327. begin
  3328. if (aktcommentstyle=comment_tp) then
  3329. begin
  3330. dec_comment_level;
  3331. if (comment_level=0) then
  3332. aktcommentstyle:=comment_none;
  3333. found:=0;
  3334. end;
  3335. end;
  3336. '$' :
  3337. begin
  3338. if found=1 then
  3339. found:=2;
  3340. end;
  3341. '''' :
  3342. if (aktcommentstyle=comment_none) then
  3343. begin
  3344. repeat
  3345. readchar;
  3346. case c of
  3347. #26 :
  3348. end_of_file;
  3349. #10,#13 :
  3350. break;
  3351. '''' :
  3352. begin
  3353. readchar;
  3354. if c<>'''' then
  3355. begin
  3356. next_char_loaded:=true;
  3357. break;
  3358. end;
  3359. end;
  3360. end;
  3361. until false;
  3362. end;
  3363. '(' :
  3364. begin
  3365. if (aktcommentstyle=comment_none) then
  3366. begin
  3367. readchar;
  3368. if c='*' then
  3369. begin
  3370. readchar;
  3371. if c='$' then
  3372. begin
  3373. found:=2;
  3374. inc_comment_level;
  3375. aktcommentstyle:=comment_oldtp;
  3376. end
  3377. else
  3378. begin
  3379. skipoldtpcomment;
  3380. next_char_loaded:=true;
  3381. end;
  3382. end
  3383. else
  3384. next_char_loaded:=true;
  3385. end
  3386. else
  3387. found:=0;
  3388. end;
  3389. '/' :
  3390. begin
  3391. if (aktcommentstyle=comment_none) then
  3392. begin
  3393. readchar;
  3394. if c='/' then
  3395. skipdelphicomment;
  3396. next_char_loaded:=true;
  3397. end
  3398. else
  3399. found:=0;
  3400. end;
  3401. else
  3402. found:=0;
  3403. end;
  3404. if next_char_loaded then
  3405. next_char_loaded:=false
  3406. else
  3407. readchar;
  3408. until (found=2);
  3409. end;
  3410. {****************************************************************************
  3411. Comment Handling
  3412. ****************************************************************************}
  3413. procedure tscannerfile.skipcomment;
  3414. begin
  3415. aktcommentstyle:=comment_tp;
  3416. readchar;
  3417. inc_comment_level;
  3418. { handle compiler switches }
  3419. if (c='$') then
  3420. handledirectives;
  3421. { handle_switches can dec comment_level, }
  3422. while (comment_level>0) do
  3423. begin
  3424. case c of
  3425. '{' :
  3426. inc_comment_level;
  3427. '}' :
  3428. dec_comment_level;
  3429. #10,#13 :
  3430. linebreak;
  3431. #26 :
  3432. begin
  3433. reload;
  3434. if (c=#26) and not assigned(inputfile.next) then
  3435. end_of_file;
  3436. continue;
  3437. end;
  3438. end;
  3439. readchar;
  3440. end;
  3441. aktcommentstyle:=comment_none;
  3442. end;
  3443. procedure tscannerfile.skipdelphicomment;
  3444. begin
  3445. aktcommentstyle:=comment_delphi;
  3446. inc_comment_level;
  3447. readchar;
  3448. { this is not supported }
  3449. if c='$' then
  3450. Message(scan_w_wrong_styled_switch);
  3451. { skip comment }
  3452. while not (c in [#10,#13,#26]) do
  3453. readchar;
  3454. dec_comment_level;
  3455. aktcommentstyle:=comment_none;
  3456. end;
  3457. procedure tscannerfile.skipoldtpcomment;
  3458. var
  3459. found : longint;
  3460. begin
  3461. aktcommentstyle:=comment_oldtp;
  3462. inc_comment_level;
  3463. { only load a char if last already processed,
  3464. was cause of bug1634 PM }
  3465. if c=#0 then
  3466. readchar;
  3467. { this is now supported }
  3468. if (c='$') then
  3469. handledirectives;
  3470. { skip comment }
  3471. while (comment_level>0) do
  3472. begin
  3473. found:=0;
  3474. repeat
  3475. case c of
  3476. #26 :
  3477. begin
  3478. reload;
  3479. if (c=#26) and not assigned(inputfile.next) then
  3480. end_of_file;
  3481. continue;
  3482. end;
  3483. #10,#13 :
  3484. begin
  3485. if found=4 then
  3486. inc_comment_level;
  3487. linebreak;
  3488. found:=0;
  3489. end;
  3490. '*' :
  3491. begin
  3492. if found=3 then
  3493. found:=4
  3494. else
  3495. found:=1;
  3496. end;
  3497. ')' :
  3498. begin
  3499. if found in [1,4] then
  3500. begin
  3501. dec_comment_level;
  3502. if comment_level=0 then
  3503. found:=2
  3504. else
  3505. found:=0;
  3506. end
  3507. else
  3508. found:=0;
  3509. end;
  3510. '(' :
  3511. begin
  3512. if found=4 then
  3513. inc_comment_level;
  3514. found:=3;
  3515. end;
  3516. else
  3517. begin
  3518. if found=4 then
  3519. inc_comment_level;
  3520. found:=0;
  3521. end;
  3522. end;
  3523. readchar;
  3524. until (found=2);
  3525. end;
  3526. aktcommentstyle:=comment_none;
  3527. end;
  3528. {****************************************************************************
  3529. Token Scanner
  3530. ****************************************************************************}
  3531. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  3532. var
  3533. code : integer;
  3534. len,
  3535. low,high,mid : longint;
  3536. w : word;
  3537. m : longint;
  3538. mac : tmacro;
  3539. asciinr : string[33];
  3540. iswidestring : boolean;
  3541. label
  3542. exit_label;
  3543. begin
  3544. flushpendingswitchesstate;
  3545. { record tokens? }
  3546. if allowrecordtoken and
  3547. assigned(recordtokenbuf) then
  3548. recordtoken;
  3549. { replay tokens? }
  3550. if assigned(replaytokenbuf) then
  3551. begin
  3552. replaytoken;
  3553. goto exit_label;
  3554. end;
  3555. { was there already a token read, then return that token }
  3556. if nexttoken<>NOTOKEN then
  3557. begin
  3558. setnexttoken;
  3559. goto exit_label;
  3560. end;
  3561. { Skip all spaces and comments }
  3562. repeat
  3563. case c of
  3564. '{' :
  3565. skipcomment;
  3566. #26 :
  3567. begin
  3568. reload;
  3569. if (c=#26) and not assigned(inputfile.next) then
  3570. break;
  3571. end;
  3572. ' ',#9..#13 :
  3573. begin
  3574. {$ifdef PREPROCWRITE}
  3575. if parapreprocess then
  3576. begin
  3577. if c=#10 then
  3578. preprocfile.eolfound:=true
  3579. else
  3580. preprocfile.spacefound:=true;
  3581. end;
  3582. {$endif PREPROCWRITE}
  3583. skipspace;
  3584. end
  3585. else
  3586. break;
  3587. end;
  3588. until false;
  3589. { Save current token position, for EOF its already loaded }
  3590. if c<>#26 then
  3591. gettokenpos;
  3592. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  3593. if c in ['A'..'Z','a'..'z','_'] then
  3594. begin
  3595. readstring;
  3596. token:=_ID;
  3597. idtoken:=_ID;
  3598. { keyword or any other known token,
  3599. pattern is always uppercased }
  3600. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  3601. begin
  3602. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  3603. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  3604. while low<high do
  3605. begin
  3606. mid:=(high+low+1) shr 1;
  3607. if pattern<tokeninfo^[ttoken(mid)].str then
  3608. high:=mid-1
  3609. else
  3610. low:=mid;
  3611. end;
  3612. with tokeninfo^[ttoken(high)] do
  3613. if pattern=str then
  3614. begin
  3615. if keyword in current_settings.modeswitches then
  3616. if op=NOTOKEN then
  3617. token:=ttoken(high)
  3618. else
  3619. token:=op;
  3620. idtoken:=ttoken(high);
  3621. end;
  3622. end;
  3623. { Only process identifiers and not keywords }
  3624. if token=_ID then
  3625. begin
  3626. { this takes some time ... }
  3627. if (cs_support_macro in current_settings.moduleswitches) then
  3628. begin
  3629. mac:=tmacro(search_macro(pattern));
  3630. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  3631. begin
  3632. if yylexcount<max_macro_nesting then
  3633. begin
  3634. mac.is_used:=true;
  3635. inc(yylexcount);
  3636. substitutemacro(pattern,mac.buftext,mac.buflen,
  3637. mac.fileinfo.line,mac.fileinfo.fileindex);
  3638. { handle empty macros }
  3639. if c=#0 then
  3640. reload;
  3641. readtoken(false);
  3642. { that's all folks }
  3643. dec(yylexcount);
  3644. exit;
  3645. end
  3646. else
  3647. Message(scan_w_macro_too_deep);
  3648. end;
  3649. end;
  3650. end;
  3651. { return token }
  3652. goto exit_label;
  3653. end
  3654. else
  3655. begin
  3656. idtoken:=_NOID;
  3657. case c of
  3658. '$' :
  3659. begin
  3660. readnumber;
  3661. token:=_INTCONST;
  3662. goto exit_label;
  3663. end;
  3664. '%' :
  3665. begin
  3666. if not(m_fpc in current_settings.modeswitches) then
  3667. Illegal_Char(c)
  3668. else
  3669. begin
  3670. readnumber;
  3671. token:=_INTCONST;
  3672. goto exit_label;
  3673. end;
  3674. end;
  3675. '&' :
  3676. begin
  3677. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  3678. begin
  3679. readnumber;
  3680. if length(pattern)=1 then
  3681. begin
  3682. readstring;
  3683. token:=_ID;
  3684. idtoken:=_ID;
  3685. end
  3686. else
  3687. token:=_INTCONST;
  3688. goto exit_label;
  3689. end
  3690. else if m_mac in current_settings.modeswitches then
  3691. begin
  3692. readchar;
  3693. token:=_AMPERSAND;
  3694. goto exit_label;
  3695. end
  3696. else
  3697. Illegal_Char(c);
  3698. end;
  3699. '0'..'9' :
  3700. begin
  3701. readnumber;
  3702. if (c in ['.','e','E']) then
  3703. begin
  3704. { first check for a . }
  3705. if c='.' then
  3706. begin
  3707. cachenexttokenpos;
  3708. readchar;
  3709. { is it a .. from a range? }
  3710. case c of
  3711. '.' :
  3712. begin
  3713. readchar;
  3714. token:=_INTCONST;
  3715. nexttoken:=_POINTPOINT;
  3716. goto exit_label;
  3717. end;
  3718. ')' :
  3719. begin
  3720. readchar;
  3721. token:=_INTCONST;
  3722. nexttoken:=_RECKKLAMMER;
  3723. goto exit_label;
  3724. end;
  3725. end;
  3726. { insert the number after the . }
  3727. pattern:=pattern+'.';
  3728. while c in ['0'..'9'] do
  3729. begin
  3730. pattern:=pattern+c;
  3731. readchar;
  3732. end;
  3733. end;
  3734. { E can also follow after a point is scanned }
  3735. if c in ['e','E'] then
  3736. begin
  3737. pattern:=pattern+'E';
  3738. readchar;
  3739. if c in ['-','+'] then
  3740. begin
  3741. pattern:=pattern+c;
  3742. readchar;
  3743. end;
  3744. if not(c in ['0'..'9']) then
  3745. Illegal_Char(c);
  3746. while c in ['0'..'9'] do
  3747. begin
  3748. pattern:=pattern+c;
  3749. readchar;
  3750. end;
  3751. end;
  3752. token:=_REALNUMBER;
  3753. goto exit_label;
  3754. end;
  3755. token:=_INTCONST;
  3756. goto exit_label;
  3757. end;
  3758. ';' :
  3759. begin
  3760. readchar;
  3761. token:=_SEMICOLON;
  3762. goto exit_label;
  3763. end;
  3764. '[' :
  3765. begin
  3766. readchar;
  3767. token:=_LECKKLAMMER;
  3768. goto exit_label;
  3769. end;
  3770. ']' :
  3771. begin
  3772. readchar;
  3773. token:=_RECKKLAMMER;
  3774. goto exit_label;
  3775. end;
  3776. '(' :
  3777. begin
  3778. readchar;
  3779. case c of
  3780. '*' :
  3781. begin
  3782. c:=#0;{Signal skipoldtpcomment to reload a char }
  3783. skipoldtpcomment;
  3784. readtoken(false);
  3785. exit;
  3786. end;
  3787. '.' :
  3788. begin
  3789. readchar;
  3790. token:=_LECKKLAMMER;
  3791. goto exit_label;
  3792. end;
  3793. end;
  3794. token:=_LKLAMMER;
  3795. goto exit_label;
  3796. end;
  3797. ')' :
  3798. begin
  3799. readchar;
  3800. token:=_RKLAMMER;
  3801. goto exit_label;
  3802. end;
  3803. '+' :
  3804. begin
  3805. readchar;
  3806. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3807. begin
  3808. readchar;
  3809. token:=_PLUSASN;
  3810. goto exit_label;
  3811. end;
  3812. token:=_PLUS;
  3813. goto exit_label;
  3814. end;
  3815. '-' :
  3816. begin
  3817. readchar;
  3818. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3819. begin
  3820. readchar;
  3821. token:=_MINUSASN;
  3822. goto exit_label;
  3823. end;
  3824. token:=_MINUS;
  3825. goto exit_label;
  3826. end;
  3827. ':' :
  3828. begin
  3829. readchar;
  3830. if c='=' then
  3831. begin
  3832. readchar;
  3833. token:=_ASSIGNMENT;
  3834. goto exit_label;
  3835. end;
  3836. token:=_COLON;
  3837. goto exit_label;
  3838. end;
  3839. '*' :
  3840. begin
  3841. readchar;
  3842. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3843. begin
  3844. readchar;
  3845. token:=_STARASN;
  3846. end
  3847. else
  3848. if c='*' then
  3849. begin
  3850. readchar;
  3851. token:=_STARSTAR;
  3852. end
  3853. else
  3854. token:=_STAR;
  3855. goto exit_label;
  3856. end;
  3857. '/' :
  3858. begin
  3859. readchar;
  3860. case c of
  3861. '=' :
  3862. begin
  3863. if (cs_support_c_operators in current_settings.moduleswitches) then
  3864. begin
  3865. readchar;
  3866. token:=_SLASHASN;
  3867. goto exit_label;
  3868. end;
  3869. end;
  3870. '/' :
  3871. begin
  3872. skipdelphicomment;
  3873. readtoken(false);
  3874. exit;
  3875. end;
  3876. end;
  3877. token:=_SLASH;
  3878. goto exit_label;
  3879. end;
  3880. '|' :
  3881. if m_mac in current_settings.modeswitches then
  3882. begin
  3883. readchar;
  3884. token:=_PIPE;
  3885. goto exit_label;
  3886. end
  3887. else
  3888. Illegal_Char(c);
  3889. '=' :
  3890. begin
  3891. readchar;
  3892. token:=_EQ;
  3893. goto exit_label;
  3894. end;
  3895. '.' :
  3896. begin
  3897. readchar;
  3898. case c of
  3899. '.' :
  3900. begin
  3901. readchar;
  3902. case c of
  3903. '.' :
  3904. begin
  3905. readchar;
  3906. token:=_POINTPOINTPOINT;
  3907. goto exit_label;
  3908. end;
  3909. else
  3910. begin
  3911. token:=_POINTPOINT;
  3912. goto exit_label;
  3913. end;
  3914. end;
  3915. end;
  3916. ')' :
  3917. begin
  3918. readchar;
  3919. token:=_RECKKLAMMER;
  3920. goto exit_label;
  3921. end;
  3922. end;
  3923. token:=_POINT;
  3924. goto exit_label;
  3925. end;
  3926. '@' :
  3927. begin
  3928. readchar;
  3929. token:=_KLAMMERAFFE;
  3930. goto exit_label;
  3931. end;
  3932. ',' :
  3933. begin
  3934. readchar;
  3935. token:=_COMMA;
  3936. goto exit_label;
  3937. end;
  3938. '''','#','^' :
  3939. begin
  3940. len:=0;
  3941. cstringpattern:='';
  3942. iswidestring:=false;
  3943. if c='^' then
  3944. begin
  3945. readchar;
  3946. c:=upcase(c);
  3947. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  3948. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  3949. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  3950. begin
  3951. token:=_CARET;
  3952. goto exit_label;
  3953. end
  3954. else
  3955. begin
  3956. inc(len);
  3957. setlength(cstringpattern,256);
  3958. if c<#64 then
  3959. cstringpattern[len]:=chr(ord(c)+64)
  3960. else
  3961. cstringpattern[len]:=chr(ord(c)-64);
  3962. readchar;
  3963. end;
  3964. end;
  3965. repeat
  3966. case c of
  3967. '#' :
  3968. begin
  3969. readchar; { read # }
  3970. case c of
  3971. '$':
  3972. begin
  3973. readchar; { read leading $ }
  3974. asciinr:='$';
  3975. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=5) do
  3976. begin
  3977. asciinr:=asciinr+c;
  3978. readchar;
  3979. end;
  3980. end;
  3981. '&':
  3982. begin
  3983. readchar; { read leading $ }
  3984. asciinr:='&';
  3985. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=7) do
  3986. begin
  3987. asciinr:=asciinr+c;
  3988. readchar;
  3989. end;
  3990. end;
  3991. '%':
  3992. begin
  3993. readchar; { read leading $ }
  3994. asciinr:='%';
  3995. while (upcase(c) in ['0','1']) and (length(asciinr)<=17) do
  3996. begin
  3997. asciinr:=asciinr+c;
  3998. readchar;
  3999. end;
  4000. end;
  4001. else
  4002. begin
  4003. asciinr:='';
  4004. while (c in ['0'..'9']) and (length(asciinr)<=5) do
  4005. begin
  4006. asciinr:=asciinr+c;
  4007. readchar;
  4008. end;
  4009. end;
  4010. end;
  4011. val(asciinr,m,code);
  4012. if (asciinr='') or (code<>0) then
  4013. Message(scan_e_illegal_char_const)
  4014. else if (m<0) or (m>255) or (length(asciinr)>3) then
  4015. begin
  4016. if (m>=0) and (m<=65535) then
  4017. begin
  4018. if not iswidestring then
  4019. begin
  4020. if len>0 then
  4021. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4022. else
  4023. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4024. iswidestring:=true;
  4025. len:=0;
  4026. end;
  4027. concatwidestringchar(patternw,tcompilerwidechar(m));
  4028. end
  4029. else
  4030. Message(scan_e_illegal_char_const)
  4031. end
  4032. else if iswidestring then
  4033. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  4034. else
  4035. begin
  4036. if len>=length(cstringpattern) then
  4037. setlength(cstringpattern,length(cstringpattern)+256);
  4038. inc(len);
  4039. cstringpattern[len]:=chr(m);
  4040. end;
  4041. end;
  4042. '''' :
  4043. begin
  4044. repeat
  4045. readchar;
  4046. case c of
  4047. #26 :
  4048. end_of_file;
  4049. #10,#13 :
  4050. Message(scan_f_string_exceeds_line);
  4051. '''' :
  4052. begin
  4053. readchar;
  4054. if c<>'''' then
  4055. break;
  4056. end;
  4057. end;
  4058. { interpret as utf-8 string? }
  4059. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  4060. begin
  4061. { convert existing string to an utf-8 string }
  4062. if not iswidestring then
  4063. begin
  4064. if len>0 then
  4065. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4066. else
  4067. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4068. iswidestring:=true;
  4069. len:=0;
  4070. end;
  4071. { four or more chars aren't handled }
  4072. if (ord(c) and $f0)=$f0 then
  4073. message(scan_e_utf8_bigger_than_65535)
  4074. { three chars }
  4075. else if (ord(c) and $e0)=$e0 then
  4076. begin
  4077. w:=ord(c) and $f;
  4078. readchar;
  4079. if (ord(c) and $c0)<>$80 then
  4080. message(scan_e_utf8_malformed);
  4081. w:=(w shl 6) or (ord(c) and $3f);
  4082. readchar;
  4083. if (ord(c) and $c0)<>$80 then
  4084. message(scan_e_utf8_malformed);
  4085. w:=(w shl 6) or (ord(c) and $3f);
  4086. concatwidestringchar(patternw,w);
  4087. end
  4088. { two chars }
  4089. else if (ord(c) and $c0)<>0 then
  4090. begin
  4091. w:=ord(c) and $1f;
  4092. readchar;
  4093. if (ord(c) and $c0)<>$80 then
  4094. message(scan_e_utf8_malformed);
  4095. w:=(w shl 6) or (ord(c) and $3f);
  4096. concatwidestringchar(patternw,w);
  4097. end
  4098. { illegal }
  4099. else if (ord(c) and $80)<>0 then
  4100. message(scan_e_utf8_malformed)
  4101. else
  4102. concatwidestringchar(patternw,tcompilerwidechar(c))
  4103. end
  4104. else if iswidestring then
  4105. begin
  4106. if current_settings.sourcecodepage=CP_UTF8 then
  4107. concatwidestringchar(patternw,ord(c))
  4108. else
  4109. concatwidestringchar(patternw,asciichar2unicode(c))
  4110. end
  4111. else
  4112. begin
  4113. if len>=length(cstringpattern) then
  4114. setlength(cstringpattern,length(cstringpattern)+256);
  4115. inc(len);
  4116. cstringpattern[len]:=c;
  4117. end;
  4118. until false;
  4119. end;
  4120. '^' :
  4121. begin
  4122. readchar;
  4123. c:=upcase(c);
  4124. if c<#64 then
  4125. c:=chr(ord(c)+64)
  4126. else
  4127. c:=chr(ord(c)-64);
  4128. if iswidestring then
  4129. concatwidestringchar(patternw,asciichar2unicode(c))
  4130. else
  4131. begin
  4132. if len>=length(cstringpattern) then
  4133. setlength(cstringpattern,length(cstringpattern)+256);
  4134. inc(len);
  4135. cstringpattern[len]:=c;
  4136. end;
  4137. readchar;
  4138. end;
  4139. else
  4140. break;
  4141. end;
  4142. until false;
  4143. { strings with length 1 become const chars }
  4144. if iswidestring then
  4145. begin
  4146. if patternw^.len=1 then
  4147. token:=_CWCHAR
  4148. else
  4149. token:=_CWSTRING;
  4150. end
  4151. else
  4152. begin
  4153. setlength(cstringpattern,len);
  4154. if length(cstringpattern)=1 then
  4155. begin
  4156. token:=_CCHAR;
  4157. pattern:=cstringpattern;
  4158. end
  4159. else
  4160. token:=_CSTRING;
  4161. end;
  4162. goto exit_label;
  4163. end;
  4164. '>' :
  4165. begin
  4166. readchar;
  4167. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4168. token:=_RSHARPBRACKET
  4169. else
  4170. begin
  4171. case c of
  4172. '=' :
  4173. begin
  4174. readchar;
  4175. token:=_GTE;
  4176. goto exit_label;
  4177. end;
  4178. '>' :
  4179. begin
  4180. readchar;
  4181. token:=_OP_SHR;
  4182. goto exit_label;
  4183. end;
  4184. '<' :
  4185. begin { >< is for a symetric diff for sets }
  4186. readchar;
  4187. token:=_SYMDIF;
  4188. goto exit_label;
  4189. end;
  4190. end;
  4191. token:=_GT;
  4192. end;
  4193. goto exit_label;
  4194. end;
  4195. '<' :
  4196. begin
  4197. readchar;
  4198. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4199. token:=_LSHARPBRACKET
  4200. else
  4201. begin
  4202. case c of
  4203. '>' :
  4204. begin
  4205. readchar;
  4206. token:=_NE;
  4207. goto exit_label;
  4208. end;
  4209. '=' :
  4210. begin
  4211. readchar;
  4212. token:=_LTE;
  4213. goto exit_label;
  4214. end;
  4215. '<' :
  4216. begin
  4217. readchar;
  4218. token:=_OP_SHL;
  4219. goto exit_label;
  4220. end;
  4221. end;
  4222. token:=_LT;
  4223. end;
  4224. goto exit_label;
  4225. end;
  4226. #26 :
  4227. begin
  4228. token:=_EOF;
  4229. checkpreprocstack;
  4230. goto exit_label;
  4231. end;
  4232. else
  4233. Illegal_Char(c);
  4234. end;
  4235. end;
  4236. exit_label:
  4237. lasttoken:=token;
  4238. end;
  4239. function tscannerfile.readpreproc:ttoken;
  4240. begin
  4241. skipspace;
  4242. case c of
  4243. '_',
  4244. 'A'..'Z',
  4245. 'a'..'z' :
  4246. begin
  4247. current_scanner.preproc_pattern:=readid;
  4248. readpreproc:=_ID;
  4249. end;
  4250. '0'..'9' :
  4251. begin
  4252. current_scanner.preproc_pattern:=readval_asstring;
  4253. { realnumber? }
  4254. if c='.' then
  4255. begin
  4256. readchar;
  4257. while c in ['0'..'9'] do
  4258. begin
  4259. current_scanner.preproc_pattern:=current_scanner.preproc_pattern+c;
  4260. readchar;
  4261. end;
  4262. end;
  4263. readpreproc:=_ID;
  4264. end;
  4265. '$','%','&' :
  4266. begin
  4267. current_scanner.preproc_pattern:=readval_asstring;
  4268. readpreproc:=_ID;
  4269. end;
  4270. ',' :
  4271. begin
  4272. readchar;
  4273. readpreproc:=_COMMA;
  4274. end;
  4275. '}' :
  4276. begin
  4277. readpreproc:=_END;
  4278. end;
  4279. '(' :
  4280. begin
  4281. readchar;
  4282. readpreproc:=_LKLAMMER;
  4283. end;
  4284. ')' :
  4285. begin
  4286. readchar;
  4287. readpreproc:=_RKLAMMER;
  4288. end;
  4289. '[' :
  4290. begin
  4291. readchar;
  4292. readpreproc:=_LECKKLAMMER;
  4293. end;
  4294. ']' :
  4295. begin
  4296. readchar;
  4297. readpreproc:=_RECKKLAMMER;
  4298. end;
  4299. '+' :
  4300. begin
  4301. readchar;
  4302. readpreproc:=_PLUS;
  4303. end;
  4304. '-' :
  4305. begin
  4306. readchar;
  4307. readpreproc:=_MINUS;
  4308. end;
  4309. '*' :
  4310. begin
  4311. readchar;
  4312. readpreproc:=_STAR;
  4313. end;
  4314. '/' :
  4315. begin
  4316. readchar;
  4317. readpreproc:=_SLASH;
  4318. end;
  4319. '=' :
  4320. begin
  4321. readchar;
  4322. readpreproc:=_EQ;
  4323. end;
  4324. '>' :
  4325. begin
  4326. readchar;
  4327. if c='=' then
  4328. begin
  4329. readchar;
  4330. readpreproc:=_GTE;
  4331. end
  4332. else
  4333. readpreproc:=_GT;
  4334. end;
  4335. '<' :
  4336. begin
  4337. readchar;
  4338. case c of
  4339. '>' :
  4340. begin
  4341. readchar;
  4342. readpreproc:=_NE;
  4343. end;
  4344. '=' :
  4345. begin
  4346. readchar;
  4347. readpreproc:=_LTE;
  4348. end;
  4349. else
  4350. readpreproc:=_LT;
  4351. end;
  4352. end;
  4353. #26 :
  4354. begin
  4355. readpreproc:=_EOF;
  4356. checkpreprocstack;
  4357. end;
  4358. else
  4359. Illegal_Char(c);
  4360. end;
  4361. end;
  4362. function tscannerfile.asmgetcharstart : char;
  4363. begin
  4364. { return first the character already
  4365. available in c }
  4366. lastasmgetchar:=c;
  4367. result:=asmgetchar;
  4368. end;
  4369. function tscannerfile.asmgetchar : char;
  4370. begin
  4371. if lastasmgetchar<>#0 then
  4372. begin
  4373. c:=lastasmgetchar;
  4374. lastasmgetchar:=#0;
  4375. end
  4376. else
  4377. readchar;
  4378. if in_asm_string then
  4379. begin
  4380. asmgetchar:=c;
  4381. exit;
  4382. end;
  4383. repeat
  4384. case c of
  4385. // the { ... } is used in ARM assembler to define register sets, so we can't used
  4386. // it as comment, either (* ... *), /* ... */ or // ... should be used instead.
  4387. // But compiler directives {$...} are allowed in ARM assembler.
  4388. '{' :
  4389. begin
  4390. {$ifdef arm}
  4391. readchar;
  4392. dec(inputpointer);
  4393. if c<>'$' then
  4394. begin
  4395. asmgetchar:='{';
  4396. exit;
  4397. end
  4398. else
  4399. {$endif arm}
  4400. skipcomment;
  4401. end;
  4402. #10,#13 :
  4403. begin
  4404. linebreak;
  4405. asmgetchar:=c;
  4406. exit;
  4407. end;
  4408. #26 :
  4409. begin
  4410. reload;
  4411. if (c=#26) and not assigned(inputfile.next) then
  4412. end_of_file;
  4413. continue;
  4414. end;
  4415. '/' :
  4416. begin
  4417. readchar;
  4418. if c='/' then
  4419. skipdelphicomment
  4420. else
  4421. begin
  4422. asmgetchar:='/';
  4423. lastasmgetchar:=c;
  4424. exit;
  4425. end;
  4426. end;
  4427. '(' :
  4428. begin
  4429. readchar;
  4430. if c='*' then
  4431. begin
  4432. c:=#0;{Signal skipoldtpcomment to reload a char }
  4433. skipoldtpcomment;
  4434. end
  4435. else
  4436. begin
  4437. asmgetchar:='(';
  4438. lastasmgetchar:=c;
  4439. exit;
  4440. end;
  4441. end;
  4442. else
  4443. begin
  4444. asmgetchar:=c;
  4445. exit;
  4446. end;
  4447. end;
  4448. until false;
  4449. end;
  4450. {*****************************************************************************
  4451. Helpers
  4452. *****************************************************************************}
  4453. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  4454. begin
  4455. if dm in [directive_all, directive_turbo] then
  4456. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  4457. if dm in [directive_all, directive_mac] then
  4458. tdirectiveitem.create(mac_scannerdirectives,s,p);
  4459. end;
  4460. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  4461. begin
  4462. if dm in [directive_all, directive_turbo] then
  4463. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  4464. if dm in [directive_all, directive_mac] then
  4465. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  4466. end;
  4467. {*****************************************************************************
  4468. Initialization
  4469. *****************************************************************************}
  4470. procedure InitScanner;
  4471. begin
  4472. InitWideString(patternw);
  4473. turbo_scannerdirectives:=TFPHashObjectList.Create;
  4474. mac_scannerdirectives:=TFPHashObjectList.Create;
  4475. { Common directives and conditionals }
  4476. AddDirective('I',directive_all, @dir_include);
  4477. AddDirective('DEFINE',directive_all, @dir_define);
  4478. AddDirective('UNDEF',directive_all, @dir_undef);
  4479. AddConditional('IF',directive_all, @dir_if);
  4480. AddConditional('IFDEF',directive_all, @dir_ifdef);
  4481. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  4482. AddConditional('ELSE',directive_all, @dir_else);
  4483. AddConditional('ELSEIF',directive_all, @dir_elseif);
  4484. AddConditional('ENDIF',directive_all, @dir_endif);
  4485. { Directives and conditionals for all modes except mode macpas}
  4486. AddDirective('INCLUDE',directive_turbo, @dir_include);
  4487. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  4488. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  4489. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  4490. AddConditional('IFEND',directive_turbo, @dir_endif);
  4491. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  4492. { Directives and conditionals for mode macpas: }
  4493. AddDirective('SETC',directive_mac, @dir_setc);
  4494. AddDirective('DEFINEC',directive_mac, @dir_definec);
  4495. AddDirective('UNDEFC',directive_mac, @dir_undef);
  4496. AddConditional('IFC',directive_mac, @dir_if);
  4497. AddConditional('ELSEC',directive_mac, @dir_else);
  4498. AddConditional('ELIFC',directive_mac, @dir_elseif);
  4499. AddConditional('ENDC',directive_mac, @dir_endif);
  4500. end;
  4501. procedure DoneScanner;
  4502. begin
  4503. turbo_scannerdirectives.Free;
  4504. mac_scannerdirectives.Free;
  4505. DoneWideString(patternw);
  4506. end;
  4507. end.