scanner.pas 101 KB

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