scanner.pas 157 KB

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