scanner.pas 136 KB

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