scanner.pas 144 KB

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