scanner.pas 203 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975
  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 preproc_comp_expr:texprvalue;
  1391. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean):texprvalue; forward;
  1392. procedure preproc_consume(t:ttoken);
  1393. begin
  1394. if t<>current_scanner.preproc_token then
  1395. Message(scan_e_preproc_syntax_error);
  1396. current_scanner.preproc_token:=current_scanner.readpreproc;
  1397. end;
  1398. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;out tokentoconsume:ttoken):boolean;
  1399. var
  1400. hmodule: tmodule;
  1401. ns:ansistring;
  1402. nssym:tsym;
  1403. begin
  1404. result:=false;
  1405. tokentoconsume:=_ID;
  1406. if assigned(srsym) and (srsym.typ in [unitsym,namespacesym]) then
  1407. begin
  1408. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  1409. internalerror(200501154);
  1410. { only allow unit.symbol access if the name was
  1411. found in the current module
  1412. we can use iscurrentunit because generic specializations does not
  1413. change current_unit variable }
  1414. hmodule:=find_module_from_symtable(srsym.Owner);
  1415. if not Assigned(hmodule) then
  1416. internalerror(201001120);
  1417. if hmodule.unit_index=current_filepos.moduleindex then
  1418. begin
  1419. preproc_consume(_POINT);
  1420. current_scanner.skipspace;
  1421. if srsym.typ=namespacesym then
  1422. begin
  1423. ns:=srsym.name;
  1424. nssym:=srsym;
  1425. while assigned(srsym) and (srsym.typ=namespacesym) do
  1426. begin
  1427. { we have a namespace. the next identifier should be either a namespace or a unit }
  1428. searchsym_in_module(hmodule,ns+'.'+current_scanner.preproc_pattern,srsym,srsymtable);
  1429. if assigned(srsym) and (srsym.typ in [namespacesym,unitsym]) then
  1430. begin
  1431. ns:=ns+'.'+current_scanner.preproc_pattern;
  1432. nssym:=srsym;
  1433. preproc_consume(_ID);
  1434. current_scanner.skipspace;
  1435. preproc_consume(_POINT);
  1436. current_scanner.skipspace;
  1437. end;
  1438. end;
  1439. { check if there is a hidden unit with this pattern in the namespace }
  1440. if not assigned(srsym) and
  1441. assigned(nssym) and (nssym.typ=namespacesym) and assigned(tnamespacesym(nssym).unitsym) then
  1442. srsym:=tnamespacesym(nssym).unitsym;
  1443. if assigned(srsym) and (srsym.typ<>unitsym) then
  1444. internalerror(201108260);
  1445. if not assigned(srsym) then
  1446. begin
  1447. result:=true;
  1448. srsymtable:=nil;
  1449. exit;
  1450. end;
  1451. end;
  1452. case current_scanner.preproc_token of
  1453. _ID:
  1454. { system.char? (char=widechar comes from the implicit
  1455. uuchar unit -> override) }
  1456. if (current_scanner.preproc_pattern='CHAR') and
  1457. (tmodule(tunitsym(srsym).module).globalsymtable=systemunit) then
  1458. begin
  1459. if m_default_unicodestring in current_settings.modeswitches then
  1460. searchsym_in_module(tunitsym(srsym).module,'WIDECHAR',srsym,srsymtable)
  1461. else
  1462. searchsym_in_module(tunitsym(srsym).module,'ANSICHAR',srsym,srsymtable)
  1463. end
  1464. else
  1465. searchsym_in_module(tunitsym(srsym).module,current_scanner.preproc_pattern,srsym,srsymtable);
  1466. _STRING:
  1467. begin
  1468. { system.string? }
  1469. if tmodule(tunitsym(srsym).module).globalsymtable=systemunit then
  1470. begin
  1471. if cs_refcountedstrings in current_settings.localswitches then
  1472. begin
  1473. if m_default_unicodestring in current_settings.modeswitches then
  1474. searchsym_in_module(tunitsym(srsym).module,'UNICODESTRING',srsym,srsymtable)
  1475. else
  1476. searchsym_in_module(tunitsym(srsym).module,'ANSISTRING',srsym,srsymtable)
  1477. end
  1478. else
  1479. searchsym_in_module(tunitsym(srsym).module,'SHORTSTRING',srsym,srsymtable);
  1480. tokentoconsume:=_STRING;
  1481. end;
  1482. end
  1483. else
  1484. ;
  1485. end;
  1486. end
  1487. else
  1488. begin
  1489. srsym:=nil;
  1490. srsymtable:=nil;
  1491. end;
  1492. result:=true;
  1493. end;
  1494. end;
  1495. procedure try_consume_nestedsym(var srsym:tsym;var srsymtable:TSymtable);
  1496. var
  1497. def:tdef;
  1498. tokentoconsume:ttoken;
  1499. found:boolean;
  1500. begin
  1501. found:=try_consume_unitsym(srsym,srsymtable,tokentoconsume);
  1502. if found then
  1503. begin
  1504. preproc_consume(tokentoconsume);
  1505. current_scanner.skipspace;
  1506. end;
  1507. while (current_scanner.preproc_token=_POINT) do
  1508. begin
  1509. if assigned(srsym)and(srsym.typ=typesym) then
  1510. begin
  1511. def:=ttypesym(srsym).typedef;
  1512. if is_class_or_object(def) or is_record(def) or is_java_class_or_interface(def) then
  1513. begin
  1514. preproc_consume(_POINT);
  1515. current_scanner.skipspace;
  1516. if def.typ=objectdef then
  1517. found:=searchsym_in_class(tobjectdef(def),tobjectdef(def),current_scanner.preproc_pattern,srsym,srsymtable,[ssf_search_helper])
  1518. else
  1519. found:=searchsym_in_record(trecorddef(def),current_scanner.preproc_pattern,srsym,srsymtable);
  1520. if not found then
  1521. begin
  1522. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  1523. exit;
  1524. end;
  1525. preproc_consume(_ID);
  1526. current_scanner.skipspace;
  1527. end
  1528. else
  1529. begin
  1530. Message(sym_e_type_must_be_rec_or_object_or_class);
  1531. exit;
  1532. end;
  1533. end
  1534. else
  1535. begin
  1536. Message(type_e_type_id_expected);
  1537. exit;
  1538. end;
  1539. end;
  1540. end;
  1541. function preproc_substitutedtoken(const basesearchstr:string;eval:Boolean):texprvalue;
  1542. { Currently this parses identifiers as well as numbers.
  1543. The result from this procedure can either be that the token
  1544. itself is a value, or that it is a compile time variable/macro,
  1545. which then is substituted for another value (for macros
  1546. recursivelly substituted).}
  1547. var
  1548. hs: string;
  1549. mac: tmacro;
  1550. macrocount,
  1551. len: integer;
  1552. foundmacro: boolean;
  1553. searchstr: pshortstring;
  1554. searchstr2store: string;
  1555. begin
  1556. if not eval then
  1557. begin
  1558. result:=texprvalue.create_str(basesearchstr);
  1559. exit;
  1560. end;
  1561. searchstr := @basesearchstr;
  1562. mac:=nil;
  1563. foundmacro:=false;
  1564. { Substitue macros and compiler variables with their content/value.
  1565. For real macros also do recursive substitution. }
  1566. macrocount:=0;
  1567. repeat
  1568. mac:=tmacro(search_macro(searchstr^));
  1569. inc(macrocount);
  1570. if macrocount>max_macro_nesting then
  1571. begin
  1572. Message(scan_w_macro_too_deep);
  1573. break;
  1574. end;
  1575. if assigned(mac) and mac.defined then
  1576. if assigned(mac.buftext) then
  1577. begin
  1578. if mac.buflen>255 then
  1579. begin
  1580. len:=255;
  1581. Message(scan_w_macro_cut_after_255_chars);
  1582. end
  1583. else
  1584. len:=mac.buflen;
  1585. hs[0]:=char(len);
  1586. move(mac.buftext^,hs[1],len);
  1587. searchstr2store:=upcase(hs);
  1588. searchstr:=@searchstr2store;
  1589. mac.is_used:=true;
  1590. foundmacro:=true;
  1591. end
  1592. else
  1593. begin
  1594. Message1(scan_e_error_macro_lacks_value,searchstr^);
  1595. break;
  1596. end
  1597. else
  1598. break;
  1599. if mac.is_compiler_var then
  1600. break;
  1601. until false;
  1602. { At this point, result do contain the value. Do some decoding and
  1603. determine the type.}
  1604. result:=texprvalue.try_parse_number(searchstr^);
  1605. if not assigned(result) then
  1606. begin
  1607. if foundmacro and (searchstr^='FALSE') then
  1608. result:=texprvalue.create_bool(false)
  1609. else if foundmacro and (searchstr^='TRUE') then
  1610. result:=texprvalue.create_bool(true)
  1611. else if (m_mac in current_settings.modeswitches) and
  1612. (not assigned(mac) or not mac.defined) and
  1613. (macrocount = 1) then
  1614. begin
  1615. {Errors in mode mac is issued here. For non macpas modes there is
  1616. more liberty, but the error will eventually be caught at a later stage.}
  1617. Message1(scan_e_error_macro_undefined,searchstr^);
  1618. result:=texprvalue.create_str(searchstr^); { just to have something }
  1619. end
  1620. else
  1621. result:=texprvalue.create_str(searchstr^);
  1622. end;
  1623. end;
  1624. function preproc_factor(eval: Boolean):texprvalue;
  1625. var
  1626. hs,countstr,storedpattern: string;
  1627. mac: tmacro;
  1628. srsym : tsym;
  1629. srsymtable : TSymtable;
  1630. hdef : TDef;
  1631. l : longint;
  1632. hasKlammer: Boolean;
  1633. exprvalue:texprvalue;
  1634. ns:tnormalset;
  1635. begin
  1636. result:=nil;
  1637. hasKlammer:=false;
  1638. if current_scanner.preproc_token=_ID then
  1639. begin
  1640. if current_scanner.preproc_pattern='DEFINED' then
  1641. begin
  1642. preproc_consume(_ID);
  1643. current_scanner.skipspace;
  1644. if current_scanner.preproc_token =_LKLAMMER then
  1645. begin
  1646. preproc_consume(_LKLAMMER);
  1647. current_scanner.skipspace;
  1648. hasKlammer:= true;
  1649. end
  1650. else if (m_mac in current_settings.modeswitches) then
  1651. hasKlammer:= false
  1652. else
  1653. Message(scan_e_error_in_preproc_expr);
  1654. if current_scanner.preproc_token =_ID then
  1655. begin
  1656. hs := current_scanner.preproc_pattern;
  1657. mac := tmacro(search_macro(hs));
  1658. if assigned(mac) and mac.defined then
  1659. begin
  1660. result:=texprvalue.create_bool(true);
  1661. mac.is_used:=true;
  1662. end
  1663. else
  1664. result:=texprvalue.create_bool(false);
  1665. preproc_consume(_ID);
  1666. current_scanner.skipspace;
  1667. end
  1668. else
  1669. Message(scan_e_error_in_preproc_expr);
  1670. if hasKlammer then
  1671. if current_scanner.preproc_token =_RKLAMMER then
  1672. preproc_consume(_RKLAMMER)
  1673. else
  1674. Message(scan_e_error_in_preproc_expr);
  1675. end
  1676. else
  1677. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  1678. begin
  1679. preproc_consume(_ID);
  1680. current_scanner.skipspace;
  1681. if current_scanner.preproc_token =_ID then
  1682. begin
  1683. hs := current_scanner.preproc_pattern;
  1684. mac := tmacro(search_macro(hs));
  1685. if assigned(mac) then
  1686. begin
  1687. result:=texprvalue.create_bool(false);
  1688. mac.is_used:=true;
  1689. end
  1690. else
  1691. result:=texprvalue.create_bool(true);
  1692. preproc_consume(_ID);
  1693. current_scanner.skipspace;
  1694. end
  1695. else
  1696. Message(scan_e_error_in_preproc_expr);
  1697. end
  1698. else
  1699. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  1700. begin
  1701. preproc_consume(_ID);
  1702. current_scanner.skipspace;
  1703. if current_scanner.preproc_token =_LKLAMMER then
  1704. begin
  1705. preproc_consume(_LKLAMMER);
  1706. current_scanner.skipspace;
  1707. end
  1708. else
  1709. Message(scan_e_error_in_preproc_expr);
  1710. if not (current_scanner.preproc_token = _ID) then
  1711. Message(scan_e_error_in_preproc_expr);
  1712. hs:=current_scanner.preproc_pattern;
  1713. if (length(hs) > 1) then
  1714. {This is allowed in Metrowerks Pascal}
  1715. Message(scan_e_error_in_preproc_expr)
  1716. else
  1717. begin
  1718. if CheckSwitch(hs[1],'+') then
  1719. result:=texprvalue.create_bool(true)
  1720. else
  1721. result:=texprvalue.create_bool(false);
  1722. end;
  1723. preproc_consume(_ID);
  1724. current_scanner.skipspace;
  1725. if current_scanner.preproc_token =_RKLAMMER then
  1726. preproc_consume(_RKLAMMER)
  1727. else
  1728. Message(scan_e_error_in_preproc_expr);
  1729. end
  1730. else
  1731. if current_scanner.preproc_pattern='SIZEOF' then
  1732. begin
  1733. preproc_consume(_ID);
  1734. current_scanner.skipspace;
  1735. if current_scanner.preproc_token =_LKLAMMER then
  1736. begin
  1737. preproc_consume(_LKLAMMER);
  1738. current_scanner.skipspace;
  1739. end
  1740. else
  1741. Message(scan_e_preproc_syntax_error);
  1742. storedpattern:=current_scanner.preproc_pattern;
  1743. preproc_consume(_ID);
  1744. current_scanner.skipspace;
  1745. if eval then
  1746. if searchsym(storedpattern,srsym,srsymtable) then
  1747. begin
  1748. try_consume_nestedsym(srsym,srsymtable);
  1749. l:=0;
  1750. if assigned(srsym) then
  1751. case srsym.typ of
  1752. staticvarsym,
  1753. localvarsym,
  1754. paravarsym :
  1755. l:=tabstractvarsym(srsym).getsize;
  1756. typesym:
  1757. l:=ttypesym(srsym).typedef.size;
  1758. else
  1759. Message(scan_e_error_in_preproc_expr);
  1760. end;
  1761. result:=texprvalue.create_int(l);
  1762. end
  1763. else
  1764. Message1(sym_e_id_not_found,storedpattern);
  1765. if current_scanner.preproc_token =_RKLAMMER then
  1766. preproc_consume(_RKLAMMER)
  1767. else
  1768. Message(scan_e_preproc_syntax_error);
  1769. end
  1770. else
  1771. if current_scanner.preproc_pattern='HIGH' then
  1772. begin
  1773. preproc_consume(_ID);
  1774. current_scanner.skipspace;
  1775. if current_scanner.preproc_token =_LKLAMMER then
  1776. begin
  1777. preproc_consume(_LKLAMMER);
  1778. current_scanner.skipspace;
  1779. end
  1780. else
  1781. Message(scan_e_preproc_syntax_error);
  1782. storedpattern:=current_scanner.preproc_pattern;
  1783. preproc_consume(_ID);
  1784. current_scanner.skipspace;
  1785. if eval then
  1786. if searchsym(storedpattern,srsym,srsymtable) then
  1787. begin
  1788. try_consume_nestedsym(srsym,srsymtable);
  1789. hdef:=nil;
  1790. hs:='';
  1791. l:=0;
  1792. if assigned(srsym) then
  1793. case srsym.typ of
  1794. staticvarsym,
  1795. localvarsym,
  1796. paravarsym :
  1797. hdef:=tabstractvarsym(srsym).vardef;
  1798. typesym:
  1799. hdef:=ttypesym(srsym).typedef;
  1800. else
  1801. Message(scan_e_error_in_preproc_expr);
  1802. end;
  1803. if assigned(hdef) then
  1804. begin
  1805. if hdef.typ=setdef then
  1806. hdef:=tsetdef(hdef).elementdef;
  1807. case hdef.typ of
  1808. orddef:
  1809. with torddef(hdef).high do
  1810. if signed then
  1811. result:=texprvalue.create_int(svalue)
  1812. else
  1813. result:=texprvalue.create_uint(uvalue);
  1814. enumdef:
  1815. result:=texprvalue.create_int(tenumdef(hdef).maxval);
  1816. arraydef:
  1817. if is_open_array(hdef) or is_array_of_const(hdef) or is_dynamic_array(hdef) then
  1818. Message(type_e_mismatch)
  1819. else
  1820. result:=texprvalue.create_int(tarraydef(hdef).highrange);
  1821. stringdef:
  1822. if is_open_string(hdef) or is_ansistring(hdef) or is_wide_or_unicode_string(hdef) then
  1823. Message(type_e_mismatch)
  1824. else
  1825. result:=texprvalue.create_int(tstringdef(hdef).len);
  1826. else
  1827. Message(type_e_mismatch);
  1828. end;
  1829. end;
  1830. end
  1831. else
  1832. Message1(sym_e_id_not_found,storedpattern);
  1833. if current_scanner.preproc_token =_RKLAMMER then
  1834. preproc_consume(_RKLAMMER)
  1835. else
  1836. Message(scan_e_preproc_syntax_error);
  1837. end
  1838. else
  1839. if current_scanner.preproc_pattern='DECLARED' then
  1840. begin
  1841. preproc_consume(_ID);
  1842. current_scanner.skipspace;
  1843. if current_scanner.preproc_token =_LKLAMMER then
  1844. begin
  1845. preproc_consume(_LKLAMMER);
  1846. current_scanner.skipspace;
  1847. end
  1848. else
  1849. Message(scan_e_error_in_preproc_expr);
  1850. if current_scanner.preproc_token =_ID then
  1851. begin
  1852. hs := upper(current_scanner.preproc_pattern);
  1853. preproc_consume(_ID);
  1854. current_scanner.skipspace;
  1855. if current_scanner.preproc_token in [_LT,_LSHARPBRACKET] then
  1856. begin
  1857. l:=1;
  1858. preproc_consume(current_scanner.preproc_token);
  1859. current_scanner.skipspace;
  1860. while current_scanner.preproc_token=_COMMA do
  1861. begin
  1862. inc(l);
  1863. preproc_consume(_COMMA);
  1864. current_scanner.skipspace;
  1865. end;
  1866. if not (current_scanner.preproc_token in [_GT,_RSHARPBRACKET]) then
  1867. Message(scan_e_error_in_preproc_expr)
  1868. else
  1869. preproc_consume(current_scanner.preproc_token);
  1870. str(l,countstr);
  1871. hs:=hs+'$'+countstr;
  1872. end
  1873. else
  1874. { special case: <> }
  1875. if current_scanner.preproc_token=_NE then
  1876. begin
  1877. hs:=hs+'$1';
  1878. preproc_consume(_NE);
  1879. end;
  1880. current_scanner.skipspace;
  1881. if searchsym(hs,srsym,srsymtable) then
  1882. begin
  1883. { TSomeGeneric<...> also adds a TSomeGeneric symbol }
  1884. if (sp_generic_dummy in srsym.symoptions) and
  1885. (srsym.typ=typesym) and
  1886. (
  1887. { mode delphi}
  1888. (ttypesym(srsym).typedef.typ in [undefineddef,errordef]) or
  1889. { non-delphi modes }
  1890. (df_generic in ttypesym(srsym).typedef.defoptions)
  1891. ) then
  1892. result:=texprvalue.create_bool(false)
  1893. else
  1894. result:=texprvalue.create_bool(true);
  1895. end
  1896. else
  1897. result:=texprvalue.create_bool(false);
  1898. end
  1899. else
  1900. Message(scan_e_error_in_preproc_expr);
  1901. if current_scanner.preproc_token =_RKLAMMER then
  1902. preproc_consume(_RKLAMMER)
  1903. else
  1904. Message(scan_e_error_in_preproc_expr);
  1905. end
  1906. else
  1907. if current_scanner.preproc_pattern='ORD' then
  1908. begin
  1909. preproc_consume(_ID);
  1910. current_scanner.skipspace;
  1911. if current_scanner.preproc_token =_LKLAMMER then
  1912. begin
  1913. preproc_consume(_LKLAMMER);
  1914. current_scanner.skipspace;
  1915. end
  1916. else
  1917. Message(scan_e_preproc_syntax_error);
  1918. exprvalue:=preproc_factor(eval);
  1919. if eval then
  1920. begin
  1921. if is_ordinal(exprvalue.def) then
  1922. result:=texprvalue.create_int(exprvalue.asInt)
  1923. else
  1924. begin
  1925. exprvalue.error('Ordinal','ORD');
  1926. result:=texprvalue.create_int(0);
  1927. end;
  1928. end
  1929. else
  1930. result:=texprvalue.create_int(0);
  1931. exprvalue.free;
  1932. if current_scanner.preproc_token =_RKLAMMER then
  1933. preproc_consume(_RKLAMMER)
  1934. else
  1935. Message(scan_e_error_in_preproc_expr);
  1936. end
  1937. else
  1938. if current_scanner.preproc_pattern='NOT' then
  1939. begin
  1940. preproc_consume(_ID);
  1941. exprvalue:=preproc_factor(eval);
  1942. if eval then
  1943. result:=exprvalue.evaluate(nil,_OP_NOT)
  1944. else
  1945. result:=texprvalue.create_bool(false); {Just to have something}
  1946. exprvalue.free;
  1947. end
  1948. else
  1949. if (current_scanner.preproc_pattern='TRUE') then
  1950. begin
  1951. result:=texprvalue.create_bool(true);
  1952. preproc_consume(_ID);
  1953. end
  1954. else
  1955. if (current_scanner.preproc_pattern='FALSE') then
  1956. begin
  1957. result:=texprvalue.create_bool(false);
  1958. preproc_consume(_ID);
  1959. end
  1960. else
  1961. begin
  1962. storedpattern:=current_scanner.preproc_pattern;
  1963. preproc_consume(_ID);
  1964. current_scanner.skipspace;
  1965. { first look for a macros/int/float }
  1966. result:=preproc_substitutedtoken(storedpattern,eval);
  1967. if eval and (result.consttyp=conststring) then
  1968. begin
  1969. if searchsym(storedpattern,srsym,srsymtable) then
  1970. begin
  1971. try_consume_nestedsym(srsym,srsymtable);
  1972. if assigned(srsym) then
  1973. case srsym.typ of
  1974. constsym:
  1975. begin
  1976. result.free;
  1977. result:=texprvalue.create_const(tconstsym(srsym));
  1978. tconstsym(srsym).IncRefCount;
  1979. end;
  1980. enumsym:
  1981. begin
  1982. result.free;
  1983. result:=texprvalue.create_int(tenumsym(srsym).value);
  1984. // tconstsym(srsym).IncRefCount;
  1985. end;
  1986. else
  1987. ;
  1988. end;
  1989. end
  1990. end
  1991. { skip id(<expr>) if expression must not be evaluated }
  1992. else if not(eval) and (result.consttyp=conststring) then
  1993. begin
  1994. if current_scanner.preproc_token =_LKLAMMER then
  1995. begin
  1996. preproc_consume(_LKLAMMER);
  1997. current_scanner.skipspace;
  1998. result:=preproc_factor(false);
  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. end;
  2005. end
  2006. end
  2007. else if current_scanner.preproc_token =_LKLAMMER then
  2008. begin
  2009. preproc_consume(_LKLAMMER);
  2010. result:=preproc_sub_expr(opcompare,eval);
  2011. preproc_consume(_RKLAMMER);
  2012. end
  2013. else if current_scanner.preproc_token = _LECKKLAMMER then
  2014. begin
  2015. preproc_consume(_LECKKLAMMER);
  2016. ns:=[];
  2017. while current_scanner.preproc_token in [_ID,_INTCONST] do
  2018. begin
  2019. exprvalue:=preproc_factor(eval);
  2020. include(ns,exprvalue.asInt);
  2021. if current_scanner.preproc_token = _COMMA then
  2022. preproc_consume(_COMMA);
  2023. end;
  2024. // TODO Add check of setElemType
  2025. preproc_consume(_RECKKLAMMER);
  2026. result:=texprvalue.create_set(ns);
  2027. end
  2028. else if current_scanner.preproc_token = _INTCONST then
  2029. begin
  2030. result:=texprvalue.try_parse_number(current_scanner.preproc_pattern);
  2031. if not assigned(result) then
  2032. begin
  2033. Message(parser_e_invalid_integer);
  2034. result:=texprvalue.create_int(1);
  2035. end;
  2036. preproc_consume(_INTCONST);
  2037. end
  2038. else if current_scanner.preproc_token = _CSTRING then
  2039. begin
  2040. result:=texprvalue.create_str(current_scanner.preproc_pattern);
  2041. preproc_consume(_CSTRING);
  2042. end
  2043. else if current_scanner.preproc_token = _REALNUMBER then
  2044. begin
  2045. result:=texprvalue.try_parse_real(current_scanner.preproc_pattern);
  2046. if not assigned(result) then
  2047. begin
  2048. Message(parser_e_error_in_real);
  2049. result:=texprvalue.create_real(1.0);
  2050. end;
  2051. preproc_consume(_REALNUMBER);
  2052. end
  2053. else
  2054. Message(scan_e_error_in_preproc_expr);
  2055. if not assigned(result) then
  2056. result:=texprvalue.create_error;
  2057. end;
  2058. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean): texprvalue;
  2059. var
  2060. hs1,hs2: texprvalue;
  2061. op: ttoken;
  2062. begin
  2063. if pred_level=highest_precedence then
  2064. result:=preproc_factor(eval)
  2065. else
  2066. result:=preproc_sub_expr(succ(pred_level),eval);
  2067. repeat
  2068. op:=current_scanner.preproc_token;
  2069. if (op in preproc_operators) and
  2070. (op in operator_levels[pred_level]) then
  2071. begin
  2072. hs1:=result;
  2073. preproc_consume(op);
  2074. if (op=_OP_OR) and hs1.isBoolean and hs1.asBool then
  2075. begin
  2076. { stop evaluation the rest of expression }
  2077. result:=texprvalue.create_bool(true);
  2078. if pred_level=highest_precedence then
  2079. hs2:=preproc_factor(false)
  2080. else
  2081. hs2:=preproc_sub_expr(succ(pred_level),false);
  2082. end
  2083. else if (op=_OP_AND) and hs1.isBoolean and not hs1.asBool then
  2084. begin
  2085. { stop evaluation the rest of expression }
  2086. result:=texprvalue.create_bool(false);
  2087. if pred_level=highest_precedence then
  2088. hs2:=preproc_factor(false)
  2089. else
  2090. hs2:=preproc_sub_expr(succ(pred_level),false);
  2091. end
  2092. else
  2093. begin
  2094. if pred_level=highest_precedence then
  2095. hs2:=preproc_factor(eval)
  2096. else
  2097. hs2:=preproc_sub_expr(succ(pred_level),eval);
  2098. if eval then
  2099. result:=hs1.evaluate(hs2,op)
  2100. else
  2101. result:=texprvalue.create_bool(false); {Just to have something}
  2102. end;
  2103. hs1.free;
  2104. hs2.free;
  2105. end
  2106. else
  2107. break;
  2108. until false;
  2109. end;
  2110. begin
  2111. current_scanner.in_preproc_comp_expr:=true;
  2112. current_scanner.skipspace;
  2113. { start preproc expression scanner }
  2114. current_scanner.preproc_token:=current_scanner.readpreproc;
  2115. preproc_comp_expr:=preproc_sub_expr(opcompare,true);
  2116. current_scanner.in_preproc_comp_expr:=false;
  2117. end;
  2118. function boolean_compile_time_expr(var valuedescr: string): Boolean;
  2119. var
  2120. hs: texprvalue;
  2121. begin
  2122. hs:=preproc_comp_expr;
  2123. if hs.isBoolean then
  2124. result:=hs.asBool
  2125. else
  2126. begin
  2127. hs.error('Boolean', 'IF or ELSEIF');
  2128. result:=false;
  2129. end;
  2130. valuedescr:=hs.asStr;
  2131. hs.free;
  2132. end;
  2133. procedure dir_if;
  2134. begin
  2135. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  2136. end;
  2137. procedure dir_elseif;
  2138. begin
  2139. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  2140. end;
  2141. procedure dir_define_impl(macstyle: boolean);
  2142. var
  2143. hs : string;
  2144. bracketcount : longint;
  2145. mac : tmacro;
  2146. macropos : longint;
  2147. macrobuffer : pmacrobuffer;
  2148. begin
  2149. current_scanner.skipspace;
  2150. hs:=current_scanner.readid;
  2151. if hs='' then
  2152. begin
  2153. Message(scan_e_emptymacroname);
  2154. exit;
  2155. end;
  2156. mac:=tmacro(search_macro(hs));
  2157. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  2158. begin
  2159. mac:=tmacro.create(hs);
  2160. mac.defined:=true;
  2161. current_module.localmacrosymtable.insertsym(mac);
  2162. end
  2163. else
  2164. begin
  2165. mac.defined:=true;
  2166. mac.is_compiler_var:=false;
  2167. { delete old definition }
  2168. if assigned(mac.buftext) then
  2169. begin
  2170. freemem(mac.buftext,mac.buflen);
  2171. mac.buftext:=nil;
  2172. end;
  2173. end;
  2174. Message1(parser_c_macro_defined,mac.name);
  2175. mac.is_used:=true;
  2176. if (cs_support_macro in current_settings.moduleswitches) then
  2177. begin
  2178. current_scanner.skipspace;
  2179. if not macstyle then
  2180. begin
  2181. { may be a macro? }
  2182. if c <> ':' then
  2183. exit;
  2184. current_scanner.readchar;
  2185. if c <> '=' then
  2186. exit;
  2187. current_scanner.readchar;
  2188. current_scanner.skipspace;
  2189. end;
  2190. { key words are never substituted }
  2191. if is_keyword(hs) then
  2192. Message(scan_e_keyword_cant_be_a_macro);
  2193. new(macrobuffer);
  2194. macropos:=0;
  2195. { parse macro, brackets are counted so it's possible
  2196. to have a $ifdef etc. in the macro }
  2197. bracketcount:=0;
  2198. repeat
  2199. case c of
  2200. '}' :
  2201. if (bracketcount=0) then
  2202. break
  2203. else
  2204. dec(bracketcount);
  2205. '{' :
  2206. inc(bracketcount);
  2207. #10,#13 :
  2208. current_scanner.linebreak;
  2209. #26 :
  2210. current_scanner.end_of_file;
  2211. end;
  2212. macrobuffer^[macropos]:=c;
  2213. inc(macropos);
  2214. if macropos>=maxmacrolen then
  2215. Message(scan_f_macro_buffer_overflow);
  2216. current_scanner.readchar;
  2217. until false;
  2218. { free buffer of macro ?}
  2219. if assigned(mac.buftext) then
  2220. freemem(mac.buftext,mac.buflen);
  2221. { get new mem }
  2222. getmem(mac.buftext,macropos);
  2223. mac.buflen:=macropos;
  2224. { copy the text }
  2225. move(macrobuffer^,mac.buftext^,macropos);
  2226. dispose(macrobuffer);
  2227. end
  2228. else
  2229. begin
  2230. { check if there is an assignment, then we need to give a
  2231. warning }
  2232. current_scanner.skipspace;
  2233. if c=':' then
  2234. begin
  2235. current_scanner.readchar;
  2236. if c='=' then
  2237. Message(scan_w_macro_support_turned_off);
  2238. end;
  2239. end;
  2240. end;
  2241. procedure dir_define;
  2242. begin
  2243. dir_define_impl(false);
  2244. end;
  2245. procedure dir_definec;
  2246. begin
  2247. dir_define_impl(true);
  2248. end;
  2249. procedure dir_setc;
  2250. var
  2251. hs : string;
  2252. mac : tmacro;
  2253. exprvalue: texprvalue;
  2254. begin
  2255. current_scanner.skipspace;
  2256. hs:=current_scanner.readid;
  2257. mac:=tmacro(search_macro(hs));
  2258. if not assigned(mac) or
  2259. (mac.owner <> current_module.localmacrosymtable) then
  2260. begin
  2261. mac:=tmacro.create(hs);
  2262. mac.defined:=true;
  2263. mac.is_compiler_var:=true;
  2264. current_module.localmacrosymtable.insertsym(mac);
  2265. end
  2266. else
  2267. begin
  2268. mac.defined:=true;
  2269. mac.is_compiler_var:=true;
  2270. { delete old definition }
  2271. if assigned(mac.buftext) then
  2272. begin
  2273. freemem(mac.buftext,mac.buflen);
  2274. mac.buftext:=nil;
  2275. end;
  2276. end;
  2277. Message1(parser_c_macro_defined,mac.name);
  2278. mac.is_used:=true;
  2279. { key words are never substituted }
  2280. if is_keyword(hs) then
  2281. Message(scan_e_keyword_cant_be_a_macro);
  2282. { macro assignment can be both := and = }
  2283. current_scanner.skipspace;
  2284. if c=':' then
  2285. current_scanner.readchar;
  2286. if c='=' then
  2287. begin
  2288. current_scanner.readchar;
  2289. exprvalue:=preproc_comp_expr;
  2290. if not is_boolean(exprvalue.def) and
  2291. not is_integer(exprvalue.def) then
  2292. exprvalue.error('Boolean, Integer', 'SETC');
  2293. hs:=exprvalue.asStr;
  2294. if length(hs) <> 0 then
  2295. begin
  2296. {If we are absolutely shure it is boolean, translate
  2297. to TRUE/FALSE to increase possibility to do future type check}
  2298. if exprvalue.isBoolean then
  2299. begin
  2300. if exprvalue.asBool then
  2301. hs:='TRUE'
  2302. else
  2303. hs:='FALSE';
  2304. end;
  2305. Message2(parser_c_macro_set_to,mac.name,hs);
  2306. { free buffer of macro ?}
  2307. if assigned(mac.buftext) then
  2308. freemem(mac.buftext,mac.buflen);
  2309. { get new mem }
  2310. getmem(mac.buftext,length(hs));
  2311. mac.buflen:=length(hs);
  2312. { copy the text }
  2313. move(hs[1],mac.buftext^,mac.buflen);
  2314. end
  2315. else
  2316. Message(scan_e_preproc_syntax_error);
  2317. exprvalue.free;
  2318. end
  2319. else
  2320. Message(scan_e_preproc_syntax_error);
  2321. end;
  2322. procedure dir_undef;
  2323. var
  2324. hs : string;
  2325. mac : tmacro;
  2326. begin
  2327. current_scanner.skipspace;
  2328. hs:=current_scanner.readid;
  2329. mac:=tmacro(search_macro(hs));
  2330. if not assigned(mac) or
  2331. (mac.owner <> current_module.localmacrosymtable) then
  2332. begin
  2333. mac:=tmacro.create(hs);
  2334. mac.defined:=false;
  2335. current_module.localmacrosymtable.insertsym(mac);
  2336. end
  2337. else
  2338. begin
  2339. mac.defined:=false;
  2340. mac.is_compiler_var:=false;
  2341. { delete old definition }
  2342. if assigned(mac.buftext) then
  2343. begin
  2344. freemem(mac.buftext,mac.buflen);
  2345. mac.buftext:=nil;
  2346. end;
  2347. end;
  2348. Message1(parser_c_macro_undefined,mac.name);
  2349. mac.is_used:=true;
  2350. end;
  2351. procedure dir_include;
  2352. function findincludefile(const path,name:TCmdStr;var foundfile:TCmdStr):boolean;
  2353. var
  2354. found : boolean;
  2355. hpath : TCmdStr;
  2356. begin
  2357. (* look for the include file
  2358. If path was absolute and specified as part of {$I } then
  2359. 1. specified path
  2360. else
  2361. 1. path of current inputfile,current dir
  2362. 2. local includepath
  2363. 3. global includepath
  2364. -- Check mantis #13461 before changing this *)
  2365. found:=false;
  2366. foundfile:='';
  2367. hpath:='';
  2368. if path_absolute(path) then
  2369. begin
  2370. found:=FindFile(name,path,true,foundfile);
  2371. end
  2372. else
  2373. begin
  2374. hpath:=current_scanner.inputfile.path+';'+CurDirRelPath(source_info);
  2375. found:=FindFile(path+name, hpath,true,foundfile);
  2376. if not found then
  2377. found:=current_module.localincludesearchpath.FindFile(path+name,true,foundfile);
  2378. if not found then
  2379. found:=includesearchpath.FindFile(path+name,true,foundfile);
  2380. end;
  2381. result:=found;
  2382. end;
  2383. var
  2384. foundfile : TCmdStr;
  2385. path,
  2386. name,
  2387. hs : tpathstr;
  2388. args : string;
  2389. hp : tinputfile;
  2390. found : boolean;
  2391. macroIsString : boolean;
  2392. begin
  2393. current_scanner.skipspace;
  2394. args:=current_scanner.readcomment;
  2395. hs:=GetToken(args,' ');
  2396. if hs='' then
  2397. exit;
  2398. if (hs[1]='%') then
  2399. begin
  2400. { case insensitive }
  2401. hs:=upper(hs);
  2402. { remove %'s }
  2403. Delete(hs,1,1);
  2404. if hs[length(hs)]='%' then
  2405. Delete(hs,length(hs),1);
  2406. { save old }
  2407. path:=hs;
  2408. { first check for internal macros }
  2409. macroIsString:=true;
  2410. case hs of
  2411. 'TIME':
  2412. if timestr<>'' then
  2413. hs:=timestr
  2414. else
  2415. hs:=gettimestr;
  2416. 'DATE':
  2417. if datestr<>'' then
  2418. hs:=datestr
  2419. else
  2420. hs:=getdatestr;
  2421. 'DATEYEAR':
  2422. begin
  2423. hs:=tostr(startsystime.Year);
  2424. macroIsString:=false;
  2425. end;
  2426. 'DATEMONTH':
  2427. begin
  2428. hs:=tostr(startsystime.Month);
  2429. macroIsString:=false;
  2430. end;
  2431. 'DATEDAY':
  2432. begin
  2433. hs:=tostr(startsystime.Day);
  2434. macroIsString:=false;
  2435. end;
  2436. 'TIMEHOUR':
  2437. begin
  2438. hs:=tostr(startsystime.Hour);
  2439. macroIsString:=false;
  2440. end;
  2441. 'TIMEMINUTE':
  2442. begin
  2443. hs:=tostr(startsystime.Minute);
  2444. macroIsString:=false;
  2445. end;
  2446. 'TIMESECOND':
  2447. begin
  2448. hs:=tostr(startsystime.Second);
  2449. macroIsString:=false;
  2450. end;
  2451. 'FILE':
  2452. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex);
  2453. 'LINE':
  2454. hs:=tostr(current_filepos.line);
  2455. 'LINENUM':
  2456. begin
  2457. hs:=tostr(current_filepos.line);
  2458. macroIsString:=false;
  2459. end;
  2460. 'FPCVERSION':
  2461. hs:=version_string;
  2462. 'FPCDATE':
  2463. hs:=date_string;
  2464. 'FPCTARGET':
  2465. hs:=target_cpu_string;
  2466. 'FPCTARGETCPU':
  2467. hs:=target_cpu_string;
  2468. 'FPCTARGETOS':
  2469. hs:=target_info.shortname;
  2470. 'CURRENTROUTINE':
  2471. hs:=current_procinfo.procdef.procsym.RealName;
  2472. else
  2473. hs:=GetEnvironmentVariable(hs);
  2474. end;
  2475. if hs='' then
  2476. Message1(scan_w_include_env_not_found,path);
  2477. { make it a stringconst }
  2478. if macroIsString then
  2479. hs:=''''+hs+'''';
  2480. current_scanner.substitutemacro(path,@hs[1],length(hs),
  2481. current_scanner.line_no,current_scanner.inputfile.ref_index,false);
  2482. end
  2483. else
  2484. begin
  2485. hs:=FixFileName(hs);
  2486. path:=ExtractFilePath(hs);
  2487. name:=ExtractFileName(hs);
  2488. { Special case for Delphi compatibility: '*' has to be replaced
  2489. by the file name of the current source file. }
  2490. if (length(name)>=1) and
  2491. (name[1]='*') then
  2492. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  2493. { try to find the file }
  2494. found:=findincludefile(path,name,foundfile);
  2495. if (not found) and (ExtractFileExt(name)='') then
  2496. begin
  2497. { try default extensions .inc , .pp and .pas }
  2498. if (not found) then
  2499. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  2500. if (not found) then
  2501. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  2502. if (not found) then
  2503. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  2504. end;
  2505. { if the name ends in dot, try without the dot }
  2506. if (not found) and (ExtractFileExt(name)=ExtensionSeparator) and (Length(name)>=2) then
  2507. found:=findincludefile(path,Copy(name,1,Length(name)-1),foundfile);
  2508. if current_scanner.inputfilecount<max_include_nesting then
  2509. begin
  2510. inc(current_scanner.inputfilecount);
  2511. { we need to reread the current char }
  2512. dec(current_scanner.inputpointer);
  2513. { reset c }
  2514. c:=#0;
  2515. { shutdown current file }
  2516. current_scanner.tempcloseinputfile;
  2517. { load new file }
  2518. hp:=do_openinputfile(foundfile);
  2519. hp.inc_path:=path;
  2520. current_scanner.addfile(hp);
  2521. current_module.sourcefiles.register_file(hp);
  2522. if (not found) then
  2523. Message1(scan_f_cannot_open_includefile,hs);
  2524. if (not current_scanner.openinputfile) then
  2525. Message1(scan_f_cannot_open_includefile,hs);
  2526. Message1(scan_t_start_include_file,current_scanner.inputfile.path+current_scanner.inputfile.name);
  2527. current_scanner.reload;
  2528. end
  2529. else
  2530. Message(scan_f_include_deep_ten);
  2531. end;
  2532. end;
  2533. {*****************************************************************************
  2534. Preprocessor writing
  2535. *****************************************************************************}
  2536. {$ifdef PREPROCWRITE}
  2537. constructor tpreprocfile.create(const fn:string);
  2538. begin
  2539. inherited create;
  2540. { open outputfile }
  2541. assign(f,fn);
  2542. {$push}{$I-}
  2543. rewrite(f);
  2544. {$pop}
  2545. if ioresult<>0 then
  2546. Comment(V_Fatal,'can''t create file '+fn);
  2547. getmem(buf,preprocbufsize);
  2548. settextbuf(f,buf^,preprocbufsize);
  2549. { reset }
  2550. eolfound:=false;
  2551. spacefound:=false;
  2552. end;
  2553. destructor tpreprocfile.destroy;
  2554. begin
  2555. close(f);
  2556. freemem(buf,preprocbufsize);
  2557. end;
  2558. procedure tpreprocfile.add(const s:string);
  2559. begin
  2560. write(f,s);
  2561. end;
  2562. procedure tpreprocfile.addspace;
  2563. begin
  2564. if eolfound then
  2565. begin
  2566. writeln(f,'');
  2567. eolfound:=false;
  2568. spacefound:=false;
  2569. end
  2570. else
  2571. if spacefound then
  2572. begin
  2573. write(f,' ');
  2574. spacefound:=false;
  2575. end;
  2576. end;
  2577. {$endif PREPROCWRITE}
  2578. {*****************************************************************************
  2579. TPreProcStack
  2580. *****************************************************************************}
  2581. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  2582. begin
  2583. accept:=a;
  2584. typ:=atyp;
  2585. next:=n;
  2586. end;
  2587. {*****************************************************************************
  2588. TReplayStack
  2589. *****************************************************************************}
  2590. constructor treplaystack.Create(atoken:ttoken;aidtoken:ttoken;
  2591. const aorgpattern,apattern:string;const acstringpattern:ansistring;
  2592. apatternw:pcompilerwidestring;asettings:tsettings;
  2593. atokenbuf:tdynamicarray;change_endian:boolean;anext:treplaystack);
  2594. begin
  2595. token:=atoken;
  2596. idtoken:=aidtoken;
  2597. orgpattern:=aorgpattern;
  2598. pattern:=apattern;
  2599. cstringpattern:=acstringpattern;
  2600. initwidestring(patternw);
  2601. if assigned(apatternw) then
  2602. begin
  2603. setlengthwidestring(patternw,apatternw^.len);
  2604. move(apatternw^.data^,patternw^.data^,apatternw^.len*sizeof(tcompilerwidechar));
  2605. end;
  2606. settings:=asettings;
  2607. tokenbuf:=atokenbuf;
  2608. tokenbuf_needs_swapping:=change_endian;
  2609. next:=anext;
  2610. end;
  2611. destructor treplaystack.destroy;
  2612. begin
  2613. donewidestring(patternw);
  2614. end;
  2615. {*****************************************************************************
  2616. TDirectiveItem
  2617. *****************************************************************************}
  2618. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2619. begin
  2620. inherited Create(AList,n);
  2621. is_conditional:=false;
  2622. proc:=p;
  2623. end;
  2624. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2625. begin
  2626. inherited Create(AList,n);
  2627. is_conditional:=true;
  2628. proc:=p;
  2629. end;
  2630. {****************************************************************************
  2631. TSCANNERFILE
  2632. ****************************************************************************}
  2633. constructor tscannerfile.create(const fn:string; is_macro: boolean = false);
  2634. begin
  2635. inputfile:=do_openinputfile(fn);
  2636. if is_macro then
  2637. inputfile.is_macro:=true;
  2638. if assigned(current_module) then
  2639. current_module.sourcefiles.register_file(inputfile);
  2640. { reset localinput }
  2641. c:=#0;
  2642. inputbuffer:=nil;
  2643. inputpointer:=nil;
  2644. inputstart:=0;
  2645. { reset scanner }
  2646. preprocstack:=nil;
  2647. replaystack:=nil;
  2648. comment_level:=0;
  2649. yylexcount:=0;
  2650. block_type:=bt_general;
  2651. line_no:=0;
  2652. lastlinepos:=0;
  2653. lasttokenpos:=0;
  2654. nexttokenpos:=0;
  2655. lasttoken:=NOTOKEN;
  2656. nexttoken:=NOTOKEN;
  2657. ignoredirectives:=TFPHashList.Create;
  2658. change_endian_for_replay:=false;
  2659. end;
  2660. procedure tscannerfile.firstfile;
  2661. begin
  2662. { load block }
  2663. if not openinputfile then
  2664. Message1(scan_f_cannot_open_input,inputfile.name);
  2665. reload;
  2666. end;
  2667. destructor tscannerfile.destroy;
  2668. begin
  2669. if assigned(current_module) and
  2670. (current_module.state=ms_compiled) and
  2671. (status.errorcount=0) then
  2672. checkpreprocstack
  2673. else
  2674. begin
  2675. while assigned(preprocstack) do
  2676. poppreprocstack;
  2677. end;
  2678. while assigned(replaystack) do
  2679. popreplaystack;
  2680. if not inputfile.closed then
  2681. closeinputfile;
  2682. if inputfile.is_macro then
  2683. inputfile.free;
  2684. ignoredirectives.free;
  2685. end;
  2686. function tscannerfile.openinputfile:boolean;
  2687. begin
  2688. openinputfile:=inputfile.open;
  2689. { load buffer }
  2690. inputbuffer:=inputfile.buf;
  2691. inputpointer:=inputfile.buf;
  2692. inputstart:=inputfile.bufstart;
  2693. { line }
  2694. line_no:=0;
  2695. lastlinepos:=0;
  2696. lasttokenpos:=0;
  2697. nexttokenpos:=0;
  2698. end;
  2699. procedure tscannerfile.closeinputfile;
  2700. begin
  2701. inputfile.close;
  2702. { reset buffer }
  2703. inputbuffer:=nil;
  2704. inputpointer:=nil;
  2705. inputstart:=0;
  2706. { reset line }
  2707. line_no:=0;
  2708. lastlinepos:=0;
  2709. lasttokenpos:=0;
  2710. nexttokenpos:=0;
  2711. end;
  2712. function tscannerfile.tempopeninputfile:boolean;
  2713. begin
  2714. tempopeninputfile:=false;
  2715. if inputfile.is_macro then
  2716. exit;
  2717. tempopeninputfile:=inputfile.tempopen;
  2718. { reload buffer }
  2719. inputbuffer:=inputfile.buf;
  2720. inputpointer:=inputfile.buf;
  2721. inputstart:=inputfile.bufstart;
  2722. end;
  2723. procedure tscannerfile.tempcloseinputfile;
  2724. begin
  2725. if inputfile.closed or inputfile.is_macro then
  2726. exit;
  2727. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  2728. inputfile.tempclose;
  2729. { reset buffer }
  2730. inputbuffer:=nil;
  2731. inputpointer:=nil;
  2732. inputstart:=0;
  2733. end;
  2734. procedure tscannerfile.saveinputfile;
  2735. begin
  2736. inputfile.saveinputpointer:=inputpointer;
  2737. inputfile.savelastlinepos:=lastlinepos;
  2738. inputfile.saveline_no:=line_no;
  2739. end;
  2740. procedure tscannerfile.restoreinputfile;
  2741. begin
  2742. inputbuffer:=inputfile.buf;
  2743. inputpointer:=inputfile.saveinputpointer;
  2744. lastlinepos:=inputfile.savelastlinepos;
  2745. line_no:=inputfile.saveline_no;
  2746. if not inputfile.is_macro then
  2747. parser_current_file:=inputfile.name;
  2748. end;
  2749. procedure tscannerfile.nextfile;
  2750. var
  2751. to_dispose : tinputfile;
  2752. begin
  2753. if assigned(inputfile.next) then
  2754. begin
  2755. if inputfile.is_macro then
  2756. begin
  2757. to_dispose:=inputfile;
  2758. dec(macro_nesting_depth);
  2759. end
  2760. else
  2761. begin
  2762. to_dispose:=nil;
  2763. dec(inputfilecount);
  2764. end;
  2765. { we can allways close the file, no ? }
  2766. inputfile.close;
  2767. inputfile:=inputfile.next;
  2768. if assigned(to_dispose) then
  2769. to_dispose.free;
  2770. restoreinputfile;
  2771. end;
  2772. end;
  2773. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  2774. begin
  2775. if not assigned(buf) then
  2776. internalerror(200511172);
  2777. if assigned(recordtokenbuf) then
  2778. internalerror(200511173);
  2779. recordtokenbuf:=buf;
  2780. fillchar(last_settings,sizeof(last_settings),0);
  2781. last_message:=nil;
  2782. fillchar(last_filepos,sizeof(last_filepos),0);
  2783. end;
  2784. procedure tscannerfile.stoprecordtokens;
  2785. begin
  2786. if not assigned(recordtokenbuf) then
  2787. internalerror(200511174);
  2788. recordtokenbuf:=nil;
  2789. end;
  2790. function tscannerfile.is_recording_tokens: boolean;
  2791. begin
  2792. result:=assigned(recordtokenbuf);
  2793. end;
  2794. procedure tscannerfile.writetoken(t : ttoken);
  2795. var
  2796. b : byte;
  2797. begin
  2798. if ord(t)>$7f then
  2799. begin
  2800. b:=(ord(t) shr 8) or $80;
  2801. recordtokenbuf.write(b,1);
  2802. end;
  2803. b:=ord(t) and $ff;
  2804. recordtokenbuf.write(b,1);
  2805. end;
  2806. procedure tscannerfile.tokenwritesizeint(val : asizeint);
  2807. begin
  2808. recordtokenbuf.write(val,sizeof(asizeint));
  2809. end;
  2810. procedure tscannerfile.tokenwritelongint(val : longint);
  2811. begin
  2812. recordtokenbuf.write(val,sizeof(longint));
  2813. end;
  2814. procedure tscannerfile.tokenwriteshortint(val : shortint);
  2815. begin
  2816. recordtokenbuf.write(val,sizeof(shortint));
  2817. end;
  2818. procedure tscannerfile.tokenwriteword(val : word);
  2819. begin
  2820. recordtokenbuf.write(val,sizeof(word));
  2821. end;
  2822. procedure tscannerfile.tokenwritelongword(val : longword);
  2823. begin
  2824. recordtokenbuf.write(val,sizeof(longword));
  2825. end;
  2826. function tscannerfile.tokenreadsizeint : asizeint;
  2827. var
  2828. val : asizeint;
  2829. begin
  2830. replaytokenbuf.read(val,sizeof(asizeint));
  2831. if change_endian_for_replay then
  2832. val:=swapendian(val);
  2833. result:=val;
  2834. end;
  2835. function tscannerfile.tokenreadlongword : longword;
  2836. var
  2837. val : longword;
  2838. begin
  2839. replaytokenbuf.read(val,sizeof(longword));
  2840. if change_endian_for_replay then
  2841. val:=swapendian(val);
  2842. result:=val;
  2843. end;
  2844. function tscannerfile.tokenreadlongint : longint;
  2845. var
  2846. val : longint;
  2847. begin
  2848. replaytokenbuf.read(val,sizeof(longint));
  2849. if change_endian_for_replay then
  2850. val:=swapendian(val);
  2851. result:=val;
  2852. end;
  2853. function tscannerfile.tokenreadshortint : shortint;
  2854. var
  2855. val : shortint;
  2856. begin
  2857. replaytokenbuf.read(val,sizeof(shortint));
  2858. result:=val;
  2859. end;
  2860. function tscannerfile.tokenreadbyte : byte;
  2861. var
  2862. val : byte;
  2863. begin
  2864. replaytokenbuf.read(val,sizeof(byte));
  2865. result:=val;
  2866. end;
  2867. function tscannerfile.tokenreadsmallint : smallint;
  2868. var
  2869. val : smallint;
  2870. begin
  2871. replaytokenbuf.read(val,sizeof(smallint));
  2872. if change_endian_for_replay then
  2873. val:=swapendian(val);
  2874. result:=val;
  2875. end;
  2876. function tscannerfile.tokenreadword : word;
  2877. var
  2878. val : word;
  2879. begin
  2880. replaytokenbuf.read(val,sizeof(word));
  2881. if change_endian_for_replay then
  2882. val:=swapendian(val);
  2883. result:=val;
  2884. end;
  2885. function tscannerfile.tokenreadenum(size : longint) : longword;
  2886. begin
  2887. if size=1 then
  2888. result:=tokenreadbyte
  2889. else if size=2 then
  2890. result:=tokenreadword
  2891. else if size=4 then
  2892. result:=tokenreadlongword
  2893. else
  2894. internalerror(2013112901);
  2895. end;
  2896. procedure tscannerfile.tokenreadset(var b;size : longint);
  2897. var
  2898. i : longint;
  2899. begin
  2900. replaytokenbuf.read(b,size);
  2901. if change_endian_for_replay then
  2902. for i:=0 to size-1 do
  2903. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  2904. end;
  2905. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  2906. begin
  2907. recordtokenbuf.write(b,size);
  2908. end;
  2909. procedure tscannerfile.tokenwriteset(var b;size : longint);
  2910. begin
  2911. recordtokenbuf.write(b,size);
  2912. end;
  2913. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  2914. { This procedure
  2915. needs to be changed whenever
  2916. globals.tsettings type is changed,
  2917. the problem is that no error will appear
  2918. before tests with generics are tested. PM }
  2919. var
  2920. startpos, endpos : longword;
  2921. begin
  2922. { WARNING all those fields need to be in the correct
  2923. order otherwise cross_endian PPU reading will fail }
  2924. startpos:=replaytokenbuf.pos;
  2925. with asettings do
  2926. begin
  2927. alignment.procalign:=tokenreadlongint;
  2928. alignment.loopalign:=tokenreadlongint;
  2929. alignment.jumpalign:=tokenreadlongint;
  2930. alignment.jumpalignskipmax:=tokenreadlongint;
  2931. alignment.coalescealign:=tokenreadlongint;
  2932. alignment.coalescealignskipmax:=tokenreadlongint;
  2933. alignment.constalignmin:=tokenreadlongint;
  2934. alignment.constalignmax:=tokenreadlongint;
  2935. alignment.varalignmin:=tokenreadlongint;
  2936. alignment.varalignmax:=tokenreadlongint;
  2937. alignment.localalignmin:=tokenreadlongint;
  2938. alignment.localalignmax:=tokenreadlongint;
  2939. alignment.recordalignmin:=tokenreadlongint;
  2940. alignment.recordalignmax:=tokenreadlongint;
  2941. alignment.maxCrecordalign:=tokenreadlongint;
  2942. tokenreadset(globalswitches,sizeof(globalswitches));
  2943. tokenreadset(targetswitches,sizeof(targetswitches));
  2944. tokenreadset(moduleswitches,sizeof(moduleswitches));
  2945. tokenreadset(localswitches,sizeof(localswitches));
  2946. tokenreadset(modeswitches,sizeof(modeswitches));
  2947. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  2948. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2949. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2950. tokenreadset(debugswitches,sizeof(debugswitches));
  2951. { 0: old behaviour for sets <=256 elements
  2952. >0: round to this size }
  2953. setalloc:=tokenreadshortint;
  2954. packenum:=tokenreadshortint;
  2955. packrecords:=tokenreadshortint;
  2956. maxfpuregisters:=tokenreadshortint;
  2957. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2958. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2959. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  2960. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  2961. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  2962. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  2963. { tstringencoding is word type,
  2964. thus this should be OK here }
  2965. sourcecodepage:=tstringEncoding(tokenreadword);
  2966. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  2967. disabledircache:=boolean(tokenreadbyte);
  2968. tlsmodel:=ttlsmodel(tokenreadenum(sizeof(ttlsmodel)));
  2969. { TH: Since the field was conditional originally, it was not stored in PPUs. }
  2970. { While adding ControllerSupport constant, I decided not to store ct_none }
  2971. { on targets not supporting controllers, but this might be changed here and }
  2972. { in tokenwritesettings in the future to unify the PPU structure and handling }
  2973. { of this field in the compiler. }
  2974. {$PUSH}
  2975. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  2976. if ControllerSupport then
  2977. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)))
  2978. else
  2979. ControllerType:=ct_none;
  2980. {$POP}
  2981. endpos:=replaytokenbuf.pos;
  2982. if endpos-startpos<>expected_size then
  2983. Comment(V_Error,'Wrong size of Settings read-in');
  2984. end;
  2985. end;
  2986. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  2987. { This procedure
  2988. needs to be changed whenever
  2989. globals.tsettings type is changed,
  2990. the problem is that no error will appear
  2991. before tests with generics are tested. PM }
  2992. var
  2993. sizepos, startpos, endpos : longword;
  2994. begin
  2995. { WARNING all those fields need to be in the correct
  2996. order otherwise cross_endian PPU reading will fail }
  2997. sizepos:=recordtokenbuf.pos;
  2998. size:=0;
  2999. tokenwritesizeint(size);
  3000. startpos:=recordtokenbuf.pos;
  3001. with asettings do
  3002. begin
  3003. tokenwritelongint(alignment.procalign);
  3004. tokenwritelongint(alignment.loopalign);
  3005. tokenwritelongint(alignment.jumpalign);
  3006. tokenwritelongint(alignment.jumpalignskipmax);
  3007. tokenwritelongint(alignment.coalescealign);
  3008. tokenwritelongint(alignment.coalescealignskipmax);
  3009. tokenwritelongint(alignment.constalignmin);
  3010. tokenwritelongint(alignment.constalignmax);
  3011. tokenwritelongint(alignment.varalignmin);
  3012. tokenwritelongint(alignment.varalignmax);
  3013. tokenwritelongint(alignment.localalignmin);
  3014. tokenwritelongint(alignment.localalignmax);
  3015. tokenwritelongint(alignment.recordalignmin);
  3016. tokenwritelongint(alignment.recordalignmax);
  3017. tokenwritelongint(alignment.maxCrecordalign);
  3018. tokenwriteset(globalswitches,sizeof(globalswitches));
  3019. tokenwriteset(targetswitches,sizeof(targetswitches));
  3020. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  3021. tokenwriteset(localswitches,sizeof(localswitches));
  3022. tokenwriteset(modeswitches,sizeof(modeswitches));
  3023. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  3024. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  3025. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  3026. tokenwriteset(debugswitches,sizeof(debugswitches));
  3027. { 0: old behaviour for sets <=256 elements
  3028. >0: round to this size }
  3029. tokenwriteshortint(setalloc);
  3030. tokenwriteshortint(packenum);
  3031. tokenwriteshortint(packrecords);
  3032. tokenwriteshortint(maxfpuregisters);
  3033. tokenwriteenum(cputype,sizeof(tcputype));
  3034. tokenwriteenum(optimizecputype,sizeof(tcputype));
  3035. tokenwriteenum(fputype,sizeof(tfputype));
  3036. tokenwriteenum(asmmode,sizeof(tasmmode));
  3037. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  3038. tokenwriteenum(defproccall,sizeof(tproccalloption));
  3039. { tstringencoding is word type,
  3040. thus this should be OK here }
  3041. tokenwriteword(sourcecodepage);
  3042. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  3043. recordtokenbuf.write(byte(disabledircache),1);
  3044. tokenwriteenum(tlsmodel,sizeof(tlsmodel));
  3045. { TH: See note about controllertype field in tokenreadsettings. }
  3046. {$PUSH}
  3047. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  3048. if ControllerSupport then
  3049. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  3050. {$POP}
  3051. endpos:=recordtokenbuf.pos;
  3052. size:=endpos-startpos;
  3053. recordtokenbuf.seek(sizepos);
  3054. tokenwritesizeint(size);
  3055. recordtokenbuf.seek(endpos);
  3056. end;
  3057. end;
  3058. procedure tscannerfile.recordtoken;
  3059. var
  3060. t : ttoken;
  3061. s : tspecialgenerictoken;
  3062. len,msgnb,copy_size : asizeint;
  3063. val : longint;
  3064. b : byte;
  3065. pmsg : pmessagestaterecord;
  3066. begin
  3067. if not assigned(recordtokenbuf) then
  3068. internalerror(200511176);
  3069. t:=_GENERICSPECIALTOKEN;
  3070. { settings changed? }
  3071. { last field pmessage is handled separately below in
  3072. ST_LOADMESSAGES }
  3073. if CompareByte(current_settings,last_settings,
  3074. sizeof(current_settings)-sizeof(pointer))<>0 then
  3075. begin
  3076. { use a special token to record it }
  3077. s:=ST_LOADSETTINGS;
  3078. writetoken(t);
  3079. recordtokenbuf.write(s,1);
  3080. copy_size:=sizeof(current_settings)-sizeof(pointer);
  3081. tokenwritesettings(current_settings,copy_size);
  3082. last_settings:=current_settings;
  3083. end;
  3084. if current_settings.pmessage<>last_message then
  3085. begin
  3086. { use a special token to record it }
  3087. s:=ST_LOADMESSAGES;
  3088. writetoken(t);
  3089. recordtokenbuf.write(s,1);
  3090. msgnb:=0;
  3091. pmsg:=current_settings.pmessage;
  3092. while assigned(pmsg) do
  3093. begin
  3094. if msgnb=high(asizeint) then
  3095. { Too many messages }
  3096. internalerror(2011090401);
  3097. inc(msgnb);
  3098. pmsg:=pmsg^.next;
  3099. end;
  3100. tokenwritesizeint(msgnb);
  3101. pmsg:=current_settings.pmessage;
  3102. while assigned(pmsg) do
  3103. begin
  3104. { What about endianess here?}
  3105. { SB: this is handled by tokenreadlongint }
  3106. val:=pmsg^.value;
  3107. tokenwritelongint(val);
  3108. val:=ord(pmsg^.state);
  3109. tokenwritelongint(val);
  3110. pmsg:=pmsg^.next;
  3111. end;
  3112. last_message:=current_settings.pmessage;
  3113. end;
  3114. { file pos changes? }
  3115. if current_tokenpos.fileindex<>last_filepos.fileindex then
  3116. begin
  3117. s:=ST_FILEINDEX;
  3118. writetoken(t);
  3119. recordtokenbuf.write(s,1);
  3120. tokenwriteword(current_tokenpos.fileindex);
  3121. last_filepos.fileindex:=current_tokenpos.fileindex;
  3122. last_filepos.line:=0;
  3123. end;
  3124. if current_tokenpos.line<>last_filepos.line then
  3125. begin
  3126. s:=ST_LINE;
  3127. writetoken(t);
  3128. recordtokenbuf.write(s,1);
  3129. tokenwritelongint(current_tokenpos.line);
  3130. last_filepos.line:=current_tokenpos.line;
  3131. last_filepos.column:=0;
  3132. end;
  3133. if current_tokenpos.column<>last_filepos.column then
  3134. begin
  3135. s:=ST_COLUMN;
  3136. writetoken(t);
  3137. { can the column be written packed? }
  3138. if current_tokenpos.column<$80 then
  3139. begin
  3140. b:=$80 or current_tokenpos.column;
  3141. recordtokenbuf.write(b,1);
  3142. end
  3143. else
  3144. begin
  3145. recordtokenbuf.write(s,1);
  3146. tokenwriteword(current_tokenpos.column);
  3147. end;
  3148. last_filepos.column:=current_tokenpos.column;
  3149. end;
  3150. writetoken(token);
  3151. if token<>_GENERICSPECIALTOKEN then
  3152. writetoken(idtoken);
  3153. case token of
  3154. _CWCHAR,
  3155. _CWSTRING :
  3156. begin
  3157. tokenwritesizeint(patternw^.len);
  3158. if patternw^.len>0 then
  3159. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3160. end;
  3161. _CSTRING:
  3162. begin
  3163. len:=length(cstringpattern);
  3164. tokenwritesizeint(len);
  3165. if len>0 then
  3166. recordtokenbuf.write(cstringpattern[1],len);
  3167. end;
  3168. _CCHAR,
  3169. _INTCONST,
  3170. _REALNUMBER :
  3171. begin
  3172. { pexpr.pas messes with pattern in case of negative integer consts,
  3173. see around line 2562 the comment of JM; remove the - before recording it
  3174. (FK)
  3175. }
  3176. if (token=_INTCONST) and (pattern[1]='-') then
  3177. delete(pattern,1,1);
  3178. recordtokenbuf.write(pattern[0],1);
  3179. recordtokenbuf.write(pattern[1],length(pattern));
  3180. end;
  3181. _ID :
  3182. begin
  3183. recordtokenbuf.write(orgpattern[0],1);
  3184. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  3185. end;
  3186. else
  3187. ;
  3188. end;
  3189. end;
  3190. procedure tscannerfile.startreplaytokens(buf:tdynamicarray; change_endian:boolean);
  3191. begin
  3192. if not assigned(buf) then
  3193. internalerror(200511175);
  3194. { save current scanner state }
  3195. replaystack:=treplaystack.create(token,idtoken,orgpattern,pattern,
  3196. cstringpattern,patternw,current_settings,replaytokenbuf,change_endian_for_replay,replaystack);
  3197. if assigned(inputpointer) then
  3198. dec(inputpointer);
  3199. { install buffer }
  3200. replaytokenbuf:=buf;
  3201. { Initialize value of change_endian_for_replay variable }
  3202. change_endian_for_replay:=change_endian;
  3203. { reload next token }
  3204. replaytokenbuf.seek(0);
  3205. replaytoken;
  3206. end;
  3207. function tscannerfile.readtoken: ttoken;
  3208. var
  3209. b,b2 : byte;
  3210. begin
  3211. replaytokenbuf.read(b,1);
  3212. if (b and $80)<>0 then
  3213. begin
  3214. replaytokenbuf.read(b2,1);
  3215. result:=ttoken(((b and $7f) shl 8) or b2);
  3216. end
  3217. else
  3218. result:=ttoken(b);
  3219. end;
  3220. procedure tscannerfile.replaytoken;
  3221. var
  3222. wlen,mesgnb,copy_size : asizeint;
  3223. specialtoken : tspecialgenerictoken;
  3224. i : byte;
  3225. pmsg,prevmsg : pmessagestaterecord;
  3226. begin
  3227. if not assigned(replaytokenbuf) then
  3228. internalerror(200511177);
  3229. { End of replay buffer? Then load the next char from the file again }
  3230. if replaytokenbuf.pos>=replaytokenbuf.size then
  3231. begin
  3232. token:=replaystack.token;
  3233. idtoken:=replaystack.idtoken;
  3234. pattern:=replaystack.pattern;
  3235. orgpattern:=replaystack.orgpattern;
  3236. setlengthwidestring(patternw,replaystack.patternw^.len);
  3237. move(replaystack.patternw^.data^,patternw^.data^,replaystack.patternw^.len*sizeof(tcompilerwidechar));
  3238. cstringpattern:=replaystack.cstringpattern;
  3239. replaytokenbuf:=replaystack.tokenbuf;
  3240. change_endian_for_replay:=replaystack.tokenbuf_needs_swapping;
  3241. { restore compiler settings }
  3242. current_settings:=replaystack.settings;
  3243. popreplaystack;
  3244. if assigned(inputpointer) then
  3245. begin
  3246. c:=inputpointer^;
  3247. inc(inputpointer);
  3248. end;
  3249. exit;
  3250. end;
  3251. repeat
  3252. { load token from the buffer }
  3253. token:=readtoken;
  3254. if token<>_GENERICSPECIALTOKEN then
  3255. idtoken:=readtoken
  3256. else
  3257. idtoken:=_NOID;
  3258. case token of
  3259. _CWCHAR,
  3260. _CWSTRING :
  3261. begin
  3262. wlen:=tokenreadsizeint;
  3263. setlengthwidestring(patternw,wlen);
  3264. if wlen>0 then
  3265. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3266. orgpattern:='';
  3267. pattern:='';
  3268. cstringpattern:='';
  3269. end;
  3270. _CSTRING:
  3271. begin
  3272. wlen:=tokenreadsizeint;
  3273. if wlen>0 then
  3274. begin
  3275. setlength(cstringpattern,wlen);
  3276. replaytokenbuf.read(cstringpattern[1],wlen);
  3277. end
  3278. else
  3279. cstringpattern:='';
  3280. orgpattern:='';
  3281. pattern:='';
  3282. end;
  3283. _CCHAR,
  3284. _INTCONST,
  3285. _REALNUMBER :
  3286. begin
  3287. replaytokenbuf.read(pattern[0],1);
  3288. replaytokenbuf.read(pattern[1],length(pattern));
  3289. orgpattern:='';
  3290. end;
  3291. _ID :
  3292. begin
  3293. replaytokenbuf.read(orgpattern[0],1);
  3294. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  3295. pattern:=upper(orgpattern);
  3296. end;
  3297. _GENERICSPECIALTOKEN:
  3298. begin
  3299. replaytokenbuf.read(specialtoken,1);
  3300. { packed column? }
  3301. if (ord(specialtoken) and $80)<>0 then
  3302. begin
  3303. current_tokenpos.column:=ord(specialtoken) and $7f;
  3304. current_filepos:=current_tokenpos;
  3305. end
  3306. else
  3307. case specialtoken of
  3308. ST_LOADSETTINGS:
  3309. begin
  3310. copy_size:=tokenreadsizeint;
  3311. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  3312. // internalerror(2011090501);
  3313. {
  3314. replaytokenbuf.read(current_settings,copy_size);
  3315. }
  3316. tokenreadsettings(current_settings,copy_size);
  3317. end;
  3318. ST_LOADMESSAGES:
  3319. begin
  3320. current_settings.pmessage:=nil;
  3321. mesgnb:=tokenreadsizeint;
  3322. prevmsg:=nil;
  3323. for i:=1 to mesgnb do
  3324. begin
  3325. new(pmsg);
  3326. if i=1 then
  3327. current_settings.pmessage:=pmsg
  3328. else
  3329. prevmsg^.next:=pmsg;
  3330. pmsg^.value:=tokenreadlongint;
  3331. pmsg^.state:=tmsgstate(tokenreadlongint);
  3332. pmsg^.next:=nil;
  3333. prevmsg:=pmsg;
  3334. end;
  3335. end;
  3336. ST_LINE:
  3337. begin
  3338. current_tokenpos.line:=tokenreadlongint;
  3339. current_filepos:=current_tokenpos;
  3340. end;
  3341. ST_COLUMN:
  3342. begin
  3343. current_tokenpos.column:=tokenreadword;
  3344. current_filepos:=current_tokenpos;
  3345. end;
  3346. ST_FILEINDEX:
  3347. begin
  3348. current_tokenpos.fileindex:=tokenreadword;
  3349. current_filepos:=current_tokenpos;
  3350. end;
  3351. end;
  3352. continue;
  3353. end;
  3354. else
  3355. ;
  3356. end;
  3357. break;
  3358. until false;
  3359. end;
  3360. procedure tscannerfile.addfile(hp:tinputfile);
  3361. begin
  3362. saveinputfile;
  3363. { add to list }
  3364. hp.next:=inputfile;
  3365. inputfile:=hp;
  3366. { load new inputfile }
  3367. restoreinputfile;
  3368. end;
  3369. procedure tscannerfile.reload;
  3370. var
  3371. wasmacro: Boolean;
  3372. begin
  3373. with inputfile do
  3374. begin
  3375. { when nothing more to read then leave immediatly, so we
  3376. don't change the current_filepos and leave it point to the last
  3377. char }
  3378. if (c=#26) and (not assigned(next)) then
  3379. exit;
  3380. repeat
  3381. { still more to read?, then change the #0 to a space so its seen
  3382. as a separator, this can't be used for macro's which can change
  3383. the place of the #0 in the buffer with tempopen }
  3384. if (c=#0) and (bufsize>0) and
  3385. not(inputfile.is_macro) and
  3386. (inputpointer-inputbuffer<bufsize) then
  3387. begin
  3388. c:=' ';
  3389. inc(inputpointer);
  3390. exit;
  3391. end;
  3392. { can we read more from this file ? }
  3393. if (c<>#26) and (not endoffile) then
  3394. begin
  3395. readbuf;
  3396. inputpointer:=buf;
  3397. inputbuffer:=buf;
  3398. inputstart:=bufstart;
  3399. { first line? }
  3400. if line_no=0 then
  3401. begin
  3402. c:=inputpointer^;
  3403. { eat utf-8 signature? }
  3404. if (ord(inputpointer^)=$ef) and
  3405. (ord((inputpointer+1)^)=$bb) and
  3406. (ord((inputpointer+2)^)=$bf) then
  3407. begin
  3408. (* we don't support including files with an UTF-8 bom
  3409. inside another file that wasn't encoded as UTF-8
  3410. already (we don't support {$codepage xxx} switches in
  3411. the middle of a file either) *)
  3412. if (current_settings.sourcecodepage<>CP_UTF8) and
  3413. not current_module.in_global then
  3414. Message(scanner_f_illegal_utf8_bom);
  3415. inc(inputpointer,3);
  3416. message(scan_c_switching_to_utf8);
  3417. current_settings.sourcecodepage:=CP_UTF8;
  3418. exclude(current_settings.moduleswitches,cs_system_codepage);
  3419. include(current_settings.moduleswitches,cs_explicit_codepage);
  3420. end;
  3421. line_no:=1;
  3422. if cs_asm_source in current_settings.globalswitches then
  3423. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3424. end;
  3425. end
  3426. else
  3427. begin
  3428. wasmacro:=inputfile.is_macro;
  3429. { load eof position in tokenpos/current_filepos }
  3430. gettokenpos;
  3431. { close file }
  3432. closeinputfile;
  3433. { no next module, than EOF }
  3434. if not assigned(inputfile.next) then
  3435. begin
  3436. c:=#26;
  3437. exit;
  3438. end;
  3439. { load next file and reopen it }
  3440. nextfile;
  3441. tempopeninputfile;
  3442. { status }
  3443. Message1(scan_t_back_in,inputfile.name);
  3444. { end of include file is like a line break which ends e.g. also // style comments }
  3445. if not(wasmacro) and (current_commentstyle=comment_delphi) then
  3446. begin
  3447. c:=#10;
  3448. { ... but we have to decrease the line number first because it is increased due to this
  3449. inserted line break later on }
  3450. dec(line_no);
  3451. exit;
  3452. end;
  3453. end;
  3454. { load next char }
  3455. c:=inputpointer^;
  3456. inc(inputpointer);
  3457. until c<>#0; { if also end, then reload again }
  3458. end;
  3459. end;
  3460. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint;internally_generated: boolean);
  3461. var
  3462. hp : tinputfile;
  3463. begin
  3464. { save old postion }
  3465. dec(inputpointer);
  3466. tempcloseinputfile;
  3467. { create macro 'file' }
  3468. { use special name to dispose after !! }
  3469. hp:=do_openinputfile('_Macro_.'+macname);
  3470. addfile(hp);
  3471. with inputfile do
  3472. begin
  3473. inc(macro_nesting_depth);
  3474. setmacro(p,len);
  3475. { local buffer }
  3476. inputbuffer:=buf;
  3477. inputpointer:=buf;
  3478. inputstart:=bufstart;
  3479. ref_index:=fileindex;
  3480. internally_generated_macro:=internally_generated;
  3481. end;
  3482. { reset line }
  3483. line_no:=line;
  3484. lastlinepos:=0;
  3485. lasttokenpos:=0;
  3486. nexttokenpos:=0;
  3487. { load new c }
  3488. c:=inputpointer^;
  3489. inc(inputpointer);
  3490. end;
  3491. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3492. begin
  3493. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3494. filepos.line:=line_no;
  3495. filepos.column:=tokenpos-lastlinepos;
  3496. filepos.fileindex:=inputfile.ref_index;
  3497. filepos.moduleindex:=current_module.unit_index;
  3498. end;
  3499. procedure tscannerfile.gettokenpos;
  3500. { load the values of tokenpos and lasttokenpos }
  3501. begin
  3502. do_gettokenpos(lasttokenpos,current_tokenpos);
  3503. current_filepos:=current_tokenpos;
  3504. end;
  3505. procedure tscannerfile.cachenexttokenpos;
  3506. begin
  3507. do_gettokenpos(nexttokenpos,next_filepos);
  3508. end;
  3509. procedure tscannerfile.setnexttoken;
  3510. begin
  3511. token:=nexttoken;
  3512. nexttoken:=NOTOKEN;
  3513. lasttokenpos:=nexttokenpos;
  3514. current_tokenpos:=next_filepos;
  3515. current_filepos:=current_tokenpos;
  3516. nexttokenpos:=0;
  3517. end;
  3518. procedure tscannerfile.savetokenpos;
  3519. begin
  3520. oldlasttokenpos:=lasttokenpos;
  3521. oldcurrent_filepos:=current_filepos;
  3522. oldcurrent_tokenpos:=current_tokenpos;
  3523. end;
  3524. procedure tscannerfile.restoretokenpos;
  3525. begin
  3526. lasttokenpos:=oldlasttokenpos;
  3527. current_filepos:=oldcurrent_filepos;
  3528. current_tokenpos:=oldcurrent_tokenpos;
  3529. end;
  3530. procedure tscannerfile.inc_comment_level;
  3531. begin
  3532. if (m_nested_comment in current_settings.modeswitches) then
  3533. inc(comment_level)
  3534. else
  3535. comment_level:=1;
  3536. if (comment_level>1) then
  3537. begin
  3538. savetokenpos;
  3539. gettokenpos; { update for warning }
  3540. Message1(scan_w_comment_level,tostr(comment_level));
  3541. restoretokenpos;
  3542. end;
  3543. end;
  3544. procedure tscannerfile.dec_comment_level;
  3545. begin
  3546. if (m_nested_comment in current_settings.modeswitches) then
  3547. dec(comment_level)
  3548. else
  3549. comment_level:=0;
  3550. end;
  3551. procedure tscannerfile.linebreak;
  3552. var
  3553. cur : char;
  3554. begin
  3555. with inputfile do
  3556. begin
  3557. if (byte(inputpointer^)=0) and not(endoffile) then
  3558. begin
  3559. cur:=c;
  3560. reload;
  3561. if byte(cur)+byte(c)<>23 then
  3562. dec(inputpointer);
  3563. end
  3564. else
  3565. begin
  3566. { Support all combination of #10 and #13 as line break }
  3567. if (byte(inputpointer^)+byte(c)=23) then
  3568. inc(inputpointer);
  3569. end;
  3570. { Always return #10 as line break }
  3571. c:=#10;
  3572. { increase line counters }
  3573. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3574. inc(line_no);
  3575. { update linebuffer }
  3576. if cs_asm_source in current_settings.globalswitches then
  3577. inputfile.setline(line_no,lastlinepos);
  3578. { update for status and call the show status routine,
  3579. but don't touch current_filepos ! }
  3580. savetokenpos;
  3581. gettokenpos; { update for v_status }
  3582. inc(status.compiledlines);
  3583. ShowStatus;
  3584. restoretokenpos;
  3585. end;
  3586. end;
  3587. procedure tscannerfile.illegal_char(c:char);
  3588. var
  3589. s : string;
  3590. begin
  3591. if c in [#32..#255] then
  3592. s:=''''+c+''''
  3593. else
  3594. s:='#'+tostr(ord(c));
  3595. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3596. end;
  3597. procedure tscannerfile.end_of_file;
  3598. begin
  3599. checkpreprocstack;
  3600. Message(scan_f_end_of_file);
  3601. end;
  3602. {-------------------------------------------
  3603. IF Conditional Handling
  3604. -------------------------------------------}
  3605. procedure tscannerfile.checkpreprocstack;
  3606. begin
  3607. { check for missing ifdefs }
  3608. while assigned(preprocstack) do
  3609. begin
  3610. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3611. current_module.sourcefiles.get_file_name(preprocstack.fileindex),
  3612. tostr(preprocstack.line_nb));
  3613. poppreprocstack;
  3614. end;
  3615. end;
  3616. procedure tscannerfile.poppreprocstack;
  3617. var
  3618. hp : tpreprocstack;
  3619. begin
  3620. if assigned(preprocstack) then
  3621. begin
  3622. Message1(scan_c_endif_found,preprocstack.name);
  3623. hp:=preprocstack.next;
  3624. preprocstack.free;
  3625. preprocstack:=hp;
  3626. end
  3627. else
  3628. Message(scan_e_endif_without_if);
  3629. end;
  3630. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  3631. var
  3632. condition: Boolean;
  3633. valuedescr: String;
  3634. begin
  3635. if (preprocstack=nil) or preprocstack.accept then
  3636. condition:=compile_time_predicate(valuedescr)
  3637. else
  3638. begin
  3639. condition:= false;
  3640. valuedescr:= '';
  3641. end;
  3642. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  3643. preprocstack.name:=valuedescr;
  3644. preprocstack.line_nb:=line_no;
  3645. preprocstack.fileindex:=current_filepos.fileindex;
  3646. if preprocstack.accept then
  3647. Message2(messid,preprocstack.name,'accepted')
  3648. else
  3649. Message2(messid,preprocstack.name,'rejected');
  3650. end;
  3651. procedure tscannerfile.elsepreprocstack;
  3652. begin
  3653. if assigned(preprocstack) and
  3654. (preprocstack.typ<>pp_else) then
  3655. begin
  3656. if (preprocstack.typ=pp_elseif) then
  3657. preprocstack.accept:=false
  3658. else
  3659. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  3660. preprocstack.accept:=not preprocstack.accept;
  3661. preprocstack.iftyp:=preprocstack.typ;
  3662. preprocstack.typ:=pp_else;
  3663. preprocstack.line_nb:=line_no;
  3664. preprocstack.fileindex:=current_filepos.fileindex;
  3665. if preprocstack.accept then
  3666. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3667. else
  3668. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3669. end
  3670. else
  3671. Message(scan_e_endif_without_if);
  3672. end;
  3673. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  3674. var
  3675. valuedescr: String;
  3676. begin
  3677. if assigned(preprocstack) and
  3678. (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef,pp_elseif]) then
  3679. begin
  3680. { when the branch is accepted we use pp_elseif so we know that
  3681. all the next branches need to be rejected. when this branch is still
  3682. not accepted then leave it at pp_if }
  3683. if (preprocstack.typ=pp_elseif) then
  3684. preprocstack.accept:=false
  3685. else if (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef]) and preprocstack.accept then
  3686. begin
  3687. preprocstack.accept:=false;
  3688. preprocstack.typ:=pp_elseif;
  3689. end
  3690. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  3691. and compile_time_predicate(valuedescr) then
  3692. begin
  3693. preprocstack.name:=valuedescr;
  3694. preprocstack.accept:=true;
  3695. preprocstack.typ:=pp_elseif;
  3696. end;
  3697. preprocstack.line_nb:=line_no;
  3698. preprocstack.fileindex:=current_filepos.fileindex;
  3699. if preprocstack.accept then
  3700. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3701. else
  3702. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3703. end
  3704. else
  3705. Message(scan_e_endif_without_if);
  3706. end;
  3707. procedure tscannerfile.popreplaystack;
  3708. var
  3709. hp : treplaystack;
  3710. begin
  3711. if assigned(replaystack) then
  3712. begin
  3713. hp:=replaystack.next;
  3714. replaystack.free;
  3715. replaystack:=hp;
  3716. end;
  3717. end;
  3718. function tscannerfile.replay_stack_depth:longint;
  3719. var
  3720. tmp: treplaystack;
  3721. begin
  3722. result:=0;
  3723. tmp:=replaystack;
  3724. while assigned(tmp) do
  3725. begin
  3726. inc(result);
  3727. tmp:=tmp.next;
  3728. end;
  3729. end;
  3730. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  3731. begin
  3732. savetokenpos;
  3733. repeat
  3734. current_scanner.gettokenpos;
  3735. Message1(scan_d_handling_switch,'$'+p.name);
  3736. p.proc();
  3737. { accept the text ? }
  3738. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  3739. break
  3740. else
  3741. begin
  3742. current_scanner.gettokenpos;
  3743. Message(scan_c_skipping_until);
  3744. repeat
  3745. current_scanner.skipuntildirective;
  3746. if not (m_mac in current_settings.modeswitches) then
  3747. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  3748. else
  3749. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  3750. until assigned(p) and (p.is_conditional);
  3751. current_scanner.gettokenpos;
  3752. end;
  3753. until false;
  3754. restoretokenpos;
  3755. end;
  3756. procedure tscannerfile.handledirectives;
  3757. var
  3758. t : tdirectiveitem;
  3759. hs : string;
  3760. begin
  3761. gettokenpos;
  3762. readchar; {Remove the $}
  3763. hs:=readid;
  3764. { handle empty directive }
  3765. if hs='' then
  3766. begin
  3767. Message1(scan_w_illegal_switch,'$');
  3768. exit;
  3769. end;
  3770. {$ifdef PREPROCWRITE}
  3771. if parapreprocess then
  3772. begin
  3773. if not (m_mac in current_settings.modeswitches) then
  3774. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  3775. else
  3776. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  3777. if assigned(t) and not(t.is_conditional) then
  3778. begin
  3779. preprocfile.AddSpace;
  3780. preprocfile.Add('{$'+hs+current_scanner.readcomment+'}');
  3781. exit;
  3782. end;
  3783. end;
  3784. {$endif PREPROCWRITE}
  3785. { skip this directive? }
  3786. if (ignoredirectives.find(hs)<>nil) then
  3787. begin
  3788. if (comment_level>0) then
  3789. readcomment;
  3790. { we've read the whole comment }
  3791. current_commentstyle:=comment_none;
  3792. exit;
  3793. end;
  3794. { Check for compiler switches }
  3795. while (length(hs)=1) and (c in ['-','+']) do
  3796. begin
  3797. Message1(scan_d_handling_switch,'$'+hs+c);
  3798. HandleSwitch(hs[1],c);
  3799. current_scanner.readchar; {Remove + or -}
  3800. if c=',' then
  3801. begin
  3802. current_scanner.readchar; {Remove , }
  3803. { read next switch, support $v+,$+}
  3804. hs:=current_scanner.readid;
  3805. if (hs='') then
  3806. begin
  3807. if (c='$') and (m_fpc in current_settings.modeswitches) then
  3808. begin
  3809. current_scanner.readchar; { skip $ }
  3810. hs:=current_scanner.readid;
  3811. end;
  3812. if (hs='') then
  3813. Message1(scan_w_illegal_directive,'$'+c);
  3814. end;
  3815. end
  3816. else
  3817. hs:='';
  3818. end;
  3819. { directives may follow switches after a , }
  3820. if hs<>'' then
  3821. begin
  3822. if not (m_mac in current_settings.modeswitches) then
  3823. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  3824. else
  3825. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  3826. if assigned(t) then
  3827. begin
  3828. if t.is_conditional then
  3829. handleconditional(t)
  3830. else
  3831. begin
  3832. Message1(scan_d_handling_switch,'$'+hs);
  3833. t.proc();
  3834. end;
  3835. end
  3836. else
  3837. begin
  3838. current_scanner.ignoredirectives.Add(hs,nil);
  3839. Message1(scan_w_illegal_directive,'$'+hs);
  3840. end;
  3841. { conditionals already read the comment }
  3842. if (current_scanner.comment_level>0) then
  3843. current_scanner.readcomment;
  3844. { we've read the whole comment }
  3845. current_commentstyle:=comment_none;
  3846. end;
  3847. end;
  3848. procedure tscannerfile.readchar;
  3849. begin
  3850. c:=inputpointer^;
  3851. if c=#0 then
  3852. reload
  3853. else
  3854. inc(inputpointer);
  3855. end;
  3856. procedure tscannerfile.readstring;
  3857. var
  3858. i : longint;
  3859. err : boolean;
  3860. begin
  3861. err:=false;
  3862. i:=0;
  3863. repeat
  3864. case c of
  3865. '_',
  3866. '0'..'9',
  3867. 'A'..'Z',
  3868. 'a'..'z' :
  3869. begin
  3870. if i<255 then
  3871. begin
  3872. inc(i);
  3873. orgpattern[i]:=c;
  3874. if c in ['a'..'z'] then
  3875. pattern[i]:=chr(ord(c)-32)
  3876. else
  3877. pattern[i]:=c;
  3878. end
  3879. else
  3880. begin
  3881. if not err then
  3882. begin
  3883. Message(scan_e_string_exceeds_255_chars);
  3884. err:=true;
  3885. end;
  3886. end;
  3887. c:=inputpointer^;
  3888. inc(inputpointer);
  3889. end;
  3890. #0 :
  3891. reload;
  3892. else if inputfile.internally_generated_macro and
  3893. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  3894. begin
  3895. if i<255 then
  3896. begin
  3897. inc(i);
  3898. orgpattern[i]:=c;
  3899. pattern[i]:=c;
  3900. end
  3901. else
  3902. begin
  3903. if not err then
  3904. begin
  3905. Message(scan_e_string_exceeds_255_chars);
  3906. err:=true;
  3907. end;
  3908. end;
  3909. c:=inputpointer^;
  3910. inc(inputpointer);
  3911. end
  3912. else
  3913. break;
  3914. end;
  3915. until false;
  3916. orgpattern[0]:=chr(i);
  3917. pattern[0]:=chr(i);
  3918. end;
  3919. procedure tscannerfile.readnumber;
  3920. var
  3921. base,
  3922. i : longint;
  3923. firstdigitread: Boolean;
  3924. begin
  3925. case c of
  3926. '%' :
  3927. begin
  3928. readchar;
  3929. base:=2;
  3930. pattern[1]:='%';
  3931. i:=1;
  3932. end;
  3933. '&' :
  3934. begin
  3935. readchar;
  3936. base:=8;
  3937. pattern[1]:='&';
  3938. i:=1;
  3939. end;
  3940. '$' :
  3941. begin
  3942. readchar;
  3943. base:=16;
  3944. pattern[1]:='$';
  3945. i:=1;
  3946. end;
  3947. else
  3948. begin
  3949. base:=10;
  3950. i:=0;
  3951. end;
  3952. end;
  3953. firstdigitread:=false;
  3954. while ((base>=10) and (c in ['0'..'9'])) or
  3955. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  3956. ((base=8) and (c in ['0'..'7'])) or
  3957. ((base=2) and (c in ['0'..'1'])) or
  3958. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  3959. begin
  3960. if (i<255) and (c<>'_') then
  3961. begin
  3962. inc(i);
  3963. pattern[i]:=c;
  3964. end;
  3965. readchar;
  3966. firstdigitread:=true;
  3967. end;
  3968. pattern[0]:=chr(i);
  3969. end;
  3970. function tscannerfile.readid:string;
  3971. begin
  3972. readstring;
  3973. readid:=pattern;
  3974. end;
  3975. function tscannerfile.readval:longint;
  3976. var
  3977. l : longint;
  3978. w : integer;
  3979. begin
  3980. readnumber;
  3981. val(pattern,l,w);
  3982. readval:=l;
  3983. end;
  3984. function tscannerfile.readcomment(include_special_char: boolean):string;
  3985. var
  3986. i : longint;
  3987. begin
  3988. i:=0;
  3989. repeat
  3990. case c of
  3991. '{' :
  3992. begin
  3993. if (include_special_char) and (i<255) then
  3994. begin
  3995. inc(i);
  3996. readcomment[i]:=c;
  3997. end;
  3998. if current_commentstyle=comment_tp then
  3999. inc_comment_level;
  4000. end;
  4001. '}' :
  4002. begin
  4003. if (include_special_char) and (i<255) then
  4004. begin
  4005. inc(i);
  4006. readcomment[i]:=c;
  4007. end;
  4008. if current_commentstyle=comment_tp then
  4009. begin
  4010. readchar;
  4011. dec_comment_level;
  4012. if comment_level=0 then
  4013. break
  4014. else
  4015. continue;
  4016. end;
  4017. end;
  4018. '*' :
  4019. begin
  4020. if current_commentstyle=comment_oldtp then
  4021. begin
  4022. readchar;
  4023. if c=')' then
  4024. begin
  4025. readchar;
  4026. dec_comment_level;
  4027. break;
  4028. end
  4029. else
  4030. { Add both characters !!}
  4031. if (i<255) then
  4032. begin
  4033. inc(i);
  4034. readcomment[i]:='*';
  4035. if (i<255) then
  4036. begin
  4037. inc(i);
  4038. readcomment[i]:=c;
  4039. end;
  4040. end;
  4041. end
  4042. else
  4043. { Not old TP comment, so add...}
  4044. begin
  4045. if (i<255) then
  4046. begin
  4047. inc(i);
  4048. readcomment[i]:='*';
  4049. end;
  4050. end;
  4051. end;
  4052. #10,#13 :
  4053. linebreak;
  4054. #26 :
  4055. end_of_file;
  4056. else
  4057. begin
  4058. if (i<255) then
  4059. begin
  4060. inc(i);
  4061. readcomment[i]:=c;
  4062. end;
  4063. end;
  4064. end;
  4065. readchar;
  4066. until false;
  4067. readcomment[0]:=chr(i);
  4068. end;
  4069. function tscannerfile.readquotedstring:string;
  4070. var
  4071. i : longint;
  4072. msgwritten : boolean;
  4073. begin
  4074. i:=0;
  4075. msgwritten:=false;
  4076. if (c='''') then
  4077. begin
  4078. repeat
  4079. readchar;
  4080. case c of
  4081. #26 :
  4082. end_of_file;
  4083. #10,#13 :
  4084. Message(scan_f_string_exceeds_line);
  4085. '''' :
  4086. begin
  4087. readchar;
  4088. if c<>'''' then
  4089. break;
  4090. end;
  4091. end;
  4092. if i<255 then
  4093. begin
  4094. inc(i);
  4095. result[i]:=c;
  4096. end
  4097. else
  4098. begin
  4099. if not msgwritten then
  4100. begin
  4101. Message(scan_e_string_exceeds_255_chars);
  4102. msgwritten:=true;
  4103. end;
  4104. end;
  4105. until false;
  4106. end;
  4107. result[0]:=chr(i);
  4108. end;
  4109. function tscannerfile.readstate:char;
  4110. var
  4111. state : char;
  4112. begin
  4113. state:=' ';
  4114. if c=' ' then
  4115. begin
  4116. current_scanner.skipspace;
  4117. current_scanner.readid;
  4118. if pattern='ON' then
  4119. state:='+'
  4120. else
  4121. if pattern='OFF' then
  4122. state:='-';
  4123. end
  4124. else
  4125. state:=c;
  4126. if not (state in ['+','-']) then
  4127. Message(scan_e_wrong_switch_toggle);
  4128. readstate:=state;
  4129. end;
  4130. function tscannerfile.readoptionalstate(fallback:char):char;
  4131. var
  4132. state : char;
  4133. begin
  4134. state:=' ';
  4135. if c=' ' then
  4136. begin
  4137. current_scanner.skipspace;
  4138. if c in ['*','}'] then
  4139. state:=fallback
  4140. else
  4141. begin
  4142. current_scanner.readid;
  4143. if pattern='ON' then
  4144. state:='+'
  4145. else
  4146. if pattern='OFF' then
  4147. state:='-';
  4148. end;
  4149. end
  4150. else
  4151. if c in ['*','}'] then
  4152. state:=fallback
  4153. else
  4154. state:=c;
  4155. if not (state in ['+','-']) then
  4156. Message(scan_e_wrong_switch_toggle);
  4157. readoptionalstate:=state;
  4158. end;
  4159. function tscannerfile.readstatedefault:char;
  4160. var
  4161. state : char;
  4162. begin
  4163. state:=' ';
  4164. if c=' ' then
  4165. begin
  4166. current_scanner.skipspace;
  4167. current_scanner.readid;
  4168. if pattern='ON' then
  4169. state:='+'
  4170. else
  4171. if pattern='OFF' then
  4172. state:='-'
  4173. else
  4174. if pattern='DEFAULT' then
  4175. state:='*';
  4176. end
  4177. else
  4178. state:=c;
  4179. if not (state in ['+','-','*']) then
  4180. Message(scan_e_wrong_switch_toggle_default);
  4181. readstatedefault:=state;
  4182. end;
  4183. procedure tscannerfile.skipspace;
  4184. begin
  4185. repeat
  4186. case c of
  4187. #26 :
  4188. begin
  4189. reload;
  4190. if (c=#26) and not assigned(inputfile.next) then
  4191. break;
  4192. continue;
  4193. end;
  4194. #10,
  4195. #13 :
  4196. linebreak;
  4197. #9,#11,#12,' ' :
  4198. ;
  4199. else
  4200. break;
  4201. end;
  4202. readchar;
  4203. until false;
  4204. end;
  4205. procedure tscannerfile.skipuntildirective;
  4206. var
  4207. found : longint;
  4208. next_char_loaded : boolean;
  4209. begin
  4210. found:=0;
  4211. next_char_loaded:=false;
  4212. repeat
  4213. case c of
  4214. #10,
  4215. #13 :
  4216. linebreak;
  4217. #26 :
  4218. begin
  4219. reload;
  4220. if (c=#26) and not assigned(inputfile.next) then
  4221. end_of_file;
  4222. continue;
  4223. end;
  4224. '{' :
  4225. begin
  4226. if (current_commentstyle in [comment_tp,comment_none]) then
  4227. begin
  4228. current_commentstyle:=comment_tp;
  4229. if (comment_level=0) then
  4230. found:=1;
  4231. inc_comment_level;
  4232. end;
  4233. end;
  4234. '*' :
  4235. begin
  4236. if (current_commentstyle=comment_oldtp) then
  4237. begin
  4238. readchar;
  4239. if c=')' then
  4240. begin
  4241. dec_comment_level;
  4242. found:=0;
  4243. current_commentstyle:=comment_none;
  4244. end
  4245. else
  4246. next_char_loaded:=true;
  4247. end
  4248. else
  4249. found := 0;
  4250. end;
  4251. '}' :
  4252. begin
  4253. if (current_commentstyle=comment_tp) then
  4254. begin
  4255. dec_comment_level;
  4256. if (comment_level=0) then
  4257. current_commentstyle:=comment_none;
  4258. found:=0;
  4259. end;
  4260. end;
  4261. '$' :
  4262. begin
  4263. if found=1 then
  4264. found:=2;
  4265. end;
  4266. '''' :
  4267. if (current_commentstyle=comment_none) then
  4268. begin
  4269. repeat
  4270. readchar;
  4271. case c of
  4272. #26 :
  4273. end_of_file;
  4274. #10,#13 :
  4275. break;
  4276. '''' :
  4277. begin
  4278. readchar;
  4279. if c<>'''' then
  4280. begin
  4281. next_char_loaded:=true;
  4282. break;
  4283. end;
  4284. end;
  4285. end;
  4286. until false;
  4287. end;
  4288. '(' :
  4289. begin
  4290. if (current_commentstyle=comment_none) then
  4291. begin
  4292. readchar;
  4293. if c='*' then
  4294. begin
  4295. readchar;
  4296. if c='$' then
  4297. begin
  4298. found:=2;
  4299. inc_comment_level;
  4300. current_commentstyle:=comment_oldtp;
  4301. end
  4302. else
  4303. begin
  4304. skipoldtpcomment(false);
  4305. next_char_loaded:=true;
  4306. end;
  4307. end
  4308. else
  4309. next_char_loaded:=true;
  4310. end
  4311. else
  4312. found:=0;
  4313. end;
  4314. '/' :
  4315. begin
  4316. if (current_commentstyle=comment_none) then
  4317. begin
  4318. readchar;
  4319. if c='/' then
  4320. skipdelphicomment;
  4321. next_char_loaded:=true;
  4322. end
  4323. else
  4324. found:=0;
  4325. end;
  4326. else
  4327. found:=0;
  4328. end;
  4329. if next_char_loaded then
  4330. next_char_loaded:=false
  4331. else
  4332. readchar;
  4333. until (found=2);
  4334. end;
  4335. {****************************************************************************
  4336. Comment Handling
  4337. ****************************************************************************}
  4338. procedure tscannerfile.skipcomment(read_first_char:boolean);
  4339. begin
  4340. current_commentstyle:=comment_tp;
  4341. if read_first_char then
  4342. readchar;
  4343. inc_comment_level;
  4344. { handle compiler switches }
  4345. if (c='$') then
  4346. handledirectives;
  4347. { handle_switches can dec comment_level, }
  4348. while (comment_level>0) do
  4349. begin
  4350. case c of
  4351. '{' :
  4352. inc_comment_level;
  4353. '}' :
  4354. dec_comment_level;
  4355. '*' :
  4356. { in iso mode, comments opened by a curly bracket can be closed by asterisk, round bracket }
  4357. if m_iso in current_settings.modeswitches then
  4358. begin
  4359. readchar;
  4360. if c=')' then
  4361. dec_comment_level
  4362. else
  4363. continue;
  4364. end;
  4365. #10,#13 :
  4366. linebreak;
  4367. #26 :
  4368. begin
  4369. reload;
  4370. if (c=#26) and not assigned(inputfile.next) then
  4371. end_of_file;
  4372. continue;
  4373. end;
  4374. end;
  4375. readchar;
  4376. end;
  4377. current_commentstyle:=comment_none;
  4378. end;
  4379. procedure tscannerfile.skipdelphicomment;
  4380. begin
  4381. current_commentstyle:=comment_delphi;
  4382. inc_comment_level;
  4383. readchar;
  4384. { this is not supported }
  4385. if c='$' then
  4386. Message(scan_w_wrong_styled_switch);
  4387. { skip comment }
  4388. while not (c in [#10,#13,#26]) do
  4389. readchar;
  4390. dec_comment_level;
  4391. current_commentstyle:=comment_none;
  4392. end;
  4393. procedure tscannerfile.skipoldtpcomment(read_first_char:boolean);
  4394. var
  4395. found : longint;
  4396. begin
  4397. current_commentstyle:=comment_oldtp;
  4398. inc_comment_level;
  4399. { only load a char if last already processed,
  4400. was cause of bug1634 PM }
  4401. if read_first_char then
  4402. readchar;
  4403. { this is now supported }
  4404. if (c='$') then
  4405. handledirectives;
  4406. { skip comment }
  4407. while (comment_level>0) do
  4408. begin
  4409. found:=0;
  4410. repeat
  4411. case c of
  4412. #26 :
  4413. begin
  4414. reload;
  4415. if (c=#26) and not assigned(inputfile.next) then
  4416. end_of_file;
  4417. continue;
  4418. end;
  4419. #10,#13 :
  4420. begin
  4421. if found=4 then
  4422. inc_comment_level;
  4423. linebreak;
  4424. found:=0;
  4425. end;
  4426. '*' :
  4427. begin
  4428. if found=3 then
  4429. found:=4
  4430. else
  4431. begin
  4432. if found=4 then
  4433. inc_comment_level;
  4434. found:=1;
  4435. end;
  4436. end;
  4437. ')' :
  4438. begin
  4439. if found in [1,4] then
  4440. begin
  4441. dec_comment_level;
  4442. if comment_level=0 then
  4443. found:=2
  4444. else
  4445. found:=0;
  4446. end
  4447. else
  4448. found:=0;
  4449. end;
  4450. '}' :
  4451. { in iso mode, comments opened by asterisk, round bracket can be closed by a curly bracket }
  4452. if m_iso in current_settings.modeswitches then
  4453. begin
  4454. dec_comment_level;
  4455. if comment_level=0 then
  4456. found:=2
  4457. else
  4458. found:=0;
  4459. end;
  4460. '(' :
  4461. begin
  4462. if found=4 then
  4463. inc_comment_level;
  4464. found:=3;
  4465. end;
  4466. else
  4467. begin
  4468. if found=4 then
  4469. inc_comment_level;
  4470. found:=0;
  4471. end;
  4472. end;
  4473. readchar;
  4474. until (found=2);
  4475. end;
  4476. current_commentstyle:=comment_none;
  4477. end;
  4478. {****************************************************************************
  4479. Token Scanner
  4480. ****************************************************************************}
  4481. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  4482. var
  4483. code : integer;
  4484. d : cardinal;
  4485. len,
  4486. low,high,mid : longint;
  4487. w : word;
  4488. m : longint;
  4489. mac : tmacro;
  4490. asciinr : string[33];
  4491. iswidestring , firstdigitread: boolean;
  4492. label
  4493. exit_label;
  4494. begin
  4495. flushpendingswitchesstate;
  4496. { record tokens? }
  4497. if allowrecordtoken and
  4498. assigned(recordtokenbuf) then
  4499. recordtoken;
  4500. { replay tokens? }
  4501. if assigned(replaytokenbuf) then
  4502. begin
  4503. replaytoken;
  4504. goto exit_label;
  4505. end;
  4506. { was there already a token read, then return that token }
  4507. if nexttoken<>NOTOKEN then
  4508. begin
  4509. setnexttoken;
  4510. goto exit_label;
  4511. end;
  4512. { Skip all spaces and comments }
  4513. repeat
  4514. case c of
  4515. '{' :
  4516. skipcomment(true);
  4517. #26 :
  4518. begin
  4519. reload;
  4520. if (c=#26) and not assigned(inputfile.next) then
  4521. break;
  4522. end;
  4523. ' ',#9..#13 :
  4524. begin
  4525. {$ifdef PREPROCWRITE}
  4526. if parapreprocess then
  4527. begin
  4528. if c=#10 then
  4529. preprocfile.eolfound:=true
  4530. else
  4531. preprocfile.spacefound:=true;
  4532. end;
  4533. {$endif PREPROCWRITE}
  4534. skipspace;
  4535. end
  4536. else
  4537. break;
  4538. end;
  4539. until false;
  4540. { Save current token position, for EOF its already loaded }
  4541. if c<>#26 then
  4542. gettokenpos;
  4543. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  4544. if c in ['A'..'Z','a'..'z','_'] then
  4545. begin
  4546. readstring;
  4547. token:=_ID;
  4548. idtoken:=_ID;
  4549. { keyword or any other known token,
  4550. pattern is always uppercased }
  4551. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4552. begin
  4553. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4554. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4555. while low<high do
  4556. begin
  4557. mid:=(high+low+1) shr 1;
  4558. if pattern<tokeninfo^[ttoken(mid)].str then
  4559. high:=mid-1
  4560. else
  4561. low:=mid;
  4562. end;
  4563. with tokeninfo^[ttoken(high)] do
  4564. if pattern=str then
  4565. begin
  4566. if (keyword*current_settings.modeswitches)<>[] then
  4567. if op=NOTOKEN then
  4568. token:=ttoken(high)
  4569. else
  4570. token:=op;
  4571. idtoken:=ttoken(high);
  4572. end;
  4573. end;
  4574. { Only process identifiers and not keywords }
  4575. if token=_ID then
  4576. begin
  4577. { this takes some time ... }
  4578. if (cs_support_macro in current_settings.moduleswitches) then
  4579. begin
  4580. mac:=tmacro(search_macro(pattern));
  4581. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  4582. begin
  4583. if (yylexcount<max_macro_nesting) and (macro_nesting_depth<max_macro_nesting) then
  4584. begin
  4585. mac.is_used:=true;
  4586. inc(yylexcount);
  4587. substitutemacro(pattern,mac.buftext,mac.buflen,
  4588. mac.fileinfo.line,mac.fileinfo.fileindex,false);
  4589. { handle empty macros }
  4590. if c=#0 then
  4591. begin
  4592. reload;
  4593. { avoid macro nesting error in case of
  4594. a sequence of empty macros, see #38802 }
  4595. dec(yylexcount);
  4596. readtoken(false);
  4597. end
  4598. else
  4599. begin
  4600. readtoken(false);
  4601. { that's all folks }
  4602. dec(yylexcount);
  4603. end;
  4604. exit;
  4605. end
  4606. else
  4607. Message(scan_w_macro_too_deep);
  4608. end;
  4609. end;
  4610. end;
  4611. { return token }
  4612. goto exit_label;
  4613. end
  4614. else
  4615. begin
  4616. idtoken:=_NOID;
  4617. case c of
  4618. '$' :
  4619. begin
  4620. readnumber;
  4621. token:=_INTCONST;
  4622. goto exit_label;
  4623. end;
  4624. '%' :
  4625. begin
  4626. if [m_fpc,m_delphi] * current_settings.modeswitches = [] then
  4627. Illegal_Char(c)
  4628. else
  4629. begin
  4630. readnumber;
  4631. token:=_INTCONST;
  4632. goto exit_label;
  4633. end;
  4634. end;
  4635. '&' :
  4636. begin
  4637. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  4638. begin
  4639. readnumber;
  4640. if length(pattern)=1 then
  4641. begin
  4642. { does really an identifier follow? }
  4643. if not (c in ['_','A'..'Z','a'..'z']) then
  4644. message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  4645. readstring;
  4646. token:=_ID;
  4647. idtoken:=_ID;
  4648. end
  4649. else
  4650. token:=_INTCONST;
  4651. goto exit_label;
  4652. end
  4653. else if m_mac in current_settings.modeswitches then
  4654. begin
  4655. readchar;
  4656. token:=_AMPERSAND;
  4657. goto exit_label;
  4658. end
  4659. else
  4660. Illegal_Char(c);
  4661. end;
  4662. '0'..'9' :
  4663. begin
  4664. readnumber;
  4665. if (c in ['.','e','E']) then
  4666. begin
  4667. { first check for a . }
  4668. if c='.' then
  4669. begin
  4670. cachenexttokenpos;
  4671. readchar;
  4672. { is it a .. from a range? }
  4673. case c of
  4674. '.' :
  4675. begin
  4676. readchar;
  4677. token:=_INTCONST;
  4678. nexttoken:=_POINTPOINT;
  4679. goto exit_label;
  4680. end;
  4681. ')' :
  4682. begin
  4683. readchar;
  4684. token:=_INTCONST;
  4685. nexttoken:=_RECKKLAMMER;
  4686. goto exit_label;
  4687. end;
  4688. '0'..'9' :
  4689. begin
  4690. { insert the number after the . }
  4691. pattern:=pattern+'.';
  4692. firstdigitread:=false;
  4693. while (c in ['0'..'9']) or
  4694. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  4695. begin
  4696. if c<>'_' then
  4697. pattern:=pattern+c;
  4698. readchar;
  4699. firstdigitread:=true;
  4700. end;
  4701. end;
  4702. else
  4703. begin
  4704. token:=_INTCONST;
  4705. nexttoken:=_POINT;
  4706. goto exit_label;
  4707. end;
  4708. end;
  4709. end;
  4710. { E can also follow after a point is scanned }
  4711. if c in ['e','E'] then
  4712. begin
  4713. pattern:=pattern+'E';
  4714. readchar;
  4715. if c in ['-','+'] then
  4716. begin
  4717. pattern:=pattern+c;
  4718. readchar;
  4719. end;
  4720. if not(c in ['0'..'9']) then
  4721. Illegal_Char(c);
  4722. firstdigitread:=false;
  4723. while (c in ['0'..'9']) or
  4724. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  4725. begin
  4726. if c<>'_' then
  4727. pattern:=pattern+c;
  4728. readchar;
  4729. firstdigitread:=true;
  4730. end;
  4731. end;
  4732. token:=_REALNUMBER;
  4733. goto exit_label;
  4734. end;
  4735. token:=_INTCONST;
  4736. goto exit_label;
  4737. end;
  4738. ';' :
  4739. begin
  4740. readchar;
  4741. token:=_SEMICOLON;
  4742. goto exit_label;
  4743. end;
  4744. '[' :
  4745. begin
  4746. readchar;
  4747. token:=_LECKKLAMMER;
  4748. goto exit_label;
  4749. end;
  4750. ']' :
  4751. begin
  4752. readchar;
  4753. token:=_RECKKLAMMER;
  4754. goto exit_label;
  4755. end;
  4756. '(' :
  4757. begin
  4758. readchar;
  4759. case c of
  4760. '*' :
  4761. begin
  4762. skipoldtpcomment(true);
  4763. readtoken(false);
  4764. exit;
  4765. end;
  4766. '.' :
  4767. begin
  4768. readchar;
  4769. token:=_LECKKLAMMER;
  4770. goto exit_label;
  4771. end;
  4772. end;
  4773. token:=_LKLAMMER;
  4774. goto exit_label;
  4775. end;
  4776. ')' :
  4777. begin
  4778. readchar;
  4779. token:=_RKLAMMER;
  4780. goto exit_label;
  4781. end;
  4782. '+' :
  4783. begin
  4784. readchar;
  4785. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4786. begin
  4787. readchar;
  4788. token:=_PLUSASN;
  4789. goto exit_label;
  4790. end;
  4791. token:=_PLUS;
  4792. goto exit_label;
  4793. end;
  4794. '-' :
  4795. begin
  4796. readchar;
  4797. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4798. begin
  4799. readchar;
  4800. token:=_MINUSASN;
  4801. goto exit_label;
  4802. end;
  4803. token:=_MINUS;
  4804. goto exit_label;
  4805. end;
  4806. ':' :
  4807. begin
  4808. readchar;
  4809. if c='=' then
  4810. begin
  4811. readchar;
  4812. token:=_ASSIGNMENT;
  4813. goto exit_label;
  4814. end;
  4815. token:=_COLON;
  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:=_STARASN;
  4825. end
  4826. else
  4827. if c='*' then
  4828. begin
  4829. readchar;
  4830. token:=_STARSTAR;
  4831. end
  4832. else
  4833. token:=_STAR;
  4834. goto exit_label;
  4835. end;
  4836. '/' :
  4837. begin
  4838. readchar;
  4839. case c of
  4840. '=' :
  4841. begin
  4842. if (cs_support_c_operators in current_settings.moduleswitches) then
  4843. begin
  4844. readchar;
  4845. token:=_SLASHASN;
  4846. goto exit_label;
  4847. end;
  4848. end;
  4849. '/' :
  4850. begin
  4851. skipdelphicomment;
  4852. readtoken(false);
  4853. exit;
  4854. end;
  4855. end;
  4856. token:=_SLASH;
  4857. goto exit_label;
  4858. end;
  4859. '|' :
  4860. if m_mac in current_settings.modeswitches then
  4861. begin
  4862. readchar;
  4863. token:=_PIPE;
  4864. goto exit_label;
  4865. end
  4866. else
  4867. Illegal_Char(c);
  4868. '=' :
  4869. begin
  4870. readchar;
  4871. token:=_EQ;
  4872. goto exit_label;
  4873. end;
  4874. '.' :
  4875. begin
  4876. readchar;
  4877. case c of
  4878. '.' :
  4879. begin
  4880. readchar;
  4881. case c of
  4882. '.' :
  4883. begin
  4884. readchar;
  4885. token:=_POINTPOINTPOINT;
  4886. goto exit_label;
  4887. end;
  4888. else
  4889. begin
  4890. token:=_POINTPOINT;
  4891. goto exit_label;
  4892. end;
  4893. end;
  4894. end;
  4895. ')' :
  4896. begin
  4897. readchar;
  4898. token:=_RECKKLAMMER;
  4899. goto exit_label;
  4900. end;
  4901. end;
  4902. token:=_POINT;
  4903. goto exit_label;
  4904. end;
  4905. '@' :
  4906. begin
  4907. readchar;
  4908. token:=_KLAMMERAFFE;
  4909. goto exit_label;
  4910. end;
  4911. ',' :
  4912. begin
  4913. readchar;
  4914. token:=_COMMA;
  4915. goto exit_label;
  4916. end;
  4917. '''','#','^' :
  4918. begin
  4919. len:=0;
  4920. cstringpattern:='';
  4921. iswidestring:=false;
  4922. if c='^' then
  4923. begin
  4924. readchar;
  4925. c:=upcase(c);
  4926. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  4927. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  4928. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  4929. begin
  4930. token:=_CARET;
  4931. goto exit_label;
  4932. end
  4933. else
  4934. begin
  4935. inc(len);
  4936. setlength(cstringpattern,256);
  4937. if c<#64 then
  4938. cstringpattern[len]:=chr(ord(c)+64)
  4939. else
  4940. cstringpattern[len]:=chr(ord(c)-64);
  4941. readchar;
  4942. end;
  4943. end;
  4944. repeat
  4945. case c of
  4946. '#' :
  4947. begin
  4948. readchar; { read # }
  4949. case c of
  4950. '$':
  4951. begin
  4952. readchar; { read leading $ }
  4953. asciinr:='$';
  4954. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=7) do
  4955. begin
  4956. asciinr:=asciinr+c;
  4957. readchar;
  4958. end;
  4959. end;
  4960. '&':
  4961. begin
  4962. readchar; { read leading $ }
  4963. asciinr:='&';
  4964. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=8) do
  4965. begin
  4966. asciinr:=asciinr+c;
  4967. readchar;
  4968. end;
  4969. end;
  4970. '%':
  4971. begin
  4972. readchar; { read leading $ }
  4973. asciinr:='%';
  4974. while (upcase(c) in ['0','1']) and (length(asciinr)<=22) do
  4975. begin
  4976. asciinr:=asciinr+c;
  4977. readchar;
  4978. end;
  4979. end;
  4980. else
  4981. begin
  4982. asciinr:='';
  4983. while (c in ['0'..'9']) and (length(asciinr)<=8) do
  4984. begin
  4985. asciinr:=asciinr+c;
  4986. readchar;
  4987. end;
  4988. end;
  4989. end;
  4990. val(asciinr,m,code);
  4991. if (asciinr='') or (code<>0) then
  4992. Message(scan_e_illegal_char_const)
  4993. else if (m<0) or (m>255) or (length(asciinr)>3) then
  4994. begin
  4995. if (m>=0) and (m<=$10FFFF) then
  4996. begin
  4997. if not iswidestring then
  4998. begin
  4999. if len>0 then
  5000. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5001. else
  5002. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5003. iswidestring:=true;
  5004. len:=0;
  5005. end;
  5006. if m<=$FFFF then
  5007. concatwidestringchar(patternw,tcompilerwidechar(m))
  5008. else
  5009. begin
  5010. { split into surrogate pair }
  5011. dec(m,$10000);
  5012. concatwidestringchar(patternw,tcompilerwidechar((m shr 10) + $D800));
  5013. concatwidestringchar(patternw,tcompilerwidechar((m and $3FF) + $DC00));
  5014. end;
  5015. end
  5016. else
  5017. Message(scan_e_illegal_char_const)
  5018. end
  5019. else if iswidestring then
  5020. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  5021. else
  5022. begin
  5023. if len>=length(cstringpattern) then
  5024. setlength(cstringpattern,length(cstringpattern)+256);
  5025. inc(len);
  5026. cstringpattern[len]:=chr(m);
  5027. end;
  5028. end;
  5029. '''' :
  5030. begin
  5031. repeat
  5032. readchar;
  5033. case c of
  5034. #26 :
  5035. end_of_file;
  5036. #10,#13 :
  5037. Message(scan_f_string_exceeds_line);
  5038. '''' :
  5039. begin
  5040. readchar;
  5041. if c<>'''' then
  5042. break;
  5043. end;
  5044. end;
  5045. { interpret as utf-8 string? }
  5046. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  5047. begin
  5048. { convert existing string to an utf-8 string }
  5049. if not iswidestring then
  5050. begin
  5051. if len>0 then
  5052. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5053. else
  5054. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5055. iswidestring:=true;
  5056. len:=0;
  5057. end;
  5058. { four chars }
  5059. if (ord(c) and $f0)=$f0 then
  5060. begin
  5061. { this always represents a surrogate pair, so
  5062. read as 32-bit value and then split into
  5063. the corresponding pair of two wchars }
  5064. d:=ord(c) and $f;
  5065. readchar;
  5066. if (ord(c) and $c0)<>$80 then
  5067. message(scan_e_utf8_malformed);
  5068. d:=(d shl 6) or (ord(c) and $3f);
  5069. readchar;
  5070. if (ord(c) and $c0)<>$80 then
  5071. message(scan_e_utf8_malformed);
  5072. d:=(d shl 6) or (ord(c) and $3f);
  5073. readchar;
  5074. if (ord(c) and $c0)<>$80 then
  5075. message(scan_e_utf8_malformed);
  5076. d:=(d shl 6) or (ord(c) and $3f);
  5077. if d<$10000 then
  5078. message(scan_e_utf8_malformed);
  5079. d:=d-$10000;
  5080. { high surrogate }
  5081. w:=$d800+(d shr 10);
  5082. concatwidestringchar(patternw,w);
  5083. { low surrogate }
  5084. w:=$dc00+(d and $3ff);
  5085. concatwidestringchar(patternw,w);
  5086. end
  5087. { three chars }
  5088. else if (ord(c) and $e0)=$e0 then
  5089. begin
  5090. w:=ord(c) and $f;
  5091. readchar;
  5092. if (ord(c) and $c0)<>$80 then
  5093. message(scan_e_utf8_malformed);
  5094. w:=(w shl 6) or (ord(c) and $3f);
  5095. readchar;
  5096. if (ord(c) and $c0)<>$80 then
  5097. message(scan_e_utf8_malformed);
  5098. w:=(w shl 6) or (ord(c) and $3f);
  5099. concatwidestringchar(patternw,w);
  5100. end
  5101. { two chars }
  5102. else if (ord(c) and $c0)<>0 then
  5103. begin
  5104. w:=ord(c) and $1f;
  5105. readchar;
  5106. if (ord(c) and $c0)<>$80 then
  5107. message(scan_e_utf8_malformed);
  5108. w:=(w shl 6) or (ord(c) and $3f);
  5109. concatwidestringchar(patternw,w);
  5110. end
  5111. { illegal }
  5112. else if (ord(c) and $80)<>0 then
  5113. message(scan_e_utf8_malformed)
  5114. else
  5115. concatwidestringchar(patternw,tcompilerwidechar(c))
  5116. end
  5117. else if iswidestring then
  5118. begin
  5119. if current_settings.sourcecodepage=CP_UTF8 then
  5120. concatwidestringchar(patternw,ord(c))
  5121. else
  5122. concatwidestringchar(patternw,asciichar2unicode(c))
  5123. end
  5124. else
  5125. begin
  5126. if len>=length(cstringpattern) then
  5127. setlength(cstringpattern,length(cstringpattern)+256);
  5128. inc(len);
  5129. cstringpattern[len]:=c;
  5130. end;
  5131. until false;
  5132. end;
  5133. '^' :
  5134. begin
  5135. readchar;
  5136. c:=upcase(c);
  5137. if c<#64 then
  5138. c:=chr(ord(c)+64)
  5139. else
  5140. c:=chr(ord(c)-64);
  5141. if iswidestring then
  5142. concatwidestringchar(patternw,asciichar2unicode(c))
  5143. else
  5144. begin
  5145. if len>=length(cstringpattern) then
  5146. setlength(cstringpattern,length(cstringpattern)+256);
  5147. inc(len);
  5148. cstringpattern[len]:=c;
  5149. end;
  5150. readchar;
  5151. end;
  5152. else
  5153. break;
  5154. end;
  5155. until false;
  5156. { strings with length 1 become const chars }
  5157. if iswidestring then
  5158. begin
  5159. if patternw^.len=1 then
  5160. token:=_CWCHAR
  5161. else
  5162. token:=_CWSTRING;
  5163. end
  5164. else
  5165. begin
  5166. setlength(cstringpattern,len);
  5167. if length(cstringpattern)=1 then
  5168. begin
  5169. token:=_CCHAR;
  5170. pattern:=cstringpattern;
  5171. end
  5172. else
  5173. token:=_CSTRING;
  5174. end;
  5175. goto exit_label;
  5176. end;
  5177. '>' :
  5178. begin
  5179. readchar;
  5180. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5181. token:=_RSHARPBRACKET
  5182. else
  5183. begin
  5184. case c of
  5185. '=' :
  5186. begin
  5187. readchar;
  5188. token:=_GTE;
  5189. goto exit_label;
  5190. end;
  5191. '>' :
  5192. begin
  5193. readchar;
  5194. token:=_OP_SHR;
  5195. goto exit_label;
  5196. end;
  5197. '<' :
  5198. begin { >< is for a symetric diff for sets }
  5199. readchar;
  5200. token:=_SYMDIF;
  5201. goto exit_label;
  5202. end;
  5203. end;
  5204. token:=_GT;
  5205. end;
  5206. goto exit_label;
  5207. end;
  5208. '<' :
  5209. begin
  5210. readchar;
  5211. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5212. token:=_LSHARPBRACKET
  5213. else
  5214. begin
  5215. case c of
  5216. '>' :
  5217. begin
  5218. readchar;
  5219. token:=_NE;
  5220. goto exit_label;
  5221. end;
  5222. '=' :
  5223. begin
  5224. readchar;
  5225. token:=_LTE;
  5226. goto exit_label;
  5227. end;
  5228. '<' :
  5229. begin
  5230. readchar;
  5231. token:=_OP_SHL;
  5232. goto exit_label;
  5233. end;
  5234. end;
  5235. token:=_LT;
  5236. end;
  5237. goto exit_label;
  5238. end;
  5239. #26 :
  5240. begin
  5241. token:=_EOF;
  5242. checkpreprocstack;
  5243. goto exit_label;
  5244. end;
  5245. else if inputfile.internally_generated_macro and
  5246. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  5247. begin
  5248. token:=_ID;
  5249. readstring;
  5250. end
  5251. else
  5252. Illegal_Char(c);
  5253. end;
  5254. end;
  5255. exit_label:
  5256. lasttoken:=token;
  5257. end;
  5258. function tscannerfile.readpreproc:ttoken;
  5259. var
  5260. low,high,mid: longint;
  5261. optoken: ttoken;
  5262. begin
  5263. skipspace;
  5264. case c of
  5265. '_',
  5266. 'A'..'Z',
  5267. 'a'..'z' :
  5268. begin
  5269. readstring;
  5270. optoken:=_ID;
  5271. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  5272. begin
  5273. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  5274. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  5275. while low<high do
  5276. begin
  5277. mid:=(high+low+1) shr 1;
  5278. if pattern<tokeninfo^[ttoken(mid)].str then
  5279. high:=mid-1
  5280. else
  5281. low:=mid;
  5282. end;
  5283. with tokeninfo^[ttoken(high)] do
  5284. if pattern=str then
  5285. begin
  5286. if (keyword*current_settings.modeswitches)<>[] then
  5287. if op=NOTOKEN then
  5288. optoken:=ttoken(high)
  5289. else
  5290. optoken:=op;
  5291. end;
  5292. if not (optoken in preproc_operators) then
  5293. optoken:=_ID;
  5294. end;
  5295. current_scanner.preproc_pattern:=pattern;
  5296. readpreproc:=optoken;
  5297. end;
  5298. '''' :
  5299. begin
  5300. readquotedstring;
  5301. current_scanner.preproc_pattern:=cstringpattern;
  5302. readpreproc:=_CSTRING;
  5303. end;
  5304. '0'..'9' :
  5305. begin
  5306. readnumber;
  5307. if (c in ['.','e','E']) then
  5308. begin
  5309. { first check for a . }
  5310. if c='.' then
  5311. begin
  5312. readchar;
  5313. if c in ['0'..'9'] then
  5314. begin
  5315. { insert the number after the . }
  5316. pattern:=pattern+'.';
  5317. while c in ['0'..'9'] do
  5318. begin
  5319. pattern:=pattern+c;
  5320. readchar;
  5321. end;
  5322. end
  5323. else
  5324. Illegal_Char(c);
  5325. end;
  5326. { E can also follow after a point is scanned }
  5327. if c in ['e','E'] then
  5328. begin
  5329. pattern:=pattern+'E';
  5330. readchar;
  5331. if c in ['-','+'] then
  5332. begin
  5333. pattern:=pattern+c;
  5334. readchar;
  5335. end;
  5336. if not(c in ['0'..'9']) then
  5337. Illegal_Char(c);
  5338. while c in ['0'..'9'] do
  5339. begin
  5340. pattern:=pattern+c;
  5341. readchar;
  5342. end;
  5343. end;
  5344. readpreproc:=_REALNUMBER;
  5345. end
  5346. else
  5347. readpreproc:=_INTCONST;
  5348. current_scanner.preproc_pattern:=pattern;
  5349. end;
  5350. '$','%':
  5351. begin
  5352. readnumber;
  5353. current_scanner.preproc_pattern:=pattern;
  5354. readpreproc:=_INTCONST;
  5355. end;
  5356. '&' :
  5357. begin
  5358. readnumber;
  5359. if length(pattern)=1 then
  5360. begin
  5361. readstring;
  5362. readpreproc:=_ID;
  5363. end
  5364. else
  5365. readpreproc:=_INTCONST;
  5366. current_scanner.preproc_pattern:=pattern;
  5367. end;
  5368. '.' :
  5369. begin
  5370. readchar;
  5371. readpreproc:=_POINT;
  5372. end;
  5373. ',' :
  5374. begin
  5375. readchar;
  5376. readpreproc:=_COMMA;
  5377. end;
  5378. '}' :
  5379. begin
  5380. readpreproc:=_END;
  5381. end;
  5382. '(' :
  5383. begin
  5384. readchar;
  5385. readpreproc:=_LKLAMMER;
  5386. end;
  5387. ')' :
  5388. begin
  5389. readchar;
  5390. readpreproc:=_RKLAMMER;
  5391. end;
  5392. '[' :
  5393. begin
  5394. readchar;
  5395. readpreproc:=_LECKKLAMMER;
  5396. end;
  5397. ']' :
  5398. begin
  5399. readchar;
  5400. readpreproc:=_RECKKLAMMER;
  5401. end;
  5402. '+' :
  5403. begin
  5404. readchar;
  5405. readpreproc:=_PLUS;
  5406. end;
  5407. '-' :
  5408. begin
  5409. readchar;
  5410. readpreproc:=_MINUS;
  5411. end;
  5412. '*' :
  5413. begin
  5414. readchar;
  5415. readpreproc:=_STAR;
  5416. end;
  5417. '/' :
  5418. begin
  5419. readchar;
  5420. readpreproc:=_SLASH;
  5421. end;
  5422. '=' :
  5423. begin
  5424. readchar;
  5425. readpreproc:=_EQ;
  5426. end;
  5427. '>' :
  5428. begin
  5429. readchar;
  5430. if c='=' then
  5431. begin
  5432. readchar;
  5433. readpreproc:=_GTE;
  5434. end
  5435. else
  5436. readpreproc:=_GT;
  5437. end;
  5438. '<' :
  5439. begin
  5440. readchar;
  5441. case c of
  5442. '>' :
  5443. begin
  5444. readchar;
  5445. readpreproc:=_NE;
  5446. end;
  5447. '=' :
  5448. begin
  5449. readchar;
  5450. readpreproc:=_LTE;
  5451. end;
  5452. else
  5453. readpreproc:=_LT;
  5454. end;
  5455. end;
  5456. #26 :
  5457. begin
  5458. readpreproc:=_EOF;
  5459. checkpreprocstack;
  5460. end;
  5461. else
  5462. begin
  5463. Illegal_Char(c);
  5464. readpreproc:=NOTOKEN;
  5465. end;
  5466. end;
  5467. end;
  5468. function tscannerfile.readpreprocint(var value:int64;const place:string):boolean;
  5469. var
  5470. hs : texprvalue;
  5471. begin
  5472. hs:=preproc_comp_expr;
  5473. if hs.isInt then
  5474. begin
  5475. value:=hs.asInt64;
  5476. result:=true;
  5477. end
  5478. else
  5479. begin
  5480. hs.error('Integer',place);
  5481. result:=false;
  5482. end;
  5483. hs.free;
  5484. end;
  5485. function tscannerfile.asmgetchar : char;
  5486. begin
  5487. readchar;
  5488. repeat
  5489. case c of
  5490. #26 :
  5491. begin
  5492. reload;
  5493. if (c=#26) and not assigned(inputfile.next) then
  5494. end_of_file;
  5495. continue;
  5496. end;
  5497. else
  5498. begin
  5499. asmgetchar:=c;
  5500. exit;
  5501. end;
  5502. end;
  5503. until false;
  5504. end;
  5505. {*****************************************************************************
  5506. Helpers
  5507. *****************************************************************************}
  5508. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5509. begin
  5510. if dm in [directive_all, directive_turbo] then
  5511. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  5512. if dm in [directive_all, directive_mac] then
  5513. tdirectiveitem.create(mac_scannerdirectives,s,p);
  5514. end;
  5515. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5516. begin
  5517. if dm in [directive_all, directive_turbo] then
  5518. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  5519. if dm in [directive_all, directive_mac] then
  5520. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  5521. end;
  5522. {*****************************************************************************
  5523. Initialization
  5524. *****************************************************************************}
  5525. procedure InitScanner;
  5526. begin
  5527. InitWideString(patternw);
  5528. turbo_scannerdirectives:=TFPHashObjectList.Create;
  5529. mac_scannerdirectives:=TFPHashObjectList.Create;
  5530. { Common directives and conditionals }
  5531. AddDirective('I',directive_all, @dir_include);
  5532. AddDirective('DEFINE',directive_all, @dir_define);
  5533. AddDirective('UNDEF',directive_all, @dir_undef);
  5534. AddConditional('IF',directive_all, @dir_if);
  5535. AddConditional('IFDEF',directive_all, @dir_ifdef);
  5536. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  5537. AddConditional('ELSE',directive_all, @dir_else);
  5538. AddConditional('ELSEIF',directive_all, @dir_elseif);
  5539. AddConditional('ENDIF',directive_all, @dir_endif);
  5540. { Directives and conditionals for all modes except mode macpas}
  5541. AddDirective('INCLUDE',directive_turbo, @dir_include);
  5542. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  5543. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  5544. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  5545. AddConditional('IFEND',directive_turbo, @dir_ifend);
  5546. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  5547. { Directives and conditionals for mode macpas: }
  5548. AddDirective('SETC',directive_mac, @dir_setc);
  5549. AddDirective('DEFINEC',directive_mac, @dir_definec);
  5550. AddDirective('UNDEFC',directive_mac, @dir_undef);
  5551. AddConditional('IFC',directive_mac, @dir_if);
  5552. AddConditional('ELSEC',directive_mac, @dir_else);
  5553. AddConditional('ELIFC',directive_mac, @dir_elseif);
  5554. AddConditional('ENDC',directive_mac, @dir_endif);
  5555. end;
  5556. procedure DoneScanner;
  5557. begin
  5558. turbo_scannerdirectives.Free;
  5559. mac_scannerdirectives.Free;
  5560. DoneWideString(patternw);
  5561. end;
  5562. end.