scanner.pas 142 KB

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