scanner.pas 105 KB

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