scanner.pas 192 KB

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