scanner.pas 130 KB

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