scanner.pas 100 KB

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