scanner.pas 194 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776
  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. if timestr<>'' then
  2318. hs:=timestr
  2319. else
  2320. hs:=gettimestr;
  2321. 'DATE':
  2322. if datestr<>'' then
  2323. hs:=datestr
  2324. else
  2325. hs:=getdatestr;
  2326. 'DATEYEAR':
  2327. begin
  2328. hs:=tostr(startsystime.Year);
  2329. macroIsString:=false;
  2330. end;
  2331. 'DATEMONTH':
  2332. begin
  2333. hs:=tostr(startsystime.Month);
  2334. macroIsString:=false;
  2335. end;
  2336. 'DATEDAY':
  2337. begin
  2338. hs:=tostr(startsystime.Day);
  2339. macroIsString:=false;
  2340. end;
  2341. 'TIMEHOUR':
  2342. begin
  2343. hs:=tostr(startsystime.Hour);
  2344. macroIsString:=false;
  2345. end;
  2346. 'TIMEMINUTE':
  2347. begin
  2348. hs:=tostr(startsystime.Minute);
  2349. macroIsString:=false;
  2350. end;
  2351. 'TIMESECOND':
  2352. begin
  2353. hs:=tostr(startsystime.Second);
  2354. macroIsString:=false;
  2355. end;
  2356. 'FILE':
  2357. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex);
  2358. 'LINE':
  2359. hs:=tostr(current_filepos.line);
  2360. 'LINENUM':
  2361. begin
  2362. hs:=tostr(current_filepos.line);
  2363. macroIsString:=false;
  2364. end;
  2365. 'FPCVERSION':
  2366. hs:=version_string;
  2367. 'FPCDATE':
  2368. hs:=date_string;
  2369. 'FPCTARGET':
  2370. hs:=target_cpu_string;
  2371. 'FPCTARGETCPU':
  2372. hs:=target_cpu_string;
  2373. 'FPCTARGETOS':
  2374. hs:=target_info.shortname;
  2375. 'CURRENTROUTINE':
  2376. hs:=current_procinfo.procdef.procsym.RealName;
  2377. else
  2378. hs:=GetEnvironmentVariable(hs);
  2379. end;
  2380. if hs='' then
  2381. Message1(scan_w_include_env_not_found,path);
  2382. { make it a stringconst }
  2383. if macroIsString then
  2384. hs:=''''+hs+'''';
  2385. current_scanner.substitutemacro(path,@hs[1],length(hs),
  2386. current_scanner.line_no,current_scanner.inputfile.ref_index);
  2387. end
  2388. else
  2389. begin
  2390. hs:=FixFileName(hs);
  2391. path:=ExtractFilePath(hs);
  2392. name:=ExtractFileName(hs);
  2393. { Special case for Delphi compatibility: '*' has to be replaced
  2394. by the file name of the current source file. }
  2395. if (length(name)>=1) and
  2396. (name[1]='*') then
  2397. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  2398. { try to find the file }
  2399. found:=findincludefile(path,name,foundfile);
  2400. if (not found) and (ExtractFileExt(name)='') then
  2401. begin
  2402. { try default extensions .inc , .pp and .pas }
  2403. if (not found) then
  2404. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  2405. if (not found) then
  2406. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  2407. if (not found) then
  2408. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  2409. end;
  2410. { if the name ends in dot, try without the dot }
  2411. if (not found) and (ExtractFileExt(name)=ExtensionSeparator) and (Length(name)>=2) then
  2412. found:=findincludefile(path,Copy(name,1,Length(name)-1),foundfile);
  2413. if current_scanner.inputfilecount<max_include_nesting then
  2414. begin
  2415. inc(current_scanner.inputfilecount);
  2416. { we need to reread the current char }
  2417. dec(current_scanner.inputpointer);
  2418. { reset c }
  2419. c:=#0;
  2420. { shutdown current file }
  2421. current_scanner.tempcloseinputfile;
  2422. { load new file }
  2423. hp:=do_openinputfile(foundfile);
  2424. hp.inc_path:=path;
  2425. current_scanner.addfile(hp);
  2426. current_module.sourcefiles.register_file(hp);
  2427. if (not found) then
  2428. Message1(scan_f_cannot_open_includefile,hs);
  2429. if (not current_scanner.openinputfile) then
  2430. Message1(scan_f_cannot_open_includefile,hs);
  2431. Message1(scan_t_start_include_file,current_scanner.inputfile.path+current_scanner.inputfile.name);
  2432. current_scanner.reload;
  2433. end
  2434. else
  2435. Message(scan_f_include_deep_ten);
  2436. end;
  2437. end;
  2438. {*****************************************************************************
  2439. Preprocessor writing
  2440. *****************************************************************************}
  2441. {$ifdef PREPROCWRITE}
  2442. constructor tpreprocfile.create(const fn:string);
  2443. begin
  2444. inherited create;
  2445. { open outputfile }
  2446. assign(f,fn);
  2447. {$push}{$I-}
  2448. rewrite(f);
  2449. {$pop}
  2450. if ioresult<>0 then
  2451. Comment(V_Fatal,'can''t create file '+fn);
  2452. getmem(buf,preprocbufsize);
  2453. settextbuf(f,buf^,preprocbufsize);
  2454. { reset }
  2455. eolfound:=false;
  2456. spacefound:=false;
  2457. end;
  2458. destructor tpreprocfile.destroy;
  2459. begin
  2460. close(f);
  2461. freemem(buf,preprocbufsize);
  2462. end;
  2463. procedure tpreprocfile.add(const s:string);
  2464. begin
  2465. write(f,s);
  2466. end;
  2467. procedure tpreprocfile.addspace;
  2468. begin
  2469. if eolfound then
  2470. begin
  2471. writeln(f,'');
  2472. eolfound:=false;
  2473. spacefound:=false;
  2474. end
  2475. else
  2476. if spacefound then
  2477. begin
  2478. write(f,' ');
  2479. spacefound:=false;
  2480. end;
  2481. end;
  2482. {$endif PREPROCWRITE}
  2483. {*****************************************************************************
  2484. TPreProcStack
  2485. *****************************************************************************}
  2486. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  2487. begin
  2488. accept:=a;
  2489. typ:=atyp;
  2490. next:=n;
  2491. end;
  2492. {*****************************************************************************
  2493. TReplayStack
  2494. *****************************************************************************}
  2495. constructor treplaystack.Create(atoken:ttoken;aidtoken:ttoken;
  2496. const aorgpattern,apattern:string;const acstringpattern:ansistring;
  2497. apatternw:pcompilerwidestring;asettings:tsettings;
  2498. atokenbuf:tdynamicarray;change_endian:boolean;anext:treplaystack);
  2499. begin
  2500. token:=atoken;
  2501. idtoken:=aidtoken;
  2502. orgpattern:=aorgpattern;
  2503. pattern:=apattern;
  2504. cstringpattern:=acstringpattern;
  2505. initwidestring(patternw);
  2506. if assigned(apatternw) then
  2507. begin
  2508. setlengthwidestring(patternw,apatternw^.len);
  2509. move(apatternw^.data^,patternw^.data^,apatternw^.len*sizeof(tcompilerwidechar));
  2510. end;
  2511. settings:=asettings;
  2512. tokenbuf:=atokenbuf;
  2513. tokenbuf_needs_swapping:=change_endian;
  2514. next:=anext;
  2515. end;
  2516. destructor treplaystack.destroy;
  2517. begin
  2518. donewidestring(patternw);
  2519. end;
  2520. {*****************************************************************************
  2521. TDirectiveItem
  2522. *****************************************************************************}
  2523. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2524. begin
  2525. inherited Create(AList,n);
  2526. is_conditional:=false;
  2527. proc:=p;
  2528. end;
  2529. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2530. begin
  2531. inherited Create(AList,n);
  2532. is_conditional:=true;
  2533. proc:=p;
  2534. end;
  2535. {****************************************************************************
  2536. TSCANNERFILE
  2537. ****************************************************************************}
  2538. constructor tscannerfile.create(const fn:string; is_macro: boolean = false);
  2539. begin
  2540. inputfile:=do_openinputfile(fn);
  2541. if is_macro then
  2542. inputfile.is_macro:=true;
  2543. if assigned(current_module) then
  2544. current_module.sourcefiles.register_file(inputfile);
  2545. { reset localinput }
  2546. c:=#0;
  2547. inputbuffer:=nil;
  2548. inputpointer:=nil;
  2549. inputstart:=0;
  2550. { reset scanner }
  2551. preprocstack:=nil;
  2552. replaystack:=nil;
  2553. comment_level:=0;
  2554. yylexcount:=0;
  2555. block_type:=bt_general;
  2556. line_no:=0;
  2557. lastlinepos:=0;
  2558. lasttokenpos:=0;
  2559. nexttokenpos:=0;
  2560. lasttoken:=NOTOKEN;
  2561. nexttoken:=NOTOKEN;
  2562. ignoredirectives:=TFPHashList.Create;
  2563. change_endian_for_replay:=false;
  2564. end;
  2565. procedure tscannerfile.firstfile;
  2566. begin
  2567. { load block }
  2568. if not openinputfile then
  2569. Message1(scan_f_cannot_open_input,inputfile.name);
  2570. reload;
  2571. end;
  2572. destructor tscannerfile.destroy;
  2573. begin
  2574. if assigned(current_module) and
  2575. (current_module.state=ms_compiled) and
  2576. (status.errorcount=0) then
  2577. checkpreprocstack
  2578. else
  2579. begin
  2580. while assigned(preprocstack) do
  2581. poppreprocstack;
  2582. end;
  2583. while assigned(replaystack) do
  2584. popreplaystack;
  2585. if not inputfile.closed then
  2586. closeinputfile;
  2587. if inputfile.is_macro then
  2588. inputfile.free;
  2589. ignoredirectives.free;
  2590. end;
  2591. function tscannerfile.openinputfile:boolean;
  2592. begin
  2593. openinputfile:=inputfile.open;
  2594. { load buffer }
  2595. inputbuffer:=inputfile.buf;
  2596. inputpointer:=inputfile.buf;
  2597. inputstart:=inputfile.bufstart;
  2598. { line }
  2599. line_no:=0;
  2600. lastlinepos:=0;
  2601. lasttokenpos:=0;
  2602. nexttokenpos:=0;
  2603. end;
  2604. procedure tscannerfile.closeinputfile;
  2605. begin
  2606. inputfile.close;
  2607. { reset buffer }
  2608. inputbuffer:=nil;
  2609. inputpointer:=nil;
  2610. inputstart:=0;
  2611. { reset line }
  2612. line_no:=0;
  2613. lastlinepos:=0;
  2614. lasttokenpos:=0;
  2615. nexttokenpos:=0;
  2616. end;
  2617. function tscannerfile.tempopeninputfile:boolean;
  2618. begin
  2619. tempopeninputfile:=false;
  2620. if inputfile.is_macro then
  2621. exit;
  2622. tempopeninputfile:=inputfile.tempopen;
  2623. { reload buffer }
  2624. inputbuffer:=inputfile.buf;
  2625. inputpointer:=inputfile.buf;
  2626. inputstart:=inputfile.bufstart;
  2627. end;
  2628. procedure tscannerfile.tempcloseinputfile;
  2629. begin
  2630. if inputfile.closed or inputfile.is_macro then
  2631. exit;
  2632. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  2633. inputfile.tempclose;
  2634. { reset buffer }
  2635. inputbuffer:=nil;
  2636. inputpointer:=nil;
  2637. inputstart:=0;
  2638. end;
  2639. procedure tscannerfile.saveinputfile;
  2640. begin
  2641. inputfile.saveinputpointer:=inputpointer;
  2642. inputfile.savelastlinepos:=lastlinepos;
  2643. inputfile.saveline_no:=line_no;
  2644. end;
  2645. procedure tscannerfile.restoreinputfile;
  2646. begin
  2647. inputbuffer:=inputfile.buf;
  2648. inputpointer:=inputfile.saveinputpointer;
  2649. lastlinepos:=inputfile.savelastlinepos;
  2650. line_no:=inputfile.saveline_no;
  2651. if not inputfile.is_macro then
  2652. parser_current_file:=inputfile.name;
  2653. end;
  2654. procedure tscannerfile.nextfile;
  2655. var
  2656. to_dispose : tinputfile;
  2657. begin
  2658. if assigned(inputfile.next) then
  2659. begin
  2660. if inputfile.is_macro then
  2661. to_dispose:=inputfile
  2662. else
  2663. begin
  2664. to_dispose:=nil;
  2665. dec(inputfilecount);
  2666. end;
  2667. { we can allways close the file, no ? }
  2668. inputfile.close;
  2669. inputfile:=inputfile.next;
  2670. if assigned(to_dispose) then
  2671. to_dispose.free;
  2672. restoreinputfile;
  2673. end;
  2674. end;
  2675. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  2676. begin
  2677. if not assigned(buf) then
  2678. internalerror(200511172);
  2679. if assigned(recordtokenbuf) then
  2680. internalerror(200511173);
  2681. recordtokenbuf:=buf;
  2682. fillchar(last_settings,sizeof(last_settings),0);
  2683. last_message:=nil;
  2684. fillchar(last_filepos,sizeof(last_filepos),0);
  2685. end;
  2686. procedure tscannerfile.stoprecordtokens;
  2687. begin
  2688. if not assigned(recordtokenbuf) then
  2689. internalerror(200511174);
  2690. recordtokenbuf:=nil;
  2691. end;
  2692. function tscannerfile.is_recording_tokens: boolean;
  2693. begin
  2694. result:=assigned(recordtokenbuf);
  2695. end;
  2696. procedure tscannerfile.writetoken(t : ttoken);
  2697. var
  2698. b : byte;
  2699. begin
  2700. if ord(t)>$7f then
  2701. begin
  2702. b:=(ord(t) shr 8) or $80;
  2703. recordtokenbuf.write(b,1);
  2704. end;
  2705. b:=ord(t) and $ff;
  2706. recordtokenbuf.write(b,1);
  2707. end;
  2708. procedure tscannerfile.tokenwritesizeint(val : asizeint);
  2709. begin
  2710. recordtokenbuf.write(val,sizeof(asizeint));
  2711. end;
  2712. procedure tscannerfile.tokenwritelongint(val : longint);
  2713. begin
  2714. recordtokenbuf.write(val,sizeof(longint));
  2715. end;
  2716. procedure tscannerfile.tokenwriteshortint(val : shortint);
  2717. begin
  2718. recordtokenbuf.write(val,sizeof(shortint));
  2719. end;
  2720. procedure tscannerfile.tokenwriteword(val : word);
  2721. begin
  2722. recordtokenbuf.write(val,sizeof(word));
  2723. end;
  2724. procedure tscannerfile.tokenwritelongword(val : longword);
  2725. begin
  2726. recordtokenbuf.write(val,sizeof(longword));
  2727. end;
  2728. function tscannerfile.tokenreadsizeint : asizeint;
  2729. var
  2730. val : asizeint;
  2731. begin
  2732. replaytokenbuf.read(val,sizeof(asizeint));
  2733. if change_endian_for_replay then
  2734. val:=swapendian(val);
  2735. result:=val;
  2736. end;
  2737. function tscannerfile.tokenreadlongword : longword;
  2738. var
  2739. val : longword;
  2740. begin
  2741. replaytokenbuf.read(val,sizeof(longword));
  2742. if change_endian_for_replay then
  2743. val:=swapendian(val);
  2744. result:=val;
  2745. end;
  2746. function tscannerfile.tokenreadlongint : longint;
  2747. var
  2748. val : longint;
  2749. begin
  2750. replaytokenbuf.read(val,sizeof(longint));
  2751. if change_endian_for_replay then
  2752. val:=swapendian(val);
  2753. result:=val;
  2754. end;
  2755. function tscannerfile.tokenreadshortint : shortint;
  2756. var
  2757. val : shortint;
  2758. begin
  2759. replaytokenbuf.read(val,sizeof(shortint));
  2760. result:=val;
  2761. end;
  2762. function tscannerfile.tokenreadbyte : byte;
  2763. var
  2764. val : byte;
  2765. begin
  2766. replaytokenbuf.read(val,sizeof(byte));
  2767. result:=val;
  2768. end;
  2769. function tscannerfile.tokenreadsmallint : smallint;
  2770. var
  2771. val : smallint;
  2772. begin
  2773. replaytokenbuf.read(val,sizeof(smallint));
  2774. if change_endian_for_replay then
  2775. val:=swapendian(val);
  2776. result:=val;
  2777. end;
  2778. function tscannerfile.tokenreadword : word;
  2779. var
  2780. val : word;
  2781. begin
  2782. replaytokenbuf.read(val,sizeof(word));
  2783. if change_endian_for_replay then
  2784. val:=swapendian(val);
  2785. result:=val;
  2786. end;
  2787. function tscannerfile.tokenreadenum(size : longint) : longword;
  2788. begin
  2789. if size=1 then
  2790. result:=tokenreadbyte
  2791. else if size=2 then
  2792. result:=tokenreadword
  2793. else if size=4 then
  2794. result:=tokenreadlongword
  2795. else
  2796. internalerror(2013112901);
  2797. end;
  2798. procedure tscannerfile.tokenreadset(var b;size : longint);
  2799. var
  2800. i : longint;
  2801. begin
  2802. replaytokenbuf.read(b,size);
  2803. if change_endian_for_replay then
  2804. for i:=0 to size-1 do
  2805. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  2806. end;
  2807. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  2808. begin
  2809. recordtokenbuf.write(b,size);
  2810. end;
  2811. procedure tscannerfile.tokenwriteset(var b;size : longint);
  2812. begin
  2813. recordtokenbuf.write(b,size);
  2814. end;
  2815. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  2816. { This procedure
  2817. needs to be changed whenever
  2818. globals.tsettings type is changed,
  2819. the problem is that no error will appear
  2820. before tests with generics are tested. PM }
  2821. var
  2822. startpos, endpos : longword;
  2823. begin
  2824. { WARNING all those fields need to be in the correct
  2825. order otherwise cross_endian PPU reading will fail }
  2826. startpos:=replaytokenbuf.pos;
  2827. with asettings do
  2828. begin
  2829. alignment.procalign:=tokenreadlongint;
  2830. alignment.loopalign:=tokenreadlongint;
  2831. alignment.jumpalign:=tokenreadlongint;
  2832. alignment.constalignmin:=tokenreadlongint;
  2833. alignment.constalignmax:=tokenreadlongint;
  2834. alignment.varalignmin:=tokenreadlongint;
  2835. alignment.varalignmax:=tokenreadlongint;
  2836. alignment.localalignmin:=tokenreadlongint;
  2837. alignment.localalignmax:=tokenreadlongint;
  2838. alignment.recordalignmin:=tokenreadlongint;
  2839. alignment.recordalignmax:=tokenreadlongint;
  2840. alignment.maxCrecordalign:=tokenreadlongint;
  2841. tokenreadset(globalswitches,sizeof(globalswitches));
  2842. tokenreadset(targetswitches,sizeof(targetswitches));
  2843. tokenreadset(moduleswitches,sizeof(moduleswitches));
  2844. tokenreadset(localswitches,sizeof(localswitches));
  2845. tokenreadset(modeswitches,sizeof(modeswitches));
  2846. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  2847. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2848. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2849. tokenreadset(debugswitches,sizeof(debugswitches));
  2850. { 0: old behaviour for sets <=256 elements
  2851. >0: round to this size }
  2852. setalloc:=tokenreadshortint;
  2853. packenum:=tokenreadshortint;
  2854. packrecords:=tokenreadshortint;
  2855. maxfpuregisters:=tokenreadshortint;
  2856. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2857. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2858. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  2859. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  2860. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  2861. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  2862. { tstringencoding is word type,
  2863. thus this should be OK here }
  2864. sourcecodepage:=tstringEncoding(tokenreadword);
  2865. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  2866. disabledircache:=boolean(tokenreadbyte);
  2867. { TH: Since the field was conditional originally, it was not stored in PPUs. }
  2868. { While adding ControllerSupport constant, I decided not to store ct_none }
  2869. { on targets not supporting controllers, but this might be changed here and }
  2870. { in tokenwritesettings in the future to unify the PPU structure and handling }
  2871. { of this field in the compiler. }
  2872. {$PUSH}
  2873. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  2874. if ControllerSupport then
  2875. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)))
  2876. else
  2877. ControllerType:=ct_none;
  2878. {$POP}
  2879. endpos:=replaytokenbuf.pos;
  2880. if endpos-startpos<>expected_size then
  2881. Comment(V_Error,'Wrong size of Settings read-in');
  2882. end;
  2883. end;
  2884. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  2885. { This procedure
  2886. needs to be changed whenever
  2887. globals.tsettings type is changed,
  2888. the problem is that no error will appear
  2889. before tests with generics are tested. PM }
  2890. var
  2891. sizepos, startpos, endpos : longword;
  2892. begin
  2893. { WARNING all those fields need to be in the correct
  2894. order otherwise cross_endian PPU reading will fail }
  2895. sizepos:=recordtokenbuf.pos;
  2896. size:=0;
  2897. tokenwritesizeint(size);
  2898. startpos:=recordtokenbuf.pos;
  2899. with asettings do
  2900. begin
  2901. tokenwritelongint(alignment.procalign);
  2902. tokenwritelongint(alignment.loopalign);
  2903. tokenwritelongint(alignment.jumpalign);
  2904. tokenwritelongint(alignment.constalignmin);
  2905. tokenwritelongint(alignment.constalignmax);
  2906. tokenwritelongint(alignment.varalignmin);
  2907. tokenwritelongint(alignment.varalignmax);
  2908. tokenwritelongint(alignment.localalignmin);
  2909. tokenwritelongint(alignment.localalignmax);
  2910. tokenwritelongint(alignment.recordalignmin);
  2911. tokenwritelongint(alignment.recordalignmax);
  2912. tokenwritelongint(alignment.maxCrecordalign);
  2913. tokenwriteset(globalswitches,sizeof(globalswitches));
  2914. tokenwriteset(targetswitches,sizeof(targetswitches));
  2915. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  2916. tokenwriteset(localswitches,sizeof(localswitches));
  2917. tokenwriteset(modeswitches,sizeof(modeswitches));
  2918. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  2919. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2920. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2921. tokenwriteset(debugswitches,sizeof(debugswitches));
  2922. { 0: old behaviour for sets <=256 elements
  2923. >0: round to this size }
  2924. tokenwriteshortint(setalloc);
  2925. tokenwriteshortint(packenum);
  2926. tokenwriteshortint(packrecords);
  2927. tokenwriteshortint(maxfpuregisters);
  2928. tokenwriteenum(cputype,sizeof(tcputype));
  2929. tokenwriteenum(optimizecputype,sizeof(tcputype));
  2930. tokenwriteenum(fputype,sizeof(tfputype));
  2931. tokenwriteenum(asmmode,sizeof(tasmmode));
  2932. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  2933. tokenwriteenum(defproccall,sizeof(tproccalloption));
  2934. { tstringencoding is word type,
  2935. thus this should be OK here }
  2936. tokenwriteword(sourcecodepage);
  2937. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  2938. recordtokenbuf.write(byte(disabledircache),1);
  2939. { TH: See note about controllertype field in tokenreadsettings. }
  2940. {$PUSH}
  2941. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  2942. if ControllerSupport then
  2943. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  2944. {$POP}
  2945. endpos:=recordtokenbuf.pos;
  2946. size:=endpos-startpos;
  2947. recordtokenbuf.seek(sizepos);
  2948. tokenwritesizeint(size);
  2949. recordtokenbuf.seek(endpos);
  2950. end;
  2951. end;
  2952. procedure tscannerfile.recordtoken;
  2953. var
  2954. t : ttoken;
  2955. s : tspecialgenerictoken;
  2956. len,msgnb,copy_size : asizeint;
  2957. val : longint;
  2958. b : byte;
  2959. pmsg : pmessagestaterecord;
  2960. begin
  2961. if not assigned(recordtokenbuf) then
  2962. internalerror(200511176);
  2963. t:=_GENERICSPECIALTOKEN;
  2964. { settings changed? }
  2965. { last field pmessage is handled separately below in
  2966. ST_LOADMESSAGES }
  2967. if CompareByte(current_settings,last_settings,
  2968. sizeof(current_settings)-sizeof(pointer))<>0 then
  2969. begin
  2970. { use a special token to record it }
  2971. s:=ST_LOADSETTINGS;
  2972. writetoken(t);
  2973. recordtokenbuf.write(s,1);
  2974. copy_size:=sizeof(current_settings)-sizeof(pointer);
  2975. tokenwritesettings(current_settings,copy_size);
  2976. last_settings:=current_settings;
  2977. end;
  2978. if current_settings.pmessage<>last_message then
  2979. begin
  2980. { use a special token to record it }
  2981. s:=ST_LOADMESSAGES;
  2982. writetoken(t);
  2983. recordtokenbuf.write(s,1);
  2984. msgnb:=0;
  2985. pmsg:=current_settings.pmessage;
  2986. while assigned(pmsg) do
  2987. begin
  2988. if msgnb=high(asizeint) then
  2989. { Too many messages }
  2990. internalerror(2011090401);
  2991. inc(msgnb);
  2992. pmsg:=pmsg^.next;
  2993. end;
  2994. tokenwritesizeint(msgnb);
  2995. pmsg:=current_settings.pmessage;
  2996. while assigned(pmsg) do
  2997. begin
  2998. { What about endianess here?}
  2999. { SB: this is handled by tokenreadlongint }
  3000. val:=pmsg^.value;
  3001. tokenwritelongint(val);
  3002. val:=ord(pmsg^.state);
  3003. tokenwritelongint(val);
  3004. pmsg:=pmsg^.next;
  3005. end;
  3006. last_message:=current_settings.pmessage;
  3007. end;
  3008. { file pos changes? }
  3009. if current_tokenpos.line<>last_filepos.line then
  3010. begin
  3011. s:=ST_LINE;
  3012. writetoken(t);
  3013. recordtokenbuf.write(s,1);
  3014. tokenwritelongint(current_tokenpos.line);
  3015. last_filepos.line:=current_tokenpos.line;
  3016. end;
  3017. if current_tokenpos.column<>last_filepos.column then
  3018. begin
  3019. s:=ST_COLUMN;
  3020. writetoken(t);
  3021. { can the column be written packed? }
  3022. if current_tokenpos.column<$80 then
  3023. begin
  3024. b:=$80 or current_tokenpos.column;
  3025. recordtokenbuf.write(b,1);
  3026. end
  3027. else
  3028. begin
  3029. recordtokenbuf.write(s,1);
  3030. tokenwriteword(current_tokenpos.column);
  3031. end;
  3032. last_filepos.column:=current_tokenpos.column;
  3033. end;
  3034. if current_tokenpos.fileindex<>last_filepos.fileindex then
  3035. begin
  3036. s:=ST_FILEINDEX;
  3037. writetoken(t);
  3038. recordtokenbuf.write(s,1);
  3039. tokenwriteword(current_tokenpos.fileindex);
  3040. last_filepos.fileindex:=current_tokenpos.fileindex;
  3041. end;
  3042. writetoken(token);
  3043. if token<>_GENERICSPECIALTOKEN then
  3044. writetoken(idtoken);
  3045. case token of
  3046. _CWCHAR,
  3047. _CWSTRING :
  3048. begin
  3049. tokenwritesizeint(patternw^.len);
  3050. if patternw^.len>0 then
  3051. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3052. end;
  3053. _CSTRING:
  3054. begin
  3055. len:=length(cstringpattern);
  3056. tokenwritesizeint(len);
  3057. if len>0 then
  3058. recordtokenbuf.write(cstringpattern[1],len);
  3059. end;
  3060. _CCHAR,
  3061. _INTCONST,
  3062. _REALNUMBER :
  3063. begin
  3064. { pexpr.pas messes with pattern in case of negative integer consts,
  3065. see around line 2562 the comment of JM; remove the - before recording it
  3066. (FK)
  3067. }
  3068. if (token=_INTCONST) and (pattern[1]='-') then
  3069. delete(pattern,1,1);
  3070. recordtokenbuf.write(pattern[0],1);
  3071. recordtokenbuf.write(pattern[1],length(pattern));
  3072. end;
  3073. _ID :
  3074. begin
  3075. recordtokenbuf.write(orgpattern[0],1);
  3076. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  3077. end;
  3078. end;
  3079. end;
  3080. procedure tscannerfile.startreplaytokens(buf:tdynamicarray; change_endian:boolean);
  3081. begin
  3082. if not assigned(buf) then
  3083. internalerror(200511175);
  3084. { save current scanner state }
  3085. replaystack:=treplaystack.create(token,idtoken,orgpattern,pattern,
  3086. cstringpattern,patternw,current_settings,replaytokenbuf,change_endian_for_replay,replaystack);
  3087. if assigned(inputpointer) then
  3088. dec(inputpointer);
  3089. { install buffer }
  3090. replaytokenbuf:=buf;
  3091. { Initialize value of change_endian_for_replay variable }
  3092. change_endian_for_replay:=change_endian;
  3093. { reload next token }
  3094. replaytokenbuf.seek(0);
  3095. replaytoken;
  3096. end;
  3097. function tscannerfile.readtoken: ttoken;
  3098. var
  3099. b,b2 : byte;
  3100. begin
  3101. replaytokenbuf.read(b,1);
  3102. if (b and $80)<>0 then
  3103. begin
  3104. replaytokenbuf.read(b2,1);
  3105. result:=ttoken(((b and $7f) shl 8) or b2);
  3106. end
  3107. else
  3108. result:=ttoken(b);
  3109. end;
  3110. procedure tscannerfile.replaytoken;
  3111. var
  3112. wlen,mesgnb,copy_size : asizeint;
  3113. specialtoken : tspecialgenerictoken;
  3114. i : byte;
  3115. pmsg,prevmsg : pmessagestaterecord;
  3116. begin
  3117. if not assigned(replaytokenbuf) then
  3118. internalerror(200511177);
  3119. { End of replay buffer? Then load the next char from the file again }
  3120. if replaytokenbuf.pos>=replaytokenbuf.size then
  3121. begin
  3122. token:=replaystack.token;
  3123. idtoken:=replaystack.idtoken;
  3124. pattern:=replaystack.pattern;
  3125. orgpattern:=replaystack.orgpattern;
  3126. setlengthwidestring(patternw,replaystack.patternw^.len);
  3127. move(replaystack.patternw^.data^,patternw^.data^,replaystack.patternw^.len*sizeof(tcompilerwidechar));
  3128. cstringpattern:=replaystack.cstringpattern;
  3129. replaytokenbuf:=replaystack.tokenbuf;
  3130. change_endian_for_replay:=replaystack.tokenbuf_needs_swapping;
  3131. { restore compiler settings }
  3132. current_settings:=replaystack.settings;
  3133. popreplaystack;
  3134. if assigned(inputpointer) then
  3135. begin
  3136. c:=inputpointer^;
  3137. inc(inputpointer);
  3138. end;
  3139. exit;
  3140. end;
  3141. repeat
  3142. { load token from the buffer }
  3143. token:=readtoken;
  3144. if token<>_GENERICSPECIALTOKEN then
  3145. idtoken:=readtoken
  3146. else
  3147. idtoken:=_NOID;
  3148. case token of
  3149. _CWCHAR,
  3150. _CWSTRING :
  3151. begin
  3152. wlen:=tokenreadsizeint;
  3153. setlengthwidestring(patternw,wlen);
  3154. if wlen>0 then
  3155. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3156. orgpattern:='';
  3157. pattern:='';
  3158. cstringpattern:='';
  3159. end;
  3160. _CSTRING:
  3161. begin
  3162. wlen:=tokenreadsizeint;
  3163. if wlen>0 then
  3164. begin
  3165. setlength(cstringpattern,wlen);
  3166. replaytokenbuf.read(cstringpattern[1],wlen);
  3167. end
  3168. else
  3169. cstringpattern:='';
  3170. orgpattern:='';
  3171. pattern:='';
  3172. end;
  3173. _CCHAR,
  3174. _INTCONST,
  3175. _REALNUMBER :
  3176. begin
  3177. replaytokenbuf.read(pattern[0],1);
  3178. replaytokenbuf.read(pattern[1],length(pattern));
  3179. orgpattern:='';
  3180. end;
  3181. _ID :
  3182. begin
  3183. replaytokenbuf.read(orgpattern[0],1);
  3184. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  3185. pattern:=upper(orgpattern);
  3186. end;
  3187. _GENERICSPECIALTOKEN:
  3188. begin
  3189. replaytokenbuf.read(specialtoken,1);
  3190. { packed column? }
  3191. if (ord(specialtoken) and $80)<>0 then
  3192. begin
  3193. current_tokenpos.column:=ord(specialtoken) and $7f;
  3194. current_filepos:=current_tokenpos;
  3195. end
  3196. else
  3197. case specialtoken of
  3198. ST_LOADSETTINGS:
  3199. begin
  3200. copy_size:=tokenreadsizeint;
  3201. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  3202. // internalerror(2011090501);
  3203. {
  3204. replaytokenbuf.read(current_settings,copy_size);
  3205. }
  3206. tokenreadsettings(current_settings,copy_size);
  3207. end;
  3208. ST_LOADMESSAGES:
  3209. begin
  3210. current_settings.pmessage:=nil;
  3211. mesgnb:=tokenreadsizeint;
  3212. prevmsg:=nil;
  3213. for i:=1 to mesgnb do
  3214. begin
  3215. new(pmsg);
  3216. if i=1 then
  3217. current_settings.pmessage:=pmsg
  3218. else
  3219. prevmsg^.next:=pmsg;
  3220. pmsg^.value:=tokenreadlongint;
  3221. pmsg^.state:=tmsgstate(tokenreadlongint);
  3222. pmsg^.next:=nil;
  3223. prevmsg:=pmsg;
  3224. end;
  3225. end;
  3226. ST_LINE:
  3227. begin
  3228. current_tokenpos.line:=tokenreadlongint;
  3229. current_filepos:=current_tokenpos;
  3230. end;
  3231. ST_COLUMN:
  3232. begin
  3233. current_tokenpos.column:=tokenreadword;
  3234. current_filepos:=current_tokenpos;
  3235. end;
  3236. ST_FILEINDEX:
  3237. begin
  3238. current_tokenpos.fileindex:=tokenreadword;
  3239. current_filepos:=current_tokenpos;
  3240. end;
  3241. else
  3242. internalerror(2006103010);
  3243. end;
  3244. continue;
  3245. end;
  3246. end;
  3247. break;
  3248. until false;
  3249. end;
  3250. procedure tscannerfile.addfile(hp:tinputfile);
  3251. begin
  3252. saveinputfile;
  3253. { add to list }
  3254. hp.next:=inputfile;
  3255. inputfile:=hp;
  3256. { load new inputfile }
  3257. restoreinputfile;
  3258. end;
  3259. procedure tscannerfile.reload;
  3260. begin
  3261. with inputfile do
  3262. begin
  3263. { when nothing more to read then leave immediatly, so we
  3264. don't change the current_filepos and leave it point to the last
  3265. char }
  3266. if (c=#26) and (not assigned(next)) then
  3267. exit;
  3268. repeat
  3269. { still more to read?, then change the #0 to a space so its seen
  3270. as a seperator, this can't be used for macro's which can change
  3271. the place of the #0 in the buffer with tempopen }
  3272. if (c=#0) and (bufsize>0) and
  3273. not(inputfile.is_macro) and
  3274. (inputpointer-inputbuffer<bufsize) then
  3275. begin
  3276. c:=' ';
  3277. inc(inputpointer);
  3278. exit;
  3279. end;
  3280. { can we read more from this file ? }
  3281. if (c<>#26) and (not endoffile) then
  3282. begin
  3283. readbuf;
  3284. inputpointer:=buf;
  3285. inputbuffer:=buf;
  3286. inputstart:=bufstart;
  3287. { first line? }
  3288. if line_no=0 then
  3289. begin
  3290. c:=inputpointer^;
  3291. { eat utf-8 signature? }
  3292. if (ord(inputpointer^)=$ef) and
  3293. (ord((inputpointer+1)^)=$bb) and
  3294. (ord((inputpointer+2)^)=$bf) then
  3295. begin
  3296. (* we don't support including files with an UTF-8 bom
  3297. inside another file that wasn't encoded as UTF-8
  3298. already (we don't support {$codepage xxx} switches in
  3299. the middle of a file either) *)
  3300. if (current_settings.sourcecodepage<>CP_UTF8) and
  3301. not current_module.in_global then
  3302. Message(scanner_f_illegal_utf8_bom);
  3303. inc(inputpointer,3);
  3304. message(scan_c_switching_to_utf8);
  3305. current_settings.sourcecodepage:=CP_UTF8;
  3306. exclude(current_settings.moduleswitches,cs_system_codepage);
  3307. include(current_settings.moduleswitches,cs_explicit_codepage);
  3308. end;
  3309. line_no:=1;
  3310. if cs_asm_source in current_settings.globalswitches then
  3311. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3312. end;
  3313. end
  3314. else
  3315. begin
  3316. { load eof position in tokenpos/current_filepos }
  3317. gettokenpos;
  3318. { close file }
  3319. closeinputfile;
  3320. { no next module, than EOF }
  3321. if not assigned(inputfile.next) then
  3322. begin
  3323. c:=#26;
  3324. exit;
  3325. end;
  3326. { load next file and reopen it }
  3327. nextfile;
  3328. tempopeninputfile;
  3329. { status }
  3330. Message1(scan_t_back_in,inputfile.name);
  3331. end;
  3332. { load next char }
  3333. c:=inputpointer^;
  3334. inc(inputpointer);
  3335. until c<>#0; { if also end, then reload again }
  3336. end;
  3337. end;
  3338. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  3339. var
  3340. hp : tinputfile;
  3341. begin
  3342. { save old postion }
  3343. dec(inputpointer);
  3344. tempcloseinputfile;
  3345. { create macro 'file' }
  3346. { use special name to dispose after !! }
  3347. hp:=do_openinputfile('_Macro_.'+macname);
  3348. addfile(hp);
  3349. with inputfile do
  3350. begin
  3351. setmacro(p,len);
  3352. { local buffer }
  3353. inputbuffer:=buf;
  3354. inputpointer:=buf;
  3355. inputstart:=bufstart;
  3356. ref_index:=fileindex;
  3357. end;
  3358. { reset line }
  3359. line_no:=line;
  3360. lastlinepos:=0;
  3361. lasttokenpos:=0;
  3362. nexttokenpos:=0;
  3363. { load new c }
  3364. c:=inputpointer^;
  3365. inc(inputpointer);
  3366. end;
  3367. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3368. begin
  3369. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3370. filepos.line:=line_no;
  3371. filepos.column:=tokenpos-lastlinepos;
  3372. filepos.fileindex:=inputfile.ref_index;
  3373. filepos.moduleindex:=current_module.unit_index;
  3374. end;
  3375. procedure tscannerfile.gettokenpos;
  3376. { load the values of tokenpos and lasttokenpos }
  3377. begin
  3378. do_gettokenpos(lasttokenpos,current_tokenpos);
  3379. current_filepos:=current_tokenpos;
  3380. end;
  3381. procedure tscannerfile.cachenexttokenpos;
  3382. begin
  3383. do_gettokenpos(nexttokenpos,next_filepos);
  3384. end;
  3385. procedure tscannerfile.setnexttoken;
  3386. begin
  3387. token:=nexttoken;
  3388. nexttoken:=NOTOKEN;
  3389. lasttokenpos:=nexttokenpos;
  3390. current_tokenpos:=next_filepos;
  3391. current_filepos:=current_tokenpos;
  3392. nexttokenpos:=0;
  3393. end;
  3394. procedure tscannerfile.savetokenpos;
  3395. begin
  3396. oldlasttokenpos:=lasttokenpos;
  3397. oldcurrent_filepos:=current_filepos;
  3398. oldcurrent_tokenpos:=current_tokenpos;
  3399. end;
  3400. procedure tscannerfile.restoretokenpos;
  3401. begin
  3402. lasttokenpos:=oldlasttokenpos;
  3403. current_filepos:=oldcurrent_filepos;
  3404. current_tokenpos:=oldcurrent_tokenpos;
  3405. end;
  3406. procedure tscannerfile.inc_comment_level;
  3407. begin
  3408. if (m_nested_comment in current_settings.modeswitches) then
  3409. inc(comment_level)
  3410. else
  3411. comment_level:=1;
  3412. if (comment_level>1) then
  3413. begin
  3414. savetokenpos;
  3415. gettokenpos; { update for warning }
  3416. Message1(scan_w_comment_level,tostr(comment_level));
  3417. restoretokenpos;
  3418. end;
  3419. end;
  3420. procedure tscannerfile.dec_comment_level;
  3421. begin
  3422. if (m_nested_comment in current_settings.modeswitches) then
  3423. dec(comment_level)
  3424. else
  3425. comment_level:=0;
  3426. end;
  3427. procedure tscannerfile.linebreak;
  3428. var
  3429. cur : char;
  3430. begin
  3431. with inputfile do
  3432. begin
  3433. if (byte(inputpointer^)=0) and not(endoffile) then
  3434. begin
  3435. cur:=c;
  3436. reload;
  3437. if byte(cur)+byte(c)<>23 then
  3438. dec(inputpointer);
  3439. end
  3440. else
  3441. begin
  3442. { Support all combination of #10 and #13 as line break }
  3443. if (byte(inputpointer^)+byte(c)=23) then
  3444. inc(inputpointer);
  3445. end;
  3446. { Always return #10 as line break }
  3447. c:=#10;
  3448. { increase line counters }
  3449. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3450. inc(line_no);
  3451. { update linebuffer }
  3452. if cs_asm_source in current_settings.globalswitches then
  3453. inputfile.setline(line_no,lastlinepos);
  3454. { update for status and call the show status routine,
  3455. but don't touch current_filepos ! }
  3456. savetokenpos;
  3457. gettokenpos; { update for v_status }
  3458. inc(status.compiledlines);
  3459. ShowStatus;
  3460. restoretokenpos;
  3461. end;
  3462. end;
  3463. procedure tscannerfile.illegal_char(c:char);
  3464. var
  3465. s : string;
  3466. begin
  3467. if c in [#32..#255] then
  3468. s:=''''+c+''''
  3469. else
  3470. s:='#'+tostr(ord(c));
  3471. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3472. end;
  3473. procedure tscannerfile.end_of_file;
  3474. begin
  3475. checkpreprocstack;
  3476. Message(scan_f_end_of_file);
  3477. end;
  3478. {-------------------------------------------
  3479. IF Conditional Handling
  3480. -------------------------------------------}
  3481. procedure tscannerfile.checkpreprocstack;
  3482. begin
  3483. { check for missing ifdefs }
  3484. while assigned(preprocstack) do
  3485. begin
  3486. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3487. current_module.sourcefiles.get_file_name(preprocstack.fileindex),
  3488. tostr(preprocstack.line_nb));
  3489. poppreprocstack;
  3490. end;
  3491. end;
  3492. procedure tscannerfile.poppreprocstack;
  3493. var
  3494. hp : tpreprocstack;
  3495. begin
  3496. if assigned(preprocstack) then
  3497. begin
  3498. Message1(scan_c_endif_found,preprocstack.name);
  3499. hp:=preprocstack.next;
  3500. preprocstack.free;
  3501. preprocstack:=hp;
  3502. end
  3503. else
  3504. Message(scan_e_endif_without_if);
  3505. end;
  3506. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  3507. var
  3508. condition: Boolean;
  3509. valuedescr: String;
  3510. begin
  3511. if (preprocstack=nil) or preprocstack.accept then
  3512. condition:=compile_time_predicate(valuedescr)
  3513. else
  3514. begin
  3515. condition:= false;
  3516. valuedescr:= '';
  3517. end;
  3518. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  3519. preprocstack.name:=valuedescr;
  3520. preprocstack.line_nb:=line_no;
  3521. preprocstack.fileindex:=current_filepos.fileindex;
  3522. if preprocstack.accept then
  3523. Message2(messid,preprocstack.name,'accepted')
  3524. else
  3525. Message2(messid,preprocstack.name,'rejected');
  3526. end;
  3527. procedure tscannerfile.elsepreprocstack;
  3528. begin
  3529. if assigned(preprocstack) and
  3530. (preprocstack.typ<>pp_else) then
  3531. begin
  3532. if (preprocstack.typ=pp_elseif) then
  3533. preprocstack.accept:=false
  3534. else
  3535. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  3536. preprocstack.accept:=not preprocstack.accept;
  3537. preprocstack.typ:=pp_else;
  3538. preprocstack.line_nb:=line_no;
  3539. preprocstack.fileindex:=current_filepos.fileindex;
  3540. if preprocstack.accept then
  3541. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3542. else
  3543. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3544. end
  3545. else
  3546. Message(scan_e_endif_without_if);
  3547. end;
  3548. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  3549. var
  3550. valuedescr: String;
  3551. begin
  3552. if assigned(preprocstack) and
  3553. (preprocstack.typ in [pp_if,pp_elseif]) then
  3554. begin
  3555. { when the branch is accepted we use pp_elseif so we know that
  3556. all the next branches need to be rejected. when this branch is still
  3557. not accepted then leave it at pp_if }
  3558. if (preprocstack.typ=pp_elseif) then
  3559. preprocstack.accept:=false
  3560. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  3561. begin
  3562. preprocstack.accept:=false;
  3563. preprocstack.typ:=pp_elseif;
  3564. end
  3565. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  3566. and compile_time_predicate(valuedescr) then
  3567. begin
  3568. preprocstack.name:=valuedescr;
  3569. preprocstack.accept:=true;
  3570. preprocstack.typ:=pp_elseif;
  3571. end;
  3572. preprocstack.line_nb:=line_no;
  3573. preprocstack.fileindex:=current_filepos.fileindex;
  3574. if preprocstack.accept then
  3575. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3576. else
  3577. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3578. end
  3579. else
  3580. Message(scan_e_endif_without_if);
  3581. end;
  3582. procedure tscannerfile.popreplaystack;
  3583. var
  3584. hp : treplaystack;
  3585. begin
  3586. if assigned(replaystack) then
  3587. begin
  3588. hp:=replaystack.next;
  3589. replaystack.free;
  3590. replaystack:=hp;
  3591. end;
  3592. end;
  3593. function tscannerfile.replay_stack_depth:longint;
  3594. var
  3595. tmp: treplaystack;
  3596. begin
  3597. result:=0;
  3598. tmp:=replaystack;
  3599. while assigned(tmp) do
  3600. begin
  3601. inc(result);
  3602. tmp:=tmp.next;
  3603. end;
  3604. end;
  3605. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  3606. begin
  3607. savetokenpos;
  3608. repeat
  3609. current_scanner.gettokenpos;
  3610. Message1(scan_d_handling_switch,'$'+p.name);
  3611. p.proc();
  3612. { accept the text ? }
  3613. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  3614. break
  3615. else
  3616. begin
  3617. current_scanner.gettokenpos;
  3618. Message(scan_c_skipping_until);
  3619. repeat
  3620. current_scanner.skipuntildirective;
  3621. if not (m_mac in current_settings.modeswitches) then
  3622. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  3623. else
  3624. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  3625. until assigned(p) and (p.is_conditional);
  3626. current_scanner.gettokenpos;
  3627. end;
  3628. until false;
  3629. restoretokenpos;
  3630. end;
  3631. procedure tscannerfile.handledirectives;
  3632. var
  3633. t : tdirectiveitem;
  3634. hs : string;
  3635. begin
  3636. gettokenpos;
  3637. readchar; {Remove the $}
  3638. hs:=readid;
  3639. { handle empty directive }
  3640. if hs='' then
  3641. begin
  3642. Message1(scan_w_illegal_switch,'$');
  3643. exit;
  3644. end;
  3645. {$ifdef PREPROCWRITE}
  3646. if parapreprocess then
  3647. begin
  3648. if not (m_mac in current_settings.modeswitches) then
  3649. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  3650. else
  3651. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  3652. if assigned(t) and not(t.is_conditional) then
  3653. begin
  3654. preprocfile.AddSpace;
  3655. preprocfile.Add('{$'+hs+current_scanner.readcomment+'}');
  3656. exit;
  3657. end;
  3658. end;
  3659. {$endif PREPROCWRITE}
  3660. { skip this directive? }
  3661. if (ignoredirectives.find(hs)<>nil) then
  3662. begin
  3663. if (comment_level>0) then
  3664. readcomment;
  3665. { we've read the whole comment }
  3666. current_commentstyle:=comment_none;
  3667. exit;
  3668. end;
  3669. { Check for compiler switches }
  3670. while (length(hs)=1) and (c in ['-','+']) do
  3671. begin
  3672. Message1(scan_d_handling_switch,'$'+hs+c);
  3673. HandleSwitch(hs[1],c);
  3674. current_scanner.readchar; {Remove + or -}
  3675. if c=',' then
  3676. begin
  3677. current_scanner.readchar; {Remove , }
  3678. { read next switch, support $v+,$+}
  3679. hs:=current_scanner.readid;
  3680. if (hs='') then
  3681. begin
  3682. if (c='$') and (m_fpc in current_settings.modeswitches) then
  3683. begin
  3684. current_scanner.readchar; { skip $ }
  3685. hs:=current_scanner.readid;
  3686. end;
  3687. if (hs='') then
  3688. Message1(scan_w_illegal_directive,'$'+c);
  3689. end;
  3690. end
  3691. else
  3692. hs:='';
  3693. end;
  3694. { directives may follow switches after a , }
  3695. if hs<>'' then
  3696. begin
  3697. if not (m_mac in current_settings.modeswitches) then
  3698. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  3699. else
  3700. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  3701. if assigned(t) then
  3702. begin
  3703. if t.is_conditional then
  3704. handleconditional(t)
  3705. else
  3706. begin
  3707. Message1(scan_d_handling_switch,'$'+hs);
  3708. t.proc();
  3709. end;
  3710. end
  3711. else
  3712. begin
  3713. current_scanner.ignoredirectives.Add(hs,nil);
  3714. Message1(scan_w_illegal_directive,'$'+hs);
  3715. end;
  3716. { conditionals already read the comment }
  3717. if (current_scanner.comment_level>0) then
  3718. current_scanner.readcomment;
  3719. { we've read the whole comment }
  3720. current_commentstyle:=comment_none;
  3721. end;
  3722. end;
  3723. procedure tscannerfile.readchar;
  3724. begin
  3725. c:=inputpointer^;
  3726. if c=#0 then
  3727. reload
  3728. else
  3729. inc(inputpointer);
  3730. end;
  3731. procedure tscannerfile.readstring;
  3732. var
  3733. i : longint;
  3734. err : boolean;
  3735. begin
  3736. err:=false;
  3737. i:=0;
  3738. repeat
  3739. case c of
  3740. '_',
  3741. '0'..'9',
  3742. 'A'..'Z' :
  3743. begin
  3744. if i<255 then
  3745. begin
  3746. inc(i);
  3747. orgpattern[i]:=c;
  3748. pattern[i]:=c;
  3749. end
  3750. else
  3751. begin
  3752. if not err then
  3753. begin
  3754. Message(scan_e_string_exceeds_255_chars);
  3755. err:=true;
  3756. end;
  3757. end;
  3758. c:=inputpointer^;
  3759. inc(inputpointer);
  3760. end;
  3761. 'a'..'z' :
  3762. begin
  3763. if i<255 then
  3764. begin
  3765. inc(i);
  3766. orgpattern[i]:=c;
  3767. pattern[i]:=chr(ord(c)-32)
  3768. end
  3769. else
  3770. begin
  3771. if not err then
  3772. begin
  3773. Message(scan_e_string_exceeds_255_chars);
  3774. err:=true;
  3775. end;
  3776. end;
  3777. c:=inputpointer^;
  3778. inc(inputpointer);
  3779. end;
  3780. #0 :
  3781. reload;
  3782. else
  3783. break;
  3784. end;
  3785. until false;
  3786. orgpattern[0]:=chr(i);
  3787. pattern[0]:=chr(i);
  3788. end;
  3789. procedure tscannerfile.readnumber;
  3790. var
  3791. base,
  3792. i : longint;
  3793. begin
  3794. case c of
  3795. '%' :
  3796. begin
  3797. readchar;
  3798. base:=2;
  3799. pattern[1]:='%';
  3800. i:=1;
  3801. end;
  3802. '&' :
  3803. begin
  3804. readchar;
  3805. base:=8;
  3806. pattern[1]:='&';
  3807. i:=1;
  3808. end;
  3809. '$' :
  3810. begin
  3811. readchar;
  3812. base:=16;
  3813. pattern[1]:='$';
  3814. i:=1;
  3815. end;
  3816. else
  3817. begin
  3818. base:=10;
  3819. i:=0;
  3820. end;
  3821. end;
  3822. while ((base>=10) and (c in ['0'..'9'])) or
  3823. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  3824. ((base=8) and (c in ['0'..'7'])) or
  3825. ((base=2) and (c in ['0'..'1'])) do
  3826. begin
  3827. if i<255 then
  3828. begin
  3829. inc(i);
  3830. pattern[i]:=c;
  3831. end;
  3832. readchar;
  3833. end;
  3834. pattern[0]:=chr(i);
  3835. end;
  3836. function tscannerfile.readid:string;
  3837. begin
  3838. readstring;
  3839. readid:=pattern;
  3840. end;
  3841. function tscannerfile.readval:longint;
  3842. var
  3843. l : longint;
  3844. w : integer;
  3845. begin
  3846. readnumber;
  3847. val(pattern,l,w);
  3848. readval:=l;
  3849. end;
  3850. function tscannerfile.readcomment:string;
  3851. var
  3852. i : longint;
  3853. begin
  3854. i:=0;
  3855. repeat
  3856. case c of
  3857. '{' :
  3858. begin
  3859. if current_commentstyle=comment_tp then
  3860. inc_comment_level;
  3861. end;
  3862. '}' :
  3863. begin
  3864. if current_commentstyle=comment_tp then
  3865. begin
  3866. readchar;
  3867. dec_comment_level;
  3868. if comment_level=0 then
  3869. break
  3870. else
  3871. continue;
  3872. end;
  3873. end;
  3874. '*' :
  3875. begin
  3876. if current_commentstyle=comment_oldtp then
  3877. begin
  3878. readchar;
  3879. if c=')' then
  3880. begin
  3881. readchar;
  3882. dec_comment_level;
  3883. break;
  3884. end
  3885. else
  3886. { Add both characters !!}
  3887. if (i<255) then
  3888. begin
  3889. inc(i);
  3890. readcomment[i]:='*';
  3891. if (i<255) then
  3892. begin
  3893. inc(i);
  3894. readcomment[i]:=c;
  3895. end;
  3896. end;
  3897. end
  3898. else
  3899. { Not old TP comment, so add...}
  3900. begin
  3901. if (i<255) then
  3902. begin
  3903. inc(i);
  3904. readcomment[i]:='*';
  3905. end;
  3906. end;
  3907. end;
  3908. #10,#13 :
  3909. linebreak;
  3910. #26 :
  3911. end_of_file;
  3912. else
  3913. begin
  3914. if (i<255) then
  3915. begin
  3916. inc(i);
  3917. readcomment[i]:=c;
  3918. end;
  3919. end;
  3920. end;
  3921. readchar;
  3922. until false;
  3923. readcomment[0]:=chr(i);
  3924. end;
  3925. function tscannerfile.readquotedstring:string;
  3926. var
  3927. i : longint;
  3928. msgwritten : boolean;
  3929. begin
  3930. i:=0;
  3931. msgwritten:=false;
  3932. if (c='''') then
  3933. begin
  3934. repeat
  3935. readchar;
  3936. case c of
  3937. #26 :
  3938. end_of_file;
  3939. #10,#13 :
  3940. Message(scan_f_string_exceeds_line);
  3941. '''' :
  3942. begin
  3943. readchar;
  3944. if c<>'''' then
  3945. break;
  3946. end;
  3947. end;
  3948. if i<255 then
  3949. begin
  3950. inc(i);
  3951. result[i]:=c;
  3952. end
  3953. else
  3954. begin
  3955. if not msgwritten then
  3956. begin
  3957. Message(scan_e_string_exceeds_255_chars);
  3958. msgwritten:=true;
  3959. end;
  3960. end;
  3961. until false;
  3962. end;
  3963. result[0]:=chr(i);
  3964. end;
  3965. function tscannerfile.readstate:char;
  3966. var
  3967. state : char;
  3968. begin
  3969. state:=' ';
  3970. if c=' ' then
  3971. begin
  3972. current_scanner.skipspace;
  3973. current_scanner.readid;
  3974. if pattern='ON' then
  3975. state:='+'
  3976. else
  3977. if pattern='OFF' then
  3978. state:='-';
  3979. end
  3980. else
  3981. state:=c;
  3982. if not (state in ['+','-']) then
  3983. Message(scan_e_wrong_switch_toggle);
  3984. readstate:=state;
  3985. end;
  3986. function tscannerfile.readoptionalstate(fallback:char):char;
  3987. var
  3988. state : char;
  3989. begin
  3990. state:=' ';
  3991. if c=' ' then
  3992. begin
  3993. current_scanner.skipspace;
  3994. if c in ['*','}'] then
  3995. state:=fallback
  3996. else
  3997. begin
  3998. current_scanner.readid;
  3999. if pattern='ON' then
  4000. state:='+'
  4001. else
  4002. if pattern='OFF' then
  4003. state:='-';
  4004. end;
  4005. end
  4006. else
  4007. if c in ['*','}'] then
  4008. state:=fallback
  4009. else
  4010. state:=c;
  4011. if not (state in ['+','-']) then
  4012. Message(scan_e_wrong_switch_toggle);
  4013. readoptionalstate:=state;
  4014. end;
  4015. function tscannerfile.readstatedefault:char;
  4016. var
  4017. state : char;
  4018. begin
  4019. state:=' ';
  4020. if c=' ' then
  4021. begin
  4022. current_scanner.skipspace;
  4023. current_scanner.readid;
  4024. if pattern='ON' then
  4025. state:='+'
  4026. else
  4027. if pattern='OFF' then
  4028. state:='-'
  4029. else
  4030. if pattern='DEFAULT' then
  4031. state:='*';
  4032. end
  4033. else
  4034. state:=c;
  4035. if not (state in ['+','-','*']) then
  4036. Message(scan_e_wrong_switch_toggle_default);
  4037. readstatedefault:=state;
  4038. end;
  4039. procedure tscannerfile.skipspace;
  4040. begin
  4041. repeat
  4042. case c of
  4043. #26 :
  4044. begin
  4045. reload;
  4046. if (c=#26) and not assigned(inputfile.next) then
  4047. break;
  4048. continue;
  4049. end;
  4050. #10,
  4051. #13 :
  4052. linebreak;
  4053. #9,#11,#12,' ' :
  4054. ;
  4055. else
  4056. break;
  4057. end;
  4058. readchar;
  4059. until false;
  4060. end;
  4061. procedure tscannerfile.skipuntildirective;
  4062. var
  4063. found : longint;
  4064. next_char_loaded : boolean;
  4065. begin
  4066. found:=0;
  4067. next_char_loaded:=false;
  4068. repeat
  4069. case c of
  4070. #10,
  4071. #13 :
  4072. linebreak;
  4073. #26 :
  4074. begin
  4075. reload;
  4076. if (c=#26) and not assigned(inputfile.next) then
  4077. end_of_file;
  4078. continue;
  4079. end;
  4080. '{' :
  4081. begin
  4082. if (current_commentstyle in [comment_tp,comment_none]) then
  4083. begin
  4084. current_commentstyle:=comment_tp;
  4085. if (comment_level=0) then
  4086. found:=1;
  4087. inc_comment_level;
  4088. end;
  4089. end;
  4090. '*' :
  4091. begin
  4092. if (current_commentstyle=comment_oldtp) then
  4093. begin
  4094. readchar;
  4095. if c=')' then
  4096. begin
  4097. dec_comment_level;
  4098. found:=0;
  4099. current_commentstyle:=comment_none;
  4100. end
  4101. else
  4102. next_char_loaded:=true;
  4103. end
  4104. else
  4105. found := 0;
  4106. end;
  4107. '}' :
  4108. begin
  4109. if (current_commentstyle=comment_tp) then
  4110. begin
  4111. dec_comment_level;
  4112. if (comment_level=0) then
  4113. current_commentstyle:=comment_none;
  4114. found:=0;
  4115. end;
  4116. end;
  4117. '$' :
  4118. begin
  4119. if found=1 then
  4120. found:=2;
  4121. end;
  4122. '''' :
  4123. if (current_commentstyle=comment_none) then
  4124. begin
  4125. repeat
  4126. readchar;
  4127. case c of
  4128. #26 :
  4129. end_of_file;
  4130. #10,#13 :
  4131. break;
  4132. '''' :
  4133. begin
  4134. readchar;
  4135. if c<>'''' then
  4136. begin
  4137. next_char_loaded:=true;
  4138. break;
  4139. end;
  4140. end;
  4141. end;
  4142. until false;
  4143. end;
  4144. '(' :
  4145. begin
  4146. if (current_commentstyle=comment_none) then
  4147. begin
  4148. readchar;
  4149. if c='*' then
  4150. begin
  4151. readchar;
  4152. if c='$' then
  4153. begin
  4154. found:=2;
  4155. inc_comment_level;
  4156. current_commentstyle:=comment_oldtp;
  4157. end
  4158. else
  4159. begin
  4160. skipoldtpcomment(false);
  4161. next_char_loaded:=true;
  4162. end;
  4163. end
  4164. else
  4165. next_char_loaded:=true;
  4166. end
  4167. else
  4168. found:=0;
  4169. end;
  4170. '/' :
  4171. begin
  4172. if (current_commentstyle=comment_none) then
  4173. begin
  4174. readchar;
  4175. if c='/' then
  4176. skipdelphicomment;
  4177. next_char_loaded:=true;
  4178. end
  4179. else
  4180. found:=0;
  4181. end;
  4182. else
  4183. found:=0;
  4184. end;
  4185. if next_char_loaded then
  4186. next_char_loaded:=false
  4187. else
  4188. readchar;
  4189. until (found=2);
  4190. end;
  4191. {****************************************************************************
  4192. Comment Handling
  4193. ****************************************************************************}
  4194. procedure tscannerfile.skipcomment(read_first_char:boolean);
  4195. begin
  4196. current_commentstyle:=comment_tp;
  4197. if read_first_char then
  4198. readchar;
  4199. inc_comment_level;
  4200. { handle compiler switches }
  4201. if (c='$') then
  4202. handledirectives;
  4203. { handle_switches can dec comment_level, }
  4204. while (comment_level>0) do
  4205. begin
  4206. case c of
  4207. '{' :
  4208. inc_comment_level;
  4209. '}' :
  4210. dec_comment_level;
  4211. #10,#13 :
  4212. linebreak;
  4213. #26 :
  4214. begin
  4215. reload;
  4216. if (c=#26) and not assigned(inputfile.next) then
  4217. end_of_file;
  4218. continue;
  4219. end;
  4220. end;
  4221. readchar;
  4222. end;
  4223. current_commentstyle:=comment_none;
  4224. end;
  4225. procedure tscannerfile.skipdelphicomment;
  4226. begin
  4227. current_commentstyle:=comment_delphi;
  4228. inc_comment_level;
  4229. readchar;
  4230. { this is not supported }
  4231. if c='$' then
  4232. Message(scan_w_wrong_styled_switch);
  4233. { skip comment }
  4234. while not (c in [#10,#13,#26]) do
  4235. readchar;
  4236. dec_comment_level;
  4237. current_commentstyle:=comment_none;
  4238. end;
  4239. procedure tscannerfile.skipoldtpcomment(read_first_char:boolean);
  4240. var
  4241. found : longint;
  4242. begin
  4243. current_commentstyle:=comment_oldtp;
  4244. inc_comment_level;
  4245. { only load a char if last already processed,
  4246. was cause of bug1634 PM }
  4247. if read_first_char then
  4248. readchar;
  4249. { this is now supported }
  4250. if (c='$') then
  4251. handledirectives;
  4252. { skip comment }
  4253. while (comment_level>0) do
  4254. begin
  4255. found:=0;
  4256. repeat
  4257. case c of
  4258. #26 :
  4259. begin
  4260. reload;
  4261. if (c=#26) and not assigned(inputfile.next) then
  4262. end_of_file;
  4263. continue;
  4264. end;
  4265. #10,#13 :
  4266. begin
  4267. if found=4 then
  4268. inc_comment_level;
  4269. linebreak;
  4270. found:=0;
  4271. end;
  4272. '*' :
  4273. begin
  4274. if found=3 then
  4275. found:=4
  4276. else
  4277. begin
  4278. if found=4 then
  4279. inc_comment_level;
  4280. found:=1;
  4281. end;
  4282. end;
  4283. ')' :
  4284. begin
  4285. if found in [1,4] then
  4286. begin
  4287. dec_comment_level;
  4288. if comment_level=0 then
  4289. found:=2
  4290. else
  4291. found:=0;
  4292. end
  4293. else
  4294. found:=0;
  4295. end;
  4296. '(' :
  4297. begin
  4298. if found=4 then
  4299. inc_comment_level;
  4300. found:=3;
  4301. end;
  4302. else
  4303. begin
  4304. if found=4 then
  4305. inc_comment_level;
  4306. found:=0;
  4307. end;
  4308. end;
  4309. readchar;
  4310. until (found=2);
  4311. end;
  4312. current_commentstyle:=comment_none;
  4313. end;
  4314. {****************************************************************************
  4315. Token Scanner
  4316. ****************************************************************************}
  4317. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  4318. var
  4319. code : integer;
  4320. d : cardinal;
  4321. len,
  4322. low,high,mid : longint;
  4323. w : word;
  4324. m : longint;
  4325. mac : tmacro;
  4326. asciinr : string[33];
  4327. iswidestring : boolean;
  4328. label
  4329. exit_label;
  4330. begin
  4331. flushpendingswitchesstate;
  4332. { record tokens? }
  4333. if allowrecordtoken and
  4334. assigned(recordtokenbuf) then
  4335. recordtoken;
  4336. { replay tokens? }
  4337. if assigned(replaytokenbuf) then
  4338. begin
  4339. replaytoken;
  4340. goto exit_label;
  4341. end;
  4342. { was there already a token read, then return that token }
  4343. if nexttoken<>NOTOKEN then
  4344. begin
  4345. setnexttoken;
  4346. goto exit_label;
  4347. end;
  4348. { Skip all spaces and comments }
  4349. repeat
  4350. case c of
  4351. '{' :
  4352. skipcomment(true);
  4353. #26 :
  4354. begin
  4355. reload;
  4356. if (c=#26) and not assigned(inputfile.next) then
  4357. break;
  4358. end;
  4359. ' ',#9..#13 :
  4360. begin
  4361. {$ifdef PREPROCWRITE}
  4362. if parapreprocess then
  4363. begin
  4364. if c=#10 then
  4365. preprocfile.eolfound:=true
  4366. else
  4367. preprocfile.spacefound:=true;
  4368. end;
  4369. {$endif PREPROCWRITE}
  4370. skipspace;
  4371. end
  4372. else
  4373. break;
  4374. end;
  4375. until false;
  4376. { Save current token position, for EOF its already loaded }
  4377. if c<>#26 then
  4378. gettokenpos;
  4379. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  4380. if c in ['A'..'Z','a'..'z','_'] then
  4381. begin
  4382. readstring;
  4383. token:=_ID;
  4384. idtoken:=_ID;
  4385. { keyword or any other known token,
  4386. pattern is always uppercased }
  4387. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4388. begin
  4389. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4390. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4391. while low<high do
  4392. begin
  4393. mid:=(high+low+1) shr 1;
  4394. if pattern<tokeninfo^[ttoken(mid)].str then
  4395. high:=mid-1
  4396. else
  4397. low:=mid;
  4398. end;
  4399. with tokeninfo^[ttoken(high)] do
  4400. if pattern=str then
  4401. begin
  4402. if (keyword*current_settings.modeswitches)<>[] then
  4403. if op=NOTOKEN then
  4404. token:=ttoken(high)
  4405. else
  4406. token:=op;
  4407. idtoken:=ttoken(high);
  4408. end;
  4409. end;
  4410. { Only process identifiers and not keywords }
  4411. if token=_ID then
  4412. begin
  4413. { this takes some time ... }
  4414. if (cs_support_macro in current_settings.moduleswitches) then
  4415. begin
  4416. mac:=tmacro(search_macro(pattern));
  4417. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  4418. begin
  4419. if yylexcount<max_macro_nesting then
  4420. begin
  4421. mac.is_used:=true;
  4422. inc(yylexcount);
  4423. substitutemacro(pattern,mac.buftext,mac.buflen,
  4424. mac.fileinfo.line,mac.fileinfo.fileindex);
  4425. { handle empty macros }
  4426. if c=#0 then
  4427. reload;
  4428. readtoken(false);
  4429. { that's all folks }
  4430. dec(yylexcount);
  4431. exit;
  4432. end
  4433. else
  4434. Message(scan_w_macro_too_deep);
  4435. end;
  4436. end;
  4437. end;
  4438. { return token }
  4439. goto exit_label;
  4440. end
  4441. else
  4442. begin
  4443. idtoken:=_NOID;
  4444. case c of
  4445. '$' :
  4446. begin
  4447. readnumber;
  4448. token:=_INTCONST;
  4449. goto exit_label;
  4450. end;
  4451. '%' :
  4452. begin
  4453. if not(m_fpc in current_settings.modeswitches) then
  4454. Illegal_Char(c)
  4455. else
  4456. begin
  4457. readnumber;
  4458. token:=_INTCONST;
  4459. goto exit_label;
  4460. end;
  4461. end;
  4462. '&' :
  4463. begin
  4464. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  4465. begin
  4466. readnumber;
  4467. if length(pattern)=1 then
  4468. begin
  4469. { does really an identifier follow? }
  4470. if not (c in ['_','A'..'Z','a'..'z']) then
  4471. message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  4472. readstring;
  4473. token:=_ID;
  4474. idtoken:=_ID;
  4475. end
  4476. else
  4477. token:=_INTCONST;
  4478. goto exit_label;
  4479. end
  4480. else if m_mac in current_settings.modeswitches then
  4481. begin
  4482. readchar;
  4483. token:=_AMPERSAND;
  4484. goto exit_label;
  4485. end
  4486. else
  4487. Illegal_Char(c);
  4488. end;
  4489. '0'..'9' :
  4490. begin
  4491. readnumber;
  4492. if (c in ['.','e','E']) then
  4493. begin
  4494. { first check for a . }
  4495. if c='.' then
  4496. begin
  4497. cachenexttokenpos;
  4498. readchar;
  4499. { is it a .. from a range? }
  4500. case c of
  4501. '.' :
  4502. begin
  4503. readchar;
  4504. token:=_INTCONST;
  4505. nexttoken:=_POINTPOINT;
  4506. goto exit_label;
  4507. end;
  4508. ')' :
  4509. begin
  4510. readchar;
  4511. token:=_INTCONST;
  4512. nexttoken:=_RECKKLAMMER;
  4513. goto exit_label;
  4514. end;
  4515. '0'..'9' :
  4516. begin
  4517. { insert the number after the . }
  4518. pattern:=pattern+'.';
  4519. while c in ['0'..'9'] do
  4520. begin
  4521. pattern:=pattern+c;
  4522. readchar;
  4523. end;
  4524. end;
  4525. else
  4526. begin
  4527. token:=_INTCONST;
  4528. nexttoken:=_POINT;
  4529. goto exit_label;
  4530. end;
  4531. end;
  4532. end;
  4533. { E can also follow after a point is scanned }
  4534. if c in ['e','E'] then
  4535. begin
  4536. pattern:=pattern+'E';
  4537. readchar;
  4538. if c in ['-','+'] then
  4539. begin
  4540. pattern:=pattern+c;
  4541. readchar;
  4542. end;
  4543. if not(c in ['0'..'9']) then
  4544. Illegal_Char(c);
  4545. while c in ['0'..'9'] do
  4546. begin
  4547. pattern:=pattern+c;
  4548. readchar;
  4549. end;
  4550. end;
  4551. token:=_REALNUMBER;
  4552. goto exit_label;
  4553. end;
  4554. token:=_INTCONST;
  4555. goto exit_label;
  4556. end;
  4557. ';' :
  4558. begin
  4559. readchar;
  4560. token:=_SEMICOLON;
  4561. goto exit_label;
  4562. end;
  4563. '[' :
  4564. begin
  4565. readchar;
  4566. token:=_LECKKLAMMER;
  4567. goto exit_label;
  4568. end;
  4569. ']' :
  4570. begin
  4571. readchar;
  4572. token:=_RECKKLAMMER;
  4573. goto exit_label;
  4574. end;
  4575. '(' :
  4576. begin
  4577. readchar;
  4578. case c of
  4579. '*' :
  4580. begin
  4581. skipoldtpcomment(true);
  4582. readtoken(false);
  4583. exit;
  4584. end;
  4585. '.' :
  4586. begin
  4587. readchar;
  4588. token:=_LECKKLAMMER;
  4589. goto exit_label;
  4590. end;
  4591. end;
  4592. token:=_LKLAMMER;
  4593. goto exit_label;
  4594. end;
  4595. ')' :
  4596. begin
  4597. readchar;
  4598. token:=_RKLAMMER;
  4599. goto exit_label;
  4600. end;
  4601. '+' :
  4602. begin
  4603. readchar;
  4604. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4605. begin
  4606. readchar;
  4607. token:=_PLUSASN;
  4608. goto exit_label;
  4609. end;
  4610. token:=_PLUS;
  4611. goto exit_label;
  4612. end;
  4613. '-' :
  4614. begin
  4615. readchar;
  4616. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4617. begin
  4618. readchar;
  4619. token:=_MINUSASN;
  4620. goto exit_label;
  4621. end;
  4622. token:=_MINUS;
  4623. goto exit_label;
  4624. end;
  4625. ':' :
  4626. begin
  4627. readchar;
  4628. if c='=' then
  4629. begin
  4630. readchar;
  4631. token:=_ASSIGNMENT;
  4632. goto exit_label;
  4633. end;
  4634. token:=_COLON;
  4635. goto exit_label;
  4636. end;
  4637. '*' :
  4638. begin
  4639. readchar;
  4640. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4641. begin
  4642. readchar;
  4643. token:=_STARASN;
  4644. end
  4645. else
  4646. if c='*' then
  4647. begin
  4648. readchar;
  4649. token:=_STARSTAR;
  4650. end
  4651. else
  4652. token:=_STAR;
  4653. goto exit_label;
  4654. end;
  4655. '/' :
  4656. begin
  4657. readchar;
  4658. case c of
  4659. '=' :
  4660. begin
  4661. if (cs_support_c_operators in current_settings.moduleswitches) then
  4662. begin
  4663. readchar;
  4664. token:=_SLASHASN;
  4665. goto exit_label;
  4666. end;
  4667. end;
  4668. '/' :
  4669. begin
  4670. skipdelphicomment;
  4671. readtoken(false);
  4672. exit;
  4673. end;
  4674. end;
  4675. token:=_SLASH;
  4676. goto exit_label;
  4677. end;
  4678. '|' :
  4679. if m_mac in current_settings.modeswitches then
  4680. begin
  4681. readchar;
  4682. token:=_PIPE;
  4683. goto exit_label;
  4684. end
  4685. else
  4686. Illegal_Char(c);
  4687. '=' :
  4688. begin
  4689. readchar;
  4690. token:=_EQ;
  4691. goto exit_label;
  4692. end;
  4693. '.' :
  4694. begin
  4695. readchar;
  4696. case c of
  4697. '.' :
  4698. begin
  4699. readchar;
  4700. case c of
  4701. '.' :
  4702. begin
  4703. readchar;
  4704. token:=_POINTPOINTPOINT;
  4705. goto exit_label;
  4706. end;
  4707. else
  4708. begin
  4709. token:=_POINTPOINT;
  4710. goto exit_label;
  4711. end;
  4712. end;
  4713. end;
  4714. ')' :
  4715. begin
  4716. readchar;
  4717. token:=_RECKKLAMMER;
  4718. goto exit_label;
  4719. end;
  4720. end;
  4721. token:=_POINT;
  4722. goto exit_label;
  4723. end;
  4724. '@' :
  4725. begin
  4726. readchar;
  4727. token:=_KLAMMERAFFE;
  4728. goto exit_label;
  4729. end;
  4730. ',' :
  4731. begin
  4732. readchar;
  4733. token:=_COMMA;
  4734. goto exit_label;
  4735. end;
  4736. '''','#','^' :
  4737. begin
  4738. len:=0;
  4739. cstringpattern:='';
  4740. iswidestring:=false;
  4741. if c='^' then
  4742. begin
  4743. readchar;
  4744. c:=upcase(c);
  4745. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  4746. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  4747. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  4748. begin
  4749. token:=_CARET;
  4750. goto exit_label;
  4751. end
  4752. else
  4753. begin
  4754. inc(len);
  4755. setlength(cstringpattern,256);
  4756. if c<#64 then
  4757. cstringpattern[len]:=chr(ord(c)+64)
  4758. else
  4759. cstringpattern[len]:=chr(ord(c)-64);
  4760. readchar;
  4761. end;
  4762. end;
  4763. repeat
  4764. case c of
  4765. '#' :
  4766. begin
  4767. readchar; { read # }
  4768. case c of
  4769. '$':
  4770. begin
  4771. readchar; { read leading $ }
  4772. asciinr:='$';
  4773. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=7) do
  4774. begin
  4775. asciinr:=asciinr+c;
  4776. readchar;
  4777. end;
  4778. end;
  4779. '&':
  4780. begin
  4781. readchar; { read leading $ }
  4782. asciinr:='&';
  4783. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=8) do
  4784. begin
  4785. asciinr:=asciinr+c;
  4786. readchar;
  4787. end;
  4788. end;
  4789. '%':
  4790. begin
  4791. readchar; { read leading $ }
  4792. asciinr:='%';
  4793. while (upcase(c) in ['0','1']) and (length(asciinr)<=22) do
  4794. begin
  4795. asciinr:=asciinr+c;
  4796. readchar;
  4797. end;
  4798. end;
  4799. else
  4800. begin
  4801. asciinr:='';
  4802. while (c in ['0'..'9']) and (length(asciinr)<=8) do
  4803. begin
  4804. asciinr:=asciinr+c;
  4805. readchar;
  4806. end;
  4807. end;
  4808. end;
  4809. val(asciinr,m,code);
  4810. if (asciinr='') or (code<>0) then
  4811. Message(scan_e_illegal_char_const)
  4812. else if (m<0) or (m>255) or (length(asciinr)>3) then
  4813. begin
  4814. if (m>=0) and (m<=$10FFFF) then
  4815. begin
  4816. if not iswidestring then
  4817. begin
  4818. if len>0 then
  4819. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4820. else
  4821. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4822. iswidestring:=true;
  4823. len:=0;
  4824. end;
  4825. if m<=$FFFF then
  4826. concatwidestringchar(patternw,tcompilerwidechar(m))
  4827. else
  4828. begin
  4829. { split into surrogate pair }
  4830. dec(m,$10000);
  4831. concatwidestringchar(patternw,tcompilerwidechar((m shr 10) + $D800));
  4832. concatwidestringchar(patternw,tcompilerwidechar((m and $3FF) + $DC00));
  4833. end;
  4834. end
  4835. else
  4836. Message(scan_e_illegal_char_const)
  4837. end
  4838. else if iswidestring then
  4839. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  4840. else
  4841. begin
  4842. if len>=length(cstringpattern) then
  4843. setlength(cstringpattern,length(cstringpattern)+256);
  4844. inc(len);
  4845. cstringpattern[len]:=chr(m);
  4846. end;
  4847. end;
  4848. '''' :
  4849. begin
  4850. repeat
  4851. readchar;
  4852. case c of
  4853. #26 :
  4854. end_of_file;
  4855. #10,#13 :
  4856. Message(scan_f_string_exceeds_line);
  4857. '''' :
  4858. begin
  4859. readchar;
  4860. if c<>'''' then
  4861. break;
  4862. end;
  4863. end;
  4864. { interpret as utf-8 string? }
  4865. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  4866. begin
  4867. { convert existing string to an utf-8 string }
  4868. if not iswidestring then
  4869. begin
  4870. if len>0 then
  4871. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4872. else
  4873. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4874. iswidestring:=true;
  4875. len:=0;
  4876. end;
  4877. { four chars }
  4878. if (ord(c) and $f0)=$f0 then
  4879. begin
  4880. { this always represents a surrogate pair, so
  4881. read as 32-bit value and then split into
  4882. the corresponding pair of two wchars }
  4883. d:=ord(c) and $f;
  4884. readchar;
  4885. if (ord(c) and $c0)<>$80 then
  4886. message(scan_e_utf8_malformed);
  4887. d:=(d shl 6) or (ord(c) and $3f);
  4888. readchar;
  4889. if (ord(c) and $c0)<>$80 then
  4890. message(scan_e_utf8_malformed);
  4891. d:=(d shl 6) or (ord(c) and $3f);
  4892. readchar;
  4893. if (ord(c) and $c0)<>$80 then
  4894. message(scan_e_utf8_malformed);
  4895. d:=(d shl 6) or (ord(c) and $3f);
  4896. if d<$10000 then
  4897. message(scan_e_utf8_malformed);
  4898. d:=d-$10000;
  4899. { high surrogate }
  4900. w:=$d800+(d shr 10);
  4901. concatwidestringchar(patternw,w);
  4902. { low surrogate }
  4903. w:=$dc00+(d and $3ff);
  4904. concatwidestringchar(patternw,w);
  4905. end
  4906. { three chars }
  4907. else if (ord(c) and $e0)=$e0 then
  4908. begin
  4909. w:=ord(c) and $f;
  4910. readchar;
  4911. if (ord(c) and $c0)<>$80 then
  4912. message(scan_e_utf8_malformed);
  4913. w:=(w shl 6) or (ord(c) and $3f);
  4914. readchar;
  4915. if (ord(c) and $c0)<>$80 then
  4916. message(scan_e_utf8_malformed);
  4917. w:=(w shl 6) or (ord(c) and $3f);
  4918. concatwidestringchar(patternw,w);
  4919. end
  4920. { two chars }
  4921. else if (ord(c) and $c0)<>0 then
  4922. begin
  4923. w:=ord(c) and $1f;
  4924. readchar;
  4925. if (ord(c) and $c0)<>$80 then
  4926. message(scan_e_utf8_malformed);
  4927. w:=(w shl 6) or (ord(c) and $3f);
  4928. concatwidestringchar(patternw,w);
  4929. end
  4930. { illegal }
  4931. else if (ord(c) and $80)<>0 then
  4932. message(scan_e_utf8_malformed)
  4933. else
  4934. concatwidestringchar(patternw,tcompilerwidechar(c))
  4935. end
  4936. else if iswidestring then
  4937. begin
  4938. if current_settings.sourcecodepage=CP_UTF8 then
  4939. concatwidestringchar(patternw,ord(c))
  4940. else
  4941. concatwidestringchar(patternw,asciichar2unicode(c))
  4942. end
  4943. else
  4944. begin
  4945. if len>=length(cstringpattern) then
  4946. setlength(cstringpattern,length(cstringpattern)+256);
  4947. inc(len);
  4948. cstringpattern[len]:=c;
  4949. end;
  4950. until false;
  4951. end;
  4952. '^' :
  4953. begin
  4954. readchar;
  4955. c:=upcase(c);
  4956. if c<#64 then
  4957. c:=chr(ord(c)+64)
  4958. else
  4959. c:=chr(ord(c)-64);
  4960. if iswidestring then
  4961. concatwidestringchar(patternw,asciichar2unicode(c))
  4962. else
  4963. begin
  4964. if len>=length(cstringpattern) then
  4965. setlength(cstringpattern,length(cstringpattern)+256);
  4966. inc(len);
  4967. cstringpattern[len]:=c;
  4968. end;
  4969. readchar;
  4970. end;
  4971. else
  4972. break;
  4973. end;
  4974. until false;
  4975. { strings with length 1 become const chars }
  4976. if iswidestring then
  4977. begin
  4978. if patternw^.len=1 then
  4979. token:=_CWCHAR
  4980. else
  4981. token:=_CWSTRING;
  4982. end
  4983. else
  4984. begin
  4985. setlength(cstringpattern,len);
  4986. if length(cstringpattern)=1 then
  4987. begin
  4988. token:=_CCHAR;
  4989. pattern:=cstringpattern;
  4990. end
  4991. else
  4992. token:=_CSTRING;
  4993. end;
  4994. goto exit_label;
  4995. end;
  4996. '>' :
  4997. begin
  4998. readchar;
  4999. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5000. token:=_RSHARPBRACKET
  5001. else
  5002. begin
  5003. case c of
  5004. '=' :
  5005. begin
  5006. readchar;
  5007. token:=_GTE;
  5008. goto exit_label;
  5009. end;
  5010. '>' :
  5011. begin
  5012. readchar;
  5013. token:=_OP_SHR;
  5014. goto exit_label;
  5015. end;
  5016. '<' :
  5017. begin { >< is for a symetric diff for sets }
  5018. readchar;
  5019. token:=_SYMDIF;
  5020. goto exit_label;
  5021. end;
  5022. end;
  5023. token:=_GT;
  5024. end;
  5025. goto exit_label;
  5026. end;
  5027. '<' :
  5028. begin
  5029. readchar;
  5030. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5031. token:=_LSHARPBRACKET
  5032. else
  5033. begin
  5034. case c of
  5035. '>' :
  5036. begin
  5037. readchar;
  5038. token:=_NE;
  5039. goto exit_label;
  5040. end;
  5041. '=' :
  5042. begin
  5043. readchar;
  5044. token:=_LTE;
  5045. goto exit_label;
  5046. end;
  5047. '<' :
  5048. begin
  5049. readchar;
  5050. token:=_OP_SHL;
  5051. goto exit_label;
  5052. end;
  5053. end;
  5054. token:=_LT;
  5055. end;
  5056. goto exit_label;
  5057. end;
  5058. #26 :
  5059. begin
  5060. token:=_EOF;
  5061. checkpreprocstack;
  5062. goto exit_label;
  5063. end;
  5064. else
  5065. Illegal_Char(c);
  5066. end;
  5067. end;
  5068. exit_label:
  5069. lasttoken:=token;
  5070. end;
  5071. function tscannerfile.readpreproc:ttoken;
  5072. var
  5073. low,high,mid: longint;
  5074. optoken: ttoken;
  5075. begin
  5076. skipspace;
  5077. case c of
  5078. '_',
  5079. 'A'..'Z',
  5080. 'a'..'z' :
  5081. begin
  5082. readstring;
  5083. optoken:=_ID;
  5084. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  5085. begin
  5086. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  5087. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  5088. while low<high do
  5089. begin
  5090. mid:=(high+low+1) shr 1;
  5091. if pattern<tokeninfo^[ttoken(mid)].str then
  5092. high:=mid-1
  5093. else
  5094. low:=mid;
  5095. end;
  5096. with tokeninfo^[ttoken(high)] do
  5097. if pattern=str then
  5098. begin
  5099. if (keyword*current_settings.modeswitches)<>[] then
  5100. if op=NOTOKEN then
  5101. optoken:=ttoken(high)
  5102. else
  5103. optoken:=op;
  5104. end;
  5105. if not (optoken in preproc_operators) then
  5106. optoken:=_ID;
  5107. end;
  5108. current_scanner.preproc_pattern:=pattern;
  5109. readpreproc:=optoken;
  5110. end;
  5111. '''' :
  5112. begin
  5113. readquotedstring;
  5114. current_scanner.preproc_pattern:=cstringpattern;
  5115. readpreproc:=_CSTRING;
  5116. end;
  5117. '0'..'9' :
  5118. begin
  5119. readnumber;
  5120. if (c in ['.','e','E']) then
  5121. begin
  5122. { first check for a . }
  5123. if c='.' then
  5124. begin
  5125. readchar;
  5126. if c in ['0'..'9'] then
  5127. begin
  5128. { insert the number after the . }
  5129. pattern:=pattern+'.';
  5130. while c in ['0'..'9'] do
  5131. begin
  5132. pattern:=pattern+c;
  5133. readchar;
  5134. end;
  5135. end
  5136. else
  5137. Illegal_Char(c);
  5138. end;
  5139. { E can also follow after a point is scanned }
  5140. if c in ['e','E'] then
  5141. begin
  5142. pattern:=pattern+'E';
  5143. readchar;
  5144. if c in ['-','+'] then
  5145. begin
  5146. pattern:=pattern+c;
  5147. readchar;
  5148. end;
  5149. if not(c in ['0'..'9']) then
  5150. Illegal_Char(c);
  5151. while c in ['0'..'9'] do
  5152. begin
  5153. pattern:=pattern+c;
  5154. readchar;
  5155. end;
  5156. end;
  5157. readpreproc:=_REALNUMBER;
  5158. end
  5159. else
  5160. readpreproc:=_INTCONST;
  5161. current_scanner.preproc_pattern:=pattern;
  5162. end;
  5163. '$','%':
  5164. begin
  5165. readnumber;
  5166. current_scanner.preproc_pattern:=pattern;
  5167. readpreproc:=_INTCONST;
  5168. end;
  5169. '&' :
  5170. begin
  5171. readnumber;
  5172. if length(pattern)=1 then
  5173. begin
  5174. readstring;
  5175. readpreproc:=_ID;
  5176. end
  5177. else
  5178. readpreproc:=_INTCONST;
  5179. current_scanner.preproc_pattern:=pattern;
  5180. end;
  5181. '.' :
  5182. begin
  5183. readchar;
  5184. readpreproc:=_POINT;
  5185. end;
  5186. ',' :
  5187. begin
  5188. readchar;
  5189. readpreproc:=_COMMA;
  5190. end;
  5191. '}' :
  5192. begin
  5193. readpreproc:=_END;
  5194. end;
  5195. '(' :
  5196. begin
  5197. readchar;
  5198. readpreproc:=_LKLAMMER;
  5199. end;
  5200. ')' :
  5201. begin
  5202. readchar;
  5203. readpreproc:=_RKLAMMER;
  5204. end;
  5205. '[' :
  5206. begin
  5207. readchar;
  5208. readpreproc:=_LECKKLAMMER;
  5209. end;
  5210. ']' :
  5211. begin
  5212. readchar;
  5213. readpreproc:=_RECKKLAMMER;
  5214. end;
  5215. '+' :
  5216. begin
  5217. readchar;
  5218. readpreproc:=_PLUS;
  5219. end;
  5220. '-' :
  5221. begin
  5222. readchar;
  5223. readpreproc:=_MINUS;
  5224. end;
  5225. '*' :
  5226. begin
  5227. readchar;
  5228. readpreproc:=_STAR;
  5229. end;
  5230. '/' :
  5231. begin
  5232. readchar;
  5233. readpreproc:=_SLASH;
  5234. end;
  5235. '=' :
  5236. begin
  5237. readchar;
  5238. readpreproc:=_EQ;
  5239. end;
  5240. '>' :
  5241. begin
  5242. readchar;
  5243. if c='=' then
  5244. begin
  5245. readchar;
  5246. readpreproc:=_GTE;
  5247. end
  5248. else
  5249. readpreproc:=_GT;
  5250. end;
  5251. '<' :
  5252. begin
  5253. readchar;
  5254. case c of
  5255. '>' :
  5256. begin
  5257. readchar;
  5258. readpreproc:=_NE;
  5259. end;
  5260. '=' :
  5261. begin
  5262. readchar;
  5263. readpreproc:=_LTE;
  5264. end;
  5265. else
  5266. readpreproc:=_LT;
  5267. end;
  5268. end;
  5269. #26 :
  5270. begin
  5271. readpreproc:=_EOF;
  5272. checkpreprocstack;
  5273. end;
  5274. else
  5275. begin
  5276. Illegal_Char(c);
  5277. readpreproc:=NOTOKEN;
  5278. end;
  5279. end;
  5280. end;
  5281. function tscannerfile.readpreprocint(var value:int64;const place:string):boolean;
  5282. var
  5283. hs : texprvalue;
  5284. begin
  5285. hs:=preproc_comp_expr;
  5286. if hs.isInt then
  5287. begin
  5288. value:=hs.asInt64;
  5289. result:=true;
  5290. end
  5291. else
  5292. begin
  5293. hs.error('Integer',place);
  5294. result:=false;
  5295. end;
  5296. hs.free;
  5297. end;
  5298. function tscannerfile.asmgetchar : char;
  5299. begin
  5300. readchar;
  5301. repeat
  5302. case c of
  5303. #26 :
  5304. begin
  5305. reload;
  5306. if (c=#26) and not assigned(inputfile.next) then
  5307. end_of_file;
  5308. continue;
  5309. end;
  5310. else
  5311. begin
  5312. asmgetchar:=c;
  5313. exit;
  5314. end;
  5315. end;
  5316. until false;
  5317. end;
  5318. {*****************************************************************************
  5319. Helpers
  5320. *****************************************************************************}
  5321. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5322. begin
  5323. if dm in [directive_all, directive_turbo] then
  5324. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  5325. if dm in [directive_all, directive_mac] then
  5326. tdirectiveitem.create(mac_scannerdirectives,s,p);
  5327. end;
  5328. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5329. begin
  5330. if dm in [directive_all, directive_turbo] then
  5331. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  5332. if dm in [directive_all, directive_mac] then
  5333. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  5334. end;
  5335. {*****************************************************************************
  5336. Initialization
  5337. *****************************************************************************}
  5338. procedure InitScanner;
  5339. begin
  5340. InitWideString(patternw);
  5341. turbo_scannerdirectives:=TFPHashObjectList.Create;
  5342. mac_scannerdirectives:=TFPHashObjectList.Create;
  5343. { Common directives and conditionals }
  5344. AddDirective('I',directive_all, @dir_include);
  5345. AddDirective('DEFINE',directive_all, @dir_define);
  5346. AddDirective('UNDEF',directive_all, @dir_undef);
  5347. AddConditional('IF',directive_all, @dir_if);
  5348. AddConditional('IFDEF',directive_all, @dir_ifdef);
  5349. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  5350. AddConditional('ELSE',directive_all, @dir_else);
  5351. AddConditional('ELSEIF',directive_all, @dir_elseif);
  5352. AddConditional('ENDIF',directive_all, @dir_endif);
  5353. { Directives and conditionals for all modes except mode macpas}
  5354. AddDirective('INCLUDE',directive_turbo, @dir_include);
  5355. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  5356. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  5357. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  5358. AddConditional('IFEND',directive_turbo, @dir_endif);
  5359. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  5360. { Directives and conditionals for mode macpas: }
  5361. AddDirective('SETC',directive_mac, @dir_setc);
  5362. AddDirective('DEFINEC',directive_mac, @dir_definec);
  5363. AddDirective('UNDEFC',directive_mac, @dir_undef);
  5364. AddConditional('IFC',directive_mac, @dir_if);
  5365. AddConditional('ELSEC',directive_mac, @dir_else);
  5366. AddConditional('ELIFC',directive_mac, @dir_elseif);
  5367. AddConditional('ENDC',directive_mac, @dir_endif);
  5368. end;
  5369. procedure DoneScanner;
  5370. begin
  5371. turbo_scannerdirectives.Free;
  5372. mac_scannerdirectives.Free;
  5373. DoneWideString(patternw);
  5374. end;
  5375. end.