scanner.pas 136 KB

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