scanner.pas 239 KB

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