scanner.pas 184 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569
  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. begin
  1768. if searchsym(storedpattern,srsym,srsymtable) then
  1769. begin
  1770. try_consume_nestedsym(srsym,srsymtable);
  1771. if assigned(srsym) then
  1772. case srsym.typ of
  1773. constsym:
  1774. begin
  1775. result.free;
  1776. result:=texprvalue.create_const(tconstsym(srsym));
  1777. end;
  1778. enumsym:
  1779. begin
  1780. result.free;
  1781. result:=texprvalue.create_int(tenumsym(srsym).value);
  1782. end;
  1783. end;
  1784. end
  1785. end
  1786. { skip id(<expr>) if expression must not be evaluated }
  1787. else if not(eval) and (result.consttyp=conststring) then
  1788. begin
  1789. if current_scanner.preproc_token =_LKLAMMER then
  1790. begin
  1791. preproc_consume(_LKLAMMER);
  1792. current_scanner.skipspace;
  1793. result:=preproc_factor(false);
  1794. if current_scanner.preproc_token =_RKLAMMER then
  1795. preproc_consume(_RKLAMMER)
  1796. else
  1797. Message(scan_e_error_in_preproc_expr);
  1798. end;
  1799. end;
  1800. end
  1801. end
  1802. else if current_scanner.preproc_token =_LKLAMMER then
  1803. begin
  1804. preproc_consume(_LKLAMMER);
  1805. result:=preproc_sub_expr(opcompare,eval);
  1806. preproc_consume(_RKLAMMER);
  1807. end
  1808. else if current_scanner.preproc_token = _LECKKLAMMER then
  1809. begin
  1810. preproc_consume(_LECKKLAMMER);
  1811. ns:=[];
  1812. while current_scanner.preproc_token in [_ID,_INTCONST] do
  1813. begin
  1814. exprvalue:=preproc_factor(eval);
  1815. include(ns,exprvalue.asInt);
  1816. if current_scanner.preproc_token = _COMMA then
  1817. preproc_consume(_COMMA);
  1818. end;
  1819. // TODO Add check of setElemType
  1820. preproc_consume(_RECKKLAMMER);
  1821. result:=texprvalue.create_set(ns);
  1822. end
  1823. else if current_scanner.preproc_token = _INTCONST then
  1824. begin
  1825. result:=texprvalue.try_parse_number(current_scanner.preproc_pattern);
  1826. if not assigned(result) then
  1827. begin
  1828. Message(parser_e_invalid_integer);
  1829. result:=texprvalue.create_int(1);
  1830. end;
  1831. preproc_consume(_INTCONST);
  1832. end
  1833. else if current_scanner.preproc_token = _REALNUMBER then
  1834. begin
  1835. result:=texprvalue.try_parse_real(current_scanner.preproc_pattern);
  1836. if not assigned(result) then
  1837. begin
  1838. Message(parser_e_error_in_real);
  1839. result:=texprvalue.create_real(1.0);
  1840. end;
  1841. preproc_consume(_REALNUMBER);
  1842. end
  1843. else
  1844. Message(scan_e_error_in_preproc_expr);
  1845. if not assigned(result) then
  1846. result:=texprvalue.create_error;
  1847. end;
  1848. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean): texprvalue;
  1849. var
  1850. hs1,hs2: texprvalue;
  1851. op: ttoken;
  1852. begin
  1853. if pred_level=highest_precedence then
  1854. result:=preproc_factor(eval)
  1855. else
  1856. result:=preproc_sub_expr(succ(pred_level),eval);
  1857. repeat
  1858. op:=current_scanner.preproc_token;
  1859. if (op in preproc_operators) and
  1860. (op in operator_levels[pred_level]) then
  1861. begin
  1862. hs1:=result;
  1863. preproc_consume(op);
  1864. if (op=_OP_OR) and hs1.isBoolean and hs1.asBool then
  1865. begin
  1866. { stop evaluation the rest of expression }
  1867. result:=texprvalue.create_bool(true);
  1868. if pred_level=highest_precedence then
  1869. hs2:=preproc_factor(false)
  1870. else
  1871. hs2:=preproc_sub_expr(succ(pred_level),false);
  1872. end
  1873. else if (op=_OP_AND) and hs1.isBoolean and not hs1.asBool then
  1874. begin
  1875. { stop evaluation the rest of expression }
  1876. result:=texprvalue.create_bool(false);
  1877. if pred_level=highest_precedence then
  1878. hs2:=preproc_factor(false)
  1879. else
  1880. hs2:=preproc_sub_expr(succ(pred_level),false);
  1881. end
  1882. else
  1883. begin
  1884. if pred_level=highest_precedence then
  1885. hs2:=preproc_factor(eval)
  1886. else
  1887. hs2:=preproc_sub_expr(succ(pred_level),eval);
  1888. if eval then
  1889. result:=hs1.evaluate(hs2,op)
  1890. else
  1891. result:=texprvalue.create_bool(false); {Just to have something}
  1892. end;
  1893. hs1.free;
  1894. hs2.free;
  1895. end
  1896. else
  1897. break;
  1898. until false;
  1899. end;
  1900. begin
  1901. current_scanner.skipspace;
  1902. { start preproc expression scanner }
  1903. current_scanner.preproc_token:=current_scanner.readpreproc;
  1904. preproc_comp_expr:=preproc_sub_expr(opcompare,true);
  1905. end;
  1906. function boolean_compile_time_expr(var valuedescr: string): Boolean;
  1907. var
  1908. hs: texprvalue;
  1909. begin
  1910. hs:=preproc_comp_expr;
  1911. if hs.isBoolean then
  1912. result:=hs.asBool
  1913. else
  1914. begin
  1915. hs.error('Boolean', 'IF or ELSEIF');
  1916. result:=false;
  1917. end;
  1918. valuedescr:=hs.asStr;
  1919. hs.free;
  1920. end;
  1921. procedure dir_if;
  1922. begin
  1923. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  1924. end;
  1925. procedure dir_elseif;
  1926. begin
  1927. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  1928. end;
  1929. procedure dir_define_impl(macstyle: boolean);
  1930. var
  1931. hs : string;
  1932. bracketcount : longint;
  1933. mac : tmacro;
  1934. macropos : longint;
  1935. macrobuffer : pmacrobuffer;
  1936. begin
  1937. current_scanner.skipspace;
  1938. hs:=current_scanner.readid;
  1939. mac:=tmacro(search_macro(hs));
  1940. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  1941. begin
  1942. mac:=tmacro.create(hs);
  1943. mac.defined:=true;
  1944. current_module.localmacrosymtable.insert(mac);
  1945. end
  1946. else
  1947. begin
  1948. mac.defined:=true;
  1949. mac.is_compiler_var:=false;
  1950. { delete old definition }
  1951. if assigned(mac.buftext) then
  1952. begin
  1953. freemem(mac.buftext,mac.buflen);
  1954. mac.buftext:=nil;
  1955. end;
  1956. end;
  1957. Message1(parser_c_macro_defined,mac.name);
  1958. mac.is_used:=true;
  1959. if (cs_support_macro in current_settings.moduleswitches) then
  1960. begin
  1961. current_scanner.skipspace;
  1962. if not macstyle then
  1963. begin
  1964. { may be a macro? }
  1965. if c <> ':' then
  1966. exit;
  1967. current_scanner.readchar;
  1968. if c <> '=' then
  1969. exit;
  1970. current_scanner.readchar;
  1971. current_scanner.skipspace;
  1972. end;
  1973. { key words are never substituted }
  1974. if is_keyword(hs) then
  1975. Message(scan_e_keyword_cant_be_a_macro);
  1976. new(macrobuffer);
  1977. macropos:=0;
  1978. { parse macro, brackets are counted so it's possible
  1979. to have a $ifdef etc. in the macro }
  1980. bracketcount:=0;
  1981. repeat
  1982. case c of
  1983. '}' :
  1984. if (bracketcount=0) then
  1985. break
  1986. else
  1987. dec(bracketcount);
  1988. '{' :
  1989. inc(bracketcount);
  1990. #10,#13 :
  1991. current_scanner.linebreak;
  1992. #26 :
  1993. current_scanner.end_of_file;
  1994. end;
  1995. macrobuffer^[macropos]:=c;
  1996. inc(macropos);
  1997. if macropos>=maxmacrolen then
  1998. Message(scan_f_macro_buffer_overflow);
  1999. current_scanner.readchar;
  2000. until false;
  2001. { free buffer of macro ?}
  2002. if assigned(mac.buftext) then
  2003. freemem(mac.buftext,mac.buflen);
  2004. { get new mem }
  2005. getmem(mac.buftext,macropos);
  2006. mac.buflen:=macropos;
  2007. { copy the text }
  2008. move(macrobuffer^,mac.buftext^,macropos);
  2009. dispose(macrobuffer);
  2010. end
  2011. else
  2012. begin
  2013. { check if there is an assignment, then we need to give a
  2014. warning }
  2015. current_scanner.skipspace;
  2016. if c=':' then
  2017. begin
  2018. current_scanner.readchar;
  2019. if c='=' then
  2020. Message(scan_w_macro_support_turned_off);
  2021. end;
  2022. end;
  2023. end;
  2024. procedure dir_define;
  2025. begin
  2026. dir_define_impl(false);
  2027. end;
  2028. procedure dir_definec;
  2029. begin
  2030. dir_define_impl(true);
  2031. end;
  2032. procedure dir_setc;
  2033. var
  2034. hs : string;
  2035. mac : tmacro;
  2036. l : longint;
  2037. w : integer;
  2038. exprvalue: texprvalue;
  2039. begin
  2040. current_scanner.skipspace;
  2041. hs:=current_scanner.readid;
  2042. mac:=tmacro(search_macro(hs));
  2043. if not assigned(mac) or
  2044. (mac.owner <> current_module.localmacrosymtable) then
  2045. begin
  2046. mac:=tmacro.create(hs);
  2047. mac.defined:=true;
  2048. mac.is_compiler_var:=true;
  2049. current_module.localmacrosymtable.insert(mac);
  2050. end
  2051. else
  2052. begin
  2053. mac.defined:=true;
  2054. mac.is_compiler_var:=true;
  2055. { delete old definition }
  2056. if assigned(mac.buftext) then
  2057. begin
  2058. freemem(mac.buftext,mac.buflen);
  2059. mac.buftext:=nil;
  2060. end;
  2061. end;
  2062. Message1(parser_c_macro_defined,mac.name);
  2063. mac.is_used:=true;
  2064. { key words are never substituted }
  2065. if is_keyword(hs) then
  2066. Message(scan_e_keyword_cant_be_a_macro);
  2067. { macro assignment can be both := and = }
  2068. current_scanner.skipspace;
  2069. if c=':' then
  2070. current_scanner.readchar;
  2071. if c='=' then
  2072. begin
  2073. current_scanner.readchar;
  2074. exprvalue:=preproc_comp_expr;
  2075. if not is_boolean(exprvalue.def) and
  2076. not is_integer(exprvalue.def) then
  2077. exprvalue.error('Boolean, Integer', 'SETC');
  2078. hs:=exprvalue.asStr;
  2079. if length(hs) <> 0 then
  2080. begin
  2081. {If we are absolutely shure it is boolean, translate
  2082. to TRUE/FALSE to increase possibility to do future type check}
  2083. if exprvalue.isBoolean then
  2084. begin
  2085. if exprvalue.asBool then
  2086. hs:='TRUE'
  2087. else
  2088. hs:='FALSE';
  2089. end;
  2090. Message2(parser_c_macro_set_to,mac.name,hs);
  2091. { free buffer of macro ?}
  2092. if assigned(mac.buftext) then
  2093. freemem(mac.buftext,mac.buflen);
  2094. { get new mem }
  2095. getmem(mac.buftext,length(hs));
  2096. mac.buflen:=length(hs);
  2097. { copy the text }
  2098. move(hs[1],mac.buftext^,mac.buflen);
  2099. end
  2100. else
  2101. Message(scan_e_preproc_syntax_error);
  2102. exprvalue.free;
  2103. end
  2104. else
  2105. Message(scan_e_preproc_syntax_error);
  2106. end;
  2107. procedure dir_undef;
  2108. var
  2109. hs : string;
  2110. mac : tmacro;
  2111. begin
  2112. current_scanner.skipspace;
  2113. hs:=current_scanner.readid;
  2114. mac:=tmacro(search_macro(hs));
  2115. if not assigned(mac) or
  2116. (mac.owner <> current_module.localmacrosymtable) then
  2117. begin
  2118. mac:=tmacro.create(hs);
  2119. mac.defined:=false;
  2120. current_module.localmacrosymtable.insert(mac);
  2121. end
  2122. else
  2123. begin
  2124. mac.defined:=false;
  2125. mac.is_compiler_var:=false;
  2126. { delete old definition }
  2127. if assigned(mac.buftext) then
  2128. begin
  2129. freemem(mac.buftext,mac.buflen);
  2130. mac.buftext:=nil;
  2131. end;
  2132. end;
  2133. Message1(parser_c_macro_undefined,mac.name);
  2134. mac.is_used:=true;
  2135. end;
  2136. procedure dir_include;
  2137. function findincludefile(const path,name:TCmdStr;var foundfile:TCmdStr):boolean;
  2138. var
  2139. found : boolean;
  2140. hpath : TCmdStr;
  2141. begin
  2142. (* look for the include file
  2143. If path was absolute and specified as part of {$I } then
  2144. 1. specified path
  2145. else
  2146. 1. path of current inputfile,current dir
  2147. 2. local includepath
  2148. 3. global includepath
  2149. -- Check mantis #13461 before changing this *)
  2150. found:=false;
  2151. foundfile:='';
  2152. hpath:='';
  2153. if path_absolute(path) then
  2154. begin
  2155. found:=FindFile(name,path,true,foundfile);
  2156. end
  2157. else
  2158. begin
  2159. hpath:=current_scanner.inputfile.path+';'+CurDirRelPath(source_info);
  2160. found:=FindFile(path+name, hpath,true,foundfile);
  2161. if not found then
  2162. found:=current_module.localincludesearchpath.FindFile(path+name,true,foundfile);
  2163. if not found then
  2164. found:=includesearchpath.FindFile(path+name,true,foundfile);
  2165. end;
  2166. result:=found;
  2167. end;
  2168. var
  2169. foundfile : TCmdStr;
  2170. path,
  2171. name,
  2172. hs : tpathstr;
  2173. args : string;
  2174. hp : tinputfile;
  2175. found : boolean;
  2176. macroIsString : boolean;
  2177. begin
  2178. current_scanner.skipspace;
  2179. args:=current_scanner.readcomment;
  2180. hs:=GetToken(args,' ');
  2181. if hs='' then
  2182. exit;
  2183. if (hs[1]='%') then
  2184. begin
  2185. { case insensitive }
  2186. hs:=upper(hs);
  2187. { remove %'s }
  2188. Delete(hs,1,1);
  2189. if hs[length(hs)]='%' then
  2190. Delete(hs,length(hs),1);
  2191. { save old }
  2192. path:=hs;
  2193. { first check for internal macros }
  2194. macroIsString:=true;
  2195. if hs='TIME' then
  2196. hs:=gettimestr
  2197. else
  2198. if hs='DATE' then
  2199. hs:=getdatestr
  2200. else
  2201. if hs='FILE' then
  2202. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex)
  2203. else
  2204. if hs='LINE' then
  2205. hs:=tostr(current_filepos.line)
  2206. else
  2207. if hs='LINENUM' then
  2208. begin
  2209. hs:=tostr(current_filepos.line);
  2210. macroIsString:=false;
  2211. end
  2212. else
  2213. if hs='FPCVERSION' then
  2214. hs:=version_string
  2215. else
  2216. if hs='FPCDATE' then
  2217. hs:=date_string
  2218. else
  2219. if hs='FPCTARGET' then
  2220. hs:=target_cpu_string
  2221. else
  2222. if hs='FPCTARGETCPU' then
  2223. hs:=target_cpu_string
  2224. else
  2225. if hs='FPCTARGETOS' then
  2226. hs:=target_info.shortname
  2227. else
  2228. hs:=GetEnvironmentVariable(hs);
  2229. if hs='' then
  2230. Message1(scan_w_include_env_not_found,path);
  2231. { make it a stringconst }
  2232. if macroIsString then
  2233. hs:=''''+hs+'''';
  2234. current_scanner.substitutemacro(path,@hs[1],length(hs),
  2235. current_scanner.line_no,current_scanner.inputfile.ref_index);
  2236. end
  2237. else
  2238. begin
  2239. hs:=FixFileName(hs);
  2240. path:=ExtractFilePath(hs);
  2241. name:=ExtractFileName(hs);
  2242. { Special case for Delphi compatibility: '*' has to be replaced
  2243. by the file name of the current source file. }
  2244. if (length(name)>=1) and
  2245. (name[1]='*') then
  2246. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  2247. { try to find the file }
  2248. found:=findincludefile(path,name,foundfile);
  2249. if (not found) and (ExtractFileExt(name)='') then
  2250. begin
  2251. { try default extensions .inc , .pp and .pas }
  2252. if (not found) then
  2253. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  2254. if (not found) then
  2255. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  2256. if (not found) then
  2257. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  2258. end;
  2259. if current_scanner.inputfilecount<max_include_nesting then
  2260. begin
  2261. inc(current_scanner.inputfilecount);
  2262. { we need to reread the current char }
  2263. dec(current_scanner.inputpointer);
  2264. { shutdown current file }
  2265. current_scanner.tempcloseinputfile;
  2266. { load new file }
  2267. hp:=do_openinputfile(foundfile);
  2268. hp.inc_path:=path;
  2269. current_scanner.addfile(hp);
  2270. current_module.sourcefiles.register_file(hp);
  2271. if (not found) then
  2272. Message1(scan_f_cannot_open_includefile,hs);
  2273. if (not current_scanner.openinputfile) then
  2274. Message1(scan_f_cannot_open_includefile,hs);
  2275. Message1(scan_t_start_include_file,current_scanner.inputfile.path+current_scanner.inputfile.name);
  2276. current_scanner.reload;
  2277. end
  2278. else
  2279. Message(scan_f_include_deep_ten);
  2280. end;
  2281. end;
  2282. {*****************************************************************************
  2283. Preprocessor writing
  2284. *****************************************************************************}
  2285. {$ifdef PREPROCWRITE}
  2286. constructor tpreprocfile.create(const fn:string);
  2287. begin
  2288. { open outputfile }
  2289. assign(f,fn);
  2290. {$push}{$I-}
  2291. rewrite(f);
  2292. {$pop}
  2293. if ioresult<>0 then
  2294. Comment(V_Fatal,'can''t create file '+fn);
  2295. getmem(buf,preprocbufsize);
  2296. settextbuf(f,buf^,preprocbufsize);
  2297. { reset }
  2298. eolfound:=false;
  2299. spacefound:=false;
  2300. end;
  2301. destructor tpreprocfile.destroy;
  2302. begin
  2303. close(f);
  2304. freemem(buf,preprocbufsize);
  2305. end;
  2306. procedure tpreprocfile.add(const s:string);
  2307. begin
  2308. write(f,s);
  2309. end;
  2310. procedure tpreprocfile.addspace;
  2311. begin
  2312. if eolfound then
  2313. begin
  2314. writeln(f,'');
  2315. eolfound:=false;
  2316. spacefound:=false;
  2317. end
  2318. else
  2319. if spacefound then
  2320. begin
  2321. write(f,' ');
  2322. spacefound:=false;
  2323. end;
  2324. end;
  2325. {$endif PREPROCWRITE}
  2326. {*****************************************************************************
  2327. TPreProcStack
  2328. *****************************************************************************}
  2329. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  2330. begin
  2331. accept:=a;
  2332. typ:=atyp;
  2333. next:=n;
  2334. end;
  2335. {*****************************************************************************
  2336. TReplayStack
  2337. *****************************************************************************}
  2338. constructor treplaystack.Create(atoken:ttoken;asettings:tsettings;
  2339. atokenbuf:tdynamicarray;anext:treplaystack);
  2340. begin
  2341. token:=atoken;
  2342. settings:=asettings;
  2343. tokenbuf:=atokenbuf;
  2344. next:=anext;
  2345. end;
  2346. {*****************************************************************************
  2347. TDirectiveItem
  2348. *****************************************************************************}
  2349. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2350. begin
  2351. inherited Create(AList,n);
  2352. is_conditional:=false;
  2353. proc:=p;
  2354. end;
  2355. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2356. begin
  2357. inherited Create(AList,n);
  2358. is_conditional:=true;
  2359. proc:=p;
  2360. end;
  2361. {****************************************************************************
  2362. TSCANNERFILE
  2363. ****************************************************************************}
  2364. constructor tscannerfile.create(const fn:string; is_macro: boolean = false);
  2365. begin
  2366. inputfile:=do_openinputfile(fn);
  2367. if is_macro then
  2368. inputfile.is_macro:=true;
  2369. if assigned(current_module) then
  2370. current_module.sourcefiles.register_file(inputfile);
  2371. { reset localinput }
  2372. c:=#0;
  2373. inputbuffer:=nil;
  2374. inputpointer:=nil;
  2375. inputstart:=0;
  2376. { reset scanner }
  2377. preprocstack:=nil;
  2378. replaystack:=nil;
  2379. comment_level:=0;
  2380. yylexcount:=0;
  2381. block_type:=bt_general;
  2382. line_no:=0;
  2383. lastlinepos:=0;
  2384. lasttokenpos:=0;
  2385. nexttokenpos:=0;
  2386. lasttoken:=NOTOKEN;
  2387. nexttoken:=NOTOKEN;
  2388. lastasmgetchar:=#0;
  2389. ignoredirectives:=TFPHashList.Create;
  2390. in_asm_string:=false;
  2391. end;
  2392. procedure tscannerfile.firstfile;
  2393. begin
  2394. { load block }
  2395. if not openinputfile then
  2396. Message1(scan_f_cannot_open_input,inputfile.name);
  2397. reload;
  2398. end;
  2399. destructor tscannerfile.destroy;
  2400. begin
  2401. if assigned(current_module) and
  2402. (current_module.state=ms_compiled) and
  2403. (status.errorcount=0) then
  2404. checkpreprocstack
  2405. else
  2406. begin
  2407. while assigned(preprocstack) do
  2408. poppreprocstack;
  2409. end;
  2410. while assigned(replaystack) do
  2411. popreplaystack;
  2412. if not inputfile.closed then
  2413. closeinputfile;
  2414. if inputfile.is_macro then
  2415. inputfile.free;
  2416. ignoredirectives.free;
  2417. end;
  2418. function tscannerfile.openinputfile:boolean;
  2419. begin
  2420. openinputfile:=inputfile.open;
  2421. { load buffer }
  2422. inputbuffer:=inputfile.buf;
  2423. inputpointer:=inputfile.buf;
  2424. inputstart:=inputfile.bufstart;
  2425. { line }
  2426. line_no:=0;
  2427. lastlinepos:=0;
  2428. lasttokenpos:=0;
  2429. nexttokenpos:=0;
  2430. end;
  2431. procedure tscannerfile.closeinputfile;
  2432. begin
  2433. inputfile.close;
  2434. { reset buffer }
  2435. inputbuffer:=nil;
  2436. inputpointer:=nil;
  2437. inputstart:=0;
  2438. { reset line }
  2439. line_no:=0;
  2440. lastlinepos:=0;
  2441. lasttokenpos:=0;
  2442. nexttokenpos:=0;
  2443. end;
  2444. function tscannerfile.tempopeninputfile:boolean;
  2445. begin
  2446. tempopeninputfile:=false;
  2447. if inputfile.is_macro then
  2448. exit;
  2449. tempopeninputfile:=inputfile.tempopen;
  2450. { reload buffer }
  2451. inputbuffer:=inputfile.buf;
  2452. inputpointer:=inputfile.buf;
  2453. inputstart:=inputfile.bufstart;
  2454. end;
  2455. procedure tscannerfile.tempcloseinputfile;
  2456. begin
  2457. if inputfile.closed or inputfile.is_macro then
  2458. exit;
  2459. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  2460. inputfile.tempclose;
  2461. { reset buffer }
  2462. inputbuffer:=nil;
  2463. inputpointer:=nil;
  2464. inputstart:=0;
  2465. end;
  2466. procedure tscannerfile.saveinputfile;
  2467. begin
  2468. inputfile.saveinputpointer:=inputpointer;
  2469. inputfile.savelastlinepos:=lastlinepos;
  2470. inputfile.saveline_no:=line_no;
  2471. end;
  2472. procedure tscannerfile.restoreinputfile;
  2473. begin
  2474. inputbuffer:=inputfile.buf;
  2475. inputpointer:=inputfile.saveinputpointer;
  2476. lastlinepos:=inputfile.savelastlinepos;
  2477. line_no:=inputfile.saveline_no;
  2478. if not inputfile.is_macro then
  2479. parser_current_file:=inputfile.name;
  2480. end;
  2481. procedure tscannerfile.nextfile;
  2482. var
  2483. to_dispose : tinputfile;
  2484. begin
  2485. if assigned(inputfile.next) then
  2486. begin
  2487. if inputfile.is_macro then
  2488. to_dispose:=inputfile
  2489. else
  2490. begin
  2491. to_dispose:=nil;
  2492. dec(inputfilecount);
  2493. end;
  2494. { we can allways close the file, no ? }
  2495. inputfile.close;
  2496. inputfile:=inputfile.next;
  2497. if assigned(to_dispose) then
  2498. to_dispose.free;
  2499. restoreinputfile;
  2500. end;
  2501. end;
  2502. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  2503. begin
  2504. if not assigned(buf) then
  2505. internalerror(200511172);
  2506. if assigned(recordtokenbuf) then
  2507. internalerror(200511173);
  2508. recordtokenbuf:=buf;
  2509. fillchar(last_settings,sizeof(last_settings),0);
  2510. last_message:=nil;
  2511. fillchar(last_filepos,sizeof(last_filepos),0);
  2512. end;
  2513. procedure tscannerfile.stoprecordtokens;
  2514. begin
  2515. if not assigned(recordtokenbuf) then
  2516. internalerror(200511174);
  2517. recordtokenbuf:=nil;
  2518. end;
  2519. procedure tscannerfile.writetoken(t : ttoken);
  2520. var
  2521. b : byte;
  2522. begin
  2523. if ord(t)>$7f then
  2524. begin
  2525. b:=(ord(t) shr 8) or $80;
  2526. recordtokenbuf.write(b,1);
  2527. end;
  2528. b:=ord(t) and $ff;
  2529. recordtokenbuf.write(b,1);
  2530. end;
  2531. procedure tscannerfile.tokenwritesizeint(val : asizeint);
  2532. begin
  2533. {$ifdef FPC_BIG_ENDIAN}
  2534. val:=swapendian(val);
  2535. {$endif}
  2536. recordtokenbuf.write(val,sizeof(asizeint));
  2537. end;
  2538. procedure tscannerfile.tokenwritelongint(val : longint);
  2539. begin
  2540. {$ifdef FPC_BIG_ENDIAN}
  2541. val:=swapendian(val);
  2542. {$endif}
  2543. recordtokenbuf.write(val,sizeof(longint));
  2544. end;
  2545. procedure tscannerfile.tokenwriteshortint(val : shortint);
  2546. begin
  2547. recordtokenbuf.write(val,sizeof(shortint));
  2548. end;
  2549. procedure tscannerfile.tokenwriteword(val : word);
  2550. begin
  2551. {$ifdef FPC_BIG_ENDIAN}
  2552. val:=swapendian(val);
  2553. {$endif}
  2554. recordtokenbuf.write(val,sizeof(word));
  2555. end;
  2556. procedure tscannerfile.tokenwritelongword(val : longword);
  2557. begin
  2558. {$ifdef FPC_BIG_ENDIAN}
  2559. val:=swapendian(val);
  2560. {$endif}
  2561. recordtokenbuf.write(val,sizeof(longword));
  2562. end;
  2563. function tscannerfile.tokenreadsizeint : asizeint;
  2564. var
  2565. val : asizeint;
  2566. begin
  2567. replaytokenbuf.read(val,sizeof(asizeint));
  2568. {$ifdef FPC_BIG_ENDIAN}
  2569. val:=swapendian(val);
  2570. {$endif}
  2571. result:=val;
  2572. end;
  2573. function tscannerfile.tokenreadlongword : longword;
  2574. var
  2575. val : longword;
  2576. begin
  2577. replaytokenbuf.read(val,sizeof(longword));
  2578. {$ifdef FPC_BIG_ENDIAN}
  2579. val:=swapendian(val);
  2580. {$endif}
  2581. result:=val;
  2582. end;
  2583. function tscannerfile.tokenreadlongint : longint;
  2584. var
  2585. val : longint;
  2586. begin
  2587. replaytokenbuf.read(val,sizeof(longint));
  2588. {$ifdef FPC_BIG_ENDIAN}
  2589. val:=swapendian(val);
  2590. {$endif}
  2591. result:=val;
  2592. end;
  2593. function tscannerfile.tokenreadshortint : shortint;
  2594. var
  2595. val : shortint;
  2596. begin
  2597. replaytokenbuf.read(val,sizeof(shortint));
  2598. result:=val;
  2599. end;
  2600. function tscannerfile.tokenreadbyte : byte;
  2601. var
  2602. val : byte;
  2603. begin
  2604. replaytokenbuf.read(val,sizeof(byte));
  2605. result:=val;
  2606. end;
  2607. function tscannerfile.tokenreadsmallint : smallint;
  2608. var
  2609. val : smallint;
  2610. begin
  2611. replaytokenbuf.read(val,sizeof(smallint));
  2612. {$ifdef FPC_BIG_ENDIAN}
  2613. val:=swapendian(val);
  2614. {$endif}
  2615. result:=val;
  2616. end;
  2617. function tscannerfile.tokenreadword : word;
  2618. var
  2619. val : word;
  2620. begin
  2621. replaytokenbuf.read(val,sizeof(word));
  2622. {$ifdef FPC_BIG_ENDIAN}
  2623. val:=swapendian(val);
  2624. {$endif}
  2625. result:=val;
  2626. end;
  2627. function tscannerfile.tokenreadenum(size : longint) : longword;
  2628. begin
  2629. if size=1 then
  2630. result:=tokenreadbyte
  2631. else if size=2 then
  2632. result:=tokenreadword
  2633. else if size=4 then
  2634. result:=tokenreadlongword
  2635. else
  2636. internalerror(2013112901);
  2637. end;
  2638. procedure tscannerfile.tokenreadset(var b;size : longint);
  2639. {$ifdef FPC_BIG_ENDIAN}
  2640. var
  2641. i : longint;
  2642. {$endif}
  2643. begin
  2644. replaytokenbuf.read(b,size);
  2645. {$ifdef FPC_BIG_ENDIAN}
  2646. for i:=0 to size-1 do
  2647. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  2648. {$endif}
  2649. end;
  2650. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  2651. begin
  2652. recordtokenbuf.write(b,size);
  2653. end;
  2654. procedure tscannerfile.tokenwriteset(var b;size : longint);
  2655. {$ifdef FPC_BIG_ENDIAN}
  2656. var
  2657. i: longint;
  2658. tmpset: array[0..31] of byte;
  2659. {$endif}
  2660. begin
  2661. {$ifdef FPC_BIG_ENDIAN}
  2662. { satisfy DFA because it assumes that size may be 0 and doesn't know that
  2663. recordtokenbuf.write wouldn't use tmpset in that case }
  2664. tmpset[0]:=0;
  2665. for i:=0 to size-1 do
  2666. tmpset[i]:=reverse_byte(Pbyte(@b)[i]);
  2667. recordtokenbuf.write(tmpset,size);
  2668. {$else}
  2669. recordtokenbuf.write(b,size);
  2670. {$endif}
  2671. end;
  2672. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  2673. { This procedure
  2674. needs to be changed whenever
  2675. globals.tsettings type is changed,
  2676. the problem is that no error will appear
  2677. before tests with generics are tested. PM }
  2678. var
  2679. startpos, endpos : longword;
  2680. begin
  2681. { WARNING all those fields need to be in the correct
  2682. order otherwise cross_endian PPU reading will fail }
  2683. startpos:=replaytokenbuf.pos;
  2684. with asettings do
  2685. begin
  2686. alignment.procalign:=tokenreadlongint;
  2687. alignment.loopalign:=tokenreadlongint;
  2688. alignment.jumpalign:=tokenreadlongint;
  2689. alignment.constalignmin:=tokenreadlongint;
  2690. alignment.constalignmax:=tokenreadlongint;
  2691. alignment.varalignmin:=tokenreadlongint;
  2692. alignment.varalignmax:=tokenreadlongint;
  2693. alignment.localalignmin:=tokenreadlongint;
  2694. alignment.localalignmax:=tokenreadlongint;
  2695. alignment.recordalignmin:=tokenreadlongint;
  2696. alignment.recordalignmax:=tokenreadlongint;
  2697. alignment.maxCrecordalign:=tokenreadlongint;
  2698. tokenreadset(globalswitches,sizeof(globalswitches));
  2699. tokenreadset(targetswitches,sizeof(targetswitches));
  2700. tokenreadset(moduleswitches,sizeof(moduleswitches));
  2701. tokenreadset(localswitches,sizeof(localswitches));
  2702. tokenreadset(modeswitches,sizeof(modeswitches));
  2703. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  2704. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2705. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2706. tokenreadset(debugswitches,sizeof(debugswitches));
  2707. { 0: old behaviour for sets <=256 elements
  2708. >0: round to this size }
  2709. setalloc:=tokenreadshortint;
  2710. packenum:=tokenreadshortint;
  2711. packrecords:=tokenreadshortint;
  2712. maxfpuregisters:=tokenreadshortint;
  2713. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2714. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2715. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  2716. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  2717. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  2718. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  2719. { tstringencoding is word type,
  2720. thus this should be OK here }
  2721. sourcecodepage:=tstringEncoding(tokenreadword);
  2722. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  2723. disabledircache:=boolean(tokenreadbyte);
  2724. { TH: Since the field was conditional originally, it was not stored in PPUs. }
  2725. { While adding ControllerSupport constant, I decided not to store ct_none }
  2726. { on targets not supporting controllers, but this might be changed here and }
  2727. { in tokenwritesettings in the future to unify the PPU structure and handling }
  2728. { of this field in the compiler. }
  2729. {$PUSH}
  2730. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  2731. if ControllerSupport then
  2732. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)))
  2733. else
  2734. ControllerType:=ct_none;
  2735. {$POP}
  2736. endpos:=replaytokenbuf.pos;
  2737. if endpos-startpos<>expected_size then
  2738. Comment(V_Error,'Wrong size of Settings read-in');
  2739. end;
  2740. end;
  2741. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  2742. { This procedure
  2743. needs to be changed whenever
  2744. globals.tsettings type is changed,
  2745. the problem is that no error will appear
  2746. before tests with generics are tested. PM }
  2747. var
  2748. sizepos, startpos, endpos : longword;
  2749. begin
  2750. { WARNING all those fields need to be in the correct
  2751. order otherwise cross_endian PPU reading will fail }
  2752. sizepos:=recordtokenbuf.pos;
  2753. size:=0;
  2754. tokenwritesizeint(size);
  2755. startpos:=recordtokenbuf.pos;
  2756. with asettings do
  2757. begin
  2758. tokenwritelongint(alignment.procalign);
  2759. tokenwritelongint(alignment.loopalign);
  2760. tokenwritelongint(alignment.jumpalign);
  2761. tokenwritelongint(alignment.constalignmin);
  2762. tokenwritelongint(alignment.constalignmax);
  2763. tokenwritelongint(alignment.varalignmin);
  2764. tokenwritelongint(alignment.varalignmax);
  2765. tokenwritelongint(alignment.localalignmin);
  2766. tokenwritelongint(alignment.localalignmax);
  2767. tokenwritelongint(alignment.recordalignmin);
  2768. tokenwritelongint(alignment.recordalignmax);
  2769. tokenwritelongint(alignment.maxCrecordalign);
  2770. tokenwriteset(globalswitches,sizeof(globalswitches));
  2771. tokenwriteset(targetswitches,sizeof(targetswitches));
  2772. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  2773. tokenwriteset(localswitches,sizeof(localswitches));
  2774. tokenwriteset(modeswitches,sizeof(modeswitches));
  2775. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  2776. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2777. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2778. tokenwriteset(debugswitches,sizeof(debugswitches));
  2779. { 0: old behaviour for sets <=256 elements
  2780. >0: round to this size }
  2781. tokenwriteshortint(setalloc);
  2782. tokenwriteshortint(packenum);
  2783. tokenwriteshortint(packrecords);
  2784. tokenwriteshortint(maxfpuregisters);
  2785. tokenwriteenum(cputype,sizeof(tcputype));
  2786. tokenwriteenum(optimizecputype,sizeof(tcputype));
  2787. tokenwriteenum(fputype,sizeof(tfputype));
  2788. tokenwriteenum(asmmode,sizeof(tasmmode));
  2789. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  2790. tokenwriteenum(defproccall,sizeof(tproccalloption));
  2791. { tstringencoding is word type,
  2792. thus this should be OK here }
  2793. tokenwriteword(sourcecodepage);
  2794. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  2795. recordtokenbuf.write(byte(disabledircache),1);
  2796. { TH: See note about controllertype field in tokenreadsettings. }
  2797. {$PUSH}
  2798. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  2799. if ControllerSupport then
  2800. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  2801. {$POP}
  2802. endpos:=recordtokenbuf.pos;
  2803. size:=endpos-startpos;
  2804. recordtokenbuf.seek(sizepos);
  2805. tokenwritesizeint(size);
  2806. recordtokenbuf.seek(endpos);
  2807. end;
  2808. end;
  2809. procedure tscannerfile.recordtoken;
  2810. var
  2811. t : ttoken;
  2812. s : tspecialgenerictoken;
  2813. len,msgnb,copy_size : asizeint;
  2814. val : longint;
  2815. b : byte;
  2816. pmsg : pmessagestaterecord;
  2817. begin
  2818. if not assigned(recordtokenbuf) then
  2819. internalerror(200511176);
  2820. t:=_GENERICSPECIALTOKEN;
  2821. { settings changed? }
  2822. { last field pmessage is handled separately below in
  2823. ST_LOADMESSAGES }
  2824. if CompareByte(current_settings,last_settings,
  2825. sizeof(current_settings)-sizeof(pointer))<>0 then
  2826. begin
  2827. { use a special token to record it }
  2828. s:=ST_LOADSETTINGS;
  2829. writetoken(t);
  2830. recordtokenbuf.write(s,1);
  2831. copy_size:=sizeof(current_settings)-sizeof(pointer);
  2832. tokenwritesettings(current_settings,copy_size);
  2833. last_settings:=current_settings;
  2834. end;
  2835. if current_settings.pmessage<>last_message then
  2836. begin
  2837. { use a special token to record it }
  2838. s:=ST_LOADMESSAGES;
  2839. writetoken(t);
  2840. recordtokenbuf.write(s,1);
  2841. msgnb:=0;
  2842. pmsg:=current_settings.pmessage;
  2843. while assigned(pmsg) do
  2844. begin
  2845. if msgnb=high(asizeint) then
  2846. { Too many messages }
  2847. internalerror(2011090401);
  2848. inc(msgnb);
  2849. pmsg:=pmsg^.next;
  2850. end;
  2851. tokenwritesizeint(msgnb);
  2852. pmsg:=current_settings.pmessage;
  2853. while assigned(pmsg) do
  2854. begin
  2855. { What about endianess here?}
  2856. { SB: this is handled by tokenreadlongint }
  2857. val:=pmsg^.value;
  2858. tokenwritelongint(val);
  2859. val:=ord(pmsg^.state);
  2860. tokenwritelongint(val);
  2861. pmsg:=pmsg^.next;
  2862. end;
  2863. last_message:=current_settings.pmessage;
  2864. end;
  2865. { file pos changes? }
  2866. if current_tokenpos.line<>last_filepos.line then
  2867. begin
  2868. s:=ST_LINE;
  2869. writetoken(t);
  2870. recordtokenbuf.write(s,1);
  2871. tokenwritelongint(current_tokenpos.line);
  2872. last_filepos.line:=current_tokenpos.line;
  2873. end;
  2874. if current_tokenpos.column<>last_filepos.column then
  2875. begin
  2876. s:=ST_COLUMN;
  2877. writetoken(t);
  2878. { can the column be written packed? }
  2879. if current_tokenpos.column<$80 then
  2880. begin
  2881. b:=$80 or current_tokenpos.column;
  2882. recordtokenbuf.write(b,1);
  2883. end
  2884. else
  2885. begin
  2886. recordtokenbuf.write(s,1);
  2887. tokenwriteword(current_tokenpos.column);
  2888. end;
  2889. last_filepos.column:=current_tokenpos.column;
  2890. end;
  2891. if current_tokenpos.fileindex<>last_filepos.fileindex then
  2892. begin
  2893. s:=ST_FILEINDEX;
  2894. writetoken(t);
  2895. recordtokenbuf.write(s,1);
  2896. tokenwriteword(current_tokenpos.fileindex);
  2897. last_filepos.fileindex:=current_tokenpos.fileindex;
  2898. end;
  2899. writetoken(token);
  2900. if token<>_GENERICSPECIALTOKEN then
  2901. writetoken(idtoken);
  2902. case token of
  2903. _CWCHAR,
  2904. _CWSTRING :
  2905. begin
  2906. tokenwritesizeint(patternw^.len);
  2907. if patternw^.len>0 then
  2908. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2909. end;
  2910. _CSTRING:
  2911. begin
  2912. len:=length(cstringpattern);
  2913. tokenwritesizeint(len);
  2914. if len>0 then
  2915. recordtokenbuf.write(cstringpattern[1],len);
  2916. end;
  2917. _CCHAR,
  2918. _INTCONST,
  2919. _REALNUMBER :
  2920. begin
  2921. { pexpr.pas messes with pattern in case of negative integer consts,
  2922. see around line 2562 the comment of JM; remove the - before recording it
  2923. (FK)
  2924. }
  2925. if (token=_INTCONST) and (pattern[1]='-') then
  2926. delete(pattern,1,1);
  2927. recordtokenbuf.write(pattern[0],1);
  2928. recordtokenbuf.write(pattern[1],length(pattern));
  2929. end;
  2930. _ID :
  2931. begin
  2932. recordtokenbuf.write(orgpattern[0],1);
  2933. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  2934. end;
  2935. end;
  2936. end;
  2937. procedure tscannerfile.startreplaytokens(buf:tdynamicarray);
  2938. begin
  2939. if not assigned(buf) then
  2940. internalerror(200511175);
  2941. { save current token }
  2942. if token in [_CWCHAR,_CWSTRING,_CCHAR,_CSTRING,_INTCONST,_REALNUMBER,_ID] then
  2943. internalerror(200511178);
  2944. replaystack:=treplaystack.create(token,current_settings,
  2945. replaytokenbuf,replaystack);
  2946. if assigned(inputpointer) then
  2947. dec(inputpointer);
  2948. { install buffer }
  2949. replaytokenbuf:=buf;
  2950. { reload next token }
  2951. replaytokenbuf.seek(0);
  2952. replaytoken;
  2953. end;
  2954. function tscannerfile.readtoken: ttoken;
  2955. var
  2956. b,b2 : byte;
  2957. begin
  2958. replaytokenbuf.read(b,1);
  2959. if (b and $80)<>0 then
  2960. begin
  2961. replaytokenbuf.read(b2,1);
  2962. result:=ttoken(((b and $7f) shl 8) or b2);
  2963. end
  2964. else
  2965. result:=ttoken(b);
  2966. end;
  2967. procedure tscannerfile.replaytoken;
  2968. var
  2969. wlen,mesgnb,copy_size : asizeint;
  2970. specialtoken : tspecialgenerictoken;
  2971. i : byte;
  2972. pmsg,prevmsg : pmessagestaterecord;
  2973. begin
  2974. if not assigned(replaytokenbuf) then
  2975. internalerror(200511177);
  2976. { End of replay buffer? Then load the next char from the file again }
  2977. if replaytokenbuf.pos>=replaytokenbuf.size then
  2978. begin
  2979. token:=replaystack.token;
  2980. replaytokenbuf:=replaystack.tokenbuf;
  2981. { restore compiler settings }
  2982. current_settings:=replaystack.settings;
  2983. popreplaystack;
  2984. if assigned(inputpointer) then
  2985. begin
  2986. c:=inputpointer^;
  2987. inc(inputpointer);
  2988. end;
  2989. exit;
  2990. end;
  2991. repeat
  2992. { load token from the buffer }
  2993. token:=readtoken;
  2994. if token<>_GENERICSPECIALTOKEN then
  2995. idtoken:=readtoken
  2996. else
  2997. idtoken:=_NOID;
  2998. case token of
  2999. _CWCHAR,
  3000. _CWSTRING :
  3001. begin
  3002. wlen:=tokenreadsizeint;
  3003. setlengthwidestring(patternw,wlen);
  3004. if wlen>0 then
  3005. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3006. orgpattern:='';
  3007. pattern:='';
  3008. cstringpattern:='';
  3009. end;
  3010. _CSTRING:
  3011. begin
  3012. wlen:=tokenreadsizeint;
  3013. if wlen>0 then
  3014. begin
  3015. setlength(cstringpattern,wlen);
  3016. replaytokenbuf.read(cstringpattern[1],wlen);
  3017. end
  3018. else
  3019. cstringpattern:='';
  3020. orgpattern:='';
  3021. pattern:='';
  3022. end;
  3023. _CCHAR,
  3024. _INTCONST,
  3025. _REALNUMBER :
  3026. begin
  3027. replaytokenbuf.read(pattern[0],1);
  3028. replaytokenbuf.read(pattern[1],length(pattern));
  3029. orgpattern:='';
  3030. end;
  3031. _ID :
  3032. begin
  3033. replaytokenbuf.read(orgpattern[0],1);
  3034. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  3035. pattern:=upper(orgpattern);
  3036. end;
  3037. _GENERICSPECIALTOKEN:
  3038. begin
  3039. replaytokenbuf.read(specialtoken,1);
  3040. { packed column? }
  3041. if (ord(specialtoken) and $80)<>0 then
  3042. begin
  3043. current_tokenpos.column:=ord(specialtoken) and $7f;
  3044. current_filepos:=current_tokenpos;
  3045. end
  3046. else
  3047. case specialtoken of
  3048. ST_LOADSETTINGS:
  3049. begin
  3050. copy_size:=tokenreadsizeint;
  3051. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  3052. // internalerror(2011090501);
  3053. {
  3054. replaytokenbuf.read(current_settings,copy_size);
  3055. }
  3056. tokenreadsettings(current_settings,copy_size);
  3057. end;
  3058. ST_LOADMESSAGES:
  3059. begin
  3060. current_settings.pmessage:=nil;
  3061. mesgnb:=tokenreadsizeint;
  3062. prevmsg:=nil;
  3063. for i:=1 to mesgnb do
  3064. begin
  3065. new(pmsg);
  3066. if i=1 then
  3067. current_settings.pmessage:=pmsg
  3068. else
  3069. prevmsg^.next:=pmsg;
  3070. pmsg^.value:=tokenreadlongint;
  3071. pmsg^.state:=tmsgstate(tokenreadlongint);
  3072. pmsg^.next:=nil;
  3073. prevmsg:=pmsg;
  3074. end;
  3075. end;
  3076. ST_LINE:
  3077. begin
  3078. current_tokenpos.line:=tokenreadlongint;
  3079. current_filepos:=current_tokenpos;
  3080. end;
  3081. ST_COLUMN:
  3082. begin
  3083. current_tokenpos.column:=tokenreadword;
  3084. current_filepos:=current_tokenpos;
  3085. end;
  3086. ST_FILEINDEX:
  3087. begin
  3088. current_tokenpos.fileindex:=tokenreadword;
  3089. current_filepos:=current_tokenpos;
  3090. end;
  3091. else
  3092. internalerror(2006103010);
  3093. end;
  3094. continue;
  3095. end;
  3096. end;
  3097. break;
  3098. until false;
  3099. end;
  3100. procedure tscannerfile.addfile(hp:tinputfile);
  3101. begin
  3102. saveinputfile;
  3103. { add to list }
  3104. hp.next:=inputfile;
  3105. inputfile:=hp;
  3106. { load new inputfile }
  3107. restoreinputfile;
  3108. end;
  3109. procedure tscannerfile.reload;
  3110. begin
  3111. with inputfile do
  3112. begin
  3113. { when nothing more to read then leave immediatly, so we
  3114. don't change the current_filepos and leave it point to the last
  3115. char }
  3116. if (c=#26) and (not assigned(next)) then
  3117. exit;
  3118. repeat
  3119. { still more to read?, then change the #0 to a space so its seen
  3120. as a seperator, this can't be used for macro's which can change
  3121. the place of the #0 in the buffer with tempopen }
  3122. if (c=#0) and (bufsize>0) and
  3123. not(inputfile.is_macro) and
  3124. (inputpointer-inputbuffer<bufsize) then
  3125. begin
  3126. c:=' ';
  3127. inc(inputpointer);
  3128. exit;
  3129. end;
  3130. { can we read more from this file ? }
  3131. if (c<>#26) and (not endoffile) then
  3132. begin
  3133. readbuf;
  3134. inputpointer:=buf;
  3135. inputbuffer:=buf;
  3136. inputstart:=bufstart;
  3137. { first line? }
  3138. if line_no=0 then
  3139. begin
  3140. c:=inputpointer^;
  3141. { eat utf-8 signature? }
  3142. if (ord(inputpointer^)=$ef) and
  3143. (ord((inputpointer+1)^)=$bb) and
  3144. (ord((inputpointer+2)^)=$bf) then
  3145. begin
  3146. (* we don't support including files with an UTF-8 bom
  3147. inside another file that wasn't encoded as UTF-8
  3148. already (we don't support {$codepage xxx} switches in
  3149. the middle of a file either) *)
  3150. if (current_settings.sourcecodepage<>CP_UTF8) and
  3151. not current_module.in_global then
  3152. Message(scanner_f_illegal_utf8_bom);
  3153. inc(inputpointer,3);
  3154. message(scan_c_switching_to_utf8);
  3155. current_settings.sourcecodepage:=CP_UTF8;
  3156. include(current_settings.moduleswitches,cs_explicit_codepage);
  3157. end;
  3158. line_no:=1;
  3159. if cs_asm_source in current_settings.globalswitches then
  3160. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3161. end;
  3162. end
  3163. else
  3164. begin
  3165. { load eof position in tokenpos/current_filepos }
  3166. gettokenpos;
  3167. { close file }
  3168. closeinputfile;
  3169. { no next module, than EOF }
  3170. if not assigned(inputfile.next) then
  3171. begin
  3172. c:=#26;
  3173. exit;
  3174. end;
  3175. { load next file and reopen it }
  3176. nextfile;
  3177. tempopeninputfile;
  3178. { status }
  3179. Message1(scan_t_back_in,inputfile.name);
  3180. end;
  3181. { load next char }
  3182. c:=inputpointer^;
  3183. inc(inputpointer);
  3184. until c<>#0; { if also end, then reload again }
  3185. end;
  3186. end;
  3187. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  3188. var
  3189. hp : tinputfile;
  3190. begin
  3191. { save old postion }
  3192. dec(inputpointer);
  3193. tempcloseinputfile;
  3194. { create macro 'file' }
  3195. { use special name to dispose after !! }
  3196. hp:=do_openinputfile('_Macro_.'+macname);
  3197. addfile(hp);
  3198. with inputfile do
  3199. begin
  3200. setmacro(p,len);
  3201. { local buffer }
  3202. inputbuffer:=buf;
  3203. inputpointer:=buf;
  3204. inputstart:=bufstart;
  3205. ref_index:=fileindex;
  3206. end;
  3207. { reset line }
  3208. line_no:=line;
  3209. lastlinepos:=0;
  3210. lasttokenpos:=0;
  3211. nexttokenpos:=0;
  3212. { load new c }
  3213. c:=inputpointer^;
  3214. inc(inputpointer);
  3215. end;
  3216. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3217. begin
  3218. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3219. filepos.line:=line_no;
  3220. filepos.column:=tokenpos-lastlinepos;
  3221. filepos.fileindex:=inputfile.ref_index;
  3222. filepos.moduleindex:=current_module.unit_index;
  3223. end;
  3224. procedure tscannerfile.gettokenpos;
  3225. { load the values of tokenpos and lasttokenpos }
  3226. begin
  3227. do_gettokenpos(lasttokenpos,current_tokenpos);
  3228. current_filepos:=current_tokenpos;
  3229. end;
  3230. procedure tscannerfile.cachenexttokenpos;
  3231. begin
  3232. do_gettokenpos(nexttokenpos,next_filepos);
  3233. end;
  3234. procedure tscannerfile.setnexttoken;
  3235. begin
  3236. token:=nexttoken;
  3237. nexttoken:=NOTOKEN;
  3238. lasttokenpos:=nexttokenpos;
  3239. current_tokenpos:=next_filepos;
  3240. current_filepos:=current_tokenpos;
  3241. nexttokenpos:=0;
  3242. end;
  3243. procedure tscannerfile.savetokenpos;
  3244. begin
  3245. oldlasttokenpos:=lasttokenpos;
  3246. oldcurrent_filepos:=current_filepos;
  3247. oldcurrent_tokenpos:=current_tokenpos;
  3248. end;
  3249. procedure tscannerfile.restoretokenpos;
  3250. begin
  3251. lasttokenpos:=oldlasttokenpos;
  3252. current_filepos:=oldcurrent_filepos;
  3253. current_tokenpos:=oldcurrent_tokenpos;
  3254. end;
  3255. procedure tscannerfile.inc_comment_level;
  3256. begin
  3257. if (m_nested_comment in current_settings.modeswitches) then
  3258. inc(comment_level)
  3259. else
  3260. comment_level:=1;
  3261. if (comment_level>1) then
  3262. begin
  3263. savetokenpos;
  3264. gettokenpos; { update for warning }
  3265. Message1(scan_w_comment_level,tostr(comment_level));
  3266. restoretokenpos;
  3267. end;
  3268. end;
  3269. procedure tscannerfile.dec_comment_level;
  3270. begin
  3271. if (m_nested_comment in current_settings.modeswitches) then
  3272. dec(comment_level)
  3273. else
  3274. comment_level:=0;
  3275. end;
  3276. procedure tscannerfile.linebreak;
  3277. var
  3278. cur : char;
  3279. begin
  3280. with inputfile do
  3281. begin
  3282. if (byte(inputpointer^)=0) and not(endoffile) then
  3283. begin
  3284. cur:=c;
  3285. reload;
  3286. if byte(cur)+byte(c)<>23 then
  3287. dec(inputpointer);
  3288. end
  3289. else
  3290. begin
  3291. { Support all combination of #10 and #13 as line break }
  3292. if (byte(inputpointer^)+byte(c)=23) then
  3293. inc(inputpointer);
  3294. end;
  3295. { Always return #10 as line break }
  3296. c:=#10;
  3297. { increase line counters }
  3298. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3299. inc(line_no);
  3300. { update linebuffer }
  3301. if cs_asm_source in current_settings.globalswitches then
  3302. inputfile.setline(line_no,lastlinepos);
  3303. { update for status and call the show status routine,
  3304. but don't touch current_filepos ! }
  3305. savetokenpos;
  3306. gettokenpos; { update for v_status }
  3307. inc(status.compiledlines);
  3308. ShowStatus;
  3309. restoretokenpos;
  3310. end;
  3311. end;
  3312. procedure tscannerfile.illegal_char(c:char);
  3313. var
  3314. s : string;
  3315. begin
  3316. if c in [#32..#255] then
  3317. s:=''''+c+''''
  3318. else
  3319. s:='#'+tostr(ord(c));
  3320. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3321. end;
  3322. procedure tscannerfile.end_of_file;
  3323. begin
  3324. checkpreprocstack;
  3325. Message(scan_f_end_of_file);
  3326. end;
  3327. {-------------------------------------------
  3328. IF Conditional Handling
  3329. -------------------------------------------}
  3330. procedure tscannerfile.checkpreprocstack;
  3331. begin
  3332. { check for missing ifdefs }
  3333. while assigned(preprocstack) do
  3334. begin
  3335. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3336. preprocstack.owner.inputfile.name,tostr(preprocstack.line_nb));
  3337. poppreprocstack;
  3338. end;
  3339. end;
  3340. procedure tscannerfile.poppreprocstack;
  3341. var
  3342. hp : tpreprocstack;
  3343. begin
  3344. if assigned(preprocstack) then
  3345. begin
  3346. Message1(scan_c_endif_found,preprocstack.name);
  3347. hp:=preprocstack.next;
  3348. preprocstack.free;
  3349. preprocstack:=hp;
  3350. end
  3351. else
  3352. Message(scan_e_endif_without_if);
  3353. end;
  3354. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  3355. var
  3356. condition: Boolean;
  3357. valuedescr: String;
  3358. begin
  3359. if (preprocstack=nil) or preprocstack.accept then
  3360. condition:=compile_time_predicate(valuedescr)
  3361. else
  3362. begin
  3363. condition:= false;
  3364. valuedescr:= '';
  3365. end;
  3366. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  3367. preprocstack.name:=valuedescr;
  3368. preprocstack.line_nb:=line_no;
  3369. preprocstack.owner:=self;
  3370. if preprocstack.accept then
  3371. Message2(messid,preprocstack.name,'accepted')
  3372. else
  3373. Message2(messid,preprocstack.name,'rejected');
  3374. end;
  3375. procedure tscannerfile.elsepreprocstack;
  3376. begin
  3377. if assigned(preprocstack) and
  3378. (preprocstack.typ<>pp_else) then
  3379. begin
  3380. if (preprocstack.typ=pp_elseif) then
  3381. preprocstack.accept:=false
  3382. else
  3383. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  3384. preprocstack.accept:=not preprocstack.accept;
  3385. preprocstack.typ:=pp_else;
  3386. preprocstack.line_nb:=line_no;
  3387. if preprocstack.accept then
  3388. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3389. else
  3390. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3391. end
  3392. else
  3393. Message(scan_e_endif_without_if);
  3394. end;
  3395. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  3396. var
  3397. valuedescr: String;
  3398. begin
  3399. if assigned(preprocstack) and
  3400. (preprocstack.typ in [pp_if,pp_elseif]) then
  3401. begin
  3402. { when the branch is accepted we use pp_elseif so we know that
  3403. all the next branches need to be rejected. when this branch is still
  3404. not accepted then leave it at pp_if }
  3405. if (preprocstack.typ=pp_elseif) then
  3406. preprocstack.accept:=false
  3407. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  3408. begin
  3409. preprocstack.accept:=false;
  3410. preprocstack.typ:=pp_elseif;
  3411. end
  3412. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  3413. and compile_time_predicate(valuedescr) then
  3414. begin
  3415. preprocstack.name:=valuedescr;
  3416. preprocstack.accept:=true;
  3417. preprocstack.typ:=pp_elseif;
  3418. end;
  3419. preprocstack.line_nb:=line_no;
  3420. if preprocstack.accept then
  3421. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3422. else
  3423. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3424. end
  3425. else
  3426. Message(scan_e_endif_without_if);
  3427. end;
  3428. procedure tscannerfile.popreplaystack;
  3429. var
  3430. hp : treplaystack;
  3431. begin
  3432. if assigned(replaystack) then
  3433. begin
  3434. hp:=replaystack.next;
  3435. replaystack.free;
  3436. replaystack:=hp;
  3437. end;
  3438. end;
  3439. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  3440. begin
  3441. savetokenpos;
  3442. repeat
  3443. current_scanner.gettokenpos;
  3444. Message1(scan_d_handling_switch,'$'+p.name);
  3445. p.proc();
  3446. { accept the text ? }
  3447. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  3448. break
  3449. else
  3450. begin
  3451. current_scanner.gettokenpos;
  3452. Message(scan_c_skipping_until);
  3453. repeat
  3454. current_scanner.skipuntildirective;
  3455. if not (m_mac in current_settings.modeswitches) then
  3456. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  3457. else
  3458. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  3459. until assigned(p) and (p.is_conditional);
  3460. current_scanner.gettokenpos;
  3461. end;
  3462. until false;
  3463. restoretokenpos;
  3464. end;
  3465. procedure tscannerfile.handledirectives;
  3466. var
  3467. t : tdirectiveitem;
  3468. hs : string;
  3469. begin
  3470. gettokenpos;
  3471. readchar; {Remove the $}
  3472. hs:=readid;
  3473. { handle empty directive }
  3474. if hs='' then
  3475. begin
  3476. Message1(scan_w_illegal_switch,'$');
  3477. exit;
  3478. end;
  3479. {$ifdef PREPROCWRITE}
  3480. if parapreprocess then
  3481. begin
  3482. t:=Get_Directive(hs);
  3483. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  3484. begin
  3485. preprocfile^.AddSpace;
  3486. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  3487. exit;
  3488. end;
  3489. end;
  3490. {$endif PREPROCWRITE}
  3491. { skip this directive? }
  3492. if (ignoredirectives.find(hs)<>nil) then
  3493. begin
  3494. if (comment_level>0) then
  3495. readcomment;
  3496. { we've read the whole comment }
  3497. aktcommentstyle:=comment_none;
  3498. exit;
  3499. end;
  3500. { Check for compiler switches }
  3501. while (length(hs)=1) and (c in ['-','+']) do
  3502. begin
  3503. Message1(scan_d_handling_switch,'$'+hs+c);
  3504. HandleSwitch(hs[1],c);
  3505. current_scanner.readchar; {Remove + or -}
  3506. if c=',' then
  3507. begin
  3508. current_scanner.readchar; {Remove , }
  3509. { read next switch, support $v+,$+}
  3510. hs:=current_scanner.readid;
  3511. if (hs='') then
  3512. begin
  3513. if (c='$') and (m_fpc in current_settings.modeswitches) then
  3514. begin
  3515. current_scanner.readchar; { skip $ }
  3516. hs:=current_scanner.readid;
  3517. end;
  3518. if (hs='') then
  3519. Message1(scan_w_illegal_directive,'$'+c);
  3520. end;
  3521. end
  3522. else
  3523. hs:='';
  3524. end;
  3525. { directives may follow switches after a , }
  3526. if hs<>'' then
  3527. begin
  3528. if not (m_mac in current_settings.modeswitches) then
  3529. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  3530. else
  3531. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  3532. if assigned(t) then
  3533. begin
  3534. if t.is_conditional then
  3535. handleconditional(t)
  3536. else
  3537. begin
  3538. Message1(scan_d_handling_switch,'$'+hs);
  3539. t.proc();
  3540. end;
  3541. end
  3542. else
  3543. begin
  3544. current_scanner.ignoredirectives.Add(hs,nil);
  3545. Message1(scan_w_illegal_directive,'$'+hs);
  3546. end;
  3547. { conditionals already read the comment }
  3548. if (current_scanner.comment_level>0) then
  3549. current_scanner.readcomment;
  3550. { we've read the whole comment }
  3551. aktcommentstyle:=comment_none;
  3552. end;
  3553. end;
  3554. procedure tscannerfile.readchar;
  3555. begin
  3556. c:=inputpointer^;
  3557. if c=#0 then
  3558. reload
  3559. else
  3560. inc(inputpointer);
  3561. end;
  3562. procedure tscannerfile.readstring;
  3563. var
  3564. i : longint;
  3565. err : boolean;
  3566. begin
  3567. err:=false;
  3568. i:=0;
  3569. repeat
  3570. case c of
  3571. '_',
  3572. '0'..'9',
  3573. 'A'..'Z' :
  3574. begin
  3575. if i<255 then
  3576. begin
  3577. inc(i);
  3578. orgpattern[i]:=c;
  3579. pattern[i]:=c;
  3580. end
  3581. else
  3582. begin
  3583. if not err then
  3584. begin
  3585. Message(scan_e_string_exceeds_255_chars);
  3586. err:=true;
  3587. end;
  3588. end;
  3589. c:=inputpointer^;
  3590. inc(inputpointer);
  3591. end;
  3592. 'a'..'z' :
  3593. begin
  3594. if i<255 then
  3595. begin
  3596. inc(i);
  3597. orgpattern[i]:=c;
  3598. pattern[i]:=chr(ord(c)-32)
  3599. end
  3600. else
  3601. begin
  3602. if not err then
  3603. begin
  3604. Message(scan_e_string_exceeds_255_chars);
  3605. err:=true;
  3606. end;
  3607. end;
  3608. c:=inputpointer^;
  3609. inc(inputpointer);
  3610. end;
  3611. #0 :
  3612. reload;
  3613. else
  3614. break;
  3615. end;
  3616. until false;
  3617. orgpattern[0]:=chr(i);
  3618. pattern[0]:=chr(i);
  3619. end;
  3620. procedure tscannerfile.readnumber;
  3621. var
  3622. base,
  3623. i : longint;
  3624. begin
  3625. case c of
  3626. '%' :
  3627. begin
  3628. readchar;
  3629. base:=2;
  3630. pattern[1]:='%';
  3631. i:=1;
  3632. end;
  3633. '&' :
  3634. begin
  3635. readchar;
  3636. base:=8;
  3637. pattern[1]:='&';
  3638. i:=1;
  3639. end;
  3640. '$' :
  3641. begin
  3642. readchar;
  3643. base:=16;
  3644. pattern[1]:='$';
  3645. i:=1;
  3646. end;
  3647. else
  3648. begin
  3649. base:=10;
  3650. i:=0;
  3651. end;
  3652. end;
  3653. while ((base>=10) and (c in ['0'..'9'])) or
  3654. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  3655. ((base=8) and (c in ['0'..'7'])) or
  3656. ((base=2) and (c in ['0'..'1'])) do
  3657. begin
  3658. if i<255 then
  3659. begin
  3660. inc(i);
  3661. pattern[i]:=c;
  3662. end;
  3663. readchar;
  3664. end;
  3665. pattern[0]:=chr(i);
  3666. end;
  3667. function tscannerfile.readid:string;
  3668. begin
  3669. readstring;
  3670. readid:=pattern;
  3671. end;
  3672. function tscannerfile.readval:longint;
  3673. var
  3674. l : longint;
  3675. w : integer;
  3676. begin
  3677. readnumber;
  3678. val(pattern,l,w);
  3679. readval:=l;
  3680. end;
  3681. function tscannerfile.readcomment:string;
  3682. var
  3683. i : longint;
  3684. begin
  3685. i:=0;
  3686. repeat
  3687. case c of
  3688. '{' :
  3689. begin
  3690. if aktcommentstyle=comment_tp then
  3691. inc_comment_level;
  3692. end;
  3693. '}' :
  3694. begin
  3695. if aktcommentstyle=comment_tp then
  3696. begin
  3697. readchar;
  3698. dec_comment_level;
  3699. if comment_level=0 then
  3700. break
  3701. else
  3702. continue;
  3703. end;
  3704. end;
  3705. '*' :
  3706. begin
  3707. if aktcommentstyle=comment_oldtp then
  3708. begin
  3709. readchar;
  3710. if c=')' then
  3711. begin
  3712. readchar;
  3713. dec_comment_level;
  3714. break;
  3715. end
  3716. else
  3717. { Add both characters !!}
  3718. if (i<255) then
  3719. begin
  3720. inc(i);
  3721. readcomment[i]:='*';
  3722. if (i<255) then
  3723. begin
  3724. inc(i);
  3725. readcomment[i]:=c;
  3726. end;
  3727. end;
  3728. end
  3729. else
  3730. { Not old TP comment, so add...}
  3731. begin
  3732. if (i<255) then
  3733. begin
  3734. inc(i);
  3735. readcomment[i]:='*';
  3736. end;
  3737. end;
  3738. end;
  3739. #10,#13 :
  3740. linebreak;
  3741. #26 :
  3742. end_of_file;
  3743. else
  3744. begin
  3745. if (i<255) then
  3746. begin
  3747. inc(i);
  3748. readcomment[i]:=c;
  3749. end;
  3750. end;
  3751. end;
  3752. readchar;
  3753. until false;
  3754. readcomment[0]:=chr(i);
  3755. end;
  3756. function tscannerfile.readquotedstring:string;
  3757. var
  3758. i : longint;
  3759. msgwritten : boolean;
  3760. begin
  3761. i:=0;
  3762. msgwritten:=false;
  3763. if (c='''') then
  3764. begin
  3765. repeat
  3766. readchar;
  3767. case c of
  3768. #26 :
  3769. end_of_file;
  3770. #10,#13 :
  3771. Message(scan_f_string_exceeds_line);
  3772. '''' :
  3773. begin
  3774. readchar;
  3775. if c<>'''' then
  3776. break;
  3777. end;
  3778. end;
  3779. if i<255 then
  3780. begin
  3781. inc(i);
  3782. result[i]:=c;
  3783. end
  3784. else
  3785. begin
  3786. if not msgwritten then
  3787. begin
  3788. Message(scan_e_string_exceeds_255_chars);
  3789. msgwritten:=true;
  3790. end;
  3791. end;
  3792. until false;
  3793. end;
  3794. result[0]:=chr(i);
  3795. end;
  3796. function tscannerfile.readstate:char;
  3797. var
  3798. state : char;
  3799. begin
  3800. state:=' ';
  3801. if c=' ' then
  3802. begin
  3803. current_scanner.skipspace;
  3804. current_scanner.readid;
  3805. if pattern='ON' then
  3806. state:='+'
  3807. else
  3808. if pattern='OFF' then
  3809. state:='-';
  3810. end
  3811. else
  3812. state:=c;
  3813. if not (state in ['+','-']) then
  3814. Message(scan_e_wrong_switch_toggle);
  3815. readstate:=state;
  3816. end;
  3817. function tscannerfile.readstatedefault:char;
  3818. var
  3819. state : char;
  3820. begin
  3821. state:=' ';
  3822. if c=' ' then
  3823. begin
  3824. current_scanner.skipspace;
  3825. current_scanner.readid;
  3826. if pattern='ON' then
  3827. state:='+'
  3828. else
  3829. if pattern='OFF' then
  3830. state:='-'
  3831. else
  3832. if pattern='DEFAULT' then
  3833. state:='*';
  3834. end
  3835. else
  3836. state:=c;
  3837. if not (state in ['+','-','*']) then
  3838. Message(scan_e_wrong_switch_toggle_default);
  3839. readstatedefault:=state;
  3840. end;
  3841. procedure tscannerfile.skipspace;
  3842. begin
  3843. repeat
  3844. case c of
  3845. #26 :
  3846. begin
  3847. reload;
  3848. if (c=#26) and not assigned(inputfile.next) then
  3849. break;
  3850. continue;
  3851. end;
  3852. #10,
  3853. #13 :
  3854. linebreak;
  3855. #9,#11,#12,' ' :
  3856. ;
  3857. else
  3858. break;
  3859. end;
  3860. readchar;
  3861. until false;
  3862. end;
  3863. procedure tscannerfile.skipuntildirective;
  3864. var
  3865. found : longint;
  3866. next_char_loaded : boolean;
  3867. begin
  3868. found:=0;
  3869. next_char_loaded:=false;
  3870. repeat
  3871. case c of
  3872. #10,
  3873. #13 :
  3874. linebreak;
  3875. #26 :
  3876. begin
  3877. reload;
  3878. if (c=#26) and not assigned(inputfile.next) then
  3879. end_of_file;
  3880. continue;
  3881. end;
  3882. '{' :
  3883. begin
  3884. if (aktcommentstyle in [comment_tp,comment_none]) then
  3885. begin
  3886. aktcommentstyle:=comment_tp;
  3887. if (comment_level=0) then
  3888. found:=1;
  3889. inc_comment_level;
  3890. end;
  3891. end;
  3892. '*' :
  3893. begin
  3894. if (aktcommentstyle=comment_oldtp) then
  3895. begin
  3896. readchar;
  3897. if c=')' then
  3898. begin
  3899. dec_comment_level;
  3900. found:=0;
  3901. aktcommentstyle:=comment_none;
  3902. end
  3903. else
  3904. next_char_loaded:=true;
  3905. end
  3906. else
  3907. found := 0;
  3908. end;
  3909. '}' :
  3910. begin
  3911. if (aktcommentstyle=comment_tp) then
  3912. begin
  3913. dec_comment_level;
  3914. if (comment_level=0) then
  3915. aktcommentstyle:=comment_none;
  3916. found:=0;
  3917. end;
  3918. end;
  3919. '$' :
  3920. begin
  3921. if found=1 then
  3922. found:=2;
  3923. end;
  3924. '''' :
  3925. if (aktcommentstyle=comment_none) then
  3926. begin
  3927. repeat
  3928. readchar;
  3929. case c of
  3930. #26 :
  3931. end_of_file;
  3932. #10,#13 :
  3933. break;
  3934. '''' :
  3935. begin
  3936. readchar;
  3937. if c<>'''' then
  3938. begin
  3939. next_char_loaded:=true;
  3940. break;
  3941. end;
  3942. end;
  3943. end;
  3944. until false;
  3945. end;
  3946. '(' :
  3947. begin
  3948. if (aktcommentstyle=comment_none) then
  3949. begin
  3950. readchar;
  3951. if c='*' then
  3952. begin
  3953. readchar;
  3954. if c='$' then
  3955. begin
  3956. found:=2;
  3957. inc_comment_level;
  3958. aktcommentstyle:=comment_oldtp;
  3959. end
  3960. else
  3961. begin
  3962. skipoldtpcomment;
  3963. next_char_loaded:=true;
  3964. end;
  3965. end
  3966. else
  3967. next_char_loaded:=true;
  3968. end
  3969. else
  3970. found:=0;
  3971. end;
  3972. '/' :
  3973. begin
  3974. if (aktcommentstyle=comment_none) then
  3975. begin
  3976. readchar;
  3977. if c='/' then
  3978. skipdelphicomment;
  3979. next_char_loaded:=true;
  3980. end
  3981. else
  3982. found:=0;
  3983. end;
  3984. else
  3985. found:=0;
  3986. end;
  3987. if next_char_loaded then
  3988. next_char_loaded:=false
  3989. else
  3990. readchar;
  3991. until (found=2);
  3992. end;
  3993. {****************************************************************************
  3994. Comment Handling
  3995. ****************************************************************************}
  3996. procedure tscannerfile.skipcomment;
  3997. begin
  3998. aktcommentstyle:=comment_tp;
  3999. readchar;
  4000. inc_comment_level;
  4001. { handle compiler switches }
  4002. if (c='$') then
  4003. handledirectives;
  4004. { handle_switches can dec comment_level, }
  4005. while (comment_level>0) do
  4006. begin
  4007. case c of
  4008. '{' :
  4009. inc_comment_level;
  4010. '}' :
  4011. dec_comment_level;
  4012. #10,#13 :
  4013. linebreak;
  4014. #26 :
  4015. begin
  4016. reload;
  4017. if (c=#26) and not assigned(inputfile.next) then
  4018. end_of_file;
  4019. continue;
  4020. end;
  4021. end;
  4022. readchar;
  4023. end;
  4024. aktcommentstyle:=comment_none;
  4025. end;
  4026. procedure tscannerfile.skipdelphicomment;
  4027. begin
  4028. aktcommentstyle:=comment_delphi;
  4029. inc_comment_level;
  4030. readchar;
  4031. { this is not supported }
  4032. if c='$' then
  4033. Message(scan_w_wrong_styled_switch);
  4034. { skip comment }
  4035. while not (c in [#10,#13,#26]) do
  4036. readchar;
  4037. dec_comment_level;
  4038. aktcommentstyle:=comment_none;
  4039. end;
  4040. procedure tscannerfile.skipoldtpcomment;
  4041. var
  4042. found : longint;
  4043. begin
  4044. aktcommentstyle:=comment_oldtp;
  4045. inc_comment_level;
  4046. { only load a char if last already processed,
  4047. was cause of bug1634 PM }
  4048. if c=#0 then
  4049. readchar;
  4050. { this is now supported }
  4051. if (c='$') then
  4052. handledirectives;
  4053. { skip comment }
  4054. while (comment_level>0) do
  4055. begin
  4056. found:=0;
  4057. repeat
  4058. case c of
  4059. #26 :
  4060. begin
  4061. reload;
  4062. if (c=#26) and not assigned(inputfile.next) then
  4063. end_of_file;
  4064. continue;
  4065. end;
  4066. #10,#13 :
  4067. begin
  4068. if found=4 then
  4069. inc_comment_level;
  4070. linebreak;
  4071. found:=0;
  4072. end;
  4073. '*' :
  4074. begin
  4075. if found=3 then
  4076. found:=4
  4077. else
  4078. found:=1;
  4079. end;
  4080. ')' :
  4081. begin
  4082. if found in [1,4] then
  4083. begin
  4084. dec_comment_level;
  4085. if comment_level=0 then
  4086. found:=2
  4087. else
  4088. found:=0;
  4089. end
  4090. else
  4091. found:=0;
  4092. end;
  4093. '(' :
  4094. begin
  4095. if found=4 then
  4096. inc_comment_level;
  4097. found:=3;
  4098. end;
  4099. else
  4100. begin
  4101. if found=4 then
  4102. inc_comment_level;
  4103. found:=0;
  4104. end;
  4105. end;
  4106. readchar;
  4107. until (found=2);
  4108. end;
  4109. aktcommentstyle:=comment_none;
  4110. end;
  4111. {****************************************************************************
  4112. Token Scanner
  4113. ****************************************************************************}
  4114. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  4115. var
  4116. code : integer;
  4117. len,
  4118. low,high,mid : longint;
  4119. w : word;
  4120. m : longint;
  4121. mac : tmacro;
  4122. asciinr : string[33];
  4123. iswidestring : boolean;
  4124. label
  4125. exit_label;
  4126. begin
  4127. flushpendingswitchesstate;
  4128. { record tokens? }
  4129. if allowrecordtoken and
  4130. assigned(recordtokenbuf) then
  4131. recordtoken;
  4132. { replay tokens? }
  4133. if assigned(replaytokenbuf) then
  4134. begin
  4135. replaytoken;
  4136. goto exit_label;
  4137. end;
  4138. { was there already a token read, then return that token }
  4139. if nexttoken<>NOTOKEN then
  4140. begin
  4141. setnexttoken;
  4142. goto exit_label;
  4143. end;
  4144. { Skip all spaces and comments }
  4145. repeat
  4146. case c of
  4147. '{' :
  4148. skipcomment;
  4149. #26 :
  4150. begin
  4151. reload;
  4152. if (c=#26) and not assigned(inputfile.next) then
  4153. break;
  4154. end;
  4155. ' ',#9..#13 :
  4156. begin
  4157. {$ifdef PREPROCWRITE}
  4158. if parapreprocess then
  4159. begin
  4160. if c=#10 then
  4161. preprocfile.eolfound:=true
  4162. else
  4163. preprocfile.spacefound:=true;
  4164. end;
  4165. {$endif PREPROCWRITE}
  4166. skipspace;
  4167. end
  4168. else
  4169. break;
  4170. end;
  4171. until false;
  4172. { Save current token position, for EOF its already loaded }
  4173. if c<>#26 then
  4174. gettokenpos;
  4175. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  4176. if c in ['A'..'Z','a'..'z','_'] then
  4177. begin
  4178. readstring;
  4179. token:=_ID;
  4180. idtoken:=_ID;
  4181. { keyword or any other known token,
  4182. pattern is always uppercased }
  4183. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4184. begin
  4185. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4186. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4187. while low<high do
  4188. begin
  4189. mid:=(high+low+1) shr 1;
  4190. if pattern<tokeninfo^[ttoken(mid)].str then
  4191. high:=mid-1
  4192. else
  4193. low:=mid;
  4194. end;
  4195. with tokeninfo^[ttoken(high)] do
  4196. if pattern=str then
  4197. begin
  4198. if (keyword*current_settings.modeswitches)<>[] then
  4199. if op=NOTOKEN then
  4200. token:=ttoken(high)
  4201. else
  4202. token:=op;
  4203. idtoken:=ttoken(high);
  4204. end;
  4205. end;
  4206. { Only process identifiers and not keywords }
  4207. if token=_ID then
  4208. begin
  4209. { this takes some time ... }
  4210. if (cs_support_macro in current_settings.moduleswitches) then
  4211. begin
  4212. mac:=tmacro(search_macro(pattern));
  4213. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  4214. begin
  4215. if yylexcount<max_macro_nesting then
  4216. begin
  4217. mac.is_used:=true;
  4218. inc(yylexcount);
  4219. substitutemacro(pattern,mac.buftext,mac.buflen,
  4220. mac.fileinfo.line,mac.fileinfo.fileindex);
  4221. { handle empty macros }
  4222. if c=#0 then
  4223. reload;
  4224. readtoken(false);
  4225. { that's all folks }
  4226. dec(yylexcount);
  4227. exit;
  4228. end
  4229. else
  4230. Message(scan_w_macro_too_deep);
  4231. end;
  4232. end;
  4233. end;
  4234. { return token }
  4235. goto exit_label;
  4236. end
  4237. else
  4238. begin
  4239. idtoken:=_NOID;
  4240. case c of
  4241. '$' :
  4242. begin
  4243. readnumber;
  4244. token:=_INTCONST;
  4245. goto exit_label;
  4246. end;
  4247. '%' :
  4248. begin
  4249. if not(m_fpc in current_settings.modeswitches) then
  4250. Illegal_Char(c)
  4251. else
  4252. begin
  4253. readnumber;
  4254. token:=_INTCONST;
  4255. goto exit_label;
  4256. end;
  4257. end;
  4258. '&' :
  4259. begin
  4260. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  4261. begin
  4262. readnumber;
  4263. if length(pattern)=1 then
  4264. begin
  4265. { does really an identifier follow? }
  4266. if not (c in ['_','A'..'Z','a'..'z']) then
  4267. message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  4268. readstring;
  4269. token:=_ID;
  4270. idtoken:=_ID;
  4271. end
  4272. else
  4273. token:=_INTCONST;
  4274. goto exit_label;
  4275. end
  4276. else if m_mac in current_settings.modeswitches then
  4277. begin
  4278. readchar;
  4279. token:=_AMPERSAND;
  4280. goto exit_label;
  4281. end
  4282. else
  4283. Illegal_Char(c);
  4284. end;
  4285. '0'..'9' :
  4286. begin
  4287. readnumber;
  4288. if (c in ['.','e','E']) then
  4289. begin
  4290. { first check for a . }
  4291. if c='.' then
  4292. begin
  4293. cachenexttokenpos;
  4294. readchar;
  4295. { is it a .. from a range? }
  4296. case c of
  4297. '.' :
  4298. begin
  4299. readchar;
  4300. token:=_INTCONST;
  4301. nexttoken:=_POINTPOINT;
  4302. goto exit_label;
  4303. end;
  4304. ')' :
  4305. begin
  4306. readchar;
  4307. token:=_INTCONST;
  4308. nexttoken:=_RECKKLAMMER;
  4309. goto exit_label;
  4310. end;
  4311. '0'..'9' :
  4312. begin
  4313. { insert the number after the . }
  4314. pattern:=pattern+'.';
  4315. while c in ['0'..'9'] do
  4316. begin
  4317. pattern:=pattern+c;
  4318. readchar;
  4319. end;
  4320. end;
  4321. else
  4322. begin
  4323. token:=_INTCONST;
  4324. nexttoken:=_POINT;
  4325. goto exit_label;
  4326. end;
  4327. end;
  4328. end;
  4329. { E can also follow after a point is scanned }
  4330. if c in ['e','E'] then
  4331. begin
  4332. pattern:=pattern+'E';
  4333. readchar;
  4334. if c in ['-','+'] then
  4335. begin
  4336. pattern:=pattern+c;
  4337. readchar;
  4338. end;
  4339. if not(c in ['0'..'9']) then
  4340. Illegal_Char(c);
  4341. while c in ['0'..'9'] do
  4342. begin
  4343. pattern:=pattern+c;
  4344. readchar;
  4345. end;
  4346. end;
  4347. token:=_REALNUMBER;
  4348. goto exit_label;
  4349. end;
  4350. token:=_INTCONST;
  4351. goto exit_label;
  4352. end;
  4353. ';' :
  4354. begin
  4355. readchar;
  4356. token:=_SEMICOLON;
  4357. goto exit_label;
  4358. end;
  4359. '[' :
  4360. begin
  4361. readchar;
  4362. token:=_LECKKLAMMER;
  4363. goto exit_label;
  4364. end;
  4365. ']' :
  4366. begin
  4367. readchar;
  4368. token:=_RECKKLAMMER;
  4369. goto exit_label;
  4370. end;
  4371. '(' :
  4372. begin
  4373. readchar;
  4374. case c of
  4375. '*' :
  4376. begin
  4377. c:=#0;{Signal skipoldtpcomment to reload a char }
  4378. skipoldtpcomment;
  4379. readtoken(false);
  4380. exit;
  4381. end;
  4382. '.' :
  4383. begin
  4384. readchar;
  4385. token:=_LECKKLAMMER;
  4386. goto exit_label;
  4387. end;
  4388. end;
  4389. token:=_LKLAMMER;
  4390. goto exit_label;
  4391. end;
  4392. ')' :
  4393. begin
  4394. readchar;
  4395. token:=_RKLAMMER;
  4396. goto exit_label;
  4397. end;
  4398. '+' :
  4399. begin
  4400. readchar;
  4401. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4402. begin
  4403. readchar;
  4404. token:=_PLUSASN;
  4405. goto exit_label;
  4406. end;
  4407. token:=_PLUS;
  4408. goto exit_label;
  4409. end;
  4410. '-' :
  4411. begin
  4412. readchar;
  4413. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4414. begin
  4415. readchar;
  4416. token:=_MINUSASN;
  4417. goto exit_label;
  4418. end;
  4419. token:=_MINUS;
  4420. goto exit_label;
  4421. end;
  4422. ':' :
  4423. begin
  4424. readchar;
  4425. if c='=' then
  4426. begin
  4427. readchar;
  4428. token:=_ASSIGNMENT;
  4429. goto exit_label;
  4430. end;
  4431. token:=_COLON;
  4432. goto exit_label;
  4433. end;
  4434. '*' :
  4435. begin
  4436. readchar;
  4437. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4438. begin
  4439. readchar;
  4440. token:=_STARASN;
  4441. end
  4442. else
  4443. if c='*' then
  4444. begin
  4445. readchar;
  4446. token:=_STARSTAR;
  4447. end
  4448. else
  4449. token:=_STAR;
  4450. goto exit_label;
  4451. end;
  4452. '/' :
  4453. begin
  4454. readchar;
  4455. case c of
  4456. '=' :
  4457. begin
  4458. if (cs_support_c_operators in current_settings.moduleswitches) then
  4459. begin
  4460. readchar;
  4461. token:=_SLASHASN;
  4462. goto exit_label;
  4463. end;
  4464. end;
  4465. '/' :
  4466. begin
  4467. skipdelphicomment;
  4468. readtoken(false);
  4469. exit;
  4470. end;
  4471. end;
  4472. token:=_SLASH;
  4473. goto exit_label;
  4474. end;
  4475. '|' :
  4476. if m_mac in current_settings.modeswitches then
  4477. begin
  4478. readchar;
  4479. token:=_PIPE;
  4480. goto exit_label;
  4481. end
  4482. else
  4483. Illegal_Char(c);
  4484. '=' :
  4485. begin
  4486. readchar;
  4487. token:=_EQ;
  4488. goto exit_label;
  4489. end;
  4490. '.' :
  4491. begin
  4492. readchar;
  4493. case c of
  4494. '.' :
  4495. begin
  4496. readchar;
  4497. case c of
  4498. '.' :
  4499. begin
  4500. readchar;
  4501. token:=_POINTPOINTPOINT;
  4502. goto exit_label;
  4503. end;
  4504. else
  4505. begin
  4506. token:=_POINTPOINT;
  4507. goto exit_label;
  4508. end;
  4509. end;
  4510. end;
  4511. ')' :
  4512. begin
  4513. readchar;
  4514. token:=_RECKKLAMMER;
  4515. goto exit_label;
  4516. end;
  4517. end;
  4518. token:=_POINT;
  4519. goto exit_label;
  4520. end;
  4521. '@' :
  4522. begin
  4523. readchar;
  4524. token:=_KLAMMERAFFE;
  4525. goto exit_label;
  4526. end;
  4527. ',' :
  4528. begin
  4529. readchar;
  4530. token:=_COMMA;
  4531. goto exit_label;
  4532. end;
  4533. '''','#','^' :
  4534. begin
  4535. len:=0;
  4536. cstringpattern:='';
  4537. iswidestring:=false;
  4538. if c='^' then
  4539. begin
  4540. readchar;
  4541. c:=upcase(c);
  4542. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  4543. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  4544. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  4545. begin
  4546. token:=_CARET;
  4547. goto exit_label;
  4548. end
  4549. else
  4550. begin
  4551. inc(len);
  4552. setlength(cstringpattern,256);
  4553. if c<#64 then
  4554. cstringpattern[len]:=chr(ord(c)+64)
  4555. else
  4556. cstringpattern[len]:=chr(ord(c)-64);
  4557. readchar;
  4558. end;
  4559. end;
  4560. repeat
  4561. case c of
  4562. '#' :
  4563. begin
  4564. readchar; { read # }
  4565. case c of
  4566. '$':
  4567. begin
  4568. readchar; { read leading $ }
  4569. asciinr:='$';
  4570. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=5) do
  4571. begin
  4572. asciinr:=asciinr+c;
  4573. readchar;
  4574. end;
  4575. end;
  4576. '&':
  4577. begin
  4578. readchar; { read leading $ }
  4579. asciinr:='&';
  4580. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=7) do
  4581. begin
  4582. asciinr:=asciinr+c;
  4583. readchar;
  4584. end;
  4585. end;
  4586. '%':
  4587. begin
  4588. readchar; { read leading $ }
  4589. asciinr:='%';
  4590. while (upcase(c) in ['0','1']) and (length(asciinr)<=17) do
  4591. begin
  4592. asciinr:=asciinr+c;
  4593. readchar;
  4594. end;
  4595. end;
  4596. else
  4597. begin
  4598. asciinr:='';
  4599. while (c in ['0'..'9']) and (length(asciinr)<=5) do
  4600. begin
  4601. asciinr:=asciinr+c;
  4602. readchar;
  4603. end;
  4604. end;
  4605. end;
  4606. val(asciinr,m,code);
  4607. if (asciinr='') or (code<>0) then
  4608. Message(scan_e_illegal_char_const)
  4609. else if (m<0) or (m>255) or (length(asciinr)>3) then
  4610. begin
  4611. if (m>=0) and (m<=65535) then
  4612. begin
  4613. if not iswidestring then
  4614. begin
  4615. if len>0 then
  4616. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4617. else
  4618. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4619. iswidestring:=true;
  4620. len:=0;
  4621. end;
  4622. concatwidestringchar(patternw,tcompilerwidechar(m));
  4623. end
  4624. else
  4625. Message(scan_e_illegal_char_const)
  4626. end
  4627. else if iswidestring then
  4628. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  4629. else
  4630. begin
  4631. if len>=length(cstringpattern) then
  4632. setlength(cstringpattern,length(cstringpattern)+256);
  4633. inc(len);
  4634. cstringpattern[len]:=chr(m);
  4635. end;
  4636. end;
  4637. '''' :
  4638. begin
  4639. repeat
  4640. readchar;
  4641. case c of
  4642. #26 :
  4643. end_of_file;
  4644. #10,#13 :
  4645. Message(scan_f_string_exceeds_line);
  4646. '''' :
  4647. begin
  4648. readchar;
  4649. if c<>'''' then
  4650. break;
  4651. end;
  4652. end;
  4653. { interpret as utf-8 string? }
  4654. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  4655. begin
  4656. { convert existing string to an utf-8 string }
  4657. if not iswidestring then
  4658. begin
  4659. if len>0 then
  4660. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4661. else
  4662. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4663. iswidestring:=true;
  4664. len:=0;
  4665. end;
  4666. { four or more chars aren't handled }
  4667. if (ord(c) and $f0)=$f0 then
  4668. message(scan_e_utf8_bigger_than_65535)
  4669. { three chars }
  4670. else if (ord(c) and $e0)=$e0 then
  4671. begin
  4672. w:=ord(c) and $f;
  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. readchar;
  4678. if (ord(c) and $c0)<>$80 then
  4679. message(scan_e_utf8_malformed);
  4680. w:=(w shl 6) or (ord(c) and $3f);
  4681. concatwidestringchar(patternw,w);
  4682. end
  4683. { two chars }
  4684. else if (ord(c) and $c0)<>0 then
  4685. begin
  4686. w:=ord(c) and $1f;
  4687. readchar;
  4688. if (ord(c) and $c0)<>$80 then
  4689. message(scan_e_utf8_malformed);
  4690. w:=(w shl 6) or (ord(c) and $3f);
  4691. concatwidestringchar(patternw,w);
  4692. end
  4693. { illegal }
  4694. else if (ord(c) and $80)<>0 then
  4695. message(scan_e_utf8_malformed)
  4696. else
  4697. concatwidestringchar(patternw,tcompilerwidechar(c))
  4698. end
  4699. else if iswidestring then
  4700. begin
  4701. if current_settings.sourcecodepage=CP_UTF8 then
  4702. concatwidestringchar(patternw,ord(c))
  4703. else
  4704. concatwidestringchar(patternw,asciichar2unicode(c))
  4705. end
  4706. else
  4707. begin
  4708. if len>=length(cstringpattern) then
  4709. setlength(cstringpattern,length(cstringpattern)+256);
  4710. inc(len);
  4711. cstringpattern[len]:=c;
  4712. end;
  4713. until false;
  4714. end;
  4715. '^' :
  4716. begin
  4717. readchar;
  4718. c:=upcase(c);
  4719. if c<#64 then
  4720. c:=chr(ord(c)+64)
  4721. else
  4722. c:=chr(ord(c)-64);
  4723. if iswidestring then
  4724. concatwidestringchar(patternw,asciichar2unicode(c))
  4725. else
  4726. begin
  4727. if len>=length(cstringpattern) then
  4728. setlength(cstringpattern,length(cstringpattern)+256);
  4729. inc(len);
  4730. cstringpattern[len]:=c;
  4731. end;
  4732. readchar;
  4733. end;
  4734. else
  4735. break;
  4736. end;
  4737. until false;
  4738. { strings with length 1 become const chars }
  4739. if iswidestring then
  4740. begin
  4741. if patternw^.len=1 then
  4742. token:=_CWCHAR
  4743. else
  4744. token:=_CWSTRING;
  4745. end
  4746. else
  4747. begin
  4748. setlength(cstringpattern,len);
  4749. if length(cstringpattern)=1 then
  4750. begin
  4751. token:=_CCHAR;
  4752. pattern:=cstringpattern;
  4753. end
  4754. else
  4755. token:=_CSTRING;
  4756. end;
  4757. goto exit_label;
  4758. end;
  4759. '>' :
  4760. begin
  4761. readchar;
  4762. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4763. token:=_RSHARPBRACKET
  4764. else
  4765. begin
  4766. case c of
  4767. '=' :
  4768. begin
  4769. readchar;
  4770. token:=_GTE;
  4771. goto exit_label;
  4772. end;
  4773. '>' :
  4774. begin
  4775. readchar;
  4776. token:=_OP_SHR;
  4777. goto exit_label;
  4778. end;
  4779. '<' :
  4780. begin { >< is for a symetric diff for sets }
  4781. readchar;
  4782. token:=_SYMDIF;
  4783. goto exit_label;
  4784. end;
  4785. end;
  4786. token:=_GT;
  4787. end;
  4788. goto exit_label;
  4789. end;
  4790. '<' :
  4791. begin
  4792. readchar;
  4793. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4794. token:=_LSHARPBRACKET
  4795. else
  4796. begin
  4797. case c of
  4798. '>' :
  4799. begin
  4800. readchar;
  4801. token:=_NE;
  4802. goto exit_label;
  4803. end;
  4804. '=' :
  4805. begin
  4806. readchar;
  4807. token:=_LTE;
  4808. goto exit_label;
  4809. end;
  4810. '<' :
  4811. begin
  4812. readchar;
  4813. token:=_OP_SHL;
  4814. goto exit_label;
  4815. end;
  4816. end;
  4817. token:=_LT;
  4818. end;
  4819. goto exit_label;
  4820. end;
  4821. #26 :
  4822. begin
  4823. token:=_EOF;
  4824. checkpreprocstack;
  4825. goto exit_label;
  4826. end;
  4827. else
  4828. Illegal_Char(c);
  4829. end;
  4830. end;
  4831. exit_label:
  4832. lasttoken:=token;
  4833. end;
  4834. function tscannerfile.readpreproc:ttoken;
  4835. var
  4836. low,high,mid: longint;
  4837. optoken: ttoken;
  4838. begin
  4839. skipspace;
  4840. case c of
  4841. '_',
  4842. 'A'..'Z',
  4843. 'a'..'z' :
  4844. begin
  4845. readstring;
  4846. optoken:=_ID;
  4847. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4848. begin
  4849. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4850. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4851. while low<high do
  4852. begin
  4853. mid:=(high+low+1) shr 1;
  4854. if pattern<tokeninfo^[ttoken(mid)].str then
  4855. high:=mid-1
  4856. else
  4857. low:=mid;
  4858. end;
  4859. with tokeninfo^[ttoken(high)] do
  4860. if pattern=str then
  4861. begin
  4862. if (keyword*current_settings.modeswitches)<>[] then
  4863. if op=NOTOKEN then
  4864. optoken:=ttoken(high)
  4865. else
  4866. optoken:=op;
  4867. end;
  4868. if not (optoken in preproc_operators) then
  4869. optoken:=_ID;
  4870. end;
  4871. current_scanner.preproc_pattern:=pattern;
  4872. readpreproc:=optoken;
  4873. end;
  4874. '0'..'9' :
  4875. begin
  4876. readnumber;
  4877. if (c in ['.','e','E']) then
  4878. begin
  4879. { first check for a . }
  4880. if c='.' then
  4881. begin
  4882. readchar;
  4883. if c in ['0'..'9'] then
  4884. begin
  4885. { insert the number after the . }
  4886. pattern:=pattern+'.';
  4887. while c in ['0'..'9'] do
  4888. begin
  4889. pattern:=pattern+c;
  4890. readchar;
  4891. end;
  4892. end
  4893. else
  4894. Illegal_Char(c);
  4895. end;
  4896. { E can also follow after a point is scanned }
  4897. if c in ['e','E'] then
  4898. begin
  4899. pattern:=pattern+'E';
  4900. readchar;
  4901. if c in ['-','+'] then
  4902. begin
  4903. pattern:=pattern+c;
  4904. readchar;
  4905. end;
  4906. if not(c in ['0'..'9']) then
  4907. Illegal_Char(c);
  4908. while c in ['0'..'9'] do
  4909. begin
  4910. pattern:=pattern+c;
  4911. readchar;
  4912. end;
  4913. end;
  4914. readpreproc:=_REALNUMBER;
  4915. end
  4916. else
  4917. readpreproc:=_INTCONST;
  4918. current_scanner.preproc_pattern:=pattern;
  4919. end;
  4920. '$','%':
  4921. begin
  4922. readnumber;
  4923. current_scanner.preproc_pattern:=pattern;
  4924. readpreproc:=_INTCONST;
  4925. end;
  4926. '&' :
  4927. begin
  4928. readnumber;
  4929. if length(pattern)=1 then
  4930. begin
  4931. readstring;
  4932. readpreproc:=_ID;
  4933. end
  4934. else
  4935. readpreproc:=_INTCONST;
  4936. current_scanner.preproc_pattern:=pattern;
  4937. end;
  4938. '.' :
  4939. begin
  4940. readchar;
  4941. readpreproc:=_POINT;
  4942. end;
  4943. ',' :
  4944. begin
  4945. readchar;
  4946. readpreproc:=_COMMA;
  4947. end;
  4948. '}' :
  4949. begin
  4950. readpreproc:=_END;
  4951. end;
  4952. '(' :
  4953. begin
  4954. readchar;
  4955. readpreproc:=_LKLAMMER;
  4956. end;
  4957. ')' :
  4958. begin
  4959. readchar;
  4960. readpreproc:=_RKLAMMER;
  4961. end;
  4962. '[' :
  4963. begin
  4964. readchar;
  4965. readpreproc:=_LECKKLAMMER;
  4966. end;
  4967. ']' :
  4968. begin
  4969. readchar;
  4970. readpreproc:=_RECKKLAMMER;
  4971. end;
  4972. '+' :
  4973. begin
  4974. readchar;
  4975. readpreproc:=_PLUS;
  4976. end;
  4977. '-' :
  4978. begin
  4979. readchar;
  4980. readpreproc:=_MINUS;
  4981. end;
  4982. '*' :
  4983. begin
  4984. readchar;
  4985. readpreproc:=_STAR;
  4986. end;
  4987. '/' :
  4988. begin
  4989. readchar;
  4990. readpreproc:=_SLASH;
  4991. end;
  4992. '=' :
  4993. begin
  4994. readchar;
  4995. readpreproc:=_EQ;
  4996. end;
  4997. '>' :
  4998. begin
  4999. readchar;
  5000. if c='=' then
  5001. begin
  5002. readchar;
  5003. readpreproc:=_GTE;
  5004. end
  5005. else
  5006. readpreproc:=_GT;
  5007. end;
  5008. '<' :
  5009. begin
  5010. readchar;
  5011. case c of
  5012. '>' :
  5013. begin
  5014. readchar;
  5015. readpreproc:=_NE;
  5016. end;
  5017. '=' :
  5018. begin
  5019. readchar;
  5020. readpreproc:=_LTE;
  5021. end;
  5022. else
  5023. readpreproc:=_LT;
  5024. end;
  5025. end;
  5026. #26 :
  5027. begin
  5028. readpreproc:=_EOF;
  5029. checkpreprocstack;
  5030. end;
  5031. else
  5032. begin
  5033. Illegal_Char(c);
  5034. readpreproc:=NOTOKEN;
  5035. end;
  5036. end;
  5037. end;
  5038. function tscannerfile.asmgetcharstart : char;
  5039. begin
  5040. { return first the character already
  5041. available in c }
  5042. lastasmgetchar:=c;
  5043. result:=asmgetchar;
  5044. end;
  5045. function tscannerfile.asmgetchar : char;
  5046. begin
  5047. if lastasmgetchar<>#0 then
  5048. begin
  5049. c:=lastasmgetchar;
  5050. lastasmgetchar:=#0;
  5051. end
  5052. else
  5053. readchar;
  5054. if in_asm_string then
  5055. begin
  5056. asmgetchar:=c;
  5057. exit;
  5058. end;
  5059. repeat
  5060. case c of
  5061. // the { ... } is used in ARM assembler to define register sets, so we can't used
  5062. // it as comment, either (* ... *), /* ... */ or // ... should be used instead.
  5063. // But compiler directives {$...} are allowed in ARM assembler.
  5064. '{' :
  5065. begin
  5066. {$ifdef arm}
  5067. readchar;
  5068. dec(inputpointer);
  5069. if c<>'$' then
  5070. begin
  5071. asmgetchar:='{';
  5072. exit;
  5073. end
  5074. else
  5075. {$endif arm}
  5076. skipcomment;
  5077. end;
  5078. #10,#13 :
  5079. begin
  5080. linebreak;
  5081. asmgetchar:=c;
  5082. exit;
  5083. end;
  5084. #26 :
  5085. begin
  5086. reload;
  5087. if (c=#26) and not assigned(inputfile.next) then
  5088. end_of_file;
  5089. continue;
  5090. end;
  5091. '/' :
  5092. begin
  5093. readchar;
  5094. if c='/' then
  5095. skipdelphicomment
  5096. else
  5097. begin
  5098. asmgetchar:='/';
  5099. lastasmgetchar:=c;
  5100. exit;
  5101. end;
  5102. end;
  5103. '(' :
  5104. begin
  5105. readchar;
  5106. if c='*' then
  5107. begin
  5108. c:=#0;{Signal skipoldtpcomment to reload a char }
  5109. skipoldtpcomment;
  5110. end
  5111. else
  5112. begin
  5113. asmgetchar:='(';
  5114. lastasmgetchar:=c;
  5115. exit;
  5116. end;
  5117. end;
  5118. else
  5119. begin
  5120. asmgetchar:=c;
  5121. exit;
  5122. end;
  5123. end;
  5124. until false;
  5125. end;
  5126. {*****************************************************************************
  5127. Helpers
  5128. *****************************************************************************}
  5129. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5130. begin
  5131. if dm in [directive_all, directive_turbo] then
  5132. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  5133. if dm in [directive_all, directive_mac] then
  5134. tdirectiveitem.create(mac_scannerdirectives,s,p);
  5135. end;
  5136. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5137. begin
  5138. if dm in [directive_all, directive_turbo] then
  5139. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  5140. if dm in [directive_all, directive_mac] then
  5141. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  5142. end;
  5143. {*****************************************************************************
  5144. Initialization
  5145. *****************************************************************************}
  5146. procedure InitScanner;
  5147. begin
  5148. InitWideString(patternw);
  5149. turbo_scannerdirectives:=TFPHashObjectList.Create;
  5150. mac_scannerdirectives:=TFPHashObjectList.Create;
  5151. { Common directives and conditionals }
  5152. AddDirective('I',directive_all, @dir_include);
  5153. AddDirective('DEFINE',directive_all, @dir_define);
  5154. AddDirective('UNDEF',directive_all, @dir_undef);
  5155. AddConditional('IF',directive_all, @dir_if);
  5156. AddConditional('IFDEF',directive_all, @dir_ifdef);
  5157. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  5158. AddConditional('ELSE',directive_all, @dir_else);
  5159. AddConditional('ELSEIF',directive_all, @dir_elseif);
  5160. AddConditional('ENDIF',directive_all, @dir_endif);
  5161. { Directives and conditionals for all modes except mode macpas}
  5162. AddDirective('INCLUDE',directive_turbo, @dir_include);
  5163. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  5164. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  5165. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  5166. AddConditional('IFEND',directive_turbo, @dir_endif);
  5167. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  5168. { Directives and conditionals for mode macpas: }
  5169. AddDirective('SETC',directive_mac, @dir_setc);
  5170. AddDirective('DEFINEC',directive_mac, @dir_definec);
  5171. AddDirective('UNDEFC',directive_mac, @dir_undef);
  5172. AddConditional('IFC',directive_mac, @dir_if);
  5173. AddConditional('ELSEC',directive_mac, @dir_else);
  5174. AddConditional('ELIFC',directive_mac, @dir_elseif);
  5175. AddConditional('ENDC',directive_mac, @dir_endif);
  5176. end;
  5177. procedure DoneScanner;
  5178. begin
  5179. turbo_scannerdirectives.Free;
  5180. mac_scannerdirectives.Free;
  5181. DoneWideString(patternw);
  5182. end;
  5183. end.