scanner.pas 215 KB

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