scanner.pas 240 KB

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