scanner.pas 144 KB

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