scanner.pas 124 KB

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