scanner.pas 193 KB

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