scanner.pas 144 KB

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