scanner.pas 155 KB

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