scanner.pas 144 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the scanner part and handling of the switches
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit scanner;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,constexp,version,tokens,
  23. verbose,comphook,
  24. finput,
  25. widestr;
  26. const
  27. max_include_nesting=32;
  28. max_macro_nesting=16;
  29. preprocbufsize=32*1024;
  30. type
  31. tcommentstyle = (comment_none,comment_tp,comment_oldtp,comment_delphi,comment_c);
  32. tscannerfile = class;
  33. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else,pp_elseif);
  34. tpreprocstack = class
  35. typ : preproctyp;
  36. accept : boolean;
  37. next : tpreprocstack;
  38. name : TIDString;
  39. line_nb : longint;
  40. owner : tscannerfile;
  41. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  42. end;
  43. tdirectiveproc=procedure;
  44. tdirectiveitem = class(TFPHashObject)
  45. public
  46. is_conditional : boolean;
  47. proc : tdirectiveproc;
  48. constructor Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  49. constructor CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  50. end;
  51. // stack for replay buffers
  52. treplaystack = class
  53. token : ttoken;
  54. settings : tsettings;
  55. tokenbuf : tdynamicarray;
  56. next : treplaystack;
  57. constructor Create(atoken: ttoken;asettings:tsettings;atokenbuf:tdynamicarray;anext:treplaystack);
  58. end;
  59. tcompile_time_predicate = function(var valuedescr: String) : Boolean;
  60. tspecialgenerictoken = (ST_LOADSETTINGS,ST_LINE,ST_COLUMN,ST_FILEINDEX);
  61. { tscannerfile }
  62. tscannerfile = class
  63. private
  64. procedure do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  65. procedure cachenexttokenpos;
  66. procedure setnexttoken;
  67. procedure savetokenpos;
  68. procedure restoretokenpos;
  69. procedure writetoken(t: ttoken);
  70. function readtoken : ttoken;
  71. public
  72. inputfile : tinputfile; { current inputfile list }
  73. inputfilecount : longint;
  74. inputbuffer, { input buffer }
  75. inputpointer : pchar;
  76. inputstart : longint;
  77. line_no, { line }
  78. lastlinepos : longint;
  79. lasttokenpos,
  80. nexttokenpos : longint; { token }
  81. lasttoken,
  82. nexttoken : ttoken;
  83. oldlasttokenpos : longint; { temporary saving/restoring tokenpos }
  84. oldcurrent_filepos,
  85. oldcurrent_tokenpos : tfileposinfo;
  86. replaytokenbuf,
  87. recordtokenbuf : tdynamicarray;
  88. { last settings we stored }
  89. last_settings : tsettings;
  90. { last filepos we stored }
  91. last_filepos,
  92. { if nexttoken<>NOTOKEN, then nexttokenpos holds its filepos }
  93. next_filepos : tfileposinfo;
  94. comment_level,
  95. yylexcount : longint;
  96. lastasmgetchar : char;
  97. ignoredirectives : TFPHashList; { ignore directives, used to give warnings only once }
  98. preprocstack : tpreprocstack;
  99. replaystack : treplaystack;
  100. in_asm_string : boolean;
  101. preproc_pattern : string;
  102. preproc_token : ttoken;
  103. constructor Create(const fn:string; is_macro: boolean = false);
  104. destructor Destroy;override;
  105. { File buffer things }
  106. function openinputfile:boolean;
  107. procedure closeinputfile;
  108. function tempopeninputfile:boolean;
  109. procedure tempcloseinputfile;
  110. procedure saveinputfile;
  111. procedure restoreinputfile;
  112. procedure firstfile;
  113. procedure nextfile;
  114. procedure addfile(hp:tinputfile);
  115. procedure reload;
  116. { replaces current token with the text in p }
  117. procedure substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  118. { Scanner things }
  119. procedure gettokenpos;
  120. procedure inc_comment_level;
  121. procedure dec_comment_level;
  122. procedure illegal_char(c:char);
  123. procedure end_of_file;
  124. procedure checkpreprocstack;
  125. procedure poppreprocstack;
  126. procedure ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  127. procedure elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  128. procedure elsepreprocstack;
  129. procedure popreplaystack;
  130. procedure handleconditional(p:tdirectiveitem);
  131. procedure handledirectives;
  132. procedure linebreak;
  133. procedure recordtoken;
  134. procedure startrecordtokens(buf:tdynamicarray);
  135. procedure stoprecordtokens;
  136. procedure replaytoken;
  137. procedure startreplaytokens(buf:tdynamicarray);
  138. procedure readchar;
  139. procedure readstring;
  140. procedure readnumber;
  141. function readid:string;
  142. function readval:longint;
  143. function readval_asstring:string;
  144. function readcomment:string;
  145. function readquotedstring:string;
  146. function readstate:char;
  147. function readstatedefault:char;
  148. procedure skipspace;
  149. procedure skipuntildirective;
  150. procedure skipcomment;
  151. procedure skipdelphicomment;
  152. procedure skipoldtpcomment;
  153. procedure readtoken(allowrecordtoken:boolean);
  154. function readpreproc:ttoken;
  155. function asmgetcharstart : char;
  156. function asmgetchar:char;
  157. end;
  158. {$ifdef PREPROCWRITE}
  159. tpreprocfile=class
  160. f : text;
  161. buf : pointer;
  162. spacefound,
  163. eolfound : boolean;
  164. constructor create(const fn:string);
  165. destructor destroy;
  166. procedure Add(const s:string);
  167. procedure AddSpace;
  168. end;
  169. {$endif PREPROCWRITE}
  170. var
  171. { read strings }
  172. c : char;
  173. orgpattern,
  174. pattern : string;
  175. cstringpattern : ansistring;
  176. patternw : pcompilerwidestring;
  177. { token }
  178. token, { current token being parsed }
  179. idtoken : ttoken; { holds the token if the pattern is a known word }
  180. current_scanner : tscannerfile; { current scanner in use }
  181. aktcommentstyle : tcommentstyle; { needed to use read_comment from directives }
  182. {$ifdef PREPROCWRITE}
  183. preprocfile : tpreprocfile; { used with only preprocessing }
  184. {$endif PREPROCWRITE}
  185. type
  186. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  187. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  188. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  189. procedure InitScanner;
  190. procedure DoneScanner;
  191. { To be called when the language mode is finally determined }
  192. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  193. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  194. implementation
  195. uses
  196. SysUtils,
  197. cutils,cfileutl,
  198. systems,
  199. switches,
  200. symbase,symtable,symtype,symsym,symconst,symdef,defutil,
  201. fmodule;
  202. var
  203. { dictionaries with the supported directives }
  204. turbo_scannerdirectives : TFPHashObjectList; { for other modes }
  205. mac_scannerdirectives : TFPHashObjectList; { for mode mac }
  206. {*****************************************************************************
  207. Helper routines
  208. *****************************************************************************}
  209. const
  210. { use any special name that is an invalid file name to avoid problems }
  211. preprocstring : array [preproctyp] of string[7]
  212. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE','$ELSEIF');
  213. function is_keyword(const s:string):boolean;
  214. var
  215. low,high,mid : longint;
  216. begin
  217. if not (length(s) in [tokenlenmin..tokenlenmax]) or
  218. not (s[1] in ['a'..'z','A'..'Z']) then
  219. begin
  220. is_keyword:=false;
  221. exit;
  222. end;
  223. low:=ord(tokenidx^[length(s),s[1]].first);
  224. high:=ord(tokenidx^[length(s),s[1]].last);
  225. while low<high do
  226. begin
  227. mid:=(high+low+1) shr 1;
  228. if pattern<tokeninfo^[ttoken(mid)].str then
  229. high:=mid-1
  230. else
  231. low:=mid;
  232. end;
  233. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  234. (tokeninfo^[ttoken(high)].keyword in current_settings.modeswitches);
  235. end;
  236. Procedure HandleModeSwitches(changeInit: boolean);
  237. begin
  238. { turn ansi/unicodestrings on by default ? }
  239. if ([m_default_ansistring,m_default_unicodestring]*current_settings.modeswitches)<>[] then
  240. begin
  241. include(current_settings.localswitches,cs_refcountedstrings);
  242. if changeinit then
  243. include(init_settings.localswitches,cs_refcountedstrings);
  244. end
  245. else
  246. begin
  247. exclude(current_settings.localswitches,cs_refcountedstrings);
  248. if changeinit then
  249. exclude(init_settings.localswitches,cs_refcountedstrings);
  250. end;
  251. { turn inline on by default ? }
  252. if (m_default_inline in current_settings.modeswitches) then
  253. begin
  254. include(current_settings.localswitches,cs_do_inline);
  255. if changeinit then
  256. include(init_settings.localswitches,cs_do_inline);
  257. end
  258. else
  259. begin
  260. exclude(current_settings.localswitches,cs_do_inline);
  261. if changeinit then
  262. exclude(init_settings.localswitches,cs_do_inline);
  263. end;
  264. end;
  265. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  266. var
  267. b : boolean;
  268. oldmodeswitches : tmodeswitches;
  269. begin
  270. oldmodeswitches:=current_settings.modeswitches;
  271. b:=true;
  272. if s='DEFAULT' then
  273. current_settings.modeswitches:=fpcmodeswitches
  274. else
  275. if s='DELPHI' then
  276. current_settings.modeswitches:=delphimodeswitches
  277. else
  278. if s='TP' then
  279. current_settings.modeswitches:=tpmodeswitches
  280. else
  281. if s='FPC' then begin
  282. current_settings.modeswitches:=fpcmodeswitches;
  283. { TODO: enable this for 2.3/2.9 }
  284. // include(current_settings.localswitches, cs_typed_addresses);
  285. end else
  286. if s='OBJFPC' then begin
  287. current_settings.modeswitches:=objfpcmodeswitches;
  288. { TODO: enable this for 2.3/2.9 }
  289. // include(current_settings.localswitches, cs_typed_addresses);
  290. end
  291. {$ifdef gpc_mode}
  292. else if s='GPC' then
  293. current_settings.modeswitches:=gpcmodeswitches
  294. {$endif}
  295. else
  296. if s='MACPAS' then
  297. current_settings.modeswitches:=macmodeswitches
  298. else
  299. if s='ISO' then
  300. current_settings.modeswitches:=isomodeswitches
  301. else
  302. b:=false;
  303. {$ifdef jvm}
  304. { enable final fields by default for the JVM targets }
  305. include(current_settings.modeswitches,m_final_fields);
  306. {$endif jvm}
  307. if b and changeInit then
  308. init_settings.modeswitches := current_settings.modeswitches;
  309. if b then
  310. begin
  311. { resolve all postponed switch changes }
  312. flushpendingswitchesstate;
  313. HandleModeSwitches(changeinit);
  314. { turn on bitpacking for mode macpas and iso pascal }
  315. if ([m_mac,m_iso] * current_settings.modeswitches <> []) then
  316. begin
  317. include(current_settings.localswitches,cs_bitpacking);
  318. if changeinit then
  319. include(init_settings.localswitches,cs_bitpacking);
  320. end;
  321. { support goto/label by default in delphi/tp7/mac modes }
  322. if ([m_delphi,m_tp7,m_mac,m_iso] * current_settings.modeswitches <> []) then
  323. begin
  324. include(current_settings.moduleswitches,cs_support_goto);
  325. if changeinit then
  326. include(init_settings.moduleswitches,cs_support_goto);
  327. end;
  328. { support pointer math by default in fpc/objfpc modes }
  329. if ([m_fpc,m_objfpc] * current_settings.modeswitches <> []) then
  330. begin
  331. include(current_settings.localswitches,cs_pointermath);
  332. if changeinit then
  333. include(init_settings.localswitches,cs_pointermath);
  334. end
  335. else
  336. begin
  337. exclude(current_settings.localswitches,cs_pointermath);
  338. if changeinit then
  339. exclude(init_settings.localswitches,cs_pointermath);
  340. end;
  341. { Default enum and set packing for delphi/tp7 }
  342. if (m_tp7 in current_settings.modeswitches) or
  343. (m_delphi in current_settings.modeswitches) then
  344. begin
  345. current_settings.packenum:=1;
  346. current_settings.setalloc:=1;
  347. end
  348. else if (m_mac in current_settings.modeswitches) then
  349. { compatible with Metrowerks Pascal }
  350. current_settings.packenum:=2
  351. else
  352. current_settings.packenum:=4;
  353. if changeinit then
  354. init_settings.packenum:=current_settings.packenum;
  355. {$ifdef i386}
  356. { Default to intel assembler for delphi/tp7 on i386 }
  357. if (m_delphi in current_settings.modeswitches) or
  358. (m_tp7 in current_settings.modeswitches) then
  359. current_settings.asmmode:=asmmode_i386_intel;
  360. if changeinit then
  361. init_settings.asmmode:=current_settings.asmmode;
  362. {$endif i386}
  363. { Exception support explicitly turned on (mainly for macpas, to }
  364. { compensate for lack of interprocedural goto support) }
  365. if (cs_support_exceptions in current_settings.globalswitches) then
  366. include(current_settings.modeswitches,m_except);
  367. { Default strict string var checking in TP/Delphi modes }
  368. if ([m_delphi,m_tp7] * current_settings.modeswitches <> []) then
  369. begin
  370. include(current_settings.localswitches,cs_strict_var_strings);
  371. if changeinit then
  372. include(init_settings.localswitches,cs_strict_var_strings);
  373. end;
  374. { Undefine old symbol }
  375. if (m_delphi in oldmodeswitches) then
  376. undef_system_macro('FPC_DELPHI')
  377. else if (m_tp7 in oldmodeswitches) then
  378. undef_system_macro('FPC_TP')
  379. else if (m_objfpc in oldmodeswitches) then
  380. undef_system_macro('FPC_OBJFPC')
  381. {$ifdef gpc_mode}
  382. else if (m_gpc in oldmodeswitches) then
  383. undef_system_macro('FPC_GPC')
  384. {$endif}
  385. else if (m_mac in oldmodeswitches) then
  386. undef_system_macro('FPC_MACPAS');
  387. { define new symbol in delphi,objfpc,tp,gpc,macpas mode }
  388. if (m_delphi in current_settings.modeswitches) then
  389. def_system_macro('FPC_DELPHI')
  390. else if (m_tp7 in current_settings.modeswitches) then
  391. def_system_macro('FPC_TP')
  392. else if (m_objfpc in current_settings.modeswitches) then
  393. def_system_macro('FPC_OBJFPC')
  394. {$ifdef gpc_mode}
  395. else if (m_gpc in current_settings.modeswitches) then
  396. def_system_macro('FPC_GPC')
  397. {$endif}
  398. else if (m_mac in current_settings.modeswitches) then
  399. def_system_macro('FPC_MACPAS');
  400. end;
  401. SetCompileMode:=b;
  402. end;
  403. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  404. var
  405. i : tmodeswitch;
  406. doinclude : boolean;
  407. begin
  408. s:=upper(s);
  409. { on/off? }
  410. doinclude:=true;
  411. case s[length(s)] of
  412. '+':
  413. s:=copy(s,1,length(s)-1);
  414. '-':
  415. begin
  416. s:=copy(s,1,length(s)-1);
  417. doinclude:=false;
  418. end;
  419. end;
  420. Result:=false;
  421. for i:=m_class to high(tmodeswitch) do
  422. if s=modeswitchstr[i] then
  423. begin
  424. { Objective-C is currently only supported for Darwin targets }
  425. if doinclude and
  426. (i in [m_objectivec1,m_objectivec2]) and
  427. not(target_info.system in systems_objc_supported) then
  428. begin
  429. Message1(option_unsupported_target_for_feature,'Objective-C');
  430. break;
  431. end;
  432. if changeInit then
  433. current_settings.modeswitches:=init_settings.modeswitches;
  434. Result:=true;
  435. if doinclude then
  436. begin
  437. include(current_settings.modeswitches,i);
  438. { Objective-C 2.0 support implies 1.0 support }
  439. if (i=m_objectivec2) then
  440. include(current_settings.modeswitches,m_objectivec1);
  441. if (i in [m_objectivec1,m_objectivec2]) then
  442. include(current_settings.modeswitches,m_class);
  443. end
  444. else
  445. begin
  446. exclude(current_settings.modeswitches,i);
  447. { Objective-C 2.0 support implies 1.0 support }
  448. if (i=m_objectivec2) then
  449. exclude(current_settings.modeswitches,m_objectivec1);
  450. if (i in [m_objectivec1,m_objectivec2]) and
  451. ([m_delphi,m_objfpc]*current_settings.modeswitches=[]) then
  452. exclude(current_settings.modeswitches,m_class);
  453. end;
  454. { set other switches depending on changed mode switch }
  455. HandleModeSwitches(changeinit);
  456. if changeInit then
  457. init_settings.modeswitches:=current_settings.modeswitches;
  458. break;
  459. end;
  460. end;
  461. {*****************************************************************************
  462. Conditional Directives
  463. *****************************************************************************}
  464. procedure dir_else;
  465. begin
  466. current_scanner.elsepreprocstack;
  467. end;
  468. procedure dir_endif;
  469. begin
  470. current_scanner.poppreprocstack;
  471. end;
  472. function isdef(var valuedescr: String): Boolean;
  473. var
  474. hs : string;
  475. begin
  476. current_scanner.skipspace;
  477. hs:=current_scanner.readid;
  478. valuedescr:= hs;
  479. if hs='' then
  480. Message(scan_e_error_in_preproc_expr);
  481. isdef:=defined_macro(hs);
  482. end;
  483. procedure dir_ifdef;
  484. begin
  485. current_scanner.ifpreprocstack(pp_ifdef,@isdef,scan_c_ifdef_found);
  486. end;
  487. function isnotdef(var valuedescr: String): Boolean;
  488. var
  489. hs : string;
  490. begin
  491. current_scanner.skipspace;
  492. hs:=current_scanner.readid;
  493. valuedescr:= hs;
  494. if hs='' then
  495. Message(scan_e_error_in_preproc_expr);
  496. isnotdef:=not defined_macro(hs);
  497. end;
  498. procedure dir_ifndef;
  499. begin
  500. current_scanner.ifpreprocstack(pp_ifndef,@isnotdef,scan_c_ifndef_found);
  501. end;
  502. function opt_check(var valuedescr: String): Boolean;
  503. var
  504. hs : string;
  505. state : char;
  506. begin
  507. opt_check:= false;
  508. current_scanner.skipspace;
  509. hs:=current_scanner.readid;
  510. valuedescr:= hs;
  511. if (length(hs)>1) then
  512. Message1(scan_w_illegal_switch,hs)
  513. else
  514. begin
  515. state:=current_scanner.ReadState;
  516. if state in ['-','+'] then
  517. opt_check:=CheckSwitch(hs[1],state)
  518. else
  519. Message(scan_e_error_in_preproc_expr);
  520. end;
  521. end;
  522. procedure dir_ifopt;
  523. begin
  524. flushpendingswitchesstate;
  525. current_scanner.ifpreprocstack(pp_ifopt,@opt_check,scan_c_ifopt_found);
  526. end;
  527. procedure dir_libprefix;
  528. var
  529. s : string;
  530. begin
  531. current_scanner.skipspace;
  532. if c <> '''' then
  533. Message2(scan_f_syn_expected, '''', c);
  534. s := current_scanner.readquotedstring;
  535. stringdispose(outputprefix);
  536. outputprefix := stringdup(s);
  537. with current_module do
  538. setfilename(paramfn^, paramallowoutput);
  539. end;
  540. procedure dir_libsuffix;
  541. var
  542. s : string;
  543. begin
  544. current_scanner.skipspace;
  545. if c <> '''' then
  546. Message2(scan_f_syn_expected, '''', c);
  547. s := current_scanner.readquotedstring;
  548. stringdispose(outputsuffix);
  549. outputsuffix := stringdup(s);
  550. with current_module do
  551. setfilename(paramfn^, paramallowoutput);
  552. end;
  553. procedure dir_extension;
  554. var
  555. s : string;
  556. begin
  557. current_scanner.skipspace;
  558. if c <> '''' then
  559. Message2(scan_f_syn_expected, '''', c);
  560. s := current_scanner.readquotedstring;
  561. if OutputFileName='' then
  562. OutputFileName:=InputFileName;
  563. OutputFileName:=ChangeFileExt(OutputFileName,'.'+s);
  564. with current_module do
  565. setfilename(paramfn^, paramallowoutput);
  566. end;
  567. {
  568. Compile time expression type check
  569. ----------------------------------
  570. Each subexpression returns its type to the caller, which then can
  571. do type check. Since data types of compile time expressions is
  572. not well defined, the type system does a best effort. The drawback is
  573. that some errors might not be detected.
  574. Instead of returning a particular data type, a set of possible data types
  575. are returned. This way ambigouos types can be handled. For instance a
  576. value of 1 can be both a boolean and and integer.
  577. Booleans
  578. --------
  579. The following forms of boolean values are supported:
  580. * C coded, that is 0 is false, non-zero is true.
  581. * TRUE/FALSE for mac style compile time variables
  582. Thus boolean mac compile time variables are always stored as TRUE/FALSE.
  583. When a compile time expression is evaluated, they are then translated
  584. to C coded booleans (0/1), to simplify for the expression evaluator.
  585. Note that this scheme then also of support mac compile time variables which
  586. are 0/1 but with a boolean meaning.
  587. The TRUE/FALSE format is new from 22 august 2005, but the above scheme
  588. means that units which is not recompiled, and thus stores
  589. compile time variables as the old format (0/1), continue to work.
  590. Short circuit evaluation
  591. ------------------------
  592. For this to work, the part of a compile time expression which is short
  593. circuited, should not be evaluated, while it still should be parsed.
  594. Therefor there is a parameter eval, telling whether evaluation is needed.
  595. In case not, the value returned can be arbitrary.
  596. }
  597. type
  598. {Compile time expression types}
  599. TCTEType = (ctetBoolean, ctetInteger, ctetString, ctetSet);
  600. TCTETypeSet = set of TCTEType;
  601. const
  602. cteTypeNames : array[TCTEType] of string[10] = (
  603. 'BOOLEAN','INTEGER','STRING','SET');
  604. {Subset of types which can be elements in sets.}
  605. setelementdefs = [ctetBoolean, ctetInteger, ctetString];
  606. function GetCTETypeName(t: TCTETypeSet): String;
  607. var
  608. i: TCTEType;
  609. begin
  610. result:= '';
  611. for i:= Low(TCTEType) to High(TCTEType) do
  612. if i in t then
  613. if result = '' then
  614. result:= cteTypeNames[i]
  615. else
  616. result:= result + ' or ' + cteTypeNames[i];
  617. end;
  618. procedure CTEError(actType, desiredExprType: TCTETypeSet; place: String);
  619. begin
  620. Message3(scan_e_compile_time_typeerror,
  621. GetCTETypeName(desiredExprType),
  622. GetCTETypeName(actType),
  623. place
  624. );
  625. end;
  626. function parse_compiler_expr(var compileExprType: TCTETypeSet):string;
  627. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string; forward;
  628. procedure preproc_consume(t : ttoken);
  629. begin
  630. if t<>current_scanner.preproc_token then
  631. Message(scan_e_preproc_syntax_error);
  632. current_scanner.preproc_token:=current_scanner.readpreproc;
  633. end;
  634. function preproc_substitutedtoken(var macroType: TCTETypeSet; eval : Boolean): string;
  635. { Currently this parses identifiers as well as numbers.
  636. The result from this procedure can either be that the token
  637. itself is a value, or that it is a compile time variable/macro,
  638. which then is substituted for another value (for macros
  639. recursivelly substituted).}
  640. var
  641. hs: string;
  642. mac : tmacro;
  643. macrocount,
  644. len : integer;
  645. numres : longint;
  646. w: word;
  647. begin
  648. result := current_scanner.preproc_pattern;
  649. if not eval then
  650. exit;
  651. mac:= nil;
  652. { Substitue macros and compiler variables with their content/value.
  653. For real macros also do recursive substitution. }
  654. macrocount:=0;
  655. repeat
  656. mac:=tmacro(search_macro(result));
  657. inc(macrocount);
  658. if macrocount>max_macro_nesting then
  659. begin
  660. Message(scan_w_macro_too_deep);
  661. break;
  662. end;
  663. if assigned(mac) and mac.defined then
  664. if assigned(mac.buftext) then
  665. begin
  666. if mac.buflen>255 then
  667. begin
  668. len:=255;
  669. Message(scan_w_macro_cut_after_255_chars);
  670. end
  671. else
  672. len:=mac.buflen;
  673. hs[0]:=char(len);
  674. move(mac.buftext^,hs[1],len);
  675. result:=upcase(hs);
  676. mac.is_used:=true;
  677. end
  678. else
  679. begin
  680. Message1(scan_e_error_macro_lacks_value, result);
  681. break;
  682. end
  683. else
  684. begin
  685. break;
  686. end;
  687. if mac.is_compiler_var then
  688. break;
  689. until false;
  690. { At this point, result do contain the value. Do some decoding and
  691. determine the type.}
  692. val(result,numres,w);
  693. if (w=0) then {It is an integer}
  694. begin
  695. if (numres = 0) or (numres = 1) then
  696. macroType := [ctetInteger, ctetBoolean]
  697. else
  698. macroType := [ctetInteger];
  699. end
  700. else if assigned(mac) and (m_mac in current_settings.modeswitches) and (result='FALSE') then
  701. begin
  702. result:= '0';
  703. macroType:= [ctetBoolean];
  704. end
  705. else if assigned(mac) and (m_mac in current_settings.modeswitches) and (result='TRUE') then
  706. begin
  707. result:= '1';
  708. macroType:= [ctetBoolean];
  709. end
  710. else if (m_mac in current_settings.modeswitches) and
  711. (not assigned(mac) or not mac.defined) and
  712. (macrocount = 1) then
  713. begin
  714. {Errors in mode mac is issued here. For non macpas modes there is
  715. more liberty, but the error will eventually be caught at a later stage.}
  716. Message1(scan_e_error_macro_undefined, result);
  717. macroType:= [ctetString]; {Just to have something}
  718. end
  719. else
  720. macroType:= [ctetString];
  721. end;
  722. function read_factor(var factorType: TCTETypeSet; eval : Boolean) : string;
  723. var
  724. hs : string;
  725. mac: tmacro;
  726. srsym : tsym;
  727. srsymtable : TSymtable;
  728. hdef : TDef;
  729. l : longint;
  730. w : integer;
  731. hasKlammer: Boolean;
  732. setElemType : TCTETypeSet;
  733. begin
  734. if current_scanner.preproc_token=_ID then
  735. begin
  736. if current_scanner.preproc_pattern='DEFINED' then
  737. begin
  738. factorType:= [ctetBoolean];
  739. preproc_consume(_ID);
  740. current_scanner.skipspace;
  741. if current_scanner.preproc_token =_LKLAMMER then
  742. begin
  743. preproc_consume(_LKLAMMER);
  744. current_scanner.skipspace;
  745. hasKlammer:= true;
  746. end
  747. else if (m_mac in current_settings.modeswitches) then
  748. hasKlammer:= false
  749. else
  750. Message(scan_e_error_in_preproc_expr);
  751. if current_scanner.preproc_token =_ID then
  752. begin
  753. hs := current_scanner.preproc_pattern;
  754. mac := tmacro(search_macro(hs));
  755. if assigned(mac) and mac.defined then
  756. begin
  757. hs := '1';
  758. mac.is_used:=true;
  759. end
  760. else
  761. hs := '0';
  762. read_factor := hs;
  763. preproc_consume(_ID);
  764. current_scanner.skipspace;
  765. end
  766. else
  767. Message(scan_e_error_in_preproc_expr);
  768. if hasKlammer then
  769. if current_scanner.preproc_token =_RKLAMMER then
  770. preproc_consume(_RKLAMMER)
  771. else
  772. Message(scan_e_error_in_preproc_expr);
  773. end
  774. else
  775. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  776. begin
  777. factorType:= [ctetBoolean];
  778. preproc_consume(_ID);
  779. current_scanner.skipspace;
  780. if current_scanner.preproc_token =_ID then
  781. begin
  782. hs := current_scanner.preproc_pattern;
  783. mac := tmacro(search_macro(hs));
  784. if assigned(mac) then
  785. begin
  786. hs := '0';
  787. mac.is_used:=true;
  788. end
  789. else
  790. hs := '1';
  791. read_factor := hs;
  792. preproc_consume(_ID);
  793. current_scanner.skipspace;
  794. end
  795. else
  796. Message(scan_e_error_in_preproc_expr);
  797. end
  798. else
  799. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  800. begin
  801. factorType:= [ctetBoolean];
  802. preproc_consume(_ID);
  803. current_scanner.skipspace;
  804. if current_scanner.preproc_token =_LKLAMMER then
  805. begin
  806. preproc_consume(_LKLAMMER);
  807. current_scanner.skipspace;
  808. end
  809. else
  810. Message(scan_e_error_in_preproc_expr);
  811. if not (current_scanner.preproc_token = _ID) then
  812. Message(scan_e_error_in_preproc_expr);
  813. hs:=current_scanner.preproc_pattern;
  814. if (length(hs) > 1) then
  815. {This is allowed in Metrowerks Pascal}
  816. Message(scan_e_error_in_preproc_expr)
  817. else
  818. begin
  819. if CheckSwitch(hs[1],'+') then
  820. read_factor := '1'
  821. else
  822. read_factor := '0';
  823. end;
  824. preproc_consume(_ID);
  825. current_scanner.skipspace;
  826. if current_scanner.preproc_token =_RKLAMMER then
  827. preproc_consume(_RKLAMMER)
  828. else
  829. Message(scan_e_error_in_preproc_expr);
  830. end
  831. else
  832. if current_scanner.preproc_pattern='SIZEOF' then
  833. begin
  834. factorType:= [ctetInteger];
  835. preproc_consume(_ID);
  836. current_scanner.skipspace;
  837. if current_scanner.preproc_token =_LKLAMMER then
  838. begin
  839. preproc_consume(_LKLAMMER);
  840. current_scanner.skipspace;
  841. end
  842. else
  843. Message(scan_e_preproc_syntax_error);
  844. if eval then
  845. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  846. begin
  847. l:=0;
  848. case srsym.typ of
  849. staticvarsym,
  850. localvarsym,
  851. paravarsym :
  852. l:=tabstractvarsym(srsym).getsize;
  853. typesym:
  854. l:=ttypesym(srsym).typedef.size;
  855. else
  856. Message(scan_e_error_in_preproc_expr);
  857. end;
  858. str(l,read_factor);
  859. end
  860. else
  861. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  862. preproc_consume(_ID);
  863. current_scanner.skipspace;
  864. if current_scanner.preproc_token =_RKLAMMER then
  865. preproc_consume(_RKLAMMER)
  866. else
  867. Message(scan_e_preproc_syntax_error);
  868. end
  869. else
  870. if current_scanner.preproc_pattern='HIGH' then
  871. begin
  872. factorType:= [ctetInteger];
  873. preproc_consume(_ID);
  874. current_scanner.skipspace;
  875. if current_scanner.preproc_token =_LKLAMMER then
  876. begin
  877. preproc_consume(_LKLAMMER);
  878. current_scanner.skipspace;
  879. end
  880. else
  881. Message(scan_e_preproc_syntax_error);
  882. if eval then
  883. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  884. begin
  885. hdef:=nil;
  886. hs:='';
  887. l:=0;
  888. case srsym.typ of
  889. staticvarsym,
  890. localvarsym,
  891. paravarsym :
  892. hdef:=tabstractvarsym(srsym).vardef;
  893. typesym:
  894. hdef:=ttypesym(srsym).typedef;
  895. else
  896. Message(scan_e_error_in_preproc_expr);
  897. end;
  898. if hdef<>nil then
  899. begin
  900. if hdef.typ=setdef then
  901. hdef:=tsetdef(hdef).elementdef;
  902. case hdef.typ of
  903. orddef:
  904. with torddef(hdef).high do
  905. if signed then
  906. str(svalue,hs)
  907. else
  908. str(uvalue,hs);
  909. enumdef:
  910. l:=tenumdef(hdef).maxval;
  911. arraydef:
  912. if is_open_array(hdef) or is_array_of_const(hdef) or is_dynamic_array(hdef) then
  913. Message(type_e_mismatch)
  914. else
  915. l:=tarraydef(hdef).highrange;
  916. stringdef:
  917. if is_open_string(hdef) or is_ansistring(hdef) or is_wide_or_unicode_string(hdef) then
  918. Message(type_e_mismatch)
  919. else
  920. l:=tstringdef(hdef).len;
  921. else
  922. Message(type_e_mismatch);
  923. end;
  924. end;
  925. if hs='' then
  926. str(l,read_factor)
  927. else
  928. read_factor:=hs;
  929. end
  930. else
  931. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  932. preproc_consume(_ID);
  933. current_scanner.skipspace;
  934. if current_scanner.preproc_token =_RKLAMMER then
  935. preproc_consume(_RKLAMMER)
  936. else
  937. Message(scan_e_preproc_syntax_error);
  938. end
  939. else
  940. if current_scanner.preproc_pattern='DECLARED' then
  941. begin
  942. factorType:= [ctetBoolean];
  943. preproc_consume(_ID);
  944. current_scanner.skipspace;
  945. if current_scanner.preproc_token =_LKLAMMER then
  946. begin
  947. preproc_consume(_LKLAMMER);
  948. current_scanner.skipspace;
  949. end
  950. else
  951. Message(scan_e_error_in_preproc_expr);
  952. if current_scanner.preproc_token =_ID then
  953. begin
  954. hs := upper(current_scanner.preproc_pattern);
  955. if searchsym(hs,srsym,srsymtable) then
  956. hs := '1'
  957. else
  958. hs := '0';
  959. read_factor := hs;
  960. preproc_consume(_ID);
  961. current_scanner.skipspace;
  962. end
  963. else
  964. Message(scan_e_error_in_preproc_expr);
  965. if current_scanner.preproc_token =_RKLAMMER then
  966. preproc_consume(_RKLAMMER)
  967. else
  968. Message(scan_e_error_in_preproc_expr);
  969. end
  970. else
  971. if current_scanner.preproc_pattern='NOT' then
  972. begin
  973. factorType:= [ctetBoolean];
  974. preproc_consume(_ID);
  975. hs:=read_factor(factorType, eval);
  976. if eval then
  977. begin
  978. if not (ctetBoolean in factorType) then
  979. CTEError(factorType, [ctetBoolean], 'NOT');
  980. val(hs,l,w);
  981. if l<>0 then
  982. read_factor:='0'
  983. else
  984. read_factor:='1';
  985. end
  986. else
  987. read_factor:='0'; {Just to have something}
  988. end
  989. else
  990. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='TRUE') then
  991. begin
  992. factorType:= [ctetBoolean];
  993. preproc_consume(_ID);
  994. read_factor:='1';
  995. end
  996. else
  997. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='FALSE') then
  998. begin
  999. factorType:= [ctetBoolean];
  1000. preproc_consume(_ID);
  1001. read_factor:='0';
  1002. end
  1003. else
  1004. begin
  1005. hs:=preproc_substitutedtoken(factorType, eval);
  1006. { Default is to return the original symbol }
  1007. read_factor:=hs;
  1008. if eval and ([m_delphi,m_objfpc]*current_settings.modeswitches<>[]) and (ctetString in factorType) then
  1009. if searchsym(current_scanner.preproc_pattern,srsym,srsymtable) then
  1010. begin
  1011. case srsym.typ of
  1012. constsym :
  1013. begin
  1014. with tconstsym(srsym) do
  1015. begin
  1016. case consttyp of
  1017. constord :
  1018. begin
  1019. case constdef.typ of
  1020. orddef:
  1021. begin
  1022. if is_integer(constdef) then
  1023. begin
  1024. read_factor:=tostr(value.valueord);
  1025. factorType:= [ctetInteger];
  1026. end
  1027. else if is_boolean(constdef) then
  1028. begin
  1029. read_factor:=tostr(value.valueord);
  1030. factorType:= [ctetBoolean];
  1031. end
  1032. else if is_char(constdef) then
  1033. begin
  1034. read_factor:=char(qword(value.valueord));
  1035. factorType:= [ctetString];
  1036. end
  1037. end;
  1038. enumdef:
  1039. begin
  1040. read_factor:=tostr(value.valueord);
  1041. factorType:= [ctetInteger];
  1042. end;
  1043. end;
  1044. end;
  1045. conststring :
  1046. begin
  1047. read_factor := upper(pchar(value.valueptr));
  1048. factorType:= [ctetString];
  1049. end;
  1050. constset :
  1051. begin
  1052. hs:=',';
  1053. for l:=0 to 255 do
  1054. if l in pconstset(tconstsym(srsym).value.valueptr)^ then
  1055. hs:=hs+tostr(l)+',';
  1056. read_factor := hs;
  1057. factorType:= [ctetSet];
  1058. end;
  1059. end;
  1060. end;
  1061. end;
  1062. enumsym :
  1063. begin
  1064. read_factor:=tostr(tenumsym(srsym).value);
  1065. factorType:= [ctetInteger];
  1066. end;
  1067. end;
  1068. end;
  1069. preproc_consume(_ID);
  1070. current_scanner.skipspace;
  1071. end
  1072. end
  1073. else if current_scanner.preproc_token =_LKLAMMER then
  1074. begin
  1075. preproc_consume(_LKLAMMER);
  1076. read_factor:=read_expr(factorType, eval);
  1077. preproc_consume(_RKLAMMER);
  1078. end
  1079. else if current_scanner.preproc_token = _LECKKLAMMER then
  1080. begin
  1081. preproc_consume(_LECKKLAMMER);
  1082. read_factor := ',';
  1083. while current_scanner.preproc_token = _ID do
  1084. begin
  1085. read_factor := read_factor+read_factor(setElemType, eval)+',';
  1086. if current_scanner.preproc_token = _COMMA then
  1087. preproc_consume(_COMMA);
  1088. end;
  1089. // TODO Add check of setElemType
  1090. preproc_consume(_RECKKLAMMER);
  1091. factorType:= [ctetSet];
  1092. end
  1093. else
  1094. Message(scan_e_error_in_preproc_expr);
  1095. end;
  1096. function read_term(var termType: TCTETypeSet; eval : Boolean) : string;
  1097. var
  1098. hs1,hs2 : string;
  1099. l1,l2 : longint;
  1100. w : integer;
  1101. termType2: TCTETypeSet;
  1102. begin
  1103. hs1:=read_factor(termType, eval);
  1104. repeat
  1105. if (current_scanner.preproc_token<>_ID) then
  1106. break;
  1107. if current_scanner.preproc_pattern<>'AND' then
  1108. break;
  1109. val(hs1,l1,w);
  1110. if l1=0 then
  1111. eval:= false; {Short circuit evaluation of OR}
  1112. if eval then
  1113. begin
  1114. {Check if first expr is boolean. Must be done here, after we know
  1115. it is an AND expression.}
  1116. if not (ctetBoolean in termType) then
  1117. CTEError(termType, [ctetBoolean], 'AND');
  1118. termType:= [ctetBoolean];
  1119. end;
  1120. preproc_consume(_ID);
  1121. hs2:=read_factor(termType2, eval);
  1122. if eval then
  1123. begin
  1124. if not (ctetBoolean in termType2) then
  1125. CTEError(termType2, [ctetBoolean], 'AND');
  1126. val(hs2,l2,w);
  1127. if (l1<>0) and (l2<>0) then
  1128. hs1:='1'
  1129. else
  1130. hs1:='0';
  1131. end;
  1132. until false;
  1133. read_term:=hs1;
  1134. end;
  1135. function read_simple_expr(var simpleExprType: TCTETypeSet; eval : Boolean) : string;
  1136. var
  1137. hs1,hs2 : string;
  1138. l1,l2 : longint;
  1139. w : integer;
  1140. simpleExprType2: TCTETypeSet;
  1141. begin
  1142. hs1:=read_term(simpleExprType, eval);
  1143. repeat
  1144. if (current_scanner.preproc_token<>_ID) then
  1145. break;
  1146. if current_scanner.preproc_pattern<>'OR' then
  1147. break;
  1148. val(hs1,l1,w);
  1149. if l1<>0 then
  1150. eval:= false; {Short circuit evaluation of OR}
  1151. if eval then
  1152. begin
  1153. {Check if first expr is boolean. Must be done here, after we know
  1154. it is an OR expression.}
  1155. if not (ctetBoolean in simpleExprType) then
  1156. CTEError(simpleExprType, [ctetBoolean], 'OR');
  1157. simpleExprType:= [ctetBoolean];
  1158. end;
  1159. preproc_consume(_ID);
  1160. hs2:=read_term(simpleExprType2, eval);
  1161. if eval then
  1162. begin
  1163. if not (ctetBoolean in simpleExprType2) then
  1164. CTEError(simpleExprType2, [ctetBoolean], 'OR');
  1165. val(hs2,l2,w);
  1166. if (l1<>0) or (l2<>0) then
  1167. hs1:='1'
  1168. else
  1169. hs1:='0';
  1170. end;
  1171. until false;
  1172. read_simple_expr:=hs1;
  1173. end;
  1174. function read_expr(var exprType: TCTETypeSet; eval : Boolean) : string;
  1175. var
  1176. hs1,hs2 : string;
  1177. b : boolean;
  1178. op : ttoken;
  1179. w : integer;
  1180. l1,l2 : longint;
  1181. exprType2: TCTETypeSet;
  1182. begin
  1183. hs1:=read_simple_expr(exprType, eval);
  1184. op:=current_scanner.preproc_token;
  1185. if (op = _ID) and (current_scanner.preproc_pattern = 'IN') then
  1186. op := _IN;
  1187. if not (op in [_IN,_EQ,_NE,_LT,_GT,_LTE,_GTE]) then
  1188. begin
  1189. read_expr:=hs1;
  1190. exit;
  1191. end;
  1192. if (op = _IN) then
  1193. preproc_consume(_ID)
  1194. else
  1195. preproc_consume(op);
  1196. hs2:=read_simple_expr(exprType2, eval);
  1197. if eval then
  1198. begin
  1199. if op = _IN then
  1200. begin
  1201. if exprType2 <> [ctetSet] then
  1202. CTEError(exprType2, [ctetSet], 'IN');
  1203. if exprType = [ctetSet] then
  1204. CTEError(exprType, setelementdefs, 'IN');
  1205. if is_number(hs1) and is_number(hs2) then
  1206. Message(scan_e_preproc_syntax_error)
  1207. else if hs2[1] = ',' then
  1208. b:=pos(','+hs1+',', hs2) > 0 { TODO For integer sets, perhaps check for numeric equivalence so that 0 = 00 }
  1209. else
  1210. Message(scan_e_preproc_syntax_error);
  1211. end
  1212. else
  1213. begin
  1214. if (exprType * exprType2) = [] then
  1215. CTEError(exprType2, exprType, '"'+hs1+' '+tokeninfo^[op].str+' '+hs2+'"');
  1216. if is_number(hs1) and is_number(hs2) then
  1217. begin
  1218. val(hs1,l1,w);
  1219. val(hs2,l2,w);
  1220. case op of
  1221. _EQ :
  1222. b:=l1=l2;
  1223. _NE :
  1224. b:=l1<>l2;
  1225. _LT :
  1226. b:=l1<l2;
  1227. _GT :
  1228. b:=l1>l2;
  1229. _GTE :
  1230. b:=l1>=l2;
  1231. _LTE :
  1232. b:=l1<=l2;
  1233. end;
  1234. end
  1235. else
  1236. begin
  1237. case op of
  1238. _EQ:
  1239. b:=hs1=hs2;
  1240. _NE :
  1241. b:=hs1<>hs2;
  1242. _LT :
  1243. b:=hs1<hs2;
  1244. _GT :
  1245. b:=hs1>hs2;
  1246. _GTE :
  1247. b:=hs1>=hs2;
  1248. _LTE :
  1249. b:=hs1<=hs2;
  1250. end;
  1251. end;
  1252. end;
  1253. end
  1254. else
  1255. b:= false; {Just to have something}
  1256. if b then
  1257. read_expr:='1'
  1258. else
  1259. read_expr:='0';
  1260. exprType:= [ctetBoolean];
  1261. end;
  1262. begin
  1263. current_scanner.skipspace;
  1264. { start preproc expression scanner }
  1265. current_scanner.preproc_token:=current_scanner.readpreproc;
  1266. parse_compiler_expr:=read_expr(compileExprType, true);
  1267. end;
  1268. function boolean_compile_time_expr(var valuedescr: String): Boolean;
  1269. var
  1270. hs : string;
  1271. exprType: TCTETypeSet;
  1272. begin
  1273. hs:=parse_compiler_expr(exprType);
  1274. if (exprType * [ctetBoolean]) = [] then
  1275. CTEError(exprType, [ctetBoolean], 'IF or ELSEIF');
  1276. boolean_compile_time_expr:= hs <> '0';
  1277. valuedescr:= hs;
  1278. end;
  1279. procedure dir_if;
  1280. begin
  1281. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  1282. end;
  1283. procedure dir_elseif;
  1284. begin
  1285. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  1286. end;
  1287. procedure dir_define_impl(macstyle: boolean);
  1288. var
  1289. hs : string;
  1290. bracketcount : longint;
  1291. mac : tmacro;
  1292. macropos : longint;
  1293. macrobuffer : pmacrobuffer;
  1294. begin
  1295. current_scanner.skipspace;
  1296. hs:=current_scanner.readid;
  1297. mac:=tmacro(search_macro(hs));
  1298. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  1299. begin
  1300. mac:=tmacro.create(hs);
  1301. mac.defined:=true;
  1302. current_module.localmacrosymtable.insert(mac);
  1303. end
  1304. else
  1305. begin
  1306. mac.defined:=true;
  1307. mac.is_compiler_var:=false;
  1308. { delete old definition }
  1309. if assigned(mac.buftext) then
  1310. begin
  1311. freemem(mac.buftext,mac.buflen);
  1312. mac.buftext:=nil;
  1313. end;
  1314. end;
  1315. Message1(parser_c_macro_defined,mac.name);
  1316. mac.is_used:=true;
  1317. if (cs_support_macro in current_settings.moduleswitches) then
  1318. begin
  1319. current_scanner.skipspace;
  1320. if not macstyle then
  1321. begin
  1322. { may be a macro? }
  1323. if c <> ':' then
  1324. exit;
  1325. current_scanner.readchar;
  1326. if c <> '=' then
  1327. exit;
  1328. current_scanner.readchar;
  1329. current_scanner.skipspace;
  1330. end;
  1331. { key words are never substituted }
  1332. if is_keyword(hs) then
  1333. Message(scan_e_keyword_cant_be_a_macro);
  1334. new(macrobuffer);
  1335. macropos:=0;
  1336. { parse macro, brackets are counted so it's possible
  1337. to have a $ifdef etc. in the macro }
  1338. bracketcount:=0;
  1339. repeat
  1340. case c of
  1341. '}' :
  1342. if (bracketcount=0) then
  1343. break
  1344. else
  1345. dec(bracketcount);
  1346. '{' :
  1347. inc(bracketcount);
  1348. #10,#13 :
  1349. current_scanner.linebreak;
  1350. #26 :
  1351. current_scanner.end_of_file;
  1352. end;
  1353. macrobuffer^[macropos]:=c;
  1354. inc(macropos);
  1355. if macropos>=maxmacrolen then
  1356. Message(scan_f_macro_buffer_overflow);
  1357. current_scanner.readchar;
  1358. until false;
  1359. { free buffer of macro ?}
  1360. if assigned(mac.buftext) then
  1361. freemem(mac.buftext,mac.buflen);
  1362. { get new mem }
  1363. getmem(mac.buftext,macropos);
  1364. mac.buflen:=macropos;
  1365. { copy the text }
  1366. move(macrobuffer^,mac.buftext^,macropos);
  1367. dispose(macrobuffer);
  1368. end
  1369. else
  1370. begin
  1371. { check if there is an assignment, then we need to give a
  1372. warning }
  1373. current_scanner.skipspace;
  1374. if c=':' then
  1375. begin
  1376. current_scanner.readchar;
  1377. if c='=' then
  1378. Message(scan_w_macro_support_turned_off);
  1379. end;
  1380. end;
  1381. end;
  1382. procedure dir_define;
  1383. begin
  1384. dir_define_impl(false);
  1385. end;
  1386. procedure dir_definec;
  1387. begin
  1388. dir_define_impl(true);
  1389. end;
  1390. procedure dir_setc;
  1391. var
  1392. hs : string;
  1393. mac : tmacro;
  1394. exprType: TCTETypeSet;
  1395. l : longint;
  1396. w : integer;
  1397. begin
  1398. current_scanner.skipspace;
  1399. hs:=current_scanner.readid;
  1400. mac:=tmacro(search_macro(hs));
  1401. if not assigned(mac) or
  1402. (mac.owner <> current_module.localmacrosymtable) then
  1403. begin
  1404. mac:=tmacro.create(hs);
  1405. mac.defined:=true;
  1406. mac.is_compiler_var:=true;
  1407. current_module.localmacrosymtable.insert(mac);
  1408. end
  1409. else
  1410. begin
  1411. mac.defined:=true;
  1412. mac.is_compiler_var:=true;
  1413. { delete old definition }
  1414. if assigned(mac.buftext) then
  1415. begin
  1416. freemem(mac.buftext,mac.buflen);
  1417. mac.buftext:=nil;
  1418. end;
  1419. end;
  1420. Message1(parser_c_macro_defined,mac.name);
  1421. mac.is_used:=true;
  1422. { key words are never substituted }
  1423. if is_keyword(hs) then
  1424. Message(scan_e_keyword_cant_be_a_macro);
  1425. { macro assignment can be both := and = }
  1426. current_scanner.skipspace;
  1427. if c=':' then
  1428. current_scanner.readchar;
  1429. if c='=' then
  1430. begin
  1431. current_scanner.readchar;
  1432. hs:= parse_compiler_expr(exprType);
  1433. if (exprType * [ctetBoolean, ctetInteger]) = [] then
  1434. CTEError(exprType, [ctetBoolean, ctetInteger], 'SETC');
  1435. if length(hs) <> 0 then
  1436. begin
  1437. {If we are absolutely shure it is boolean, translate
  1438. to TRUE/FALSE to increase possibility to do future type check}
  1439. if exprType = [ctetBoolean] then
  1440. begin
  1441. val(hs,l,w);
  1442. if l<>0 then
  1443. hs:='TRUE'
  1444. else
  1445. hs:='FALSE';
  1446. end;
  1447. Message2(parser_c_macro_set_to,mac.name,hs);
  1448. { free buffer of macro ?}
  1449. if assigned(mac.buftext) then
  1450. freemem(mac.buftext,mac.buflen);
  1451. { get new mem }
  1452. getmem(mac.buftext,length(hs));
  1453. mac.buflen:=length(hs);
  1454. { copy the text }
  1455. move(hs[1],mac.buftext^,mac.buflen);
  1456. end
  1457. else
  1458. Message(scan_e_preproc_syntax_error);
  1459. end
  1460. else
  1461. Message(scan_e_preproc_syntax_error);
  1462. end;
  1463. procedure dir_undef;
  1464. var
  1465. hs : string;
  1466. mac : tmacro;
  1467. begin
  1468. current_scanner.skipspace;
  1469. hs:=current_scanner.readid;
  1470. mac:=tmacro(search_macro(hs));
  1471. if not assigned(mac) or
  1472. (mac.owner <> current_module.localmacrosymtable) then
  1473. begin
  1474. mac:=tmacro.create(hs);
  1475. mac.defined:=false;
  1476. current_module.localmacrosymtable.insert(mac);
  1477. end
  1478. else
  1479. begin
  1480. mac.defined:=false;
  1481. mac.is_compiler_var:=false;
  1482. { delete old definition }
  1483. if assigned(mac.buftext) then
  1484. begin
  1485. freemem(mac.buftext,mac.buflen);
  1486. mac.buftext:=nil;
  1487. end;
  1488. end;
  1489. Message1(parser_c_macro_undefined,mac.name);
  1490. mac.is_used:=true;
  1491. end;
  1492. procedure dir_include;
  1493. function findincludefile(const path,name:TCmdStr;var foundfile:TCmdStr):boolean;
  1494. var
  1495. found : boolean;
  1496. hpath : TCmdStr;
  1497. begin
  1498. (* look for the include file
  1499. If path was absolute and specified as part of {$I } then
  1500. 1. specified path
  1501. else
  1502. 1. path of current inputfile,current dir
  1503. 2. local includepath
  1504. 3. global includepath
  1505. -- Check mantis #13461 before changing this *)
  1506. found:=false;
  1507. foundfile:='';
  1508. hpath:='';
  1509. if path_absolute(path) then
  1510. begin
  1511. found:=FindFile(name,path,true,foundfile);
  1512. end
  1513. else
  1514. begin
  1515. hpath:=current_scanner.inputfile.path^+';'+CurDirRelPath(source_info);
  1516. found:=FindFile(path+name, hpath,true,foundfile);
  1517. if not found then
  1518. found:=current_module.localincludesearchpath.FindFile(path+name,true,foundfile);
  1519. if not found then
  1520. found:=includesearchpath.FindFile(path+name,true,foundfile);
  1521. end;
  1522. result:=found;
  1523. end;
  1524. var
  1525. foundfile : TCmdStr;
  1526. path,
  1527. name,
  1528. hs : tpathstr;
  1529. args : string;
  1530. hp : tinputfile;
  1531. found : boolean;
  1532. begin
  1533. current_scanner.skipspace;
  1534. args:=current_scanner.readcomment;
  1535. hs:=GetToken(args,' ');
  1536. if hs='' then
  1537. exit;
  1538. if (hs[1]='%') then
  1539. begin
  1540. { case insensitive }
  1541. hs:=upper(hs);
  1542. { remove %'s }
  1543. Delete(hs,1,1);
  1544. if hs[length(hs)]='%' then
  1545. Delete(hs,length(hs),1);
  1546. { save old }
  1547. path:=hs;
  1548. { first check for internal macros }
  1549. if hs='TIME' then
  1550. hs:=gettimestr
  1551. else
  1552. if hs='DATE' then
  1553. hs:=getdatestr
  1554. else
  1555. if hs='FILE' then
  1556. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex)
  1557. else
  1558. if hs='LINE' then
  1559. hs:=tostr(current_filepos.line)
  1560. else
  1561. if hs='FPCVERSION' then
  1562. hs:=version_string
  1563. else
  1564. if hs='FPCDATE' then
  1565. hs:=date_string
  1566. else
  1567. if hs='FPCTARGET' then
  1568. hs:=target_cpu_string
  1569. else
  1570. if hs='FPCTARGETCPU' then
  1571. hs:=target_cpu_string
  1572. else
  1573. if hs='FPCTARGETOS' then
  1574. hs:=target_info.shortname
  1575. else
  1576. hs:=GetEnvironmentVariable(hs);
  1577. if hs='' then
  1578. Message1(scan_w_include_env_not_found,path);
  1579. { make it a stringconst }
  1580. hs:=''''+hs+'''';
  1581. current_scanner.substitutemacro(path,@hs[1],length(hs),
  1582. current_scanner.line_no,current_scanner.inputfile.ref_index);
  1583. end
  1584. else
  1585. begin
  1586. hs:=FixFileName(hs);
  1587. path:=ExtractFilePath(hs);
  1588. name:=ExtractFileName(hs);
  1589. { Special case for Delphi compatibility: '*' has to be replaced
  1590. by the file name of the current source file. }
  1591. if (length(name)>=1) and
  1592. (name[1]='*') then
  1593. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  1594. { try to find the file }
  1595. found:=findincludefile(path,name,foundfile);
  1596. if (ExtractFileExt(name)='') then
  1597. begin
  1598. { try default extensions .inc , .pp and .pas }
  1599. if (not found) then
  1600. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  1601. if (not found) then
  1602. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  1603. if (not found) then
  1604. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  1605. end;
  1606. if current_scanner.inputfilecount<max_include_nesting then
  1607. begin
  1608. inc(current_scanner.inputfilecount);
  1609. { we need to reread the current char }
  1610. dec(current_scanner.inputpointer);
  1611. { shutdown current file }
  1612. current_scanner.tempcloseinputfile;
  1613. { load new file }
  1614. hp:=do_openinputfile(foundfile);
  1615. current_scanner.addfile(hp);
  1616. current_module.sourcefiles.register_file(hp);
  1617. if (not found) then
  1618. Message1(scan_f_cannot_open_includefile,hs);
  1619. if (not current_scanner.openinputfile) then
  1620. Message1(scan_f_cannot_open_includefile,hs);
  1621. Message1(scan_t_start_include_file,current_scanner.inputfile.path^+current_scanner.inputfile.name^);
  1622. current_scanner.reload;
  1623. end
  1624. else
  1625. Message(scan_f_include_deep_ten);
  1626. end;
  1627. end;
  1628. {*****************************************************************************
  1629. Preprocessor writing
  1630. *****************************************************************************}
  1631. {$ifdef PREPROCWRITE}
  1632. constructor tpreprocfile.create(const fn:string);
  1633. begin
  1634. { open outputfile }
  1635. assign(f,fn);
  1636. {$I-}
  1637. rewrite(f);
  1638. {$I+}
  1639. if ioresult<>0 then
  1640. Comment(V_Fatal,'can''t create file '+fn);
  1641. getmem(buf,preprocbufsize);
  1642. settextbuf(f,buf^,preprocbufsize);
  1643. { reset }
  1644. eolfound:=false;
  1645. spacefound:=false;
  1646. end;
  1647. destructor tpreprocfile.destroy;
  1648. begin
  1649. close(f);
  1650. freemem(buf,preprocbufsize);
  1651. end;
  1652. procedure tpreprocfile.add(const s:string);
  1653. begin
  1654. write(f,s);
  1655. end;
  1656. procedure tpreprocfile.addspace;
  1657. begin
  1658. if eolfound then
  1659. begin
  1660. writeln(f,'');
  1661. eolfound:=false;
  1662. spacefound:=false;
  1663. end
  1664. else
  1665. if spacefound then
  1666. begin
  1667. write(f,' ');
  1668. spacefound:=false;
  1669. end;
  1670. end;
  1671. {$endif PREPROCWRITE}
  1672. {*****************************************************************************
  1673. TPreProcStack
  1674. *****************************************************************************}
  1675. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  1676. begin
  1677. accept:=a;
  1678. typ:=atyp;
  1679. next:=n;
  1680. end;
  1681. {*****************************************************************************
  1682. TReplayStack
  1683. *****************************************************************************}
  1684. constructor treplaystack.Create(atoken:ttoken;asettings:tsettings;atokenbuf:tdynamicarray;anext:treplaystack);
  1685. begin
  1686. token:=atoken;
  1687. settings:=asettings;
  1688. tokenbuf:=atokenbuf;
  1689. next:=anext;
  1690. end;
  1691. {*****************************************************************************
  1692. TDirectiveItem
  1693. *****************************************************************************}
  1694. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  1695. begin
  1696. inherited Create(AList,n);
  1697. is_conditional:=false;
  1698. proc:=p;
  1699. end;
  1700. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  1701. begin
  1702. inherited Create(AList,n);
  1703. is_conditional:=true;
  1704. proc:=p;
  1705. end;
  1706. {****************************************************************************
  1707. TSCANNERFILE
  1708. ****************************************************************************}
  1709. constructor tscannerfile.create(const fn:string; is_macro: boolean = false);
  1710. begin
  1711. inputfile:=do_openinputfile(fn);
  1712. if is_macro then
  1713. inputfile.is_macro:=true;
  1714. if assigned(current_module) then
  1715. current_module.sourcefiles.register_file(inputfile);
  1716. { reset localinput }
  1717. c:=#0;
  1718. inputbuffer:=nil;
  1719. inputpointer:=nil;
  1720. inputstart:=0;
  1721. { reset scanner }
  1722. preprocstack:=nil;
  1723. replaystack:=nil;
  1724. comment_level:=0;
  1725. yylexcount:=0;
  1726. block_type:=bt_general;
  1727. line_no:=0;
  1728. lastlinepos:=0;
  1729. lasttokenpos:=0;
  1730. nexttokenpos:=0;
  1731. lasttoken:=NOTOKEN;
  1732. nexttoken:=NOTOKEN;
  1733. lastasmgetchar:=#0;
  1734. ignoredirectives:=TFPHashList.Create;
  1735. in_asm_string:=false;
  1736. end;
  1737. procedure tscannerfile.firstfile;
  1738. begin
  1739. { load block }
  1740. if not openinputfile then
  1741. Message1(scan_f_cannot_open_input,inputfile.name^);
  1742. reload;
  1743. end;
  1744. destructor tscannerfile.destroy;
  1745. begin
  1746. if assigned(current_module) and
  1747. (current_module.state=ms_compiled) and
  1748. (status.errorcount=0) then
  1749. checkpreprocstack
  1750. else
  1751. begin
  1752. while assigned(preprocstack) do
  1753. poppreprocstack;
  1754. end;
  1755. while assigned(replaystack) do
  1756. popreplaystack;
  1757. if not inputfile.closed then
  1758. closeinputfile;
  1759. if inputfile.is_macro then
  1760. inputfile.free;
  1761. ignoredirectives.free;
  1762. end;
  1763. function tscannerfile.openinputfile:boolean;
  1764. begin
  1765. openinputfile:=inputfile.open;
  1766. { load buffer }
  1767. inputbuffer:=inputfile.buf;
  1768. inputpointer:=inputfile.buf;
  1769. inputstart:=inputfile.bufstart;
  1770. { line }
  1771. line_no:=0;
  1772. lastlinepos:=0;
  1773. lasttokenpos:=0;
  1774. nexttokenpos:=0;
  1775. end;
  1776. procedure tscannerfile.closeinputfile;
  1777. begin
  1778. inputfile.close;
  1779. { reset buffer }
  1780. inputbuffer:=nil;
  1781. inputpointer:=nil;
  1782. inputstart:=0;
  1783. { reset line }
  1784. line_no:=0;
  1785. lastlinepos:=0;
  1786. lasttokenpos:=0;
  1787. nexttokenpos:=0;
  1788. end;
  1789. function tscannerfile.tempopeninputfile:boolean;
  1790. begin
  1791. if inputfile.is_macro then
  1792. exit;
  1793. tempopeninputfile:=inputfile.tempopen;
  1794. { reload buffer }
  1795. inputbuffer:=inputfile.buf;
  1796. inputpointer:=inputfile.buf;
  1797. inputstart:=inputfile.bufstart;
  1798. end;
  1799. procedure tscannerfile.tempcloseinputfile;
  1800. begin
  1801. if inputfile.closed or inputfile.is_macro then
  1802. exit;
  1803. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  1804. inputfile.tempclose;
  1805. { reset buffer }
  1806. inputbuffer:=nil;
  1807. inputpointer:=nil;
  1808. inputstart:=0;
  1809. end;
  1810. procedure tscannerfile.saveinputfile;
  1811. begin
  1812. inputfile.saveinputpointer:=inputpointer;
  1813. inputfile.savelastlinepos:=lastlinepos;
  1814. inputfile.saveline_no:=line_no;
  1815. end;
  1816. procedure tscannerfile.restoreinputfile;
  1817. begin
  1818. inputbuffer:=inputfile.buf;
  1819. inputpointer:=inputfile.saveinputpointer;
  1820. lastlinepos:=inputfile.savelastlinepos;
  1821. line_no:=inputfile.saveline_no;
  1822. if not inputfile.is_macro then
  1823. parser_current_file:=inputfile.name^;
  1824. end;
  1825. procedure tscannerfile.nextfile;
  1826. var
  1827. to_dispose : tinputfile;
  1828. begin
  1829. if assigned(inputfile.next) then
  1830. begin
  1831. if inputfile.is_macro then
  1832. to_dispose:=inputfile
  1833. else
  1834. begin
  1835. to_dispose:=nil;
  1836. dec(inputfilecount);
  1837. end;
  1838. { we can allways close the file, no ? }
  1839. inputfile.close;
  1840. inputfile:=inputfile.next;
  1841. if assigned(to_dispose) then
  1842. to_dispose.free;
  1843. restoreinputfile;
  1844. end;
  1845. end;
  1846. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  1847. begin
  1848. if not assigned(buf) then
  1849. internalerror(200511172);
  1850. if assigned(recordtokenbuf) then
  1851. internalerror(200511173);
  1852. recordtokenbuf:=buf;
  1853. fillchar(last_settings,sizeof(last_settings),0);
  1854. fillchar(last_filepos,sizeof(last_filepos),0);
  1855. end;
  1856. procedure tscannerfile.stoprecordtokens;
  1857. begin
  1858. if not assigned(recordtokenbuf) then
  1859. internalerror(200511174);
  1860. recordtokenbuf:=nil;
  1861. end;
  1862. procedure tscannerfile.writetoken(t : ttoken);
  1863. var
  1864. b : byte;
  1865. begin
  1866. if ord(t)>$7f then
  1867. begin
  1868. b:=(ord(t) shr 8) or $80;
  1869. recordtokenbuf.write(b,1);
  1870. end;
  1871. b:=ord(t) and $ff;
  1872. recordtokenbuf.write(b,1);
  1873. end;
  1874. procedure tscannerfile.recordtoken;
  1875. var
  1876. t : ttoken;
  1877. s : tspecialgenerictoken;
  1878. len : sizeint;
  1879. b : byte;
  1880. begin
  1881. if not assigned(recordtokenbuf) then
  1882. internalerror(200511176);
  1883. t:=_GENERICSPECIALTOKEN;
  1884. { settings changed? }
  1885. if CompareByte(current_settings,last_settings,sizeof(current_settings))<>0 then
  1886. begin
  1887. { use a special token to record it }
  1888. s:=ST_LOADSETTINGS;
  1889. writetoken(t);
  1890. recordtokenbuf.write(s,1);
  1891. recordtokenbuf.write(current_settings,sizeof(current_settings));
  1892. last_settings:=current_settings;
  1893. end;
  1894. { file pos changes? }
  1895. if current_tokenpos.line<>last_filepos.line then
  1896. begin
  1897. s:=ST_LINE;
  1898. writetoken(t);
  1899. recordtokenbuf.write(s,1);
  1900. recordtokenbuf.write(current_tokenpos.line,sizeof(current_tokenpos.line));
  1901. last_filepos.line:=current_tokenpos.line;
  1902. end;
  1903. if current_tokenpos.column<>last_filepos.column then
  1904. begin
  1905. s:=ST_COLUMN;
  1906. writetoken(t);
  1907. { can the column be written packed? }
  1908. if current_tokenpos.column<$80 then
  1909. begin
  1910. b:=$80 or current_tokenpos.column;
  1911. recordtokenbuf.write(b,1);
  1912. end
  1913. else
  1914. begin
  1915. recordtokenbuf.write(s,1);
  1916. recordtokenbuf.write(current_tokenpos.column,sizeof(current_tokenpos.column));
  1917. end;
  1918. last_filepos.column:=current_tokenpos.column;
  1919. end;
  1920. if current_tokenpos.fileindex<>last_filepos.fileindex then
  1921. begin
  1922. s:=ST_FILEINDEX;
  1923. writetoken(t);
  1924. recordtokenbuf.write(s,1);
  1925. recordtokenbuf.write(current_tokenpos.fileindex,sizeof(current_tokenpos.fileindex));
  1926. last_filepos.fileindex:=current_tokenpos.fileindex;
  1927. end;
  1928. writetoken(token);
  1929. if token<>_GENERICSPECIALTOKEN then
  1930. writetoken(idtoken);
  1931. case token of
  1932. _CWCHAR,
  1933. _CWSTRING :
  1934. begin
  1935. recordtokenbuf.write(patternw^.len,sizeof(sizeint));
  1936. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  1937. end;
  1938. _CSTRING:
  1939. begin
  1940. len:=length(cstringpattern);
  1941. recordtokenbuf.write(len,sizeof(sizeint));
  1942. recordtokenbuf.write(cstringpattern[1],length(cstringpattern));
  1943. end;
  1944. _CCHAR,
  1945. _INTCONST,
  1946. _REALNUMBER :
  1947. begin
  1948. { pexpr.pas messes with pattern in case of negative integer consts,
  1949. see around line 2562 the comment of JM; remove the - before recording it
  1950. (FK)
  1951. }
  1952. if (token=_INTCONST) and (pattern[1]='-') then
  1953. delete(pattern,1,1);
  1954. recordtokenbuf.write(pattern[0],1);
  1955. recordtokenbuf.write(pattern[1],length(pattern));
  1956. end;
  1957. _ID :
  1958. begin
  1959. recordtokenbuf.write(orgpattern[0],1);
  1960. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  1961. end;
  1962. end;
  1963. end;
  1964. procedure tscannerfile.startreplaytokens(buf:tdynamicarray);
  1965. begin
  1966. if not assigned(buf) then
  1967. internalerror(200511175);
  1968. { save current token }
  1969. if token in [_CWCHAR,_CWSTRING,_CCHAR,_CSTRING,_INTCONST,_REALNUMBER,_ID] then
  1970. internalerror(200511178);
  1971. replaystack:=treplaystack.create(token,current_settings,replaytokenbuf,replaystack);
  1972. if assigned(inputpointer) then
  1973. dec(inputpointer);
  1974. { install buffer }
  1975. replaytokenbuf:=buf;
  1976. { reload next token }
  1977. replaytokenbuf.seek(0);
  1978. replaytoken;
  1979. end;
  1980. function tscannerfile.readtoken: ttoken;
  1981. var
  1982. b,b2 : byte;
  1983. begin
  1984. replaytokenbuf.read(b,1);
  1985. if (b and $80)<>0 then
  1986. begin
  1987. replaytokenbuf.read(b2,1);
  1988. result:=ttoken(((b and $7f) shl 8) or b2);
  1989. end
  1990. else
  1991. result:=ttoken(b);
  1992. end;
  1993. procedure tscannerfile.replaytoken;
  1994. var
  1995. wlen : sizeint;
  1996. specialtoken : tspecialgenerictoken;
  1997. begin
  1998. if not assigned(replaytokenbuf) then
  1999. internalerror(200511177);
  2000. { End of replay buffer? Then load the next char from the file again }
  2001. if replaytokenbuf.pos>=replaytokenbuf.size then
  2002. begin
  2003. token:=replaystack.token;
  2004. replaytokenbuf:=replaystack.tokenbuf;
  2005. { restore compiler settings }
  2006. current_settings:=replaystack.settings;
  2007. popreplaystack;
  2008. if assigned(inputpointer) then
  2009. begin
  2010. c:=inputpointer^;
  2011. inc(inputpointer);
  2012. end;
  2013. exit;
  2014. end;
  2015. repeat
  2016. { load token from the buffer }
  2017. token:=readtoken;
  2018. if token<>_GENERICSPECIALTOKEN then
  2019. idtoken:=readtoken
  2020. else
  2021. idtoken:=_NOID;
  2022. case token of
  2023. _CWCHAR,
  2024. _CWSTRING :
  2025. begin
  2026. replaytokenbuf.read(wlen,sizeof(SizeInt));
  2027. setlengthwidestring(patternw,wlen);
  2028. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2029. orgpattern:='';
  2030. pattern:='';
  2031. cstringpattern:='';
  2032. end;
  2033. _CSTRING:
  2034. begin
  2035. replaytokenbuf.read(wlen,sizeof(sizeint));
  2036. setlength(cstringpattern,wlen);
  2037. replaytokenbuf.read(cstringpattern[1],wlen);
  2038. orgpattern:='';
  2039. pattern:='';
  2040. end;
  2041. _CCHAR,
  2042. _INTCONST,
  2043. _REALNUMBER :
  2044. begin
  2045. replaytokenbuf.read(pattern[0],1);
  2046. replaytokenbuf.read(pattern[1],length(pattern));
  2047. orgpattern:='';
  2048. end;
  2049. _ID :
  2050. begin
  2051. replaytokenbuf.read(orgpattern[0],1);
  2052. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  2053. pattern:=upper(orgpattern);
  2054. end;
  2055. _GENERICSPECIALTOKEN:
  2056. begin
  2057. replaytokenbuf.read(specialtoken,1);
  2058. { packed column? }
  2059. if (ord(specialtoken) and $80)<>0 then
  2060. begin
  2061. current_tokenpos.column:=ord(specialtoken) and $7f;
  2062. { don't generate invalid line info if no sources are available for the current module }
  2063. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2064. current_tokenpos.column:=0;
  2065. current_filepos:=current_tokenpos;
  2066. end
  2067. else
  2068. case specialtoken of
  2069. ST_LOADSETTINGS:
  2070. replaytokenbuf.read(current_settings,sizeof(current_settings));
  2071. ST_LINE:
  2072. begin
  2073. replaytokenbuf.read(current_tokenpos.line,sizeof(current_tokenpos.line));
  2074. { don't generate invalid line info if no sources are available for the current module }
  2075. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2076. current_tokenpos.line:=0;
  2077. current_filepos:=current_tokenpos;
  2078. end;
  2079. ST_COLUMN:
  2080. begin
  2081. replaytokenbuf.read(current_tokenpos.column,sizeof(current_tokenpos.column));
  2082. { don't generate invalid line info if no sources are available for the current module }
  2083. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2084. current_tokenpos.column:=0;
  2085. current_filepos:=current_tokenpos;
  2086. end;
  2087. ST_FILEINDEX:
  2088. begin
  2089. replaytokenbuf.read(current_tokenpos.fileindex,sizeof(current_tokenpos.fileindex));
  2090. { don't generate invalid line info if no sources are available for the current module }
  2091. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2092. begin
  2093. current_tokenpos.column:=0;
  2094. current_tokenpos.line:=0;
  2095. end;
  2096. current_filepos:=current_tokenpos;
  2097. end;
  2098. else
  2099. internalerror(2006103010);
  2100. end;
  2101. continue;
  2102. end;
  2103. end;
  2104. break;
  2105. until false;
  2106. end;
  2107. procedure tscannerfile.addfile(hp:tinputfile);
  2108. begin
  2109. saveinputfile;
  2110. { add to list }
  2111. hp.next:=inputfile;
  2112. inputfile:=hp;
  2113. { load new inputfile }
  2114. restoreinputfile;
  2115. end;
  2116. procedure tscannerfile.reload;
  2117. begin
  2118. with inputfile do
  2119. begin
  2120. { when nothing more to read then leave immediatly, so we
  2121. don't change the current_filepos and leave it point to the last
  2122. char }
  2123. if (c=#26) and (not assigned(next)) then
  2124. exit;
  2125. repeat
  2126. { still more to read?, then change the #0 to a space so its seen
  2127. as a seperator, this can't be used for macro's which can change
  2128. the place of the #0 in the buffer with tempopen }
  2129. if (c=#0) and (bufsize>0) and
  2130. not(inputfile.is_macro) and
  2131. (inputpointer-inputbuffer<bufsize) then
  2132. begin
  2133. c:=' ';
  2134. inc(inputpointer);
  2135. exit;
  2136. end;
  2137. { can we read more from this file ? }
  2138. if (c<>#26) and (not endoffile) then
  2139. begin
  2140. readbuf;
  2141. inputpointer:=buf;
  2142. inputbuffer:=buf;
  2143. inputstart:=bufstart;
  2144. { first line? }
  2145. if line_no=0 then
  2146. begin
  2147. c:=inputpointer^;
  2148. { eat utf-8 signature? }
  2149. if (ord(inputpointer^)=$ef) and
  2150. (ord((inputpointer+1)^)=$bb) and
  2151. (ord((inputpointer+2)^)=$bf) then
  2152. begin
  2153. inc(inputpointer,3);
  2154. message(scan_c_switching_to_utf8);
  2155. current_settings.sourcecodepage:='utf8';
  2156. end;
  2157. line_no:=1;
  2158. if cs_asm_source in current_settings.globalswitches then
  2159. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  2160. end;
  2161. end
  2162. else
  2163. begin
  2164. { load eof position in tokenpos/current_filepos }
  2165. gettokenpos;
  2166. { close file }
  2167. closeinputfile;
  2168. { no next module, than EOF }
  2169. if not assigned(inputfile.next) then
  2170. begin
  2171. c:=#26;
  2172. exit;
  2173. end;
  2174. { load next file and reopen it }
  2175. nextfile;
  2176. tempopeninputfile;
  2177. { status }
  2178. Message1(scan_t_back_in,inputfile.name^);
  2179. end;
  2180. { load next char }
  2181. c:=inputpointer^;
  2182. inc(inputpointer);
  2183. until c<>#0; { if also end, then reload again }
  2184. end;
  2185. end;
  2186. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  2187. var
  2188. hp : tinputfile;
  2189. begin
  2190. { save old postion }
  2191. dec(inputpointer);
  2192. tempcloseinputfile;
  2193. { create macro 'file' }
  2194. { use special name to dispose after !! }
  2195. hp:=do_openinputfile('_Macro_.'+macname);
  2196. addfile(hp);
  2197. with inputfile do
  2198. begin
  2199. setmacro(p,len);
  2200. { local buffer }
  2201. inputbuffer:=buf;
  2202. inputpointer:=buf;
  2203. inputstart:=bufstart;
  2204. ref_index:=fileindex;
  2205. end;
  2206. { reset line }
  2207. line_no:=line;
  2208. lastlinepos:=0;
  2209. lasttokenpos:=0;
  2210. nexttokenpos:=0;
  2211. { load new c }
  2212. c:=inputpointer^;
  2213. inc(inputpointer);
  2214. end;
  2215. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  2216. begin
  2217. tokenpos:=inputstart+(inputpointer-inputbuffer);
  2218. filepos.line:=line_no;
  2219. filepos.column:=tokenpos-lastlinepos;
  2220. filepos.fileindex:=inputfile.ref_index;
  2221. filepos.moduleindex:=current_module.unit_index;
  2222. end;
  2223. procedure tscannerfile.gettokenpos;
  2224. { load the values of tokenpos and lasttokenpos }
  2225. begin
  2226. do_gettokenpos(lasttokenpos,current_tokenpos);
  2227. current_filepos:=current_tokenpos;
  2228. end;
  2229. procedure tscannerfile.cachenexttokenpos;
  2230. begin
  2231. do_gettokenpos(nexttokenpos,next_filepos);
  2232. end;
  2233. procedure tscannerfile.setnexttoken;
  2234. begin
  2235. token:=nexttoken;
  2236. nexttoken:=NOTOKEN;
  2237. lasttokenpos:=nexttokenpos;
  2238. current_tokenpos:=next_filepos;
  2239. current_filepos:=current_tokenpos;
  2240. nexttokenpos:=0;
  2241. end;
  2242. procedure tscannerfile.savetokenpos;
  2243. begin
  2244. oldlasttokenpos:=lasttokenpos;
  2245. oldcurrent_filepos:=current_filepos;
  2246. oldcurrent_tokenpos:=current_tokenpos;
  2247. end;
  2248. procedure tscannerfile.restoretokenpos;
  2249. begin
  2250. lasttokenpos:=oldlasttokenpos;
  2251. current_filepos:=oldcurrent_filepos;
  2252. current_tokenpos:=oldcurrent_tokenpos;
  2253. end;
  2254. procedure tscannerfile.inc_comment_level;
  2255. begin
  2256. if (m_nested_comment in current_settings.modeswitches) then
  2257. inc(comment_level)
  2258. else
  2259. comment_level:=1;
  2260. if (comment_level>1) then
  2261. begin
  2262. savetokenpos;
  2263. gettokenpos; { update for warning }
  2264. Message1(scan_w_comment_level,tostr(comment_level));
  2265. restoretokenpos;
  2266. end;
  2267. end;
  2268. procedure tscannerfile.dec_comment_level;
  2269. begin
  2270. if (m_nested_comment in current_settings.modeswitches) then
  2271. dec(comment_level)
  2272. else
  2273. comment_level:=0;
  2274. end;
  2275. procedure tscannerfile.linebreak;
  2276. var
  2277. cur : char;
  2278. begin
  2279. with inputfile do
  2280. begin
  2281. if (byte(inputpointer^)=0) and not(endoffile) then
  2282. begin
  2283. cur:=c;
  2284. reload;
  2285. if byte(cur)+byte(c)<>23 then
  2286. dec(inputpointer);
  2287. end
  2288. else
  2289. begin
  2290. { Support all combination of #10 and #13 as line break }
  2291. if (byte(inputpointer^)+byte(c)=23) then
  2292. inc(inputpointer);
  2293. end;
  2294. { Always return #10 as line break }
  2295. c:=#10;
  2296. { increase line counters }
  2297. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  2298. inc(line_no);
  2299. { update linebuffer }
  2300. if cs_asm_source in current_settings.globalswitches then
  2301. inputfile.setline(line_no,lastlinepos);
  2302. { update for status and call the show status routine,
  2303. but don't touch current_filepos ! }
  2304. savetokenpos;
  2305. gettokenpos; { update for v_status }
  2306. inc(status.compiledlines);
  2307. ShowStatus;
  2308. restoretokenpos;
  2309. end;
  2310. end;
  2311. procedure tscannerfile.illegal_char(c:char);
  2312. var
  2313. s : string;
  2314. begin
  2315. if c in [#32..#255] then
  2316. s:=''''+c+''''
  2317. else
  2318. s:='#'+tostr(ord(c));
  2319. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  2320. end;
  2321. procedure tscannerfile.end_of_file;
  2322. begin
  2323. checkpreprocstack;
  2324. Message(scan_f_end_of_file);
  2325. end;
  2326. {-------------------------------------------
  2327. IF Conditional Handling
  2328. -------------------------------------------}
  2329. procedure tscannerfile.checkpreprocstack;
  2330. begin
  2331. { check for missing ifdefs }
  2332. while assigned(preprocstack) do
  2333. begin
  2334. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  2335. preprocstack.owner.inputfile.name^,tostr(preprocstack.line_nb));
  2336. poppreprocstack;
  2337. end;
  2338. end;
  2339. procedure tscannerfile.poppreprocstack;
  2340. var
  2341. hp : tpreprocstack;
  2342. begin
  2343. if assigned(preprocstack) then
  2344. begin
  2345. Message1(scan_c_endif_found,preprocstack.name);
  2346. hp:=preprocstack.next;
  2347. preprocstack.free;
  2348. preprocstack:=hp;
  2349. end
  2350. else
  2351. Message(scan_e_endif_without_if);
  2352. end;
  2353. procedure tscannerfile.ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  2354. var
  2355. condition: Boolean;
  2356. valuedescr: String;
  2357. begin
  2358. if (preprocstack=nil) or preprocstack.accept then
  2359. condition:= compile_time_predicate(valuedescr)
  2360. else
  2361. begin
  2362. condition:= false;
  2363. valuedescr:= '';
  2364. end;
  2365. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  2366. preprocstack.name:=valuedescr;
  2367. preprocstack.line_nb:=line_no;
  2368. preprocstack.owner:=self;
  2369. if preprocstack.accept then
  2370. Message2(messid,preprocstack.name,'accepted')
  2371. else
  2372. Message2(messid,preprocstack.name,'rejected');
  2373. end;
  2374. procedure tscannerfile.elsepreprocstack;
  2375. begin
  2376. if assigned(preprocstack) and
  2377. (preprocstack.typ<>pp_else) then
  2378. begin
  2379. if (preprocstack.typ=pp_elseif) then
  2380. preprocstack.accept:=false
  2381. else
  2382. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  2383. preprocstack.accept:=not preprocstack.accept;
  2384. preprocstack.typ:=pp_else;
  2385. preprocstack.line_nb:=line_no;
  2386. if preprocstack.accept then
  2387. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2388. else
  2389. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2390. end
  2391. else
  2392. Message(scan_e_endif_without_if);
  2393. end;
  2394. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  2395. var
  2396. valuedescr: String;
  2397. begin
  2398. if assigned(preprocstack) and
  2399. (preprocstack.typ in [pp_if,pp_elseif]) then
  2400. begin
  2401. { when the branch is accepted we use pp_elseif so we know that
  2402. all the next branches need to be rejected. when this branch is still
  2403. not accepted then leave it at pp_if }
  2404. if (preprocstack.typ=pp_elseif) then
  2405. preprocstack.accept:=false
  2406. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  2407. begin
  2408. preprocstack.accept:=false;
  2409. preprocstack.typ:=pp_elseif;
  2410. end
  2411. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  2412. and compile_time_predicate(valuedescr) then
  2413. begin
  2414. preprocstack.name:=valuedescr;
  2415. preprocstack.accept:=true;
  2416. preprocstack.typ:=pp_elseif;
  2417. end;
  2418. preprocstack.line_nb:=line_no;
  2419. if preprocstack.accept then
  2420. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2421. else
  2422. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2423. end
  2424. else
  2425. Message(scan_e_endif_without_if);
  2426. end;
  2427. procedure tscannerfile.popreplaystack;
  2428. var
  2429. hp : treplaystack;
  2430. begin
  2431. if assigned(replaystack) then
  2432. begin
  2433. hp:=replaystack.next;
  2434. replaystack.free;
  2435. replaystack:=hp;
  2436. end;
  2437. end;
  2438. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  2439. begin
  2440. savetokenpos;
  2441. repeat
  2442. current_scanner.gettokenpos;
  2443. p.proc();
  2444. { accept the text ? }
  2445. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  2446. break
  2447. else
  2448. begin
  2449. current_scanner.gettokenpos;
  2450. Message(scan_c_skipping_until);
  2451. repeat
  2452. current_scanner.skipuntildirective;
  2453. if not (m_mac in current_settings.modeswitches) then
  2454. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  2455. else
  2456. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  2457. until assigned(p) and (p.is_conditional);
  2458. current_scanner.gettokenpos;
  2459. Message1(scan_d_handling_switch,'$'+p.name);
  2460. end;
  2461. until false;
  2462. restoretokenpos;
  2463. end;
  2464. procedure tscannerfile.handledirectives;
  2465. var
  2466. t : tdirectiveitem;
  2467. hs : string;
  2468. begin
  2469. gettokenpos;
  2470. readchar; {Remove the $}
  2471. hs:=readid;
  2472. { handle empty directive }
  2473. if hs='' then
  2474. begin
  2475. Message1(scan_w_illegal_switch,'$');
  2476. exit;
  2477. end;
  2478. {$ifdef PREPROCWRITE}
  2479. if parapreprocess then
  2480. begin
  2481. t:=Get_Directive(hs);
  2482. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  2483. begin
  2484. preprocfile^.AddSpace;
  2485. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  2486. exit;
  2487. end;
  2488. end;
  2489. {$endif PREPROCWRITE}
  2490. { skip this directive? }
  2491. if (ignoredirectives.find(hs)<>nil) then
  2492. begin
  2493. if (comment_level>0) then
  2494. readcomment;
  2495. { we've read the whole comment }
  2496. aktcommentstyle:=comment_none;
  2497. exit;
  2498. end;
  2499. { Check for compiler switches }
  2500. while (length(hs)=1) and (c in ['-','+']) do
  2501. begin
  2502. HandleSwitch(hs[1],c);
  2503. current_scanner.readchar; {Remove + or -}
  2504. if c=',' then
  2505. begin
  2506. current_scanner.readchar; {Remove , }
  2507. { read next switch, support $v+,$+}
  2508. hs:=current_scanner.readid;
  2509. if (hs='') then
  2510. begin
  2511. if (c='$') and (m_fpc in current_settings.modeswitches) then
  2512. begin
  2513. current_scanner.readchar; { skip $ }
  2514. hs:=current_scanner.readid;
  2515. end;
  2516. if (hs='') then
  2517. Message1(scan_w_illegal_directive,'$'+c);
  2518. end
  2519. else
  2520. Message1(scan_d_handling_switch,'$'+hs);
  2521. end
  2522. else
  2523. hs:='';
  2524. end;
  2525. { directives may follow switches after a , }
  2526. if hs<>'' then
  2527. begin
  2528. if not (m_mac in current_settings.modeswitches) then
  2529. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  2530. else
  2531. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  2532. if assigned(t) then
  2533. begin
  2534. if t.is_conditional then
  2535. handleconditional(t)
  2536. else
  2537. begin
  2538. Message1(scan_d_handling_switch,'$'+hs);
  2539. t.proc();
  2540. end;
  2541. end
  2542. else
  2543. begin
  2544. current_scanner.ignoredirectives.Add(hs,nil);
  2545. Message1(scan_w_illegal_directive,'$'+hs);
  2546. end;
  2547. { conditionals already read the comment }
  2548. if (current_scanner.comment_level>0) then
  2549. current_scanner.readcomment;
  2550. { we've read the whole comment }
  2551. aktcommentstyle:=comment_none;
  2552. end;
  2553. end;
  2554. procedure tscannerfile.readchar;
  2555. begin
  2556. c:=inputpointer^;
  2557. if c=#0 then
  2558. reload
  2559. else
  2560. inc(inputpointer);
  2561. end;
  2562. procedure tscannerfile.readstring;
  2563. var
  2564. i : longint;
  2565. err : boolean;
  2566. begin
  2567. err:=false;
  2568. i:=0;
  2569. repeat
  2570. case c of
  2571. '_',
  2572. '0'..'9',
  2573. 'A'..'Z' :
  2574. begin
  2575. if i<255 then
  2576. begin
  2577. inc(i);
  2578. orgpattern[i]:=c;
  2579. pattern[i]:=c;
  2580. end
  2581. else
  2582. begin
  2583. if not err then
  2584. begin
  2585. Message(scan_e_string_exceeds_255_chars);
  2586. err:=true;
  2587. end;
  2588. end;
  2589. c:=inputpointer^;
  2590. inc(inputpointer);
  2591. end;
  2592. 'a'..'z' :
  2593. begin
  2594. if i<255 then
  2595. begin
  2596. inc(i);
  2597. orgpattern[i]:=c;
  2598. pattern[i]:=chr(ord(c)-32)
  2599. end
  2600. else
  2601. begin
  2602. if not err then
  2603. begin
  2604. Message(scan_e_string_exceeds_255_chars);
  2605. err:=true;
  2606. end;
  2607. end;
  2608. c:=inputpointer^;
  2609. inc(inputpointer);
  2610. end;
  2611. #0 :
  2612. reload;
  2613. else
  2614. break;
  2615. end;
  2616. until false;
  2617. orgpattern[0]:=chr(i);
  2618. pattern[0]:=chr(i);
  2619. end;
  2620. procedure tscannerfile.readnumber;
  2621. var
  2622. base,
  2623. i : longint;
  2624. begin
  2625. case c of
  2626. '%' :
  2627. begin
  2628. readchar;
  2629. base:=2;
  2630. pattern[1]:='%';
  2631. i:=1;
  2632. end;
  2633. '&' :
  2634. begin
  2635. readchar;
  2636. base:=8;
  2637. pattern[1]:='&';
  2638. i:=1;
  2639. end;
  2640. '$' :
  2641. begin
  2642. readchar;
  2643. base:=16;
  2644. pattern[1]:='$';
  2645. i:=1;
  2646. end;
  2647. else
  2648. begin
  2649. base:=10;
  2650. i:=0;
  2651. end;
  2652. end;
  2653. while ((base>=10) and (c in ['0'..'9'])) or
  2654. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  2655. ((base=8) and (c in ['0'..'7'])) or
  2656. ((base=2) and (c in ['0'..'1'])) do
  2657. begin
  2658. if i<255 then
  2659. begin
  2660. inc(i);
  2661. pattern[i]:=c;
  2662. end;
  2663. readchar;
  2664. end;
  2665. pattern[0]:=chr(i);
  2666. end;
  2667. function tscannerfile.readid:string;
  2668. begin
  2669. readstring;
  2670. readid:=pattern;
  2671. end;
  2672. function tscannerfile.readval:longint;
  2673. var
  2674. l : longint;
  2675. w : integer;
  2676. begin
  2677. readnumber;
  2678. val(pattern,l,w);
  2679. readval:=l;
  2680. end;
  2681. function tscannerfile.readval_asstring:string;
  2682. begin
  2683. readnumber;
  2684. readval_asstring:=pattern;
  2685. end;
  2686. function tscannerfile.readcomment:string;
  2687. var
  2688. i : longint;
  2689. begin
  2690. i:=0;
  2691. repeat
  2692. case c of
  2693. '{' :
  2694. begin
  2695. if aktcommentstyle=comment_tp then
  2696. inc_comment_level;
  2697. end;
  2698. '}' :
  2699. begin
  2700. if aktcommentstyle=comment_tp then
  2701. begin
  2702. readchar;
  2703. dec_comment_level;
  2704. if comment_level=0 then
  2705. break
  2706. else
  2707. continue;
  2708. end;
  2709. end;
  2710. '*' :
  2711. begin
  2712. if aktcommentstyle=comment_oldtp then
  2713. begin
  2714. readchar;
  2715. if c=')' then
  2716. begin
  2717. readchar;
  2718. dec_comment_level;
  2719. break;
  2720. end
  2721. else
  2722. { Add both characters !!}
  2723. if (i<255) then
  2724. begin
  2725. inc(i);
  2726. readcomment[i]:='*';
  2727. if (i<255) then
  2728. begin
  2729. inc(i);
  2730. readcomment[i]:=c;
  2731. end;
  2732. end;
  2733. end
  2734. else
  2735. { Not old TP comment, so add...}
  2736. begin
  2737. if (i<255) then
  2738. begin
  2739. inc(i);
  2740. readcomment[i]:='*';
  2741. end;
  2742. end;
  2743. end;
  2744. #10,#13 :
  2745. linebreak;
  2746. #26 :
  2747. end_of_file;
  2748. else
  2749. begin
  2750. if (i<255) then
  2751. begin
  2752. inc(i);
  2753. readcomment[i]:=c;
  2754. end;
  2755. end;
  2756. end;
  2757. readchar;
  2758. until false;
  2759. readcomment[0]:=chr(i);
  2760. end;
  2761. function tscannerfile.readquotedstring:string;
  2762. var
  2763. i : longint;
  2764. msgwritten : boolean;
  2765. begin
  2766. i:=0;
  2767. msgwritten:=false;
  2768. if (c='''') then
  2769. begin
  2770. repeat
  2771. readchar;
  2772. case c of
  2773. #26 :
  2774. end_of_file;
  2775. #10,#13 :
  2776. Message(scan_f_string_exceeds_line);
  2777. '''' :
  2778. begin
  2779. readchar;
  2780. if c<>'''' then
  2781. break;
  2782. end;
  2783. end;
  2784. if i<255 then
  2785. begin
  2786. inc(i);
  2787. result[i]:=c;
  2788. end
  2789. else
  2790. begin
  2791. if not msgwritten then
  2792. begin
  2793. Message(scan_e_string_exceeds_255_chars);
  2794. msgwritten:=true;
  2795. end;
  2796. end;
  2797. until false;
  2798. end;
  2799. result[0]:=chr(i);
  2800. end;
  2801. function tscannerfile.readstate:char;
  2802. var
  2803. state : char;
  2804. begin
  2805. state:=' ';
  2806. if c=' ' then
  2807. begin
  2808. current_scanner.skipspace;
  2809. current_scanner.readid;
  2810. if pattern='ON' then
  2811. state:='+'
  2812. else
  2813. if pattern='OFF' then
  2814. state:='-';
  2815. end
  2816. else
  2817. state:=c;
  2818. if not (state in ['+','-']) then
  2819. Message(scan_e_wrong_switch_toggle);
  2820. readstate:=state;
  2821. end;
  2822. function tscannerfile.readstatedefault:char;
  2823. var
  2824. state : char;
  2825. begin
  2826. state:=' ';
  2827. if c=' ' then
  2828. begin
  2829. current_scanner.skipspace;
  2830. current_scanner.readid;
  2831. if pattern='ON' then
  2832. state:='+'
  2833. else
  2834. if pattern='OFF' then
  2835. state:='-'
  2836. else
  2837. if pattern='DEFAULT' then
  2838. state:='*';
  2839. end
  2840. else
  2841. state:=c;
  2842. if not (state in ['+','-','*']) then
  2843. Message(scan_e_wrong_switch_toggle_default);
  2844. readstatedefault:=state;
  2845. end;
  2846. procedure tscannerfile.skipspace;
  2847. begin
  2848. repeat
  2849. case c of
  2850. #26 :
  2851. begin
  2852. reload;
  2853. if (c=#26) and not assigned(inputfile.next) then
  2854. break;
  2855. continue;
  2856. end;
  2857. #10,
  2858. #13 :
  2859. linebreak;
  2860. #9,#11,#12,' ' :
  2861. ;
  2862. else
  2863. break;
  2864. end;
  2865. readchar;
  2866. until false;
  2867. end;
  2868. procedure tscannerfile.skipuntildirective;
  2869. var
  2870. found : longint;
  2871. next_char_loaded : boolean;
  2872. begin
  2873. found:=0;
  2874. next_char_loaded:=false;
  2875. repeat
  2876. case c of
  2877. #10,
  2878. #13 :
  2879. linebreak;
  2880. #26 :
  2881. begin
  2882. reload;
  2883. if (c=#26) and not assigned(inputfile.next) then
  2884. end_of_file;
  2885. continue;
  2886. end;
  2887. '{' :
  2888. begin
  2889. if (aktcommentstyle in [comment_tp,comment_none]) then
  2890. begin
  2891. aktcommentstyle:=comment_tp;
  2892. if (comment_level=0) then
  2893. found:=1;
  2894. inc_comment_level;
  2895. end;
  2896. end;
  2897. '*' :
  2898. begin
  2899. if (aktcommentstyle=comment_oldtp) then
  2900. begin
  2901. readchar;
  2902. if c=')' then
  2903. begin
  2904. dec_comment_level;
  2905. found:=0;
  2906. aktcommentstyle:=comment_none;
  2907. end
  2908. else
  2909. next_char_loaded:=true;
  2910. end
  2911. else
  2912. found := 0;
  2913. end;
  2914. '}' :
  2915. begin
  2916. if (aktcommentstyle=comment_tp) then
  2917. begin
  2918. dec_comment_level;
  2919. if (comment_level=0) then
  2920. aktcommentstyle:=comment_none;
  2921. found:=0;
  2922. end;
  2923. end;
  2924. '$' :
  2925. begin
  2926. if found=1 then
  2927. found:=2;
  2928. end;
  2929. '''' :
  2930. if (aktcommentstyle=comment_none) then
  2931. begin
  2932. repeat
  2933. readchar;
  2934. case c of
  2935. #26 :
  2936. end_of_file;
  2937. #10,#13 :
  2938. break;
  2939. '''' :
  2940. begin
  2941. readchar;
  2942. if c<>'''' then
  2943. begin
  2944. next_char_loaded:=true;
  2945. break;
  2946. end;
  2947. end;
  2948. end;
  2949. until false;
  2950. end;
  2951. '(' :
  2952. begin
  2953. if (aktcommentstyle=comment_none) then
  2954. begin
  2955. readchar;
  2956. if c='*' then
  2957. begin
  2958. readchar;
  2959. if c='$' then
  2960. begin
  2961. found:=2;
  2962. inc_comment_level;
  2963. aktcommentstyle:=comment_oldtp;
  2964. end
  2965. else
  2966. begin
  2967. skipoldtpcomment;
  2968. next_char_loaded:=true;
  2969. end;
  2970. end
  2971. else
  2972. next_char_loaded:=true;
  2973. end
  2974. else
  2975. found:=0;
  2976. end;
  2977. '/' :
  2978. begin
  2979. if (aktcommentstyle=comment_none) then
  2980. begin
  2981. readchar;
  2982. if c='/' then
  2983. skipdelphicomment;
  2984. next_char_loaded:=true;
  2985. end
  2986. else
  2987. found:=0;
  2988. end;
  2989. else
  2990. found:=0;
  2991. end;
  2992. if next_char_loaded then
  2993. next_char_loaded:=false
  2994. else
  2995. readchar;
  2996. until (found=2);
  2997. end;
  2998. {****************************************************************************
  2999. Comment Handling
  3000. ****************************************************************************}
  3001. procedure tscannerfile.skipcomment;
  3002. begin
  3003. aktcommentstyle:=comment_tp;
  3004. readchar;
  3005. inc_comment_level;
  3006. { handle compiler switches }
  3007. if (c='$') then
  3008. handledirectives;
  3009. { handle_switches can dec comment_level, }
  3010. while (comment_level>0) do
  3011. begin
  3012. case c of
  3013. '{' :
  3014. inc_comment_level;
  3015. '}' :
  3016. dec_comment_level;
  3017. #10,#13 :
  3018. linebreak;
  3019. #26 :
  3020. begin
  3021. reload;
  3022. if (c=#26) and not assigned(inputfile.next) then
  3023. end_of_file;
  3024. continue;
  3025. end;
  3026. end;
  3027. readchar;
  3028. end;
  3029. aktcommentstyle:=comment_none;
  3030. end;
  3031. procedure tscannerfile.skipdelphicomment;
  3032. begin
  3033. aktcommentstyle:=comment_delphi;
  3034. inc_comment_level;
  3035. readchar;
  3036. { this is not supported }
  3037. if c='$' then
  3038. Message(scan_w_wrong_styled_switch);
  3039. { skip comment }
  3040. while not (c in [#10,#13,#26]) do
  3041. readchar;
  3042. dec_comment_level;
  3043. aktcommentstyle:=comment_none;
  3044. end;
  3045. procedure tscannerfile.skipoldtpcomment;
  3046. var
  3047. found : longint;
  3048. begin
  3049. aktcommentstyle:=comment_oldtp;
  3050. inc_comment_level;
  3051. { only load a char if last already processed,
  3052. was cause of bug1634 PM }
  3053. if c=#0 then
  3054. readchar;
  3055. { this is now supported }
  3056. if (c='$') then
  3057. handledirectives;
  3058. { skip comment }
  3059. while (comment_level>0) do
  3060. begin
  3061. found:=0;
  3062. repeat
  3063. case c of
  3064. #26 :
  3065. begin
  3066. reload;
  3067. if (c=#26) and not assigned(inputfile.next) then
  3068. end_of_file;
  3069. continue;
  3070. end;
  3071. #10,#13 :
  3072. begin
  3073. if found=4 then
  3074. inc_comment_level;
  3075. linebreak;
  3076. found:=0;
  3077. end;
  3078. '*' :
  3079. begin
  3080. if found=3 then
  3081. found:=4
  3082. else
  3083. found:=1;
  3084. end;
  3085. ')' :
  3086. begin
  3087. if found in [1,4] then
  3088. begin
  3089. dec_comment_level;
  3090. if comment_level=0 then
  3091. found:=2
  3092. else
  3093. found:=0;
  3094. end
  3095. else
  3096. found:=0;
  3097. end;
  3098. '(' :
  3099. begin
  3100. if found=4 then
  3101. inc_comment_level;
  3102. found:=3;
  3103. end;
  3104. else
  3105. begin
  3106. if found=4 then
  3107. inc_comment_level;
  3108. found:=0;
  3109. end;
  3110. end;
  3111. readchar;
  3112. until (found=2);
  3113. end;
  3114. aktcommentstyle:=comment_none;
  3115. end;
  3116. {****************************************************************************
  3117. Token Scanner
  3118. ****************************************************************************}
  3119. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  3120. var
  3121. code : integer;
  3122. len,
  3123. low,high,mid : longint;
  3124. w : word;
  3125. m : longint;
  3126. mac : tmacro;
  3127. asciinr : string[6];
  3128. iswidestring : boolean;
  3129. label
  3130. exit_label;
  3131. begin
  3132. flushpendingswitchesstate;
  3133. { record tokens? }
  3134. if allowrecordtoken and
  3135. assigned(recordtokenbuf) then
  3136. recordtoken;
  3137. { replay tokens? }
  3138. if assigned(replaytokenbuf) then
  3139. begin
  3140. replaytoken;
  3141. goto exit_label;
  3142. end;
  3143. { was there already a token read, then return that token }
  3144. if nexttoken<>NOTOKEN then
  3145. begin
  3146. setnexttoken;
  3147. goto exit_label;
  3148. end;
  3149. { Skip all spaces and comments }
  3150. repeat
  3151. case c of
  3152. '{' :
  3153. skipcomment;
  3154. #26 :
  3155. begin
  3156. reload;
  3157. if (c=#26) and not assigned(inputfile.next) then
  3158. break;
  3159. end;
  3160. ' ',#9..#13 :
  3161. begin
  3162. {$ifdef PREPROCWRITE}
  3163. if parapreprocess then
  3164. begin
  3165. if c=#10 then
  3166. preprocfile.eolfound:=true
  3167. else
  3168. preprocfile.spacefound:=true;
  3169. end;
  3170. {$endif PREPROCWRITE}
  3171. skipspace;
  3172. end
  3173. else
  3174. break;
  3175. end;
  3176. until false;
  3177. { Save current token position, for EOF its already loaded }
  3178. if c<>#26 then
  3179. gettokenpos;
  3180. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  3181. if c in ['A'..'Z','a'..'z','_'] then
  3182. begin
  3183. readstring;
  3184. token:=_ID;
  3185. idtoken:=_ID;
  3186. { keyword or any other known token,
  3187. pattern is always uppercased }
  3188. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  3189. begin
  3190. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  3191. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  3192. while low<high do
  3193. begin
  3194. mid:=(high+low+1) shr 1;
  3195. if pattern<tokeninfo^[ttoken(mid)].str then
  3196. high:=mid-1
  3197. else
  3198. low:=mid;
  3199. end;
  3200. with tokeninfo^[ttoken(high)] do
  3201. if pattern=str then
  3202. begin
  3203. if keyword in current_settings.modeswitches then
  3204. if op=NOTOKEN then
  3205. token:=ttoken(high)
  3206. else
  3207. token:=op;
  3208. idtoken:=ttoken(high);
  3209. end;
  3210. end;
  3211. { Only process identifiers and not keywords }
  3212. if token=_ID then
  3213. begin
  3214. { this takes some time ... }
  3215. if (cs_support_macro in current_settings.moduleswitches) then
  3216. begin
  3217. mac:=tmacro(search_macro(pattern));
  3218. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  3219. begin
  3220. if yylexcount<max_macro_nesting then
  3221. begin
  3222. mac.is_used:=true;
  3223. inc(yylexcount);
  3224. substitutemacro(pattern,mac.buftext,mac.buflen,
  3225. mac.fileinfo.line,mac.fileinfo.fileindex);
  3226. { handle empty macros }
  3227. if c=#0 then
  3228. reload;
  3229. readtoken(false);
  3230. { that's all folks }
  3231. dec(yylexcount);
  3232. exit;
  3233. end
  3234. else
  3235. Message(scan_w_macro_too_deep);
  3236. end;
  3237. end;
  3238. end;
  3239. { return token }
  3240. goto exit_label;
  3241. end
  3242. else
  3243. begin
  3244. idtoken:=_NOID;
  3245. case c of
  3246. '$' :
  3247. begin
  3248. readnumber;
  3249. token:=_INTCONST;
  3250. goto exit_label;
  3251. end;
  3252. '%' :
  3253. begin
  3254. if not(m_fpc in current_settings.modeswitches) then
  3255. Illegal_Char(c)
  3256. else
  3257. begin
  3258. readnumber;
  3259. token:=_INTCONST;
  3260. goto exit_label;
  3261. end;
  3262. end;
  3263. '&' :
  3264. begin
  3265. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  3266. begin
  3267. readnumber;
  3268. if length(pattern)=1 then
  3269. begin
  3270. readstring;
  3271. token:=_ID;
  3272. idtoken:=_ID;
  3273. end
  3274. else
  3275. token:=_INTCONST;
  3276. goto exit_label;
  3277. end
  3278. else if m_mac in current_settings.modeswitches then
  3279. begin
  3280. readchar;
  3281. token:=_AMPERSAND;
  3282. goto exit_label;
  3283. end
  3284. else
  3285. Illegal_Char(c);
  3286. end;
  3287. '0'..'9' :
  3288. begin
  3289. readnumber;
  3290. if (c in ['.','e','E']) then
  3291. begin
  3292. { first check for a . }
  3293. if c='.' then
  3294. begin
  3295. cachenexttokenpos;
  3296. readchar;
  3297. { is it a .. from a range? }
  3298. case c of
  3299. '.' :
  3300. begin
  3301. readchar;
  3302. token:=_INTCONST;
  3303. nexttoken:=_POINTPOINT;
  3304. goto exit_label;
  3305. end;
  3306. ')' :
  3307. begin
  3308. readchar;
  3309. token:=_INTCONST;
  3310. nexttoken:=_RECKKLAMMER;
  3311. goto exit_label;
  3312. end;
  3313. end;
  3314. { insert the number after the . }
  3315. pattern:=pattern+'.';
  3316. while c in ['0'..'9'] do
  3317. begin
  3318. pattern:=pattern+c;
  3319. readchar;
  3320. end;
  3321. end;
  3322. { E can also follow after a point is scanned }
  3323. if c in ['e','E'] then
  3324. begin
  3325. pattern:=pattern+'E';
  3326. readchar;
  3327. if c in ['-','+'] then
  3328. begin
  3329. pattern:=pattern+c;
  3330. readchar;
  3331. end;
  3332. if not(c in ['0'..'9']) then
  3333. Illegal_Char(c);
  3334. while c in ['0'..'9'] do
  3335. begin
  3336. pattern:=pattern+c;
  3337. readchar;
  3338. end;
  3339. end;
  3340. token:=_REALNUMBER;
  3341. goto exit_label;
  3342. end;
  3343. token:=_INTCONST;
  3344. goto exit_label;
  3345. end;
  3346. ';' :
  3347. begin
  3348. readchar;
  3349. token:=_SEMICOLON;
  3350. goto exit_label;
  3351. end;
  3352. '[' :
  3353. begin
  3354. readchar;
  3355. token:=_LECKKLAMMER;
  3356. goto exit_label;
  3357. end;
  3358. ']' :
  3359. begin
  3360. readchar;
  3361. token:=_RECKKLAMMER;
  3362. goto exit_label;
  3363. end;
  3364. '(' :
  3365. begin
  3366. readchar;
  3367. case c of
  3368. '*' :
  3369. begin
  3370. c:=#0;{Signal skipoldtpcomment to reload a char }
  3371. skipoldtpcomment;
  3372. readtoken(false);
  3373. exit;
  3374. end;
  3375. '.' :
  3376. begin
  3377. readchar;
  3378. token:=_LECKKLAMMER;
  3379. goto exit_label;
  3380. end;
  3381. end;
  3382. token:=_LKLAMMER;
  3383. goto exit_label;
  3384. end;
  3385. ')' :
  3386. begin
  3387. readchar;
  3388. token:=_RKLAMMER;
  3389. goto exit_label;
  3390. end;
  3391. '+' :
  3392. begin
  3393. readchar;
  3394. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3395. begin
  3396. readchar;
  3397. token:=_PLUSASN;
  3398. goto exit_label;
  3399. end;
  3400. token:=_PLUS;
  3401. goto exit_label;
  3402. end;
  3403. '-' :
  3404. begin
  3405. readchar;
  3406. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3407. begin
  3408. readchar;
  3409. token:=_MINUSASN;
  3410. goto exit_label;
  3411. end;
  3412. token:=_MINUS;
  3413. goto exit_label;
  3414. end;
  3415. ':' :
  3416. begin
  3417. readchar;
  3418. if c='=' then
  3419. begin
  3420. readchar;
  3421. token:=_ASSIGNMENT;
  3422. goto exit_label;
  3423. end;
  3424. token:=_COLON;
  3425. goto exit_label;
  3426. end;
  3427. '*' :
  3428. begin
  3429. readchar;
  3430. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3431. begin
  3432. readchar;
  3433. token:=_STARASN;
  3434. end
  3435. else
  3436. if c='*' then
  3437. begin
  3438. readchar;
  3439. token:=_STARSTAR;
  3440. end
  3441. else
  3442. token:=_STAR;
  3443. goto exit_label;
  3444. end;
  3445. '/' :
  3446. begin
  3447. readchar;
  3448. case c of
  3449. '=' :
  3450. begin
  3451. if (cs_support_c_operators in current_settings.moduleswitches) then
  3452. begin
  3453. readchar;
  3454. token:=_SLASHASN;
  3455. goto exit_label;
  3456. end;
  3457. end;
  3458. '/' :
  3459. begin
  3460. skipdelphicomment;
  3461. readtoken(false);
  3462. exit;
  3463. end;
  3464. end;
  3465. token:=_SLASH;
  3466. goto exit_label;
  3467. end;
  3468. '|' :
  3469. if m_mac in current_settings.modeswitches then
  3470. begin
  3471. readchar;
  3472. token:=_PIPE;
  3473. goto exit_label;
  3474. end
  3475. else
  3476. Illegal_Char(c);
  3477. '=' :
  3478. begin
  3479. readchar;
  3480. token:=_EQ;
  3481. goto exit_label;
  3482. end;
  3483. '.' :
  3484. begin
  3485. readchar;
  3486. case c of
  3487. '.' :
  3488. begin
  3489. readchar;
  3490. case c of
  3491. '.' :
  3492. begin
  3493. readchar;
  3494. token:=_POINTPOINTPOINT;
  3495. goto exit_label;
  3496. end;
  3497. else
  3498. begin
  3499. token:=_POINTPOINT;
  3500. goto exit_label;
  3501. end;
  3502. end;
  3503. end;
  3504. ')' :
  3505. begin
  3506. readchar;
  3507. token:=_RECKKLAMMER;
  3508. goto exit_label;
  3509. end;
  3510. end;
  3511. token:=_POINT;
  3512. goto exit_label;
  3513. end;
  3514. '@' :
  3515. begin
  3516. readchar;
  3517. token:=_KLAMMERAFFE;
  3518. goto exit_label;
  3519. end;
  3520. ',' :
  3521. begin
  3522. readchar;
  3523. token:=_COMMA;
  3524. goto exit_label;
  3525. end;
  3526. '''','#','^' :
  3527. begin
  3528. len:=0;
  3529. cstringpattern:='';
  3530. iswidestring:=false;
  3531. if c='^' then
  3532. begin
  3533. readchar;
  3534. c:=upcase(c);
  3535. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  3536. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  3537. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  3538. begin
  3539. token:=_CARET;
  3540. goto exit_label;
  3541. end
  3542. else
  3543. begin
  3544. inc(len);
  3545. setlength(cstringpattern,256);
  3546. if c<#64 then
  3547. cstringpattern[len]:=chr(ord(c)+64)
  3548. else
  3549. cstringpattern[len]:=chr(ord(c)-64);
  3550. readchar;
  3551. end;
  3552. end;
  3553. repeat
  3554. case c of
  3555. '#' :
  3556. begin
  3557. readchar; { read # }
  3558. case c of
  3559. '$':
  3560. begin
  3561. readchar; { read leading $ }
  3562. asciinr:='$';
  3563. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=5) do
  3564. begin
  3565. asciinr:=asciinr+c;
  3566. readchar;
  3567. end;
  3568. end;
  3569. '&':
  3570. begin
  3571. readchar; { read leading $ }
  3572. asciinr:='&';
  3573. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=7) do
  3574. begin
  3575. asciinr:=asciinr+c;
  3576. readchar;
  3577. end;
  3578. end;
  3579. '%':
  3580. begin
  3581. readchar; { read leading $ }
  3582. asciinr:='%';
  3583. while (upcase(c) in ['0','1']) and (length(asciinr)<=17) do
  3584. begin
  3585. asciinr:=asciinr+c;
  3586. readchar;
  3587. end;
  3588. end;
  3589. else
  3590. begin
  3591. asciinr:='';
  3592. while (c in ['0'..'9']) and (length(asciinr)<=5) do
  3593. begin
  3594. asciinr:=asciinr+c;
  3595. readchar;
  3596. end;
  3597. end;
  3598. end;
  3599. val(asciinr,m,code);
  3600. if (asciinr='') or (code<>0) then
  3601. Message(scan_e_illegal_char_const)
  3602. else if (m<0) or (m>255) or (length(asciinr)>3) then
  3603. begin
  3604. if (m>=0) and (m<=65535) then
  3605. begin
  3606. if not iswidestring then
  3607. begin
  3608. if len>0 then
  3609. ascii2unicode(@cstringpattern[1],len,patternw)
  3610. else
  3611. ascii2unicode(nil,len,patternw);
  3612. iswidestring:=true;
  3613. len:=0;
  3614. end;
  3615. concatwidestringchar(patternw,tcompilerwidechar(m));
  3616. end
  3617. else
  3618. Message(scan_e_illegal_char_const)
  3619. end
  3620. else if iswidestring then
  3621. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  3622. else
  3623. begin
  3624. if len>=length(cstringpattern) then
  3625. setlength(cstringpattern,length(cstringpattern)+256);
  3626. inc(len);
  3627. cstringpattern[len]:=chr(m);
  3628. end;
  3629. end;
  3630. '''' :
  3631. begin
  3632. repeat
  3633. readchar;
  3634. case c of
  3635. #26 :
  3636. end_of_file;
  3637. #10,#13 :
  3638. Message(scan_f_string_exceeds_line);
  3639. '''' :
  3640. begin
  3641. readchar;
  3642. if c<>'''' then
  3643. break;
  3644. end;
  3645. end;
  3646. { interpret as utf-8 string? }
  3647. if (ord(c)>=$80) and (current_settings.sourcecodepage='utf8') then
  3648. begin
  3649. { convert existing string to an utf-8 string }
  3650. if not iswidestring then
  3651. begin
  3652. if len>0 then
  3653. ascii2unicode(@cstringpattern[1],len,patternw)
  3654. else
  3655. ascii2unicode(nil,len,patternw);
  3656. iswidestring:=true;
  3657. len:=0;
  3658. end;
  3659. { four or more chars aren't handled }
  3660. if (ord(c) and $f0)=$f0 then
  3661. message(scan_e_utf8_bigger_than_65535)
  3662. { three chars }
  3663. else if (ord(c) and $e0)=$e0 then
  3664. begin
  3665. w:=ord(c) and $f;
  3666. readchar;
  3667. if (ord(c) and $c0)<>$80 then
  3668. message(scan_e_utf8_malformed);
  3669. w:=(w shl 6) or (ord(c) and $3f);
  3670. readchar;
  3671. if (ord(c) and $c0)<>$80 then
  3672. message(scan_e_utf8_malformed);
  3673. w:=(w shl 6) or (ord(c) and $3f);
  3674. concatwidestringchar(patternw,w);
  3675. end
  3676. { two chars }
  3677. else if (ord(c) and $c0)<>0 then
  3678. begin
  3679. w:=ord(c) and $1f;
  3680. readchar;
  3681. if (ord(c) and $c0)<>$80 then
  3682. message(scan_e_utf8_malformed);
  3683. w:=(w shl 6) or (ord(c) and $3f);
  3684. concatwidestringchar(patternw,w);
  3685. end
  3686. { illegal }
  3687. else if (ord(c) and $80)<>0 then
  3688. message(scan_e_utf8_malformed)
  3689. else
  3690. concatwidestringchar(patternw,tcompilerwidechar(c))
  3691. end
  3692. else if iswidestring then
  3693. begin
  3694. if current_settings.sourcecodepage='utf8' then
  3695. concatwidestringchar(patternw,ord(c))
  3696. else
  3697. concatwidestringchar(patternw,asciichar2unicode(c))
  3698. end
  3699. else
  3700. begin
  3701. if len>=length(cstringpattern) then
  3702. setlength(cstringpattern,length(cstringpattern)+256);
  3703. inc(len);
  3704. cstringpattern[len]:=c;
  3705. end;
  3706. until false;
  3707. end;
  3708. '^' :
  3709. begin
  3710. readchar;
  3711. c:=upcase(c);
  3712. if c<#64 then
  3713. c:=chr(ord(c)+64)
  3714. else
  3715. c:=chr(ord(c)-64);
  3716. if iswidestring then
  3717. concatwidestringchar(patternw,asciichar2unicode(c))
  3718. else
  3719. begin
  3720. if len>=length(cstringpattern) then
  3721. setlength(cstringpattern,length(cstringpattern)+256);
  3722. inc(len);
  3723. cstringpattern[len]:=c;
  3724. end;
  3725. readchar;
  3726. end;
  3727. else
  3728. break;
  3729. end;
  3730. until false;
  3731. { strings with length 1 become const chars }
  3732. if iswidestring then
  3733. begin
  3734. if patternw^.len=1 then
  3735. token:=_CWCHAR
  3736. else
  3737. token:=_CWSTRING;
  3738. end
  3739. else
  3740. begin
  3741. setlength(cstringpattern,len);
  3742. if length(cstringpattern)=1 then
  3743. begin
  3744. token:=_CCHAR;
  3745. pattern:=cstringpattern;
  3746. end
  3747. else
  3748. token:=_CSTRING;
  3749. end;
  3750. goto exit_label;
  3751. end;
  3752. '>' :
  3753. begin
  3754. readchar;
  3755. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  3756. token:=_RSHARPBRACKET
  3757. else
  3758. begin
  3759. case c of
  3760. '=' :
  3761. begin
  3762. readchar;
  3763. token:=_GTE;
  3764. goto exit_label;
  3765. end;
  3766. '>' :
  3767. begin
  3768. readchar;
  3769. token:=_OP_SHR;
  3770. goto exit_label;
  3771. end;
  3772. '<' :
  3773. begin { >< is for a symetric diff for sets }
  3774. readchar;
  3775. token:=_SYMDIF;
  3776. goto exit_label;
  3777. end;
  3778. end;
  3779. token:=_GT;
  3780. end;
  3781. goto exit_label;
  3782. end;
  3783. '<' :
  3784. begin
  3785. readchar;
  3786. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  3787. token:=_LSHARPBRACKET
  3788. else
  3789. begin
  3790. case c of
  3791. '>' :
  3792. begin
  3793. readchar;
  3794. token:=_NE;
  3795. goto exit_label;
  3796. end;
  3797. '=' :
  3798. begin
  3799. readchar;
  3800. token:=_LTE;
  3801. goto exit_label;
  3802. end;
  3803. '<' :
  3804. begin
  3805. readchar;
  3806. token:=_OP_SHL;
  3807. goto exit_label;
  3808. end;
  3809. end;
  3810. token:=_LT;
  3811. end;
  3812. goto exit_label;
  3813. end;
  3814. #26 :
  3815. begin
  3816. token:=_EOF;
  3817. checkpreprocstack;
  3818. goto exit_label;
  3819. end;
  3820. else
  3821. Illegal_Char(c);
  3822. end;
  3823. end;
  3824. exit_label:
  3825. lasttoken:=token;
  3826. end;
  3827. function tscannerfile.readpreproc:ttoken;
  3828. begin
  3829. skipspace;
  3830. case c of
  3831. '_',
  3832. 'A'..'Z',
  3833. 'a'..'z' :
  3834. begin
  3835. current_scanner.preproc_pattern:=readid;
  3836. readpreproc:=_ID;
  3837. end;
  3838. '0'..'9' :
  3839. begin
  3840. current_scanner.preproc_pattern:=readval_asstring;
  3841. { realnumber? }
  3842. if c='.' then
  3843. begin
  3844. readchar;
  3845. while c in ['0'..'9'] do
  3846. begin
  3847. current_scanner.preproc_pattern:=current_scanner.preproc_pattern+c;
  3848. readchar;
  3849. end;
  3850. end;
  3851. readpreproc:=_ID;
  3852. end;
  3853. '$','%','&' :
  3854. begin
  3855. current_scanner.preproc_pattern:=readval_asstring;
  3856. readpreproc:=_ID;
  3857. end;
  3858. ',' :
  3859. begin
  3860. readchar;
  3861. readpreproc:=_COMMA;
  3862. end;
  3863. '}' :
  3864. begin
  3865. readpreproc:=_END;
  3866. end;
  3867. '(' :
  3868. begin
  3869. readchar;
  3870. readpreproc:=_LKLAMMER;
  3871. end;
  3872. ')' :
  3873. begin
  3874. readchar;
  3875. readpreproc:=_RKLAMMER;
  3876. end;
  3877. '[' :
  3878. begin
  3879. readchar;
  3880. readpreproc:=_LECKKLAMMER;
  3881. end;
  3882. ']' :
  3883. begin
  3884. readchar;
  3885. readpreproc:=_RECKKLAMMER;
  3886. end;
  3887. '+' :
  3888. begin
  3889. readchar;
  3890. readpreproc:=_PLUS;
  3891. end;
  3892. '-' :
  3893. begin
  3894. readchar;
  3895. readpreproc:=_MINUS;
  3896. end;
  3897. '*' :
  3898. begin
  3899. readchar;
  3900. readpreproc:=_STAR;
  3901. end;
  3902. '/' :
  3903. begin
  3904. readchar;
  3905. readpreproc:=_SLASH;
  3906. end;
  3907. '=' :
  3908. begin
  3909. readchar;
  3910. readpreproc:=_EQ;
  3911. end;
  3912. '>' :
  3913. begin
  3914. readchar;
  3915. if c='=' then
  3916. begin
  3917. readchar;
  3918. readpreproc:=_GTE;
  3919. end
  3920. else
  3921. readpreproc:=_GT;
  3922. end;
  3923. '<' :
  3924. begin
  3925. readchar;
  3926. case c of
  3927. '>' :
  3928. begin
  3929. readchar;
  3930. readpreproc:=_NE;
  3931. end;
  3932. '=' :
  3933. begin
  3934. readchar;
  3935. readpreproc:=_LTE;
  3936. end;
  3937. else
  3938. readpreproc:=_LT;
  3939. end;
  3940. end;
  3941. #26 :
  3942. begin
  3943. readpreproc:=_EOF;
  3944. checkpreprocstack;
  3945. end;
  3946. else
  3947. Illegal_Char(c);
  3948. end;
  3949. end;
  3950. function tscannerfile.asmgetcharstart : char;
  3951. begin
  3952. { return first the character already
  3953. available in c }
  3954. lastasmgetchar:=c;
  3955. result:=asmgetchar;
  3956. end;
  3957. function tscannerfile.asmgetchar : char;
  3958. begin
  3959. if lastasmgetchar<>#0 then
  3960. begin
  3961. c:=lastasmgetchar;
  3962. lastasmgetchar:=#0;
  3963. end
  3964. else
  3965. readchar;
  3966. if in_asm_string then
  3967. begin
  3968. asmgetchar:=c;
  3969. exit;
  3970. end;
  3971. repeat
  3972. case c of
  3973. // the { ... } is used in ARM assembler to define register sets, so we can't used
  3974. // it as comment, either (* ... *), /* ... */ or // ... should be used instead.
  3975. // But compiler directives {$...} are allowed in ARM assembler.
  3976. '{' :
  3977. begin
  3978. {$ifdef arm}
  3979. readchar;
  3980. dec(inputpointer);
  3981. if c<>'$' then
  3982. begin
  3983. asmgetchar:='{';
  3984. exit;
  3985. end
  3986. else
  3987. {$endif arm}
  3988. skipcomment;
  3989. end;
  3990. #10,#13 :
  3991. begin
  3992. linebreak;
  3993. asmgetchar:=c;
  3994. exit;
  3995. end;
  3996. #26 :
  3997. begin
  3998. reload;
  3999. if (c=#26) and not assigned(inputfile.next) then
  4000. end_of_file;
  4001. continue;
  4002. end;
  4003. '/' :
  4004. begin
  4005. readchar;
  4006. if c='/' then
  4007. skipdelphicomment
  4008. else
  4009. begin
  4010. asmgetchar:='/';
  4011. lastasmgetchar:=c;
  4012. exit;
  4013. end;
  4014. end;
  4015. '(' :
  4016. begin
  4017. readchar;
  4018. if c='*' then
  4019. begin
  4020. c:=#0;{Signal skipoldtpcomment to reload a char }
  4021. skipoldtpcomment;
  4022. end
  4023. else
  4024. begin
  4025. asmgetchar:='(';
  4026. lastasmgetchar:=c;
  4027. exit;
  4028. end;
  4029. end;
  4030. else
  4031. begin
  4032. asmgetchar:=c;
  4033. exit;
  4034. end;
  4035. end;
  4036. until false;
  4037. end;
  4038. {*****************************************************************************
  4039. Helpers
  4040. *****************************************************************************}
  4041. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  4042. begin
  4043. if dm in [directive_all, directive_turbo] then
  4044. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  4045. if dm in [directive_all, directive_mac] then
  4046. tdirectiveitem.create(mac_scannerdirectives,s,p);
  4047. end;
  4048. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  4049. begin
  4050. if dm in [directive_all, directive_turbo] then
  4051. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  4052. if dm in [directive_all, directive_mac] then
  4053. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  4054. end;
  4055. {*****************************************************************************
  4056. Initialization
  4057. *****************************************************************************}
  4058. procedure InitScanner;
  4059. begin
  4060. InitWideString(patternw);
  4061. turbo_scannerdirectives:=TFPHashObjectList.Create;
  4062. mac_scannerdirectives:=TFPHashObjectList.Create;
  4063. { Common directives and conditionals }
  4064. AddDirective('I',directive_all, @dir_include);
  4065. AddDirective('DEFINE',directive_all, @dir_define);
  4066. AddDirective('UNDEF',directive_all, @dir_undef);
  4067. AddConditional('IF',directive_all, @dir_if);
  4068. AddConditional('IFDEF',directive_all, @dir_ifdef);
  4069. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  4070. AddConditional('ELSE',directive_all, @dir_else);
  4071. AddConditional('ELSEIF',directive_all, @dir_elseif);
  4072. AddConditional('ENDIF',directive_all, @dir_endif);
  4073. { Directives and conditionals for all modes except mode macpas}
  4074. AddDirective('INCLUDE',directive_turbo, @dir_include);
  4075. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  4076. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  4077. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  4078. AddConditional('IFEND',directive_turbo, @dir_endif);
  4079. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  4080. { Directives and conditionals for mode macpas: }
  4081. AddDirective('SETC',directive_mac, @dir_setc);
  4082. AddDirective('DEFINEC',directive_mac, @dir_definec);
  4083. AddDirective('UNDEFC',directive_mac, @dir_undef);
  4084. AddConditional('IFC',directive_mac, @dir_if);
  4085. AddConditional('ELSEC',directive_mac, @dir_else);
  4086. AddConditional('ELIFC',directive_mac, @dir_elseif);
  4087. AddConditional('ENDC',directive_mac, @dir_endif);
  4088. end;
  4089. procedure DoneScanner;
  4090. begin
  4091. turbo_scannerdirectives.Free;
  4092. mac_scannerdirectives.Free;
  4093. DoneWideString(patternw);
  4094. end;
  4095. end.