scanner.pas 154 KB

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