scanner.pas 102 KB

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