scanner.pas 188 KB

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