scanner.pas 129 KB

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