scanner.pas 154 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750
  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. macroIsString : boolean;
  1579. begin
  1580. current_scanner.skipspace;
  1581. args:=current_scanner.readcomment;
  1582. hs:=GetToken(args,' ');
  1583. if hs='' then
  1584. exit;
  1585. if (hs[1]='%') then
  1586. begin
  1587. { case insensitive }
  1588. hs:=upper(hs);
  1589. { remove %'s }
  1590. Delete(hs,1,1);
  1591. if hs[length(hs)]='%' then
  1592. Delete(hs,length(hs),1);
  1593. { save old }
  1594. path:=hs;
  1595. { first check for internal macros }
  1596. macroIsString:=true;
  1597. if hs='TIME' then
  1598. hs:=gettimestr
  1599. else
  1600. if hs='DATE' then
  1601. hs:=getdatestr
  1602. else
  1603. if hs='FILE' then
  1604. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex)
  1605. else
  1606. if hs='LINE' then
  1607. hs:=tostr(current_filepos.line)
  1608. else
  1609. if hs='LINENUM' then
  1610. begin
  1611. hs:=tostr(current_filepos.line);
  1612. macroIsString:=false;
  1613. end
  1614. else
  1615. if hs='FPCVERSION' then
  1616. hs:=version_string
  1617. else
  1618. if hs='FPCDATE' then
  1619. hs:=date_string
  1620. else
  1621. if hs='FPCTARGET' then
  1622. hs:=target_cpu_string
  1623. else
  1624. if hs='FPCTARGETCPU' then
  1625. hs:=target_cpu_string
  1626. else
  1627. if hs='FPCTARGETOS' then
  1628. hs:=target_info.shortname
  1629. else
  1630. hs:=GetEnvironmentVariable(hs);
  1631. if hs='' then
  1632. Message1(scan_w_include_env_not_found,path);
  1633. { make it a stringconst }
  1634. if macroIsString then
  1635. hs:=''''+hs+'''';
  1636. current_scanner.insertmacro(path,@hs[1],length(hs),
  1637. current_scanner.line_no,current_scanner.inputfile.ref_index);
  1638. end
  1639. else
  1640. begin
  1641. hs:=FixFileName(hs);
  1642. path:=ExtractFilePath(hs);
  1643. name:=ExtractFileName(hs);
  1644. { Special case for Delphi compatibility: '*' has to be replaced
  1645. by the file name of the current source file. }
  1646. if (length(name)>=1) and
  1647. (name[1]='*') then
  1648. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  1649. { try to find the file }
  1650. found:=findincludefile(path,name,foundfile);
  1651. if (ExtractFileExt(name)='') then
  1652. begin
  1653. { try default extensions .inc , .pp and .pas }
  1654. if (not found) then
  1655. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  1656. if (not found) then
  1657. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  1658. if (not found) then
  1659. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  1660. end;
  1661. if current_scanner.inputfilecount<max_include_nesting then
  1662. begin
  1663. inc(current_scanner.inputfilecount);
  1664. { we need to reread the current char }
  1665. dec(current_scanner.inputpointer);
  1666. { shutdown current file }
  1667. current_scanner.tempcloseinputfile;
  1668. { load new file }
  1669. hp:=do_openinputfile(foundfile);
  1670. current_scanner.addfile(hp);
  1671. current_module.sourcefiles.register_file(hp);
  1672. if (not found) then
  1673. Message1(scan_f_cannot_open_includefile,hs);
  1674. if (not current_scanner.openinputfile) then
  1675. Message1(scan_f_cannot_open_includefile,hs);
  1676. Message1(scan_t_start_include_file,current_scanner.inputfile.path^+current_scanner.inputfile.name^);
  1677. current_scanner.reload;
  1678. end
  1679. else
  1680. Message(scan_f_include_deep_ten);
  1681. end;
  1682. end;
  1683. {*****************************************************************************
  1684. Preprocessor writing
  1685. *****************************************************************************}
  1686. {$ifdef PREPROCWRITE}
  1687. constructor tpreprocfile.create(const fn:string);
  1688. begin
  1689. { open outputfile }
  1690. assign(f,fn);
  1691. {$push}{$I-}
  1692. rewrite(f);
  1693. {$pop}
  1694. if ioresult<>0 then
  1695. Comment(V_Fatal,'can''t create file '+fn);
  1696. getmem(buf,preprocbufsize);
  1697. settextbuf(f,buf^,preprocbufsize);
  1698. { reset }
  1699. eolfound:=false;
  1700. spacefound:=false;
  1701. end;
  1702. destructor tpreprocfile.destroy;
  1703. begin
  1704. close(f);
  1705. freemem(buf,preprocbufsize);
  1706. end;
  1707. procedure tpreprocfile.add(const s:string);
  1708. begin
  1709. write(f,s);
  1710. end;
  1711. procedure tpreprocfile.addspace;
  1712. begin
  1713. if eolfound then
  1714. begin
  1715. writeln(f,'');
  1716. eolfound:=false;
  1717. spacefound:=false;
  1718. end
  1719. else
  1720. if spacefound then
  1721. begin
  1722. write(f,' ');
  1723. spacefound:=false;
  1724. end;
  1725. end;
  1726. {$endif PREPROCWRITE}
  1727. {*****************************************************************************
  1728. TPreProcStack
  1729. *****************************************************************************}
  1730. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  1731. begin
  1732. accept:=a;
  1733. typ:=atyp;
  1734. next:=n;
  1735. end;
  1736. {*****************************************************************************
  1737. TReplayStack
  1738. *****************************************************************************}
  1739. constructor treplaystack.Create(atoken:ttoken;asettings:tsettings;
  1740. atokenbuf:tdynamicarray;anext:treplaystack;achange_endian : boolean);
  1741. begin
  1742. token:=atoken;
  1743. settings:=asettings;
  1744. tokenbuf:=atokenbuf;
  1745. change_endian:=achange_endian;
  1746. next:=anext;
  1747. end;
  1748. {*****************************************************************************
  1749. TDirectiveItem
  1750. *****************************************************************************}
  1751. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  1752. begin
  1753. inherited Create(AList,n);
  1754. is_conditional:=false;
  1755. proc:=p;
  1756. end;
  1757. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  1758. begin
  1759. inherited Create(AList,n);
  1760. is_conditional:=true;
  1761. proc:=p;
  1762. end;
  1763. {****************************************************************************
  1764. TSCANNERFILE
  1765. ****************************************************************************}
  1766. constructor tscannerfile.create(const fn:string);
  1767. begin
  1768. inputfile:=do_openinputfile(fn);
  1769. if assigned(current_module) then
  1770. current_module.sourcefiles.register_file(inputfile);
  1771. { reset localinput }
  1772. c:=#0;
  1773. inputbuffer:=nil;
  1774. inputpointer:=nil;
  1775. inputstart:=0;
  1776. { reset scanner }
  1777. preprocstack:=nil;
  1778. replaystack:=nil;
  1779. tokenbuf_change_endian:=false;
  1780. comment_level:=0;
  1781. yylexcount:=0;
  1782. block_type:=bt_general;
  1783. line_no:=0;
  1784. lastlinepos:=0;
  1785. lasttokenpos:=0;
  1786. nexttokenpos:=0;
  1787. lasttoken:=NOTOKEN;
  1788. nexttoken:=NOTOKEN;
  1789. lastasmgetchar:=#0;
  1790. ignoredirectives:=TFPHashList.Create;
  1791. in_asm_string:=false;
  1792. end;
  1793. procedure tscannerfile.firstfile;
  1794. begin
  1795. { load block }
  1796. if not openinputfile then
  1797. Message1(scan_f_cannot_open_input,inputfile.name^);
  1798. reload;
  1799. end;
  1800. destructor tscannerfile.destroy;
  1801. begin
  1802. if assigned(current_module) and
  1803. (current_module.state=ms_compiled) and
  1804. (status.errorcount=0) then
  1805. checkpreprocstack
  1806. else
  1807. begin
  1808. while assigned(preprocstack) do
  1809. poppreprocstack;
  1810. end;
  1811. while assigned(replaystack) do
  1812. popreplaystack;
  1813. if not inputfile.closed then
  1814. closeinputfile;
  1815. ignoredirectives.free;
  1816. end;
  1817. function tscannerfile.openinputfile:boolean;
  1818. begin
  1819. openinputfile:=inputfile.open;
  1820. { load buffer }
  1821. inputbuffer:=inputfile.buf;
  1822. inputpointer:=inputfile.buf;
  1823. inputstart:=inputfile.bufstart;
  1824. { line }
  1825. line_no:=0;
  1826. lastlinepos:=0;
  1827. lasttokenpos:=0;
  1828. nexttokenpos:=0;
  1829. end;
  1830. procedure tscannerfile.closeinputfile;
  1831. begin
  1832. inputfile.close;
  1833. { reset buffer }
  1834. inputbuffer:=nil;
  1835. inputpointer:=nil;
  1836. inputstart:=0;
  1837. { reset line }
  1838. line_no:=0;
  1839. lastlinepos:=0;
  1840. lasttokenpos:=0;
  1841. nexttokenpos:=0;
  1842. end;
  1843. function tscannerfile.tempopeninputfile:boolean;
  1844. begin
  1845. if inputfile.is_macro then
  1846. exit;
  1847. tempopeninputfile:=inputfile.tempopen;
  1848. { reload buffer }
  1849. inputbuffer:=inputfile.buf;
  1850. inputpointer:=inputfile.buf;
  1851. inputstart:=inputfile.bufstart;
  1852. end;
  1853. procedure tscannerfile.tempcloseinputfile;
  1854. begin
  1855. if inputfile.closed or inputfile.is_macro then
  1856. exit;
  1857. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  1858. inputfile.tempclose;
  1859. { reset buffer }
  1860. inputbuffer:=nil;
  1861. inputpointer:=nil;
  1862. inputstart:=0;
  1863. end;
  1864. procedure tscannerfile.saveinputfile;
  1865. begin
  1866. inputfile.saveinputpointer:=inputpointer;
  1867. inputfile.savelastlinepos:=lastlinepos;
  1868. inputfile.saveline_no:=line_no;
  1869. end;
  1870. procedure tscannerfile.restoreinputfile;
  1871. begin
  1872. inputbuffer:=inputfile.buf;
  1873. inputpointer:=inputfile.saveinputpointer;
  1874. lastlinepos:=inputfile.savelastlinepos;
  1875. line_no:=inputfile.saveline_no;
  1876. if not inputfile.is_macro then
  1877. parser_current_file:=inputfile.name^;
  1878. end;
  1879. procedure tscannerfile.nextfile;
  1880. var
  1881. to_dispose : tinputfile;
  1882. begin
  1883. if assigned(inputfile.next) then
  1884. begin
  1885. if inputfile.is_macro then
  1886. to_dispose:=inputfile
  1887. else
  1888. begin
  1889. to_dispose:=nil;
  1890. dec(inputfilecount);
  1891. end;
  1892. { we can allways close the file, no ? }
  1893. inputfile.close;
  1894. inputfile:=inputfile.next;
  1895. if assigned(to_dispose) then
  1896. to_dispose.free;
  1897. restoreinputfile;
  1898. end;
  1899. end;
  1900. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  1901. begin
  1902. if not assigned(buf) then
  1903. internalerror(200511172);
  1904. if assigned(recordtokenbuf) then
  1905. internalerror(200511173);
  1906. recordtokenbuf:=buf;
  1907. fillchar(last_settings,sizeof(last_settings),0);
  1908. last_message:=nil;
  1909. fillchar(last_filepos,sizeof(last_filepos),0);
  1910. end;
  1911. procedure tscannerfile.stoprecordtokens;
  1912. begin
  1913. if not assigned(recordtokenbuf) then
  1914. internalerror(200511174);
  1915. recordtokenbuf:=nil;
  1916. end;
  1917. procedure tscannerfile.writetoken(t : ttoken);
  1918. var
  1919. b : byte;
  1920. begin
  1921. if ord(t)>$7f then
  1922. begin
  1923. b:=(ord(t) shr 8) or $80;
  1924. recordtokenbuf.write(b,1);
  1925. end;
  1926. b:=ord(t) and $ff;
  1927. recordtokenbuf.write(b,1);
  1928. end;
  1929. procedure tscannerfile.tokenwritesizeint(val : sizeint);
  1930. begin
  1931. recordtokenbuf.write(val,sizeof(sizeint));
  1932. end;
  1933. function tscannerfile.tokenreadsizeint : sizeint;
  1934. var
  1935. val : sizeint;
  1936. begin
  1937. replaytokenbuf.read(val,sizeof(sizeint));
  1938. if tokenbuf_change_endian then
  1939. val:=swapendian(val);
  1940. result:=val;
  1941. end;
  1942. function tscannerfile.tokenreadlongword : longword;
  1943. var
  1944. val : longword;
  1945. begin
  1946. replaytokenbuf.read(val,sizeof(longword));
  1947. if tokenbuf_change_endian then
  1948. val:=swapendian(val);
  1949. result:=val;
  1950. end;
  1951. function tscannerfile.tokenreadlongint : longint;
  1952. var
  1953. val : longint;
  1954. begin
  1955. replaytokenbuf.read(val,sizeof(longint));
  1956. if tokenbuf_change_endian then
  1957. val:=swapendian(val);
  1958. result:=val;
  1959. end;
  1960. function tscannerfile.tokenreadshortint : shortint;
  1961. var
  1962. val : shortint;
  1963. begin
  1964. replaytokenbuf.read(val,sizeof(shortint));
  1965. result:=val;
  1966. end;
  1967. function tscannerfile.tokenreadbyte : byte;
  1968. var
  1969. val : byte;
  1970. begin
  1971. replaytokenbuf.read(val,sizeof(byte));
  1972. result:=val;
  1973. end;
  1974. function tscannerfile.tokenreadsmallint : smallint;
  1975. var
  1976. val : smallint;
  1977. begin
  1978. replaytokenbuf.read(val,sizeof(smallint));
  1979. if tokenbuf_change_endian then
  1980. val:=swapendian(val);
  1981. result:=val;
  1982. end;
  1983. function tscannerfile.tokenreadword : word;
  1984. var
  1985. val : word;
  1986. begin
  1987. replaytokenbuf.read(val,sizeof(word));
  1988. if tokenbuf_change_endian then
  1989. val:=swapendian(val);
  1990. result:=val;
  1991. end;
  1992. function tscannerfile.tokenreadenum(size : longint) : longword;
  1993. begin
  1994. if size=1 then
  1995. result:=tokenreadbyte
  1996. else if size=2 then
  1997. result:=tokenreadword
  1998. else if size=4 then
  1999. result:=tokenreadlongword;
  2000. end;
  2001. procedure tscannerfile.tokenreadset(var b;size : longint);
  2002. var
  2003. i : longint;
  2004. begin
  2005. replaytokenbuf.read(b,size);
  2006. if tokenbuf_change_endian then
  2007. for i:=0 to size-1 do
  2008. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  2009. end;
  2010. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : longint);
  2011. { This procedure
  2012. needs to be changed whenever
  2013. globals.tsettings type is changed,
  2014. the problem is that no error will appear
  2015. before tests with generics are tested. PM }
  2016. var
  2017. startpos, endpos : longword;
  2018. begin
  2019. { WARNING all those fields need to be in the correct
  2020. order otherwise cross_endian PPU reading will fail }
  2021. startpos:=replaytokenbuf.pos;
  2022. with asettings do
  2023. begin
  2024. alignment.procalign:=tokenreadlongint;
  2025. alignment.loopalign:=tokenreadlongint;
  2026. alignment.jumpalign:=tokenreadlongint;
  2027. alignment.constalignmin:=tokenreadlongint;
  2028. alignment.constalignmax:=tokenreadlongint;
  2029. alignment.varalignmin:=tokenreadlongint;
  2030. alignment.varalignmax:=tokenreadlongint;
  2031. alignment.localalignmin:=tokenreadlongint;
  2032. alignment.localalignmax:=tokenreadlongint;
  2033. alignment.recordalignmin:=tokenreadlongint;
  2034. alignment.recordalignmax:=tokenreadlongint;
  2035. alignment.maxCrecordalign:=tokenreadlongint;
  2036. tokenreadset(globalswitches,sizeof(globalswitches));
  2037. tokenreadset(targetswitches,sizeof(targetswitches));
  2038. tokenreadset(moduleswitches,sizeof(moduleswitches));
  2039. tokenreadset(localswitches,sizeof(localswitches));
  2040. tokenreadset(modeswitches,sizeof(modeswitches));
  2041. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  2042. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2043. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2044. tokenreadset(debugswitches,sizeof(debugswitches));
  2045. { 0: old behaviour for sets <=256 elements
  2046. >0: round to this size }
  2047. setalloc:=tokenreadshortint;
  2048. packenum:=tokenreadshortint;
  2049. packrecords:=tokenreadshortint;
  2050. maxfpuregisters:=tokenreadshortint;
  2051. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2052. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2053. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  2054. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  2055. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  2056. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  2057. { tstringencoding is word type,
  2058. thus this should be OK here }
  2059. sourcecodepage:=tstringEncoding(tokenreadword);
  2060. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  2061. disabledircache:=boolean(tokenreadbyte);
  2062. {$if defined(ARM) or defined(AVR)}
  2063. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)));
  2064. {$endif defined(ARM) or defined(AVR)}
  2065. endpos:=replaytokenbuf.pos;
  2066. if endpos-startpos<>expected_size then
  2067. Comment(V_Error,'Wrong size of Settings read-in');
  2068. end;
  2069. end;
  2070. procedure tscannerfile.recordtoken;
  2071. var
  2072. t : ttoken;
  2073. s : tspecialgenerictoken;
  2074. len,val,msgnb,copy_size : sizeint;
  2075. b : byte;
  2076. pmsg : pmessagestaterecord;
  2077. begin
  2078. if not assigned(recordtokenbuf) then
  2079. internalerror(200511176);
  2080. t:=_GENERICSPECIALTOKEN;
  2081. { settings changed? }
  2082. { last field pmessage is handled separately below in
  2083. ST_LOADMESSAGES }
  2084. if CompareByte(current_settings,last_settings,
  2085. sizeof(current_settings)-sizeof(pointer))<>0 then
  2086. begin
  2087. { use a special token to record it }
  2088. s:=ST_LOADSETTINGS;
  2089. writetoken(t);
  2090. recordtokenbuf.write(s,1);
  2091. copy_size:=sizeof(current_settings)-sizeof(pointer);
  2092. tokenwritesizeint(copy_size);
  2093. recordtokenbuf.write(current_settings,copy_size);
  2094. last_settings:=current_settings;
  2095. end;
  2096. if current_settings.pmessage<>last_message then
  2097. begin
  2098. { use a special token to record it }
  2099. s:=ST_LOADMESSAGES;
  2100. writetoken(t);
  2101. recordtokenbuf.write(s,1);
  2102. msgnb:=0;
  2103. pmsg:=current_settings.pmessage;
  2104. while assigned(pmsg) do
  2105. begin
  2106. if msgnb=high(sizeint) then
  2107. { Too many messages }
  2108. internalerror(2011090401);
  2109. inc(msgnb);
  2110. pmsg:=pmsg^.next;
  2111. end;
  2112. tokenwritesizeint(msgnb);
  2113. pmsg:=current_settings.pmessage;
  2114. while assigned(pmsg) do
  2115. begin
  2116. { What about endianess here? }
  2117. val:=pmsg^.value;
  2118. tokenwritesizeint(val);
  2119. val:=ord(pmsg^.state);
  2120. tokenwritesizeint(val);
  2121. pmsg:=pmsg^.next;
  2122. end;
  2123. last_message:=current_settings.pmessage;
  2124. end;
  2125. { file pos changes? }
  2126. if current_tokenpos.line<>last_filepos.line then
  2127. begin
  2128. s:=ST_LINE;
  2129. writetoken(t);
  2130. recordtokenbuf.write(s,1);
  2131. recordtokenbuf.write(current_tokenpos.line,sizeof(current_tokenpos.line));
  2132. last_filepos.line:=current_tokenpos.line;
  2133. end;
  2134. if current_tokenpos.column<>last_filepos.column then
  2135. begin
  2136. s:=ST_COLUMN;
  2137. writetoken(t);
  2138. { can the column be written packed? }
  2139. if current_tokenpos.column<$80 then
  2140. begin
  2141. b:=$80 or current_tokenpos.column;
  2142. recordtokenbuf.write(b,1);
  2143. end
  2144. else
  2145. begin
  2146. recordtokenbuf.write(s,1);
  2147. recordtokenbuf.write(current_tokenpos.column,sizeof(current_tokenpos.column));
  2148. end;
  2149. last_filepos.column:=current_tokenpos.column;
  2150. end;
  2151. if current_tokenpos.fileindex<>last_filepos.fileindex then
  2152. begin
  2153. s:=ST_FILEINDEX;
  2154. writetoken(t);
  2155. recordtokenbuf.write(s,1);
  2156. recordtokenbuf.write(current_tokenpos.fileindex,sizeof(current_tokenpos.fileindex));
  2157. last_filepos.fileindex:=current_tokenpos.fileindex;
  2158. end;
  2159. writetoken(token);
  2160. if token<>_GENERICSPECIALTOKEN then
  2161. writetoken(idtoken);
  2162. case token of
  2163. _CWCHAR,
  2164. _CWSTRING :
  2165. begin
  2166. tokenwritesizeint(patternw^.len);
  2167. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2168. end;
  2169. _CSTRING:
  2170. begin
  2171. len:=length(cstringpattern);
  2172. tokenwritesizeint(len);
  2173. recordtokenbuf.write(cstringpattern[1],length(cstringpattern));
  2174. end;
  2175. _CCHAR,
  2176. _INTCONST,
  2177. _REALNUMBER :
  2178. begin
  2179. { pexpr.pas messes with pattern in case of negative integer consts,
  2180. see around line 2562 the comment of JM; remove the - before recording it
  2181. (FK)
  2182. }
  2183. if (token=_INTCONST) and (pattern[1]='-') then
  2184. delete(pattern,1,1);
  2185. recordtokenbuf.write(pattern[0],1);
  2186. recordtokenbuf.write(pattern[1],length(pattern));
  2187. end;
  2188. _ID :
  2189. begin
  2190. recordtokenbuf.write(orgpattern[0],1);
  2191. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  2192. end;
  2193. end;
  2194. end;
  2195. procedure tscannerfile.startreplaytokens(buf:tdynamicarray; achange_endian : boolean);
  2196. begin
  2197. if not assigned(buf) then
  2198. internalerror(200511175);
  2199. { save current token }
  2200. if token in [_CWCHAR,_CWSTRING,_CCHAR,_CSTRING,_INTCONST,_REALNUMBER,_ID] then
  2201. internalerror(200511178);
  2202. replaystack:=treplaystack.create(token,current_settings,
  2203. replaytokenbuf,replaystack,tokenbuf_change_endian);
  2204. if assigned(inputpointer) then
  2205. dec(inputpointer);
  2206. { install buffer }
  2207. replaytokenbuf:=buf;
  2208. tokenbuf_change_endian:=achange_endian;
  2209. { reload next token }
  2210. replaytokenbuf.seek(0);
  2211. replaytoken;
  2212. end;
  2213. function tscannerfile.readtoken: ttoken;
  2214. var
  2215. b,b2 : byte;
  2216. begin
  2217. replaytokenbuf.read(b,1);
  2218. if (b and $80)<>0 then
  2219. begin
  2220. replaytokenbuf.read(b2,1);
  2221. result:=ttoken(((b and $7f) shl 8) or b2);
  2222. end
  2223. else
  2224. result:=ttoken(b);
  2225. end;
  2226. procedure tscannerfile.replaytoken;
  2227. var
  2228. wlen,mesgnb,copy_size : sizeint;
  2229. specialtoken : tspecialgenerictoken;
  2230. i : byte;
  2231. pmsg,prevmsg : pmessagestaterecord;
  2232. begin
  2233. if not assigned(replaytokenbuf) then
  2234. internalerror(200511177);
  2235. { End of replay buffer? Then load the next char from the file again }
  2236. if replaytokenbuf.pos>=replaytokenbuf.size then
  2237. begin
  2238. token:=replaystack.token;
  2239. replaytokenbuf:=replaystack.tokenbuf;
  2240. { restore compiler settings }
  2241. current_settings:=replaystack.settings;
  2242. popreplaystack;
  2243. if assigned(inputpointer) then
  2244. begin
  2245. c:=inputpointer^;
  2246. inc(inputpointer);
  2247. end;
  2248. exit;
  2249. end;
  2250. repeat
  2251. { load token from the buffer }
  2252. token:=readtoken;
  2253. if token<>_GENERICSPECIALTOKEN then
  2254. idtoken:=readtoken
  2255. else
  2256. idtoken:=_NOID;
  2257. case token of
  2258. _CWCHAR,
  2259. _CWSTRING :
  2260. begin
  2261. wlen:=tokenreadsizeint;
  2262. setlengthwidestring(patternw,wlen);
  2263. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2264. orgpattern:='';
  2265. pattern:='';
  2266. cstringpattern:='';
  2267. end;
  2268. _CSTRING:
  2269. begin
  2270. wlen:=tokenreadsizeint;
  2271. setlength(cstringpattern,wlen);
  2272. replaytokenbuf.read(cstringpattern[1],wlen);
  2273. orgpattern:='';
  2274. pattern:='';
  2275. end;
  2276. _CCHAR,
  2277. _INTCONST,
  2278. _REALNUMBER :
  2279. begin
  2280. replaytokenbuf.read(pattern[0],1);
  2281. replaytokenbuf.read(pattern[1],length(pattern));
  2282. orgpattern:='';
  2283. end;
  2284. _ID :
  2285. begin
  2286. replaytokenbuf.read(orgpattern[0],1);
  2287. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  2288. pattern:=upper(orgpattern);
  2289. end;
  2290. _GENERICSPECIALTOKEN:
  2291. begin
  2292. replaytokenbuf.read(specialtoken,1);
  2293. { packed column? }
  2294. if (ord(specialtoken) and $80)<>0 then
  2295. begin
  2296. current_tokenpos.column:=ord(specialtoken) and $7f;
  2297. { don't generate invalid line info if no sources are available for the current module }
  2298. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2299. current_tokenpos.column:=0;
  2300. current_filepos:=current_tokenpos;
  2301. end
  2302. else
  2303. case specialtoken of
  2304. ST_LOADSETTINGS:
  2305. begin
  2306. copy_size:=tokenreadsizeint;
  2307. if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  2308. internalerror(2011090501);
  2309. {
  2310. replaytokenbuf.read(current_settings,copy_size);
  2311. }
  2312. tokenreadsettings(current_settings,copy_size);
  2313. end;
  2314. ST_LOADMESSAGES:
  2315. begin
  2316. current_settings.pmessage:=nil;
  2317. mesgnb:=tokenreadsizeint;
  2318. if mesgnb>0 then
  2319. Comment(V_Error,'Message recordind not yet supported');
  2320. for i:=1 to mesgnb do
  2321. begin
  2322. new(pmsg);
  2323. if i=1 then
  2324. begin
  2325. current_settings.pmessage:=pmsg;
  2326. prevmsg:=nil;
  2327. end
  2328. else
  2329. prevmsg^.next:=pmsg;
  2330. replaytokenbuf.read(pmsg^.value,sizeof(longint));
  2331. replaytokenbuf.read(pmsg^.state,sizeof(tmsgstate));
  2332. pmsg^.next:=nil;
  2333. prevmsg:=pmsg;
  2334. end;
  2335. end;
  2336. ST_LINE:
  2337. begin
  2338. current_tokenpos.line:=tokenreadlongint;
  2339. { don't generate invalid line info if no sources are available for the current module }
  2340. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2341. current_tokenpos.line:=0;
  2342. current_filepos:=current_tokenpos;
  2343. end;
  2344. ST_COLUMN:
  2345. begin
  2346. current_tokenpos.column:=tokenreadword;
  2347. { don't generate invalid line info if no sources are available for the current module }
  2348. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2349. current_tokenpos.column:=0;
  2350. current_filepos:=current_tokenpos;
  2351. end;
  2352. ST_FILEINDEX:
  2353. begin
  2354. current_tokenpos.fileindex:=tokenreadword;
  2355. { don't generate invalid line info if no sources are available for the current module }
  2356. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2357. begin
  2358. current_tokenpos.column:=0;
  2359. current_tokenpos.line:=0;
  2360. end;
  2361. current_filepos:=current_tokenpos;
  2362. end;
  2363. else
  2364. internalerror(2006103010);
  2365. end;
  2366. continue;
  2367. end;
  2368. end;
  2369. break;
  2370. until false;
  2371. end;
  2372. procedure tscannerfile.addfile(hp:tinputfile);
  2373. begin
  2374. saveinputfile;
  2375. { add to list }
  2376. hp.next:=inputfile;
  2377. inputfile:=hp;
  2378. { load new inputfile }
  2379. restoreinputfile;
  2380. end;
  2381. procedure tscannerfile.reload;
  2382. begin
  2383. with inputfile do
  2384. begin
  2385. { when nothing more to read then leave immediatly, so we
  2386. don't change the current_filepos and leave it point to the last
  2387. char }
  2388. if (c=#26) and (not assigned(next)) then
  2389. exit;
  2390. repeat
  2391. { still more to read?, then change the #0 to a space so its seen
  2392. as a seperator, this can't be used for macro's which can change
  2393. the place of the #0 in the buffer with tempopen }
  2394. if (c=#0) and (bufsize>0) and
  2395. not(inputfile.is_macro) and
  2396. (inputpointer-inputbuffer<bufsize) then
  2397. begin
  2398. c:=' ';
  2399. inc(inputpointer);
  2400. exit;
  2401. end;
  2402. { can we read more from this file ? }
  2403. if (c<>#26) and (not endoffile) then
  2404. begin
  2405. readbuf;
  2406. inputpointer:=buf;
  2407. inputbuffer:=buf;
  2408. inputstart:=bufstart;
  2409. { first line? }
  2410. if line_no=0 then
  2411. begin
  2412. c:=inputpointer^;
  2413. { eat utf-8 signature? }
  2414. if (ord(inputpointer^)=$ef) and
  2415. (ord((inputpointer+1)^)=$bb) and
  2416. (ord((inputpointer+2)^)=$bf) then
  2417. begin
  2418. (* we don't support including files with an UTF-8 bom
  2419. inside another file that wasn't encoded as UTF-8
  2420. already (we don't support {$codepage xxx} switches in
  2421. the middle of a file either) *)
  2422. if (current_settings.sourcecodepage<>CP_UTF8) and
  2423. not current_module.in_global then
  2424. Message(scanner_f_illegal_utf8_bom);
  2425. inc(inputpointer,3);
  2426. message(scan_c_switching_to_utf8);
  2427. current_settings.sourcecodepage:=CP_UTF8;
  2428. include(current_settings.moduleswitches,cs_explicit_codepage);
  2429. end;
  2430. line_no:=1;
  2431. if cs_asm_source in current_settings.globalswitches then
  2432. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  2433. end;
  2434. end
  2435. else
  2436. begin
  2437. { load eof position in tokenpos/current_filepos }
  2438. gettokenpos;
  2439. { close file }
  2440. closeinputfile;
  2441. { no next module, than EOF }
  2442. if not assigned(inputfile.next) then
  2443. begin
  2444. c:=#26;
  2445. exit;
  2446. end;
  2447. { load next file and reopen it }
  2448. nextfile;
  2449. tempopeninputfile;
  2450. { status }
  2451. Message1(scan_t_back_in,inputfile.name^);
  2452. end;
  2453. { load next char }
  2454. c:=inputpointer^;
  2455. inc(inputpointer);
  2456. until c<>#0; { if also end, then reload again }
  2457. end;
  2458. end;
  2459. procedure tscannerfile.insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  2460. var
  2461. hp : tinputfile;
  2462. begin
  2463. { save old postion }
  2464. dec(inputpointer);
  2465. tempcloseinputfile;
  2466. { create macro 'file' }
  2467. { use special name to dispose after !! }
  2468. hp:=do_openinputfile('_Macro_.'+macname);
  2469. addfile(hp);
  2470. with inputfile do
  2471. begin
  2472. setmacro(p,len);
  2473. { local buffer }
  2474. inputbuffer:=buf;
  2475. inputpointer:=buf;
  2476. inputstart:=bufstart;
  2477. ref_index:=fileindex;
  2478. end;
  2479. { reset line }
  2480. line_no:=line;
  2481. lastlinepos:=0;
  2482. lasttokenpos:=0;
  2483. nexttokenpos:=0;
  2484. { load new c }
  2485. c:=inputpointer^;
  2486. inc(inputpointer);
  2487. end;
  2488. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  2489. begin
  2490. tokenpos:=inputstart+(inputpointer-inputbuffer);
  2491. filepos.line:=line_no;
  2492. filepos.column:=tokenpos-lastlinepos;
  2493. filepos.fileindex:=inputfile.ref_index;
  2494. filepos.moduleindex:=current_module.unit_index;
  2495. end;
  2496. procedure tscannerfile.gettokenpos;
  2497. { load the values of tokenpos and lasttokenpos }
  2498. begin
  2499. do_gettokenpos(lasttokenpos,current_tokenpos);
  2500. current_filepos:=current_tokenpos;
  2501. end;
  2502. procedure tscannerfile.cachenexttokenpos;
  2503. begin
  2504. do_gettokenpos(nexttokenpos,next_filepos);
  2505. end;
  2506. procedure tscannerfile.setnexttoken;
  2507. begin
  2508. token:=nexttoken;
  2509. nexttoken:=NOTOKEN;
  2510. lasttokenpos:=nexttokenpos;
  2511. current_tokenpos:=next_filepos;
  2512. current_filepos:=current_tokenpos;
  2513. nexttokenpos:=0;
  2514. end;
  2515. procedure tscannerfile.savetokenpos;
  2516. begin
  2517. oldlasttokenpos:=lasttokenpos;
  2518. oldcurrent_filepos:=current_filepos;
  2519. oldcurrent_tokenpos:=current_tokenpos;
  2520. end;
  2521. procedure tscannerfile.restoretokenpos;
  2522. begin
  2523. lasttokenpos:=oldlasttokenpos;
  2524. current_filepos:=oldcurrent_filepos;
  2525. current_tokenpos:=oldcurrent_tokenpos;
  2526. end;
  2527. procedure tscannerfile.inc_comment_level;
  2528. begin
  2529. if (m_nested_comment in current_settings.modeswitches) then
  2530. inc(comment_level)
  2531. else
  2532. comment_level:=1;
  2533. if (comment_level>1) then
  2534. begin
  2535. savetokenpos;
  2536. gettokenpos; { update for warning }
  2537. Message1(scan_w_comment_level,tostr(comment_level));
  2538. restoretokenpos;
  2539. end;
  2540. end;
  2541. procedure tscannerfile.dec_comment_level;
  2542. begin
  2543. if (m_nested_comment in current_settings.modeswitches) then
  2544. dec(comment_level)
  2545. else
  2546. comment_level:=0;
  2547. end;
  2548. procedure tscannerfile.linebreak;
  2549. var
  2550. cur : char;
  2551. begin
  2552. with inputfile do
  2553. begin
  2554. if (byte(inputpointer^)=0) and not(endoffile) then
  2555. begin
  2556. cur:=c;
  2557. reload;
  2558. if byte(cur)+byte(c)<>23 then
  2559. dec(inputpointer);
  2560. end
  2561. else
  2562. begin
  2563. { Support all combination of #10 and #13 as line break }
  2564. if (byte(inputpointer^)+byte(c)=23) then
  2565. inc(inputpointer);
  2566. end;
  2567. { Always return #10 as line break }
  2568. c:=#10;
  2569. { increase line counters }
  2570. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  2571. inc(line_no);
  2572. { update linebuffer }
  2573. if cs_asm_source in current_settings.globalswitches then
  2574. inputfile.setline(line_no,lastlinepos);
  2575. { update for status and call the show status routine,
  2576. but don't touch current_filepos ! }
  2577. savetokenpos;
  2578. gettokenpos; { update for v_status }
  2579. inc(status.compiledlines);
  2580. ShowStatus;
  2581. restoretokenpos;
  2582. end;
  2583. end;
  2584. procedure tscannerfile.illegal_char(c:char);
  2585. var
  2586. s : string;
  2587. begin
  2588. if c in [#32..#255] then
  2589. s:=''''+c+''''
  2590. else
  2591. s:='#'+tostr(ord(c));
  2592. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  2593. end;
  2594. procedure tscannerfile.end_of_file;
  2595. begin
  2596. checkpreprocstack;
  2597. Message(scan_f_end_of_file);
  2598. end;
  2599. {-------------------------------------------
  2600. IF Conditional Handling
  2601. -------------------------------------------}
  2602. procedure tscannerfile.checkpreprocstack;
  2603. begin
  2604. { check for missing ifdefs }
  2605. while assigned(preprocstack) do
  2606. begin
  2607. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  2608. preprocstack.owner.inputfile.name^,tostr(preprocstack.line_nb));
  2609. poppreprocstack;
  2610. end;
  2611. end;
  2612. procedure tscannerfile.poppreprocstack;
  2613. var
  2614. hp : tpreprocstack;
  2615. begin
  2616. if assigned(preprocstack) then
  2617. begin
  2618. Message1(scan_c_endif_found,preprocstack.name);
  2619. hp:=preprocstack.next;
  2620. preprocstack.free;
  2621. preprocstack:=hp;
  2622. end
  2623. else
  2624. Message(scan_e_endif_without_if);
  2625. end;
  2626. procedure tscannerfile.ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  2627. var
  2628. condition: Boolean;
  2629. valuedescr: String;
  2630. begin
  2631. if (preprocstack=nil) or preprocstack.accept then
  2632. condition:= compile_time_predicate(valuedescr)
  2633. else
  2634. begin
  2635. condition:= false;
  2636. valuedescr:= '';
  2637. end;
  2638. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  2639. preprocstack.name:=valuedescr;
  2640. preprocstack.line_nb:=line_no;
  2641. preprocstack.owner:=self;
  2642. if preprocstack.accept then
  2643. Message2(messid,preprocstack.name,'accepted')
  2644. else
  2645. Message2(messid,preprocstack.name,'rejected');
  2646. end;
  2647. procedure tscannerfile.elsepreprocstack;
  2648. begin
  2649. if assigned(preprocstack) and
  2650. (preprocstack.typ<>pp_else) then
  2651. begin
  2652. if (preprocstack.typ=pp_elseif) then
  2653. preprocstack.accept:=false
  2654. else
  2655. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  2656. preprocstack.accept:=not preprocstack.accept;
  2657. preprocstack.typ:=pp_else;
  2658. preprocstack.line_nb:=line_no;
  2659. if preprocstack.accept then
  2660. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2661. else
  2662. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2663. end
  2664. else
  2665. Message(scan_e_endif_without_if);
  2666. end;
  2667. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  2668. var
  2669. valuedescr: String;
  2670. begin
  2671. if assigned(preprocstack) and
  2672. (preprocstack.typ in [pp_if,pp_elseif]) then
  2673. begin
  2674. { when the branch is accepted we use pp_elseif so we know that
  2675. all the next branches need to be rejected. when this branch is still
  2676. not accepted then leave it at pp_if }
  2677. if (preprocstack.typ=pp_elseif) then
  2678. preprocstack.accept:=false
  2679. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  2680. begin
  2681. preprocstack.accept:=false;
  2682. preprocstack.typ:=pp_elseif;
  2683. end
  2684. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  2685. and compile_time_predicate(valuedescr) then
  2686. begin
  2687. preprocstack.name:=valuedescr;
  2688. preprocstack.accept:=true;
  2689. preprocstack.typ:=pp_elseif;
  2690. end;
  2691. preprocstack.line_nb:=line_no;
  2692. if preprocstack.accept then
  2693. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2694. else
  2695. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2696. end
  2697. else
  2698. Message(scan_e_endif_without_if);
  2699. end;
  2700. procedure tscannerfile.popreplaystack;
  2701. var
  2702. hp : treplaystack;
  2703. begin
  2704. if assigned(replaystack) then
  2705. begin
  2706. hp:=replaystack.next;
  2707. replaystack.free;
  2708. replaystack:=hp;
  2709. if assigned (replaystack) then
  2710. tokenbuf_change_endian:=replaystack.change_endian
  2711. else
  2712. tokenbuf_change_endian:=false;
  2713. end;
  2714. end;
  2715. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  2716. begin
  2717. savetokenpos;
  2718. repeat
  2719. current_scanner.gettokenpos;
  2720. Message1(scan_d_handling_switch,'$'+p.name);
  2721. p.proc();
  2722. { accept the text ? }
  2723. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  2724. break
  2725. else
  2726. begin
  2727. current_scanner.gettokenpos;
  2728. Message(scan_c_skipping_until);
  2729. repeat
  2730. current_scanner.skipuntildirective;
  2731. if not (m_mac in current_settings.modeswitches) then
  2732. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  2733. else
  2734. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  2735. until assigned(p) and (p.is_conditional);
  2736. current_scanner.gettokenpos;
  2737. end;
  2738. until false;
  2739. restoretokenpos;
  2740. end;
  2741. procedure tscannerfile.handledirectives;
  2742. var
  2743. t : tdirectiveitem;
  2744. hs : string;
  2745. begin
  2746. gettokenpos;
  2747. readchar; {Remove the $}
  2748. hs:=readid;
  2749. { handle empty directive }
  2750. if hs='' then
  2751. begin
  2752. Message1(scan_w_illegal_switch,'$');
  2753. exit;
  2754. end;
  2755. {$ifdef PREPROCWRITE}
  2756. if parapreprocess then
  2757. begin
  2758. t:=Get_Directive(hs);
  2759. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  2760. begin
  2761. preprocfile^.AddSpace;
  2762. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  2763. exit;
  2764. end;
  2765. end;
  2766. {$endif PREPROCWRITE}
  2767. { skip this directive? }
  2768. if (ignoredirectives.find(hs)<>nil) then
  2769. begin
  2770. if (comment_level>0) then
  2771. readcomment;
  2772. { we've read the whole comment }
  2773. aktcommentstyle:=comment_none;
  2774. exit;
  2775. end;
  2776. { Check for compiler switches }
  2777. while (length(hs)=1) and (c in ['-','+']) do
  2778. begin
  2779. Message1(scan_d_handling_switch,'$'+hs+c);
  2780. HandleSwitch(hs[1],c);
  2781. current_scanner.readchar; {Remove + or -}
  2782. if c=',' then
  2783. begin
  2784. current_scanner.readchar; {Remove , }
  2785. { read next switch, support $v+,$+}
  2786. hs:=current_scanner.readid;
  2787. if (hs='') then
  2788. begin
  2789. if (c='$') and (m_fpc in current_settings.modeswitches) then
  2790. begin
  2791. current_scanner.readchar; { skip $ }
  2792. hs:=current_scanner.readid;
  2793. end;
  2794. if (hs='') then
  2795. Message1(scan_w_illegal_directive,'$'+c);
  2796. end;
  2797. end
  2798. else
  2799. hs:='';
  2800. end;
  2801. { directives may follow switches after a , }
  2802. if hs<>'' then
  2803. begin
  2804. if not (m_mac in current_settings.modeswitches) then
  2805. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  2806. else
  2807. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  2808. if assigned(t) then
  2809. begin
  2810. if t.is_conditional then
  2811. handleconditional(t)
  2812. else
  2813. begin
  2814. Message1(scan_d_handling_switch,'$'+hs);
  2815. t.proc();
  2816. end;
  2817. end
  2818. else
  2819. begin
  2820. current_scanner.ignoredirectives.Add(hs,nil);
  2821. Message1(scan_w_illegal_directive,'$'+hs);
  2822. end;
  2823. { conditionals already read the comment }
  2824. if (current_scanner.comment_level>0) then
  2825. current_scanner.readcomment;
  2826. { we've read the whole comment }
  2827. aktcommentstyle:=comment_none;
  2828. end;
  2829. end;
  2830. procedure tscannerfile.readchar;
  2831. begin
  2832. c:=inputpointer^;
  2833. if c=#0 then
  2834. reload
  2835. else
  2836. inc(inputpointer);
  2837. end;
  2838. procedure tscannerfile.readstring;
  2839. var
  2840. i : longint;
  2841. err : boolean;
  2842. begin
  2843. err:=false;
  2844. i:=0;
  2845. repeat
  2846. case c of
  2847. '_',
  2848. '0'..'9',
  2849. 'A'..'Z' :
  2850. begin
  2851. if i<255 then
  2852. begin
  2853. inc(i);
  2854. orgpattern[i]:=c;
  2855. pattern[i]:=c;
  2856. end
  2857. else
  2858. begin
  2859. if not err then
  2860. begin
  2861. Message(scan_e_string_exceeds_255_chars);
  2862. err:=true;
  2863. end;
  2864. end;
  2865. c:=inputpointer^;
  2866. inc(inputpointer);
  2867. end;
  2868. 'a'..'z' :
  2869. begin
  2870. if i<255 then
  2871. begin
  2872. inc(i);
  2873. orgpattern[i]:=c;
  2874. pattern[i]:=chr(ord(c)-32)
  2875. end
  2876. else
  2877. begin
  2878. if not err then
  2879. begin
  2880. Message(scan_e_string_exceeds_255_chars);
  2881. err:=true;
  2882. end;
  2883. end;
  2884. c:=inputpointer^;
  2885. inc(inputpointer);
  2886. end;
  2887. #0 :
  2888. reload;
  2889. else
  2890. break;
  2891. end;
  2892. until false;
  2893. orgpattern[0]:=chr(i);
  2894. pattern[0]:=chr(i);
  2895. end;
  2896. procedure tscannerfile.readnumber;
  2897. var
  2898. base,
  2899. i : longint;
  2900. begin
  2901. case c of
  2902. '%' :
  2903. begin
  2904. readchar;
  2905. base:=2;
  2906. pattern[1]:='%';
  2907. i:=1;
  2908. end;
  2909. '&' :
  2910. begin
  2911. readchar;
  2912. base:=8;
  2913. pattern[1]:='&';
  2914. i:=1;
  2915. end;
  2916. '$' :
  2917. begin
  2918. readchar;
  2919. base:=16;
  2920. pattern[1]:='$';
  2921. i:=1;
  2922. end;
  2923. else
  2924. begin
  2925. base:=10;
  2926. i:=0;
  2927. end;
  2928. end;
  2929. while ((base>=10) and (c in ['0'..'9'])) or
  2930. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  2931. ((base=8) and (c in ['0'..'7'])) or
  2932. ((base=2) and (c in ['0'..'1'])) do
  2933. begin
  2934. if i<255 then
  2935. begin
  2936. inc(i);
  2937. pattern[i]:=c;
  2938. end;
  2939. readchar;
  2940. end;
  2941. pattern[0]:=chr(i);
  2942. end;
  2943. function tscannerfile.readid:string;
  2944. begin
  2945. readstring;
  2946. readid:=pattern;
  2947. end;
  2948. function tscannerfile.readval:longint;
  2949. var
  2950. l : longint;
  2951. w : integer;
  2952. begin
  2953. readnumber;
  2954. val(pattern,l,w);
  2955. readval:=l;
  2956. end;
  2957. function tscannerfile.readval_asstring:string;
  2958. begin
  2959. readnumber;
  2960. readval_asstring:=pattern;
  2961. end;
  2962. function tscannerfile.readcomment:string;
  2963. var
  2964. i : longint;
  2965. begin
  2966. i:=0;
  2967. repeat
  2968. case c of
  2969. '{' :
  2970. begin
  2971. if aktcommentstyle=comment_tp then
  2972. inc_comment_level;
  2973. end;
  2974. '}' :
  2975. begin
  2976. if aktcommentstyle=comment_tp then
  2977. begin
  2978. readchar;
  2979. dec_comment_level;
  2980. if comment_level=0 then
  2981. break
  2982. else
  2983. continue;
  2984. end;
  2985. end;
  2986. '*' :
  2987. begin
  2988. if aktcommentstyle=comment_oldtp then
  2989. begin
  2990. readchar;
  2991. if c=')' then
  2992. begin
  2993. readchar;
  2994. dec_comment_level;
  2995. break;
  2996. end
  2997. else
  2998. { Add both characters !!}
  2999. if (i<255) then
  3000. begin
  3001. inc(i);
  3002. readcomment[i]:='*';
  3003. if (i<255) then
  3004. begin
  3005. inc(i);
  3006. readcomment[i]:=c;
  3007. end;
  3008. end;
  3009. end
  3010. else
  3011. { Not old TP comment, so add...}
  3012. begin
  3013. if (i<255) then
  3014. begin
  3015. inc(i);
  3016. readcomment[i]:='*';
  3017. end;
  3018. end;
  3019. end;
  3020. #10,#13 :
  3021. linebreak;
  3022. #26 :
  3023. end_of_file;
  3024. else
  3025. begin
  3026. if (i<255) then
  3027. begin
  3028. inc(i);
  3029. readcomment[i]:=c;
  3030. end;
  3031. end;
  3032. end;
  3033. readchar;
  3034. until false;
  3035. readcomment[0]:=chr(i);
  3036. end;
  3037. function tscannerfile.readquotedstring:string;
  3038. var
  3039. i : longint;
  3040. msgwritten : boolean;
  3041. begin
  3042. i:=0;
  3043. msgwritten:=false;
  3044. if (c='''') then
  3045. begin
  3046. repeat
  3047. readchar;
  3048. case c of
  3049. #26 :
  3050. end_of_file;
  3051. #10,#13 :
  3052. Message(scan_f_string_exceeds_line);
  3053. '''' :
  3054. begin
  3055. readchar;
  3056. if c<>'''' then
  3057. break;
  3058. end;
  3059. end;
  3060. if i<255 then
  3061. begin
  3062. inc(i);
  3063. result[i]:=c;
  3064. end
  3065. else
  3066. begin
  3067. if not msgwritten then
  3068. begin
  3069. Message(scan_e_string_exceeds_255_chars);
  3070. msgwritten:=true;
  3071. end;
  3072. end;
  3073. until false;
  3074. end;
  3075. result[0]:=chr(i);
  3076. end;
  3077. function tscannerfile.readstate:char;
  3078. var
  3079. state : char;
  3080. begin
  3081. state:=' ';
  3082. if c=' ' then
  3083. begin
  3084. current_scanner.skipspace;
  3085. current_scanner.readid;
  3086. if pattern='ON' then
  3087. state:='+'
  3088. else
  3089. if pattern='OFF' then
  3090. state:='-';
  3091. end
  3092. else
  3093. state:=c;
  3094. if not (state in ['+','-']) then
  3095. Message(scan_e_wrong_switch_toggle);
  3096. readstate:=state;
  3097. end;
  3098. function tscannerfile.readstatedefault:char;
  3099. var
  3100. state : char;
  3101. begin
  3102. state:=' ';
  3103. if c=' ' then
  3104. begin
  3105. current_scanner.skipspace;
  3106. current_scanner.readid;
  3107. if pattern='ON' then
  3108. state:='+'
  3109. else
  3110. if pattern='OFF' then
  3111. state:='-'
  3112. else
  3113. if pattern='DEFAULT' then
  3114. state:='*';
  3115. end
  3116. else
  3117. state:=c;
  3118. if not (state in ['+','-','*']) then
  3119. Message(scan_e_wrong_switch_toggle_default);
  3120. readstatedefault:=state;
  3121. end;
  3122. procedure tscannerfile.skipspace;
  3123. begin
  3124. repeat
  3125. case c of
  3126. #26 :
  3127. begin
  3128. reload;
  3129. if (c=#26) and not assigned(inputfile.next) then
  3130. break;
  3131. continue;
  3132. end;
  3133. #10,
  3134. #13 :
  3135. linebreak;
  3136. #9,#11,#12,' ' :
  3137. ;
  3138. else
  3139. break;
  3140. end;
  3141. readchar;
  3142. until false;
  3143. end;
  3144. procedure tscannerfile.skipuntildirective;
  3145. var
  3146. found : longint;
  3147. next_char_loaded : boolean;
  3148. begin
  3149. found:=0;
  3150. next_char_loaded:=false;
  3151. repeat
  3152. case c of
  3153. #10,
  3154. #13 :
  3155. linebreak;
  3156. #26 :
  3157. begin
  3158. reload;
  3159. if (c=#26) and not assigned(inputfile.next) then
  3160. end_of_file;
  3161. continue;
  3162. end;
  3163. '{' :
  3164. begin
  3165. if (aktcommentstyle in [comment_tp,comment_none]) then
  3166. begin
  3167. aktcommentstyle:=comment_tp;
  3168. if (comment_level=0) then
  3169. found:=1;
  3170. inc_comment_level;
  3171. end;
  3172. end;
  3173. '*' :
  3174. begin
  3175. if (aktcommentstyle=comment_oldtp) then
  3176. begin
  3177. readchar;
  3178. if c=')' then
  3179. begin
  3180. dec_comment_level;
  3181. found:=0;
  3182. aktcommentstyle:=comment_none;
  3183. end
  3184. else
  3185. next_char_loaded:=true;
  3186. end
  3187. else
  3188. found := 0;
  3189. end;
  3190. '}' :
  3191. begin
  3192. if (aktcommentstyle=comment_tp) then
  3193. begin
  3194. dec_comment_level;
  3195. if (comment_level=0) then
  3196. aktcommentstyle:=comment_none;
  3197. found:=0;
  3198. end;
  3199. end;
  3200. '$' :
  3201. begin
  3202. if found=1 then
  3203. found:=2;
  3204. end;
  3205. '''' :
  3206. if (aktcommentstyle=comment_none) then
  3207. begin
  3208. repeat
  3209. readchar;
  3210. case c of
  3211. #26 :
  3212. end_of_file;
  3213. #10,#13 :
  3214. break;
  3215. '''' :
  3216. begin
  3217. readchar;
  3218. if c<>'''' then
  3219. begin
  3220. next_char_loaded:=true;
  3221. break;
  3222. end;
  3223. end;
  3224. end;
  3225. until false;
  3226. end;
  3227. '(' :
  3228. begin
  3229. if (aktcommentstyle=comment_none) then
  3230. begin
  3231. readchar;
  3232. if c='*' then
  3233. begin
  3234. readchar;
  3235. if c='$' then
  3236. begin
  3237. found:=2;
  3238. inc_comment_level;
  3239. aktcommentstyle:=comment_oldtp;
  3240. end
  3241. else
  3242. begin
  3243. skipoldtpcomment;
  3244. next_char_loaded:=true;
  3245. end;
  3246. end
  3247. else
  3248. next_char_loaded:=true;
  3249. end
  3250. else
  3251. found:=0;
  3252. end;
  3253. '/' :
  3254. begin
  3255. if (aktcommentstyle=comment_none) then
  3256. begin
  3257. readchar;
  3258. if c='/' then
  3259. skipdelphicomment;
  3260. next_char_loaded:=true;
  3261. end
  3262. else
  3263. found:=0;
  3264. end;
  3265. else
  3266. found:=0;
  3267. end;
  3268. if next_char_loaded then
  3269. next_char_loaded:=false
  3270. else
  3271. readchar;
  3272. until (found=2);
  3273. end;
  3274. {****************************************************************************
  3275. Comment Handling
  3276. ****************************************************************************}
  3277. procedure tscannerfile.skipcomment;
  3278. begin
  3279. aktcommentstyle:=comment_tp;
  3280. readchar;
  3281. inc_comment_level;
  3282. { handle compiler switches }
  3283. if (c='$') then
  3284. handledirectives;
  3285. { handle_switches can dec comment_level, }
  3286. while (comment_level>0) do
  3287. begin
  3288. case c of
  3289. '{' :
  3290. inc_comment_level;
  3291. '}' :
  3292. dec_comment_level;
  3293. #10,#13 :
  3294. linebreak;
  3295. #26 :
  3296. begin
  3297. reload;
  3298. if (c=#26) and not assigned(inputfile.next) then
  3299. end_of_file;
  3300. continue;
  3301. end;
  3302. end;
  3303. readchar;
  3304. end;
  3305. aktcommentstyle:=comment_none;
  3306. end;
  3307. procedure tscannerfile.skipdelphicomment;
  3308. begin
  3309. aktcommentstyle:=comment_delphi;
  3310. inc_comment_level;
  3311. readchar;
  3312. { this is not supported }
  3313. if c='$' then
  3314. Message(scan_w_wrong_styled_switch);
  3315. { skip comment }
  3316. while not (c in [#10,#13,#26]) do
  3317. readchar;
  3318. dec_comment_level;
  3319. aktcommentstyle:=comment_none;
  3320. end;
  3321. procedure tscannerfile.skipoldtpcomment;
  3322. var
  3323. found : longint;
  3324. begin
  3325. aktcommentstyle:=comment_oldtp;
  3326. inc_comment_level;
  3327. { only load a char if last already processed,
  3328. was cause of bug1634 PM }
  3329. if c=#0 then
  3330. readchar;
  3331. { this is now supported }
  3332. if (c='$') then
  3333. handledirectives;
  3334. { skip comment }
  3335. while (comment_level>0) do
  3336. begin
  3337. found:=0;
  3338. repeat
  3339. case c of
  3340. #26 :
  3341. begin
  3342. reload;
  3343. if (c=#26) and not assigned(inputfile.next) then
  3344. end_of_file;
  3345. continue;
  3346. end;
  3347. #10,#13 :
  3348. begin
  3349. if found=4 then
  3350. inc_comment_level;
  3351. linebreak;
  3352. found:=0;
  3353. end;
  3354. '*' :
  3355. begin
  3356. if found=3 then
  3357. found:=4
  3358. else
  3359. found:=1;
  3360. end;
  3361. ')' :
  3362. begin
  3363. if found in [1,4] then
  3364. begin
  3365. dec_comment_level;
  3366. if comment_level=0 then
  3367. found:=2
  3368. else
  3369. found:=0;
  3370. end
  3371. else
  3372. found:=0;
  3373. end;
  3374. '(' :
  3375. begin
  3376. if found=4 then
  3377. inc_comment_level;
  3378. found:=3;
  3379. end;
  3380. else
  3381. begin
  3382. if found=4 then
  3383. inc_comment_level;
  3384. found:=0;
  3385. end;
  3386. end;
  3387. readchar;
  3388. until (found=2);
  3389. end;
  3390. aktcommentstyle:=comment_none;
  3391. end;
  3392. {****************************************************************************
  3393. Token Scanner
  3394. ****************************************************************************}
  3395. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  3396. var
  3397. code : integer;
  3398. len,
  3399. low,high,mid : longint;
  3400. w : word;
  3401. m : longint;
  3402. mac : tmacro;
  3403. asciinr : string[33];
  3404. iswidestring : boolean;
  3405. label
  3406. exit_label;
  3407. begin
  3408. flushpendingswitchesstate;
  3409. { record tokens? }
  3410. if allowrecordtoken and
  3411. assigned(recordtokenbuf) then
  3412. recordtoken;
  3413. { replay tokens? }
  3414. if assigned(replaytokenbuf) then
  3415. begin
  3416. replaytoken;
  3417. goto exit_label;
  3418. end;
  3419. { was there already a token read, then return that token }
  3420. if nexttoken<>NOTOKEN then
  3421. begin
  3422. setnexttoken;
  3423. goto exit_label;
  3424. end;
  3425. { Skip all spaces and comments }
  3426. repeat
  3427. case c of
  3428. '{' :
  3429. skipcomment;
  3430. #26 :
  3431. begin
  3432. reload;
  3433. if (c=#26) and not assigned(inputfile.next) then
  3434. break;
  3435. end;
  3436. ' ',#9..#13 :
  3437. begin
  3438. {$ifdef PREPROCWRITE}
  3439. if parapreprocess then
  3440. begin
  3441. if c=#10 then
  3442. preprocfile.eolfound:=true
  3443. else
  3444. preprocfile.spacefound:=true;
  3445. end;
  3446. {$endif PREPROCWRITE}
  3447. skipspace;
  3448. end
  3449. else
  3450. break;
  3451. end;
  3452. until false;
  3453. { Save current token position, for EOF its already loaded }
  3454. if c<>#26 then
  3455. gettokenpos;
  3456. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  3457. if c in ['A'..'Z','a'..'z','_'] then
  3458. begin
  3459. readstring;
  3460. token:=_ID;
  3461. idtoken:=_ID;
  3462. { keyword or any other known token,
  3463. pattern is always uppercased }
  3464. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  3465. begin
  3466. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  3467. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  3468. while low<high do
  3469. begin
  3470. mid:=(high+low+1) shr 1;
  3471. if pattern<tokeninfo^[ttoken(mid)].str then
  3472. high:=mid-1
  3473. else
  3474. low:=mid;
  3475. end;
  3476. with tokeninfo^[ttoken(high)] do
  3477. if pattern=str then
  3478. begin
  3479. if keyword in current_settings.modeswitches then
  3480. if op=NOTOKEN then
  3481. token:=ttoken(high)
  3482. else
  3483. token:=op;
  3484. idtoken:=ttoken(high);
  3485. end;
  3486. end;
  3487. { Only process identifiers and not keywords }
  3488. if token=_ID then
  3489. begin
  3490. { this takes some time ... }
  3491. if (cs_support_macro in current_settings.moduleswitches) then
  3492. begin
  3493. mac:=tmacro(search_macro(pattern));
  3494. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  3495. begin
  3496. if yylexcount<max_macro_nesting then
  3497. begin
  3498. mac.is_used:=true;
  3499. inc(yylexcount);
  3500. insertmacro(pattern,mac.buftext,mac.buflen,
  3501. mac.fileinfo.line,mac.fileinfo.fileindex);
  3502. { handle empty macros }
  3503. if c=#0 then
  3504. reload;
  3505. readtoken(false);
  3506. { that's all folks }
  3507. dec(yylexcount);
  3508. exit;
  3509. end
  3510. else
  3511. Message(scan_w_macro_too_deep);
  3512. end;
  3513. end;
  3514. end;
  3515. { return token }
  3516. goto exit_label;
  3517. end
  3518. else
  3519. begin
  3520. idtoken:=_NOID;
  3521. case c of
  3522. '$' :
  3523. begin
  3524. readnumber;
  3525. token:=_INTCONST;
  3526. goto exit_label;
  3527. end;
  3528. '%' :
  3529. begin
  3530. if not(m_fpc in current_settings.modeswitches) then
  3531. Illegal_Char(c)
  3532. else
  3533. begin
  3534. readnumber;
  3535. token:=_INTCONST;
  3536. goto exit_label;
  3537. end;
  3538. end;
  3539. '&' :
  3540. begin
  3541. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  3542. begin
  3543. readnumber;
  3544. if length(pattern)=1 then
  3545. begin
  3546. readstring;
  3547. token:=_ID;
  3548. idtoken:=_ID;
  3549. end
  3550. else
  3551. token:=_INTCONST;
  3552. goto exit_label;
  3553. end
  3554. else if m_mac in current_settings.modeswitches then
  3555. begin
  3556. readchar;
  3557. token:=_AMPERSAND;
  3558. goto exit_label;
  3559. end
  3560. else
  3561. Illegal_Char(c);
  3562. end;
  3563. '0'..'9' :
  3564. begin
  3565. readnumber;
  3566. if (c in ['.','e','E']) then
  3567. begin
  3568. { first check for a . }
  3569. if c='.' then
  3570. begin
  3571. cachenexttokenpos;
  3572. readchar;
  3573. { is it a .. from a range? }
  3574. case c of
  3575. '.' :
  3576. begin
  3577. readchar;
  3578. token:=_INTCONST;
  3579. nexttoken:=_POINTPOINT;
  3580. goto exit_label;
  3581. end;
  3582. ')' :
  3583. begin
  3584. readchar;
  3585. token:=_INTCONST;
  3586. nexttoken:=_RECKKLAMMER;
  3587. goto exit_label;
  3588. end;
  3589. end;
  3590. { insert the number after the . }
  3591. pattern:=pattern+'.';
  3592. while c in ['0'..'9'] do
  3593. begin
  3594. pattern:=pattern+c;
  3595. readchar;
  3596. end;
  3597. end;
  3598. { E can also follow after a point is scanned }
  3599. if c in ['e','E'] then
  3600. begin
  3601. pattern:=pattern+'E';
  3602. readchar;
  3603. if c in ['-','+'] then
  3604. begin
  3605. pattern:=pattern+c;
  3606. readchar;
  3607. end;
  3608. if not(c in ['0'..'9']) then
  3609. Illegal_Char(c);
  3610. while c in ['0'..'9'] do
  3611. begin
  3612. pattern:=pattern+c;
  3613. readchar;
  3614. end;
  3615. end;
  3616. token:=_REALNUMBER;
  3617. goto exit_label;
  3618. end;
  3619. token:=_INTCONST;
  3620. goto exit_label;
  3621. end;
  3622. ';' :
  3623. begin
  3624. readchar;
  3625. token:=_SEMICOLON;
  3626. goto exit_label;
  3627. end;
  3628. '[' :
  3629. begin
  3630. readchar;
  3631. token:=_LECKKLAMMER;
  3632. goto exit_label;
  3633. end;
  3634. ']' :
  3635. begin
  3636. readchar;
  3637. token:=_RECKKLAMMER;
  3638. goto exit_label;
  3639. end;
  3640. '(' :
  3641. begin
  3642. readchar;
  3643. case c of
  3644. '*' :
  3645. begin
  3646. c:=#0;{Signal skipoldtpcomment to reload a char }
  3647. skipoldtpcomment;
  3648. readtoken(false);
  3649. exit;
  3650. end;
  3651. '.' :
  3652. begin
  3653. readchar;
  3654. token:=_LECKKLAMMER;
  3655. goto exit_label;
  3656. end;
  3657. end;
  3658. token:=_LKLAMMER;
  3659. goto exit_label;
  3660. end;
  3661. ')' :
  3662. begin
  3663. readchar;
  3664. token:=_RKLAMMER;
  3665. goto exit_label;
  3666. end;
  3667. '+' :
  3668. begin
  3669. readchar;
  3670. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3671. begin
  3672. readchar;
  3673. token:=_PLUSASN;
  3674. goto exit_label;
  3675. end;
  3676. token:=_PLUS;
  3677. goto exit_label;
  3678. end;
  3679. '-' :
  3680. begin
  3681. readchar;
  3682. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3683. begin
  3684. readchar;
  3685. token:=_MINUSASN;
  3686. goto exit_label;
  3687. end;
  3688. token:=_MINUS;
  3689. goto exit_label;
  3690. end;
  3691. ':' :
  3692. begin
  3693. readchar;
  3694. if c='=' then
  3695. begin
  3696. readchar;
  3697. token:=_ASSIGNMENT;
  3698. goto exit_label;
  3699. end;
  3700. token:=_COLON;
  3701. goto exit_label;
  3702. end;
  3703. '*' :
  3704. begin
  3705. readchar;
  3706. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3707. begin
  3708. readchar;
  3709. token:=_STARASN;
  3710. end
  3711. else
  3712. if c='*' then
  3713. begin
  3714. readchar;
  3715. token:=_STARSTAR;
  3716. end
  3717. else
  3718. token:=_STAR;
  3719. goto exit_label;
  3720. end;
  3721. '/' :
  3722. begin
  3723. readchar;
  3724. case c of
  3725. '=' :
  3726. begin
  3727. if (cs_support_c_operators in current_settings.moduleswitches) then
  3728. begin
  3729. readchar;
  3730. token:=_SLASHASN;
  3731. goto exit_label;
  3732. end;
  3733. end;
  3734. '/' :
  3735. begin
  3736. skipdelphicomment;
  3737. readtoken(false);
  3738. exit;
  3739. end;
  3740. end;
  3741. token:=_SLASH;
  3742. goto exit_label;
  3743. end;
  3744. '|' :
  3745. if m_mac in current_settings.modeswitches then
  3746. begin
  3747. readchar;
  3748. token:=_PIPE;
  3749. goto exit_label;
  3750. end
  3751. else
  3752. Illegal_Char(c);
  3753. '=' :
  3754. begin
  3755. readchar;
  3756. token:=_EQ;
  3757. goto exit_label;
  3758. end;
  3759. '.' :
  3760. begin
  3761. readchar;
  3762. case c of
  3763. '.' :
  3764. begin
  3765. readchar;
  3766. case c of
  3767. '.' :
  3768. begin
  3769. readchar;
  3770. token:=_POINTPOINTPOINT;
  3771. goto exit_label;
  3772. end;
  3773. else
  3774. begin
  3775. token:=_POINTPOINT;
  3776. goto exit_label;
  3777. end;
  3778. end;
  3779. end;
  3780. ')' :
  3781. begin
  3782. readchar;
  3783. token:=_RECKKLAMMER;
  3784. goto exit_label;
  3785. end;
  3786. end;
  3787. token:=_POINT;
  3788. goto exit_label;
  3789. end;
  3790. '@' :
  3791. begin
  3792. readchar;
  3793. token:=_KLAMMERAFFE;
  3794. goto exit_label;
  3795. end;
  3796. ',' :
  3797. begin
  3798. readchar;
  3799. token:=_COMMA;
  3800. goto exit_label;
  3801. end;
  3802. '''','#','^' :
  3803. begin
  3804. len:=0;
  3805. cstringpattern:='';
  3806. iswidestring:=false;
  3807. if c='^' then
  3808. begin
  3809. readchar;
  3810. c:=upcase(c);
  3811. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  3812. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  3813. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  3814. begin
  3815. token:=_CARET;
  3816. goto exit_label;
  3817. end
  3818. else
  3819. begin
  3820. inc(len);
  3821. setlength(cstringpattern,256);
  3822. if c<#64 then
  3823. cstringpattern[len]:=chr(ord(c)+64)
  3824. else
  3825. cstringpattern[len]:=chr(ord(c)-64);
  3826. readchar;
  3827. end;
  3828. end;
  3829. repeat
  3830. case c of
  3831. '#' :
  3832. begin
  3833. readchar; { read # }
  3834. case c of
  3835. '$':
  3836. begin
  3837. readchar; { read leading $ }
  3838. asciinr:='$';
  3839. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=5) do
  3840. begin
  3841. asciinr:=asciinr+c;
  3842. readchar;
  3843. end;
  3844. end;
  3845. '&':
  3846. begin
  3847. readchar; { read leading $ }
  3848. asciinr:='&';
  3849. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=7) do
  3850. begin
  3851. asciinr:=asciinr+c;
  3852. readchar;
  3853. end;
  3854. end;
  3855. '%':
  3856. begin
  3857. readchar; { read leading $ }
  3858. asciinr:='%';
  3859. while (upcase(c) in ['0','1']) and (length(asciinr)<=17) do
  3860. begin
  3861. asciinr:=asciinr+c;
  3862. readchar;
  3863. end;
  3864. end;
  3865. else
  3866. begin
  3867. asciinr:='';
  3868. while (c in ['0'..'9']) and (length(asciinr)<=5) do
  3869. begin
  3870. asciinr:=asciinr+c;
  3871. readchar;
  3872. end;
  3873. end;
  3874. end;
  3875. val(asciinr,m,code);
  3876. if (asciinr='') or (code<>0) then
  3877. Message(scan_e_illegal_char_const)
  3878. else if (m<0) or (m>255) or (length(asciinr)>3) then
  3879. begin
  3880. if (m>=0) and (m<=65535) then
  3881. begin
  3882. if not iswidestring then
  3883. begin
  3884. if len>0 then
  3885. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  3886. else
  3887. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  3888. iswidestring:=true;
  3889. len:=0;
  3890. end;
  3891. concatwidestringchar(patternw,tcompilerwidechar(m));
  3892. end
  3893. else
  3894. Message(scan_e_illegal_char_const)
  3895. end
  3896. else if iswidestring then
  3897. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  3898. else
  3899. begin
  3900. if len>=length(cstringpattern) then
  3901. setlength(cstringpattern,length(cstringpattern)+256);
  3902. inc(len);
  3903. cstringpattern[len]:=chr(m);
  3904. end;
  3905. end;
  3906. '''' :
  3907. begin
  3908. repeat
  3909. readchar;
  3910. case c of
  3911. #26 :
  3912. end_of_file;
  3913. #10,#13 :
  3914. Message(scan_f_string_exceeds_line);
  3915. '''' :
  3916. begin
  3917. readchar;
  3918. if c<>'''' then
  3919. break;
  3920. end;
  3921. end;
  3922. { interpret as utf-8 string? }
  3923. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  3924. begin
  3925. { convert existing string to an utf-8 string }
  3926. if not iswidestring then
  3927. begin
  3928. if len>0 then
  3929. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  3930. else
  3931. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  3932. iswidestring:=true;
  3933. len:=0;
  3934. end;
  3935. { four or more chars aren't handled }
  3936. if (ord(c) and $f0)=$f0 then
  3937. message(scan_e_utf8_bigger_than_65535)
  3938. { three chars }
  3939. else if (ord(c) and $e0)=$e0 then
  3940. begin
  3941. w:=ord(c) and $f;
  3942. readchar;
  3943. if (ord(c) and $c0)<>$80 then
  3944. message(scan_e_utf8_malformed);
  3945. w:=(w shl 6) or (ord(c) and $3f);
  3946. readchar;
  3947. if (ord(c) and $c0)<>$80 then
  3948. message(scan_e_utf8_malformed);
  3949. w:=(w shl 6) or (ord(c) and $3f);
  3950. concatwidestringchar(patternw,w);
  3951. end
  3952. { two chars }
  3953. else if (ord(c) and $c0)<>0 then
  3954. begin
  3955. w:=ord(c) and $1f;
  3956. readchar;
  3957. if (ord(c) and $c0)<>$80 then
  3958. message(scan_e_utf8_malformed);
  3959. w:=(w shl 6) or (ord(c) and $3f);
  3960. concatwidestringchar(patternw,w);
  3961. end
  3962. { illegal }
  3963. else if (ord(c) and $80)<>0 then
  3964. message(scan_e_utf8_malformed)
  3965. else
  3966. concatwidestringchar(patternw,tcompilerwidechar(c))
  3967. end
  3968. else if iswidestring then
  3969. begin
  3970. if current_settings.sourcecodepage=CP_UTF8 then
  3971. concatwidestringchar(patternw,ord(c))
  3972. else
  3973. concatwidestringchar(patternw,asciichar2unicode(c))
  3974. end
  3975. else
  3976. begin
  3977. if len>=length(cstringpattern) then
  3978. setlength(cstringpattern,length(cstringpattern)+256);
  3979. inc(len);
  3980. cstringpattern[len]:=c;
  3981. end;
  3982. until false;
  3983. end;
  3984. '^' :
  3985. begin
  3986. readchar;
  3987. c:=upcase(c);
  3988. if c<#64 then
  3989. c:=chr(ord(c)+64)
  3990. else
  3991. c:=chr(ord(c)-64);
  3992. if iswidestring then
  3993. concatwidestringchar(patternw,asciichar2unicode(c))
  3994. else
  3995. begin
  3996. if len>=length(cstringpattern) then
  3997. setlength(cstringpattern,length(cstringpattern)+256);
  3998. inc(len);
  3999. cstringpattern[len]:=c;
  4000. end;
  4001. readchar;
  4002. end;
  4003. else
  4004. break;
  4005. end;
  4006. until false;
  4007. { strings with length 1 become const chars }
  4008. if iswidestring then
  4009. begin
  4010. if patternw^.len=1 then
  4011. token:=_CWCHAR
  4012. else
  4013. token:=_CWSTRING;
  4014. end
  4015. else
  4016. begin
  4017. setlength(cstringpattern,len);
  4018. if length(cstringpattern)=1 then
  4019. begin
  4020. token:=_CCHAR;
  4021. pattern:=cstringpattern;
  4022. end
  4023. else
  4024. token:=_CSTRING;
  4025. end;
  4026. goto exit_label;
  4027. end;
  4028. '>' :
  4029. begin
  4030. readchar;
  4031. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4032. token:=_RSHARPBRACKET
  4033. else
  4034. begin
  4035. case c of
  4036. '=' :
  4037. begin
  4038. readchar;
  4039. token:=_GTE;
  4040. goto exit_label;
  4041. end;
  4042. '>' :
  4043. begin
  4044. readchar;
  4045. token:=_OP_SHR;
  4046. goto exit_label;
  4047. end;
  4048. '<' :
  4049. begin { >< is for a symetric diff for sets }
  4050. readchar;
  4051. token:=_SYMDIF;
  4052. goto exit_label;
  4053. end;
  4054. end;
  4055. token:=_GT;
  4056. end;
  4057. goto exit_label;
  4058. end;
  4059. '<' :
  4060. begin
  4061. readchar;
  4062. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4063. token:=_LSHARPBRACKET
  4064. else
  4065. begin
  4066. case c of
  4067. '>' :
  4068. begin
  4069. readchar;
  4070. token:=_NE;
  4071. goto exit_label;
  4072. end;
  4073. '=' :
  4074. begin
  4075. readchar;
  4076. token:=_LTE;
  4077. goto exit_label;
  4078. end;
  4079. '<' :
  4080. begin
  4081. readchar;
  4082. token:=_OP_SHL;
  4083. goto exit_label;
  4084. end;
  4085. end;
  4086. token:=_LT;
  4087. end;
  4088. goto exit_label;
  4089. end;
  4090. #26 :
  4091. begin
  4092. token:=_EOF;
  4093. checkpreprocstack;
  4094. goto exit_label;
  4095. end;
  4096. else
  4097. Illegal_Char(c);
  4098. end;
  4099. end;
  4100. exit_label:
  4101. lasttoken:=token;
  4102. end;
  4103. function tscannerfile.readpreproc:ttoken;
  4104. begin
  4105. skipspace;
  4106. case c of
  4107. '_',
  4108. 'A'..'Z',
  4109. 'a'..'z' :
  4110. begin
  4111. current_scanner.preproc_pattern:=readid;
  4112. readpreproc:=_ID;
  4113. end;
  4114. '0'..'9' :
  4115. begin
  4116. current_scanner.preproc_pattern:=readval_asstring;
  4117. { realnumber? }
  4118. if c='.' then
  4119. begin
  4120. readchar;
  4121. while c in ['0'..'9'] do
  4122. begin
  4123. current_scanner.preproc_pattern:=current_scanner.preproc_pattern+c;
  4124. readchar;
  4125. end;
  4126. end;
  4127. readpreproc:=_ID;
  4128. end;
  4129. '$','%','&' :
  4130. begin
  4131. current_scanner.preproc_pattern:=readval_asstring;
  4132. readpreproc:=_ID;
  4133. end;
  4134. ',' :
  4135. begin
  4136. readchar;
  4137. readpreproc:=_COMMA;
  4138. end;
  4139. '}' :
  4140. begin
  4141. readpreproc:=_END;
  4142. end;
  4143. '(' :
  4144. begin
  4145. readchar;
  4146. readpreproc:=_LKLAMMER;
  4147. end;
  4148. ')' :
  4149. begin
  4150. readchar;
  4151. readpreproc:=_RKLAMMER;
  4152. end;
  4153. '[' :
  4154. begin
  4155. readchar;
  4156. readpreproc:=_LECKKLAMMER;
  4157. end;
  4158. ']' :
  4159. begin
  4160. readchar;
  4161. readpreproc:=_RECKKLAMMER;
  4162. end;
  4163. '+' :
  4164. begin
  4165. readchar;
  4166. readpreproc:=_PLUS;
  4167. end;
  4168. '-' :
  4169. begin
  4170. readchar;
  4171. readpreproc:=_MINUS;
  4172. end;
  4173. '*' :
  4174. begin
  4175. readchar;
  4176. readpreproc:=_STAR;
  4177. end;
  4178. '/' :
  4179. begin
  4180. readchar;
  4181. readpreproc:=_SLASH;
  4182. end;
  4183. '=' :
  4184. begin
  4185. readchar;
  4186. readpreproc:=_EQ;
  4187. end;
  4188. '>' :
  4189. begin
  4190. readchar;
  4191. if c='=' then
  4192. begin
  4193. readchar;
  4194. readpreproc:=_GTE;
  4195. end
  4196. else
  4197. readpreproc:=_GT;
  4198. end;
  4199. '<' :
  4200. begin
  4201. readchar;
  4202. case c of
  4203. '>' :
  4204. begin
  4205. readchar;
  4206. readpreproc:=_NE;
  4207. end;
  4208. '=' :
  4209. begin
  4210. readchar;
  4211. readpreproc:=_LTE;
  4212. end;
  4213. else
  4214. readpreproc:=_LT;
  4215. end;
  4216. end;
  4217. #26 :
  4218. begin
  4219. readpreproc:=_EOF;
  4220. checkpreprocstack;
  4221. end;
  4222. else
  4223. Illegal_Char(c);
  4224. end;
  4225. end;
  4226. function tscannerfile.asmgetcharstart : char;
  4227. begin
  4228. { return first the character already
  4229. available in c }
  4230. lastasmgetchar:=c;
  4231. result:=asmgetchar;
  4232. end;
  4233. function tscannerfile.asmgetchar : char;
  4234. begin
  4235. if lastasmgetchar<>#0 then
  4236. begin
  4237. c:=lastasmgetchar;
  4238. lastasmgetchar:=#0;
  4239. end
  4240. else
  4241. readchar;
  4242. if in_asm_string then
  4243. begin
  4244. asmgetchar:=c;
  4245. exit;
  4246. end;
  4247. repeat
  4248. case c of
  4249. // the { ... } is used in ARM assembler to define register sets, so we can't used
  4250. // it as comment, either (* ... *), /* ... */ or // ... should be used instead.
  4251. // But compiler directives {$...} are allowed in ARM assembler.
  4252. '{' :
  4253. begin
  4254. {$ifdef arm}
  4255. readchar;
  4256. dec(inputpointer);
  4257. if c<>'$' then
  4258. begin
  4259. asmgetchar:='{';
  4260. exit;
  4261. end
  4262. else
  4263. {$endif arm}
  4264. skipcomment;
  4265. end;
  4266. #10,#13 :
  4267. begin
  4268. linebreak;
  4269. asmgetchar:=c;
  4270. exit;
  4271. end;
  4272. #26 :
  4273. begin
  4274. reload;
  4275. if (c=#26) and not assigned(inputfile.next) then
  4276. end_of_file;
  4277. continue;
  4278. end;
  4279. '/' :
  4280. begin
  4281. readchar;
  4282. if c='/' then
  4283. skipdelphicomment
  4284. else
  4285. begin
  4286. asmgetchar:='/';
  4287. lastasmgetchar:=c;
  4288. exit;
  4289. end;
  4290. end;
  4291. '(' :
  4292. begin
  4293. readchar;
  4294. if c='*' then
  4295. begin
  4296. c:=#0;{Signal skipoldtpcomment to reload a char }
  4297. skipoldtpcomment;
  4298. end
  4299. else
  4300. begin
  4301. asmgetchar:='(';
  4302. lastasmgetchar:=c;
  4303. exit;
  4304. end;
  4305. end;
  4306. else
  4307. begin
  4308. asmgetchar:=c;
  4309. exit;
  4310. end;
  4311. end;
  4312. until false;
  4313. end;
  4314. {*****************************************************************************
  4315. Helpers
  4316. *****************************************************************************}
  4317. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  4318. begin
  4319. if dm in [directive_all, directive_turbo] then
  4320. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  4321. if dm in [directive_all, directive_mac] then
  4322. tdirectiveitem.create(mac_scannerdirectives,s,p);
  4323. end;
  4324. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  4325. begin
  4326. if dm in [directive_all, directive_turbo] then
  4327. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  4328. if dm in [directive_all, directive_mac] then
  4329. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  4330. end;
  4331. {*****************************************************************************
  4332. Initialization
  4333. *****************************************************************************}
  4334. procedure InitScanner;
  4335. begin
  4336. InitWideString(patternw);
  4337. turbo_scannerdirectives:=TFPHashObjectList.Create;
  4338. mac_scannerdirectives:=TFPHashObjectList.Create;
  4339. { Common directives and conditionals }
  4340. AddDirective('I',directive_all, @dir_include);
  4341. AddDirective('DEFINE',directive_all, @dir_define);
  4342. AddDirective('UNDEF',directive_all, @dir_undef);
  4343. AddConditional('IF',directive_all, @dir_if);
  4344. AddConditional('IFDEF',directive_all, @dir_ifdef);
  4345. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  4346. AddConditional('ELSE',directive_all, @dir_else);
  4347. AddConditional('ELSEIF',directive_all, @dir_elseif);
  4348. AddConditional('ENDIF',directive_all, @dir_endif);
  4349. { Directives and conditionals for all modes except mode macpas}
  4350. AddDirective('INCLUDE',directive_turbo, @dir_include);
  4351. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  4352. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  4353. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  4354. AddConditional('IFEND',directive_turbo, @dir_endif);
  4355. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  4356. { Directives and conditionals for mode macpas: }
  4357. AddDirective('SETC',directive_mac, @dir_setc);
  4358. AddDirective('DEFINEC',directive_mac, @dir_definec);
  4359. AddDirective('UNDEFC',directive_mac, @dir_undef);
  4360. AddConditional('IFC',directive_mac, @dir_if);
  4361. AddConditional('ELSEC',directive_mac, @dir_else);
  4362. AddConditional('ELIFC',directive_mac, @dir_elseif);
  4363. AddConditional('ENDC',directive_mac, @dir_endif);
  4364. end;
  4365. procedure DoneScanner;
  4366. begin
  4367. turbo_scannerdirectives.Free;
  4368. mac_scannerdirectives.Free;
  4369. DoneWideString(patternw);
  4370. end;
  4371. end.