scanner.pas 239 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002
  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. {$ifdef FPC_BIG_ENDIAN}
  3069. val:=swapendian(val);
  3070. {$endif}
  3071. recordtokenbuf.write(val,sizeof(byte));
  3072. end;
  3073. procedure tscannerfile.tokenwriteword(val : word);
  3074. begin
  3075. recordtokenbuf.write(val,sizeof(word));
  3076. end;
  3077. procedure tscannerfile.tokenwritelongword(val : longword);
  3078. begin
  3079. recordtokenbuf.write(val,sizeof(longword));
  3080. end;
  3081. function tscannerfile.tokenreadsizeint : asizeint;
  3082. var
  3083. val : asizeint;
  3084. begin
  3085. replaytokenbuf.read(val,sizeof(asizeint));
  3086. if change_endian_for_replay then
  3087. val:=swapendian(val);
  3088. result:=val;
  3089. end;
  3090. function tscannerfile.tokenreadlongword : longword;
  3091. var
  3092. val : longword;
  3093. begin
  3094. replaytokenbuf.read(val,sizeof(longword));
  3095. if change_endian_for_replay then
  3096. val:=swapendian(val);
  3097. result:=val;
  3098. end;
  3099. function tscannerfile.tokenreadlongint : longint;
  3100. var
  3101. val : longint;
  3102. begin
  3103. replaytokenbuf.read(val,sizeof(longint));
  3104. if change_endian_for_replay then
  3105. val:=swapendian(val);
  3106. result:=val;
  3107. end;
  3108. function tscannerfile.tokenreadshortint : shortint;
  3109. var
  3110. val : shortint;
  3111. begin
  3112. replaytokenbuf.read(val,sizeof(shortint));
  3113. result:=val;
  3114. end;
  3115. function tscannerfile.tokenreadbyte : byte;
  3116. var
  3117. val : byte;
  3118. begin
  3119. replaytokenbuf.read(val,sizeof(byte));
  3120. result:=val;
  3121. end;
  3122. function tscannerfile.tokenreadsmallint : smallint;
  3123. var
  3124. val : smallint;
  3125. begin
  3126. replaytokenbuf.read(val,sizeof(smallint));
  3127. if change_endian_for_replay then
  3128. val:=swapendian(val);
  3129. result:=val;
  3130. end;
  3131. function tscannerfile.tokenreadword : word;
  3132. var
  3133. val : word;
  3134. begin
  3135. replaytokenbuf.read(val,sizeof(word));
  3136. if change_endian_for_replay then
  3137. val:=swapendian(val);
  3138. result:=val;
  3139. end;
  3140. function tscannerfile.tokenreadenum(size : longint) : longword;
  3141. begin
  3142. if size=1 then
  3143. result:=tokenreadbyte
  3144. else if size=2 then
  3145. result:=tokenreadword
  3146. else if size=4 then
  3147. result:=tokenreadlongword
  3148. else
  3149. internalerror(2013112901);
  3150. end;
  3151. procedure tscannerfile.tokenreadset(var b;size : longint);
  3152. var
  3153. i : longint;
  3154. begin
  3155. replaytokenbuf.read(b,size);
  3156. if change_endian_for_replay then
  3157. for i:=0 to size-1 do
  3158. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  3159. end;
  3160. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  3161. begin
  3162. recordtokenbuf.write(b,size);
  3163. end;
  3164. procedure tscannerfile.tokenwriteset(var b;size : longint);
  3165. begin
  3166. recordtokenbuf.write(b,size);
  3167. end;
  3168. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  3169. { This procedure
  3170. needs to be changed whenever
  3171. globals.tsettings type is changed,
  3172. the problem is that no error will appear
  3173. before tests with generics are tested. PM }
  3174. var
  3175. startpos, endpos : longword;
  3176. begin
  3177. { WARNING all those fields need to be in the correct
  3178. order otherwise cross_endian PPU reading will fail }
  3179. startpos:=replaytokenbuf.pos;
  3180. with asettings do
  3181. begin
  3182. alignment.procalign:=tokenreadlongint;
  3183. alignment.loopalign:=tokenreadlongint;
  3184. alignment.jumpalign:=tokenreadlongint;
  3185. alignment.jumpalignskipmax:=tokenreadlongint;
  3186. alignment.coalescealign:=tokenreadlongint;
  3187. alignment.coalescealignskipmax:=tokenreadlongint;
  3188. alignment.constalignmin:=tokenreadlongint;
  3189. alignment.constalignmax:=tokenreadlongint;
  3190. alignment.varalignmin:=tokenreadlongint;
  3191. alignment.varalignmax:=tokenreadlongint;
  3192. alignment.localalignmin:=tokenreadlongint;
  3193. alignment.localalignmax:=tokenreadlongint;
  3194. alignment.recordalignmin:=tokenreadlongint;
  3195. alignment.recordalignmax:=tokenreadlongint;
  3196. alignment.maxCrecordalign:=tokenreadlongint;
  3197. tokenreadset(globalswitches,sizeof(globalswitches));
  3198. tokenreadset(targetswitches,sizeof(targetswitches));
  3199. tokenreadset(moduleswitches,sizeof(moduleswitches));
  3200. tokenreadset(localswitches,sizeof(localswitches));
  3201. tokenreadset(modeswitches,sizeof(modeswitches));
  3202. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  3203. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  3204. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  3205. tokenreadset(debugswitches,sizeof(debugswitches));
  3206. { 0: old behaviour for sets <=256 elements
  3207. >0: round to this size }
  3208. setalloc:=tokenreadshortint;
  3209. packenum:=tokenreadshortint;
  3210. packrecords:=tokenreadshortint;
  3211. maxfpuregisters:=tokenreadshortint;
  3212. verbosity:=tokenreadlongint;
  3213. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  3214. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  3215. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  3216. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  3217. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  3218. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  3219. { tstringencoding is word type,
  3220. thus this should be OK here }
  3221. sourcecodepage:=tstringEncoding(tokenreadword);
  3222. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  3223. disabledircache:=boolean(tokenreadbyte);
  3224. tlsmodel:=ttlsmodel(tokenreadenum(sizeof(ttlsmodel)));
  3225. { TH: Since the field was conditional originally, it was not stored in PPUs. }
  3226. { While adding ControllerSupport constant, I decided not to store ct_none }
  3227. { on targets not supporting controllers, but this might be changed here and }
  3228. { in tokenwritesettings in the future to unify the PPU structure and handling }
  3229. { of this field in the compiler. }
  3230. {$PUSH}
  3231. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  3232. if ControllerSupport then
  3233. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)))
  3234. else
  3235. ControllerType:=ct_none;
  3236. {$POP}
  3237. lineendingtype:=tlineendingtype(tokenreadenum(sizeof(tlineendingtype)));
  3238. whitespacetrimcount:=tokenreadword;
  3239. whitespacetrimauto:=boolean(tokenreadbyte);
  3240. endpos:=replaytokenbuf.pos;
  3241. if endpos-startpos<>expected_size then
  3242. Comment(V_Error,'Wrong size of Settings read-in');
  3243. end;
  3244. end;
  3245. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  3246. { This procedure
  3247. needs to be changed whenever
  3248. globals.tsettings type is changed,
  3249. the problem is that no error will appear
  3250. before tests with generics are tested. PM }
  3251. var
  3252. sizepos, startpos, endpos : longword;
  3253. begin
  3254. { WARNING all those fields need to be in the correct
  3255. order otherwise cross_endian PPU reading will fail }
  3256. sizepos:=recordtokenbuf.pos;
  3257. size:=0;
  3258. tokenwritesizeint(size);
  3259. startpos:=recordtokenbuf.pos;
  3260. with asettings do
  3261. begin
  3262. tokenwritelongint(alignment.procalign);
  3263. tokenwritelongint(alignment.loopalign);
  3264. tokenwritelongint(alignment.jumpalign);
  3265. tokenwritelongint(alignment.jumpalignskipmax);
  3266. tokenwritelongint(alignment.coalescealign);
  3267. tokenwritelongint(alignment.coalescealignskipmax);
  3268. tokenwritelongint(alignment.constalignmin);
  3269. tokenwritelongint(alignment.constalignmax);
  3270. tokenwritelongint(alignment.varalignmin);
  3271. tokenwritelongint(alignment.varalignmax);
  3272. tokenwritelongint(alignment.localalignmin);
  3273. tokenwritelongint(alignment.localalignmax);
  3274. tokenwritelongint(alignment.recordalignmin);
  3275. tokenwritelongint(alignment.recordalignmax);
  3276. tokenwritelongint(alignment.maxCrecordalign);
  3277. tokenwriteset(globalswitches,sizeof(globalswitches));
  3278. tokenwriteset(targetswitches,sizeof(targetswitches));
  3279. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  3280. tokenwriteset(localswitches,sizeof(localswitches));
  3281. tokenwriteset(modeswitches,sizeof(modeswitches));
  3282. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  3283. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  3284. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  3285. tokenwriteset(debugswitches,sizeof(debugswitches));
  3286. { 0: old behaviour for sets <=256 elements
  3287. >0: round to this size }
  3288. tokenwriteshortint(setalloc);
  3289. tokenwriteshortint(packenum);
  3290. tokenwriteshortint(packrecords);
  3291. tokenwriteshortint(maxfpuregisters);
  3292. tokenwritelongint(verbosity);
  3293. tokenwriteenum(cputype,sizeof(tcputype));
  3294. tokenwriteenum(optimizecputype,sizeof(tcputype));
  3295. tokenwriteenum(fputype,sizeof(tfputype));
  3296. tokenwriteenum(asmmode,sizeof(tasmmode));
  3297. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  3298. tokenwriteenum(defproccall,sizeof(tproccalloption));
  3299. { tstringencoding is word type,
  3300. thus this should be OK here }
  3301. tokenwriteword(sourcecodepage);
  3302. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  3303. recordtokenbuf.write(byte(disabledircache),1);
  3304. tokenwriteenum(tlsmodel,sizeof(tlsmodel));
  3305. { TH: See note about controllertype field in tokenreadsettings. }
  3306. {$PUSH}
  3307. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  3308. if ControllerSupport then
  3309. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  3310. {$POP}
  3311. tokenwriteenum(lineendingtype,sizeof(tlineendingtype));
  3312. tokenwriteword(whitespacetrimcount);
  3313. tokenwritebyte(byte(whitespacetrimauto));
  3314. endpos:=recordtokenbuf.pos;
  3315. size:=endpos-startpos;
  3316. recordtokenbuf.seek(sizepos);
  3317. tokenwritesizeint(size);
  3318. recordtokenbuf.seek(endpos);
  3319. end;
  3320. end;
  3321. procedure tscannerfile.recordtoken;
  3322. var
  3323. t : ttoken;
  3324. s : tspecialgenerictoken;
  3325. len,msgnb,copy_size : asizeint;
  3326. val : longint;
  3327. b : byte;
  3328. pmsg : pmessagestaterecord;
  3329. begin
  3330. if not assigned(recordtokenbuf) then
  3331. internalerror(200511176);
  3332. t:=_GENERICSPECIALTOKEN;
  3333. { ensure that all fields of settings are up to date }
  3334. current_settings.verbosity:=status.verbosity;
  3335. { settings changed? }
  3336. { last field pmessage is handled separately below in
  3337. ST_LOADMESSAGES }
  3338. if CompareByte(current_settings,last_settings,
  3339. sizeof(current_settings)-sizeof(pointer))<>0 then
  3340. begin
  3341. { use a special token to record it }
  3342. s:=ST_LOADSETTINGS;
  3343. writetoken(t);
  3344. recordtokenbuf.write(s,1);
  3345. copy_size:=sizeof(current_settings)-sizeof(pointer);
  3346. tokenwritesettings(current_settings,copy_size);
  3347. last_settings:=current_settings;
  3348. end;
  3349. if current_settings.pmessage<>last_message then
  3350. begin
  3351. { use a special token to record it }
  3352. s:=ST_LOADMESSAGES;
  3353. writetoken(t);
  3354. recordtokenbuf.write(s,1);
  3355. msgnb:=0;
  3356. pmsg:=current_settings.pmessage;
  3357. while assigned(pmsg) do
  3358. begin
  3359. if msgnb=high(asizeint) then
  3360. { Too many messages }
  3361. internalerror(2011090401);
  3362. inc(msgnb);
  3363. pmsg:=pmsg^.next;
  3364. end;
  3365. tokenwritesizeint(msgnb);
  3366. pmsg:=current_settings.pmessage;
  3367. while assigned(pmsg) do
  3368. begin
  3369. { What about endianess here?}
  3370. { SB: this is handled by tokenreadlongint }
  3371. val:=pmsg^.value;
  3372. tokenwritelongint(val);
  3373. val:=ord(pmsg^.state);
  3374. tokenwritelongint(val);
  3375. pmsg:=pmsg^.next;
  3376. end;
  3377. last_message:=current_settings.pmessage;
  3378. end;
  3379. { file pos changes? }
  3380. if current_tokenpos.fileindex<>last_filepos.fileindex then
  3381. begin
  3382. s:=ST_FILEINDEX;
  3383. writetoken(t);
  3384. recordtokenbuf.write(s,1);
  3385. tokenwriteword(current_tokenpos.fileindex);
  3386. last_filepos.fileindex:=current_tokenpos.fileindex;
  3387. last_filepos.line:=0;
  3388. end;
  3389. if current_tokenpos.line<>last_filepos.line then
  3390. begin
  3391. s:=ST_LINE;
  3392. writetoken(t);
  3393. recordtokenbuf.write(s,1);
  3394. tokenwritelongint(current_tokenpos.line);
  3395. last_filepos.line:=current_tokenpos.line;
  3396. last_filepos.column:=0;
  3397. end;
  3398. if current_tokenpos.column<>last_filepos.column then
  3399. begin
  3400. s:=ST_COLUMN;
  3401. writetoken(t);
  3402. { can the column be written packed? }
  3403. if current_tokenpos.column<$80 then
  3404. begin
  3405. b:=$80 or current_tokenpos.column;
  3406. recordtokenbuf.write(b,1);
  3407. end
  3408. else
  3409. begin
  3410. recordtokenbuf.write(s,1);
  3411. tokenwriteword(current_tokenpos.column);
  3412. end;
  3413. last_filepos.column:=current_tokenpos.column;
  3414. end;
  3415. writetoken(token);
  3416. if token<>_GENERICSPECIALTOKEN then
  3417. writetoken(idtoken);
  3418. case token of
  3419. _CWCHAR,
  3420. _CWSTRING :
  3421. begin
  3422. tokenwritesizeint(patternw.len);
  3423. if patternw.len>0 then
  3424. recordtokenbuf.write(patternw.data[0],patternw.len*sizeof(tcompilerwidechar));
  3425. end;
  3426. _CSTRING:
  3427. begin
  3428. len:=length(cstringpattern);
  3429. tokenwritesizeint(len);
  3430. if len>0 then
  3431. recordtokenbuf.write(cstringpattern[1],len);
  3432. end;
  3433. _CCHAR,
  3434. _INTCONST,
  3435. _REALNUMBER :
  3436. begin
  3437. { pexpr.pas messes with pattern in case of negative integer consts,
  3438. see around line 2562 the comment of JM; remove the - before recording it
  3439. (FK)
  3440. }
  3441. if (token=_INTCONST) and (pattern[1]='-') then
  3442. delete(pattern,1,1);
  3443. recordtokenbuf.write(pattern[0],1);
  3444. recordtokenbuf.write(pattern[1],length(pattern));
  3445. end;
  3446. _ID :
  3447. begin
  3448. recordtokenbuf.write(orgpattern[0],1);
  3449. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  3450. end;
  3451. else
  3452. ;
  3453. end;
  3454. end;
  3455. procedure tscannerfile.startreplaytokens(buf:tdynamicarray; change_endian:boolean);
  3456. begin
  3457. if not assigned(buf) then
  3458. internalerror(200511175);
  3459. { save current scanner state }
  3460. replaystack:=treplaystack.create(token,idtoken,orgpattern,pattern,
  3461. cstringpattern,patternw,current_settings,replaytokenbuf,change_endian_for_replay,
  3462. pendingstate,status.verbosity,replaystack);
  3463. {$ifdef check_inputpointer_limits}
  3464. if assigned(hidden_inputpointer) then
  3465. dec_inputpointer;
  3466. {$else not check_inputpointer_limits}
  3467. if assigned(inputpointer) then
  3468. dec(inputpointer);
  3469. {$endif check_inputpointer_limits}
  3470. { install buffer }
  3471. replaytokenbuf:=buf;
  3472. { ensure that existing message state records won't be freed }
  3473. current_settings.pmessage:=nil;
  3474. pendingstate:=default(tpendingstate);
  3475. { Initialize value of change_endian_for_replay variable }
  3476. change_endian_for_replay:=change_endian;
  3477. { reload next token }
  3478. replaytokenbuf.seek(0);
  3479. replaytoken;
  3480. end;
  3481. function tscannerfile.readtoken: ttoken;
  3482. var
  3483. b,b2 : byte;
  3484. begin
  3485. replaytokenbuf.read(b,1);
  3486. if (b and $80)<>0 then
  3487. begin
  3488. replaytokenbuf.read(b2,1);
  3489. result:=ttoken(((b and $7f) shl 8) or b2);
  3490. end
  3491. else
  3492. result:=ttoken(b);
  3493. end;
  3494. procedure tscannerfile.replaytoken;
  3495. var
  3496. wlen,mesgnb,copy_size : asizeint;
  3497. specialtoken : tspecialgenerictoken;
  3498. i : byte;
  3499. pmsg,prevmsg : pmessagestaterecord;
  3500. msgset : thashset;
  3501. msgfound : boolean;
  3502. begin
  3503. if not assigned(replaytokenbuf) then
  3504. internalerror(200511177);
  3505. { End of replay buffer? Then load the next char from the file again }
  3506. if replaytokenbuf.pos>=replaytokenbuf.size then
  3507. begin
  3508. token:=replaystack.token;
  3509. idtoken:=replaystack.idtoken;
  3510. pattern:=replaystack.pattern;
  3511. orgpattern:=replaystack.orgpattern;
  3512. copywidestring(replaystack.patternw,patternw);
  3513. cstringpattern:=replaystack.cstringpattern;
  3514. replaytokenbuf:=replaystack.tokenbuf;
  3515. change_endian_for_replay:=replaystack.tokenbuf_needs_swapping;
  3516. { restore compiler settings }
  3517. current_settings:=replaystack.settings;
  3518. pendingstate:=replaystack.pending;
  3519. if assigned(pendingstate.nextmessagerecord) then
  3520. FreeLocalVerbosity(pendingstate.nextmessagerecord);
  3521. recordpendingverbosityfullswitch(replaystack.verbosity);
  3522. pendingstate.nextmessagerecord:=current_settings.pmessage;
  3523. current_settings.pmessage:=nil;
  3524. popreplaystack;
  3525. {$ifdef check_inputpointer_limits}
  3526. if assigned(hidden_inputpointer) then
  3527. begin
  3528. c:=get_inputpointer_char;
  3529. inc_inputpointer;
  3530. end;
  3531. {$else not check_inputpointer_limits}
  3532. if assigned(inputpointer) then
  3533. begin
  3534. c:=inputpointer^;
  3535. inc(inputpointer);
  3536. end;
  3537. {$endif check_inputpointer_limits}
  3538. exit;
  3539. end;
  3540. repeat
  3541. { load token from the buffer }
  3542. token:=readtoken;
  3543. if token<>_GENERICSPECIALTOKEN then
  3544. idtoken:=readtoken
  3545. else
  3546. idtoken:=_NOID;
  3547. case token of
  3548. _CWCHAR,
  3549. _CWSTRING :
  3550. begin
  3551. wlen:=tokenreadsizeint;
  3552. setlengthwidestring(patternw,wlen);
  3553. if wlen>0 then
  3554. replaytokenbuf.read(patternw.data[0],patternw.len*sizeof(tcompilerwidechar));
  3555. orgpattern:='';
  3556. pattern:='';
  3557. cstringpattern:='';
  3558. end;
  3559. _CSTRING:
  3560. begin
  3561. wlen:=tokenreadsizeint;
  3562. if wlen>0 then
  3563. begin
  3564. setlength(cstringpattern,wlen);
  3565. replaytokenbuf.read(cstringpattern[1],wlen);
  3566. end
  3567. else
  3568. cstringpattern:='';
  3569. orgpattern:='';
  3570. pattern:='';
  3571. end;
  3572. _CCHAR,
  3573. _INTCONST,
  3574. _REALNUMBER :
  3575. begin
  3576. replaytokenbuf.read(pattern[0],1);
  3577. replaytokenbuf.read(pattern[1],length(pattern));
  3578. orgpattern:='';
  3579. end;
  3580. _ID :
  3581. begin
  3582. replaytokenbuf.read(orgpattern[0],1);
  3583. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  3584. pattern:=upper(orgpattern);
  3585. end;
  3586. _GENERICSPECIALTOKEN:
  3587. begin
  3588. replaytokenbuf.read(specialtoken,1);
  3589. { packed column? }
  3590. if (ord(specialtoken) and $80)<>0 then
  3591. begin
  3592. current_tokenpos.column:=ord(specialtoken) and $7f;
  3593. current_filepos:=current_tokenpos;
  3594. end
  3595. else
  3596. case specialtoken of
  3597. ST_LOADSETTINGS:
  3598. begin
  3599. copy_size:=tokenreadsizeint;
  3600. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  3601. // internalerror(2011090501);
  3602. {
  3603. replaytokenbuf.read(current_settings,copy_size);
  3604. }
  3605. tokenreadsettings(current_settings,copy_size);
  3606. recordpendingverbosityfullswitch(current_settings.verbosity);
  3607. end;
  3608. ST_LOADMESSAGES:
  3609. begin
  3610. { free current and pending messages }
  3611. FreeLocalVerbosity(current_settings.pmessage);
  3612. FreeLocalVerbosity(pendingstate.nextmessagerecord);
  3613. { the message settings are stored from newest to oldest
  3614. change for the whole stack, so we only want to apply
  3615. the newest changes for each message type }
  3616. mesgnb:=tokenreadsizeint;
  3617. msgset:=thashset.create(min(mesgnb,10),false,false);
  3618. prevmsg:=nil;
  3619. pmsg:=nil;
  3620. for i:=1 to mesgnb do
  3621. begin
  3622. if not assigned(pmsg) then
  3623. new(pmsg);
  3624. pmsg^.value:=tokenreadlongint;
  3625. pmsg^.state:=tmsgstate(tokenreadlongint);
  3626. pmsg^.next:=nil;
  3627. msgfound:=false;
  3628. if assigned(msgset.findoradd(@pmsg^.value,sizeof(pmsg^.value),msgfound)) and msgfound then
  3629. continue;
  3630. if i=1 then
  3631. pendingstate.nextmessagerecord:=pmsg
  3632. else
  3633. prevmsg^.next:=pmsg;
  3634. prevmsg:=pmsg;
  3635. pmsg:=nil;
  3636. end;
  3637. if assigned(pmsg) then
  3638. dispose(pmsg);
  3639. msgset.free;
  3640. end;
  3641. ST_LINE:
  3642. begin
  3643. current_tokenpos.line:=tokenreadlongint;
  3644. current_filepos:=current_tokenpos;
  3645. end;
  3646. ST_COLUMN:
  3647. begin
  3648. current_tokenpos.column:=tokenreadword;
  3649. current_filepos:=current_tokenpos;
  3650. end;
  3651. ST_FILEINDEX:
  3652. begin
  3653. current_tokenpos.fileindex:=tokenreadword;
  3654. current_filepos:=current_tokenpos;
  3655. end;
  3656. end;
  3657. continue;
  3658. end;
  3659. else
  3660. ;
  3661. end;
  3662. break;
  3663. until false;
  3664. end;
  3665. procedure tscannerfile.addfile(hp:tinputfile);
  3666. begin
  3667. saveinputfile;
  3668. { add to list }
  3669. hp.next:=inputfile;
  3670. inputfile:=hp;
  3671. { load new inputfile }
  3672. restoreinputfile;
  3673. end;
  3674. procedure tscannerfile.reload;
  3675. var
  3676. wasmacro: Boolean;
  3677. begin
  3678. with inputfile do
  3679. begin
  3680. { when nothing more to read then leave immediatly, so we
  3681. don't change the current_filepos and leave it point to the last
  3682. char }
  3683. if (c=#26) and (not assigned(next)) then
  3684. exit;
  3685. repeat
  3686. { still more to read?, then change the #0 to a space so its seen
  3687. as a separator, this can't be used for macro's which can change
  3688. the place of the #0 in the buffer with tempopen }
  3689. if (c=#0) and (bufsize>0) and
  3690. not(inputfile.is_macro) and
  3691. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3692. (hidden_inputpointer-hidden_inputbuffer<bufsize) then
  3693. {$else not CHECK_INPUTPOINTER_LIMITS}
  3694. (inputpointer-inputbuffer<bufsize) then
  3695. {$endif CHECK_INPUTPOINTER_LIMITS}
  3696. begin
  3697. c:=' ';
  3698. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3699. inc_inputpointer;
  3700. {$else not CHECK_INPUTPOINTER_LIMITS}
  3701. inc(inputpointer);
  3702. {$endif CHECK_INPUTPOINTER_LIMITS}
  3703. exit;
  3704. end;
  3705. { can we read more from this file ? }
  3706. if (c<>#26) and (not endoffile) then
  3707. begin
  3708. readbuf;
  3709. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3710. hidden_inputpointer:=PAnsiChar(buf);
  3711. hidden_inputbuffer:=PAnsiChar(buf);
  3712. {$else not CHECK_INPUTPOINTER_LIMITS}
  3713. inputpointer:=PAnsiChar(buf);
  3714. inputbuffer:=PAnsiChar(buf);
  3715. {$endif CHECK_INPUTPOINTER_LIMITS}
  3716. inputstart:=bufstart;
  3717. { first line? }
  3718. if line_no=0 then
  3719. begin
  3720. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3721. c:=get_inputpointer_char;
  3722. { eat utf-8 signature? }
  3723. if (bufsize>2) and
  3724. (ord(get_inputpointer_char)=$ef) and
  3725. (ord(get_inputpointer_char(1))=$bb) and
  3726. (ord(get_inputpointer_char(2))=$bf) then
  3727. begin
  3728. {$else not CHECK_INPUTPOINTER_LIMITS}
  3729. c:=inputpointer^;
  3730. { eat utf-8 signature? }
  3731. if (bufsize>2) and
  3732. (ord(inputpointer^)=$ef) and
  3733. (ord((inputpointer+1)^)=$bb) and
  3734. (ord((inputpointer+2)^)=$bf) then
  3735. begin
  3736. {$endif CHECK_INPUTPOINTER_LIMITS}
  3737. (* we don't support including files with an UTF-8 bom
  3738. inside another file that wasn't encoded as UTF-8
  3739. already (we don't support {$codepage xxx} switches in
  3740. the middle of a file either) *)
  3741. if (current_settings.sourcecodepage<>CP_UTF8) and
  3742. not current_module.in_global then
  3743. Message(scanner_f_illegal_utf8_bom);
  3744. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3745. inc_inputpointer(3);
  3746. {$else not CHECK_INPUTPOINTER_LIMITS}
  3747. inc(inputpointer,3);
  3748. {$endif CHECK_INPUTPOINTER_LIMITS}
  3749. message(scan_c_switching_to_utf8);
  3750. current_settings.sourcecodepage:=CP_UTF8;
  3751. exclude(current_settings.moduleswitches,cs_system_codepage);
  3752. include(current_settings.moduleswitches,cs_explicit_codepage);
  3753. end;
  3754. line_no:=1;
  3755. if cs_asm_source in current_settings.globalswitches then
  3756. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3757. inputfile.setline(line_no,inputstart+hidden_inputpointer-hidden_inputbuffer);
  3758. {$else not CHECK_INPUTPOINTER_LIMITS}
  3759. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3760. {$endif CHECK_INPUTPOINTER_LIMITS}
  3761. end;
  3762. end
  3763. else
  3764. begin
  3765. wasmacro:=inputfile.is_macro;
  3766. { load eof position in tokenpos/current_filepos }
  3767. gettokenpos;
  3768. { close file }
  3769. closeinputfile;
  3770. { no next module, than EOF }
  3771. if not assigned(inputfile.next) then
  3772. begin
  3773. c:=#26;
  3774. exit;
  3775. end;
  3776. { load next file and reopen it }
  3777. nextfile;
  3778. tempopeninputfile;
  3779. { status }
  3780. Message1(scan_t_back_in,inputfile.name);
  3781. { end of include file is like a line break which ends e.g. also // style comments }
  3782. if not(wasmacro) and (current_commentstyle=comment_delphi) then
  3783. begin
  3784. c:=#10;
  3785. { ... but we have to decrease the line number first because it is increased due to this
  3786. inserted line break later on }
  3787. dec(line_no);
  3788. exit;
  3789. end;
  3790. end;
  3791. { load next char }
  3792. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3793. c:=get_inputpointer_char;
  3794. inc_inputpointer;
  3795. {$else not CHECK_INPUTPOINTER_LIMITS}
  3796. c:=inputpointer^;
  3797. inc(inputpointer);
  3798. {$endif CHECK_INPUTPOINTER_LIMITS}
  3799. until c<>#0; { if also end, then reload again }
  3800. end;
  3801. end;
  3802. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint;internally_generated: boolean);
  3803. var
  3804. hp : tinputfile;
  3805. begin
  3806. { save old postion }
  3807. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3808. dec_inputpointer;
  3809. {$else not CHECK_INPUTPOINTER_LIMITS}
  3810. dec(inputpointer);
  3811. {$endif CHECK_INPUTPOINTER_LIMITS}
  3812. tempcloseinputfile;
  3813. { create macro 'file' }
  3814. { use special name to dispose after !! }
  3815. hp:=do_openinputfile('_Macro_.'+macname);
  3816. addfile(hp);
  3817. with inputfile do
  3818. begin
  3819. inc(macro_nesting_depth);
  3820. setmacro(p,len);
  3821. { local buffer }
  3822. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3823. hidden_inputbuffer:=PAnsiChar(buf);
  3824. hidden_inputpointer:=PAnsiChar(buf);
  3825. {$else not CHECK_INPUTPOINTER_LIMITS}
  3826. inputbuffer:=PAnsiChar(buf);
  3827. inputpointer:=PAnsiChar(buf);
  3828. {$endif CHECK_INPUTPOINTER_LIMITS}
  3829. inputstart:=bufstart;
  3830. ref_index:=fileindex;
  3831. internally_generated_macro:=internally_generated;
  3832. end;
  3833. { reset line }
  3834. line_no:=line;
  3835. lastlinepos:=0;
  3836. lasttokenpos:=0;
  3837. nexttokenpos:=0;
  3838. { load new c }
  3839. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3840. c:=get_inputpointer_char;
  3841. inc_inputpointer;
  3842. {$else not CHECK_INPUTPOINTER_LIMITS}
  3843. c:=inputpointer^;
  3844. inc(inputpointer);
  3845. {$endif CHECK_INPUTPOINTER_LIMITS}
  3846. end;
  3847. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3848. begin
  3849. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3850. tokenpos:=inputstart+(hidden_inputpointer-hidden_inputbuffer);
  3851. {$else not CHECK_INPUTPOINTER_LIMITS}
  3852. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3853. {$endif CHECK_INPUTPOINTER_LIMITS}
  3854. filepos.line:=line_no;
  3855. filepos.column:=tokenpos-lastlinepos;
  3856. filepos.fileindex:=inputfile.ref_index;
  3857. filepos.moduleindex:=current_module.unit_index;
  3858. end;
  3859. procedure tscannerfile.gettokenpos;
  3860. { load the values of tokenpos and lasttokenpos }
  3861. begin
  3862. do_gettokenpos(lasttokenpos,current_tokenpos);
  3863. current_filepos:=current_tokenpos;
  3864. end;
  3865. procedure tscannerfile.cachenexttokenpos;
  3866. begin
  3867. do_gettokenpos(nexttokenpos,next_filepos);
  3868. end;
  3869. procedure tscannerfile.setnexttoken;
  3870. begin
  3871. token:=nexttoken;
  3872. nexttoken:=NOTOKEN;
  3873. lasttokenpos:=nexttokenpos;
  3874. current_tokenpos:=next_filepos;
  3875. current_filepos:=current_tokenpos;
  3876. nexttokenpos:=0;
  3877. end;
  3878. procedure tscannerfile.savetokenpos;
  3879. begin
  3880. oldlasttokenpos:=lasttokenpos;
  3881. oldcurrent_filepos:=current_filepos;
  3882. oldcurrent_tokenpos:=current_tokenpos;
  3883. end;
  3884. procedure tscannerfile.restoretokenpos;
  3885. begin
  3886. lasttokenpos:=oldlasttokenpos;
  3887. current_filepos:=oldcurrent_filepos;
  3888. current_tokenpos:=oldcurrent_tokenpos;
  3889. end;
  3890. procedure tscannerfile.inc_comment_level;
  3891. begin
  3892. if (m_nested_comment in current_settings.modeswitches) then
  3893. inc(comment_level)
  3894. else
  3895. comment_level:=1;
  3896. if (comment_level>1) then
  3897. begin
  3898. savetokenpos;
  3899. gettokenpos; { update for warning }
  3900. Message1(scan_w_comment_level,tostr(comment_level));
  3901. restoretokenpos;
  3902. end;
  3903. end;
  3904. procedure tscannerfile.dec_comment_level;
  3905. begin
  3906. if (m_nested_comment in current_settings.modeswitches) then
  3907. dec(comment_level)
  3908. else
  3909. comment_level:=0;
  3910. end;
  3911. procedure tscannerfile.linebreak;
  3912. var
  3913. cur : char;
  3914. begin
  3915. with inputfile do
  3916. begin
  3917. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3918. if (byte(get_inputpointer_char)=0) and not(endoffile) then
  3919. {$else not CHECK_INPUTPOINTER_LIMITS}
  3920. if (byte(inputpointer^)=0) and not(endoffile) then
  3921. {$endif CHECK_INPUTPOINTER_LIMITS}
  3922. begin
  3923. cur:=c;
  3924. reload;
  3925. if byte(cur)+byte(c)<>23 then
  3926. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3927. dec_inputpointer;
  3928. {$else not CHECK_INPUTPOINTER_LIMITS}
  3929. dec(inputpointer);
  3930. {$endif CHECK_INPUTPOINTER_LIMITS}
  3931. end
  3932. else
  3933. begin
  3934. { Support all combination of #10 and #13 as line break }
  3935. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3936. if (byte(get_inputpointer_char)+byte(c)=23) then
  3937. inc_inputpointer;
  3938. {$else not CHECK_INPUTPOINTER_LIMITS}
  3939. if (byte(inputpointer^)+byte(c)=23) then
  3940. inc(inputpointer);
  3941. {$endif CHECK_INPUTPOINTER_LIMITS}
  3942. end;
  3943. { Always return #10 as line break }
  3944. c:=#10;
  3945. { increase line counters }
  3946. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3947. lastlinepos:=inputstart+(hidden_inputpointer-hidden_inputbuffer);
  3948. {$else not CHECK_INPUTPOINTER_LIMITS}
  3949. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3950. {$endif CHECK_INPUTPOINTER_LIMITS}
  3951. inc(line_no);
  3952. { update linebuffer }
  3953. if cs_asm_source in current_settings.globalswitches then
  3954. inputfile.setline(line_no,lastlinepos);
  3955. { update for status and call the show status routine,
  3956. but don't touch current_filepos ! }
  3957. savetokenpos;
  3958. gettokenpos; { update for v_status }
  3959. inc(status.compiledlines);
  3960. ShowStatus;
  3961. restoretokenpos;
  3962. end;
  3963. end;
  3964. procedure tscannerfile.illegal_char(c:char);
  3965. var
  3966. s : string;
  3967. begin
  3968. if c in [#32..#255] then
  3969. s:=''''+c+''''
  3970. else
  3971. s:='#'+tostr(ord(c));
  3972. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3973. end;
  3974. procedure tscannerfile.end_of_file;
  3975. begin
  3976. checkpreprocstack;
  3977. if in_multiline_string then
  3978. Message2(scan_f_unterminated_multiline_string, tostr(multiline_start_line), tostr(multiline_start_column))
  3979. else
  3980. Message(scan_f_end_of_file);
  3981. end;
  3982. {-------------------------------------------
  3983. IF Conditional Handling
  3984. -------------------------------------------}
  3985. procedure tscannerfile.checkpreprocstack;
  3986. begin
  3987. { check for missing ifdefs }
  3988. while assigned(preprocstack) do
  3989. begin
  3990. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3991. current_module.sourcefiles.get_file_name(preprocstack.fileindex),
  3992. tostr(preprocstack.line_nb));
  3993. poppreprocstack;
  3994. end;
  3995. end;
  3996. procedure tscannerfile.poppreprocstack;
  3997. var
  3998. hp : tpreprocstack;
  3999. begin
  4000. if assigned(preprocstack) then
  4001. begin
  4002. Message1(scan_c_endif_found,preprocstack.name);
  4003. hp:=preprocstack.next;
  4004. preprocstack.free;
  4005. preprocstack:=hp;
  4006. end
  4007. else
  4008. Message(scan_e_endif_without_if);
  4009. end;
  4010. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  4011. var
  4012. condition: Boolean;
  4013. valuedescr: String;
  4014. begin
  4015. if (preprocstack=nil) or preprocstack.accept then
  4016. condition:=compile_time_predicate(valuedescr)
  4017. else
  4018. begin
  4019. condition:= false;
  4020. valuedescr:= '';
  4021. end;
  4022. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  4023. preprocstack.name:=valuedescr;
  4024. preprocstack.line_nb:=line_no;
  4025. preprocstack.fileindex:=current_filepos.fileindex;
  4026. if preprocstack.accept then
  4027. Message2(messid,preprocstack.name,'accepted')
  4028. else
  4029. Message2(messid,preprocstack.name,'rejected');
  4030. end;
  4031. procedure tscannerfile.elsepreprocstack;
  4032. begin
  4033. if assigned(preprocstack) and
  4034. (preprocstack.typ<>pp_else) then
  4035. begin
  4036. if (preprocstack.typ=pp_elseif) then
  4037. preprocstack.accept:=false
  4038. else
  4039. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  4040. preprocstack.accept:=not preprocstack.accept;
  4041. preprocstack.iftyp:=preprocstack.typ;
  4042. preprocstack.typ:=pp_else;
  4043. preprocstack.line_nb:=line_no;
  4044. preprocstack.fileindex:=current_filepos.fileindex;
  4045. if preprocstack.accept then
  4046. Message2(scan_c_else_found,preprocstack.name,'accepted')
  4047. else
  4048. Message2(scan_c_else_found,preprocstack.name,'rejected');
  4049. end
  4050. else
  4051. Message(scan_e_endif_without_if);
  4052. end;
  4053. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  4054. var
  4055. valuedescr: String;
  4056. begin
  4057. if assigned(preprocstack) and
  4058. (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef,pp_elseif]) then
  4059. begin
  4060. { when the branch is accepted we use pp_elseif so we know that
  4061. all the next branches need to be rejected. when this branch is still
  4062. not accepted then leave it at pp_if }
  4063. if (preprocstack.typ=pp_elseif) then
  4064. preprocstack.accept:=false
  4065. else if (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef]) and preprocstack.accept then
  4066. begin
  4067. preprocstack.accept:=false;
  4068. preprocstack.typ:=pp_elseif;
  4069. end
  4070. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  4071. and compile_time_predicate(valuedescr) then
  4072. begin
  4073. preprocstack.name:=valuedescr;
  4074. preprocstack.accept:=true;
  4075. preprocstack.typ:=pp_elseif;
  4076. end;
  4077. preprocstack.line_nb:=line_no;
  4078. preprocstack.fileindex:=current_filepos.fileindex;
  4079. if preprocstack.accept then
  4080. Message2(scan_c_else_found,preprocstack.name,'accepted')
  4081. else
  4082. Message2(scan_c_else_found,preprocstack.name,'rejected');
  4083. end
  4084. else
  4085. Message(scan_e_endif_without_if);
  4086. end;
  4087. procedure tscannerfile.popreplaystack;
  4088. var
  4089. hp : treplaystack;
  4090. begin
  4091. if assigned(replaystack) then
  4092. begin
  4093. hp:=replaystack.next;
  4094. replaystack.free;
  4095. replaystack:=hp;
  4096. end;
  4097. end;
  4098. function tscannerfile.replay_stack_depth:longint;
  4099. var
  4100. tmp: treplaystack;
  4101. begin
  4102. result:=0;
  4103. tmp:=replaystack;
  4104. while assigned(tmp) do
  4105. begin
  4106. inc(result);
  4107. tmp:=tmp.next;
  4108. end;
  4109. end;
  4110. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  4111. begin
  4112. savetokenpos;
  4113. repeat
  4114. current_scanner.gettokenpos;
  4115. Message1(scan_d_handling_switch,'$'+p.name);
  4116. p.proc();
  4117. { accept the text ? }
  4118. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  4119. break
  4120. else
  4121. begin
  4122. current_scanner.gettokenpos;
  4123. Message(scan_c_skipping_until);
  4124. repeat
  4125. current_scanner.skipuntildirective;
  4126. if not (m_mac in current_settings.modeswitches) then
  4127. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  4128. else
  4129. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  4130. until assigned(p) and (p.is_conditional);
  4131. current_scanner.gettokenpos;
  4132. end;
  4133. until false;
  4134. restoretokenpos;
  4135. end;
  4136. procedure tscannerfile.handledirectives;
  4137. var
  4138. t : tdirectiveitem;
  4139. hs : string;
  4140. begin
  4141. gettokenpos;
  4142. readchar; {Remove the $}
  4143. hs:=readid;
  4144. { handle empty directive }
  4145. if hs='' then
  4146. begin
  4147. Message1(scan_w_illegal_switch,'$');
  4148. exit;
  4149. end;
  4150. {$ifdef PREPROCWRITE}
  4151. if parapreprocess then
  4152. begin
  4153. if not (m_mac in current_settings.modeswitches) then
  4154. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  4155. else
  4156. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  4157. if assigned(t) and not(t.is_conditional) then
  4158. begin
  4159. preprocfile.AddSpace;
  4160. preprocfile.Add('{$'+hs+current_scanner.readcomment+'}');
  4161. exit;
  4162. end;
  4163. end;
  4164. {$endif PREPROCWRITE}
  4165. { skip this directive? }
  4166. if (ignoredirectives.find(hs)<>nil) then
  4167. begin
  4168. if (comment_level>0) then
  4169. readcomment;
  4170. { we've read the whole comment }
  4171. current_commentstyle:=comment_none;
  4172. exit;
  4173. end;
  4174. { Check for compiler switches }
  4175. while (length(hs)=1) and (c in ['-','+']) do
  4176. begin
  4177. Message1(scan_d_handling_switch,'$'+hs+c);
  4178. HandleSwitch(hs[1],c);
  4179. current_scanner.readchar; {Remove + or -}
  4180. if c=',' then
  4181. begin
  4182. current_scanner.readchar; {Remove , }
  4183. { read next switch, support $v+,$+}
  4184. hs:=current_scanner.readid;
  4185. if (hs='') then
  4186. begin
  4187. if (c='$') and (m_fpc in current_settings.modeswitches) then
  4188. begin
  4189. current_scanner.readchar; { skip $ }
  4190. hs:=current_scanner.readid;
  4191. end;
  4192. if (hs='') then
  4193. Message1(scan_w_illegal_directive,'$'+c);
  4194. end;
  4195. end
  4196. else
  4197. hs:='';
  4198. end;
  4199. { directives may follow switches after a , }
  4200. if hs<>'' then
  4201. begin
  4202. if not (m_mac in current_settings.modeswitches) then
  4203. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  4204. else
  4205. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  4206. if assigned(t) then
  4207. begin
  4208. if t.is_conditional then
  4209. handleconditional(t)
  4210. else
  4211. begin
  4212. Message1(scan_d_handling_switch,'$'+hs);
  4213. t.proc();
  4214. end;
  4215. end
  4216. else
  4217. begin
  4218. current_scanner.ignoredirectives.Add(hs,nil);
  4219. Message1(scan_w_illegal_directive,'$'+hs);
  4220. end;
  4221. { conditionals already read the comment }
  4222. if (current_scanner.comment_level>0) then
  4223. current_scanner.readcomment;
  4224. { we've read the whole comment }
  4225. current_commentstyle:=comment_none;
  4226. end;
  4227. end;
  4228. procedure tscannerfile.readchar;
  4229. begin
  4230. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4231. c:=get_inputpointer_char;
  4232. {$else not CHECK_INPUTPOINTER_LIMITS}
  4233. c:=inputpointer^;
  4234. {$endif CHECK_INPUTPOINTER_LIMITS}
  4235. if c=#0 then
  4236. reload
  4237. else
  4238. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4239. inc_inputpointer;
  4240. {$else not CHECK_INPUTPOINTER_LIMITS}
  4241. inc(inputpointer);
  4242. {$endif CHECK_INPUTPOINTER_LIMITS}
  4243. end;
  4244. procedure tscannerfile.readstring;
  4245. var
  4246. i : longint;
  4247. err : boolean;
  4248. begin
  4249. err:=false;
  4250. i:=0;
  4251. repeat
  4252. case c of
  4253. '_','$',
  4254. '0'..'9',
  4255. 'A'..'Z',
  4256. 'a'..'z' :
  4257. begin
  4258. if (c='$') and not allowgenericid then
  4259. break;
  4260. if i<255 then
  4261. begin
  4262. inc(i);
  4263. orgpattern[i]:=c;
  4264. if c in ['a'..'z'] then
  4265. pattern[i]:=chr(ord(c)-32)
  4266. else
  4267. pattern[i]:=c;
  4268. end
  4269. else
  4270. begin
  4271. if not err then
  4272. begin
  4273. Message(scan_e_string_exceeds_255_chars);
  4274. err:=true;
  4275. end;
  4276. end;
  4277. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4278. c:=get_inputpointer_char;
  4279. inc_inputpointer;
  4280. {$else not CHECK_INPUTPOINTER_LIMITS}
  4281. c:=inputpointer^;
  4282. inc(inputpointer);
  4283. {$endif CHECK_INPUTPOINTER_LIMITS}
  4284. end;
  4285. #0 :
  4286. reload;
  4287. else if inputfile.internally_generated_macro and
  4288. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  4289. begin
  4290. if i<255 then
  4291. begin
  4292. inc(i);
  4293. orgpattern[i]:=c;
  4294. pattern[i]:=c;
  4295. end
  4296. else
  4297. begin
  4298. if not err then
  4299. begin
  4300. Message(scan_e_string_exceeds_255_chars);
  4301. err:=true;
  4302. end;
  4303. end;
  4304. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4305. c:=get_inputpointer_char;
  4306. inc_inputpointer;
  4307. {$else not CHECK_INPUTPOINTER_LIMITS}
  4308. c:=inputpointer^;
  4309. inc(inputpointer);
  4310. {$endif CHECK_INPUTPOINTER_LIMITS}
  4311. end
  4312. else
  4313. break;
  4314. end;
  4315. until false;
  4316. orgpattern[0]:=chr(i);
  4317. pattern[0]:=chr(i);
  4318. end;
  4319. procedure tscannerfile.readnumber;
  4320. var
  4321. base,
  4322. i : longint;
  4323. firstdigitread: Boolean;
  4324. begin
  4325. case c of
  4326. '%' :
  4327. begin
  4328. readchar;
  4329. base:=2;
  4330. pattern[1]:='%';
  4331. i:=1;
  4332. end;
  4333. '&' :
  4334. begin
  4335. readchar;
  4336. base:=8;
  4337. pattern[1]:='&';
  4338. i:=1;
  4339. end;
  4340. '$' :
  4341. begin
  4342. readchar;
  4343. base:=16;
  4344. pattern[1]:='$';
  4345. i:=1;
  4346. end;
  4347. else
  4348. begin
  4349. base:=10;
  4350. i:=0;
  4351. end;
  4352. end;
  4353. firstdigitread:=false;
  4354. while ((base>=10) and (c in ['0'..'9'])) or
  4355. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  4356. ((base=8) and (c in ['0'..'7'])) or
  4357. ((base=2) and (c in ['0'..'1'])) or
  4358. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  4359. begin
  4360. if (i<255) and (c<>'_') then
  4361. begin
  4362. inc(i);
  4363. pattern[i]:=c;
  4364. end;
  4365. readchar;
  4366. firstdigitread:=true;
  4367. end;
  4368. pattern[0]:=chr(i);
  4369. end;
  4370. function tscannerfile.readid:string;
  4371. begin
  4372. readstring;
  4373. readid:=pattern;
  4374. end;
  4375. function tscannerfile.readval:longint;
  4376. var
  4377. l : longint;
  4378. w : integer;
  4379. begin
  4380. readnumber;
  4381. val(pattern,l,w);
  4382. readval:=l;
  4383. end;
  4384. function tscannerfile.readval64:int64;
  4385. var
  4386. l : int64;
  4387. w : integer;
  4388. begin
  4389. readnumber;
  4390. val(pattern,l,w);
  4391. readval64:=l;
  4392. end;
  4393. function tscannerfile.readcomment(include_special_char: boolean):string;
  4394. var
  4395. i : longint;
  4396. begin
  4397. i:=0;
  4398. repeat
  4399. case c of
  4400. '{' :
  4401. begin
  4402. if (include_special_char) and (i<255) then
  4403. begin
  4404. inc(i);
  4405. readcomment[i]:=c;
  4406. end;
  4407. if current_commentstyle=comment_tp then
  4408. inc_comment_level;
  4409. end;
  4410. '}' :
  4411. begin
  4412. if (include_special_char) and (i<255) then
  4413. begin
  4414. inc(i);
  4415. readcomment[i]:=c;
  4416. end;
  4417. if current_commentstyle=comment_tp then
  4418. begin
  4419. readchar;
  4420. dec_comment_level;
  4421. if comment_level=0 then
  4422. break
  4423. else
  4424. continue;
  4425. end;
  4426. end;
  4427. '*' :
  4428. begin
  4429. if current_commentstyle=comment_oldtp then
  4430. begin
  4431. readchar;
  4432. if c=')' then
  4433. begin
  4434. readchar;
  4435. dec_comment_level;
  4436. break;
  4437. end
  4438. else
  4439. { Add both characters !!}
  4440. if (i<255) then
  4441. begin
  4442. inc(i);
  4443. readcomment[i]:='*';
  4444. if (i<255) then
  4445. begin
  4446. inc(i);
  4447. readcomment[i]:=c;
  4448. end;
  4449. end;
  4450. end
  4451. else
  4452. { Not old TP comment, so add...}
  4453. begin
  4454. if (i<255) then
  4455. begin
  4456. inc(i);
  4457. readcomment[i]:='*';
  4458. end;
  4459. end;
  4460. end;
  4461. #10,#13 :
  4462. linebreak;
  4463. #26 :
  4464. end_of_file;
  4465. else
  4466. begin
  4467. if (i<255) then
  4468. begin
  4469. inc(i);
  4470. readcomment[i]:=c;
  4471. end;
  4472. end;
  4473. end;
  4474. readchar;
  4475. until false;
  4476. readcomment[0]:=chr(i);
  4477. end;
  4478. function tscannerfile.readquotedstring:string;
  4479. var
  4480. i : longint;
  4481. msgwritten : boolean;
  4482. begin
  4483. i:=0;
  4484. msgwritten:=false;
  4485. if (c in ['''','`']) then
  4486. begin
  4487. had_multiline_string:=in_multiline_string;
  4488. in_multiline_string:=(c='`');
  4489. if in_multiline_string and (not (m_multiline_strings in current_settings.modeswitches)) then
  4490. begin
  4491. result[0]:=chr(0);
  4492. Illegal_Char(c);
  4493. end;
  4494. repeat
  4495. readchar;
  4496. case c of
  4497. #26 :
  4498. end_of_file;
  4499. #10,#13 :
  4500. if not in_multiline_string then
  4501. begin
  4502. if had_multiline_string then
  4503. Message2(scan_f_unterminated_multiline_string,
  4504. tostr(multiline_start_line),
  4505. tostr(multiline_start_column))
  4506. else
  4507. Message(scan_f_string_exceeds_line);
  4508. end;
  4509. '''' :
  4510. if not in_multiline_string then
  4511. begin
  4512. readchar;
  4513. if c<>'''' then
  4514. break;
  4515. end;
  4516. '`' :
  4517. if in_multiline_string then
  4518. begin
  4519. readchar;
  4520. if c<>'`' then
  4521. break;
  4522. end;
  4523. end;
  4524. if i<255 then
  4525. begin
  4526. inc(i);
  4527. result[i]:=c;
  4528. end
  4529. else
  4530. begin
  4531. if not msgwritten then
  4532. begin
  4533. Message(scan_e_string_exceeds_255_chars);
  4534. msgwritten:=true;
  4535. end;
  4536. end;
  4537. until false;
  4538. end;
  4539. result[0]:=chr(i);
  4540. end;
  4541. function tscannerfile.readlongcomment(include_special_char: boolean):RawByteString;
  4542. var
  4543. i : longint;
  4544. procedure addchar(char: AnsiChar = #0);
  4545. begin
  4546. Inc(i);
  4547. if Length(readlongcomment)>=i then
  4548. SetLength(readlongcomment, Length(readlongcomment)+256);
  4549. if char<>#0 then
  4550. readlongcomment[i]:=char
  4551. else
  4552. readlongcomment[i]:=c;
  4553. end;
  4554. begin
  4555. i:=0;
  4556. SetLength(readlongcomment, 256);
  4557. repeat
  4558. case c of
  4559. '{' :
  4560. begin
  4561. if (include_special_char) then
  4562. addchar;
  4563. if current_commentstyle=comment_tp then
  4564. inc_comment_level;
  4565. end;
  4566. '}' :
  4567. begin
  4568. if (include_special_char) then
  4569. addchar;
  4570. if current_commentstyle=comment_tp then
  4571. begin
  4572. readchar;
  4573. dec_comment_level;
  4574. if comment_level=0 then
  4575. break
  4576. else
  4577. continue;
  4578. end;
  4579. end;
  4580. '*' :
  4581. begin
  4582. if current_commentstyle=comment_oldtp then
  4583. begin
  4584. readchar;
  4585. if c=')' then
  4586. begin
  4587. readchar;
  4588. dec_comment_level;
  4589. break;
  4590. end
  4591. else
  4592. { Add both characters !!}
  4593. begin
  4594. addchar('*');
  4595. addchar;
  4596. end;
  4597. end
  4598. else
  4599. { Not old TP comment, so add...}
  4600. addchar('*');
  4601. end;
  4602. #10,#13 :
  4603. linebreak;
  4604. #26 :
  4605. end_of_file;
  4606. else
  4607. addchar;
  4608. end;
  4609. readchar;
  4610. until false;
  4611. SetLength(readlongcomment, i);
  4612. SetCodePage(readlongcomment, current_settings.sourcecodepage, False);
  4613. end;
  4614. function tscannerfile.readlongquotedstring:RawByteString;
  4615. var
  4616. i : longint;
  4617. msgwritten : boolean;
  4618. procedure addchar;
  4619. begin
  4620. Inc(i);
  4621. if Length(readlongquotedstring)>=i then
  4622. SetLength(readlongquotedstring, Length(readlongquotedstring)+256);
  4623. readlongquotedstring[i]:=c;
  4624. end;
  4625. begin
  4626. i:=0;
  4627. Setlength(readlongquotedstring, 256);
  4628. msgwritten:=false;
  4629. if (c='''') then
  4630. begin
  4631. repeat
  4632. readchar;
  4633. case c of
  4634. #26 :
  4635. end_of_file;
  4636. #10,#13 :
  4637. Message(scan_f_string_exceeds_line);
  4638. '''' :
  4639. begin
  4640. readchar;
  4641. if c<>'''' then
  4642. break;
  4643. end;
  4644. end;
  4645. addchar;
  4646. until false;
  4647. end;
  4648. SetLength(readlongquotedstring, i);
  4649. SetCodePage(readlongquotedstring, current_settings.sourcecodepage, False);
  4650. end;
  4651. function tscannerfile.readstate:char;
  4652. var
  4653. state : char;
  4654. begin
  4655. state:=' ';
  4656. if c=' ' then
  4657. begin
  4658. current_scanner.skipspace;
  4659. current_scanner.readid;
  4660. if pattern='ON' then
  4661. state:='+'
  4662. else
  4663. if pattern='OFF' then
  4664. state:='-';
  4665. end
  4666. else
  4667. state:=c;
  4668. if not (state in ['+','-']) then
  4669. Message(scan_e_wrong_switch_toggle);
  4670. readstate:=state;
  4671. end;
  4672. function tscannerfile.readoptionalstate(fallback:char):char;
  4673. var
  4674. state : char;
  4675. begin
  4676. state:=' ';
  4677. if c=' ' then
  4678. begin
  4679. current_scanner.skipspace;
  4680. if c in ['*','}'] then
  4681. state:=fallback
  4682. else
  4683. begin
  4684. current_scanner.readid;
  4685. if pattern='ON' then
  4686. state:='+'
  4687. else
  4688. if pattern='OFF' then
  4689. state:='-';
  4690. end;
  4691. end
  4692. else
  4693. if c in ['*','}'] then
  4694. state:=fallback
  4695. else
  4696. state:=c;
  4697. if not (state in ['+','-']) then
  4698. Message(scan_e_wrong_switch_toggle);
  4699. readoptionalstate:=state;
  4700. end;
  4701. function tscannerfile.readstatedefault:char;
  4702. var
  4703. state : char;
  4704. begin
  4705. state:=' ';
  4706. if c=' ' then
  4707. begin
  4708. current_scanner.skipspace;
  4709. current_scanner.readid;
  4710. if pattern='ON' then
  4711. state:='+'
  4712. else
  4713. if pattern='OFF' then
  4714. state:='-'
  4715. else
  4716. if pattern='DEFAULT' then
  4717. state:='*';
  4718. end
  4719. else
  4720. state:=c;
  4721. if not (state in ['+','-','*']) then
  4722. Message(scan_e_wrong_switch_toggle_default);
  4723. readstatedefault:=state;
  4724. end;
  4725. procedure tscannerfile.skipspace;
  4726. begin
  4727. repeat
  4728. case c of
  4729. #26 :
  4730. begin
  4731. reload;
  4732. if (c=#26) and not assigned(inputfile.next) then
  4733. break;
  4734. continue;
  4735. end;
  4736. #10,
  4737. #13 :
  4738. linebreak;
  4739. #9,#11,#12,' ' :
  4740. ;
  4741. else
  4742. break;
  4743. end;
  4744. readchar;
  4745. until false;
  4746. end;
  4747. procedure tscannerfile.skipuntildirective;
  4748. var
  4749. found : longint;
  4750. next_char_loaded : boolean;
  4751. begin
  4752. found:=0;
  4753. next_char_loaded:=false;
  4754. repeat
  4755. case c of
  4756. #10,
  4757. #13 :
  4758. linebreak;
  4759. #26 :
  4760. begin
  4761. reload;
  4762. if (c=#26) and not assigned(inputfile.next) then
  4763. end_of_file;
  4764. continue;
  4765. end;
  4766. '{' :
  4767. begin
  4768. if (current_commentstyle in [comment_tp,comment_none]) then
  4769. begin
  4770. current_commentstyle:=comment_tp;
  4771. if (comment_level=0) then
  4772. found:=1;
  4773. inc_comment_level;
  4774. end;
  4775. end;
  4776. '*' :
  4777. begin
  4778. if (current_commentstyle=comment_oldtp) then
  4779. begin
  4780. readchar;
  4781. if c=')' then
  4782. begin
  4783. dec_comment_level;
  4784. found:=0;
  4785. current_commentstyle:=comment_none;
  4786. end
  4787. else
  4788. next_char_loaded:=true;
  4789. end
  4790. else
  4791. found := 0;
  4792. end;
  4793. '}' :
  4794. begin
  4795. if (current_commentstyle=comment_tp) then
  4796. begin
  4797. dec_comment_level;
  4798. if (comment_level=0) then
  4799. current_commentstyle:=comment_none;
  4800. found:=0;
  4801. end;
  4802. end;
  4803. '$' :
  4804. begin
  4805. if found=1 then
  4806. found:=2;
  4807. end;
  4808. '''','`' :
  4809. if (current_commentstyle=comment_none) then
  4810. begin
  4811. had_multiline_string:=in_multiline_string;
  4812. in_multiline_string:=(c='`');
  4813. if in_multiline_string and (not (m_multiline_strings in current_settings.modeswitches)) then
  4814. Illegal_Char(c);
  4815. repeat
  4816. readchar;
  4817. case c of
  4818. #26 :
  4819. end_of_file;
  4820. #10,#13 :
  4821. if not in_multiline_string then
  4822. break;
  4823. '''' :
  4824. if not in_multiline_string then
  4825. begin
  4826. readchar;
  4827. if c<>'''' then
  4828. begin
  4829. next_char_loaded:=true;
  4830. break;
  4831. end;
  4832. end;
  4833. '`' :
  4834. if in_multiline_string then
  4835. begin
  4836. readchar;
  4837. if c<>'`' then
  4838. begin
  4839. next_char_loaded:=true;
  4840. break;
  4841. end;
  4842. end;
  4843. end;
  4844. until false;
  4845. end;
  4846. '(' :
  4847. begin
  4848. if (current_commentstyle=comment_none) then
  4849. begin
  4850. readchar;
  4851. if c='*' then
  4852. begin
  4853. readchar;
  4854. if c='$' then
  4855. begin
  4856. found:=2;
  4857. inc_comment_level;
  4858. current_commentstyle:=comment_oldtp;
  4859. end
  4860. else
  4861. begin
  4862. skipoldtpcomment(false);
  4863. next_char_loaded:=true;
  4864. end;
  4865. end
  4866. else
  4867. next_char_loaded:=true;
  4868. end
  4869. else
  4870. found:=0;
  4871. end;
  4872. '/' :
  4873. begin
  4874. if (current_commentstyle=comment_none) then
  4875. begin
  4876. readchar;
  4877. if c='/' then
  4878. skipdelphicomment;
  4879. next_char_loaded:=true;
  4880. end
  4881. else
  4882. found:=0;
  4883. end;
  4884. else
  4885. found:=0;
  4886. end;
  4887. if next_char_loaded then
  4888. next_char_loaded:=false
  4889. else
  4890. readchar;
  4891. until (found=2);
  4892. end;
  4893. {****************************************************************************
  4894. Comment Handling
  4895. ****************************************************************************}
  4896. procedure tscannerfile.skipcomment(read_first_char:boolean);
  4897. begin
  4898. current_commentstyle:=comment_tp;
  4899. if read_first_char then
  4900. readchar;
  4901. inc_comment_level;
  4902. { handle compiler switches }
  4903. if (c='$') then
  4904. handledirectives;
  4905. { handle_switches can dec comment_level, }
  4906. while (comment_level>0) do
  4907. begin
  4908. case c of
  4909. '{' :
  4910. inc_comment_level;
  4911. '}' :
  4912. dec_comment_level;
  4913. '*' :
  4914. { in iso mode, comments opened by a curly bracket can be closed by asterisk, round bracket }
  4915. if m_iso in current_settings.modeswitches then
  4916. begin
  4917. readchar;
  4918. if c=')' then
  4919. dec_comment_level
  4920. else
  4921. continue;
  4922. end;
  4923. #10,#13 :
  4924. linebreak;
  4925. #26 :
  4926. begin
  4927. reload;
  4928. if (c=#26) and not assigned(inputfile.next) then
  4929. end_of_file;
  4930. continue;
  4931. end;
  4932. end;
  4933. readchar;
  4934. end;
  4935. current_commentstyle:=comment_none;
  4936. end;
  4937. procedure tscannerfile.skipdelphicomment;
  4938. begin
  4939. current_commentstyle:=comment_delphi;
  4940. inc_comment_level;
  4941. readchar;
  4942. { this is not supported }
  4943. if c='$' then
  4944. Message(scan_w_wrong_styled_switch);
  4945. { skip comment }
  4946. while not (c in [#10,#13,#26]) do
  4947. readchar;
  4948. dec_comment_level;
  4949. current_commentstyle:=comment_none;
  4950. end;
  4951. procedure tscannerfile.skipoldtpcomment(read_first_char:boolean);
  4952. var
  4953. found : longint;
  4954. begin
  4955. current_commentstyle:=comment_oldtp;
  4956. inc_comment_level;
  4957. { only load a char if last already processed,
  4958. was cause of bug1634 PM }
  4959. if read_first_char then
  4960. readchar;
  4961. { this is now supported }
  4962. if (c='$') then
  4963. handledirectives;
  4964. { skip comment }
  4965. while (comment_level>0) do
  4966. begin
  4967. found:=0;
  4968. repeat
  4969. case c of
  4970. #26 :
  4971. begin
  4972. reload;
  4973. if (c=#26) and not assigned(inputfile.next) then
  4974. end_of_file;
  4975. continue;
  4976. end;
  4977. #10,#13 :
  4978. begin
  4979. if found=4 then
  4980. inc_comment_level;
  4981. linebreak;
  4982. found:=0;
  4983. end;
  4984. '*' :
  4985. begin
  4986. if found=3 then
  4987. found:=4
  4988. else
  4989. begin
  4990. if found=4 then
  4991. inc_comment_level;
  4992. found:=1;
  4993. end;
  4994. end;
  4995. ')' :
  4996. begin
  4997. if found in [1,4] then
  4998. begin
  4999. dec_comment_level;
  5000. if comment_level=0 then
  5001. found:=2
  5002. else
  5003. found:=0;
  5004. end
  5005. else
  5006. found:=0;
  5007. end;
  5008. '}' :
  5009. { in iso mode, comments opened by asterisk, round bracket can be closed by a curly bracket }
  5010. if m_iso in current_settings.modeswitches then
  5011. begin
  5012. dec_comment_level;
  5013. if comment_level=0 then
  5014. found:=2
  5015. else
  5016. found:=0;
  5017. end;
  5018. '(' :
  5019. begin
  5020. if found=4 then
  5021. inc_comment_level;
  5022. found:=3;
  5023. end;
  5024. else
  5025. begin
  5026. if found=4 then
  5027. inc_comment_level;
  5028. found:=0;
  5029. end;
  5030. end;
  5031. readchar;
  5032. until (found=2);
  5033. end;
  5034. current_commentstyle:=comment_none;
  5035. end;
  5036. procedure tscannerfile.postprocessutf8multiline(len,quote_pos,quote_count : integer);
  5037. var
  5038. malformed : boolean;
  5039. start, i,stripcol,col,newlen : integer;
  5040. crlf : boolean;
  5041. tmp : tcompilerwidestring;
  5042. c : tcompilerwidechar;
  5043. file_pos : tfileposinfo;
  5044. begin
  5045. stripcol:=quote_pos;
  5046. malformed:=false;
  5047. newlen:=0;
  5048. col:=0;
  5049. start:=1;
  5050. initwidestring(tmp);
  5051. { Strip initial cr/lf }
  5052. Case current_settings.lineendingtype of
  5053. le_cr,le_lf : inc(start);
  5054. le_crlf : inc(start,2);
  5055. le_source :
  5056. begin
  5057. inc(start);
  5058. if (getcharwidestring(patternw,1)=13) and (getcharwidestring(patternw,start)=10) then
  5059. inc(start);
  5060. end;
  5061. le_platform : inc(start,length(target_info.newline));
  5062. end;
  5063. { we don't need the last added quotes }
  5064. dec(len,quote_count-1);
  5065. for I:=Start to len do
  5066. begin
  5067. c:=getcharwidestring(patternw,i);
  5068. inc(col);
  5069. if (col>stripcol) or (c=10) or (c=13) then
  5070. begin
  5071. inc(newlen);
  5072. concatwidestringchar(patternw,c);
  5073. end
  5074. else
  5075. begin
  5076. // if less spaces than in the last line, report error
  5077. if not (c in [9,32,11]) then
  5078. begin
  5079. if not malformed then
  5080. begin
  5081. malformed:=true;
  5082. message3(scan_e_improperly_indented_multiline_string,
  5083. tostr(stripcol),
  5084. tostr(multiline_start_line),
  5085. tostr(multiline_start_column));
  5086. end;
  5087. end;
  5088. end;
  5089. if (c=10) or (c=13) then
  5090. col:=0;
  5091. end;
  5092. // remove last CR/LF
  5093. c:=getcharwidestring(tmp,newlen);
  5094. if (c=10) or (c=13) then
  5095. begin
  5096. Case current_settings.lineendingtype of
  5097. le_cr,le_lf : dec(newlen);
  5098. le_crlf : dec(newlen,2);
  5099. le_platform : dec(newlen,length(target_info.newline));
  5100. le_source :
  5101. begin
  5102. crlf:=getcharwidestring(tmp,newlen)=10;
  5103. dec(newlen);
  5104. if crlf and (newLen>0) and (getcharwidestring(tmp,newlen)=13) then
  5105. dec(newlen);
  5106. end;
  5107. end;
  5108. end;
  5109. tmp.len:=newLen;
  5110. donewidestring(patternw);
  5111. patternw:=tmp;
  5112. end;
  5113. procedure tscannerfile.postprocessmultiline(len,quote_pos,quote_count : integer);
  5114. var
  5115. malformed : boolean;
  5116. start, i,stripcol,col,newlen : integer;
  5117. crlf : boolean;
  5118. tmp : ansistring;
  5119. c : ansichar;
  5120. file_pos : tfileposinfo;
  5121. begin
  5122. stripcol:=quote_pos;
  5123. malformed:=false;
  5124. newlen:=0;
  5125. setlength(tmp,len-quote_count+1);
  5126. col:=0;
  5127. start:=1;
  5128. { Strip initial cr/lf }
  5129. Case current_settings.lineendingtype of
  5130. le_cr,le_lf : inc(start);
  5131. le_crlf : inc(start,2);
  5132. le_platform : inc(start,length(target_info.newline));
  5133. le_source :
  5134. begin
  5135. inc(start);
  5136. if (cstringPattern[1]=#13) and (cstringpattern[start]=#10) then
  5137. inc(start);
  5138. end;
  5139. end;
  5140. { we don't need the last added quotes }
  5141. dec(len,quote_count-1);
  5142. for I:=Start to len do
  5143. begin
  5144. c:=cstringpattern[i];
  5145. inc(col);
  5146. if (col>stripcol) or (c in [#10,#13]) then
  5147. begin
  5148. inc(newlen);
  5149. tmp[newlen]:=c;
  5150. end
  5151. else
  5152. begin
  5153. // if less spaces than in the last line, report error
  5154. if not (c in [#9,#32,#11]) then
  5155. begin
  5156. if not malformed then
  5157. begin
  5158. malformed:=true;
  5159. message3(scan_e_improperly_indented_multiline_string,
  5160. tostr(stripcol),
  5161. tostr(multiline_start_line),
  5162. tostr(multiline_start_column));
  5163. end;
  5164. end;
  5165. end;
  5166. if c in [#10,#13] then
  5167. col:=0;
  5168. end;
  5169. // remove last CR/LF
  5170. if tmp[newlen] in [#10,#13] then
  5171. begin
  5172. Case current_settings.lineendingtype of
  5173. le_cr,le_lf : dec(newlen);
  5174. le_crlf : dec(newlen,2);
  5175. le_platform : dec(newlen,length(target_info.newline));
  5176. le_source :
  5177. begin
  5178. crlf:=tmp[newlen]=#10;
  5179. dec(newlen);
  5180. if crlf and (tmp[newlen]=#13) then
  5181. Dec(newlen);
  5182. end;
  5183. end;
  5184. end;
  5185. SetLength(tmp,newlen);
  5186. cstringpattern:=tmp;
  5187. end;
  5188. function tscannerfile.readstringconstant : boolean;
  5189. type
  5190. tQuoteStyle = (qsNone,qsBacktick,qsMultiQuote);
  5191. var
  5192. trimcount,m,code,len,quote_count,init_quote_count,whitespace_count,quote_col : integer;
  5193. style : tQuoteStyle;
  5194. iswidestring : boolean;
  5195. asciinr : string[33];
  5196. last_c : char;
  5197. whitespace_only, had_newline, first_multiline,backtick : boolean;
  5198. d : cardinal;
  5199. w : word;
  5200. label
  5201. quote_label;
  5202. begin
  5203. last_c :=#0;
  5204. trimcount:=0;
  5205. quote_col:=0;
  5206. whitespace_count:=0;
  5207. had_newline:=false;
  5208. had_multiline_string:=in_multiline_string;
  5209. backtick:=(c='`');
  5210. if backtick then
  5211. style:=qsBacktick
  5212. else
  5213. style:=qsNone;
  5214. whitespace_only:=true;
  5215. in_multiline_string:=backtick;
  5216. quote_count:=0;
  5217. whitespace_only:=true;
  5218. if in_multiline_string then
  5219. begin
  5220. if not (m_multiline_strings in current_settings.modeswitches) then
  5221. Illegal_Char(c)
  5222. else
  5223. begin
  5224. multiline_start_line:=current_filepos.line;
  5225. multiline_start_column:=current_filepos.column;
  5226. end;
  5227. end;
  5228. len:=0;
  5229. cstringpattern:='';
  5230. iswidestring:=false;
  5231. if c='^' then
  5232. begin
  5233. readchar;
  5234. c:=upcase(c);
  5235. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  5236. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  5237. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  5238. begin
  5239. token:=_CARET;
  5240. exit(true);
  5241. end
  5242. else
  5243. begin
  5244. inc(len);
  5245. setlength(cstringpattern,256);
  5246. if c<#64 then
  5247. cstringpattern[len]:=chr(ord(c)+64)
  5248. else
  5249. cstringpattern[len]:=chr(ord(c)-64);
  5250. readchar;
  5251. end;
  5252. end;
  5253. repeat
  5254. case c of
  5255. '#' :
  5256. begin
  5257. readchar; { read # }
  5258. case c of
  5259. '$':
  5260. begin
  5261. readchar; { read leading $ }
  5262. asciinr:='$';
  5263. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=7) do
  5264. begin
  5265. asciinr:=asciinr+c;
  5266. readchar;
  5267. end;
  5268. end;
  5269. '&':
  5270. begin
  5271. readchar; { read leading $ }
  5272. asciinr:='&';
  5273. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=8) do
  5274. begin
  5275. asciinr:=asciinr+c;
  5276. readchar;
  5277. end;
  5278. end;
  5279. '%':
  5280. begin
  5281. readchar; { read leading $ }
  5282. asciinr:='%';
  5283. while (upcase(c) in ['0','1']) and (length(asciinr)<=22) do
  5284. begin
  5285. asciinr:=asciinr+c;
  5286. readchar;
  5287. end;
  5288. end;
  5289. else
  5290. begin
  5291. asciinr:='';
  5292. while (c in ['0'..'9']) and (length(asciinr)<=8) do
  5293. begin
  5294. asciinr:=asciinr+c;
  5295. readchar;
  5296. end;
  5297. end;
  5298. end;
  5299. val(asciinr,m,code);
  5300. if (asciinr='') or (code<>0) then
  5301. Message(scan_e_illegal_char_const)
  5302. else if (m<0) or (m>255) or (length(asciinr)>3) then
  5303. begin
  5304. if (m>=0) and (m<=$10FFFF) then
  5305. begin
  5306. if not iswidestring then
  5307. begin
  5308. if len>0 then
  5309. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5310. else
  5311. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5312. iswidestring:=true;
  5313. len:=0;
  5314. end;
  5315. if m<=$FFFF then
  5316. concatwidestringchar(patternw,tcompilerwidechar(m))
  5317. else
  5318. begin
  5319. { split into surrogate pair }
  5320. dec(m,$10000);
  5321. concatwidestringchar(patternw,tcompilerwidechar((m shr 10) + $D800));
  5322. concatwidestringchar(patternw,tcompilerwidechar((m and $3FF) + $DC00));
  5323. end;
  5324. end
  5325. else
  5326. Message(scan_e_illegal_char_const)
  5327. end
  5328. else if iswidestring then
  5329. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  5330. else
  5331. begin
  5332. if len>=length(cstringpattern) then
  5333. setlength(cstringpattern,length(cstringpattern)+256);
  5334. inc(len);
  5335. cstringpattern[len]:=chr(m);
  5336. end;
  5337. end;
  5338. '''','`' :
  5339. begin
  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. len:=0;
  5400. if c=#13 then
  5401. begin
  5402. readchar;
  5403. if c<>#10 then
  5404. goto quote_label;
  5405. end;
  5406. end
  5407. else
  5408. Message(scan_f_string_exceeds_line);
  5409. end;
  5410. end;
  5411. '''' :
  5412. begin
  5413. inc(quote_count);
  5414. if not in_multiline_string then
  5415. begin
  5416. readchar;
  5417. if c='''' then
  5418. inc(quote_count)
  5419. else
  5420. break;
  5421. end
  5422. else if not backtick then
  5423. if whitespace_only and (quote_count=init_quote_count) then
  5424. begin
  5425. in_multiline_string:=false;
  5426. quote_col:=whitespace_count;
  5427. readchar;
  5428. break;
  5429. end;
  5430. end;
  5431. '`' :
  5432. if in_multiline_string then
  5433. begin
  5434. readchar;
  5435. if c<>'`' then
  5436. break;
  5437. whitespace_only:=false;
  5438. end;
  5439. else
  5440. whitespace_only:=false;
  5441. quote_count:=0;
  5442. end;
  5443. first_multiline:=false;
  5444. { interpret as utf-8 string? }
  5445. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  5446. begin
  5447. { convert existing string to an utf-8 string }
  5448. if not iswidestring then
  5449. begin
  5450. if len>0 then
  5451. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5452. else
  5453. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5454. iswidestring:=true;
  5455. len:=0;
  5456. end;
  5457. { four chars }
  5458. if (ord(c) and $f0)=$f0 then
  5459. begin
  5460. { this always represents a surrogate pair, so
  5461. read as 32-bit value and then split into
  5462. the corresponding pair of two wchars }
  5463. d:=ord(c) and $f;
  5464. readchar;
  5465. if (ord(c) and $c0)<>$80 then
  5466. message(scan_e_utf8_malformed);
  5467. d:=(d shl 6) or (ord(c) and $3f);
  5468. readchar;
  5469. if (ord(c) and $c0)<>$80 then
  5470. message(scan_e_utf8_malformed);
  5471. d:=(d shl 6) or (ord(c) and $3f);
  5472. readchar;
  5473. if (ord(c) and $c0)<>$80 then
  5474. message(scan_e_utf8_malformed);
  5475. d:=(d shl 6) or (ord(c) and $3f);
  5476. if d<$10000 then
  5477. message(scan_e_utf8_malformed);
  5478. d:=d-$10000;
  5479. { high surrogate }
  5480. w:=$d800+(d shr 10);
  5481. concatwidestringchar(patternw,w);
  5482. { low surrogate }
  5483. w:=$dc00+(d and $3ff);
  5484. concatwidestringchar(patternw,w);
  5485. end
  5486. { three chars }
  5487. else if (ord(c) and $e0)=$e0 then
  5488. begin
  5489. w:=ord(c) and $f;
  5490. readchar;
  5491. if (ord(c) and $c0)<>$80 then
  5492. message(scan_e_utf8_malformed);
  5493. w:=(w shl 6) or (ord(c) and $3f);
  5494. readchar;
  5495. if (ord(c) and $c0)<>$80 then
  5496. message(scan_e_utf8_malformed);
  5497. w:=(w shl 6) or (ord(c) and $3f);
  5498. concatwidestringchar(patternw,w);
  5499. end
  5500. { two chars }
  5501. else if (ord(c) and $c0)<>0 then
  5502. begin
  5503. w:=ord(c) and $1f;
  5504. readchar;
  5505. if (ord(c) and $c0)<>$80 then
  5506. message(scan_e_utf8_malformed);
  5507. w:=(w shl 6) or (ord(c) and $3f);
  5508. concatwidestringchar(patternw,w);
  5509. end
  5510. { illegal }
  5511. else if (ord(c) and $80)<>0 then
  5512. message(scan_e_utf8_malformed)
  5513. else
  5514. concatwidestringchar(patternw,tcompilerwidechar(c))
  5515. end
  5516. else if iswidestring then
  5517. begin
  5518. if in_multiline_string and (c in [#10,#13]) and (not ((c=#10) and (last_c=#13))) then
  5519. begin
  5520. if current_settings.sourcecodepage=CP_UTF8 then
  5521. begin
  5522. case current_settings.lineendingtype of
  5523. le_cr :
  5524. concatwidestringchar(patternw,ord(#13));
  5525. le_crlf :
  5526. begin
  5527. concatwidestringchar(patternw,ord(#13));
  5528. concatwidestringchar(patternw,ord(#10));
  5529. end;
  5530. le_lf :
  5531. concatwidestringchar(patternw,ord(#10));
  5532. le_platform :
  5533. begin
  5534. if target_info.newline=#13 then
  5535. concatwidestringchar(patternw,ord(#13))
  5536. else if target_info.newline=#13#10 then
  5537. begin
  5538. concatwidestringchar(patternw,ord(#13));
  5539. concatwidestringchar(patternw,ord(#10));
  5540. end
  5541. else if target_info.newline=#10 then
  5542. concatwidestringchar(patternw,ord(#10));
  5543. end;
  5544. le_source :
  5545. concatwidestringchar(patternw,ord(c));
  5546. end;
  5547. end
  5548. else
  5549. case current_settings.lineendingtype of
  5550. le_cr :
  5551. concatwidestringchar(patternw,asciichar2unicode(#13));
  5552. le_crlf :
  5553. begin
  5554. concatwidestringchar(patternw,asciichar2unicode(#13));
  5555. concatwidestringchar(patternw,asciichar2unicode(#10));
  5556. end;
  5557. le_lf :
  5558. concatwidestringchar(patternw,asciichar2unicode(#10));
  5559. le_platform :
  5560. begin
  5561. if target_info.newline=#13 then
  5562. concatwidestringchar(patternw,asciichar2unicode(#13))
  5563. else if target_info.newline=#13#10 then
  5564. begin
  5565. concatwidestringchar(patternw,asciichar2unicode(#13));
  5566. concatwidestringchar(patternw,asciichar2unicode(#10));
  5567. end
  5568. else if target_info.newline=#10 then
  5569. concatwidestringchar(patternw,asciichar2unicode(#10));
  5570. end;
  5571. le_source :
  5572. concatwidestringchar(patternw,asciichar2unicode(c));
  5573. end;
  5574. had_newline:=true;
  5575. inc(line_no);
  5576. end
  5577. else if not (in_multiline_string and (c in [#10,#13])) then
  5578. begin
  5579. if current_settings.sourcecodepage=CP_UTF8 then
  5580. concatwidestringchar(patternw,ord(c))
  5581. else
  5582. concatwidestringchar(patternw,asciichar2unicode(c));
  5583. end;
  5584. end
  5585. else
  5586. begin
  5587. if in_multiline_string and (c in [#10,#13]) and (not ((c=#10) and (last_c=#13))) then
  5588. begin
  5589. if len>=length(cstringpattern) then
  5590. setlength(cstringpattern,length(cstringpattern)+256);
  5591. inc(len);
  5592. case current_settings.lineendingtype of
  5593. le_cr :
  5594. cstringpattern[len]:=#13;
  5595. le_crlf :
  5596. begin
  5597. cstringpattern[len]:=#13;
  5598. inc(len);
  5599. cstringpattern[len]:=#10;
  5600. end;
  5601. le_lf :
  5602. cstringpattern[len]:=#10;
  5603. le_platform :
  5604. begin
  5605. if target_info.newline=#13 then
  5606. cstringpattern[len]:=#13
  5607. else if target_info.newline=#13#10 then
  5608. begin
  5609. cstringpattern[len]:=#13;
  5610. inc(len);
  5611. cstringpattern[len]:=#10;
  5612. end
  5613. else if target_info.newline=#10 then
  5614. cstringpattern[len]:=#10;
  5615. end;
  5616. le_source :
  5617. cstringpattern[len]:=c;
  5618. end;
  5619. had_newline:=true;
  5620. inc(line_no);
  5621. end
  5622. else if not (in_multiline_string and (c in [#10,#13])) then
  5623. begin
  5624. if len>=length(cstringpattern) then
  5625. setlength(cstringpattern,length(cstringpattern)+256);
  5626. inc(len);
  5627. cstringpattern[len]:=c;
  5628. end;
  5629. end;
  5630. last_c:=c;
  5631. until false;
  5632. end;
  5633. '^' :
  5634. begin
  5635. readchar;
  5636. c:=upcase(c);
  5637. if c<#64 then
  5638. c:=chr(ord(c)+64)
  5639. else
  5640. c:=chr(ord(c)-64);
  5641. if iswidestring then
  5642. concatwidestringchar(patternw,asciichar2unicode(c))
  5643. else
  5644. begin
  5645. if len>=length(cstringpattern) then
  5646. setlength(cstringpattern,length(cstringpattern)+256);
  5647. inc(len);
  5648. cstringpattern[len]:=c;
  5649. end;
  5650. readchar;
  5651. end;
  5652. else
  5653. break;
  5654. end;
  5655. last_c:=c;
  5656. until false;
  5657. { strings with length 1 become const chars }
  5658. if iswidestring then
  5659. begin
  5660. if had_multiline_string and not backtick then
  5661. begin
  5662. postprocessutf8multiline(len,quote_col,init_quote_count);
  5663. end;
  5664. if patternw.len=1 then
  5665. token:=_CWCHAR
  5666. else
  5667. token:=_CWSTRING;
  5668. end
  5669. else
  5670. begin
  5671. if had_multiline_string and not backtick then
  5672. begin
  5673. postprocessmultiline(len,quote_col,init_quote_count);
  5674. end
  5675. else
  5676. setlength(cstringpattern,len);
  5677. if length(cstringpattern)=1 then
  5678. begin
  5679. token:=_CCHAR;
  5680. pattern:=cstringpattern;
  5681. end
  5682. else
  5683. token:=_CSTRING;
  5684. end;
  5685. exit(true);
  5686. end;
  5687. {****************************************************************************
  5688. Token Scanner
  5689. ****************************************************************************}
  5690. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  5691. var
  5692. code : integer;
  5693. d : cardinal;
  5694. len,
  5695. low,high,mid : longint;
  5696. w : word;
  5697. m : longint;
  5698. mac : tmacro;
  5699. firstdigitread: boolean;
  5700. had_newline,first_multiline : boolean;
  5701. trimcount : word;
  5702. label
  5703. exit_label;
  5704. begin
  5705. had_newline:=false;
  5706. first_multiline:=false;
  5707. flushpendingswitchesstate;
  5708. { record tokens? }
  5709. if allowrecordtoken and
  5710. assigned(recordtokenbuf) then
  5711. recordtoken;
  5712. { replay tokens? }
  5713. if assigned(replaytokenbuf) then
  5714. begin
  5715. replaytoken;
  5716. goto exit_label;
  5717. end;
  5718. { was there already a token read, then return that token }
  5719. if nexttoken<>NOTOKEN then
  5720. begin
  5721. setnexttoken;
  5722. goto exit_label;
  5723. end;
  5724. { Skip all spaces and comments }
  5725. repeat
  5726. case c of
  5727. '{' :
  5728. skipcomment(true);
  5729. #26 :
  5730. begin
  5731. reload;
  5732. if (c=#26) and not assigned(inputfile.next) then
  5733. break;
  5734. end;
  5735. ' ',#9..#13 :
  5736. begin
  5737. {$ifdef PREPROCWRITE}
  5738. if parapreprocess then
  5739. begin
  5740. if c=#10 then
  5741. preprocfile.eolfound:=true
  5742. else
  5743. preprocfile.spacefound:=true;
  5744. end;
  5745. {$endif PREPROCWRITE}
  5746. skipspace;
  5747. end
  5748. else
  5749. break;
  5750. end;
  5751. until false;
  5752. { Save current token position, for EOF its already loaded }
  5753. if c<>#26 then
  5754. gettokenpos;
  5755. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  5756. if c in ['A'..'Z','a'..'z','_'] then
  5757. begin
  5758. readstring;
  5759. token:=_ID;
  5760. idtoken:=_ID;
  5761. { keyword or any other known token,
  5762. pattern is always uppercased }
  5763. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  5764. begin
  5765. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  5766. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  5767. while low<high do
  5768. begin
  5769. mid:=(high+low+1) shr 1;
  5770. if pattern<tokeninfo^[ttoken(mid)].str then
  5771. high:=mid-1
  5772. else
  5773. low:=mid;
  5774. end;
  5775. with tokeninfo^[ttoken(high)] do
  5776. if pattern=str then
  5777. begin
  5778. if (keyword*current_settings.modeswitches)<>[] then
  5779. if op=NOTOKEN then
  5780. token:=ttoken(high)
  5781. else
  5782. token:=op;
  5783. idtoken:=ttoken(high);
  5784. end;
  5785. end;
  5786. { Only process identifiers and not keywords }
  5787. if token=_ID then
  5788. begin
  5789. { this takes some time ... }
  5790. if (cs_support_macro in current_settings.moduleswitches) then
  5791. begin
  5792. mac:=tmacro(search_macro(pattern));
  5793. if assigned(mac) and (not mac.is_compiler_var) and mac.is_c_macro then
  5794. begin
  5795. if (yylexcount<max_macro_nesting) and (macro_nesting_depth<max_macro_nesting) then
  5796. begin
  5797. mac.is_used:=true;
  5798. inc(yylexcount);
  5799. substitutemacro(pattern,pchar(mac.buftext),mac.buflen,
  5800. mac.fileinfo.line,mac.fileinfo.fileindex,false);
  5801. { handle empty macros }
  5802. if c=#0 then
  5803. begin
  5804. reload;
  5805. { avoid macro nesting error in case of
  5806. a sequence of empty macros, see #38802 }
  5807. dec(yylexcount);
  5808. readtoken(false);
  5809. end
  5810. else
  5811. begin
  5812. readtoken(false);
  5813. { that's all folks }
  5814. dec(yylexcount);
  5815. end;
  5816. exit;
  5817. end
  5818. else
  5819. Message(scan_w_macro_too_deep);
  5820. end;
  5821. end;
  5822. end;
  5823. { return token }
  5824. goto exit_label;
  5825. end
  5826. else
  5827. begin
  5828. idtoken:=_NOID;
  5829. case c of
  5830. '$' :
  5831. begin
  5832. readnumber;
  5833. token:=_INTCONST;
  5834. goto exit_label;
  5835. end;
  5836. '%' :
  5837. begin
  5838. if [m_fpc,m_delphi] * current_settings.modeswitches = [] then
  5839. Illegal_Char(c)
  5840. else
  5841. begin
  5842. readnumber;
  5843. token:=_INTCONST;
  5844. goto exit_label;
  5845. end;
  5846. end;
  5847. '&' :
  5848. begin
  5849. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  5850. begin
  5851. readnumber;
  5852. if length(pattern)=1 then
  5853. begin
  5854. { does really an identifier follow? }
  5855. if not (c in ['_','A'..'Z','a'..'z']) then
  5856. message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  5857. readstring;
  5858. token:=_ID;
  5859. idtoken:=_ID;
  5860. end
  5861. else
  5862. token:=_INTCONST;
  5863. goto exit_label;
  5864. end
  5865. else if m_mac in current_settings.modeswitches then
  5866. begin
  5867. readchar;
  5868. token:=_AMPERSAND;
  5869. goto exit_label;
  5870. end
  5871. else
  5872. Illegal_Char(c);
  5873. end;
  5874. '0'..'9' :
  5875. begin
  5876. readnumber;
  5877. if (c in ['.','e','E']) then
  5878. begin
  5879. { first check for a . }
  5880. if c='.' then
  5881. begin
  5882. cachenexttokenpos;
  5883. readchar;
  5884. { is it a .. from a range? }
  5885. case c of
  5886. '.' :
  5887. begin
  5888. readchar;
  5889. token:=_INTCONST;
  5890. nexttoken:=_POINTPOINT;
  5891. goto exit_label;
  5892. end;
  5893. ')' :
  5894. begin
  5895. readchar;
  5896. token:=_INTCONST;
  5897. nexttoken:=_RECKKLAMMER;
  5898. goto exit_label;
  5899. end;
  5900. '0'..'9' :
  5901. begin
  5902. { insert the number after the . }
  5903. pattern:=pattern+'.';
  5904. firstdigitread:=false;
  5905. while (c in ['0'..'9']) or
  5906. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  5907. begin
  5908. if c<>'_' then
  5909. pattern:=pattern+c;
  5910. readchar;
  5911. firstdigitread:=true;
  5912. end;
  5913. end;
  5914. else
  5915. begin
  5916. token:=_INTCONST;
  5917. nexttoken:=_POINT;
  5918. goto exit_label;
  5919. end;
  5920. end;
  5921. end;
  5922. { E can also follow after a point is scanned }
  5923. if c in ['e','E'] then
  5924. begin
  5925. pattern:=pattern+'E';
  5926. readchar;
  5927. if c in ['-','+'] then
  5928. begin
  5929. pattern:=pattern+c;
  5930. readchar;
  5931. end;
  5932. if not(c in ['0'..'9']) then
  5933. Illegal_Char(c);
  5934. firstdigitread:=false;
  5935. while (c in ['0'..'9']) or
  5936. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  5937. begin
  5938. if c<>'_' then
  5939. pattern:=pattern+c;
  5940. readchar;
  5941. firstdigitread:=true;
  5942. end;
  5943. end;
  5944. token:=_REALNUMBER;
  5945. goto exit_label;
  5946. end;
  5947. token:=_INTCONST;
  5948. goto exit_label;
  5949. end;
  5950. ';' :
  5951. begin
  5952. readchar;
  5953. token:=_SEMICOLON;
  5954. goto exit_label;
  5955. end;
  5956. '[' :
  5957. begin
  5958. readchar;
  5959. token:=_LECKKLAMMER;
  5960. goto exit_label;
  5961. end;
  5962. ']' :
  5963. begin
  5964. readchar;
  5965. token:=_RECKKLAMMER;
  5966. goto exit_label;
  5967. end;
  5968. '(' :
  5969. begin
  5970. readchar;
  5971. case c of
  5972. '*' :
  5973. begin
  5974. skipoldtpcomment(true);
  5975. readtoken(false);
  5976. exit;
  5977. end;
  5978. '.' :
  5979. begin
  5980. readchar;
  5981. token:=_LECKKLAMMER;
  5982. goto exit_label;
  5983. end;
  5984. end;
  5985. token:=_LKLAMMER;
  5986. goto exit_label;
  5987. end;
  5988. ')' :
  5989. begin
  5990. readchar;
  5991. token:=_RKLAMMER;
  5992. goto exit_label;
  5993. end;
  5994. '+' :
  5995. begin
  5996. readchar;
  5997. if c='=' then
  5998. begin
  5999. readchar;
  6000. token:=_PLUSASN;
  6001. goto exit_label;
  6002. end;
  6003. token:=_PLUS;
  6004. goto exit_label;
  6005. end;
  6006. '-' :
  6007. begin
  6008. readchar;
  6009. if c='=' then
  6010. begin
  6011. readchar;
  6012. token:=_MINUSASN;
  6013. goto exit_label;
  6014. end;
  6015. token:=_MINUS;
  6016. goto exit_label;
  6017. end;
  6018. ':' :
  6019. begin
  6020. readchar;
  6021. if c='=' then
  6022. begin
  6023. readchar;
  6024. token:=_ASSIGNMENT;
  6025. goto exit_label;
  6026. end;
  6027. token:=_COLON;
  6028. goto exit_label;
  6029. end;
  6030. '*' :
  6031. begin
  6032. readchar;
  6033. if c='=' then
  6034. begin
  6035. readchar;
  6036. token:=_STARASN;
  6037. end
  6038. else
  6039. if c='*' then
  6040. begin
  6041. readchar;
  6042. token:=_STARSTAR;
  6043. end
  6044. else
  6045. token:=_STAR;
  6046. goto exit_label;
  6047. end;
  6048. '/' :
  6049. begin
  6050. readchar;
  6051. case c of
  6052. '=' :
  6053. begin
  6054. readchar;
  6055. token:=_SLASHASN;
  6056. goto exit_label;
  6057. end;
  6058. '/' :
  6059. begin
  6060. skipdelphicomment;
  6061. readtoken(false);
  6062. exit;
  6063. end;
  6064. end;
  6065. token:=_SLASH;
  6066. goto exit_label;
  6067. end;
  6068. '|' :
  6069. if m_mac in current_settings.modeswitches then
  6070. begin
  6071. readchar;
  6072. token:=_PIPE;
  6073. goto exit_label;
  6074. end
  6075. else
  6076. Illegal_Char(c);
  6077. '=' :
  6078. begin
  6079. readchar;
  6080. token:=_EQ;
  6081. goto exit_label;
  6082. end;
  6083. '.' :
  6084. begin
  6085. readchar;
  6086. case c of
  6087. '.' :
  6088. begin
  6089. readchar;
  6090. case c of
  6091. '.' :
  6092. begin
  6093. readchar;
  6094. token:=_POINTPOINTPOINT;
  6095. goto exit_label;
  6096. end;
  6097. else
  6098. begin
  6099. token:=_POINTPOINT;
  6100. goto exit_label;
  6101. end;
  6102. end;
  6103. end;
  6104. ')' :
  6105. begin
  6106. readchar;
  6107. token:=_RECKKLAMMER;
  6108. goto exit_label;
  6109. end;
  6110. end;
  6111. token:=_POINT;
  6112. goto exit_label;
  6113. end;
  6114. '@' :
  6115. begin
  6116. readchar;
  6117. token:=_KLAMMERAFFE;
  6118. goto exit_label;
  6119. end;
  6120. ',' :
  6121. begin
  6122. readchar;
  6123. token:=_COMMA;
  6124. goto exit_label;
  6125. end;
  6126. '''','#','^','`' :
  6127. begin
  6128. readstringconstant;
  6129. goto exit_label;
  6130. end;
  6131. '>' :
  6132. begin
  6133. readchar;
  6134. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  6135. token:=_RSHARPBRACKET
  6136. else
  6137. begin
  6138. case c of
  6139. '=' :
  6140. begin
  6141. readchar;
  6142. token:=_GTE;
  6143. goto exit_label;
  6144. end;
  6145. '>' :
  6146. begin
  6147. readchar;
  6148. token:=_OP_SHR;
  6149. goto exit_label;
  6150. end;
  6151. '<' :
  6152. begin { >< is for a symetric diff for sets }
  6153. readchar;
  6154. token:=_SYMDIF;
  6155. goto exit_label;
  6156. end;
  6157. end;
  6158. token:=_GT;
  6159. end;
  6160. goto exit_label;
  6161. end;
  6162. '<' :
  6163. begin
  6164. readchar;
  6165. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  6166. token:=_LSHARPBRACKET
  6167. else
  6168. begin
  6169. case c of
  6170. '>' :
  6171. begin
  6172. readchar;
  6173. token:=_NE;
  6174. goto exit_label;
  6175. end;
  6176. '=' :
  6177. begin
  6178. readchar;
  6179. token:=_LTE;
  6180. goto exit_label;
  6181. end;
  6182. '<' :
  6183. begin
  6184. readchar;
  6185. token:=_OP_SHL;
  6186. goto exit_label;
  6187. end;
  6188. end;
  6189. token:=_LT;
  6190. end;
  6191. goto exit_label;
  6192. end;
  6193. #26 :
  6194. begin
  6195. token:=_EOF;
  6196. checkpreprocstack;
  6197. goto exit_label;
  6198. end;
  6199. else if inputfile.internally_generated_macro and
  6200. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  6201. begin
  6202. token:=_ID;
  6203. readstring;
  6204. end
  6205. else
  6206. Illegal_Char(c);
  6207. end;
  6208. end;
  6209. exit_label:
  6210. lasttoken:=token;
  6211. end;
  6212. function tscannerfile.readpreproc:ttoken;
  6213. var
  6214. low,high,mid: longint;
  6215. optoken: ttoken;
  6216. begin
  6217. { Added the assignment to NOTOKEN below because I got a DFA uninitialized result
  6218. warning when building the compiler with -O3, which broke compilation with -Sew.
  6219. - Akira1364 }
  6220. readpreproc:=NOTOKEN;
  6221. skipspace;
  6222. case c of
  6223. '_',
  6224. 'A'..'Z',
  6225. 'a'..'z' :
  6226. begin
  6227. readstring;
  6228. optoken:=_ID;
  6229. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  6230. begin
  6231. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  6232. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  6233. while low<high do
  6234. begin
  6235. mid:=(high+low+1) shr 1;
  6236. if pattern<tokeninfo^[ttoken(mid)].str then
  6237. high:=mid-1
  6238. else
  6239. low:=mid;
  6240. end;
  6241. with tokeninfo^[ttoken(high)] do
  6242. if pattern=str then
  6243. begin
  6244. if (keyword*current_settings.modeswitches)<>[] then
  6245. if op=NOTOKEN then
  6246. optoken:=ttoken(high)
  6247. else
  6248. optoken:=op;
  6249. end;
  6250. if not (optoken in preproc_operators) then
  6251. optoken:=_ID;
  6252. end;
  6253. current_scanner.preproc_pattern:=pattern;
  6254. readpreproc:=optoken;
  6255. end;
  6256. '''','`' :
  6257. if not ((c='`') and (not (m_multiline_strings in current_settings.modeswitches))) then
  6258. begin
  6259. readquotedstring;
  6260. current_scanner.preproc_pattern:=cstringpattern;
  6261. readpreproc:=_CSTRING;
  6262. end;
  6263. '0'..'9' :
  6264. begin
  6265. readnumber;
  6266. if (c in ['.','e','E']) then
  6267. begin
  6268. { first check for a . }
  6269. if c='.' then
  6270. begin
  6271. readchar;
  6272. if c in ['0'..'9'] then
  6273. begin
  6274. { insert the number after the . }
  6275. pattern:=pattern+'.';
  6276. while c in ['0'..'9'] do
  6277. begin
  6278. pattern:=pattern+c;
  6279. readchar;
  6280. end;
  6281. end
  6282. else
  6283. Illegal_Char(c);
  6284. end;
  6285. { E can also follow after a point is scanned }
  6286. if c in ['e','E'] then
  6287. begin
  6288. pattern:=pattern+'E';
  6289. readchar;
  6290. if c in ['-','+'] then
  6291. begin
  6292. pattern:=pattern+c;
  6293. readchar;
  6294. end;
  6295. if not(c in ['0'..'9']) then
  6296. Illegal_Char(c);
  6297. while c in ['0'..'9'] do
  6298. begin
  6299. pattern:=pattern+c;
  6300. readchar;
  6301. end;
  6302. end;
  6303. readpreproc:=_REALNUMBER;
  6304. end
  6305. else
  6306. readpreproc:=_INTCONST;
  6307. current_scanner.preproc_pattern:=pattern;
  6308. end;
  6309. '$','%':
  6310. begin
  6311. readnumber;
  6312. current_scanner.preproc_pattern:=pattern;
  6313. readpreproc:=_INTCONST;
  6314. end;
  6315. '&' :
  6316. begin
  6317. readnumber;
  6318. if length(pattern)=1 then
  6319. begin
  6320. readstring;
  6321. readpreproc:=_ID;
  6322. end
  6323. else
  6324. readpreproc:=_INTCONST;
  6325. current_scanner.preproc_pattern:=pattern;
  6326. end;
  6327. '.' :
  6328. begin
  6329. readchar;
  6330. readpreproc:=_POINT;
  6331. end;
  6332. ',' :
  6333. begin
  6334. readchar;
  6335. readpreproc:=_COMMA;
  6336. end;
  6337. '}' :
  6338. begin
  6339. readpreproc:=_END;
  6340. end;
  6341. '(' :
  6342. begin
  6343. readchar;
  6344. readpreproc:=_LKLAMMER;
  6345. end;
  6346. ')' :
  6347. begin
  6348. readchar;
  6349. readpreproc:=_RKLAMMER;
  6350. end;
  6351. '[' :
  6352. begin
  6353. readchar;
  6354. readpreproc:=_LECKKLAMMER;
  6355. end;
  6356. ']' :
  6357. begin
  6358. readchar;
  6359. readpreproc:=_RECKKLAMMER;
  6360. end;
  6361. '+' :
  6362. begin
  6363. readchar;
  6364. readpreproc:=_PLUS;
  6365. end;
  6366. '-' :
  6367. begin
  6368. readchar;
  6369. readpreproc:=_MINUS;
  6370. end;
  6371. '*' :
  6372. begin
  6373. readchar;
  6374. readpreproc:=_STAR;
  6375. end;
  6376. '/' :
  6377. begin
  6378. readchar;
  6379. readpreproc:=_SLASH;
  6380. end;
  6381. '=' :
  6382. begin
  6383. readchar;
  6384. readpreproc:=_EQ;
  6385. end;
  6386. '>' :
  6387. begin
  6388. readchar;
  6389. if c='=' then
  6390. begin
  6391. readchar;
  6392. readpreproc:=_GTE;
  6393. end
  6394. else
  6395. readpreproc:=_GT;
  6396. end;
  6397. '<' :
  6398. begin
  6399. readchar;
  6400. case c of
  6401. '>' :
  6402. begin
  6403. readchar;
  6404. readpreproc:=_NE;
  6405. end;
  6406. '=' :
  6407. begin
  6408. readchar;
  6409. readpreproc:=_LTE;
  6410. end;
  6411. else
  6412. readpreproc:=_LT;
  6413. end;
  6414. end;
  6415. #26 :
  6416. begin
  6417. readpreproc:=_EOF;
  6418. checkpreprocstack;
  6419. end;
  6420. else
  6421. begin
  6422. Illegal_Char(c);
  6423. readpreproc:=NOTOKEN;
  6424. end;
  6425. end;
  6426. end;
  6427. function tscannerfile.readpreprocint(var value:int64;const place:string):boolean;
  6428. var
  6429. hs : texprvalue;
  6430. begin
  6431. hs:=preproc_comp_expr(nil);
  6432. if hs.isInt then
  6433. begin
  6434. value:=hs.asInt64;
  6435. result:=true;
  6436. end
  6437. else
  6438. begin
  6439. hs.error('Integer',place);
  6440. result:=false;
  6441. end;
  6442. hs.free;
  6443. end;
  6444. function tscannerfile.readpreprocset(conform_to:tsetdef;var value:tnormalset;const place:string):boolean;
  6445. var
  6446. hs : texprvalue;
  6447. begin
  6448. hs:=preproc_comp_expr(conform_to);
  6449. if hs.def.typ=setdef then
  6450. begin
  6451. value:=hs.asSet;
  6452. result:=true;
  6453. end
  6454. else
  6455. begin
  6456. hs.error('Set',place);
  6457. result:=false;
  6458. end;
  6459. hs.free;
  6460. end;
  6461. function tscannerfile.asmgetchar : char;
  6462. begin
  6463. readchar;
  6464. repeat
  6465. case c of
  6466. #26 :
  6467. begin
  6468. reload;
  6469. if (c=#26) and not assigned(inputfile.next) then
  6470. end_of_file;
  6471. continue;
  6472. end;
  6473. else
  6474. begin
  6475. asmgetchar:=c;
  6476. exit;
  6477. end;
  6478. end;
  6479. until false;
  6480. end;
  6481. {$ifdef EXTDEBUG}
  6482. function tscannerfile.DumpPointer: string;
  6483. var
  6484. i: Integer;
  6485. begin
  6486. Result:='';
  6487. if inputpointer=nil then exit;
  6488. i:=0;
  6489. While (inputpointer[I]<>#0) and (i<200) do
  6490. inc(i);
  6491. Setlength(result,I);
  6492. move(inputpointer^,Result[1],I);
  6493. result:='<'+inttostr(inputstart)+'>'+result;
  6494. end;
  6495. {$endif EXTDEBUG}
  6496. {*****************************************************************************
  6497. Helpers
  6498. *****************************************************************************}
  6499. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  6500. begin
  6501. if dm in [directive_all, directive_turbo] then
  6502. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  6503. if dm in [directive_all, directive_mac] then
  6504. tdirectiveitem.create(mac_scannerdirectives,s,p);
  6505. end;
  6506. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  6507. begin
  6508. if dm in [directive_all, directive_turbo] then
  6509. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  6510. if dm in [directive_all, directive_mac] then
  6511. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  6512. end;
  6513. {*****************************************************************************
  6514. Initialization
  6515. *****************************************************************************}
  6516. procedure InitScanner;
  6517. begin
  6518. InitWideString(patternw);
  6519. turbo_scannerdirectives:=TFPHashObjectList.Create;
  6520. mac_scannerdirectives:=TFPHashObjectList.Create;
  6521. { Common directives and conditionals }
  6522. AddDirective('I',directive_all, @dir_include);
  6523. AddDirective('DEFINE',directive_all, @dir_define);
  6524. AddDirective('UNDEF',directive_all, @dir_undef);
  6525. AddConditional('IF',directive_all, @dir_if);
  6526. AddConditional('IFDEF',directive_all, @dir_ifdef);
  6527. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  6528. AddConditional('ELSE',directive_all, @dir_else);
  6529. AddConditional('ELSEIF',directive_all, @dir_elseif);
  6530. AddConditional('ENDIF',directive_all, @dir_endif);
  6531. { Directives and conditionals for all modes except mode macpas}
  6532. AddDirective('INCLUDE',directive_turbo, @dir_include);
  6533. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  6534. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  6535. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  6536. AddConditional('IFEND',directive_turbo, @dir_ifend);
  6537. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  6538. { Directives and conditionals for mode macpas: }
  6539. AddDirective('SETC',directive_mac, @dir_setc);
  6540. AddDirective('DEFINEC',directive_mac, @dir_definec);
  6541. AddDirective('UNDEFC',directive_mac, @dir_undef);
  6542. AddConditional('IFC',directive_mac, @dir_if);
  6543. AddConditional('ELSEC',directive_mac, @dir_else);
  6544. AddConditional('ELIFC',directive_mac, @dir_elseif);
  6545. AddConditional('ENDC',directive_mac, @dir_endif);
  6546. end;
  6547. procedure DoneScanner;
  6548. begin
  6549. turbo_scannerdirectives.Free;
  6550. mac_scannerdirectives.Free;
  6551. DoneWideString(patternw);
  6552. end;
  6553. end.