scanner.pas 100 KB

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