2
0

scanner.pas 182 KB

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