scanner.pas 121 KB

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