scanner.pas 101 KB

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