scanner.pas 99 KB

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