scanner.pas 130 KB

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