scanner.pas 102 KB

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