scanner.pas 186 KB

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