scanner.pas 187 KB

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