scanner.pas 122 KB

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