scanner.pas 197 KB

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