scanner.pas 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the scanner part and handling of the switches
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit scanner;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. globtype,globals,version,tokens,
  24. verbose,comphook,
  25. finput,
  26. widestr,cpuinfo;
  27. const
  28. max_include_nesting=32;
  29. max_macro_nesting=16;
  30. maxmacrolen=16*1024;
  31. preprocbufsize=32*1024;
  32. type
  33. tcommentstyle = (comment_none,comment_tp,comment_oldtp,comment_delphi,comment_c);
  34. pmacrobuffer = ^tmacrobuffer;
  35. tmacrobuffer = array[0..maxmacrolen-1] of char;
  36. tscannerfile = class;
  37. tmacro = class(TNamedIndexItem)
  38. defined,
  39. defined_at_startup,
  40. is_used : boolean;
  41. buftext : pchar;
  42. buflen : longint;
  43. fileinfo : tfileposinfo;
  44. constructor Create(const n : string);
  45. destructor destroy;override;
  46. end;
  47. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else);
  48. tpreprocstack = class
  49. typ : preproctyp;
  50. accept : boolean;
  51. next : tpreprocstack;
  52. name : stringid;
  53. line_nb : longint;
  54. owner : tscannerfile;
  55. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  56. end;
  57. tdirectiveproc=procedure;
  58. tdirectiveitem = class(TNamedIndexItem)
  59. public
  60. is_conditional : boolean;
  61. proc : tdirectiveproc;
  62. constructor Create(const n:string;p:tdirectiveproc);
  63. constructor CreateCond(const n:string;p:tdirectiveproc);
  64. end;
  65. tscannerfile = class
  66. public
  67. inputfile : tinputfile; { current inputfile list }
  68. inputfilecount : longint;
  69. inputbuffer, { input buffer }
  70. inputpointer : pchar;
  71. inputstart : longint;
  72. line_no, { line }
  73. lastlinepos : longint;
  74. lasttokenpos : longint; { token }
  75. lasttoken,
  76. nexttoken : ttoken;
  77. comment_level,
  78. yylexcount : longint;
  79. lastasmgetchar : char;
  80. ignoredirectives : tstringlist; { ignore directives, used to give warnings only once }
  81. preprocstack : tpreprocstack;
  82. macros : Tdictionary;
  83. in_asm_string : boolean;
  84. preproc_pattern : string;
  85. preproc_token : ttoken;
  86. constructor Create(const fn:string);
  87. destructor Destroy;override;
  88. { File buffer things }
  89. function openinputfile:boolean;
  90. procedure closeinputfile;
  91. function tempopeninputfile:boolean;
  92. procedure tempcloseinputfile;
  93. procedure saveinputfile;
  94. procedure restoreinputfile;
  95. procedure firstfile;
  96. procedure nextfile;
  97. procedure addfile(hp:tinputfile);
  98. procedure reload;
  99. procedure insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  100. { Scanner things }
  101. procedure def_macro(const s : string);
  102. procedure set_macro(const s : string;value : string);
  103. procedure gettokenpos;
  104. procedure inc_comment_level;
  105. procedure dec_comment_level;
  106. procedure illegal_char(c:char);
  107. procedure end_of_file;
  108. procedure checkpreprocstack;
  109. procedure poppreprocstack;
  110. procedure addpreprocstack(atyp : preproctyp;a:boolean;const s:string;w:longint);
  111. procedure elsepreprocstack;
  112. procedure handleconditional(p:tdirectiveitem);
  113. procedure handledirectives;
  114. procedure linebreak;
  115. procedure readchar;
  116. procedure readstring;
  117. procedure readnumber;
  118. function readid:string;
  119. function readval:longint;
  120. function readcomment:string;
  121. function readstate:char;
  122. procedure skipspace;
  123. procedure skipuntildirective;
  124. procedure skipcomment;
  125. procedure skipdelphicomment;
  126. procedure skipoldtpcomment;
  127. procedure readtoken;
  128. function readpreproc:ttoken;
  129. function asmgetcharstart : char;
  130. function asmgetchar:char;
  131. end;
  132. {$ifdef PREPROCWRITE}
  133. tpreprocfile=class
  134. f : text;
  135. buf : pointer;
  136. spacefound,
  137. eolfound : boolean;
  138. constructor create(const fn:string);
  139. destructor destroy;
  140. procedure Add(const s:string);
  141. procedure AddSpace;
  142. end;
  143. {$endif PREPROCWRITE}
  144. var
  145. { read strings }
  146. c : char;
  147. orgpattern,
  148. pattern : string;
  149. patternw : pcompilerwidestring;
  150. { token }
  151. token, { current token being parsed }
  152. idtoken : ttoken; { holds the token if the pattern is a known word }
  153. current_scanner : tscannerfile; { current scanner in use }
  154. aktcommentstyle : tcommentstyle; { needed to use read_comment from directives }
  155. {$ifdef PREPROCWRITE}
  156. preprocfile : tpreprocfile; { used with only preprocessing }
  157. {$endif PREPROCWRITE}
  158. type
  159. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  160. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  161. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  162. procedure InitScanner;
  163. procedure DoneScanner;
  164. implementation
  165. uses
  166. {$ifdef delphi}
  167. dmisc,
  168. {$else}
  169. dos,
  170. {$endif delphi}
  171. cutils,
  172. systems,
  173. switches,
  174. fmodule;
  175. var
  176. { dictionaries with the supported directives }
  177. turbo_scannerdirectives : tdictionary; { for other modes }
  178. mac_scannerdirectives : tdictionary; { for mode mac }
  179. {*****************************************************************************
  180. Helper routines
  181. *****************************************************************************}
  182. const
  183. { use any special name that is an invalid file name to avoid problems }
  184. preprocstring : array [preproctyp] of string[7]
  185. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE');
  186. function is_keyword(const s:string):boolean;
  187. var
  188. low,high,mid : longint;
  189. begin
  190. if not (length(s) in [tokenlenmin..tokenlenmax]) then
  191. begin
  192. is_keyword:=false;
  193. exit;
  194. end;
  195. low:=ord(tokenidx^[length(s),s[1]].first);
  196. high:=ord(tokenidx^[length(s),s[1]].last);
  197. while low<high do
  198. begin
  199. mid:=(high+low+1) shr 1;
  200. if pattern<tokeninfo^[ttoken(mid)].str then
  201. high:=mid-1
  202. else
  203. low:=mid;
  204. end;
  205. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  206. (tokeninfo^[ttoken(high)].keyword in aktmodeswitches);
  207. end;
  208. {*****************************************************************************
  209. Conditional Directives
  210. *****************************************************************************}
  211. procedure dir_else;
  212. begin
  213. current_scanner.elsepreprocstack;
  214. end;
  215. procedure dir_endif;
  216. begin
  217. current_scanner.poppreprocstack;
  218. end;
  219. procedure dir_ifdef;
  220. var
  221. hs : string;
  222. mac : tmacro;
  223. begin
  224. current_scanner.skipspace;
  225. hs:=current_scanner.readid;
  226. if hs='' then
  227. Message(scan_e_error_in_preproc_expr);
  228. mac:=tmacro(current_scanner.macros.search(hs));
  229. if assigned(mac) then
  230. mac.is_used:=true;
  231. current_scanner.addpreprocstack(pp_ifdef,assigned(mac) and mac.defined,hs,scan_c_ifdef_found);
  232. end;
  233. procedure dir_ifndef;
  234. var
  235. hs : string;
  236. mac : tmacro;
  237. begin
  238. current_scanner.skipspace;
  239. hs:=current_scanner.readid;
  240. if hs='' then
  241. Message(scan_e_error_in_preproc_expr);
  242. mac:=tmacro(current_scanner.macros.search(hs));
  243. if assigned(mac) then
  244. mac.is_used:=true;
  245. current_scanner.addpreprocstack(pp_ifndef,not(assigned(mac) and mac.defined),hs,scan_c_ifndef_found);
  246. end;
  247. procedure dir_ifopt;
  248. var
  249. hs : string;
  250. found : boolean;
  251. state : char;
  252. begin
  253. current_scanner.skipspace;
  254. hs:=current_scanner.readid;
  255. if (length(hs)>1) then
  256. Message1(scan_w_illegal_switch,hs)
  257. else
  258. begin
  259. state:=current_scanner.ReadState;
  260. if state in ['-','+'] then
  261. found:=CheckSwitch(hs[1],state);
  262. end;
  263. current_scanner.addpreprocstack(pp_ifopt,found,hs,scan_c_ifopt_found);
  264. end;
  265. function parse_compiler_expr:string;
  266. function read_expr : string; forward;
  267. procedure preproc_consume(t : ttoken);
  268. begin
  269. if t<>current_scanner.preproc_token then
  270. Message(scan_e_preproc_syntax_error);
  271. current_scanner.preproc_token:=current_scanner.readpreproc;
  272. end;
  273. function readpreproc: string;
  274. var
  275. hs: string;
  276. mac : tmacro;
  277. len : integer;
  278. begin
  279. hs := current_scanner.preproc_pattern;
  280. mac:=tmacro(current_scanner.macros.search(hs));
  281. if assigned(mac) then
  282. begin
  283. if mac.defined and assigned(mac.buftext) then
  284. begin
  285. if mac.buflen>255 then
  286. begin
  287. len:=255;
  288. Message(scan_w_macro_cut_after_255_chars);
  289. end
  290. else
  291. len:=mac.buflen;
  292. hs[0]:=char(len);
  293. move(mac.buftext^,hs[1],len);
  294. end;
  295. end;
  296. readpreproc := hs;
  297. end;
  298. function read_factor : string;
  299. var
  300. hs : string;
  301. mac: tmacro;
  302. begin
  303. if current_scanner.preproc_token=_ID then
  304. begin
  305. if readpreproc='DEFINED' then
  306. begin
  307. preproc_consume(_ID);
  308. current_scanner.skipspace;
  309. if current_scanner.preproc_token =_LKLAMMER then
  310. begin
  311. preproc_consume(_LKLAMMER);
  312. current_scanner.skipspace;
  313. end
  314. else
  315. Message(scan_e_error_in_preproc_expr);
  316. if current_scanner.preproc_token =_ID then
  317. begin
  318. hs := current_scanner.preproc_pattern;
  319. mac := tmacro(current_scanner.macros.search(hs));
  320. if assigned(mac) then
  321. hs := '1'
  322. else
  323. hs := '0';
  324. read_factor := hs;
  325. preproc_consume(_ID);
  326. current_scanner.skipspace;
  327. end
  328. else
  329. Message(scan_e_error_in_preproc_expr);
  330. if current_scanner.preproc_token =_RKLAMMER then
  331. preproc_consume(_RKLAMMER)
  332. else
  333. Message(scan_e_error_in_preproc_expr);
  334. end
  335. else
  336. if readpreproc='NOT' then
  337. begin
  338. preproc_consume(_ID);
  339. hs:=read_expr;
  340. if hs='0' then
  341. read_factor:='1'
  342. else
  343. read_factor:='0';
  344. end
  345. else
  346. begin
  347. hs:=readpreproc;
  348. preproc_consume(_ID);
  349. read_factor:=hs;
  350. end
  351. end
  352. else if current_scanner.preproc_token =_LKLAMMER then
  353. begin
  354. preproc_consume(_LKLAMMER);
  355. read_factor:=read_expr;
  356. preproc_consume(_RKLAMMER);
  357. end
  358. else
  359. Message(scan_e_error_in_preproc_expr);
  360. end;
  361. function read_term : string;
  362. var
  363. hs1,hs2 : string;
  364. l1,l2 : longint;
  365. w : integer;
  366. begin
  367. hs1:=read_factor;
  368. while true do
  369. begin
  370. if (current_scanner.preproc_token=_ID) then
  371. begin
  372. if readpreproc='AND' then
  373. begin
  374. preproc_consume(_ID);
  375. hs2:=read_expr;
  376. valint(hs1,l1,w); valint(hs2,l2,w);
  377. if (l1>0) and (l2>0) then
  378. hs1:='1'
  379. else
  380. hs1:='0';
  381. read_term := hs1;
  382. exit;
  383. end
  384. else
  385. break;
  386. end
  387. else
  388. break;
  389. end;
  390. read_term:=hs1;
  391. end;
  392. function read_simple_expr : string;
  393. var
  394. hs1,hs2 : string;
  395. l1,l2 : longint;
  396. w : integer;
  397. begin
  398. hs1:=read_term;
  399. while true do
  400. begin
  401. if (current_scanner.preproc_token=_ID) then
  402. begin
  403. if readpreproc='OR' then
  404. begin
  405. preproc_consume(_ID);
  406. hs2:=read_expr;
  407. valint(hs1,l1,w); valint(hs2,l2,w);
  408. if (l1>0) or (l2>0) then
  409. hs1:='1'
  410. else
  411. hs1:='0';
  412. read_simple_expr := hs1;
  413. exit;
  414. end
  415. else
  416. break;
  417. end
  418. else
  419. break;
  420. end;
  421. read_simple_expr:=hs1;
  422. end;
  423. function read_expr : string;
  424. var
  425. hs1,hs2 : string;
  426. b : boolean;
  427. t : ttoken;
  428. w : integer;
  429. l1,l2 : longint;
  430. begin
  431. hs1:=read_simple_expr;
  432. t:=current_scanner.preproc_token;
  433. if not(t in [_EQUAL,_UNEQUAL,_LT,_GT,_LTE,_GTE]) then
  434. begin
  435. read_expr:=hs1;
  436. exit;
  437. end;
  438. preproc_consume(t);
  439. hs2:=read_simple_expr;
  440. if is_number(hs1) and is_number(hs2) then
  441. begin
  442. valint(hs1,l1,w);
  443. valint(hs2,l2,w);
  444. case t of
  445. _EQUAL : b:=l1=l2;
  446. _UNEQUAL : b:=l1<>l2;
  447. _LT : b:=l1<l2;
  448. _GT : b:=l1>l2;
  449. _GTE : b:=l1>=l2;
  450. _LTE : b:=l1<=l2;
  451. end;
  452. end
  453. else
  454. begin
  455. case t of
  456. _EQUAL : b:=hs1=hs2;
  457. _UNEQUAL : b:=hs1<>hs2;
  458. _LT : b:=hs1<hs2;
  459. _GT : b:=hs1>hs2;
  460. _GTE : b:=hs1>=hs2;
  461. _LTE : b:=hs1<=hs2;
  462. end;
  463. end;
  464. if b then
  465. read_expr:='1'
  466. else
  467. read_expr:='0';
  468. end;
  469. begin
  470. current_scanner.skipspace;
  471. { start preproc expression scanner }
  472. current_scanner.preproc_token:=current_scanner.readpreproc;
  473. parse_compiler_expr:=read_expr;
  474. end;
  475. procedure dir_if;
  476. var
  477. hs : string;
  478. begin
  479. hs:=parse_compiler_expr;
  480. current_scanner.addpreprocstack(pp_if,hs<>'0',hs,scan_c_if_found);
  481. end;
  482. procedure dir_define;
  483. var
  484. hs : string;
  485. bracketcount : longint;
  486. mac : tmacro;
  487. macropos : longint;
  488. macrobuffer : pmacrobuffer;
  489. begin
  490. current_scanner.skipspace;
  491. hs:=current_scanner.readid;
  492. mac:=tmacro(current_scanner.macros.search(hs));
  493. if not assigned(mac) then
  494. begin
  495. mac:=tmacro.create(hs);
  496. mac.defined:=true;
  497. Message1(parser_c_macro_defined,mac.name);
  498. current_scanner.macros.insert(mac);
  499. end
  500. else
  501. begin
  502. Message1(parser_c_macro_defined,mac.name);
  503. mac.defined:=true;
  504. { delete old definition }
  505. if assigned(mac.buftext) then
  506. begin
  507. freemem(mac.buftext,mac.buflen);
  508. mac.buftext:=nil;
  509. end;
  510. end;
  511. mac.is_used:=true;
  512. if (cs_support_macro in aktmoduleswitches) then
  513. begin
  514. { key words are never substituted }
  515. if is_keyword(hs) then
  516. Message(scan_e_keyword_cant_be_a_macro);
  517. { !!!!!! handle macro params, need we this? }
  518. current_scanner.skipspace;
  519. { may be a macro? }
  520. if c=':' then
  521. begin
  522. current_scanner.readchar;
  523. if c='=' then
  524. begin
  525. new(macrobuffer);
  526. macropos:=0;
  527. { parse macro, brackets are counted so it's possible
  528. to have a $ifdef etc. in the macro }
  529. bracketcount:=0;
  530. repeat
  531. current_scanner.readchar;
  532. case c of
  533. '}' :
  534. if (bracketcount=0) then
  535. break
  536. else
  537. dec(bracketcount);
  538. '{' :
  539. inc(bracketcount);
  540. #10,#13 :
  541. current_scanner.linebreak;
  542. #26 :
  543. current_scanner.end_of_file;
  544. end;
  545. macrobuffer^[macropos]:=c;
  546. inc(macropos);
  547. if macropos>maxmacrolen then
  548. Message(scan_f_macro_buffer_overflow);
  549. until false;
  550. { free buffer of macro ?}
  551. if assigned(mac.buftext) then
  552. freemem(mac.buftext,mac.buflen);
  553. { get new mem }
  554. getmem(mac.buftext,macropos);
  555. mac.buflen:=macropos;
  556. { copy the text }
  557. move(macrobuffer^,mac.buftext^,macropos);
  558. dispose(macrobuffer);
  559. end;
  560. end;
  561. end
  562. else
  563. begin
  564. { check if there is an assignment, then we need to give a
  565. warning }
  566. current_scanner.skipspace;
  567. if c=':' then
  568. begin
  569. current_scanner.readchar;
  570. if c='=' then
  571. Message(scan_w_macro_support_turned_off);
  572. end;
  573. end;
  574. end;
  575. procedure dir_setc;
  576. var
  577. hs : string;
  578. mac : tmacro;
  579. begin
  580. current_scanner.skipspace;
  581. hs:=current_scanner.readid;
  582. mac:=tmacro(current_scanner.macros.search(hs));
  583. if not assigned(mac) then
  584. begin
  585. mac:=tmacro.create(hs);
  586. mac.defined:=true;
  587. Message1(parser_c_macro_defined,mac.name);
  588. current_scanner.macros.insert(mac);
  589. end
  590. else
  591. begin
  592. Message1(parser_c_macro_defined,mac.name);
  593. mac.defined:=true;
  594. { delete old definition }
  595. if assigned(mac.buftext) then
  596. begin
  597. freemem(mac.buftext,mac.buflen);
  598. mac.buftext:=nil;
  599. end;
  600. end;
  601. mac.is_used:=true;
  602. { key words are never substituted }
  603. if is_keyword(hs) then
  604. Message(scan_e_keyword_cant_be_a_macro);
  605. { !!!!!! handle macro params, need we this? }
  606. current_scanner.skipspace;
  607. { may be a macro? }
  608. //both versions with := and = are allowed
  609. if c=':' then
  610. current_scanner.readchar;
  611. if c='=' then
  612. begin
  613. current_scanner.readchar;
  614. hs:= parse_compiler_expr;
  615. if length(hs) <> 0 then
  616. begin
  617. { free buffer of macro ?}
  618. if assigned(mac.buftext) then
  619. freemem(mac.buftext,mac.buflen);
  620. { get new mem }
  621. getmem(mac.buftext,length(hs));
  622. mac.buflen:=length(hs);
  623. { copy the text }
  624. move(hs[1],mac.buftext^,mac.buflen);
  625. end
  626. else
  627. Message(scan_e_preproc_syntax_error);
  628. end
  629. else
  630. Message(scan_e_preproc_syntax_error);
  631. end;
  632. procedure dir_undef;
  633. var
  634. hs : string;
  635. mac : tmacro;
  636. begin
  637. current_scanner.skipspace;
  638. hs:=current_scanner.readid;
  639. mac:=tmacro(current_scanner.macros.search(hs));
  640. if not assigned(mac) then
  641. begin
  642. mac:=tmacro.create(hs);
  643. Message1(parser_c_macro_undefined,mac.name);
  644. mac.defined:=false;
  645. current_scanner.macros.insert(mac);
  646. end
  647. else
  648. begin
  649. Message1(parser_c_macro_undefined,mac.name);
  650. mac.defined:=false;
  651. { delete old definition }
  652. if assigned(mac.buftext) then
  653. begin
  654. freemem(mac.buftext,mac.buflen);
  655. mac.buftext:=nil;
  656. end;
  657. end;
  658. mac.is_used:=true;
  659. end;
  660. procedure dir_include;
  661. function findincludefile(const path,name,ext:string;var foundfile:string):boolean;
  662. var
  663. found : boolean;
  664. hpath : string;
  665. begin
  666. { look for the include file
  667. 1. specified path,path of current inputfile,current dir
  668. 2. local includepath
  669. 3. global includepath }
  670. found:=false;
  671. foundfile:='';
  672. hpath:='';
  673. if path<>'' then
  674. begin
  675. if not path_absolute(path) then
  676. hpath:=current_scanner.inputfile.path^+path
  677. else
  678. hpath:=path+';'+current_scanner.inputfile.path^;
  679. end
  680. else
  681. hpath:=current_scanner.inputfile.path^;
  682. found:=FindFile(name+ext,hpath+';.'+source_info.DirSep,foundfile);
  683. if (not found) then
  684. found:=current_module.localincludesearchpath.FindFile(name+ext,foundfile);
  685. if (not found) then
  686. found:=includesearchpath.FindFile(name+ext,foundfile);
  687. findincludefile:=found;
  688. end;
  689. var
  690. args,
  691. foundfile,
  692. hs : string;
  693. path : dirstr;
  694. name : namestr;
  695. ext : extstr;
  696. hp : tinputfile;
  697. found : boolean;
  698. begin
  699. current_scanner.skipspace;
  700. args:=current_scanner.readcomment;
  701. hs:=GetToken(args,' ');
  702. if hs='' then
  703. exit;
  704. if (hs[1]='%') then
  705. begin
  706. { case insensitive }
  707. hs:=upper(hs);
  708. { remove %'s }
  709. Delete(hs,1,1);
  710. if hs[length(hs)]='%' then
  711. Delete(hs,length(hs),1);
  712. { save old }
  713. path:=hs;
  714. { first check for internal macros }
  715. if hs='TIME' then
  716. hs:=gettimestr
  717. else
  718. if hs='DATE' then
  719. hs:=getdatestr
  720. else
  721. if hs='FILE' then
  722. hs:=current_module.sourcefiles.get_file_name(aktfilepos.fileindex)
  723. else
  724. if hs='LINE' then
  725. hs:=tostr(aktfilepos.line)
  726. else
  727. if hs='FPCVERSION' then
  728. hs:=version_string
  729. else
  730. if hs='FPCTARGET' then
  731. hs:=target_cpu_string
  732. else
  733. if hs='FPCTARGETCPU' then
  734. hs:=target_cpu_string
  735. else
  736. if hs='FPCTARGETOS' then
  737. hs:=target_info.shortname
  738. else
  739. hs:=getenv(hs);
  740. if hs='' then
  741. Message1(scan_w_include_env_not_found,path);
  742. { make it a stringconst }
  743. hs:=''''+hs+'''';
  744. current_scanner.insertmacro(path,@hs[1],length(hs),
  745. current_scanner.line_no,current_scanner.inputfile.ref_index);
  746. end
  747. else
  748. begin
  749. hs:=FixFileName(hs);
  750. fsplit(hs,path,name,ext);
  751. { try to find the file }
  752. found:=findincludefile(path,name,ext,foundfile);
  753. if (ext='') then
  754. begin
  755. { try default extensions .inc , .pp and .pas }
  756. if (not found) then
  757. found:=findincludefile(path,name,'.inc',foundfile);
  758. if (not found) then
  759. found:=findincludefile(path,name,target_info.sourceext,foundfile);
  760. if (not found) then
  761. found:=findincludefile(path,name,target_info.pasext,foundfile);
  762. end;
  763. if current_scanner.inputfilecount<max_include_nesting then
  764. begin
  765. inc(current_scanner.inputfilecount);
  766. { we need to reread the current char }
  767. dec(current_scanner.inputpointer);
  768. { shutdown current file }
  769. current_scanner.tempcloseinputfile;
  770. { load new file }
  771. hp:=do_openinputfile(foundfile);
  772. current_scanner.addfile(hp);
  773. current_module.sourcefiles.register_file(hp);
  774. if not current_scanner.openinputfile then
  775. Message1(scan_f_cannot_open_includefile,hs);
  776. Message1(scan_t_start_include_file,current_scanner.inputfile.path^+current_scanner.inputfile.name^);
  777. current_scanner.reload;
  778. end
  779. else
  780. Message(scan_f_include_deep_ten);
  781. end;
  782. end;
  783. {*****************************************************************************
  784. TMacro
  785. *****************************************************************************}
  786. constructor tmacro.create(const n : string);
  787. begin
  788. inherited createname(n);
  789. defined:=true;
  790. defined_at_startup:=false;
  791. fileinfo:=akttokenpos;
  792. is_used:=false;
  793. buftext:=nil;
  794. buflen:=0;
  795. end;
  796. destructor tmacro.destroy;
  797. begin
  798. if assigned(buftext) then
  799. freemem(buftext,buflen);
  800. inherited destroy;
  801. end;
  802. {*****************************************************************************
  803. Preprocessor writting
  804. *****************************************************************************}
  805. {$ifdef PREPROCWRITE}
  806. constructor tpreprocfile.create(const fn:string);
  807. begin
  808. { open outputfile }
  809. assign(f,fn);
  810. {$I-}
  811. rewrite(f);
  812. {$I+}
  813. if ioresult<>0 then
  814. Comment(V_Fatal,'can''t create file '+fn);
  815. getmem(buf,preprocbufsize);
  816. settextbuf(f,buf^,preprocbufsize);
  817. { reset }
  818. eolfound:=false;
  819. spacefound:=false;
  820. end;
  821. destructor tpreprocfile.destroy;
  822. begin
  823. close(f);
  824. freemem(buf,preprocbufsize);
  825. end;
  826. procedure tpreprocfile.add(const s:string);
  827. begin
  828. write(f,s);
  829. end;
  830. procedure tpreprocfile.addspace;
  831. begin
  832. if eolfound then
  833. begin
  834. writeln(f,'');
  835. eolfound:=false;
  836. spacefound:=false;
  837. end
  838. else
  839. if spacefound then
  840. begin
  841. write(f,' ');
  842. spacefound:=false;
  843. end;
  844. end;
  845. {$endif PREPROCWRITE}
  846. {*****************************************************************************
  847. TPreProcStack
  848. *****************************************************************************}
  849. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  850. begin
  851. accept:=a;
  852. typ:=atyp;
  853. next:=n;
  854. end;
  855. {*****************************************************************************
  856. TDirectiveItem
  857. *****************************************************************************}
  858. constructor TDirectiveItem.Create(const n:string;p:tdirectiveproc);
  859. begin
  860. inherited CreateName(n);
  861. is_conditional:=false;
  862. proc:={$ifndef FPCPROCVAR}@{$endif}p;
  863. end;
  864. constructor TDirectiveItem.CreateCond(const n:string;p:tdirectiveproc);
  865. begin
  866. inherited CreateName(n);
  867. is_conditional:=true;
  868. proc:={$ifndef FPCPROCVAR}@{$endif}p;
  869. end;
  870. {****************************************************************************
  871. TSCANNERFILE
  872. ****************************************************************************}
  873. constructor tscannerfile.create(const fn:string);
  874. begin
  875. inputfile:=do_openinputfile(fn);
  876. if assigned(current_module) then
  877. current_module.sourcefiles.register_file(inputfile);
  878. { reset localinput }
  879. inputbuffer:=nil;
  880. inputpointer:=nil;
  881. inputstart:=0;
  882. { reset scanner }
  883. preprocstack:=nil;
  884. comment_level:=0;
  885. yylexcount:=0;
  886. block_type:=bt_general;
  887. line_no:=0;
  888. lastlinepos:=0;
  889. lasttokenpos:=0;
  890. lasttoken:=NOTOKEN;
  891. nexttoken:=NOTOKEN;
  892. lastasmgetchar:=#0;
  893. ignoredirectives:=TStringList.Create;
  894. in_asm_string:=false;
  895. macros:=tdictionary.create;
  896. end;
  897. procedure tscannerfile.firstfile;
  898. begin
  899. { load block }
  900. if not openinputfile then
  901. Message1(scan_f_cannot_open_input,inputfile.name^);
  902. reload;
  903. end;
  904. destructor tscannerfile.destroy;
  905. begin
  906. if assigned(current_module) and
  907. (current_module.state=ms_compiled) and
  908. (status.errorcount=0) then
  909. checkpreprocstack
  910. else
  911. begin
  912. while assigned(preprocstack) do
  913. poppreprocstack;
  914. end;
  915. if not inputfile.closed then
  916. closeinputfile;
  917. ignoredirectives.free;
  918. macros.free;
  919. end;
  920. procedure tscannerfile.def_macro(const s : string);
  921. var
  922. mac : tmacro;
  923. begin
  924. mac:=tmacro(macros.search(s));
  925. if mac=nil then
  926. begin
  927. mac:=tmacro.create(s);
  928. Message1(parser_c_macro_defined,mac.name);
  929. macros.insert(mac);
  930. end;
  931. mac.defined:=true;
  932. mac.defined_at_startup:=true;
  933. end;
  934. procedure tscannerfile.set_macro(const s : string;value : string);
  935. var
  936. mac : tmacro;
  937. begin
  938. mac:=tmacro(macros.search(s));
  939. if mac=nil then
  940. begin
  941. mac:=tmacro.create(s);
  942. macros.insert(mac);
  943. end
  944. else
  945. begin
  946. if assigned(mac.buftext) then
  947. freemem(mac.buftext,mac.buflen);
  948. end;
  949. Message2(parser_c_macro_set_to,mac.name,value);
  950. mac.buflen:=length(value);
  951. getmem(mac.buftext,mac.buflen);
  952. move(value[1],mac.buftext^,mac.buflen);
  953. mac.defined:=true;
  954. mac.defined_at_startup:=true;
  955. end;
  956. function tscannerfile.openinputfile:boolean;
  957. begin
  958. openinputfile:=inputfile.open;
  959. { load buffer }
  960. inputbuffer:=inputfile.buf;
  961. inputpointer:=inputfile.buf;
  962. inputstart:=inputfile.bufstart;
  963. { line }
  964. line_no:=0;
  965. lastlinepos:=0;
  966. lasttokenpos:=0;
  967. end;
  968. procedure tscannerfile.closeinputfile;
  969. begin
  970. inputfile.close;
  971. { reset buffer }
  972. inputbuffer:=nil;
  973. inputpointer:=nil;
  974. inputstart:=0;
  975. { reset line }
  976. line_no:=0;
  977. lastlinepos:=0;
  978. lasttokenpos:=0;
  979. end;
  980. function tscannerfile.tempopeninputfile:boolean;
  981. begin
  982. tempopeninputfile:=inputfile.tempopen;
  983. { reload buffer }
  984. inputbuffer:=inputfile.buf;
  985. inputpointer:=inputfile.buf;
  986. inputstart:=inputfile.bufstart;
  987. end;
  988. procedure tscannerfile.tempcloseinputfile;
  989. begin
  990. if inputfile.closed then
  991. exit;
  992. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  993. inputfile.tempclose;
  994. { reset buffer }
  995. inputbuffer:=nil;
  996. inputpointer:=nil;
  997. inputstart:=0;
  998. end;
  999. procedure tscannerfile.saveinputfile;
  1000. begin
  1001. inputfile.saveinputpointer:=inputpointer;
  1002. inputfile.savelastlinepos:=lastlinepos;
  1003. inputfile.saveline_no:=line_no;
  1004. end;
  1005. procedure tscannerfile.restoreinputfile;
  1006. begin
  1007. inputpointer:=inputfile.saveinputpointer;
  1008. lastlinepos:=inputfile.savelastlinepos;
  1009. line_no:=inputfile.saveline_no;
  1010. if not inputfile.is_macro then
  1011. parser_current_file:=inputfile.name^;
  1012. end;
  1013. procedure tscannerfile.nextfile;
  1014. var
  1015. to_dispose : tinputfile;
  1016. begin
  1017. if assigned(inputfile.next) then
  1018. begin
  1019. if inputfile.is_macro then
  1020. to_dispose:=inputfile
  1021. else
  1022. begin
  1023. to_dispose:=nil;
  1024. dec(inputfilecount);
  1025. end;
  1026. { we can allways close the file, no ? }
  1027. inputfile.close;
  1028. inputfile:=inputfile.next;
  1029. if assigned(to_dispose) then
  1030. to_dispose.free;
  1031. restoreinputfile;
  1032. end;
  1033. end;
  1034. procedure tscannerfile.addfile(hp:tinputfile);
  1035. begin
  1036. saveinputfile;
  1037. { add to list }
  1038. hp.next:=inputfile;
  1039. inputfile:=hp;
  1040. { load new inputfile }
  1041. restoreinputfile;
  1042. end;
  1043. procedure tscannerfile.reload;
  1044. begin
  1045. with inputfile do
  1046. begin
  1047. { when nothing more to read then leave immediatly, so we
  1048. don't change the aktfilepos and leave it point to the last
  1049. char }
  1050. if (c=#26) and (not assigned(next)) then
  1051. exit;
  1052. repeat
  1053. { still more to read?, then change the #0 to a space so its seen
  1054. as a seperator, this can't be used for macro's which can change
  1055. the place of the #0 in the buffer with tempopen }
  1056. if (c=#0) and (bufsize>0) and
  1057. not(inputfile.is_macro) and
  1058. (inputpointer-inputbuffer<bufsize) then
  1059. begin
  1060. c:=' ';
  1061. inc(inputpointer);
  1062. exit;
  1063. end;
  1064. { can we read more from this file ? }
  1065. if (c<>#26) and (not endoffile) then
  1066. begin
  1067. readbuf;
  1068. inputpointer:=buf;
  1069. inputbuffer:=buf;
  1070. inputstart:=bufstart;
  1071. { first line? }
  1072. if line_no=0 then
  1073. begin
  1074. line_no:=1;
  1075. if cs_asm_source in aktglobalswitches then
  1076. inputfile.setline(line_no,bufstart);
  1077. end;
  1078. end
  1079. else
  1080. begin
  1081. { load eof position in tokenpos/aktfilepos }
  1082. gettokenpos;
  1083. { close file }
  1084. closeinputfile;
  1085. { no next module, than EOF }
  1086. if not assigned(inputfile.next) then
  1087. begin
  1088. c:=#26;
  1089. exit;
  1090. end;
  1091. { load next file and reopen it }
  1092. nextfile;
  1093. tempopeninputfile;
  1094. { status }
  1095. Message1(scan_t_back_in,inputfile.name^);
  1096. end;
  1097. { load next char }
  1098. c:=inputpointer^;
  1099. inc(inputpointer);
  1100. until c<>#0; { if also end, then reload again }
  1101. end;
  1102. end;
  1103. procedure tscannerfile.insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  1104. var
  1105. hp : tinputfile;
  1106. begin
  1107. { save old postion }
  1108. dec(inputpointer);
  1109. tempcloseinputfile;
  1110. { create macro 'file' }
  1111. { use special name to dispose after !! }
  1112. hp:=do_openinputfile('_Macro_.'+macname);
  1113. addfile(hp);
  1114. with inputfile do
  1115. begin
  1116. setmacro(p,len);
  1117. { local buffer }
  1118. inputbuffer:=buf;
  1119. inputpointer:=buf;
  1120. inputstart:=bufstart;
  1121. ref_index:=fileindex;
  1122. end;
  1123. { reset line }
  1124. line_no:=line;
  1125. lastlinepos:=0;
  1126. lasttokenpos:=0;
  1127. { load new c }
  1128. c:=inputpointer^;
  1129. inc(inputpointer);
  1130. end;
  1131. procedure tscannerfile.gettokenpos;
  1132. { load the values of tokenpos and lasttokenpos }
  1133. begin
  1134. lasttokenpos:=inputstart+(inputpointer-inputbuffer);
  1135. akttokenpos.line:=line_no;
  1136. akttokenpos.column:=lasttokenpos-lastlinepos;
  1137. akttokenpos.fileindex:=inputfile.ref_index;
  1138. aktfilepos:=akttokenpos;
  1139. end;
  1140. procedure tscannerfile.inc_comment_level;
  1141. var
  1142. oldaktfilepos : tfileposinfo;
  1143. begin
  1144. if (m_nested_comment in aktmodeswitches) then
  1145. inc(comment_level)
  1146. else
  1147. comment_level:=1;
  1148. if (comment_level>1) then
  1149. begin
  1150. oldaktfilepos:=aktfilepos;
  1151. gettokenpos; { update for warning }
  1152. Message1(scan_w_comment_level,tostr(comment_level));
  1153. aktfilepos:=oldaktfilepos;
  1154. end;
  1155. end;
  1156. procedure tscannerfile.dec_comment_level;
  1157. begin
  1158. if (m_nested_comment in aktmodeswitches) then
  1159. dec(comment_level)
  1160. else
  1161. comment_level:=0;
  1162. end;
  1163. procedure tscannerfile.linebreak;
  1164. var
  1165. cur : char;
  1166. oldtokenpos,
  1167. oldaktfilepos : tfileposinfo;
  1168. begin
  1169. with inputfile do
  1170. begin
  1171. if (byte(inputpointer^)=0) and not(endoffile) then
  1172. begin
  1173. cur:=c;
  1174. reload;
  1175. if byte(cur)+byte(c)<>23 then
  1176. dec(inputpointer);
  1177. end
  1178. else
  1179. begin
  1180. { Support all combination of #10 and #13 as line break }
  1181. if (byte(inputpointer^)+byte(c)=23) then
  1182. inc(inputpointer);
  1183. end;
  1184. { Always return #10 as line break }
  1185. c:=#10;
  1186. { increase line counters }
  1187. lastlinepos:=bufstart+(inputpointer-inputbuffer);
  1188. inc(line_no);
  1189. { update linebuffer }
  1190. if cs_asm_source in aktglobalswitches then
  1191. inputfile.setline(line_no,lastlinepos);
  1192. { update for status and call the show status routine,
  1193. but don't touch aktfilepos ! }
  1194. oldaktfilepos:=aktfilepos;
  1195. oldtokenpos:=akttokenpos;
  1196. gettokenpos; { update for v_status }
  1197. inc(status.compiledlines);
  1198. ShowStatus;
  1199. aktfilepos:=oldaktfilepos;
  1200. akttokenpos:=oldtokenpos;
  1201. end;
  1202. end;
  1203. procedure tscannerfile.illegal_char(c:char);
  1204. var
  1205. s : string;
  1206. begin
  1207. if c in [#32..#255] then
  1208. s:=''''+c+''''
  1209. else
  1210. s:='#'+tostr(ord(c));
  1211. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  1212. end;
  1213. procedure tscannerfile.end_of_file;
  1214. begin
  1215. checkpreprocstack;
  1216. Message(scan_f_end_of_file);
  1217. end;
  1218. {-------------------------------------------
  1219. IF Conditional Handling
  1220. -------------------------------------------}
  1221. procedure tscannerfile.checkpreprocstack;
  1222. begin
  1223. { check for missing ifdefs }
  1224. while assigned(preprocstack) do
  1225. begin
  1226. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  1227. preprocstack.owner.inputfile.name^,tostr(preprocstack.line_nb));
  1228. poppreprocstack;
  1229. end;
  1230. end;
  1231. procedure tscannerfile.poppreprocstack;
  1232. var
  1233. hp : tpreprocstack;
  1234. begin
  1235. if assigned(preprocstack) then
  1236. begin
  1237. Message1(scan_c_endif_found,preprocstack.name);
  1238. hp:=preprocstack.next;
  1239. preprocstack.free;
  1240. preprocstack:=hp;
  1241. end
  1242. else
  1243. Message(scan_e_endif_without_if);
  1244. end;
  1245. procedure tscannerfile.addpreprocstack(atyp : preproctyp;a:boolean;const s:string;w:longint);
  1246. begin
  1247. preprocstack:=tpreprocstack.create(atyp,((preprocstack=nil) or preprocstack.accept) and a,preprocstack);
  1248. preprocstack.name:=s;
  1249. preprocstack.line_nb:=line_no;
  1250. preprocstack.owner:=self;
  1251. if preprocstack.accept then
  1252. Message2(w,preprocstack.name,'accepted')
  1253. else
  1254. Message2(w,preprocstack.name,'rejected');
  1255. end;
  1256. procedure tscannerfile.elsepreprocstack;
  1257. begin
  1258. if assigned(preprocstack) then
  1259. begin
  1260. preprocstack.typ:=pp_else;
  1261. preprocstack.line_nb:=line_no;
  1262. if not(assigned(preprocstack.next)) or (preprocstack.next.accept) then
  1263. preprocstack.accept:=not preprocstack.accept;
  1264. if preprocstack.accept then
  1265. Message2(scan_c_else_found,preprocstack.name,'accepted')
  1266. else
  1267. Message2(scan_c_else_found,preprocstack.name,'rejected');
  1268. end
  1269. else
  1270. Message(scan_e_endif_without_if);
  1271. end;
  1272. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  1273. var
  1274. oldaktfilepos : tfileposinfo;
  1275. begin
  1276. oldaktfilepos:=aktfilepos;
  1277. repeat
  1278. current_scanner.gettokenpos;
  1279. p.proc{$ifdef FPCPROCVAR}(){$endif};
  1280. { accept the text ? }
  1281. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  1282. break
  1283. else
  1284. begin
  1285. current_scanner.gettokenpos;
  1286. Message(scan_c_skipping_until);
  1287. repeat
  1288. current_scanner.skipuntildirective;
  1289. if not (m_mac in aktmodeswitches) then
  1290. p:=tdirectiveitem(turbo_scannerdirectives.search(current_scanner.readid))
  1291. else
  1292. p:=tdirectiveitem(mac_scannerdirectives.search(current_scanner.readid));
  1293. until assigned(p) and (p.is_conditional);
  1294. current_scanner.gettokenpos;
  1295. Message1(scan_d_handling_switch,'$'+p.name);
  1296. end;
  1297. until false;
  1298. aktfilepos:=oldaktfilepos;
  1299. end;
  1300. procedure tscannerfile.handledirectives;
  1301. var
  1302. t : tdirectiveitem;
  1303. hs : string;
  1304. begin
  1305. gettokenpos;
  1306. readchar; {Remove the $}
  1307. hs:=readid;
  1308. {$ifdef PREPROCWRITE}
  1309. if parapreprocess then
  1310. begin
  1311. t:=Get_Directive(hs);
  1312. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  1313. begin
  1314. preprocfile^.AddSpace;
  1315. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  1316. exit;
  1317. end;
  1318. end;
  1319. {$endif PREPROCWRITE}
  1320. { skip this directive? }
  1321. if (ignoredirectives.find(hs)<>nil) then
  1322. begin
  1323. if (comment_level>0) then
  1324. readcomment;
  1325. { we've read the whole comment }
  1326. aktcommentstyle:=comment_none;
  1327. exit;
  1328. end;
  1329. if hs='' then
  1330. begin
  1331. Message1(scan_w_illegal_switch,'$'+hs);
  1332. end;
  1333. { Check for compiler switches }
  1334. while (length(hs)=1) and (c in ['-','+']) do
  1335. begin
  1336. HandleSwitch(hs[1],c);
  1337. current_scanner.readchar; {Remove + or -}
  1338. if c=',' then
  1339. begin
  1340. current_scanner.readchar; {Remove , }
  1341. { read next switch, support $v+,$+}
  1342. hs:=current_scanner.readid;
  1343. if (hs='') then
  1344. begin
  1345. if (c='$') and (m_fpc in aktmodeswitches) then
  1346. begin
  1347. current_scanner.readchar; { skip $ }
  1348. hs:=current_scanner.readid;
  1349. end;
  1350. if (hs='') then
  1351. Message1(scan_w_illegal_directive,'$'+c);
  1352. end
  1353. else
  1354. Message1(scan_d_handling_switch,'$'+hs);
  1355. end
  1356. else
  1357. hs:='';
  1358. end;
  1359. { directives may follow switches after a , }
  1360. if hs<>'' then
  1361. begin
  1362. if not (m_mac in aktmodeswitches) then
  1363. t:=tdirectiveitem(turbo_scannerdirectives.search(hs))
  1364. else
  1365. t:=tdirectiveitem(mac_scannerdirectives.search(hs));
  1366. if assigned(t) then
  1367. begin
  1368. if t.is_conditional then
  1369. handleconditional(t)
  1370. else
  1371. begin
  1372. Message1(scan_d_handling_switch,'$'+hs);
  1373. t.proc{$ifdef FPCPROCVAR}(){$endif};
  1374. end;
  1375. end
  1376. else
  1377. begin
  1378. current_scanner.ignoredirectives.insert(hs);
  1379. Message1(scan_w_illegal_directive,'$'+hs);
  1380. end;
  1381. { conditionals already read the comment }
  1382. if (current_scanner.comment_level>0) then
  1383. current_scanner.readcomment;
  1384. { we've read the whole comment }
  1385. aktcommentstyle:=comment_none;
  1386. end;
  1387. end;
  1388. procedure tscannerfile.readchar;
  1389. begin
  1390. c:=inputpointer^;
  1391. if c=#0 then
  1392. reload
  1393. else
  1394. inc(inputpointer);
  1395. end;
  1396. procedure tscannerfile.readstring;
  1397. var
  1398. i : longint;
  1399. begin
  1400. i:=0;
  1401. repeat
  1402. case c of
  1403. '_',
  1404. '0'..'9',
  1405. 'A'..'Z' :
  1406. begin
  1407. if i<255 then
  1408. begin
  1409. inc(i);
  1410. orgpattern[i]:=c;
  1411. pattern[i]:=c;
  1412. end;
  1413. c:=inputpointer^;
  1414. inc(inputpointer);
  1415. end;
  1416. 'a'..'z' :
  1417. begin
  1418. if i<255 then
  1419. begin
  1420. inc(i);
  1421. orgpattern[i]:=c;
  1422. pattern[i]:=chr(ord(c)-32)
  1423. end;
  1424. c:=inputpointer^;
  1425. inc(inputpointer);
  1426. end;
  1427. #0 :
  1428. reload;
  1429. else
  1430. break;
  1431. end;
  1432. until false;
  1433. orgpattern[0]:=chr(i);
  1434. pattern[0]:=chr(i);
  1435. end;
  1436. procedure tscannerfile.readnumber;
  1437. var
  1438. base,
  1439. i : longint;
  1440. begin
  1441. case c of
  1442. '%' :
  1443. begin
  1444. readchar;
  1445. base:=2;
  1446. pattern[1]:='%';
  1447. i:=1;
  1448. end;
  1449. '&' :
  1450. begin
  1451. readchar;
  1452. base:=8;
  1453. pattern[1]:='&';
  1454. i:=1;
  1455. end;
  1456. '$' :
  1457. begin
  1458. readchar;
  1459. base:=16;
  1460. pattern[1]:='$';
  1461. i:=1;
  1462. end;
  1463. else
  1464. begin
  1465. base:=10;
  1466. i:=0;
  1467. end;
  1468. end;
  1469. while ((base>=10) and (c in ['0'..'9'])) or
  1470. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  1471. ((base=8) and (c in ['0'..'7'])) or
  1472. ((base=2) and (c in ['0'..'1'])) do
  1473. begin
  1474. if i<255 then
  1475. begin
  1476. inc(i);
  1477. pattern[i]:=c;
  1478. end;
  1479. readchar;
  1480. end;
  1481. pattern[0]:=chr(i);
  1482. end;
  1483. function tscannerfile.readid:string;
  1484. begin
  1485. readstring;
  1486. readid:=pattern;
  1487. end;
  1488. function tscannerfile.readval:longint;
  1489. var
  1490. l : longint;
  1491. w : integer;
  1492. begin
  1493. readnumber;
  1494. valint(pattern,l,w);
  1495. readval:=l;
  1496. end;
  1497. function tscannerfile.readcomment:string;
  1498. var
  1499. i : longint;
  1500. begin
  1501. i:=0;
  1502. repeat
  1503. case c of
  1504. '{' :
  1505. begin
  1506. if aktcommentstyle=comment_tp then
  1507. inc_comment_level;
  1508. end;
  1509. '}' :
  1510. begin
  1511. if aktcommentstyle=comment_tp then
  1512. begin
  1513. readchar;
  1514. dec_comment_level;
  1515. if comment_level=0 then
  1516. break
  1517. else
  1518. continue;
  1519. end;
  1520. end;
  1521. '*' :
  1522. begin
  1523. if aktcommentstyle=comment_oldtp then
  1524. begin
  1525. readchar;
  1526. if c=')' then
  1527. begin
  1528. readchar;
  1529. dec_comment_level;
  1530. break;
  1531. end
  1532. else
  1533. { Add both characters !!}
  1534. if (i<255) then
  1535. begin
  1536. inc(i);
  1537. readcomment[i]:='*';
  1538. if (i<255) then
  1539. begin
  1540. inc(i);
  1541. readcomment[i]:='*';
  1542. end;
  1543. end;
  1544. end
  1545. else
  1546. { Not old TP comment, so add...}
  1547. begin
  1548. if (i<255) then
  1549. begin
  1550. inc(i);
  1551. readcomment[i]:='*';
  1552. end;
  1553. end;
  1554. end;
  1555. #10,#13 :
  1556. linebreak;
  1557. #26 :
  1558. end_of_file;
  1559. else
  1560. begin
  1561. if (i<255) then
  1562. begin
  1563. inc(i);
  1564. readcomment[i]:=c;
  1565. end;
  1566. end;
  1567. end;
  1568. readchar;
  1569. until false;
  1570. readcomment[0]:=chr(i);
  1571. end;
  1572. function tscannerfile.readstate:char;
  1573. var
  1574. state : char;
  1575. begin
  1576. state:=' ';
  1577. if c=' ' then
  1578. begin
  1579. current_scanner.skipspace;
  1580. current_scanner.readid;
  1581. if pattern='ON' then
  1582. state:='+'
  1583. else
  1584. if pattern='OFF' then
  1585. state:='-';
  1586. end
  1587. else
  1588. state:=c;
  1589. if not (state in ['+','-']) then
  1590. Message(scan_e_wrong_switch_toggle);
  1591. readstate:=state;
  1592. end;
  1593. procedure tscannerfile.skipspace;
  1594. begin
  1595. repeat
  1596. case c of
  1597. #26 :
  1598. begin
  1599. reload;
  1600. if (c=#26) and not assigned(inputfile.next) then
  1601. break;
  1602. continue;
  1603. end;
  1604. #10,
  1605. #13 :
  1606. linebreak;
  1607. #9,#11,#12,' ' :
  1608. ;
  1609. else
  1610. break;
  1611. end;
  1612. readchar;
  1613. until false;
  1614. end;
  1615. procedure tscannerfile.skipuntildirective;
  1616. var
  1617. incomment : boolean;
  1618. found : longint;
  1619. next_char_loaded : boolean;
  1620. oldcommentstyle : tcommentstyle;
  1621. begin
  1622. found:=0;
  1623. next_char_loaded:=false;
  1624. incomment:=true;
  1625. oldcommentstyle:=aktcommentstyle;
  1626. repeat
  1627. case c of
  1628. #10,
  1629. #13 :
  1630. linebreak;
  1631. #26 :
  1632. begin
  1633. reload;
  1634. if (c=#26) and not assigned(inputfile.next) then
  1635. end_of_file;
  1636. continue;
  1637. end;
  1638. '{' :
  1639. begin
  1640. if not(m_nested_comment in aktmodeswitches) or
  1641. (comment_level=0) then
  1642. begin
  1643. found:=1;
  1644. aktcommentstyle:=comment_tp;
  1645. end;
  1646. inc_comment_level;
  1647. incomment:=true;
  1648. end;
  1649. '*' :
  1650. begin
  1651. if incomment then
  1652. begin
  1653. readchar;
  1654. if c=')' then
  1655. begin
  1656. dec_comment_level;
  1657. found:=0;
  1658. incomment:=false;
  1659. end
  1660. else
  1661. next_char_loaded:=true;
  1662. end
  1663. else
  1664. found := 0;
  1665. end;
  1666. '}' :
  1667. begin
  1668. dec_comment_level;
  1669. found:=0;
  1670. incomment:=false;
  1671. end;
  1672. '$' :
  1673. begin
  1674. if found=1 then
  1675. found:=2;
  1676. end;
  1677. '''' :
  1678. if not incomment then
  1679. begin
  1680. repeat
  1681. readchar;
  1682. case c of
  1683. #26 :
  1684. end_of_file;
  1685. #10,#13 :
  1686. break;
  1687. '''' :
  1688. begin
  1689. readchar;
  1690. if c<>'''' then
  1691. begin
  1692. next_char_loaded:=true;
  1693. break;
  1694. end;
  1695. end;
  1696. end;
  1697. until false;
  1698. end;
  1699. '(' :
  1700. begin
  1701. if not incomment then
  1702. begin
  1703. readchar;
  1704. if c='*' then
  1705. begin
  1706. readchar;
  1707. if c='$' then
  1708. begin
  1709. found:=2;
  1710. inc_comment_level;
  1711. aktcommentstyle:=comment_oldtp;
  1712. end
  1713. else
  1714. begin
  1715. skipoldtpcomment;
  1716. aktcommentstyle:=oldcommentstyle;
  1717. end;
  1718. end;
  1719. next_char_loaded:=true;
  1720. end
  1721. else
  1722. found:=0;
  1723. end;
  1724. '/' :
  1725. begin
  1726. if not incomment then
  1727. begin
  1728. readchar;
  1729. if c='/' then
  1730. begin
  1731. skipdelphicomment;
  1732. aktcommentstyle:=oldcommentstyle;
  1733. end;
  1734. next_char_loaded:=true;
  1735. end
  1736. else
  1737. found:=0;
  1738. end;
  1739. else
  1740. found:=0;
  1741. end;
  1742. if next_char_loaded then
  1743. next_char_loaded:=false
  1744. else
  1745. readchar;
  1746. until (found=2);
  1747. end;
  1748. {****************************************************************************
  1749. Comment Handling
  1750. ****************************************************************************}
  1751. procedure tscannerfile.skipcomment;
  1752. begin
  1753. aktcommentstyle:=comment_tp;
  1754. readchar;
  1755. inc_comment_level;
  1756. { handle compiler switches }
  1757. if (c='$') then
  1758. handledirectives;
  1759. { handle_switches can dec comment_level, }
  1760. while (comment_level>0) do
  1761. begin
  1762. case c of
  1763. '{' :
  1764. inc_comment_level;
  1765. '}' :
  1766. dec_comment_level;
  1767. #10,#13 :
  1768. linebreak;
  1769. #26 :
  1770. begin
  1771. reload;
  1772. if (c=#26) and not assigned(inputfile.next) then
  1773. end_of_file;
  1774. continue;
  1775. end;
  1776. end;
  1777. readchar;
  1778. end;
  1779. aktcommentstyle:=comment_none;
  1780. end;
  1781. procedure tscannerfile.skipdelphicomment;
  1782. begin
  1783. aktcommentstyle:=comment_delphi;
  1784. inc_comment_level;
  1785. readchar;
  1786. { this is not supported }
  1787. if c='$' then
  1788. Message(scan_e_wrong_styled_switch);
  1789. { skip comment }
  1790. while not (c in [#10,#13,#26]) do
  1791. readchar;
  1792. dec_comment_level;
  1793. aktcommentstyle:=comment_none;
  1794. end;
  1795. procedure tscannerfile.skipoldtpcomment;
  1796. var
  1797. found : longint;
  1798. begin
  1799. aktcommentstyle:=comment_oldtp;
  1800. inc_comment_level;
  1801. { only load a char if last already processed,
  1802. was cause of bug1634 PM }
  1803. if c=#0 then
  1804. readchar;
  1805. { this is now supported }
  1806. if (c='$') then
  1807. handledirectives;
  1808. { skip comment }
  1809. while (comment_level>0) do
  1810. begin
  1811. found:=0;
  1812. repeat
  1813. case c of
  1814. #26 :
  1815. begin
  1816. reload;
  1817. if (c=#26) and not assigned(inputfile.next) then
  1818. end_of_file;
  1819. continue;
  1820. end;
  1821. #10,#13 :
  1822. linebreak;
  1823. '*' :
  1824. begin
  1825. if found=3 then
  1826. found:=4
  1827. else
  1828. found:=1;
  1829. end;
  1830. ')' :
  1831. begin
  1832. if found in [1,4] then
  1833. begin
  1834. dec_comment_level;
  1835. if comment_level=0 then
  1836. found:=2
  1837. else
  1838. found:=0;
  1839. end;
  1840. end;
  1841. '(' :
  1842. begin
  1843. if found=4 then
  1844. inc_comment_level;
  1845. found:=3;
  1846. end;
  1847. else
  1848. begin
  1849. if found=4 then
  1850. inc_comment_level;
  1851. found:=0;
  1852. end;
  1853. end;
  1854. readchar;
  1855. until (found=2);
  1856. end;
  1857. aktcommentstyle:=comment_none;
  1858. end;
  1859. {****************************************************************************
  1860. Token Scanner
  1861. ****************************************************************************}
  1862. procedure tscannerfile.readtoken;
  1863. var
  1864. code : integer;
  1865. len,
  1866. low,high,mid : longint;
  1867. m : longint;
  1868. mac : tmacro;
  1869. asciinr : string[6];
  1870. msgwritten,
  1871. iswidestring : boolean;
  1872. label
  1873. exit_label;
  1874. begin
  1875. if localswitcheschanged then
  1876. begin
  1877. aktlocalswitches:=nextaktlocalswitches;
  1878. localswitcheschanged:=false;
  1879. end;
  1880. { was there already a token read, then return that token }
  1881. if nexttoken<>NOTOKEN then
  1882. begin
  1883. token:=nexttoken;
  1884. nexttoken:=NOTOKEN;
  1885. goto exit_label;
  1886. end;
  1887. { Skip all spaces and comments }
  1888. repeat
  1889. case c of
  1890. '{' :
  1891. skipcomment;
  1892. #26 :
  1893. begin
  1894. reload;
  1895. if (c=#26) and not assigned(inputfile.next) then
  1896. break;
  1897. end;
  1898. ' ',#9..#13 :
  1899. begin
  1900. {$ifdef PREPROCWRITE}
  1901. if parapreprocess then
  1902. begin
  1903. if c=#10 then
  1904. preprocfile.eolfound:=true
  1905. else
  1906. preprocfile.spacefound:=true;
  1907. end;
  1908. {$endif PREPROCWRITE}
  1909. skipspace;
  1910. end
  1911. else
  1912. break;
  1913. end;
  1914. until false;
  1915. { Save current token position, for EOF its already loaded }
  1916. if c<>#26 then
  1917. gettokenpos;
  1918. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  1919. if c in ['A'..'Z','a'..'z','_'] then
  1920. begin
  1921. readstring;
  1922. token:=_ID;
  1923. idtoken:=_ID;
  1924. { keyword or any other known token,
  1925. pattern is always uppercased }
  1926. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  1927. begin
  1928. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  1929. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  1930. while low<high do
  1931. begin
  1932. mid:=(high+low+1) shr 1;
  1933. if pattern<tokeninfo^[ttoken(mid)].str then
  1934. high:=mid-1
  1935. else
  1936. low:=mid;
  1937. end;
  1938. if pattern=tokeninfo^[ttoken(high)].str then
  1939. begin
  1940. if tokeninfo^[ttoken(high)].keyword in aktmodeswitches then
  1941. if tokeninfo^[ttoken(high)].op=NOTOKEN then
  1942. token:=ttoken(high)
  1943. else
  1944. token:=tokeninfo^[ttoken(high)].op;
  1945. idtoken:=ttoken(high);
  1946. end;
  1947. end;
  1948. { Only process identifiers and not keywords }
  1949. if token=_ID then
  1950. begin
  1951. { this takes some time ... }
  1952. if (cs_support_macro in aktmoduleswitches) then
  1953. begin
  1954. mac:=tmacro(macros.search(pattern));
  1955. if assigned(mac) and (assigned(mac.buftext)) then
  1956. begin
  1957. if yylexcount<max_macro_nesting then
  1958. begin
  1959. inc(yylexcount);
  1960. insertmacro(pattern,mac.buftext,mac.buflen,
  1961. mac.fileinfo.line,mac.fileinfo.fileindex);
  1962. { handle empty macros }
  1963. if c=#0 then
  1964. reload;
  1965. readtoken;
  1966. { that's all folks }
  1967. dec(yylexcount);
  1968. exit;
  1969. end
  1970. else
  1971. Message(scan_w_macro_deep_ten);
  1972. end;
  1973. end;
  1974. end;
  1975. { return token }
  1976. goto exit_label;
  1977. end
  1978. else
  1979. begin
  1980. idtoken:=_NOID;
  1981. case c of
  1982. '$' :
  1983. begin
  1984. readnumber;
  1985. token:=_INTCONST;
  1986. goto exit_label;
  1987. end;
  1988. '%' :
  1989. begin
  1990. if not(m_fpc in aktmodeswitches) then
  1991. Illegal_Char(c)
  1992. else
  1993. begin
  1994. readnumber;
  1995. token:=_INTCONST;
  1996. goto exit_label;
  1997. end;
  1998. end;
  1999. '&' :
  2000. begin
  2001. if not(m_fpc in aktmodeswitches) then
  2002. Illegal_Char(c)
  2003. else
  2004. begin
  2005. readnumber;
  2006. token:=_INTCONST;
  2007. goto exit_label;
  2008. end;
  2009. end;
  2010. '0'..'9' :
  2011. begin
  2012. readnumber;
  2013. if (c in ['.','e','E']) then
  2014. begin
  2015. { first check for a . }
  2016. if c='.' then
  2017. begin
  2018. readchar;
  2019. { is it a .. from a range? }
  2020. case c of
  2021. '.' :
  2022. begin
  2023. readchar;
  2024. token:=_INTCONST;
  2025. nexttoken:=_POINTPOINT;
  2026. goto exit_label;
  2027. end;
  2028. ')' :
  2029. begin
  2030. readchar;
  2031. token:=_INTCONST;
  2032. nexttoken:=_RECKKLAMMER;
  2033. goto exit_label;
  2034. end;
  2035. end;
  2036. { insert the number after the . }
  2037. pattern:=pattern+'.';
  2038. while c in ['0'..'9'] do
  2039. begin
  2040. pattern:=pattern+c;
  2041. readchar;
  2042. end;
  2043. end;
  2044. { E can also follow after a point is scanned }
  2045. if c in ['e','E'] then
  2046. begin
  2047. pattern:=pattern+'E';
  2048. readchar;
  2049. if c in ['-','+'] then
  2050. begin
  2051. pattern:=pattern+c;
  2052. readchar;
  2053. end;
  2054. if not(c in ['0'..'9']) then
  2055. Illegal_Char(c);
  2056. while c in ['0'..'9'] do
  2057. begin
  2058. pattern:=pattern+c;
  2059. readchar;
  2060. end;
  2061. end;
  2062. token:=_REALNUMBER;
  2063. goto exit_label;
  2064. end;
  2065. token:=_INTCONST;
  2066. goto exit_label;
  2067. end;
  2068. ';' :
  2069. begin
  2070. readchar;
  2071. token:=_SEMICOLON;
  2072. goto exit_label;
  2073. end;
  2074. '[' :
  2075. begin
  2076. readchar;
  2077. token:=_LECKKLAMMER;
  2078. goto exit_label;
  2079. end;
  2080. ']' :
  2081. begin
  2082. readchar;
  2083. token:=_RECKKLAMMER;
  2084. goto exit_label;
  2085. end;
  2086. '(' :
  2087. begin
  2088. readchar;
  2089. case c of
  2090. '*' :
  2091. begin
  2092. c:=#0;{Signal skipoldtpcomment to reload a char }
  2093. skipoldtpcomment;
  2094. readtoken;
  2095. exit;
  2096. end;
  2097. '.' :
  2098. begin
  2099. readchar;
  2100. token:=_LECKKLAMMER;
  2101. goto exit_label;
  2102. end;
  2103. end;
  2104. token:=_LKLAMMER;
  2105. goto exit_label;
  2106. end;
  2107. ')' :
  2108. begin
  2109. readchar;
  2110. token:=_RKLAMMER;
  2111. goto exit_label;
  2112. end;
  2113. '+' :
  2114. begin
  2115. readchar;
  2116. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2117. begin
  2118. readchar;
  2119. token:=_PLUSASN;
  2120. goto exit_label;
  2121. end;
  2122. token:=_PLUS;
  2123. goto exit_label;
  2124. end;
  2125. '-' :
  2126. begin
  2127. readchar;
  2128. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2129. begin
  2130. readchar;
  2131. token:=_MINUSASN;
  2132. goto exit_label;
  2133. end;
  2134. token:=_MINUS;
  2135. goto exit_label;
  2136. end;
  2137. ':' :
  2138. begin
  2139. readchar;
  2140. if c='=' then
  2141. begin
  2142. readchar;
  2143. token:=_ASSIGNMENT;
  2144. goto exit_label;
  2145. end;
  2146. token:=_COLON;
  2147. goto exit_label;
  2148. end;
  2149. '*' :
  2150. begin
  2151. readchar;
  2152. if (c='=') and (cs_support_c_operators in aktmoduleswitches) then
  2153. begin
  2154. readchar;
  2155. token:=_STARASN;
  2156. end
  2157. else
  2158. if c='*' then
  2159. begin
  2160. readchar;
  2161. token:=_STARSTAR;
  2162. end
  2163. else
  2164. token:=_STAR;
  2165. goto exit_label;
  2166. end;
  2167. '/' :
  2168. begin
  2169. readchar;
  2170. case c of
  2171. '=' :
  2172. begin
  2173. if (cs_support_c_operators in aktmoduleswitches) then
  2174. begin
  2175. readchar;
  2176. token:=_SLASHASN;
  2177. goto exit_label;
  2178. end;
  2179. end;
  2180. '/' :
  2181. begin
  2182. skipdelphicomment;
  2183. readtoken;
  2184. exit;
  2185. end;
  2186. end;
  2187. token:=_SLASH;
  2188. goto exit_label;
  2189. end;
  2190. '=' :
  2191. begin
  2192. readchar;
  2193. token:=_EQUAL;
  2194. goto exit_label;
  2195. end;
  2196. '.' :
  2197. begin
  2198. readchar;
  2199. case c of
  2200. '.' :
  2201. begin
  2202. readchar;
  2203. token:=_POINTPOINT;
  2204. goto exit_label;
  2205. end;
  2206. ')' :
  2207. begin
  2208. readchar;
  2209. token:=_RECKKLAMMER;
  2210. goto exit_label;
  2211. end;
  2212. end;
  2213. token:=_POINT;
  2214. goto exit_label;
  2215. end;
  2216. '@' :
  2217. begin
  2218. readchar;
  2219. token:=_KLAMMERAFFE;
  2220. goto exit_label;
  2221. end;
  2222. ',' :
  2223. begin
  2224. readchar;
  2225. token:=_COMMA;
  2226. goto exit_label;
  2227. end;
  2228. '''','#','^' :
  2229. begin
  2230. len:=0;
  2231. msgwritten:=false;
  2232. pattern:='';
  2233. iswidestring:=false;
  2234. if c='^' then
  2235. begin
  2236. readchar;
  2237. c:=upcase(c);
  2238. if (block_type=bt_type) or
  2239. (lasttoken=_ID) or (lasttoken=_NIL) or
  2240. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  2241. begin
  2242. token:=_CARET;
  2243. goto exit_label;
  2244. end
  2245. else
  2246. begin
  2247. inc(len);
  2248. if c<#64 then
  2249. pattern[len]:=chr(ord(c)+64)
  2250. else
  2251. pattern[len]:=chr(ord(c)-64);
  2252. readchar;
  2253. end;
  2254. end;
  2255. repeat
  2256. case c of
  2257. '#' :
  2258. begin
  2259. readchar; { read # }
  2260. if c='$' then
  2261. begin
  2262. readchar; { read leading $ }
  2263. asciinr:='$';
  2264. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<6) do
  2265. begin
  2266. asciinr:=asciinr+c;
  2267. readchar;
  2268. end;
  2269. end
  2270. else
  2271. begin
  2272. asciinr:='';
  2273. while (c in ['0'..'9']) and (length(asciinr)<6) do
  2274. begin
  2275. asciinr:=asciinr+c;
  2276. readchar;
  2277. end;
  2278. end;
  2279. valint(asciinr,m,code);
  2280. if (asciinr='') or (code<>0) then
  2281. Message(scan_e_illegal_char_const)
  2282. else if (m<0) or (m>255) or (length(asciinr)>3) then
  2283. begin
  2284. if (m>=0) and (m<=65535) then
  2285. begin
  2286. if not iswidestring then
  2287. begin
  2288. ascii2unicode(@pattern[1],len,patternw);
  2289. iswidestring:=true;
  2290. len:=0;
  2291. end;
  2292. concatwidestringchar(patternw,tcompilerwidechar(m));
  2293. end
  2294. else
  2295. Message(scan_e_illegal_char_const)
  2296. end
  2297. else if iswidestring then
  2298. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  2299. else
  2300. begin
  2301. if len<255 then
  2302. begin
  2303. inc(len);
  2304. pattern[len]:=chr(m);
  2305. end
  2306. else
  2307. begin
  2308. if not msgwritten then
  2309. begin
  2310. Message(scan_e_string_exceeds_255_chars);
  2311. msgwritten:=true;
  2312. end;
  2313. end;
  2314. end;
  2315. end;
  2316. '''' :
  2317. begin
  2318. repeat
  2319. readchar;
  2320. case c of
  2321. #26 :
  2322. end_of_file;
  2323. #10,#13 :
  2324. Message(scan_f_string_exceeds_line);
  2325. '''' :
  2326. begin
  2327. readchar;
  2328. if c<>'''' then
  2329. break;
  2330. end;
  2331. end;
  2332. if iswidestring then
  2333. concatwidestringchar(patternw,asciichar2unicode(c))
  2334. else
  2335. begin
  2336. if len<255 then
  2337. begin
  2338. inc(len);
  2339. pattern[len]:=c;
  2340. end
  2341. else
  2342. begin
  2343. if not msgwritten then
  2344. begin
  2345. Message(scan_e_string_exceeds_255_chars);
  2346. msgwritten:=true;
  2347. end;
  2348. end;
  2349. end;
  2350. until false;
  2351. end;
  2352. '^' :
  2353. begin
  2354. readchar;
  2355. c:=upcase(c);
  2356. if c<#64 then
  2357. c:=chr(ord(c)+64)
  2358. else
  2359. c:=chr(ord(c)-64);
  2360. if iswidestring then
  2361. concatwidestringchar(patternw,asciichar2unicode(c))
  2362. else
  2363. begin
  2364. if len<255 then
  2365. begin
  2366. inc(len);
  2367. pattern[len]:=c;
  2368. end
  2369. else
  2370. begin
  2371. if not msgwritten then
  2372. begin
  2373. Message(scan_e_string_exceeds_255_chars);
  2374. msgwritten:=true;
  2375. end;
  2376. end;
  2377. end;
  2378. readchar;
  2379. end;
  2380. else
  2381. break;
  2382. end;
  2383. until false;
  2384. { strings with length 1 become const chars }
  2385. if iswidestring then
  2386. begin
  2387. if patternw^.len=1 then
  2388. token:=_CWCHAR
  2389. else
  2390. token:=_CWSTRING;
  2391. end
  2392. else
  2393. begin
  2394. pattern[0]:=chr(len);
  2395. if len=1 then
  2396. token:=_CCHAR
  2397. else
  2398. token:=_CSTRING;
  2399. end;
  2400. goto exit_label;
  2401. end;
  2402. '>' :
  2403. begin
  2404. readchar;
  2405. case c of
  2406. '=' :
  2407. begin
  2408. readchar;
  2409. token:=_GTE;
  2410. goto exit_label;
  2411. end;
  2412. '>' :
  2413. begin
  2414. readchar;
  2415. token:=_OP_SHR;
  2416. goto exit_label;
  2417. end;
  2418. '<' :
  2419. begin { >< is for a symetric diff for sets }
  2420. readchar;
  2421. token:=_SYMDIF;
  2422. goto exit_label;
  2423. end;
  2424. end;
  2425. token:=_GT;
  2426. goto exit_label;
  2427. end;
  2428. '<' :
  2429. begin
  2430. readchar;
  2431. case c of
  2432. '>' :
  2433. begin
  2434. readchar;
  2435. token:=_UNEQUAL;
  2436. goto exit_label;
  2437. end;
  2438. '=' :
  2439. begin
  2440. readchar;
  2441. token:=_LTE;
  2442. goto exit_label;
  2443. end;
  2444. '<' :
  2445. begin
  2446. readchar;
  2447. token:=_OP_SHL;
  2448. goto exit_label;
  2449. end;
  2450. end;
  2451. token:=_LT;
  2452. goto exit_label;
  2453. end;
  2454. #26 :
  2455. begin
  2456. token:=_EOF;
  2457. checkpreprocstack;
  2458. goto exit_label;
  2459. end;
  2460. else
  2461. Illegal_Char(c);
  2462. end;
  2463. end;
  2464. exit_label:
  2465. lasttoken:=token;
  2466. end;
  2467. function tscannerfile.readpreproc:ttoken;
  2468. begin
  2469. skipspace;
  2470. case c of
  2471. 'A'..'Z',
  2472. 'a'..'z',
  2473. '_','0'..'9' : begin
  2474. current_scanner.preproc_pattern:=readid;
  2475. readpreproc:=_ID;
  2476. end;
  2477. '}' : begin
  2478. readpreproc:=_END;
  2479. end;
  2480. '(' : begin
  2481. readchar;
  2482. readpreproc:=_LKLAMMER;
  2483. end;
  2484. ')' : begin
  2485. readchar;
  2486. readpreproc:=_RKLAMMER;
  2487. end;
  2488. '+' : begin
  2489. readchar;
  2490. readpreproc:=_PLUS;
  2491. end;
  2492. '-' : begin
  2493. readchar;
  2494. readpreproc:=_MINUS;
  2495. end;
  2496. '*' : begin
  2497. readchar;
  2498. readpreproc:=_STAR;
  2499. end;
  2500. '/' : begin
  2501. readchar;
  2502. readpreproc:=_SLASH;
  2503. end;
  2504. '=' : begin
  2505. readchar;
  2506. readpreproc:=_EQUAL;
  2507. end;
  2508. '>' : begin
  2509. readchar;
  2510. if c='=' then
  2511. begin
  2512. readchar;
  2513. readpreproc:=_GTE;
  2514. end
  2515. else
  2516. readpreproc:=_GT;
  2517. end;
  2518. '<' : begin
  2519. readchar;
  2520. case c of
  2521. '>' : begin
  2522. readchar;
  2523. readpreproc:=_UNEQUAL;
  2524. end;
  2525. '=' : begin
  2526. readchar;
  2527. readpreproc:=_LTE;
  2528. end;
  2529. else readpreproc:=_LT;
  2530. end;
  2531. end;
  2532. #26 :
  2533. end_of_file;
  2534. else
  2535. begin
  2536. readpreproc:=_EOF;
  2537. checkpreprocstack;
  2538. end;
  2539. end;
  2540. end;
  2541. function tscannerfile.asmgetcharstart : char;
  2542. begin
  2543. { return first the character already
  2544. available in c }
  2545. lastasmgetchar:=c;
  2546. result:=asmgetchar;
  2547. end;
  2548. function tscannerfile.asmgetchar : char;
  2549. begin
  2550. if lastasmgetchar<>#0 then
  2551. begin
  2552. c:=lastasmgetchar;
  2553. lastasmgetchar:=#0;
  2554. end
  2555. else
  2556. readchar;
  2557. if in_asm_string then
  2558. begin
  2559. asmgetchar:=c;
  2560. exit;
  2561. end;
  2562. repeat
  2563. case c of
  2564. {$ifndef arm}
  2565. // the { ... } is used in ARM assembler to define register sets, so we can't used
  2566. // it as comment, either (* ... *), /* ... */ or // ... should be used instead
  2567. '{' :
  2568. skipcomment;
  2569. {$endif arm}
  2570. #10,#13 :
  2571. begin
  2572. linebreak;
  2573. asmgetchar:=c;
  2574. exit;
  2575. end;
  2576. #26 :
  2577. begin
  2578. reload;
  2579. if (c=#26) and not assigned(inputfile.next) then
  2580. end_of_file;
  2581. continue;
  2582. end;
  2583. '/' :
  2584. begin
  2585. readchar;
  2586. if c='/' then
  2587. skipdelphicomment
  2588. else
  2589. begin
  2590. asmgetchar:='/';
  2591. lastasmgetchar:=c;
  2592. exit;
  2593. end;
  2594. end;
  2595. '(' :
  2596. begin
  2597. readchar;
  2598. if c='*' then
  2599. begin
  2600. c:=#0;{Signal skipoldtpcomment to reload a char }
  2601. skipoldtpcomment;
  2602. end
  2603. else
  2604. begin
  2605. asmgetchar:='(';
  2606. lastasmgetchar:=c;
  2607. exit;
  2608. end;
  2609. end;
  2610. else
  2611. begin
  2612. asmgetchar:=c;
  2613. exit;
  2614. end;
  2615. end;
  2616. until false;
  2617. end;
  2618. {*****************************************************************************
  2619. Helpers
  2620. *****************************************************************************}
  2621. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  2622. begin
  2623. if dm in [directive_all, directive_turbo] then
  2624. turbo_scannerdirectives.insert(tdirectiveitem.create(s,p));
  2625. if dm in [directive_all, directive_mac] then
  2626. mac_scannerdirectives.insert(tdirectiveitem.create(s,p));
  2627. end;
  2628. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  2629. begin
  2630. if dm in [directive_all, directive_turbo] then
  2631. turbo_scannerdirectives.insert(tdirectiveitem.createcond(s,p));
  2632. if dm in [directive_all, directive_mac] then
  2633. mac_scannerdirectives.insert(tdirectiveitem.createcond(s,p));
  2634. end;
  2635. {*****************************************************************************
  2636. Initialization
  2637. *****************************************************************************}
  2638. procedure InitScanner;
  2639. begin
  2640. InitWideString(patternw);
  2641. turbo_scannerdirectives:=TDictionary.Create;
  2642. mac_scannerdirectives:=TDictionary.Create;
  2643. { Default Turbo directives and conditionals }
  2644. AddDirective('DEFINE',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_define);
  2645. AddDirective('UNDEF',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_undef);
  2646. AddDirective('I',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_include);
  2647. AddDirective('INCLUDE',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_include);
  2648. AddConditional('ELSE',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_else);
  2649. AddConditional('ENDIF',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_endif);
  2650. AddConditional('IF',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_if);
  2651. AddConditional('IFDEF',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_ifdef);
  2652. AddConditional('IFNDEF',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_ifndef);
  2653. AddConditional('IFOPT',directive_turbo, {$ifdef FPCPROCVAR}@{$endif}dir_ifopt);
  2654. { Default Mac directives and conditionals: }
  2655. AddDirective('SETC',directive_mac, {$ifdef FPCPROCVAR}@{$endif}dir_setc);
  2656. AddConditional('IFC',directive_mac, {$ifdef FPCPROCVAR}@{$endif}dir_if);
  2657. AddConditional('ELSEC',directive_mac, {$ifdef FPCPROCVAR}@{$endif}dir_else);
  2658. AddConditional('ENDC',directive_mac, {$ifdef FPCPROCVAR}@{$endif}dir_endif);
  2659. end;
  2660. procedure DoneScanner;
  2661. begin
  2662. turbo_scannerdirectives.Free;
  2663. mac_scannerdirectives.Free;
  2664. DoneWideString(patternw);
  2665. end;
  2666. end.
  2667. {
  2668. $Log$
  2669. Revision 1.64 2003-11-10 19:08:32 peter
  2670. * line numbering is now only done when #10, #10#13 is really parsed
  2671. instead of when it is the next character
  2672. Revision 1.63 2003/10/29 21:02:51 peter
  2673. * set ms_compiled after the program/unit is parsed
  2674. * check for ms_compiled before checking preproc matches
  2675. Revision 1.62 2003/09/17 22:30:19 olle
  2676. + support for a different set of compiler directives under $MODE MAC
  2677. + added mac directives $SETC $IFC $ELSEC $ENDC
  2678. Revision 1.61 2003/09/03 11:18:37 florian
  2679. * fixed arm concatcopy
  2680. + arm support in the common compiler sources added
  2681. * moved some generic cg code around
  2682. + tfputype added
  2683. * ...
  2684. Revision 1.60 2003/08/10 17:25:23 peter
  2685. * fixed some reported bugs
  2686. Revision 1.59 2003/05/25 10:26:43 peter
  2687. * recursive include depth check
  2688. Revision 1.58 2003/04/26 00:30:27 peter
  2689. * don't close inputfile when still closed
  2690. Revision 1.57 2003/01/09 21:52:37 peter
  2691. * merged some verbosity options.
  2692. * V_LineInfo is a verbosity flag to include line info
  2693. Revision 1.56 2002/12/29 14:57:50 peter
  2694. * unit loading changed to first register units and load them
  2695. afterwards. This is needed to support uses xxx in yyy correctly
  2696. * unit dependency check fixed
  2697. Revision 1.55 2002/12/27 18:05:58 peter
  2698. * use gettoken to get filename for include
  2699. Revision 1.54 2002/12/27 16:45:50 peter
  2700. * fix delphi comment parsing when skipping preproc directive
  2701. Revision 1.53 2002/12/27 15:26:43 peter
  2702. * give an error when no symbol is specified after $if(n)def
  2703. Revision 1.52 2002/12/24 23:32:02 peter
  2704. * support quotes around include filenames
  2705. Revision 1.51 2002/12/05 19:27:00 carl
  2706. * remove a stupid thing that i commited
  2707. Revision 1.50 2002/11/29 22:31:19 carl
  2708. + unimplemented hint directive added
  2709. * hint directive parsing implemented
  2710. * warning on these directives
  2711. Revision 1.49 2002/11/26 22:56:40 peter
  2712. * fix macro nesting check
  2713. Revision 1.48 2002/09/16 19:05:48 peter
  2714. * parse ^ after nil as caret
  2715. Revision 1.47 2002/09/06 14:58:42 carl
  2716. * bugfix of bug report 2072 (merged)
  2717. Revision 1.46 2002/09/05 19:27:05 peter
  2718. * fixed crash when current_module becomes nil
  2719. Revision 1.45 2002/09/05 14:17:27 pierre
  2720. * fix for bug 2004 merged
  2721. Revision 1.44 2002/08/12 16:46:04 peter
  2722. * tscannerfile is now destroyed in tmodule.reset and current_scanner
  2723. is updated accordingly. This removes all the loading and saving of
  2724. the old scanner and the invalid flag marking
  2725. Revision 1.43 2002/08/11 14:28:19 peter
  2726. * TScannerFile.SetInvalid added that will also reset inputfile
  2727. Revision 1.42 2002/08/10 14:46:31 carl
  2728. + moved target_cpu_string to cpuinfo
  2729. * renamed asmmode enum.
  2730. * assembler reader has now less ifdef's
  2731. * move from nppcmem.pas -> ncgmem.pas vec. node.
  2732. Revision 1.41 2002/08/06 21:12:16 florian
  2733. + support for octal constants, they are specified by a leading &
  2734. Revision 1.40 2002/07/20 17:35:52 florian
  2735. + char constants specified with #.. with more than 3 digits are handled as widechar
  2736. Revision 1.39 2002/05/18 13:34:17 peter
  2737. * readded missing revisions
  2738. Revision 1.38 2002/05/16 19:46:44 carl
  2739. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2740. + try to fix temp allocation (still in ifdef)
  2741. + generic constructor calls
  2742. + start of tassembler / tmodulebase class cleanup
  2743. Revision 1.36 2002/04/21 18:57:23 peter
  2744. * fixed memleaks when file can't be opened
  2745. Revision 1.35 2002/04/21 15:22:26 carl
  2746. * first check .inc file extension
  2747. Revision 1.34 2002/04/21 07:24:09 carl
  2748. - remove my fixes until Peter agrees on the fix (sorry Peter)
  2749. Revision 1.32 2002/04/19 15:42:11 peter
  2750. * default extension checking for include files
  2751. Revision 1.31 2002/03/01 14:39:44 peter
  2752. * fixed // and (* parsing to not be done when already parsing a
  2753. tp comment in skipuntildirective
  2754. Revision 1.30 2002/03/01 12:39:26 peter
  2755. * support // parsing in skipuntildirective
  2756. Revision 1.29 2002/01/27 21:44:26 peter
  2757. * FPCTARGETOS/FPCTARGETCPU added as internal environment variable
  2758. Revision 1.28 2002/01/24 18:25:50 peter
  2759. * implicit result variable generation for assembler routines
  2760. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  2761. }