scanner.pas 153 KB

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