scanner.pas 190 KB

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