scanner.pas 143 KB

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