scanner.pas 162 KB

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