scanner.pas 129 KB

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