scanner.pas 187 KB

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