scanner.pas 131 KB

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