scanner.pas 138 KB

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