scanner.pas 147 KB

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