scanner.pas 142 KB

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