scanner.pas 177 KB

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