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