scanner.pas 192 KB

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