scanner.pas 141 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376
  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,_EQUAL,_UNEQUAL,_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. _EQUAL :
  1201. b:=l1=l2;
  1202. _UNEQUAL :
  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. _EQUAL :
  1218. b:=hs1=hs2;
  1219. _UNEQUAL :
  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. inputbuffer:=nil;
  1695. inputpointer:=nil;
  1696. inputstart:=0;
  1697. { reset scanner }
  1698. preprocstack:=nil;
  1699. replaystack:=nil;
  1700. comment_level:=0;
  1701. yylexcount:=0;
  1702. block_type:=bt_general;
  1703. line_no:=0;
  1704. lastlinepos:=0;
  1705. lasttokenpos:=0;
  1706. nexttokenpos:=0;
  1707. lasttoken:=NOTOKEN;
  1708. nexttoken:=NOTOKEN;
  1709. lastasmgetchar:=#0;
  1710. ignoredirectives:=TFPHashList.Create;
  1711. in_asm_string:=false;
  1712. end;
  1713. procedure tscannerfile.firstfile;
  1714. begin
  1715. { load block }
  1716. if not openinputfile then
  1717. Message1(scan_f_cannot_open_input,inputfile.name^);
  1718. reload;
  1719. end;
  1720. destructor tscannerfile.destroy;
  1721. begin
  1722. if assigned(current_module) and
  1723. (current_module.state=ms_compiled) and
  1724. (status.errorcount=0) then
  1725. checkpreprocstack
  1726. else
  1727. begin
  1728. while assigned(preprocstack) do
  1729. poppreprocstack;
  1730. end;
  1731. while assigned(replaystack) do
  1732. popreplaystack;
  1733. if not inputfile.closed then
  1734. closeinputfile;
  1735. ignoredirectives.free;
  1736. end;
  1737. function tscannerfile.openinputfile:boolean;
  1738. begin
  1739. openinputfile:=inputfile.open;
  1740. { load buffer }
  1741. inputbuffer:=inputfile.buf;
  1742. inputpointer:=inputfile.buf;
  1743. inputstart:=inputfile.bufstart;
  1744. { line }
  1745. line_no:=0;
  1746. lastlinepos:=0;
  1747. lasttokenpos:=0;
  1748. nexttokenpos:=0;
  1749. end;
  1750. procedure tscannerfile.closeinputfile;
  1751. begin
  1752. inputfile.close;
  1753. { reset buffer }
  1754. inputbuffer:=nil;
  1755. inputpointer:=nil;
  1756. inputstart:=0;
  1757. { reset line }
  1758. line_no:=0;
  1759. lastlinepos:=0;
  1760. lasttokenpos:=0;
  1761. nexttokenpos:=0;
  1762. end;
  1763. function tscannerfile.tempopeninputfile:boolean;
  1764. begin
  1765. if inputfile.is_macro then
  1766. exit;
  1767. tempopeninputfile:=inputfile.tempopen;
  1768. { reload buffer }
  1769. inputbuffer:=inputfile.buf;
  1770. inputpointer:=inputfile.buf;
  1771. inputstart:=inputfile.bufstart;
  1772. end;
  1773. procedure tscannerfile.tempcloseinputfile;
  1774. begin
  1775. if inputfile.closed or inputfile.is_macro then
  1776. exit;
  1777. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  1778. inputfile.tempclose;
  1779. { reset buffer }
  1780. inputbuffer:=nil;
  1781. inputpointer:=nil;
  1782. inputstart:=0;
  1783. end;
  1784. procedure tscannerfile.saveinputfile;
  1785. begin
  1786. inputfile.saveinputpointer:=inputpointer;
  1787. inputfile.savelastlinepos:=lastlinepos;
  1788. inputfile.saveline_no:=line_no;
  1789. end;
  1790. procedure tscannerfile.restoreinputfile;
  1791. begin
  1792. inputbuffer:=inputfile.buf;
  1793. inputpointer:=inputfile.saveinputpointer;
  1794. lastlinepos:=inputfile.savelastlinepos;
  1795. line_no:=inputfile.saveline_no;
  1796. if not inputfile.is_macro then
  1797. parser_current_file:=inputfile.name^;
  1798. end;
  1799. procedure tscannerfile.nextfile;
  1800. var
  1801. to_dispose : tinputfile;
  1802. begin
  1803. if assigned(inputfile.next) then
  1804. begin
  1805. if inputfile.is_macro then
  1806. to_dispose:=inputfile
  1807. else
  1808. begin
  1809. to_dispose:=nil;
  1810. dec(inputfilecount);
  1811. end;
  1812. { we can allways close the file, no ? }
  1813. inputfile.close;
  1814. inputfile:=inputfile.next;
  1815. if assigned(to_dispose) then
  1816. to_dispose.free;
  1817. restoreinputfile;
  1818. end;
  1819. end;
  1820. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  1821. begin
  1822. if not assigned(buf) then
  1823. internalerror(200511172);
  1824. if assigned(recordtokenbuf) then
  1825. internalerror(200511173);
  1826. recordtokenbuf:=buf;
  1827. fillchar(last_settings,sizeof(last_settings),0);
  1828. fillchar(last_filepos,sizeof(last_filepos),0);
  1829. end;
  1830. procedure tscannerfile.stoprecordtokens;
  1831. begin
  1832. if not assigned(recordtokenbuf) then
  1833. internalerror(200511174);
  1834. recordtokenbuf:=nil;
  1835. end;
  1836. procedure tscannerfile.recordtoken;
  1837. var
  1838. a : array[0..1] of byte;
  1839. len : sizeint;
  1840. begin
  1841. if not assigned(recordtokenbuf) then
  1842. internalerror(200511176);
  1843. { settings changed? }
  1844. if CompareByte(current_settings,last_settings,sizeof(current_settings))<>0 then
  1845. begin
  1846. { use a special token to record it }
  1847. a[0]:=byte(_GENERICSPECIALTOKEN);
  1848. a[1]:=byte(ST_LOADSETTINGS);
  1849. recordtokenbuf.write(a,2);
  1850. recordtokenbuf.write(current_settings,sizeof(current_settings));
  1851. last_settings:=current_settings;
  1852. end;
  1853. { file pos changes? }
  1854. if current_tokenpos.line<>last_filepos.line then
  1855. begin
  1856. a[0]:=byte(_GENERICSPECIALTOKEN);
  1857. a[1]:=byte(ST_LINE);
  1858. recordtokenbuf.write(a,2);
  1859. recordtokenbuf.write(current_tokenpos.line,sizeof(current_tokenpos.line));
  1860. last_filepos.line:=current_tokenpos.line;
  1861. end;
  1862. if current_tokenpos.column<>last_filepos.column then
  1863. begin
  1864. a[0]:=byte(_GENERICSPECIALTOKEN);
  1865. a[1]:=byte(ST_COLUMN);
  1866. recordtokenbuf.write(a,2);
  1867. recordtokenbuf.write(current_tokenpos.column,sizeof(current_tokenpos.column));
  1868. last_filepos.column:=current_tokenpos.column;
  1869. end;
  1870. if current_tokenpos.fileindex<>last_filepos.fileindex then
  1871. begin
  1872. a[0]:=byte(_GENERICSPECIALTOKEN);
  1873. a[1]:=byte(ST_FILEINDEX);
  1874. recordtokenbuf.write(a,2);
  1875. recordtokenbuf.write(current_tokenpos.fileindex,sizeof(current_tokenpos.fileindex));
  1876. last_filepos.fileindex:=current_tokenpos.fileindex;
  1877. end;
  1878. recordtokenbuf.write(token,1);
  1879. if token=_ID then
  1880. recordtokenbuf.write(idtoken,1);
  1881. case token of
  1882. _CWCHAR,
  1883. _CWSTRING :
  1884. begin
  1885. recordtokenbuf.write(patternw^.len,sizeof(sizeint));
  1886. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  1887. end;
  1888. _CSTRING:
  1889. begin
  1890. len:=length(cstringpattern);
  1891. recordtokenbuf.write(len,sizeof(sizeint));
  1892. recordtokenbuf.write(cstringpattern[1],length(cstringpattern));
  1893. end;
  1894. _CCHAR,
  1895. _INTCONST,
  1896. _REALNUMBER :
  1897. begin
  1898. { pexpr.pas messes with pattern in case of negative integer consts,
  1899. see around line 2562 the comment of JM; remove the - before recording it
  1900. (FK)
  1901. }
  1902. if (token=_INTCONST) and (pattern[1]='-') then
  1903. delete(pattern,1,1);
  1904. recordtokenbuf.write(pattern[0],1);
  1905. recordtokenbuf.write(pattern[1],length(pattern));
  1906. end;
  1907. _ID :
  1908. begin
  1909. recordtokenbuf.write(orgpattern[0],1);
  1910. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  1911. end;
  1912. end;
  1913. end;
  1914. procedure tscannerfile.startreplaytokens(buf:tdynamicarray);
  1915. begin
  1916. if not assigned(buf) then
  1917. internalerror(200511175);
  1918. { save current token }
  1919. if token in [_CWCHAR,_CWSTRING,_CCHAR,_CSTRING,_INTCONST,_REALNUMBER,_ID] then
  1920. internalerror(200511178);
  1921. replaystack:=treplaystack.create(token,current_settings,replaytokenbuf,replaystack);
  1922. if assigned(inputpointer) then
  1923. dec(inputpointer);
  1924. { install buffer }
  1925. replaytokenbuf:=buf;
  1926. { reload next token }
  1927. replaytokenbuf.seek(0);
  1928. replaytoken;
  1929. end;
  1930. procedure tscannerfile.replaytoken;
  1931. var
  1932. wlen : sizeint;
  1933. specialtoken : tspecialgenerictoken;
  1934. begin
  1935. if not assigned(replaytokenbuf) then
  1936. internalerror(200511177);
  1937. { End of replay buffer? Then load the next char from the file again }
  1938. if replaytokenbuf.pos>=replaytokenbuf.size then
  1939. begin
  1940. token:=replaystack.token;
  1941. replaytokenbuf:=replaystack.tokenbuf;
  1942. { restore compiler settings }
  1943. current_settings:=replaystack.settings;
  1944. popreplaystack;
  1945. if assigned(inputpointer) then
  1946. begin
  1947. c:=inputpointer^;
  1948. inc(inputpointer);
  1949. end;
  1950. exit;
  1951. end;
  1952. repeat
  1953. { load token from the buffer }
  1954. replaytokenbuf.read(token,1);
  1955. if token=_ID then
  1956. replaytokenbuf.read(idtoken,1)
  1957. else
  1958. idtoken:=_NOID;
  1959. case token of
  1960. _CWCHAR,
  1961. _CWSTRING :
  1962. begin
  1963. replaytokenbuf.read(wlen,sizeof(SizeInt));
  1964. setlengthwidestring(patternw,wlen);
  1965. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  1966. orgpattern:='';
  1967. pattern:='';
  1968. cstringpattern:='';
  1969. end;
  1970. _CSTRING:
  1971. begin
  1972. replaytokenbuf.read(wlen,sizeof(sizeint));
  1973. setlength(cstringpattern,wlen);
  1974. replaytokenbuf.read(cstringpattern[1],wlen);
  1975. orgpattern:='';
  1976. pattern:='';
  1977. end;
  1978. _CCHAR,
  1979. _INTCONST,
  1980. _REALNUMBER :
  1981. begin
  1982. replaytokenbuf.read(pattern[0],1);
  1983. replaytokenbuf.read(pattern[1],length(pattern));
  1984. orgpattern:='';
  1985. end;
  1986. _ID :
  1987. begin
  1988. replaytokenbuf.read(orgpattern[0],1);
  1989. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  1990. pattern:=upper(orgpattern);
  1991. end;
  1992. _GENERICSPECIALTOKEN:
  1993. begin
  1994. replaytokenbuf.read(specialtoken,1);
  1995. case specialtoken of
  1996. ST_LOADSETTINGS:
  1997. replaytokenbuf.read(current_settings,sizeof(current_settings));
  1998. ST_LINE:
  1999. begin
  2000. replaytokenbuf.read(current_tokenpos.line,sizeof(current_tokenpos.line));
  2001. { don't generate invalid line info if no sources are available for the current module }
  2002. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2003. current_tokenpos.line:=0;
  2004. current_filepos:=current_tokenpos;
  2005. end;
  2006. ST_COLUMN:
  2007. begin
  2008. replaytokenbuf.read(current_tokenpos.column,sizeof(current_tokenpos.column));
  2009. { don't generate invalid line info if no sources are available for the current module }
  2010. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2011. current_tokenpos.column:=0;
  2012. current_filepos:=current_tokenpos;
  2013. end;
  2014. ST_FILEINDEX:
  2015. begin
  2016. replaytokenbuf.read(current_tokenpos.fileindex,sizeof(current_tokenpos.fileindex));
  2017. { don't generate invalid line info if no sources are available for the current module }
  2018. if not(get_module(current_filepos.moduleindex).sources_avail) then
  2019. begin
  2020. current_tokenpos.column:=0;
  2021. current_tokenpos.line:=0;
  2022. end;
  2023. current_filepos:=current_tokenpos;
  2024. end;
  2025. else
  2026. internalerror(2006103010);
  2027. end;
  2028. continue;
  2029. end;
  2030. end;
  2031. break;
  2032. until false;
  2033. end;
  2034. procedure tscannerfile.addfile(hp:tinputfile);
  2035. begin
  2036. saveinputfile;
  2037. { add to list }
  2038. hp.next:=inputfile;
  2039. inputfile:=hp;
  2040. { load new inputfile }
  2041. restoreinputfile;
  2042. end;
  2043. procedure tscannerfile.reload;
  2044. begin
  2045. with inputfile do
  2046. begin
  2047. { when nothing more to read then leave immediatly, so we
  2048. don't change the current_filepos and leave it point to the last
  2049. char }
  2050. if (c=#26) and (not assigned(next)) then
  2051. exit;
  2052. repeat
  2053. { still more to read?, then change the #0 to a space so its seen
  2054. as a seperator, this can't be used for macro's which can change
  2055. the place of the #0 in the buffer with tempopen }
  2056. if (c=#0) and (bufsize>0) and
  2057. not(inputfile.is_macro) and
  2058. (inputpointer-inputbuffer<bufsize) then
  2059. begin
  2060. c:=' ';
  2061. inc(inputpointer);
  2062. exit;
  2063. end;
  2064. { can we read more from this file ? }
  2065. if (c<>#26) and (not endoffile) then
  2066. begin
  2067. readbuf;
  2068. inputpointer:=buf;
  2069. inputbuffer:=buf;
  2070. inputstart:=bufstart;
  2071. { first line? }
  2072. if line_no=0 then
  2073. begin
  2074. c:=inputpointer^;
  2075. { eat utf-8 signature? }
  2076. if (ord(inputpointer^)=$ef) and
  2077. (ord((inputpointer+1)^)=$bb) and
  2078. (ord((inputpointer+2)^)=$bf) then
  2079. begin
  2080. inc(inputpointer,3);
  2081. message(scan_c_switching_to_utf8);
  2082. current_settings.sourcecodepage:='utf8';
  2083. end;
  2084. line_no:=1;
  2085. if cs_asm_source in current_settings.globalswitches then
  2086. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  2087. end;
  2088. end
  2089. else
  2090. begin
  2091. { load eof position in tokenpos/current_filepos }
  2092. gettokenpos;
  2093. { close file }
  2094. closeinputfile;
  2095. { no next module, than EOF }
  2096. if not assigned(inputfile.next) then
  2097. begin
  2098. c:=#26;
  2099. exit;
  2100. end;
  2101. { load next file and reopen it }
  2102. nextfile;
  2103. tempopeninputfile;
  2104. { status }
  2105. Message1(scan_t_back_in,inputfile.name^);
  2106. end;
  2107. { load next char }
  2108. c:=inputpointer^;
  2109. inc(inputpointer);
  2110. until c<>#0; { if also end, then reload again }
  2111. end;
  2112. end;
  2113. procedure tscannerfile.insertmacro(const macname:string;p:pchar;len,line,fileindex:longint);
  2114. var
  2115. hp : tinputfile;
  2116. begin
  2117. { save old postion }
  2118. dec(inputpointer);
  2119. tempcloseinputfile;
  2120. { create macro 'file' }
  2121. { use special name to dispose after !! }
  2122. hp:=do_openinputfile('_Macro_.'+macname);
  2123. addfile(hp);
  2124. with inputfile do
  2125. begin
  2126. setmacro(p,len);
  2127. { local buffer }
  2128. inputbuffer:=buf;
  2129. inputpointer:=buf;
  2130. inputstart:=bufstart;
  2131. ref_index:=fileindex;
  2132. end;
  2133. { reset line }
  2134. line_no:=line;
  2135. lastlinepos:=0;
  2136. lasttokenpos:=0;
  2137. nexttokenpos:=0;
  2138. { load new c }
  2139. c:=inputpointer^;
  2140. inc(inputpointer);
  2141. end;
  2142. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  2143. begin
  2144. tokenpos:=inputstart+(inputpointer-inputbuffer);
  2145. filepos.line:=line_no;
  2146. filepos.column:=tokenpos-lastlinepos;
  2147. filepos.fileindex:=inputfile.ref_index;
  2148. filepos.moduleindex:=current_module.unit_index;
  2149. end;
  2150. procedure tscannerfile.gettokenpos;
  2151. { load the values of tokenpos and lasttokenpos }
  2152. begin
  2153. do_gettokenpos(lasttokenpos,current_tokenpos);
  2154. current_filepos:=current_tokenpos;
  2155. end;
  2156. procedure tscannerfile.cachenexttokenpos;
  2157. begin
  2158. do_gettokenpos(nexttokenpos,next_filepos);
  2159. end;
  2160. procedure tscannerfile.setnexttoken;
  2161. begin
  2162. token:=nexttoken;
  2163. nexttoken:=NOTOKEN;
  2164. lasttokenpos:=nexttokenpos;
  2165. current_tokenpos:=next_filepos;
  2166. current_filepos:=current_tokenpos;
  2167. nexttokenpos:=0;
  2168. end;
  2169. procedure tscannerfile.savetokenpos;
  2170. begin
  2171. oldlasttokenpos:=lasttokenpos;
  2172. oldcurrent_filepos:=current_filepos;
  2173. oldcurrent_tokenpos:=current_tokenpos;
  2174. end;
  2175. procedure tscannerfile.restoretokenpos;
  2176. begin
  2177. lasttokenpos:=oldlasttokenpos;
  2178. current_filepos:=oldcurrent_filepos;
  2179. current_tokenpos:=oldcurrent_tokenpos;
  2180. end;
  2181. procedure tscannerfile.inc_comment_level;
  2182. begin
  2183. if (m_nested_comment in current_settings.modeswitches) then
  2184. inc(comment_level)
  2185. else
  2186. comment_level:=1;
  2187. if (comment_level>1) then
  2188. begin
  2189. savetokenpos;
  2190. gettokenpos; { update for warning }
  2191. Message1(scan_w_comment_level,tostr(comment_level));
  2192. restoretokenpos;
  2193. end;
  2194. end;
  2195. procedure tscannerfile.dec_comment_level;
  2196. begin
  2197. if (m_nested_comment in current_settings.modeswitches) then
  2198. dec(comment_level)
  2199. else
  2200. comment_level:=0;
  2201. end;
  2202. procedure tscannerfile.linebreak;
  2203. var
  2204. cur : char;
  2205. begin
  2206. with inputfile do
  2207. begin
  2208. if (byte(inputpointer^)=0) and not(endoffile) then
  2209. begin
  2210. cur:=c;
  2211. reload;
  2212. if byte(cur)+byte(c)<>23 then
  2213. dec(inputpointer);
  2214. end
  2215. else
  2216. begin
  2217. { Support all combination of #10 and #13 as line break }
  2218. if (byte(inputpointer^)+byte(c)=23) then
  2219. inc(inputpointer);
  2220. end;
  2221. { Always return #10 as line break }
  2222. c:=#10;
  2223. { increase line counters }
  2224. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  2225. inc(line_no);
  2226. { update linebuffer }
  2227. if cs_asm_source in current_settings.globalswitches then
  2228. inputfile.setline(line_no,lastlinepos);
  2229. { update for status and call the show status routine,
  2230. but don't touch current_filepos ! }
  2231. savetokenpos;
  2232. gettokenpos; { update for v_status }
  2233. inc(status.compiledlines);
  2234. ShowStatus;
  2235. restoretokenpos;
  2236. end;
  2237. end;
  2238. procedure tscannerfile.illegal_char(c:char);
  2239. var
  2240. s : string;
  2241. begin
  2242. if c in [#32..#255] then
  2243. s:=''''+c+''''
  2244. else
  2245. s:='#'+tostr(ord(c));
  2246. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  2247. end;
  2248. procedure tscannerfile.end_of_file;
  2249. begin
  2250. checkpreprocstack;
  2251. Message(scan_f_end_of_file);
  2252. end;
  2253. {-------------------------------------------
  2254. IF Conditional Handling
  2255. -------------------------------------------}
  2256. procedure tscannerfile.checkpreprocstack;
  2257. begin
  2258. { check for missing ifdefs }
  2259. while assigned(preprocstack) do
  2260. begin
  2261. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  2262. preprocstack.owner.inputfile.name^,tostr(preprocstack.line_nb));
  2263. poppreprocstack;
  2264. end;
  2265. end;
  2266. procedure tscannerfile.poppreprocstack;
  2267. var
  2268. hp : tpreprocstack;
  2269. begin
  2270. if assigned(preprocstack) then
  2271. begin
  2272. Message1(scan_c_endif_found,preprocstack.name);
  2273. hp:=preprocstack.next;
  2274. preprocstack.free;
  2275. preprocstack:=hp;
  2276. end
  2277. else
  2278. Message(scan_e_endif_without_if);
  2279. end;
  2280. procedure tscannerfile.ifpreprocstack(atyp : preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  2281. var
  2282. condition: Boolean;
  2283. valuedescr: String;
  2284. begin
  2285. if (preprocstack=nil) or preprocstack.accept then
  2286. condition:= compile_time_predicate(valuedescr)
  2287. else
  2288. begin
  2289. condition:= false;
  2290. valuedescr:= '';
  2291. end;
  2292. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  2293. preprocstack.name:=valuedescr;
  2294. preprocstack.line_nb:=line_no;
  2295. preprocstack.owner:=self;
  2296. if preprocstack.accept then
  2297. Message2(messid,preprocstack.name,'accepted')
  2298. else
  2299. Message2(messid,preprocstack.name,'rejected');
  2300. end;
  2301. procedure tscannerfile.elsepreprocstack;
  2302. begin
  2303. if assigned(preprocstack) and
  2304. (preprocstack.typ<>pp_else) then
  2305. begin
  2306. if (preprocstack.typ=pp_elseif) then
  2307. preprocstack.accept:=false
  2308. else
  2309. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  2310. preprocstack.accept:=not preprocstack.accept;
  2311. preprocstack.typ:=pp_else;
  2312. preprocstack.line_nb:=line_no;
  2313. if preprocstack.accept then
  2314. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2315. else
  2316. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2317. end
  2318. else
  2319. Message(scan_e_endif_without_if);
  2320. end;
  2321. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  2322. var
  2323. valuedescr: String;
  2324. begin
  2325. if assigned(preprocstack) and
  2326. (preprocstack.typ in [pp_if,pp_elseif]) then
  2327. begin
  2328. { when the branch is accepted we use pp_elseif so we know that
  2329. all the next branches need to be rejected. when this branch is still
  2330. not accepted then leave it at pp_if }
  2331. if (preprocstack.typ=pp_elseif) then
  2332. preprocstack.accept:=false
  2333. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  2334. begin
  2335. preprocstack.accept:=false;
  2336. preprocstack.typ:=pp_elseif;
  2337. end
  2338. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  2339. and compile_time_predicate(valuedescr) then
  2340. begin
  2341. preprocstack.name:=valuedescr;
  2342. preprocstack.accept:=true;
  2343. preprocstack.typ:=pp_elseif;
  2344. end;
  2345. preprocstack.line_nb:=line_no;
  2346. if preprocstack.accept then
  2347. Message2(scan_c_else_found,preprocstack.name,'accepted')
  2348. else
  2349. Message2(scan_c_else_found,preprocstack.name,'rejected');
  2350. end
  2351. else
  2352. Message(scan_e_endif_without_if);
  2353. end;
  2354. procedure tscannerfile.popreplaystack;
  2355. var
  2356. hp : treplaystack;
  2357. begin
  2358. if assigned(replaystack) then
  2359. begin
  2360. hp:=replaystack.next;
  2361. replaystack.free;
  2362. replaystack:=hp;
  2363. end;
  2364. end;
  2365. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  2366. begin
  2367. savetokenpos;
  2368. repeat
  2369. current_scanner.gettokenpos;
  2370. p.proc();
  2371. { accept the text ? }
  2372. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  2373. break
  2374. else
  2375. begin
  2376. current_scanner.gettokenpos;
  2377. Message(scan_c_skipping_until);
  2378. repeat
  2379. current_scanner.skipuntildirective;
  2380. if not (m_mac in current_settings.modeswitches) then
  2381. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  2382. else
  2383. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  2384. until assigned(p) and (p.is_conditional);
  2385. current_scanner.gettokenpos;
  2386. Message1(scan_d_handling_switch,'$'+p.name);
  2387. end;
  2388. until false;
  2389. restoretokenpos;
  2390. end;
  2391. procedure tscannerfile.handledirectives;
  2392. var
  2393. t : tdirectiveitem;
  2394. hs : string;
  2395. begin
  2396. gettokenpos;
  2397. readchar; {Remove the $}
  2398. hs:=readid;
  2399. { handle empty directive }
  2400. if hs='' then
  2401. begin
  2402. Message1(scan_w_illegal_switch,'$');
  2403. exit;
  2404. end;
  2405. {$ifdef PREPROCWRITE}
  2406. if parapreprocess then
  2407. begin
  2408. t:=Get_Directive(hs);
  2409. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  2410. begin
  2411. preprocfile^.AddSpace;
  2412. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  2413. exit;
  2414. end;
  2415. end;
  2416. {$endif PREPROCWRITE}
  2417. { skip this directive? }
  2418. if (ignoredirectives.find(hs)<>nil) then
  2419. begin
  2420. if (comment_level>0) then
  2421. readcomment;
  2422. { we've read the whole comment }
  2423. aktcommentstyle:=comment_none;
  2424. exit;
  2425. end;
  2426. { Check for compiler switches }
  2427. while (length(hs)=1) and (c in ['-','+']) do
  2428. begin
  2429. HandleSwitch(hs[1],c);
  2430. current_scanner.readchar; {Remove + or -}
  2431. if c=',' then
  2432. begin
  2433. current_scanner.readchar; {Remove , }
  2434. { read next switch, support $v+,$+}
  2435. hs:=current_scanner.readid;
  2436. if (hs='') then
  2437. begin
  2438. if (c='$') and (m_fpc in current_settings.modeswitches) then
  2439. begin
  2440. current_scanner.readchar; { skip $ }
  2441. hs:=current_scanner.readid;
  2442. end;
  2443. if (hs='') then
  2444. Message1(scan_w_illegal_directive,'$'+c);
  2445. end
  2446. else
  2447. Message1(scan_d_handling_switch,'$'+hs);
  2448. end
  2449. else
  2450. hs:='';
  2451. end;
  2452. { directives may follow switches after a , }
  2453. if hs<>'' then
  2454. begin
  2455. if not (m_mac in current_settings.modeswitches) then
  2456. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  2457. else
  2458. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  2459. if assigned(t) then
  2460. begin
  2461. if t.is_conditional then
  2462. handleconditional(t)
  2463. else
  2464. begin
  2465. Message1(scan_d_handling_switch,'$'+hs);
  2466. t.proc();
  2467. end;
  2468. end
  2469. else
  2470. begin
  2471. current_scanner.ignoredirectives.Add(hs,nil);
  2472. Message1(scan_w_illegal_directive,'$'+hs);
  2473. end;
  2474. { conditionals already read the comment }
  2475. if (current_scanner.comment_level>0) then
  2476. current_scanner.readcomment;
  2477. { we've read the whole comment }
  2478. aktcommentstyle:=comment_none;
  2479. end;
  2480. end;
  2481. procedure tscannerfile.readchar;
  2482. begin
  2483. c:=inputpointer^;
  2484. if c=#0 then
  2485. reload
  2486. else
  2487. inc(inputpointer);
  2488. end;
  2489. procedure tscannerfile.readstring;
  2490. var
  2491. i : longint;
  2492. err : boolean;
  2493. begin
  2494. err:=false;
  2495. i:=0;
  2496. repeat
  2497. case c of
  2498. '_',
  2499. '0'..'9',
  2500. 'A'..'Z' :
  2501. begin
  2502. if i<255 then
  2503. begin
  2504. inc(i);
  2505. orgpattern[i]:=c;
  2506. pattern[i]:=c;
  2507. end
  2508. else
  2509. begin
  2510. if not err then
  2511. begin
  2512. Message(scan_e_string_exceeds_255_chars);
  2513. err:=true;
  2514. end;
  2515. end;
  2516. c:=inputpointer^;
  2517. inc(inputpointer);
  2518. end;
  2519. 'a'..'z' :
  2520. begin
  2521. if i<255 then
  2522. begin
  2523. inc(i);
  2524. orgpattern[i]:=c;
  2525. pattern[i]:=chr(ord(c)-32)
  2526. end
  2527. else
  2528. begin
  2529. if not err then
  2530. begin
  2531. Message(scan_e_string_exceeds_255_chars);
  2532. err:=true;
  2533. end;
  2534. end;
  2535. c:=inputpointer^;
  2536. inc(inputpointer);
  2537. end;
  2538. #0 :
  2539. reload;
  2540. else
  2541. break;
  2542. end;
  2543. until false;
  2544. orgpattern[0]:=chr(i);
  2545. pattern[0]:=chr(i);
  2546. end;
  2547. procedure tscannerfile.readnumber;
  2548. var
  2549. base,
  2550. i : longint;
  2551. begin
  2552. case c of
  2553. '%' :
  2554. begin
  2555. readchar;
  2556. base:=2;
  2557. pattern[1]:='%';
  2558. i:=1;
  2559. end;
  2560. '&' :
  2561. begin
  2562. readchar;
  2563. base:=8;
  2564. pattern[1]:='&';
  2565. i:=1;
  2566. end;
  2567. '$' :
  2568. begin
  2569. readchar;
  2570. base:=16;
  2571. pattern[1]:='$';
  2572. i:=1;
  2573. end;
  2574. else
  2575. begin
  2576. base:=10;
  2577. i:=0;
  2578. end;
  2579. end;
  2580. while ((base>=10) and (c in ['0'..'9'])) or
  2581. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  2582. ((base=8) and (c in ['0'..'7'])) or
  2583. ((base=2) and (c in ['0'..'1'])) do
  2584. begin
  2585. if i<255 then
  2586. begin
  2587. inc(i);
  2588. pattern[i]:=c;
  2589. end;
  2590. readchar;
  2591. end;
  2592. pattern[0]:=chr(i);
  2593. end;
  2594. function tscannerfile.readid:string;
  2595. begin
  2596. readstring;
  2597. readid:=pattern;
  2598. end;
  2599. function tscannerfile.readval:longint;
  2600. var
  2601. l : longint;
  2602. w : integer;
  2603. begin
  2604. readnumber;
  2605. val(pattern,l,w);
  2606. readval:=l;
  2607. end;
  2608. function tscannerfile.readval_asstring:string;
  2609. begin
  2610. readnumber;
  2611. readval_asstring:=pattern;
  2612. end;
  2613. function tscannerfile.readcomment:string;
  2614. var
  2615. i : longint;
  2616. begin
  2617. i:=0;
  2618. repeat
  2619. case c of
  2620. '{' :
  2621. begin
  2622. if aktcommentstyle=comment_tp then
  2623. inc_comment_level;
  2624. end;
  2625. '}' :
  2626. begin
  2627. if aktcommentstyle=comment_tp then
  2628. begin
  2629. readchar;
  2630. dec_comment_level;
  2631. if comment_level=0 then
  2632. break
  2633. else
  2634. continue;
  2635. end;
  2636. end;
  2637. '*' :
  2638. begin
  2639. if aktcommentstyle=comment_oldtp then
  2640. begin
  2641. readchar;
  2642. if c=')' then
  2643. begin
  2644. readchar;
  2645. dec_comment_level;
  2646. break;
  2647. end
  2648. else
  2649. { Add both characters !!}
  2650. if (i<255) then
  2651. begin
  2652. inc(i);
  2653. readcomment[i]:='*';
  2654. if (i<255) then
  2655. begin
  2656. inc(i);
  2657. readcomment[i]:=c;
  2658. end;
  2659. end;
  2660. end
  2661. else
  2662. { Not old TP comment, so add...}
  2663. begin
  2664. if (i<255) then
  2665. begin
  2666. inc(i);
  2667. readcomment[i]:='*';
  2668. end;
  2669. end;
  2670. end;
  2671. #10,#13 :
  2672. linebreak;
  2673. #26 :
  2674. end_of_file;
  2675. else
  2676. begin
  2677. if (i<255) then
  2678. begin
  2679. inc(i);
  2680. readcomment[i]:=c;
  2681. end;
  2682. end;
  2683. end;
  2684. readchar;
  2685. until false;
  2686. readcomment[0]:=chr(i);
  2687. end;
  2688. function tscannerfile.readquotedstring:string;
  2689. var
  2690. i : longint;
  2691. msgwritten : boolean;
  2692. begin
  2693. i:=0;
  2694. msgwritten:=false;
  2695. if (c='''') then
  2696. begin
  2697. repeat
  2698. readchar;
  2699. case c of
  2700. #26 :
  2701. end_of_file;
  2702. #10,#13 :
  2703. Message(scan_f_string_exceeds_line);
  2704. '''' :
  2705. begin
  2706. readchar;
  2707. if c<>'''' then
  2708. break;
  2709. end;
  2710. end;
  2711. if i<255 then
  2712. begin
  2713. inc(i);
  2714. result[i]:=c;
  2715. end
  2716. else
  2717. begin
  2718. if not msgwritten then
  2719. begin
  2720. Message(scan_e_string_exceeds_255_chars);
  2721. msgwritten:=true;
  2722. end;
  2723. end;
  2724. until false;
  2725. end;
  2726. result[0]:=chr(i);
  2727. end;
  2728. function tscannerfile.readstate:char;
  2729. var
  2730. state : char;
  2731. begin
  2732. state:=' ';
  2733. if c=' ' then
  2734. begin
  2735. current_scanner.skipspace;
  2736. current_scanner.readid;
  2737. if pattern='ON' then
  2738. state:='+'
  2739. else
  2740. if pattern='OFF' then
  2741. state:='-';
  2742. end
  2743. else
  2744. state:=c;
  2745. if not (state in ['+','-']) then
  2746. Message(scan_e_wrong_switch_toggle);
  2747. readstate:=state;
  2748. end;
  2749. function tscannerfile.readstatedefault:char;
  2750. var
  2751. state : char;
  2752. begin
  2753. state:=' ';
  2754. if c=' ' then
  2755. begin
  2756. current_scanner.skipspace;
  2757. current_scanner.readid;
  2758. if pattern='ON' then
  2759. state:='+'
  2760. else
  2761. if pattern='OFF' then
  2762. state:='-'
  2763. else
  2764. if pattern='DEFAULT' then
  2765. state:='*';
  2766. end
  2767. else
  2768. state:=c;
  2769. if not (state in ['+','-','*']) then
  2770. Message(scan_e_wrong_switch_toggle_default);
  2771. readstatedefault:=state;
  2772. end;
  2773. procedure tscannerfile.skipspace;
  2774. begin
  2775. repeat
  2776. case c of
  2777. #26 :
  2778. begin
  2779. reload;
  2780. if (c=#26) and not assigned(inputfile.next) then
  2781. break;
  2782. continue;
  2783. end;
  2784. #10,
  2785. #13 :
  2786. linebreak;
  2787. #9,#11,#12,' ' :
  2788. ;
  2789. else
  2790. break;
  2791. end;
  2792. readchar;
  2793. until false;
  2794. end;
  2795. procedure tscannerfile.skipuntildirective;
  2796. var
  2797. found : longint;
  2798. next_char_loaded : boolean;
  2799. begin
  2800. found:=0;
  2801. next_char_loaded:=false;
  2802. repeat
  2803. case c of
  2804. #10,
  2805. #13 :
  2806. linebreak;
  2807. #26 :
  2808. begin
  2809. reload;
  2810. if (c=#26) and not assigned(inputfile.next) then
  2811. end_of_file;
  2812. continue;
  2813. end;
  2814. '{' :
  2815. begin
  2816. if (aktcommentstyle in [comment_tp,comment_none]) then
  2817. begin
  2818. aktcommentstyle:=comment_tp;
  2819. if (comment_level=0) then
  2820. found:=1;
  2821. inc_comment_level;
  2822. end;
  2823. end;
  2824. '*' :
  2825. begin
  2826. if (aktcommentstyle=comment_oldtp) then
  2827. begin
  2828. readchar;
  2829. if c=')' then
  2830. begin
  2831. dec_comment_level;
  2832. found:=0;
  2833. aktcommentstyle:=comment_none;
  2834. end
  2835. else
  2836. next_char_loaded:=true;
  2837. end
  2838. else
  2839. found := 0;
  2840. end;
  2841. '}' :
  2842. begin
  2843. if (aktcommentstyle=comment_tp) then
  2844. begin
  2845. dec_comment_level;
  2846. if (comment_level=0) then
  2847. aktcommentstyle:=comment_none;
  2848. found:=0;
  2849. end;
  2850. end;
  2851. '$' :
  2852. begin
  2853. if found=1 then
  2854. found:=2;
  2855. end;
  2856. '''' :
  2857. if (aktcommentstyle=comment_none) then
  2858. begin
  2859. repeat
  2860. readchar;
  2861. case c of
  2862. #26 :
  2863. end_of_file;
  2864. #10,#13 :
  2865. break;
  2866. '''' :
  2867. begin
  2868. readchar;
  2869. if c<>'''' then
  2870. begin
  2871. next_char_loaded:=true;
  2872. break;
  2873. end;
  2874. end;
  2875. end;
  2876. until false;
  2877. end;
  2878. '(' :
  2879. begin
  2880. if (aktcommentstyle=comment_none) then
  2881. begin
  2882. readchar;
  2883. if c='*' then
  2884. begin
  2885. readchar;
  2886. if c='$' then
  2887. begin
  2888. found:=2;
  2889. inc_comment_level;
  2890. aktcommentstyle:=comment_oldtp;
  2891. end
  2892. else
  2893. begin
  2894. skipoldtpcomment;
  2895. next_char_loaded:=true;
  2896. end;
  2897. end
  2898. else
  2899. next_char_loaded:=true;
  2900. end
  2901. else
  2902. found:=0;
  2903. end;
  2904. '/' :
  2905. begin
  2906. if (aktcommentstyle=comment_none) then
  2907. begin
  2908. readchar;
  2909. if c='/' then
  2910. skipdelphicomment;
  2911. next_char_loaded:=true;
  2912. end
  2913. else
  2914. found:=0;
  2915. end;
  2916. else
  2917. found:=0;
  2918. end;
  2919. if next_char_loaded then
  2920. next_char_loaded:=false
  2921. else
  2922. readchar;
  2923. until (found=2);
  2924. end;
  2925. {****************************************************************************
  2926. Comment Handling
  2927. ****************************************************************************}
  2928. procedure tscannerfile.skipcomment;
  2929. begin
  2930. aktcommentstyle:=comment_tp;
  2931. readchar;
  2932. inc_comment_level;
  2933. { handle compiler switches }
  2934. if (c='$') then
  2935. handledirectives;
  2936. { handle_switches can dec comment_level, }
  2937. while (comment_level>0) do
  2938. begin
  2939. case c of
  2940. '{' :
  2941. inc_comment_level;
  2942. '}' :
  2943. dec_comment_level;
  2944. #10,#13 :
  2945. linebreak;
  2946. #26 :
  2947. begin
  2948. reload;
  2949. if (c=#26) and not assigned(inputfile.next) then
  2950. end_of_file;
  2951. continue;
  2952. end;
  2953. end;
  2954. readchar;
  2955. end;
  2956. aktcommentstyle:=comment_none;
  2957. end;
  2958. procedure tscannerfile.skipdelphicomment;
  2959. begin
  2960. aktcommentstyle:=comment_delphi;
  2961. inc_comment_level;
  2962. readchar;
  2963. { this is not supported }
  2964. if c='$' then
  2965. Message(scan_w_wrong_styled_switch);
  2966. { skip comment }
  2967. while not (c in [#10,#13,#26]) do
  2968. readchar;
  2969. dec_comment_level;
  2970. aktcommentstyle:=comment_none;
  2971. end;
  2972. procedure tscannerfile.skipoldtpcomment;
  2973. var
  2974. found : longint;
  2975. begin
  2976. aktcommentstyle:=comment_oldtp;
  2977. inc_comment_level;
  2978. { only load a char if last already processed,
  2979. was cause of bug1634 PM }
  2980. if c=#0 then
  2981. readchar;
  2982. { this is now supported }
  2983. if (c='$') then
  2984. handledirectives;
  2985. { skip comment }
  2986. while (comment_level>0) do
  2987. begin
  2988. found:=0;
  2989. repeat
  2990. case c of
  2991. #26 :
  2992. begin
  2993. reload;
  2994. if (c=#26) and not assigned(inputfile.next) then
  2995. end_of_file;
  2996. continue;
  2997. end;
  2998. #10,#13 :
  2999. begin
  3000. if found=4 then
  3001. inc_comment_level;
  3002. linebreak;
  3003. found:=0;
  3004. end;
  3005. '*' :
  3006. begin
  3007. if found=3 then
  3008. found:=4
  3009. else
  3010. found:=1;
  3011. end;
  3012. ')' :
  3013. begin
  3014. if found in [1,4] then
  3015. begin
  3016. dec_comment_level;
  3017. if comment_level=0 then
  3018. found:=2
  3019. else
  3020. found:=0;
  3021. end
  3022. else
  3023. found:=0;
  3024. end;
  3025. '(' :
  3026. begin
  3027. if found=4 then
  3028. inc_comment_level;
  3029. found:=3;
  3030. end;
  3031. else
  3032. begin
  3033. if found=4 then
  3034. inc_comment_level;
  3035. found:=0;
  3036. end;
  3037. end;
  3038. readchar;
  3039. until (found=2);
  3040. end;
  3041. aktcommentstyle:=comment_none;
  3042. end;
  3043. {****************************************************************************
  3044. Token Scanner
  3045. ****************************************************************************}
  3046. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  3047. var
  3048. code : integer;
  3049. len,
  3050. low,high,mid : longint;
  3051. w : word;
  3052. m : longint;
  3053. mac : tmacro;
  3054. asciinr : string[6];
  3055. iswidestring : boolean;
  3056. label
  3057. exit_label;
  3058. begin
  3059. flushpendingswitchesstate;
  3060. { record tokens? }
  3061. if allowrecordtoken and
  3062. assigned(recordtokenbuf) then
  3063. recordtoken;
  3064. { replay tokens? }
  3065. if assigned(replaytokenbuf) then
  3066. begin
  3067. replaytoken;
  3068. goto exit_label;
  3069. end;
  3070. { was there already a token read, then return that token }
  3071. if nexttoken<>NOTOKEN then
  3072. begin
  3073. setnexttoken;
  3074. goto exit_label;
  3075. end;
  3076. { Skip all spaces and comments }
  3077. repeat
  3078. case c of
  3079. '{' :
  3080. skipcomment;
  3081. #26 :
  3082. begin
  3083. reload;
  3084. if (c=#26) and not assigned(inputfile.next) then
  3085. break;
  3086. end;
  3087. ' ',#9..#13 :
  3088. begin
  3089. {$ifdef PREPROCWRITE}
  3090. if parapreprocess then
  3091. begin
  3092. if c=#10 then
  3093. preprocfile.eolfound:=true
  3094. else
  3095. preprocfile.spacefound:=true;
  3096. end;
  3097. {$endif PREPROCWRITE}
  3098. skipspace;
  3099. end
  3100. else
  3101. break;
  3102. end;
  3103. until false;
  3104. { Save current token position, for EOF its already loaded }
  3105. if c<>#26 then
  3106. gettokenpos;
  3107. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  3108. if c in ['A'..'Z','a'..'z','_'] then
  3109. begin
  3110. readstring;
  3111. token:=_ID;
  3112. idtoken:=_ID;
  3113. { keyword or any other known token,
  3114. pattern is always uppercased }
  3115. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  3116. begin
  3117. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  3118. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  3119. while low<high do
  3120. begin
  3121. mid:=(high+low+1) shr 1;
  3122. if pattern<tokeninfo^[ttoken(mid)].str then
  3123. high:=mid-1
  3124. else
  3125. low:=mid;
  3126. end;
  3127. with tokeninfo^[ttoken(high)] do
  3128. if pattern=str then
  3129. begin
  3130. if keyword in current_settings.modeswitches then
  3131. if op=NOTOKEN then
  3132. token:=ttoken(high)
  3133. else
  3134. token:=op;
  3135. idtoken:=ttoken(high);
  3136. end;
  3137. end;
  3138. { Only process identifiers and not keywords }
  3139. if token=_ID then
  3140. begin
  3141. { this takes some time ... }
  3142. if (cs_support_macro in current_settings.moduleswitches) then
  3143. begin
  3144. mac:=tmacro(search_macro(pattern));
  3145. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  3146. begin
  3147. if yylexcount<max_macro_nesting then
  3148. begin
  3149. mac.is_used:=true;
  3150. inc(yylexcount);
  3151. insertmacro(pattern,mac.buftext,mac.buflen,
  3152. mac.fileinfo.line,mac.fileinfo.fileindex);
  3153. { handle empty macros }
  3154. if c=#0 then
  3155. reload;
  3156. readtoken(false);
  3157. { that's all folks }
  3158. dec(yylexcount);
  3159. exit;
  3160. end
  3161. else
  3162. Message(scan_w_macro_too_deep);
  3163. end;
  3164. end;
  3165. end;
  3166. { return token }
  3167. goto exit_label;
  3168. end
  3169. else
  3170. begin
  3171. idtoken:=_NOID;
  3172. case c of
  3173. '$' :
  3174. begin
  3175. readnumber;
  3176. token:=_INTCONST;
  3177. goto exit_label;
  3178. end;
  3179. '%' :
  3180. begin
  3181. if not(m_fpc in current_settings.modeswitches) then
  3182. Illegal_Char(c)
  3183. else
  3184. begin
  3185. readnumber;
  3186. token:=_INTCONST;
  3187. goto exit_label;
  3188. end;
  3189. end;
  3190. '&' :
  3191. begin
  3192. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  3193. begin
  3194. readnumber;
  3195. if length(pattern)=1 then
  3196. begin
  3197. readstring;
  3198. token:=_ID;
  3199. idtoken:=_ID;
  3200. end
  3201. else
  3202. token:=_INTCONST;
  3203. goto exit_label;
  3204. end
  3205. else if m_mac in current_settings.modeswitches then
  3206. begin
  3207. readchar;
  3208. token:=_AMPERSAND;
  3209. goto exit_label;
  3210. end
  3211. else
  3212. Illegal_Char(c);
  3213. end;
  3214. '0'..'9' :
  3215. begin
  3216. readnumber;
  3217. if (c in ['.','e','E']) then
  3218. begin
  3219. { first check for a . }
  3220. if c='.' then
  3221. begin
  3222. cachenexttokenpos;
  3223. readchar;
  3224. { is it a .. from a range? }
  3225. case c of
  3226. '.' :
  3227. begin
  3228. readchar;
  3229. token:=_INTCONST;
  3230. nexttoken:=_POINTPOINT;
  3231. goto exit_label;
  3232. end;
  3233. ')' :
  3234. begin
  3235. readchar;
  3236. token:=_INTCONST;
  3237. nexttoken:=_RECKKLAMMER;
  3238. goto exit_label;
  3239. end;
  3240. end;
  3241. { insert the number after the . }
  3242. pattern:=pattern+'.';
  3243. while c in ['0'..'9'] do
  3244. begin
  3245. pattern:=pattern+c;
  3246. readchar;
  3247. end;
  3248. end;
  3249. { E can also follow after a point is scanned }
  3250. if c in ['e','E'] then
  3251. begin
  3252. pattern:=pattern+'E';
  3253. readchar;
  3254. if c in ['-','+'] then
  3255. begin
  3256. pattern:=pattern+c;
  3257. readchar;
  3258. end;
  3259. if not(c in ['0'..'9']) then
  3260. Illegal_Char(c);
  3261. while c in ['0'..'9'] do
  3262. begin
  3263. pattern:=pattern+c;
  3264. readchar;
  3265. end;
  3266. end;
  3267. token:=_REALNUMBER;
  3268. goto exit_label;
  3269. end;
  3270. token:=_INTCONST;
  3271. goto exit_label;
  3272. end;
  3273. ';' :
  3274. begin
  3275. readchar;
  3276. token:=_SEMICOLON;
  3277. goto exit_label;
  3278. end;
  3279. '[' :
  3280. begin
  3281. readchar;
  3282. token:=_LECKKLAMMER;
  3283. goto exit_label;
  3284. end;
  3285. ']' :
  3286. begin
  3287. readchar;
  3288. token:=_RECKKLAMMER;
  3289. goto exit_label;
  3290. end;
  3291. '(' :
  3292. begin
  3293. readchar;
  3294. case c of
  3295. '*' :
  3296. begin
  3297. c:=#0;{Signal skipoldtpcomment to reload a char }
  3298. skipoldtpcomment;
  3299. readtoken(false);
  3300. exit;
  3301. end;
  3302. '.' :
  3303. begin
  3304. readchar;
  3305. token:=_LECKKLAMMER;
  3306. goto exit_label;
  3307. end;
  3308. end;
  3309. token:=_LKLAMMER;
  3310. goto exit_label;
  3311. end;
  3312. ')' :
  3313. begin
  3314. readchar;
  3315. token:=_RKLAMMER;
  3316. goto exit_label;
  3317. end;
  3318. '+' :
  3319. begin
  3320. readchar;
  3321. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3322. begin
  3323. readchar;
  3324. token:=_PLUSASN;
  3325. goto exit_label;
  3326. end;
  3327. token:=_PLUS;
  3328. goto exit_label;
  3329. end;
  3330. '-' :
  3331. begin
  3332. readchar;
  3333. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3334. begin
  3335. readchar;
  3336. token:=_MINUSASN;
  3337. goto exit_label;
  3338. end;
  3339. token:=_MINUS;
  3340. goto exit_label;
  3341. end;
  3342. ':' :
  3343. begin
  3344. readchar;
  3345. if c='=' then
  3346. begin
  3347. readchar;
  3348. token:=_ASSIGNMENT;
  3349. goto exit_label;
  3350. end;
  3351. token:=_COLON;
  3352. goto exit_label;
  3353. end;
  3354. '*' :
  3355. begin
  3356. readchar;
  3357. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  3358. begin
  3359. readchar;
  3360. token:=_STARASN;
  3361. end
  3362. else
  3363. if c='*' then
  3364. begin
  3365. readchar;
  3366. token:=_STARSTAR;
  3367. end
  3368. else
  3369. token:=_STAR;
  3370. goto exit_label;
  3371. end;
  3372. '/' :
  3373. begin
  3374. readchar;
  3375. case c of
  3376. '=' :
  3377. begin
  3378. if (cs_support_c_operators in current_settings.moduleswitches) then
  3379. begin
  3380. readchar;
  3381. token:=_SLASHASN;
  3382. goto exit_label;
  3383. end;
  3384. end;
  3385. '/' :
  3386. begin
  3387. skipdelphicomment;
  3388. readtoken(false);
  3389. exit;
  3390. end;
  3391. end;
  3392. token:=_SLASH;
  3393. goto exit_label;
  3394. end;
  3395. '|' :
  3396. if m_mac in current_settings.modeswitches then
  3397. begin
  3398. readchar;
  3399. token:=_PIPE;
  3400. goto exit_label;
  3401. end
  3402. else
  3403. Illegal_Char(c);
  3404. '=' :
  3405. begin
  3406. readchar;
  3407. token:=_EQUAL;
  3408. goto exit_label;
  3409. end;
  3410. '.' :
  3411. begin
  3412. readchar;
  3413. case c of
  3414. '.' :
  3415. begin
  3416. readchar;
  3417. case c of
  3418. '.' :
  3419. begin
  3420. readchar;
  3421. token:=_POINTPOINTPOINT;
  3422. goto exit_label;
  3423. end;
  3424. else
  3425. begin
  3426. token:=_POINTPOINT;
  3427. goto exit_label;
  3428. end;
  3429. end;
  3430. end;
  3431. ')' :
  3432. begin
  3433. readchar;
  3434. token:=_RECKKLAMMER;
  3435. goto exit_label;
  3436. end;
  3437. end;
  3438. token:=_POINT;
  3439. goto exit_label;
  3440. end;
  3441. '@' :
  3442. begin
  3443. readchar;
  3444. token:=_KLAMMERAFFE;
  3445. goto exit_label;
  3446. end;
  3447. ',' :
  3448. begin
  3449. readchar;
  3450. token:=_COMMA;
  3451. goto exit_label;
  3452. end;
  3453. '''','#','^' :
  3454. begin
  3455. len:=0;
  3456. cstringpattern:='';
  3457. iswidestring:=false;
  3458. if c='^' then
  3459. begin
  3460. readchar;
  3461. c:=upcase(c);
  3462. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  3463. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  3464. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  3465. begin
  3466. token:=_CARET;
  3467. goto exit_label;
  3468. end
  3469. else
  3470. begin
  3471. inc(len);
  3472. setlength(cstringpattern,256);
  3473. if c<#64 then
  3474. cstringpattern[len]:=chr(ord(c)+64)
  3475. else
  3476. cstringpattern[len]:=chr(ord(c)-64);
  3477. readchar;
  3478. end;
  3479. end;
  3480. repeat
  3481. case c of
  3482. '#' :
  3483. begin
  3484. readchar; { read # }
  3485. case c of
  3486. '$':
  3487. begin
  3488. readchar; { read leading $ }
  3489. asciinr:='$';
  3490. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=5) do
  3491. begin
  3492. asciinr:=asciinr+c;
  3493. readchar;
  3494. end;
  3495. end;
  3496. '&':
  3497. begin
  3498. readchar; { read leading $ }
  3499. asciinr:='&';
  3500. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=7) do
  3501. begin
  3502. asciinr:=asciinr+c;
  3503. readchar;
  3504. end;
  3505. end;
  3506. '%':
  3507. begin
  3508. readchar; { read leading $ }
  3509. asciinr:='%';
  3510. while (upcase(c) in ['0','1']) and (length(asciinr)<=17) do
  3511. begin
  3512. asciinr:=asciinr+c;
  3513. readchar;
  3514. end;
  3515. end;
  3516. else
  3517. begin
  3518. asciinr:='';
  3519. while (c in ['0'..'9']) and (length(asciinr)<=5) do
  3520. begin
  3521. asciinr:=asciinr+c;
  3522. readchar;
  3523. end;
  3524. end;
  3525. end;
  3526. val(asciinr,m,code);
  3527. if (asciinr='') or (code<>0) then
  3528. Message(scan_e_illegal_char_const)
  3529. else if (m<0) or (m>255) or (length(asciinr)>3) then
  3530. begin
  3531. if (m>=0) and (m<=65535) then
  3532. begin
  3533. if not iswidestring then
  3534. begin
  3535. if len>0 then
  3536. ascii2unicode(@cstringpattern[1],len,patternw)
  3537. else
  3538. ascii2unicode(nil,len,patternw);
  3539. iswidestring:=true;
  3540. len:=0;
  3541. end;
  3542. concatwidestringchar(patternw,tcompilerwidechar(m));
  3543. end
  3544. else
  3545. Message(scan_e_illegal_char_const)
  3546. end
  3547. else if iswidestring then
  3548. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  3549. else
  3550. begin
  3551. if len>=length(cstringpattern) then
  3552. setlength(cstringpattern,length(cstringpattern)+256);
  3553. inc(len);
  3554. cstringpattern[len]:=chr(m);
  3555. end;
  3556. end;
  3557. '''' :
  3558. begin
  3559. repeat
  3560. readchar;
  3561. case c of
  3562. #26 :
  3563. end_of_file;
  3564. #10,#13 :
  3565. Message(scan_f_string_exceeds_line);
  3566. '''' :
  3567. begin
  3568. readchar;
  3569. if c<>'''' then
  3570. break;
  3571. end;
  3572. end;
  3573. { interpret as utf-8 string? }
  3574. if (ord(c)>=$80) and (current_settings.sourcecodepage='utf8') then
  3575. begin
  3576. { convert existing string to an utf-8 string }
  3577. if not iswidestring then
  3578. begin
  3579. if len>0 then
  3580. ascii2unicode(@cstringpattern[1],len,patternw)
  3581. else
  3582. ascii2unicode(nil,len,patternw);
  3583. iswidestring:=true;
  3584. len:=0;
  3585. end;
  3586. { four or more chars aren't handled }
  3587. if (ord(c) and $f0)=$f0 then
  3588. message(scan_e_utf8_bigger_than_65535)
  3589. { three chars }
  3590. else if (ord(c) and $e0)=$e0 then
  3591. begin
  3592. w:=ord(c) and $f;
  3593. readchar;
  3594. if (ord(c) and $c0)<>$80 then
  3595. message(scan_e_utf8_malformed);
  3596. w:=(w shl 6) or (ord(c) and $3f);
  3597. readchar;
  3598. if (ord(c) and $c0)<>$80 then
  3599. message(scan_e_utf8_malformed);
  3600. w:=(w shl 6) or (ord(c) and $3f);
  3601. concatwidestringchar(patternw,w);
  3602. end
  3603. { two chars }
  3604. else if (ord(c) and $c0)<>0 then
  3605. begin
  3606. w:=ord(c) and $1f;
  3607. readchar;
  3608. if (ord(c) and $c0)<>$80 then
  3609. message(scan_e_utf8_malformed);
  3610. w:=(w shl 6) or (ord(c) and $3f);
  3611. concatwidestringchar(patternw,w);
  3612. end
  3613. { illegal }
  3614. else if (ord(c) and $80)<>0 then
  3615. message(scan_e_utf8_malformed)
  3616. else
  3617. concatwidestringchar(patternw,tcompilerwidechar(c))
  3618. end
  3619. else if iswidestring then
  3620. begin
  3621. if current_settings.sourcecodepage='utf8' then
  3622. concatwidestringchar(patternw,ord(c))
  3623. else
  3624. concatwidestringchar(patternw,asciichar2unicode(c))
  3625. end
  3626. else
  3627. begin
  3628. if len>=length(cstringpattern) then
  3629. setlength(cstringpattern,length(cstringpattern)+256);
  3630. inc(len);
  3631. cstringpattern[len]:=c;
  3632. end;
  3633. until false;
  3634. end;
  3635. '^' :
  3636. begin
  3637. readchar;
  3638. c:=upcase(c);
  3639. if c<#64 then
  3640. c:=chr(ord(c)+64)
  3641. else
  3642. c:=chr(ord(c)-64);
  3643. if iswidestring then
  3644. concatwidestringchar(patternw,asciichar2unicode(c))
  3645. else
  3646. begin
  3647. if len>=length(cstringpattern) then
  3648. setlength(cstringpattern,length(cstringpattern)+256);
  3649. inc(len);
  3650. cstringpattern[len]:=c;
  3651. end;
  3652. readchar;
  3653. end;
  3654. else
  3655. break;
  3656. end;
  3657. until false;
  3658. { strings with length 1 become const chars }
  3659. if iswidestring then
  3660. begin
  3661. if patternw^.len=1 then
  3662. token:=_CWCHAR
  3663. else
  3664. token:=_CWSTRING;
  3665. end
  3666. else
  3667. begin
  3668. setlength(cstringpattern,len);
  3669. if length(cstringpattern)=1 then
  3670. begin
  3671. token:=_CCHAR;
  3672. pattern:=cstringpattern;
  3673. end
  3674. else
  3675. token:=_CSTRING;
  3676. end;
  3677. goto exit_label;
  3678. end;
  3679. '>' :
  3680. begin
  3681. readchar;
  3682. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  3683. token:=_RSHARPBRACKET
  3684. else
  3685. begin
  3686. case c of
  3687. '=' :
  3688. begin
  3689. readchar;
  3690. token:=_GTE;
  3691. goto exit_label;
  3692. end;
  3693. '>' :
  3694. begin
  3695. readchar;
  3696. token:=_OP_SHR;
  3697. goto exit_label;
  3698. end;
  3699. '<' :
  3700. begin { >< is for a symetric diff for sets }
  3701. readchar;
  3702. token:=_SYMDIF;
  3703. goto exit_label;
  3704. end;
  3705. end;
  3706. token:=_GT;
  3707. end;
  3708. goto exit_label;
  3709. end;
  3710. '<' :
  3711. begin
  3712. readchar;
  3713. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  3714. token:=_LSHARPBRACKET
  3715. else
  3716. begin
  3717. case c of
  3718. '>' :
  3719. begin
  3720. readchar;
  3721. token:=_UNEQUAL;
  3722. goto exit_label;
  3723. end;
  3724. '=' :
  3725. begin
  3726. readchar;
  3727. token:=_LTE;
  3728. goto exit_label;
  3729. end;
  3730. '<' :
  3731. begin
  3732. readchar;
  3733. token:=_OP_SHL;
  3734. goto exit_label;
  3735. end;
  3736. end;
  3737. token:=_LT;
  3738. end;
  3739. goto exit_label;
  3740. end;
  3741. #26 :
  3742. begin
  3743. token:=_EOF;
  3744. checkpreprocstack;
  3745. goto exit_label;
  3746. end;
  3747. else
  3748. Illegal_Char(c);
  3749. end;
  3750. end;
  3751. exit_label:
  3752. lasttoken:=token;
  3753. end;
  3754. function tscannerfile.readpreproc:ttoken;
  3755. begin
  3756. skipspace;
  3757. case c of
  3758. '_',
  3759. 'A'..'Z',
  3760. 'a'..'z' :
  3761. begin
  3762. current_scanner.preproc_pattern:=readid;
  3763. readpreproc:=_ID;
  3764. end;
  3765. '0'..'9' :
  3766. begin
  3767. current_scanner.preproc_pattern:=readval_asstring;
  3768. { realnumber? }
  3769. if c='.' then
  3770. begin
  3771. readchar;
  3772. while c in ['0'..'9'] do
  3773. begin
  3774. current_scanner.preproc_pattern:=current_scanner.preproc_pattern+c;
  3775. readchar;
  3776. end;
  3777. end;
  3778. readpreproc:=_ID;
  3779. end;
  3780. '$','%','&' :
  3781. begin
  3782. current_scanner.preproc_pattern:=readval_asstring;
  3783. readpreproc:=_ID;
  3784. end;
  3785. ',' :
  3786. begin
  3787. readchar;
  3788. readpreproc:=_COMMA;
  3789. end;
  3790. '}' :
  3791. begin
  3792. readpreproc:=_END;
  3793. end;
  3794. '(' :
  3795. begin
  3796. readchar;
  3797. readpreproc:=_LKLAMMER;
  3798. end;
  3799. ')' :
  3800. begin
  3801. readchar;
  3802. readpreproc:=_RKLAMMER;
  3803. end;
  3804. '[' :
  3805. begin
  3806. readchar;
  3807. readpreproc:=_LECKKLAMMER;
  3808. end;
  3809. ']' :
  3810. begin
  3811. readchar;
  3812. readpreproc:=_RECKKLAMMER;
  3813. end;
  3814. '+' :
  3815. begin
  3816. readchar;
  3817. readpreproc:=_PLUS;
  3818. end;
  3819. '-' :
  3820. begin
  3821. readchar;
  3822. readpreproc:=_MINUS;
  3823. end;
  3824. '*' :
  3825. begin
  3826. readchar;
  3827. readpreproc:=_STAR;
  3828. end;
  3829. '/' :
  3830. begin
  3831. readchar;
  3832. readpreproc:=_SLASH;
  3833. end;
  3834. '=' :
  3835. begin
  3836. readchar;
  3837. readpreproc:=_EQUAL;
  3838. end;
  3839. '>' :
  3840. begin
  3841. readchar;
  3842. if c='=' then
  3843. begin
  3844. readchar;
  3845. readpreproc:=_GTE;
  3846. end
  3847. else
  3848. readpreproc:=_GT;
  3849. end;
  3850. '<' :
  3851. begin
  3852. readchar;
  3853. case c of
  3854. '>' :
  3855. begin
  3856. readchar;
  3857. readpreproc:=_UNEQUAL;
  3858. end;
  3859. '=' :
  3860. begin
  3861. readchar;
  3862. readpreproc:=_LTE;
  3863. end;
  3864. else
  3865. readpreproc:=_LT;
  3866. end;
  3867. end;
  3868. #26 :
  3869. begin
  3870. readpreproc:=_EOF;
  3871. checkpreprocstack;
  3872. end;
  3873. else
  3874. Illegal_Char(c);
  3875. end;
  3876. end;
  3877. function tscannerfile.asmgetcharstart : char;
  3878. begin
  3879. { return first the character already
  3880. available in c }
  3881. lastasmgetchar:=c;
  3882. result:=asmgetchar;
  3883. end;
  3884. function tscannerfile.asmgetchar : char;
  3885. begin
  3886. if lastasmgetchar<>#0 then
  3887. begin
  3888. c:=lastasmgetchar;
  3889. lastasmgetchar:=#0;
  3890. end
  3891. else
  3892. readchar;
  3893. if in_asm_string then
  3894. begin
  3895. asmgetchar:=c;
  3896. exit;
  3897. end;
  3898. repeat
  3899. case c of
  3900. // the { ... } is used in ARM assembler to define register sets, so we can't used
  3901. // it as comment, either (* ... *), /* ... */ or // ... should be used instead.
  3902. // But compiler directives {$...} are allowed in ARM assembler.
  3903. '{' :
  3904. begin
  3905. {$ifdef arm}
  3906. readchar;
  3907. dec(inputpointer);
  3908. if c<>'$' then
  3909. begin
  3910. asmgetchar:='{';
  3911. exit;
  3912. end
  3913. else
  3914. {$endif arm}
  3915. skipcomment;
  3916. end;
  3917. #10,#13 :
  3918. begin
  3919. linebreak;
  3920. asmgetchar:=c;
  3921. exit;
  3922. end;
  3923. #26 :
  3924. begin
  3925. reload;
  3926. if (c=#26) and not assigned(inputfile.next) then
  3927. end_of_file;
  3928. continue;
  3929. end;
  3930. '/' :
  3931. begin
  3932. readchar;
  3933. if c='/' then
  3934. skipdelphicomment
  3935. else
  3936. begin
  3937. asmgetchar:='/';
  3938. lastasmgetchar:=c;
  3939. exit;
  3940. end;
  3941. end;
  3942. '(' :
  3943. begin
  3944. readchar;
  3945. if c='*' then
  3946. begin
  3947. c:=#0;{Signal skipoldtpcomment to reload a char }
  3948. skipoldtpcomment;
  3949. end
  3950. else
  3951. begin
  3952. asmgetchar:='(';
  3953. lastasmgetchar:=c;
  3954. exit;
  3955. end;
  3956. end;
  3957. else
  3958. begin
  3959. asmgetchar:=c;
  3960. exit;
  3961. end;
  3962. end;
  3963. until false;
  3964. end;
  3965. {*****************************************************************************
  3966. Helpers
  3967. *****************************************************************************}
  3968. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  3969. begin
  3970. if dm in [directive_all, directive_turbo] then
  3971. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  3972. if dm in [directive_all, directive_mac] then
  3973. tdirectiveitem.create(mac_scannerdirectives,s,p);
  3974. end;
  3975. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  3976. begin
  3977. if dm in [directive_all, directive_turbo] then
  3978. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  3979. if dm in [directive_all, directive_mac] then
  3980. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  3981. end;
  3982. {*****************************************************************************
  3983. Initialization
  3984. *****************************************************************************}
  3985. procedure InitScanner;
  3986. begin
  3987. InitWideString(patternw);
  3988. turbo_scannerdirectives:=TFPHashObjectList.Create;
  3989. mac_scannerdirectives:=TFPHashObjectList.Create;
  3990. { Common directives and conditionals }
  3991. AddDirective('I',directive_all, @dir_include);
  3992. AddDirective('DEFINE',directive_all, @dir_define);
  3993. AddDirective('UNDEF',directive_all, @dir_undef);
  3994. AddConditional('IF',directive_all, @dir_if);
  3995. AddConditional('IFDEF',directive_all, @dir_ifdef);
  3996. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  3997. AddConditional('ELSE',directive_all, @dir_else);
  3998. AddConditional('ELSEIF',directive_all, @dir_elseif);
  3999. AddConditional('ENDIF',directive_all, @dir_endif);
  4000. { Directives and conditionals for all modes except mode macpas}
  4001. AddDirective('INCLUDE',directive_turbo, @dir_include);
  4002. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  4003. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  4004. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  4005. AddConditional('IFEND',directive_turbo, @dir_endif);
  4006. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  4007. { Directives and conditionals for mode macpas: }
  4008. AddDirective('SETC',directive_mac, @dir_setc);
  4009. AddDirective('DEFINEC',directive_mac, @dir_definec);
  4010. AddDirective('UNDEFC',directive_mac, @dir_undef);
  4011. AddConditional('IFC',directive_mac, @dir_if);
  4012. AddConditional('ELSEC',directive_mac, @dir_else);
  4013. AddConditional('ELIFC',directive_mac, @dir_elseif);
  4014. AddConditional('ENDC',directive_mac, @dir_endif);
  4015. end;
  4016. procedure DoneScanner;
  4017. begin
  4018. turbo_scannerdirectives.Free;
  4019. mac_scannerdirectives.Free;
  4020. DoneWideString(patternw);
  4021. end;
  4022. end.