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