scanner.pas 194 KB

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