scanner.pas 106 KB

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