scanner.pas 163 KB

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