scanner.pas 204 KB

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