scanner.pas 141 KB

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