scanner.pas 104 KB

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