scanner.pas 106 KB

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