scanner.pas 183 KB

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