scanner.pas 211 KB

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