scanner.pas 216 KB

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