scanner.pas 163 KB

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