scanner.pas 183 KB

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