scanner.pas 129 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060
  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 : stringid;
  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. { last settings we stored }
  69. last_settings : tsettings;
  70. { last filepos we stored }
  71. last_filepos : tfileposinfo;
  72. comment_level,
  73. yylexcount : longint;
  74. lastasmgetchar : char;
  75. ignoredirectives : tstringlist; { ignore directives, used to give warnings only once }
  76. preprocstack : tpreprocstack;
  77. in_asm_string : boolean;
  78. preproc_pattern : string;
  79. preproc_token : ttoken;
  80. constructor Create(const fn:string);
  81. destructor Destroy;override;
  82. { File buffer things }
  83. function openinputfile:boolean;
  84. procedure closeinputfile;
  85. function tempopeninputfile:boolean;
  86. procedure tempcloseinputfile;
  87. procedure saveinputfile;
  88. procedure restoreinputfile;
  89. procedure firstfile;
  90. procedure nextfile;
  91. procedure addfile(hp:tinputfile);
  92. procedure reload;
  93. procedure insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  94. { Scanner things }
  95. procedure gettokenpos;
  96. procedure inc_comment_level;
  97. procedure dec_comment_level;
  98. procedure illegal_char(c:char);
  99. procedure end_of_file;
  100. procedure checkpreprocstack;
  101. procedure poppreprocstack;
  102. procedure ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  103. procedure elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  104. procedure elsepreprocstack;
  105. procedure handleconditional(p:tdirectiveitem);
  106. procedure handledirectives;
  107. procedure linebreak;
  108. procedure recordtoken;
  109. procedure startrecordtokens(buf:tdynamicarray);
  110. procedure stoprecordtokens;
  111. procedure replaytoken;
  112. procedure startreplaytokens(buf:tdynamicarray);
  113. procedure readchar;
  114. procedure readstring;
  115. procedure readnumber;
  116. function readid:string;
  117. function readval:longint;
  118. function readval_asstring:string;
  119. function readcomment:string;
  120. function readquotedstring:string;
  121. function readstate:char;
  122. function readstatedefault:char;
  123. procedure skipspace;
  124. procedure skipuntildirective;
  125. procedure skipcomment;
  126. procedure skipdelphicomment;
  127. procedure skipoldtpcomment;
  128. procedure readtoken(allowrecordtoken:boolean);
  129. function readpreproc:ttoken;
  130. function asmgetcharstart : char;
  131. function asmgetchar:char;
  132. end;
  133. {$ifdef PREPROCWRITE}
  134. tpreprocfile=class
  135. f : text;
  136. buf : pointer;
  137. spacefound,
  138. eolfound : boolean;
  139. constructor create(const fn:string);
  140. destructor destroy;
  141. procedure Add(const s:string);
  142. procedure AddSpace;
  143. end;
  144. {$endif PREPROCWRITE}
  145. var
  146. { read strings }
  147. c : char;
  148. orgpattern,
  149. pattern : string;
  150. patternw : pcompilerwidestring;
  151. { token }
  152. token, { current token being parsed }
  153. idtoken : ttoken; { holds the token if the pattern is a known word }
  154. current_scanner : tscannerfile; { current scanner in use }
  155. aktcommentstyle : tcommentstyle; { needed to use read_comment from directives }
  156. {$ifdef PREPROCWRITE}
  157. preprocfile : tpreprocfile; { used with only preprocessing }
  158. {$endif PREPROCWRITE}
  159. type
  160. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  161. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  162. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  163. procedure InitScanner;
  164. procedure DoneScanner;
  165. {To be called when the language mode is finally determined}
  166. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  167. implementation
  168. uses
  169. SysUtils,
  170. cutils,cfileutils,
  171. systems,
  172. switches,
  173. symbase,symtable,symtype,symsym,symconst,symdef,defutil,
  174. fmodule;
  175. var
  176. { dictionaries with the supported directives }
  177. turbo_scannerdirectives : TFPHashObjectList; { for other modes }
  178. mac_scannerdirectives : TFPHashObjectList; { for mode mac }
  179. {*****************************************************************************
  180. Helper routines
  181. *****************************************************************************}
  182. const
  183. { use any special name that is an invalid file name to avoid problems }
  184. preprocstring : array [preproctyp] of string[7]
  185. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE','$ELSEIF');
  186. function is_keyword(const s:string):boolean;
  187. var
  188. low,high,mid : longint;
  189. begin
  190. if not (length(s) in [tokenlenmin..tokenlenmax]) or
  191. not (s[1] in ['a'..'z','A'..'Z']) then
  192. begin
  193. is_keyword:=false;
  194. exit;
  195. end;
  196. low:=ord(tokenidx^[length(s),s[1]].first);
  197. high:=ord(tokenidx^[length(s),s[1]].last);
  198. while low<high do
  199. begin
  200. mid:=(high+low+1) shr 1;
  201. if pattern<tokeninfo^[ttoken(mid)].str then
  202. high:=mid-1
  203. else
  204. low:=mid;
  205. end;
  206. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  207. (tokeninfo^[ttoken(high)].keyword in current_settings.modeswitches);
  208. end;
  209. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  210. var
  211. b : boolean;
  212. oldmodeswitches : tmodeswitches;
  213. begin
  214. oldmodeswitches:=current_settings.modeswitches;
  215. b:=true;
  216. if s='DEFAULT' then
  217. current_settings.modeswitches:=fpcmodeswitches
  218. else
  219. if s='DELPHI' then
  220. current_settings.modeswitches:=delphimodeswitches
  221. else
  222. if s='TP' then
  223. current_settings.modeswitches:=tpmodeswitches
  224. else
  225. if s='FPC' then begin
  226. current_settings.modeswitches:=fpcmodeswitches;
  227. { TODO: enable this for 2.3/2.9 }
  228. // include(current_settings.localswitches, cs_typed_addresses);
  229. end else
  230. if s='OBJFPC' then begin
  231. current_settings.modeswitches:=objfpcmodeswitches;
  232. { TODO: enable this for 2.3/2.9 }
  233. // include(current_settings.localswitches, cs_typed_addresses);
  234. end else
  235. if s='GPC' then
  236. current_settings.modeswitches:=gpcmodeswitches
  237. else
  238. if s='MACPAS' then
  239. current_settings.modeswitches:=macmodeswitches
  240. else
  241. b:=false;
  242. if b and changeInit then
  243. init_settings.modeswitches := current_settings.modeswitches;
  244. if b then
  245. begin
  246. { resolve all postponed switch changes }
  247. if localswitcheschanged then
  248. begin
  249. current_settings.localswitches:=nextlocalswitches;
  250. localswitcheschanged:=false;
  251. end;
  252. { turn ansistrings on by default ? }
  253. if (m_default_ansistring in current_settings.modeswitches) then
  254. begin
  255. include(current_settings.localswitches,cs_ansistrings);
  256. if changeinit then
  257. include(init_settings.localswitches,cs_ansistrings);
  258. end
  259. else
  260. begin
  261. exclude(current_settings.localswitches,cs_ansistrings);
  262. if changeinit then
  263. exclude(init_settings.localswitches,cs_ansistrings);
  264. end;
  265. { turn inline on by default ? }
  266. if (m_default_inline in current_settings.modeswitches) then
  267. begin
  268. include(current_settings.localswitches,cs_do_inline);
  269. if changeinit then
  270. include(init_settings.localswitches,cs_do_inline);
  271. end
  272. else
  273. begin
  274. exclude(current_settings.localswitches,cs_ansistrings);
  275. if changeinit then
  276. exclude(init_settings.localswitches,cs_ansistrings);
  277. end;
  278. { turn on bitpacking for mode macpas }
  279. if (m_mac in current_settings.modeswitches) then
  280. begin
  281. include(current_settings.localswitches,cs_bitpacking);
  282. if changeinit then
  283. include(init_settings.localswitches,cs_bitpacking);
  284. end;
  285. { support goto/label by default in delphi/tp7/mac modes }
  286. if ([m_delphi,m_tp7,m_mac] * current_settings.modeswitches <> []) then
  287. begin
  288. include(current_settings.moduleswitches,cs_support_goto);
  289. if changeinit then
  290. include(init_settings.moduleswitches,cs_support_goto);
  291. end;
  292. { Default enum packing for delphi/tp7 }
  293. if (m_tp7 in current_settings.modeswitches) or
  294. (m_delphi in current_settings.modeswitches) then
  295. current_settings.packenum:=1
  296. else if (m_mac in current_settings.modeswitches) then
  297. { compatible with Metrowerks Pascal }
  298. current_settings.packenum:=2
  299. else
  300. current_settings.packenum:=4;
  301. if changeinit then
  302. init_settings.packenum:=current_settings.packenum;
  303. {$ifdef i386}
  304. { Default to intel assembler for delphi/tp7 on i386 }
  305. if (m_delphi in current_settings.modeswitches) or
  306. (m_tp7 in current_settings.modeswitches) then
  307. current_settings.asmmode:=asmmode_i386_intel;
  308. if changeinit then
  309. init_settings.asmmode:=current_settings.asmmode;
  310. {$endif i386}
  311. { Exception support explicitly turned on (mainly for macpas, to }
  312. { compensate for lack of interprocedural goto support) }
  313. if (cs_support_exceptions in current_settings.globalswitches) then
  314. include(current_settings.modeswitches,m_except);
  315. { Undefine old symbol }
  316. if (m_delphi in oldmodeswitches) then
  317. undef_system_macro('FPC_DELPHI')
  318. else if (m_tp7 in oldmodeswitches) then
  319. undef_system_macro('FPC_TP')
  320. else if (m_objfpc in oldmodeswitches) then
  321. undef_system_macro('FPC_OBJFPC')
  322. else if (m_gpc in oldmodeswitches) then
  323. undef_system_macro('FPC_GPC')
  324. else if (m_mac in oldmodeswitches) then
  325. undef_system_macro('FPC_MACPAS');
  326. { define new symbol in delphi,objfpc,tp,gpc,macpas mode }
  327. if (m_delphi in current_settings.modeswitches) then
  328. def_system_macro('FPC_DELPHI')
  329. else if (m_tp7 in current_settings.modeswitches) then
  330. def_system_macro('FPC_TP')
  331. else if (m_objfpc in current_settings.modeswitches) then
  332. def_system_macro('FPC_OBJFPC')
  333. else if (m_gpc in current_settings.modeswitches) then
  334. def_system_macro('FPC_GPC')
  335. else if (m_mac in current_settings.modeswitches) then
  336. def_system_macro('FPC_MACPAS');
  337. end;
  338. SetCompileMode:=b;
  339. end;
  340. {*****************************************************************************
  341. Conditional Directives
  342. *****************************************************************************}
  343. procedure dir_else;
  344. begin
  345. current_scanner.elsepreprocstack;
  346. end;
  347. procedure dir_endif;
  348. begin
  349. current_scanner.poppreprocstack;
  350. end;
  351. function isdef(var valuedescr: String): Boolean;
  352. var
  353. hs : string;
  354. mac : tmacro;
  355. begin
  356. current_scanner.skipspace;
  357. hs:=current_scanner.readid;
  358. valuedescr:= hs;
  359. if hs='' then
  360. Message(scan_e_error_in_preproc_expr);
  361. mac:=tmacro(search_macro(hs));
  362. if assigned(mac) then
  363. mac.is_used:=true;
  364. isdef:= assigned(mac) and mac.defined;
  365. end;
  366. procedure dir_ifdef;
  367. begin
  368. current_scanner.ifpreprocstack(pp_ifdef,@isdef,scan_c_ifdef_found);
  369. end;
  370. function isnotdef(var valuedescr: String): Boolean;
  371. var
  372. hs : string;
  373. mac : tmacro;
  374. begin
  375. current_scanner.skipspace;
  376. hs:=current_scanner.readid;
  377. valuedescr:= hs;
  378. if hs='' then
  379. Message(scan_e_error_in_preproc_expr);
  380. mac:=tmacro(search_macro(hs));
  381. if assigned(mac) then
  382. mac.is_used:=true;
  383. isnotdef:= not (assigned(mac) and mac.defined);
  384. end;
  385. procedure dir_ifndef;
  386. begin
  387. current_scanner.ifpreprocstack(pp_ifndef,@isnotdef,scan_c_ifndef_found);
  388. end;
  389. function opt_check(var valuedescr: String): Boolean;
  390. var
  391. hs : string;
  392. state : char;
  393. begin
  394. opt_check:= false;
  395. current_scanner.skipspace;
  396. hs:=current_scanner.readid;
  397. valuedescr:= hs;
  398. if (length(hs)>1) then
  399. Message1(scan_w_illegal_switch,hs)
  400. else
  401. begin
  402. state:=current_scanner.ReadState;
  403. if state in ['-','+'] then
  404. opt_check:=CheckSwitch(hs[1],state)
  405. else
  406. Message(scan_e_error_in_preproc_expr);
  407. end;
  408. end;
  409. procedure dir_ifopt;
  410. begin
  411. current_scanner.ifpreprocstack(pp_ifopt,@opt_check,scan_c_ifopt_found);
  412. end;
  413. procedure dir_libprefix;
  414. var
  415. s : string;
  416. begin
  417. current_scanner.skipspace;
  418. if c <> '''' then
  419. Message2(scan_f_syn_expected, '''', c);
  420. s := current_scanner.readquotedstring;
  421. stringdispose(outputprefix);
  422. outputprefix := stringdup(s);
  423. with current_module do
  424. setfilename(paramfn^, paramallowoutput);
  425. end;
  426. procedure dir_libsuffix;
  427. var
  428. s : string;
  429. begin
  430. current_scanner.skipspace;
  431. if c <> '''' then
  432. Message2(scan_f_syn_expected, '''', c);
  433. s := current_scanner.readquotedstring;
  434. stringdispose(outputsuffix);
  435. outputsuffix := stringdup(s);
  436. with current_module do
  437. setfilename(paramfn^, paramallowoutput);
  438. end;
  439. procedure dir_extension;
  440. var
  441. s : string;
  442. begin
  443. current_scanner.skipspace;
  444. if c <> '''' then
  445. Message2(scan_f_syn_expected, '''', c);
  446. s := current_scanner.readquotedstring;
  447. if OutputFileName='' then
  448. OutputFileName:=InputFileName;
  449. OutputFileName:=ChangeFileExt(OutputFileName,'.'+s);
  450. with current_module do
  451. setfilename(paramfn^, paramallowoutput);
  452. end;
  453. {
  454. Compile time expression type check
  455. ----------------------------------
  456. Each subexpression returns its type to the caller, which then can
  457. do type check. Since data types of compile time expressions is
  458. not well defined, the type system does a best effort. The drawback is
  459. that some errors might not be detected.
  460. Instead of returning a particular data type, a set of possible data types
  461. are returned. This way ambigouos types can be handled. For instance a
  462. value of 1 can be both a boolean and and integer.
  463. Booleans
  464. --------
  465. The following forms of boolean values are supported:
  466. * C coded, that is 0 is false, non-zero is true.
  467. * TRUE/FALSE for mac style compile time variables
  468. Thus boolean mac compile time variables are always stored as TRUE/FALSE.
  469. When a compile time expression is evaluated, they are then translated
  470. to C coded booleans (0/1), to simplify for the expression evaluator.
  471. Note that this scheme then also of support mac compile time variables which
  472. are 0/1 but with a boolean meaning.
  473. The TRUE/FALSE format is new from 22 august 2005, but the above scheme
  474. means that units which is not recompiled, and thus stores
  475. compile time variables as the old format (0/1), continue to work.
  476. Short circuit evaluation
  477. ------------------------
  478. For this to work, the part of a compile time expression which is short
  479. circuited, should not be evaluated, while it still should be parsed.
  480. Therefor there is a parameter eval, telling whether evaluation is needed.
  481. In case not, the value returned can be arbitrary.
  482. }
  483. type
  484. {Compile time expression types}
  485. TCTEType = (ctetBoolean, ctetInteger, ctetString, ctetSet);
  486. TCTETypeSet = set of TCTEType;
  487. const
  488. cteTypeNames : array[TCTEType] of string[10] = (
  489. 'BOOLEAN','INTEGER','STRING','SET');
  490. {Subset of types which can be elements in sets.}
  491. setelementdefs = [ctetBoolean, ctetInteger, ctetString];
  492. function GetCTETypeName(t: TCTETypeSet): String;
  493. var
  494. i: TCTEType;
  495. begin
  496. result:= '';
  497. for i:= Low(TCTEType) to High(TCTEType) do
  498. if i in t then
  499. if result = '' then
  500. result:= cteTypeNames[i]
  501. else
  502. result:= result + ' or ' + cteTypeNames[i];
  503. end;
  504. procedure CTEError(actType, desiredExprType: TCTETypeSet; place: String);
  505. begin
  506. Message3(scan_e_compile_time_typeerror,
  507. GetCTETypeName(desiredExprType),
  508. GetCTETypeName(actType),
  509. place
  510. );
  511. end;
  512. function parse_compiler_expr(var compileExprType: TCTETypeSet):string;
  513. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string; forward;
  514. procedure preproc_consume(t : ttoken);
  515. begin
  516. if t<>current_scanner.preproc_token then
  517. Message(scan_e_preproc_syntax_error);
  518. current_scanner.preproc_token:=current_scanner.readpreproc;
  519. end;
  520. function preproc_substitutedtoken(var macroType: TCTETypeSet; eval : Boolean): string;
  521. { Currently this parses identifiers as well as numbers.
  522. The result from this procedure can either be that the token
  523. itself is a value, or that it is a compile time variable/macro,
  524. which then is substituted for another value (for macros
  525. recursivelly substituted).}
  526. var
  527. hs: string;
  528. mac : tmacro;
  529. macrocount,
  530. len : integer;
  531. numres : longint;
  532. w: word;
  533. begin
  534. result := current_scanner.preproc_pattern;
  535. if not eval then
  536. exit;
  537. mac:= nil;
  538. { Substitue macros and compiler variables with their content/value.
  539. For real macros also do recursive substitution. }
  540. macrocount:=0;
  541. repeat
  542. mac:=tmacro(search_macro(result));
  543. inc(macrocount);
  544. if macrocount>max_macro_nesting then
  545. begin
  546. Message(scan_w_macro_too_deep);
  547. break;
  548. end;
  549. if assigned(mac) and mac.defined then
  550. if assigned(mac.buftext) then
  551. begin
  552. if mac.buflen>255 then
  553. begin
  554. len:=255;
  555. Message(scan_w_macro_cut_after_255_chars);
  556. end
  557. else
  558. len:=mac.buflen;
  559. hs[0]:=char(len);
  560. move(mac.buftext^,hs[1],len);
  561. result:=upcase(hs);
  562. mac.is_used:=true;
  563. end
  564. else
  565. begin
  566. Message1(scan_e_error_macro_lacks_value, result);
  567. break;
  568. end
  569. else
  570. begin
  571. break;
  572. end;
  573. if mac.is_compiler_var then
  574. break;
  575. until false;
  576. { At this point, result do contain the value. Do some decoding and
  577. determine the type.}
  578. val(result,numres,w);
  579. if (w=0) then {It is an integer}
  580. begin
  581. if (numres = 0) or (numres = 1) then
  582. macroType := [ctetInteger, ctetBoolean]
  583. else
  584. macroType := [ctetInteger];
  585. end
  586. else if assigned(mac) and (m_mac in current_settings.modeswitches) and (result='FALSE') then
  587. begin
  588. result:= '0';
  589. macroType:= [ctetBoolean];
  590. end
  591. else if assigned(mac) and (m_mac in current_settings.modeswitches) and (result='TRUE') then
  592. begin
  593. result:= '1';
  594. macroType:= [ctetBoolean];
  595. end
  596. else if (m_mac in current_settings.modeswitches) and
  597. (not assigned(mac) or not mac.defined) and
  598. (macrocount = 1) then
  599. begin
  600. {Errors in mode mac is issued here. For non macpas modes there is
  601. more liberty, but the error will eventually be caught at a later stage.}
  602. Message1(scan_e_error_macro_undefined, result);
  603. macroType:= [ctetString]; {Just to have something}
  604. end
  605. else
  606. macroType:= [ctetString];
  607. end;
  608. function read_factor(var factorType: TCTETypeSet; eval : Boolean) : string;
  609. var
  610. hs : string;
  611. mac: tmacro;
  612. srsym : tsym;
  613. srsymtable : tsymtable;
  614. l : longint;
  615. w : integer;
  616. hasKlammer: Boolean;
  617. setElemType : TCTETypeSet;
  618. begin
  619. if current_scanner.preproc_token=_ID then
  620. begin
  621. if current_scanner.preproc_pattern='DEFINED' then
  622. begin
  623. factorType:= [ctetBoolean];
  624. preproc_consume(_ID);
  625. current_scanner.skipspace;
  626. if current_scanner.preproc_token =_LKLAMMER then
  627. begin
  628. preproc_consume(_LKLAMMER);
  629. current_scanner.skipspace;
  630. hasKlammer:= true;
  631. end
  632. else if (m_mac in current_settings.modeswitches) then
  633. hasKlammer:= false
  634. else
  635. Message(scan_e_error_in_preproc_expr);
  636. if current_scanner.preproc_token =_ID then
  637. begin
  638. hs := current_scanner.preproc_pattern;
  639. mac := tmacro(search_macro(hs));
  640. if assigned(mac) and mac.defined then
  641. begin
  642. hs := '1';
  643. mac.is_used:=true;
  644. end
  645. else
  646. hs := '0';
  647. read_factor := hs;
  648. preproc_consume(_ID);
  649. current_scanner.skipspace;
  650. end
  651. else
  652. Message(scan_e_error_in_preproc_expr);
  653. if hasKlammer then
  654. if current_scanner.preproc_token =_RKLAMMER then
  655. preproc_consume(_RKLAMMER)
  656. else
  657. Message(scan_e_error_in_preproc_expr);
  658. end
  659. else
  660. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  661. begin
  662. factorType:= [ctetBoolean];
  663. preproc_consume(_ID);
  664. current_scanner.skipspace;
  665. if current_scanner.preproc_token =_ID then
  666. begin
  667. hs := current_scanner.preproc_pattern;
  668. mac := tmacro(search_macro(hs));
  669. if assigned(mac) then
  670. begin
  671. hs := '0';
  672. mac.is_used:=true;
  673. end
  674. else
  675. hs := '1';
  676. read_factor := hs;
  677. preproc_consume(_ID);
  678. current_scanner.skipspace;
  679. end
  680. else
  681. Message(scan_e_error_in_preproc_expr);
  682. end
  683. else
  684. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  685. begin
  686. factorType:= [ctetBoolean];
  687. preproc_consume(_ID);
  688. current_scanner.skipspace;
  689. if current_scanner.preproc_token =_LKLAMMER then
  690. begin
  691. preproc_consume(_LKLAMMER);
  692. current_scanner.skipspace;
  693. end
  694. else
  695. Message(scan_e_error_in_preproc_expr);
  696. if not (current_scanner.preproc_token = _ID) then
  697. Message(scan_e_error_in_preproc_expr);
  698. hs:=current_scanner.preproc_pattern;
  699. if (length(hs) > 1) then
  700. {This is allowed in Metrowerks Pascal}
  701. Message(scan_e_error_in_preproc_expr)
  702. else
  703. begin
  704. if CheckSwitch(hs[1],'+') then
  705. read_factor := '1'
  706. else
  707. read_factor := '0';
  708. end;
  709. preproc_consume(_ID);
  710. current_scanner.skipspace;
  711. if current_scanner.preproc_token =_RKLAMMER then
  712. preproc_consume(_RKLAMMER)
  713. else
  714. Message(scan_e_error_in_preproc_expr);
  715. end
  716. else
  717. if current_scanner.preproc_pattern='SIZEOF' then
  718. begin
  719. factorType:= [ctetInteger];
  720. preproc_consume(_ID);
  721. current_scanner.skipspace;
  722. if current_scanner.preproc_token =_LKLAMMER then
  723. begin
  724. preproc_consume(_LKLAMMER);
  725. current_scanner.skipspace;
  726. end
  727. else
  728. Message(scan_e_preproc_syntax_error);
  729. if eval then
  730. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  731. begin
  732. l:=0;
  733. case srsym.typ of
  734. globalvarsym,
  735. localvarsym,
  736. paravarsym :
  737. l:=tabstractvarsym(srsym).getsize;
  738. typedconstsym :
  739. l:=ttypedconstsym(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.deftype 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. Message1(parser_c_macro_defined,mac.name);
  1120. current_module.localmacrosymtable.insert(mac);
  1121. end
  1122. else
  1123. begin
  1124. Message1(parser_c_macro_defined,mac.name);
  1125. mac.defined:=true;
  1126. mac.is_compiler_var:=false;
  1127. { delete old definition }
  1128. if assigned(mac.buftext) then
  1129. begin
  1130. freemem(mac.buftext,mac.buflen);
  1131. mac.buftext:=nil;
  1132. end;
  1133. end;
  1134. mac.is_used:=true;
  1135. if (cs_support_macro in current_settings.moduleswitches) then
  1136. begin
  1137. current_scanner.skipspace;
  1138. if not macstyle then
  1139. begin
  1140. { may be a macro? }
  1141. if c <> ':' then
  1142. exit;
  1143. current_scanner.readchar;
  1144. if c <> '=' then
  1145. exit;
  1146. current_scanner.readchar;
  1147. current_scanner.skipspace;
  1148. end;
  1149. { key words are never substituted }
  1150. if is_keyword(hs) then
  1151. Message(scan_e_keyword_cant_be_a_macro);
  1152. new(macrobuffer);
  1153. macropos:=0;
  1154. { parse macro, brackets are counted so it's possible
  1155. to have a $ifdef etc. in the macro }
  1156. bracketcount:=0;
  1157. repeat
  1158. case c of
  1159. '}' :
  1160. if (bracketcount=0) then
  1161. break
  1162. else
  1163. dec(bracketcount);
  1164. '{' :
  1165. inc(bracketcount);
  1166. #10,#13 :
  1167. current_scanner.linebreak;
  1168. #26 :
  1169. current_scanner.end_of_file;
  1170. end;
  1171. macrobuffer^[macropos]:=c;
  1172. inc(macropos);
  1173. if macropos>=maxmacrolen then
  1174. Message(scan_f_macro_buffer_overflow);
  1175. current_scanner.readchar;
  1176. until false;
  1177. { free buffer of macro ?}
  1178. if assigned(mac.buftext) then
  1179. freemem(mac.buftext,mac.buflen);
  1180. { get new mem }
  1181. getmem(mac.buftext,macropos);
  1182. mac.buflen:=macropos;
  1183. { copy the text }
  1184. move(macrobuffer^,mac.buftext^,macropos);
  1185. dispose(macrobuffer);
  1186. end
  1187. else
  1188. begin
  1189. { check if there is an assignment, then we need to give a
  1190. warning }
  1191. current_scanner.skipspace;
  1192. if c=':' then
  1193. begin
  1194. current_scanner.readchar;
  1195. if c='=' then
  1196. Message(scan_w_macro_support_turned_off);
  1197. end;
  1198. end;
  1199. end;
  1200. procedure dir_define;
  1201. begin
  1202. dir_define_impl(false);
  1203. end;
  1204. procedure dir_definec;
  1205. begin
  1206. dir_define_impl(true);
  1207. end;
  1208. procedure dir_setc;
  1209. var
  1210. hs : string;
  1211. mac : tmacro;
  1212. exprType: TCTETypeSet;
  1213. l : longint;
  1214. w : integer;
  1215. begin
  1216. current_scanner.skipspace;
  1217. hs:=current_scanner.readid;
  1218. mac:=tmacro(search_macro(hs));
  1219. if not assigned(mac) or
  1220. (mac.owner <> current_module.localmacrosymtable) then
  1221. begin
  1222. mac:=tmacro.create(hs);
  1223. mac.defined:=true;
  1224. mac.is_compiler_var:=true;
  1225. Message1(parser_c_macro_defined,mac.name);
  1226. current_module.localmacrosymtable.insert(mac);
  1227. end
  1228. else
  1229. begin
  1230. mac.defined:=true;
  1231. mac.is_compiler_var:=true;
  1232. { delete old definition }
  1233. if assigned(mac.buftext) then
  1234. begin
  1235. freemem(mac.buftext,mac.buflen);
  1236. mac.buftext:=nil;
  1237. end;
  1238. end;
  1239. mac.is_used:=true;
  1240. { key words are never substituted }
  1241. if is_keyword(hs) then
  1242. Message(scan_e_keyword_cant_be_a_macro);
  1243. { macro assignment can be both := and = }
  1244. current_scanner.skipspace;
  1245. if c=':' then
  1246. current_scanner.readchar;
  1247. if c='=' then
  1248. begin
  1249. current_scanner.readchar;
  1250. hs:= parse_compiler_expr(exprType);
  1251. if (exprType * [ctetBoolean, ctetInteger]) = [] then
  1252. CTEError(exprType, [ctetBoolean, ctetInteger], 'SETC');
  1253. if length(hs) <> 0 then
  1254. begin
  1255. {If we are absolutely shure it is boolean, translate
  1256. to TRUE/FALSE to increase possibility to do future type check}
  1257. if exprType = [ctetBoolean] then
  1258. begin
  1259. val(hs,l,w);
  1260. if l<>0 then
  1261. hs:='TRUE'
  1262. else
  1263. hs:='FALSE';
  1264. end;
  1265. Message2(parser_c_macro_set_to,mac.name,hs);
  1266. { free buffer of macro ?}
  1267. if assigned(mac.buftext) then
  1268. freemem(mac.buftext,mac.buflen);
  1269. { get new mem }
  1270. getmem(mac.buftext,length(hs));
  1271. mac.buflen:=length(hs);
  1272. { copy the text }
  1273. move(hs[1],mac.buftext^,mac.buflen);
  1274. end
  1275. else
  1276. Message(scan_e_preproc_syntax_error);
  1277. end
  1278. else
  1279. Message(scan_e_preproc_syntax_error);
  1280. end;
  1281. procedure dir_undef;
  1282. var
  1283. hs : string;
  1284. mac : tmacro;
  1285. begin
  1286. current_scanner.skipspace;
  1287. hs:=current_scanner.readid;
  1288. mac:=tmacro(search_macro(hs));
  1289. if not assigned(mac) or
  1290. (mac.owner <> current_module.localmacrosymtable) then
  1291. begin
  1292. mac:=tmacro.create(hs);
  1293. Message1(parser_c_macro_undefined,mac.name);
  1294. mac.defined:=false;
  1295. current_module.localmacrosymtable.insert(mac);
  1296. end
  1297. else
  1298. begin
  1299. Message1(parser_c_macro_undefined,mac.name);
  1300. mac.defined:=false;
  1301. mac.is_compiler_var:=false;
  1302. { delete old definition }
  1303. if assigned(mac.buftext) then
  1304. begin
  1305. freemem(mac.buftext,mac.buflen);
  1306. mac.buftext:=nil;
  1307. end;
  1308. end;
  1309. mac.is_used:=true;
  1310. end;
  1311. procedure dir_include;
  1312. function findincludefile(const path,name:string;var foundfile:string):boolean;
  1313. var
  1314. found : boolean;
  1315. hpath : string;
  1316. begin
  1317. (* look for the include file
  1318. If path was specified as part of {$I } then
  1319. 1. specified path (expanded with path of inputfile if relative)
  1320. else
  1321. 1. path of current inputfile,current dir
  1322. 2. local includepath
  1323. 3. global includepath *)
  1324. found:=false;
  1325. foundfile:='';
  1326. hpath:='';
  1327. if path<>'' then
  1328. begin
  1329. if not path_absolute(path) then
  1330. hpath:=current_scanner.inputfile.path^+path
  1331. else
  1332. hpath:=path;
  1333. found:=FindFile(name, hpath,true,foundfile);
  1334. end
  1335. else
  1336. begin
  1337. hpath:=current_scanner.inputfile.path^+';'+CurDirRelPath(source_info);
  1338. found:=FindFile(name, hpath,true,foundfile);
  1339. if not found then
  1340. found:=current_module.localincludesearchpath.FindFile(name,true,foundfile);
  1341. if not found then
  1342. found:=includesearchpath.FindFile(name,true,foundfile);
  1343. end;
  1344. result:=found;
  1345. end;
  1346. var
  1347. path,
  1348. name,
  1349. args,
  1350. foundfile,
  1351. hs : tpathstr;
  1352. hp : tinputfile;
  1353. found : boolean;
  1354. begin
  1355. current_scanner.skipspace;
  1356. args:=current_scanner.readcomment;
  1357. hs:=GetToken(args,' ');
  1358. if hs='' then
  1359. exit;
  1360. if (hs[1]='%') then
  1361. begin
  1362. { case insensitive }
  1363. hs:=upper(hs);
  1364. { remove %'s }
  1365. Delete(hs,1,1);
  1366. if hs[length(hs)]='%' then
  1367. Delete(hs,length(hs),1);
  1368. { save old }
  1369. path:=hs;
  1370. { first check for internal macros }
  1371. if hs='TIME' then
  1372. hs:=gettimestr
  1373. else
  1374. if hs='DATE' then
  1375. hs:=getdatestr
  1376. else
  1377. if hs='FILE' then
  1378. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex)
  1379. else
  1380. if hs='LINE' then
  1381. hs:=tostr(current_filepos.line)
  1382. else
  1383. if hs='FPCVERSION' then
  1384. hs:=version_string
  1385. else
  1386. if hs='FPCDATE' then
  1387. hs:=date_string
  1388. else
  1389. if hs='FPCTARGET' then
  1390. hs:=target_cpu_string
  1391. else
  1392. if hs='FPCTARGETCPU' then
  1393. hs:=target_cpu_string
  1394. else
  1395. if hs='FPCTARGETOS' then
  1396. hs:=target_info.shortname
  1397. else
  1398. hs:=GetEnvironmentVariable(hs);
  1399. if hs='' then
  1400. Message1(scan_w_include_env_not_found,path);
  1401. { make it a stringconst }
  1402. hs:=''''+hs+'''';
  1403. current_scanner.insertmacro(path,@hs[1],length(hs),
  1404. current_scanner.line_no,current_scanner.inputfile.ref_index);
  1405. end
  1406. else
  1407. begin
  1408. hs:=FixFileName(hs);
  1409. path:=ExtractFilePath(hs);
  1410. name:=ExtractFileName(hs);
  1411. { try to find the file }
  1412. found:=findincludefile(path,name,foundfile);
  1413. if (ExtractFileExt(name)='') then
  1414. begin
  1415. { try default extensions .inc , .pp and .pas }
  1416. if (not found) then
  1417. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  1418. if (not found) then
  1419. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  1420. if (not found) then
  1421. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  1422. end;
  1423. if current_scanner.inputfilecount<max_include_nesting then
  1424. begin
  1425. inc(current_scanner.inputfilecount);
  1426. { we need to reread the current char }
  1427. dec(current_scanner.inputpointer);
  1428. { shutdown current file }
  1429. current_scanner.tempcloseinputfile;
  1430. { load new file }
  1431. hp:=do_openinputfile(foundfile);
  1432. current_scanner.addfile(hp);
  1433. current_module.sourcefiles.register_file(hp);
  1434. if (not found) then
  1435. Message1(scan_f_cannot_open_includefile,hs);
  1436. if (not current_scanner.openinputfile) then
  1437. Message1(scan_f_cannot_open_includefile,hs);
  1438. Message1(scan_t_start_include_file,current_scanner.inputfile.path^+current_scanner.inputfile.name^);
  1439. current_scanner.reload;
  1440. end
  1441. else
  1442. Message(scan_f_include_deep_ten);
  1443. end;
  1444. end;
  1445. {*****************************************************************************
  1446. Preprocessor writting
  1447. *****************************************************************************}
  1448. {$ifdef PREPROCWRITE}
  1449. constructor tpreprocfile.create(const fn:string);
  1450. begin
  1451. { open outputfile }
  1452. assign(f,fn);
  1453. {$I-}
  1454. rewrite(f);
  1455. {$I+}
  1456. if ioresult<>0 then
  1457. Comment(V_Fatal,'can''t create file '+fn);
  1458. getmem(buf,preprocbufsize);
  1459. settextbuf(f,buf^,preprocbufsize);
  1460. { reset }
  1461. eolfound:=false;
  1462. spacefound:=false;
  1463. end;
  1464. destructor tpreprocfile.destroy;
  1465. begin
  1466. close(f);
  1467. freemem(buf,preprocbufsize);
  1468. end;
  1469. procedure tpreprocfile.add(const s:string);
  1470. begin
  1471. write(f,s);
  1472. end;
  1473. procedure tpreprocfile.addspace;
  1474. begin
  1475. if eolfound then
  1476. begin
  1477. writeln(f,'');
  1478. eolfound:=false;
  1479. spacefound:=false;
  1480. end
  1481. else
  1482. if spacefound then
  1483. begin
  1484. write(f,' ');
  1485. spacefound:=false;
  1486. end;
  1487. end;
  1488. {$endif PREPROCWRITE}
  1489. {*****************************************************************************
  1490. TPreProcStack
  1491. *****************************************************************************}
  1492. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  1493. begin
  1494. accept:=a;
  1495. typ:=atyp;
  1496. next:=n;
  1497. end;
  1498. {*****************************************************************************
  1499. TDirectiveItem
  1500. *****************************************************************************}
  1501. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  1502. begin
  1503. inherited Create(AList,n);
  1504. is_conditional:=false;
  1505. proc:=p;
  1506. end;
  1507. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  1508. begin
  1509. inherited Create(AList,n);
  1510. is_conditional:=true;
  1511. proc:=p;
  1512. end;
  1513. {****************************************************************************
  1514. TSCANNERFILE
  1515. ****************************************************************************}
  1516. constructor tscannerfile.create(const fn:string);
  1517. begin
  1518. inputfile:=do_openinputfile(fn);
  1519. if assigned(current_module) then
  1520. current_module.sourcefiles.register_file(inputfile);
  1521. { reset localinput }
  1522. inputbuffer:=nil;
  1523. inputpointer:=nil;
  1524. inputstart:=0;
  1525. { reset scanner }
  1526. preprocstack:=nil;
  1527. comment_level:=0;
  1528. yylexcount:=0;
  1529. block_type:=bt_general;
  1530. line_no:=0;
  1531. lastlinepos:=0;
  1532. lasttokenpos:=0;
  1533. lasttoken:=NOTOKEN;
  1534. nexttoken:=NOTOKEN;
  1535. lastasmgetchar:=#0;
  1536. ignoredirectives:=TStringList.Create;
  1537. in_asm_string:=false;
  1538. end;
  1539. procedure tscannerfile.firstfile;
  1540. begin
  1541. { load block }
  1542. if not openinputfile then
  1543. Message1(scan_f_cannot_open_input,inputfile.name^);
  1544. reload;
  1545. end;
  1546. destructor tscannerfile.destroy;
  1547. begin
  1548. if assigned(current_module) and
  1549. (current_module.state=ms_compiled) and
  1550. (status.errorcount=0) then
  1551. checkpreprocstack
  1552. else
  1553. begin
  1554. while assigned(preprocstack) do
  1555. poppreprocstack;
  1556. end;
  1557. if not inputfile.closed then
  1558. closeinputfile;
  1559. ignoredirectives.free;
  1560. end;
  1561. function tscannerfile.openinputfile:boolean;
  1562. begin
  1563. openinputfile:=inputfile.open;
  1564. { load buffer }
  1565. inputbuffer:=inputfile.buf;
  1566. inputpointer:=inputfile.buf;
  1567. inputstart:=inputfile.bufstart;
  1568. { line }
  1569. line_no:=0;
  1570. lastlinepos:=0;
  1571. lasttokenpos:=0;
  1572. end;
  1573. procedure tscannerfile.closeinputfile;
  1574. begin
  1575. inputfile.close;
  1576. { reset buffer }
  1577. inputbuffer:=nil;
  1578. inputpointer:=nil;
  1579. inputstart:=0;
  1580. { reset line }
  1581. line_no:=0;
  1582. lastlinepos:=0;
  1583. lasttokenpos:=0;
  1584. end;
  1585. function tscannerfile.tempopeninputfile:boolean;
  1586. begin
  1587. if inputfile.is_macro then
  1588. exit;
  1589. tempopeninputfile:=inputfile.tempopen;
  1590. { reload buffer }
  1591. inputbuffer:=inputfile.buf;
  1592. inputpointer:=inputfile.buf;
  1593. inputstart:=inputfile.bufstart;
  1594. end;
  1595. procedure tscannerfile.tempcloseinputfile;
  1596. begin
  1597. if inputfile.closed or inputfile.is_macro then
  1598. exit;
  1599. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  1600. inputfile.tempclose;
  1601. { reset buffer }
  1602. inputbuffer:=nil;
  1603. inputpointer:=nil;
  1604. inputstart:=0;
  1605. end;
  1606. procedure tscannerfile.saveinputfile;
  1607. begin
  1608. inputfile.saveinputpointer:=inputpointer;
  1609. inputfile.savelastlinepos:=lastlinepos;
  1610. inputfile.saveline_no:=line_no;
  1611. end;
  1612. procedure tscannerfile.restoreinputfile;
  1613. begin
  1614. inputpointer:=inputfile.saveinputpointer;
  1615. lastlinepos:=inputfile.savelastlinepos;
  1616. line_no:=inputfile.saveline_no;
  1617. if not inputfile.is_macro then
  1618. parser_current_file:=inputfile.name^;
  1619. end;
  1620. procedure tscannerfile.nextfile;
  1621. var
  1622. to_dispose : tinputfile;
  1623. begin
  1624. if assigned(inputfile.next) then
  1625. begin
  1626. if inputfile.is_macro then
  1627. to_dispose:=inputfile
  1628. else
  1629. begin
  1630. to_dispose:=nil;
  1631. dec(inputfilecount);
  1632. end;
  1633. { we can allways close the file, no ? }
  1634. inputfile.close;
  1635. inputfile:=inputfile.next;
  1636. if assigned(to_dispose) then
  1637. to_dispose.free;
  1638. restoreinputfile;
  1639. end;
  1640. end;
  1641. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  1642. begin
  1643. if not assigned(buf) then
  1644. internalerror(200511172);
  1645. if assigned(recordtokenbuf) then
  1646. internalerror(200511173);
  1647. recordtokenbuf:=buf;
  1648. fillchar(last_settings,sizeof(last_settings),0);
  1649. fillchar(last_filepos,sizeof(last_filepos),0);
  1650. end;
  1651. procedure tscannerfile.stoprecordtokens;
  1652. begin
  1653. if not assigned(recordtokenbuf) then
  1654. internalerror(200511174);
  1655. recordtokenbuf:=nil;
  1656. end;
  1657. procedure tscannerfile.recordtoken;
  1658. begin
  1659. if not assigned(recordtokenbuf) then
  1660. internalerror(200511176);
  1661. { settings changed? }
  1662. if CompareByte(current_settings,last_settings,sizeof(current_settings))<>0 then
  1663. begin
  1664. { use a special token to record it }
  1665. recordtokenbuf.write(_GENERICSPECIALTOKEN,1);
  1666. recordtokenbuf.write(ST_LOADSETTINGS,1);
  1667. recordtokenbuf.write(current_settings,sizeof(current_settings));
  1668. last_settings:=current_settings;
  1669. end;
  1670. { file pos changes? }
  1671. if current_tokenpos.line<>last_filepos.line then
  1672. begin
  1673. recordtokenbuf.write(_GENERICSPECIALTOKEN,1);
  1674. recordtokenbuf.write(ST_LINE,1);
  1675. recordtokenbuf.write(current_tokenpos.line,sizeof(current_tokenpos.line));
  1676. last_filepos.line:=current_tokenpos.line;
  1677. end;
  1678. if current_tokenpos.column<>last_filepos.column then
  1679. begin
  1680. recordtokenbuf.write(_GENERICSPECIALTOKEN,1);
  1681. recordtokenbuf.write(ST_COLUMN,1);
  1682. recordtokenbuf.write(current_tokenpos.column,sizeof(current_tokenpos.column));
  1683. last_filepos.column:=current_tokenpos.column;
  1684. end;
  1685. if current_tokenpos.fileindex<>last_filepos.fileindex then
  1686. begin
  1687. recordtokenbuf.write(_GENERICSPECIALTOKEN,1);
  1688. recordtokenbuf.write(ST_FILEINDEX,1);
  1689. recordtokenbuf.write(current_tokenpos.fileindex,sizeof(current_tokenpos.fileindex));
  1690. last_filepos.fileindex:=current_tokenpos.fileindex;
  1691. end;
  1692. recordtokenbuf.write(token,1);
  1693. if token=_ID then
  1694. recordtokenbuf.write(idtoken,1);
  1695. case token of
  1696. _CWCHAR,
  1697. _CWSTRING :
  1698. begin
  1699. recordtokenbuf.write(patternw^.len,sizeof(sizeint));
  1700. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  1701. end;
  1702. _CCHAR,
  1703. _CSTRING,
  1704. _INTCONST,
  1705. _REALNUMBER :
  1706. begin
  1707. recordtokenbuf.write(pattern[0],1);
  1708. recordtokenbuf.write(pattern[1],length(pattern));
  1709. end;
  1710. _ID :
  1711. begin
  1712. recordtokenbuf.write(orgpattern[0],1);
  1713. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  1714. end;
  1715. end;
  1716. end;
  1717. procedure tscannerfile.startreplaytokens(buf:tdynamicarray);
  1718. begin
  1719. if not assigned(buf) then
  1720. internalerror(200511175);
  1721. { save current token }
  1722. if token in [_CWCHAR,_CWSTRING,_CCHAR,_CSTRING,_INTCONST,_REALNUMBER,_ID] then
  1723. internalerror(200511178);
  1724. replaysavetoken:=token;
  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. exit;
  1751. end;
  1752. repeat
  1753. { load token from the buffer }
  1754. replaytokenbuf.read(token,1);
  1755. if token=_ID then
  1756. replaytokenbuf.read(idtoken,1)
  1757. else
  1758. idtoken:=_NOID;
  1759. case token of
  1760. _CWCHAR,
  1761. _CWSTRING :
  1762. begin
  1763. replaytokenbuf.read(wlen,sizeof(SizeInt));
  1764. setlengthwidestring(patternw,wlen);
  1765. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  1766. pattern:='';
  1767. end;
  1768. _CCHAR,
  1769. _CSTRING,
  1770. _INTCONST,
  1771. _REALNUMBER :
  1772. begin
  1773. replaytokenbuf.read(pattern[0],1);
  1774. replaytokenbuf.read(pattern[1],length(pattern));
  1775. orgpattern:='';
  1776. end;
  1777. _ID :
  1778. begin
  1779. replaytokenbuf.read(orgpattern[0],1);
  1780. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  1781. pattern:=upper(orgpattern);
  1782. end;
  1783. _GENERICSPECIALTOKEN:
  1784. begin
  1785. replaytokenbuf.read(specialtoken,1);
  1786. case specialtoken of
  1787. ST_LOADSETTINGS:
  1788. replaytokenbuf.read(current_settings,sizeof(current_settings));
  1789. ST_LINE:
  1790. begin
  1791. replaytokenbuf.read(current_tokenpos.line,sizeof(current_tokenpos.line));
  1792. current_filepos:=current_tokenpos;
  1793. end;
  1794. ST_COLUMN:
  1795. begin
  1796. replaytokenbuf.read(current_tokenpos.column,sizeof(current_tokenpos.column));
  1797. current_filepos:=current_tokenpos;
  1798. end;
  1799. ST_FILEINDEX:
  1800. begin
  1801. replaytokenbuf.read(current_tokenpos.fileindex,sizeof(current_tokenpos.fileindex));
  1802. current_filepos:=current_tokenpos;
  1803. end;
  1804. else
  1805. internalerror(2006103010);
  1806. end;
  1807. continue;
  1808. end;
  1809. end;
  1810. break;
  1811. until false;
  1812. end;
  1813. procedure tscannerfile.addfile(hp:tinputfile);
  1814. begin
  1815. saveinputfile;
  1816. { add to list }
  1817. hp.next:=inputfile;
  1818. inputfile:=hp;
  1819. { load new inputfile }
  1820. restoreinputfile;
  1821. end;
  1822. procedure tscannerfile.reload;
  1823. begin
  1824. with inputfile do
  1825. begin
  1826. { when nothing more to read then leave immediatly, so we
  1827. don't change the current_filepos and leave it point to the last
  1828. char }
  1829. if (c=#26) and (not assigned(next)) then
  1830. exit;
  1831. repeat
  1832. { still more to read?, then change the #0 to a space so its seen
  1833. as a seperator, this can't be used for macro's which can change
  1834. the place of the #0 in the buffer with tempopen }
  1835. if (c=#0) and (bufsize>0) and
  1836. not(inputfile.is_macro) and
  1837. (inputpointer-inputbuffer<bufsize) then
  1838. begin
  1839. c:=' ';
  1840. inc(inputpointer);
  1841. exit;
  1842. end;
  1843. { can we read more from this file ? }
  1844. if (c<>#26) and (not endoffile) then
  1845. begin
  1846. readbuf;
  1847. inputpointer:=buf;
  1848. inputbuffer:=buf;
  1849. inputstart:=bufstart;
  1850. { first line? }
  1851. if line_no=0 then
  1852. begin
  1853. c:=inputpointer^;
  1854. { eat utf-8 signature? }
  1855. if (ord(inputpointer^)=$ef) and
  1856. (ord((inputpointer+1)^)=$bb) and
  1857. (ord((inputpointer+2)^)=$bf) then
  1858. begin
  1859. inc(inputpointer,3);
  1860. message(scan_c_switching_to_utf8);
  1861. current_settings.sourcecodepage:='utf8';
  1862. end;
  1863. line_no:=1;
  1864. if cs_asm_source in current_settings.globalswitches then
  1865. inputfile.setline(line_no,bufstart);
  1866. end;
  1867. end
  1868. else
  1869. begin
  1870. { load eof position in tokenpos/current_filepos }
  1871. gettokenpos;
  1872. { close file }
  1873. closeinputfile;
  1874. { no next module, than EOF }
  1875. if not assigned(inputfile.next) then
  1876. begin
  1877. c:=#26;
  1878. exit;
  1879. end;
  1880. { load next file and reopen it }
  1881. nextfile;
  1882. tempopeninputfile;
  1883. { status }
  1884. Message1(scan_t_back_in,inputfile.name^);
  1885. end;
  1886. { load next char }
  1887. c:=inputpointer^;
  1888. inc(inputpointer);
  1889. until c<>#0; { if also end, then reload again }
  1890. end;
  1891. end;
  1892. procedure tscannerfile.insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  1893. var
  1894. hp : tinputfile;
  1895. begin
  1896. { save old postion }
  1897. dec(inputpointer);
  1898. tempcloseinputfile;
  1899. { create macro 'file' }
  1900. { use special name to dispose after !! }
  1901. hp:=do_openinputfile('_Macro_.'+macname);
  1902. addfile(hp);
  1903. with inputfile do
  1904. begin
  1905. setmacro(p,len);
  1906. { local buffer }
  1907. inputbuffer:=buf;
  1908. inputpointer:=buf;
  1909. inputstart:=bufstart;
  1910. ref_index:=fileindex;
  1911. end;
  1912. { reset line }
  1913. line_no:=line;
  1914. lastlinepos:=0;
  1915. lasttokenpos:=0;
  1916. { load new c }
  1917. c:=inputpointer^;
  1918. inc(inputpointer);
  1919. end;
  1920. procedure tscannerfile.gettokenpos;
  1921. { load the values of tokenpos and lasttokenpos }
  1922. begin
  1923. lasttokenpos:=inputstart+(inputpointer-inputbuffer);
  1924. current_tokenpos.line:=line_no;
  1925. current_tokenpos.column:=lasttokenpos-lastlinepos;
  1926. current_tokenpos.fileindex:=inputfile.ref_index;
  1927. current_tokenpos.moduleindex:=current_module.unit_index;
  1928. current_filepos:=current_tokenpos;
  1929. end;
  1930. procedure tscannerfile.inc_comment_level;
  1931. var
  1932. oldcurrent_filepos : tfileposinfo;
  1933. begin
  1934. if (m_nested_comment in current_settings.modeswitches) then
  1935. inc(comment_level)
  1936. else
  1937. comment_level:=1;
  1938. if (comment_level>1) then
  1939. begin
  1940. oldcurrent_filepos:=current_filepos;
  1941. gettokenpos; { update for warning }
  1942. Message1(scan_w_comment_level,tostr(comment_level));
  1943. current_filepos:=oldcurrent_filepos;
  1944. end;
  1945. end;
  1946. procedure tscannerfile.dec_comment_level;
  1947. begin
  1948. if (m_nested_comment in current_settings.modeswitches) then
  1949. dec(comment_level)
  1950. else
  1951. comment_level:=0;
  1952. end;
  1953. procedure tscannerfile.linebreak;
  1954. var
  1955. cur : char;
  1956. oldtokenpos,
  1957. oldcurrent_filepos : tfileposinfo;
  1958. begin
  1959. with inputfile do
  1960. begin
  1961. if (byte(inputpointer^)=0) and not(endoffile) then
  1962. begin
  1963. cur:=c;
  1964. reload;
  1965. if byte(cur)+byte(c)<>23 then
  1966. dec(inputpointer);
  1967. end
  1968. else
  1969. begin
  1970. { Support all combination of #10 and #13 as line break }
  1971. if (byte(inputpointer^)+byte(c)=23) then
  1972. inc(inputpointer);
  1973. end;
  1974. { Always return #10 as line break }
  1975. c:=#10;
  1976. { increase line counters }
  1977. lastlinepos:=bufstart+(inputpointer-inputbuffer);
  1978. inc(line_no);
  1979. { update linebuffer }
  1980. if cs_asm_source in current_settings.globalswitches then
  1981. inputfile.setline(line_no,lastlinepos);
  1982. { update for status and call the show status routine,
  1983. but don't touch current_filepos ! }
  1984. oldcurrent_filepos:=current_filepos;
  1985. oldtokenpos:=current_tokenpos;
  1986. gettokenpos; { update for v_status }
  1987. inc(status.compiledlines);
  1988. ShowStatus;
  1989. current_filepos:=oldcurrent_filepos;
  1990. current_tokenpos:=oldtokenpos;
  1991. end;
  1992. end;
  1993. procedure tscannerfile.illegal_char(c:char);
  1994. var
  1995. s : string;
  1996. begin
  1997. if c in [#32..#255] then
  1998. s:=''''+c+''''
  1999. else
  2000. s:='#'+tostr(ord(c));
  2001. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  2002. end;
  2003. procedure tscannerfile.end_of_file;
  2004. begin
  2005. checkpreprocstack;
  2006. Message(scan_f_end_of_file);
  2007. end;
  2008. {-------------------------------------------
  2009. IF Conditional Handling
  2010. -------------------------------------------}
  2011. procedure tscannerfile.checkpreprocstack;
  2012. begin
  2013. { check for missing ifdefs }
  2014. while assigned(preprocstack) do
  2015. begin
  2016. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  2017. preprocstack.owner.inputfile.name^,tostr(preprocstack.line_nb));
  2018. poppreprocstack;
  2019. end;
  2020. end;
  2021. procedure tscannerfile.poppreprocstack;
  2022. var
  2023. hp : tpreprocstack;
  2024. begin
  2025. if assigned(preprocstack) then
  2026. begin
  2027. Message1(scan_c_endif_found,preprocstack.name);
  2028. hp:=preprocstack.next;
  2029. preprocstack.free;
  2030. preprocstack:=hp;
  2031. end
  2032. else
  2033. Message(scan_e_endif_without_if);
  2034. end;
  2035. procedure tscannerfile.ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  2036. var
  2037. condition: Boolean;
  2038. valuedescr: String;
  2039. begin
  2040. if (preprocstack=nil) or preprocstack.accept then
  2041. condition:= compile_time_predicate(valuedescr)
  2042. else
  2043. begin
  2044. condition:= false;
  2045. valuedescr:= '';
  2046. end;
  2047. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  2048. preprocstack.name:=valuedescr;
  2049. preprocstack.line_nb:=line_no;
  2050. preprocstack.owner:=self;
  2051. if preprocstack.accept then
  2052. Message2(messid,preprocstack.name,'accepted')
  2053. else
  2054. Message2(messid,preprocstack.name,'rejected');
  2055. end;
  2056. procedure tscannerfile.elsepreprocstack;
  2057. begin
  2058. if assigned(preprocstack) and
  2059. (preprocstack.typ<>pp_else) then
  2060. begin
  2061. if (preprocstack.typ=pp_elseif) then
  2062. preprocstack.accept:=false
  2063. else
  2064. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  2065. preprocstack.accept:=not preprocstack.accept;
  2066. preprocstack.typ:=pp_else;
  2067. preprocstack.line_nb:=line_no;
  2068. if preprocstack.accept then
  2069. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2070. else
  2071. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2072. end
  2073. else
  2074. Message(scan_e_endif_without_if);
  2075. end;
  2076. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  2077. var
  2078. valuedescr: String;
  2079. begin
  2080. if assigned(preprocstack) and
  2081. (preprocstack.typ in [pp_if,pp_elseif]) then
  2082. begin
  2083. { when the branch is accepted we use pp_elseif so we know that
  2084. all the next branches need to be rejected. when this branch is still
  2085. not accepted then leave it at pp_if }
  2086. if (preprocstack.typ=pp_elseif) then
  2087. preprocstack.accept:=false
  2088. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  2089. begin
  2090. preprocstack.accept:=false;
  2091. preprocstack.typ:=pp_elseif;
  2092. end
  2093. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  2094. and compile_time_predicate(valuedescr) then
  2095. begin
  2096. preprocstack.name:=valuedescr;
  2097. preprocstack.accept:=true;
  2098. preprocstack.typ:=pp_elseif;
  2099. end;
  2100. preprocstack.line_nb:=line_no;
  2101. if preprocstack.accept then
  2102. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2103. else
  2104. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2105. end
  2106. else
  2107. Message(scan_e_endif_without_if);
  2108. end;
  2109. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  2110. var
  2111. oldcurrent_filepos : tfileposinfo;
  2112. begin
  2113. oldcurrent_filepos:=current_filepos;
  2114. repeat
  2115. current_scanner.gettokenpos;
  2116. p.proc();
  2117. { accept the text ? }
  2118. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  2119. break
  2120. else
  2121. begin
  2122. current_scanner.gettokenpos;
  2123. Message(scan_c_skipping_until);
  2124. repeat
  2125. current_scanner.skipuntildirective;
  2126. if not (m_mac in current_settings.modeswitches) then
  2127. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  2128. else
  2129. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  2130. until assigned(p) and (p.is_conditional);
  2131. current_scanner.gettokenpos;
  2132. Message1(scan_d_handling_switch,'$'+p.name);
  2133. end;
  2134. until false;
  2135. current_filepos:=oldcurrent_filepos;
  2136. end;
  2137. procedure tscannerfile.handledirectives;
  2138. var
  2139. t : tdirectiveitem;
  2140. hs : string;
  2141. begin
  2142. gettokenpos;
  2143. readchar; {Remove the $}
  2144. hs:=readid;
  2145. {$ifdef PREPROCWRITE}
  2146. if parapreprocess then
  2147. begin
  2148. t:=Get_Directive(hs);
  2149. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  2150. begin
  2151. preprocfile^.AddSpace;
  2152. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  2153. exit;
  2154. end;
  2155. end;
  2156. {$endif PREPROCWRITE}
  2157. { skip this directive? }
  2158. if (ignoredirectives.find(hs)<>nil) then
  2159. begin
  2160. if (comment_level>0) then
  2161. readcomment;
  2162. { we've read the whole comment }
  2163. aktcommentstyle:=comment_none;
  2164. exit;
  2165. end;
  2166. if hs='' then
  2167. begin
  2168. Message1(scan_w_illegal_switch,'$'+hs);
  2169. end;
  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.insert(hs);
  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. {$ifndef arm}
  3632. // the { ... } is used in ARM assembler to define register sets, so we can't used
  3633. // it as comment, either (* ... *), /* ... */ or // ... should be used instead
  3634. '{' :
  3635. skipcomment;
  3636. {$endif arm}
  3637. #10,#13 :
  3638. begin
  3639. linebreak;
  3640. asmgetchar:=c;
  3641. exit;
  3642. end;
  3643. #26 :
  3644. begin
  3645. reload;
  3646. if (c=#26) and not assigned(inputfile.next) then
  3647. end_of_file;
  3648. continue;
  3649. end;
  3650. '/' :
  3651. begin
  3652. readchar;
  3653. if c='/' then
  3654. skipdelphicomment
  3655. else
  3656. begin
  3657. asmgetchar:='/';
  3658. lastasmgetchar:=c;
  3659. exit;
  3660. end;
  3661. end;
  3662. '(' :
  3663. begin
  3664. readchar;
  3665. if c='*' then
  3666. begin
  3667. c:=#0;{Signal skipoldtpcomment to reload a char }
  3668. skipoldtpcomment;
  3669. end
  3670. else
  3671. begin
  3672. asmgetchar:='(';
  3673. lastasmgetchar:=c;
  3674. exit;
  3675. end;
  3676. end;
  3677. else
  3678. begin
  3679. asmgetchar:=c;
  3680. exit;
  3681. end;
  3682. end;
  3683. until false;
  3684. end;
  3685. {*****************************************************************************
  3686. Helpers
  3687. *****************************************************************************}
  3688. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  3689. begin
  3690. if dm in [directive_all, directive_turbo] then
  3691. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  3692. if dm in [directive_all, directive_mac] then
  3693. tdirectiveitem.create(mac_scannerdirectives,s,p);
  3694. end;
  3695. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  3696. begin
  3697. if dm in [directive_all, directive_turbo] then
  3698. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  3699. if dm in [directive_all, directive_mac] then
  3700. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  3701. end;
  3702. {*****************************************************************************
  3703. Initialization
  3704. *****************************************************************************}
  3705. procedure InitScanner;
  3706. begin
  3707. InitWideString(patternw);
  3708. turbo_scannerdirectives:=TFPHashObjectList.Create;
  3709. mac_scannerdirectives:=TFPHashObjectList.Create;
  3710. { Common directives and conditionals }
  3711. AddDirective('I',directive_all, @dir_include);
  3712. AddDirective('DEFINE',directive_all, @dir_define);
  3713. AddDirective('UNDEF',directive_all, @dir_undef);
  3714. AddConditional('IF',directive_all, @dir_if);
  3715. AddConditional('IFDEF',directive_all, @dir_ifdef);
  3716. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  3717. AddConditional('ELSE',directive_all, @dir_else);
  3718. AddConditional('ELSEIF',directive_all, @dir_elseif);
  3719. AddConditional('ENDIF',directive_all, @dir_endif);
  3720. { Directives and conditionals for all modes except mode macpas}
  3721. AddDirective('INCLUDE',directive_turbo, @dir_include);
  3722. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  3723. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  3724. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  3725. AddConditional('IFEND',directive_turbo, @dir_endif);
  3726. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  3727. { Directives and conditionals for mode macpas: }
  3728. AddDirective('SETC',directive_mac, @dir_setc);
  3729. AddDirective('DEFINEC',directive_mac, @dir_definec);
  3730. AddDirective('UNDEFC',directive_mac, @dir_undef);
  3731. AddConditional('IFC',directive_mac, @dir_if);
  3732. AddConditional('ELSEC',directive_mac, @dir_else);
  3733. AddConditional('ELIFC',directive_mac, @dir_elseif);
  3734. AddConditional('ENDC',directive_mac, @dir_endif);
  3735. end;
  3736. procedure DoneScanner;
  3737. begin
  3738. turbo_scannerdirectives.Free;
  3739. mac_scannerdirectives.Free;
  3740. DoneWideString(patternw);
  3741. end;
  3742. end.