scanner.pas 178 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the scanner part and handling of the switches
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit scanner;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,constexp,version,tokens,
  23. verbose,comphook,
  24. finput,
  25. widestr;
  26. const
  27. max_include_nesting=32;
  28. max_macro_nesting=16;
  29. preprocbufsize=32*1024;
  30. type
  31. tcommentstyle = (comment_none,comment_tp,comment_oldtp,comment_delphi,comment_c);
  32. tscannerfile = class;
  33. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else,pp_elseif);
  34. tpreprocstack = class
  35. typ : preproctyp;
  36. accept : boolean;
  37. next : tpreprocstack;
  38. name : TIDString;
  39. line_nb : longint;
  40. owner : tscannerfile;
  41. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  42. end;
  43. tdirectiveproc=procedure;
  44. tdirectiveitem = class(TFPHashObject)
  45. public
  46. is_conditional : boolean;
  47. proc : tdirectiveproc;
  48. constructor Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  49. constructor CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  50. end;
  51. // stack for replay buffers
  52. treplaystack = class
  53. token : ttoken;
  54. settings : tsettings;
  55. tokenbuf : tdynamicarray;
  56. next : treplaystack;
  57. constructor Create(atoken: ttoken;asettings:tsettings;
  58. atokenbuf:tdynamicarray;anext:treplaystack);
  59. end;
  60. tcompile_time_predicate = function(var valuedescr: String) : Boolean;
  61. tspecialgenerictoken =
  62. (ST_LOADSETTINGS,
  63. ST_LINE,
  64. ST_COLUMN,
  65. ST_FILEINDEX,
  66. ST_LOADMESSAGES);
  67. { tscannerfile }
  68. tscannerfile = class
  69. private
  70. procedure do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  71. procedure cachenexttokenpos;
  72. procedure setnexttoken;
  73. procedure savetokenpos;
  74. procedure restoretokenpos;
  75. procedure writetoken(t: ttoken);
  76. function readtoken : ttoken;
  77. public
  78. inputfile : tinputfile; { current inputfile list }
  79. inputfilecount : longint;
  80. inputbuffer, { input buffer }
  81. inputpointer : pchar;
  82. inputstart : longint;
  83. line_no, { line }
  84. lastlinepos : longint;
  85. lasttokenpos,
  86. nexttokenpos : longint; { token }
  87. lasttoken,
  88. nexttoken : ttoken;
  89. oldlasttokenpos : longint; { temporary saving/restoring tokenpos }
  90. oldcurrent_filepos,
  91. oldcurrent_tokenpos : tfileposinfo;
  92. replaytokenbuf,
  93. recordtokenbuf : tdynamicarray;
  94. { last settings we stored }
  95. last_settings : tsettings;
  96. last_message : pmessagestaterecord;
  97. { last filepos we stored }
  98. last_filepos,
  99. { if nexttoken<>NOTOKEN, then nexttokenpos holds its filepos }
  100. next_filepos : tfileposinfo;
  101. comment_level,
  102. yylexcount : longint;
  103. lastasmgetchar : char;
  104. ignoredirectives : TFPHashList; { ignore directives, used to give warnings only once }
  105. preprocstack : tpreprocstack;
  106. replaystack : treplaystack;
  107. in_asm_string : boolean;
  108. preproc_pattern : string;
  109. preproc_token : ttoken;
  110. constructor Create(const fn:string; is_macro: boolean = false);
  111. destructor Destroy;override;
  112. { File buffer things }
  113. function openinputfile:boolean;
  114. procedure closeinputfile;
  115. function tempopeninputfile:boolean;
  116. procedure tempcloseinputfile;
  117. procedure saveinputfile;
  118. procedure restoreinputfile;
  119. procedure firstfile;
  120. procedure nextfile;
  121. procedure addfile(hp:tinputfile);
  122. procedure reload;
  123. { replaces current token with the text in p }
  124. procedure substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  125. { Scanner things }
  126. procedure gettokenpos;
  127. procedure inc_comment_level;
  128. procedure dec_comment_level;
  129. procedure illegal_char(c:char);
  130. procedure end_of_file;
  131. procedure checkpreprocstack;
  132. procedure poppreprocstack;
  133. procedure ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  134. procedure elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  135. procedure elsepreprocstack;
  136. procedure popreplaystack;
  137. procedure handleconditional(p:tdirectiveitem);
  138. procedure handledirectives;
  139. procedure linebreak;
  140. procedure recordtoken;
  141. procedure startrecordtokens(buf:tdynamicarray);
  142. procedure stoprecordtokens;
  143. procedure replaytoken;
  144. procedure startreplaytokens(buf:tdynamicarray);
  145. { bit length asizeint is target depend }
  146. procedure tokenwritesizeint(val : asizeint);
  147. procedure tokenwritelongint(val : longint);
  148. procedure tokenwritelongword(val : longword);
  149. procedure tokenwriteword(val : word);
  150. procedure tokenwriteshortint(val : shortint);
  151. procedure tokenwriteset(var b;size : longint);
  152. procedure tokenwriteenum(var b;size : longint);
  153. function tokenreadsizeint : asizeint;
  154. procedure tokenwritesettings(var asettings : tsettings; var size : asizeint);
  155. { longword/longint are 32 bits on all targets }
  156. { word/smallint are 16-bits on all targest }
  157. function tokenreadlongword : longword;
  158. function tokenreadword : word;
  159. function tokenreadlongint : longint;
  160. function tokenreadsmallint : smallint;
  161. { short int is one a signed byte }
  162. function tokenreadshortint : shortint;
  163. function tokenreadbyte : byte;
  164. { This one takes the set size as an parameter }
  165. procedure tokenreadset(var b;size : longint);
  166. function tokenreadenum(size : longint) : longword;
  167. procedure tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  168. procedure readchar;
  169. procedure readstring;
  170. procedure readnumber;
  171. function readid:string;
  172. function readval:longint;
  173. function readcomment:string;
  174. function readquotedstring:string;
  175. function readstate:char;
  176. function readstatedefault:char;
  177. procedure skipspace;
  178. procedure skipuntildirective;
  179. procedure skipcomment;
  180. procedure skipdelphicomment;
  181. procedure skipoldtpcomment;
  182. procedure readtoken(allowrecordtoken:boolean);
  183. function readpreproc:ttoken;
  184. function asmgetcharstart : char;
  185. function asmgetchar:char;
  186. end;
  187. {$ifdef PREPROCWRITE}
  188. tpreprocfile=class
  189. f : text;
  190. buf : pointer;
  191. spacefound,
  192. eolfound : boolean;
  193. constructor create(const fn:string);
  194. destructor destroy;
  195. procedure Add(const s:string);
  196. procedure AddSpace;
  197. end;
  198. {$endif PREPROCWRITE}
  199. var
  200. { read strings }
  201. c : char;
  202. orgpattern,
  203. pattern : string;
  204. cstringpattern : ansistring;
  205. patternw : pcompilerwidestring;
  206. { token }
  207. token, { current token being parsed }
  208. idtoken : ttoken; { holds the token if the pattern is a known word }
  209. current_scanner : tscannerfile; { current scanner in use }
  210. aktcommentstyle : tcommentstyle; { needed to use read_comment from directives }
  211. {$ifdef PREPROCWRITE}
  212. preprocfile : tpreprocfile; { used with only preprocessing }
  213. {$endif PREPROCWRITE}
  214. type
  215. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  216. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  217. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  218. procedure InitScanner;
  219. procedure DoneScanner;
  220. { To be called when the language mode is finally determined }
  221. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  222. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  223. procedure SetAppType(NewAppType:tapptype);
  224. implementation
  225. uses
  226. SysUtils,
  227. cutils,cfileutl,
  228. systems,
  229. switches,
  230. symbase,symtable,symtype,symsym,symconst,symdef,defutil,
  231. { This is needed for tcputype }
  232. cpuinfo,
  233. fmodule
  234. {$if FPC_FULLVERSION<20700}
  235. ,ccharset
  236. {$endif}
  237. ;
  238. var
  239. { dictionaries with the supported directives }
  240. turbo_scannerdirectives : TFPHashObjectList; { for other modes }
  241. mac_scannerdirectives : TFPHashObjectList; { for mode mac }
  242. {*****************************************************************************
  243. Helper routines
  244. *****************************************************************************}
  245. const
  246. { use any special name that is an invalid file name to avoid problems }
  247. preprocstring : array [preproctyp] of string[7]
  248. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE','$ELSEIF');
  249. function is_keyword(const s:string):boolean;
  250. var
  251. low,high,mid : longint;
  252. begin
  253. if not (length(s) in [tokenlenmin..tokenlenmax]) or
  254. not (s[1] in ['a'..'z','A'..'Z']) then
  255. begin
  256. is_keyword:=false;
  257. exit;
  258. end;
  259. low:=ord(tokenidx^[length(s),s[1]].first);
  260. high:=ord(tokenidx^[length(s),s[1]].last);
  261. while low<high do
  262. begin
  263. mid:=(high+low+1) shr 1;
  264. if pattern<tokeninfo^[ttoken(mid)].str then
  265. high:=mid-1
  266. else
  267. low:=mid;
  268. end;
  269. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  270. ((tokeninfo^[ttoken(high)].keyword*current_settings.modeswitches)<>[]);
  271. end;
  272. Procedure HandleModeSwitches(switch: tmodeswitch; changeInit: boolean);
  273. begin
  274. { turn ansi/unicodestrings on by default ? (only change when this
  275. particular setting is changed, so that a random modeswitch won't
  276. change the state of $h+/$h-) }
  277. if switch in [m_none,m_default_ansistring,m_default_unicodestring] then
  278. begin
  279. if ([m_default_ansistring,m_default_unicodestring]*current_settings.modeswitches)<>[] then
  280. begin
  281. { can't have both ansistring and unicodestring as default }
  282. if switch=m_default_ansistring then
  283. begin
  284. exclude(current_settings.modeswitches,m_default_unicodestring);
  285. if changeinit then
  286. exclude(init_settings.modeswitches,m_default_unicodestring);
  287. end
  288. else if switch=m_default_unicodestring then
  289. begin
  290. exclude(current_settings.modeswitches,m_default_ansistring);
  291. if changeinit then
  292. exclude(init_settings.modeswitches,m_default_ansistring);
  293. end;
  294. { enable $h+ }
  295. include(current_settings.localswitches,cs_refcountedstrings);
  296. if changeinit then
  297. include(init_settings.localswitches,cs_refcountedstrings);
  298. if m_default_unicodestring in current_settings.modeswitches then
  299. begin
  300. def_system_macro('FPC_UNICODESTRINGS');
  301. def_system_macro('UNICODE');
  302. end;
  303. end
  304. else
  305. begin
  306. exclude(current_settings.localswitches,cs_refcountedstrings);
  307. if changeinit then
  308. exclude(init_settings.localswitches,cs_refcountedstrings);
  309. undef_system_macro('FPC_UNICODESTRINGS');
  310. undef_system_macro('UNICODE');
  311. end;
  312. end;
  313. { turn inline on by default ? }
  314. if switch in [m_none,m_default_inline] then
  315. begin
  316. if (m_default_inline in current_settings.modeswitches) then
  317. begin
  318. include(current_settings.localswitches,cs_do_inline);
  319. if changeinit then
  320. include(init_settings.localswitches,cs_do_inline);
  321. end
  322. else
  323. begin
  324. exclude(current_settings.localswitches,cs_do_inline);
  325. if changeinit then
  326. exclude(init_settings.localswitches,cs_do_inline);
  327. end;
  328. end;
  329. { turn on system codepage by default }
  330. if switch in [m_none,m_systemcodepage] then
  331. begin
  332. if m_systemcodepage in current_settings.modeswitches then
  333. begin
  334. current_settings.sourcecodepage:=DefaultSystemCodePage;
  335. if (current_settings.sourcecodepage<>CP_UTF8) and not cpavailable(current_settings.sourcecodepage) then
  336. begin
  337. Message2(scan_w_unavailable_system_codepage,IntToStr(current_settings.sourcecodepage),IntToStr(default_settings.sourcecodepage));
  338. current_settings.sourcecodepage:=default_settings.sourcecodepage;
  339. end;
  340. include(current_settings.moduleswitches,cs_explicit_codepage);
  341. if changeinit then
  342. begin
  343. init_settings.sourcecodepage:=current_settings.sourcecodepage;
  344. include(init_settings.moduleswitches,cs_explicit_codepage);
  345. end;
  346. end
  347. else
  348. begin
  349. exclude(current_settings.moduleswitches,cs_explicit_codepage);
  350. if changeinit then
  351. exclude(init_settings.moduleswitches,cs_explicit_codepage);
  352. end;
  353. end;
  354. end;
  355. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  356. var
  357. b : boolean;
  358. oldmodeswitches : tmodeswitches;
  359. begin
  360. oldmodeswitches:=current_settings.modeswitches;
  361. b:=true;
  362. if s='DEFAULT' then
  363. current_settings.modeswitches:=fpcmodeswitches
  364. else
  365. if s='DELPHI' then
  366. current_settings.modeswitches:=delphimodeswitches
  367. else
  368. if s='DELPHIUNICODE' then
  369. current_settings.modeswitches:=delphiunicodemodeswitches
  370. else
  371. if s='TP' then
  372. current_settings.modeswitches:=tpmodeswitches
  373. else
  374. if s='FPC' then begin
  375. current_settings.modeswitches:=fpcmodeswitches;
  376. { TODO: enable this for 2.3/2.9 }
  377. // include(current_settings.localswitches, cs_typed_addresses);
  378. end else
  379. if s='OBJFPC' then begin
  380. current_settings.modeswitches:=objfpcmodeswitches;
  381. { TODO: enable this for 2.3/2.9 }
  382. // include(current_settings.localswitches, cs_typed_addresses);
  383. end
  384. {$ifdef gpc_mode}
  385. else if s='GPC' then
  386. current_settings.modeswitches:=gpcmodeswitches
  387. {$endif}
  388. else
  389. if s='MACPAS' then
  390. current_settings.modeswitches:=macmodeswitches
  391. else
  392. if s='ISO' then
  393. current_settings.modeswitches:=isomodeswitches
  394. else
  395. b:=false;
  396. {$ifdef jvm}
  397. { enable final fields by default for the JVM targets }
  398. include(current_settings.modeswitches,m_final_fields);
  399. {$endif jvm}
  400. if b and changeInit then
  401. init_settings.modeswitches := current_settings.modeswitches;
  402. if b then
  403. begin
  404. { resolve all postponed switch changes }
  405. flushpendingswitchesstate;
  406. HandleModeSwitches(m_none,changeinit);
  407. { turn on bitpacking for mode macpas and iso pascal }
  408. if ([m_mac,m_iso] * current_settings.modeswitches <> []) then
  409. begin
  410. include(current_settings.localswitches,cs_bitpacking);
  411. if changeinit then
  412. include(init_settings.localswitches,cs_bitpacking);
  413. end;
  414. { support goto/label by default in delphi/tp7/mac modes }
  415. if ([m_delphi,m_tp7,m_mac,m_iso] * current_settings.modeswitches <> []) then
  416. begin
  417. include(current_settings.moduleswitches,cs_support_goto);
  418. if changeinit then
  419. include(init_settings.moduleswitches,cs_support_goto);
  420. end;
  421. { support pointer math by default in fpc/objfpc modes }
  422. if ([m_fpc,m_objfpc] * current_settings.modeswitches <> []) then
  423. begin
  424. include(current_settings.localswitches,cs_pointermath);
  425. if changeinit then
  426. include(init_settings.localswitches,cs_pointermath);
  427. end
  428. else
  429. begin
  430. exclude(current_settings.localswitches,cs_pointermath);
  431. if changeinit then
  432. exclude(init_settings.localswitches,cs_pointermath);
  433. end;
  434. { Default enum and set packing for delphi/tp7 }
  435. if (m_tp7 in current_settings.modeswitches) or
  436. (m_delphi in current_settings.modeswitches) then
  437. begin
  438. current_settings.packenum:=1;
  439. current_settings.setalloc:=1;
  440. end
  441. else if (m_mac in current_settings.modeswitches) then
  442. { compatible with Metrowerks Pascal }
  443. current_settings.packenum:=2
  444. else
  445. current_settings.packenum:=4;
  446. if changeinit then
  447. begin
  448. init_settings.packenum:=current_settings.packenum;
  449. init_settings.setalloc:=current_settings.setalloc;
  450. end;
  451. {$if defined(i386) or defined(i8086)}
  452. { Default to intel assembler for delphi/tp7 on i386/i8086 }
  453. if (m_delphi in current_settings.modeswitches) or
  454. (m_tp7 in current_settings.modeswitches) then
  455. current_settings.asmmode:=asmmode_i386_intel;
  456. if changeinit then
  457. init_settings.asmmode:=current_settings.asmmode;
  458. {$endif i386 or i8086}
  459. { Exception support explicitly turned on (mainly for macpas, to }
  460. { compensate for lack of interprocedural goto support) }
  461. if (cs_support_exceptions in current_settings.globalswitches) then
  462. include(current_settings.modeswitches,m_except);
  463. { Default strict string var checking in TP/Delphi modes }
  464. if ([m_delphi,m_tp7] * current_settings.modeswitches <> []) then
  465. begin
  466. include(current_settings.localswitches,cs_strict_var_strings);
  467. if changeinit then
  468. include(init_settings.localswitches,cs_strict_var_strings);
  469. end;
  470. { Undefine old symbol }
  471. if (m_delphi in oldmodeswitches) then
  472. undef_system_macro('FPC_DELPHI')
  473. else if (m_tp7 in oldmodeswitches) then
  474. undef_system_macro('FPC_TP')
  475. else if (m_objfpc in oldmodeswitches) then
  476. undef_system_macro('FPC_OBJFPC')
  477. {$ifdef gpc_mode}
  478. else if (m_gpc in oldmodeswitches) then
  479. undef_system_macro('FPC_GPC')
  480. {$endif}
  481. else if (m_mac in oldmodeswitches) then
  482. undef_system_macro('FPC_MACPAS');
  483. { define new symbol in delphi,objfpc,tp,gpc,macpas mode }
  484. if (m_delphi in current_settings.modeswitches) then
  485. def_system_macro('FPC_DELPHI')
  486. else if (m_tp7 in current_settings.modeswitches) then
  487. def_system_macro('FPC_TP')
  488. else if (m_objfpc in current_settings.modeswitches) then
  489. def_system_macro('FPC_OBJFPC')
  490. {$ifdef gpc_mode}
  491. else if (m_gpc in current_settings.modeswitches) then
  492. def_system_macro('FPC_GPC')
  493. {$endif}
  494. else if (m_mac in current_settings.modeswitches) then
  495. def_system_macro('FPC_MACPAS');
  496. end;
  497. SetCompileMode:=b;
  498. end;
  499. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  500. var
  501. i : tmodeswitch;
  502. doinclude : boolean;
  503. begin
  504. s:=upper(s);
  505. { on/off? }
  506. doinclude:=true;
  507. case s[length(s)] of
  508. '+':
  509. s:=copy(s,1,length(s)-1);
  510. '-':
  511. begin
  512. s:=copy(s,1,length(s)-1);
  513. doinclude:=false;
  514. end;
  515. end;
  516. Result:=false;
  517. for i:=m_class to high(tmodeswitch) do
  518. if s=modeswitchstr[i] then
  519. begin
  520. { Objective-C is currently only supported for Darwin targets }
  521. if doinclude and
  522. (i in [m_objectivec1,m_objectivec2]) and
  523. not(target_info.system in systems_objc_supported) then
  524. begin
  525. Message1(option_unsupported_target_for_feature,'Objective-C');
  526. break;
  527. end;
  528. if changeInit then
  529. current_settings.modeswitches:=init_settings.modeswitches;
  530. Result:=true;
  531. if doinclude then
  532. begin
  533. include(current_settings.modeswitches,i);
  534. { Objective-C 2.0 support implies 1.0 support }
  535. if (i=m_objectivec2) then
  536. include(current_settings.modeswitches,m_objectivec1);
  537. if (i in [m_objectivec1,m_objectivec2]) then
  538. include(current_settings.modeswitches,m_class);
  539. end
  540. else
  541. begin
  542. exclude(current_settings.modeswitches,i);
  543. { Objective-C 2.0 support implies 1.0 support }
  544. if (i=m_objectivec2) then
  545. exclude(current_settings.modeswitches,m_objectivec1);
  546. if (i in [m_objectivec1,m_objectivec2]) and
  547. ([m_delphi,m_objfpc]*current_settings.modeswitches=[]) then
  548. exclude(current_settings.modeswitches,m_class);
  549. end;
  550. { set other switches depending on changed mode switch }
  551. HandleModeSwitches(i,changeinit);
  552. if changeInit then
  553. init_settings.modeswitches:=current_settings.modeswitches;
  554. break;
  555. end;
  556. end;
  557. procedure SetAppType(NewAppType:tapptype);
  558. begin
  559. if apptype=app_cui then
  560. undef_system_macro('CONSOLE');
  561. apptype:=NewAppType;
  562. if apptype=app_cui then
  563. def_system_macro('CONSOLE');
  564. end;
  565. {*****************************************************************************
  566. Conditional Directives
  567. *****************************************************************************}
  568. procedure dir_else;
  569. begin
  570. current_scanner.elsepreprocstack;
  571. end;
  572. procedure dir_endif;
  573. begin
  574. current_scanner.poppreprocstack;
  575. end;
  576. function isdef(var valuedescr: String): Boolean;
  577. var
  578. hs : string;
  579. begin
  580. current_scanner.skipspace;
  581. hs:=current_scanner.readid;
  582. valuedescr:= hs;
  583. if hs='' then
  584. Message(scan_e_error_in_preproc_expr);
  585. isdef:=defined_macro(hs);
  586. end;
  587. procedure dir_ifdef;
  588. begin
  589. current_scanner.ifpreprocstack(pp_ifdef,@isdef,scan_c_ifdef_found);
  590. end;
  591. function isnotdef(var valuedescr: String): Boolean;
  592. var
  593. hs : string;
  594. begin
  595. current_scanner.skipspace;
  596. hs:=current_scanner.readid;
  597. valuedescr:= hs;
  598. if hs='' then
  599. Message(scan_e_error_in_preproc_expr);
  600. isnotdef:=not defined_macro(hs);
  601. end;
  602. procedure dir_ifndef;
  603. begin
  604. current_scanner.ifpreprocstack(pp_ifndef,@isnotdef,scan_c_ifndef_found);
  605. end;
  606. function opt_check(var valuedescr: String): Boolean;
  607. var
  608. hs : string;
  609. state : char;
  610. begin
  611. opt_check:= false;
  612. current_scanner.skipspace;
  613. hs:=current_scanner.readid;
  614. valuedescr:= hs;
  615. if (length(hs)>1) then
  616. Message1(scan_w_illegal_switch,hs)
  617. else
  618. begin
  619. state:=current_scanner.ReadState;
  620. if state in ['-','+'] then
  621. opt_check:=CheckSwitch(hs[1],state)
  622. else
  623. Message(scan_e_error_in_preproc_expr);
  624. end;
  625. end;
  626. procedure dir_ifopt;
  627. begin
  628. flushpendingswitchesstate;
  629. current_scanner.ifpreprocstack(pp_ifopt,@opt_check,scan_c_ifopt_found);
  630. end;
  631. procedure dir_libprefix;
  632. var
  633. s : string;
  634. begin
  635. current_scanner.skipspace;
  636. if c <> '''' then
  637. Message2(scan_f_syn_expected, '''', c);
  638. s := current_scanner.readquotedstring;
  639. stringdispose(outputprefix);
  640. outputprefix := stringdup(s);
  641. with current_module do
  642. setfilename(paramfn, paramallowoutput);
  643. end;
  644. procedure dir_libsuffix;
  645. var
  646. s : string;
  647. begin
  648. current_scanner.skipspace;
  649. if c <> '''' then
  650. Message2(scan_f_syn_expected, '''', c);
  651. s := current_scanner.readquotedstring;
  652. stringdispose(outputsuffix);
  653. outputsuffix := stringdup(s);
  654. with current_module do
  655. setfilename(paramfn, paramallowoutput);
  656. end;
  657. procedure dir_extension;
  658. var
  659. s : string;
  660. begin
  661. current_scanner.skipspace;
  662. if c <> '''' then
  663. Message2(scan_f_syn_expected, '''', c);
  664. s := current_scanner.readquotedstring;
  665. if OutputFileName='' then
  666. OutputFileName:=InputFileName;
  667. OutputFileName:=ChangeFileExt(OutputFileName,'.'+s);
  668. with current_module do
  669. setfilename(paramfn, paramallowoutput);
  670. end;
  671. {
  672. Compile time expression type check
  673. ----------------------------------
  674. Each subexpression returns its type to the caller, which then can
  675. do type check. Since data types of compile time expressions is
  676. not well defined, the type system does a best effort. The drawback is
  677. that some errors might not be detected.
  678. Instead of returning a particular data type, a set of possible data types
  679. are returned. This way ambigouos types can be handled. For instance a
  680. value of 1 can be both a boolean and and integer.
  681. Booleans
  682. --------
  683. The following forms of boolean values are supported:
  684. * C coded, that is 0 is false, non-zero is true.
  685. * TRUE/FALSE for mac style compile time variables
  686. Thus boolean mac compile time variables are always stored as TRUE/FALSE.
  687. When a compile time expression is evaluated, they are then translated
  688. to C coded booleans (0/1), to simplify for the expression evaluator.
  689. Note that this scheme then also of support mac compile time variables which
  690. are 0/1 but with a boolean meaning.
  691. The TRUE/FALSE format is new from 22 august 2005, but the above scheme
  692. means that units which is not recompiled, and thus stores
  693. compile time variables as the old format (0/1), continue to work.
  694. Short circuit evaluation
  695. ------------------------
  696. For this to work, the part of a compile time expression which is short
  697. circuited, should not be evaluated, while it still should be parsed.
  698. Therefor there is a parameter eval, telling whether evaluation is needed.
  699. In case not, the value returned can be arbitrary.
  700. }
  701. type
  702. { texprvalue }
  703. texprvalue = class
  704. private
  705. { we can't use built-in defs since they
  706. may be not created at the moment }
  707. class var
  708. sintdef,uintdef,booldef,strdef,setdef,realdef: tdef;
  709. class constructor createdefs;
  710. class destructor destroydefs;
  711. public
  712. consttyp: tconsttyp;
  713. value: tconstvalue;
  714. def: tdef;
  715. constructor create_const(c:tconstsym);
  716. constructor create_error;
  717. constructor create_ord(v: Tconstexprint);
  718. constructor create_int(v: int64);
  719. constructor create_uint(v: qword);
  720. constructor create_bool(b: boolean);
  721. constructor create_str(s: string);
  722. constructor create_set(ns: tnormalset);
  723. constructor create_real(r: bestreal);
  724. class function try_parse_number(s:string):texprvalue; static;
  725. class function try_parse_real(s:string):texprvalue; static;
  726. function evaluate(v:texprvalue;op:ttoken):texprvalue;
  727. procedure error(expecteddef, place: string);
  728. function isBoolean: Boolean;
  729. function asBool: Boolean;
  730. function asInt: Integer;
  731. function asStr: String;
  732. destructor destroy; override;
  733. end;
  734. class constructor texprvalue.createdefs;
  735. begin
  736. sintdef:=torddef.create(s64bit,low(int64),high(int64));
  737. uintdef:=torddef.create(u64bit,low(qword),high(qword));
  738. booldef:=torddef.create(pasbool8,0,1);
  739. strdef:=tstringdef.createansi(0);
  740. setdef:=tsetdef.create(sintdef,0,255);
  741. realdef:=tfloatdef.create(s80real);
  742. end;
  743. class destructor texprvalue.destroydefs;
  744. begin
  745. setdef.free;
  746. sintdef.free;
  747. uintdef.free;
  748. booldef.free;
  749. strdef.free;
  750. realdef.free;
  751. end;
  752. constructor texprvalue.create_const(c: tconstsym);
  753. begin
  754. consttyp:=c.consttyp;
  755. def:=c.constdef;
  756. case consttyp of
  757. conststring,
  758. constresourcestring:
  759. begin
  760. value.len:=c.value.len;
  761. getmem(value.valueptr,value.len+1);
  762. move(c.value.valueptr^,value.valueptr,value.len+1);
  763. end;
  764. constwstring:
  765. begin
  766. initwidestring(value.valueptr);
  767. copywidestring(c.value.valueptr,value.valueptr);
  768. end;
  769. constreal:
  770. begin
  771. new(pbestreal(value.valueptr));
  772. pbestreal(value.valueptr)^:=pbestreal(c.value.valueptr)^;
  773. end;
  774. constset:
  775. begin
  776. new(pnormalset(value.valueptr));
  777. pnormalset(value.valueptr)^:=pnormalset(c.value.valueptr)^;
  778. end;
  779. constguid:
  780. begin
  781. new(pguid(value.valueptr));
  782. pguid(value.valueptr)^:=pguid(c.value.valueptr)^;
  783. end;
  784. else
  785. value:=c.value;
  786. end;
  787. end;
  788. constructor texprvalue.create_error;
  789. begin
  790. fillchar(value,sizeof(value),#0);
  791. consttyp:=constnone;
  792. def:=generrordef;
  793. end;
  794. constructor texprvalue.create_ord(v: Tconstexprint);
  795. begin
  796. fillchar(value,sizeof(value),#0);
  797. consttyp:=constord;
  798. value.valueord:=v;
  799. if v.signed then
  800. def:=sintdef
  801. else
  802. def:=uintdef;
  803. end;
  804. constructor texprvalue.create_int(v: int64);
  805. begin
  806. fillchar(value,sizeof(value),#0);
  807. consttyp:=constord;
  808. value.valueord:=v;
  809. def:=sintdef;
  810. end;
  811. constructor texprvalue.create_uint(v: qword);
  812. begin
  813. fillchar(value,sizeof(value),#0);
  814. consttyp:=constord;
  815. value.valueord:=v;
  816. def:=uintdef;
  817. end;
  818. constructor texprvalue.create_bool(b: boolean);
  819. begin
  820. fillchar(value,sizeof(value),#0);
  821. consttyp:=constord;
  822. value.valueord:=ord(b);
  823. def:=booldef;
  824. end;
  825. constructor texprvalue.create_str(s: string);
  826. var
  827. sp: pansichar;
  828. len: integer;
  829. begin
  830. fillchar(value,sizeof(value),#0);
  831. consttyp:=conststring;
  832. len:=length(s);
  833. getmem(sp,len+1);
  834. move(s[1],sp^,len+1);
  835. value.valueptr:=sp;
  836. value.len:=length(s);
  837. def:=strdef;
  838. end;
  839. constructor texprvalue.create_set(ns: tnormalset);
  840. begin
  841. fillchar(value,sizeof(value),#0);
  842. consttyp:=constset;
  843. new(pnormalset(value.valueptr));
  844. pnormalset(value.valueptr)^:=ns;
  845. def:=setdef;
  846. end;
  847. constructor texprvalue.create_real(r: bestreal);
  848. begin
  849. fillchar(value,sizeof(value),#0);
  850. consttyp:=constreal;
  851. new(pbestreal(value.valueptr));
  852. pbestreal(value.valueptr)^:=r;
  853. def:=realdef;
  854. end;
  855. class function texprvalue.try_parse_number(s:string):texprvalue;
  856. var
  857. ic: int64;
  858. qc: qword;
  859. code: integer;
  860. begin
  861. { try int64 }
  862. val(s,ic,code);
  863. if code=0 then
  864. result:=texprvalue.create_int(ic)
  865. else
  866. begin
  867. { try qword }
  868. val(s,qc,code);
  869. if code=0 then
  870. result:=texprvalue.create_uint(qc)
  871. else
  872. result:=try_parse_real(s);
  873. end;
  874. end;
  875. class function texprvalue.try_parse_real(s:string):texprvalue;
  876. var
  877. d: bestreal;
  878. code: integer;
  879. begin
  880. val(s,d,code);
  881. if code=0 then
  882. result:=texprvalue.create_real(d)
  883. else
  884. result:=nil;
  885. end;
  886. function texprvalue.evaluate(v:texprvalue;op:ttoken):texprvalue;
  887. function check_compatbile: boolean;
  888. begin
  889. result:=(
  890. (is_ordinal(v.def) or is_fpu(v.def)) and
  891. (is_ordinal(def) or is_fpu(def))
  892. ) or
  893. (is_string(v.def) and is_string(def));
  894. if not result then
  895. Message2(type_e_incompatible_types,def.typename,v.def.typename);
  896. end;
  897. var
  898. lv,rv: tconstexprint;
  899. lvd,rvd: bestreal;
  900. lvs,rvs: string;
  901. begin
  902. case op of
  903. _OP_IN:
  904. begin
  905. if not is_set(v.def) then
  906. begin
  907. v.error('Set', 'IN');
  908. result:=texprvalue.create_error;
  909. end
  910. else
  911. if not is_ordinal(def) then
  912. begin
  913. error('Ordinal', 'IN');
  914. result:=texprvalue.create_error;
  915. end
  916. else
  917. if value.valueord.signed then
  918. result:=texprvalue.create_bool(value.valueord.svalue in pnormalset(v.value.valueptr)^)
  919. else
  920. result:=texprvalue.create_bool(value.valueord.uvalue in pnormalset(v.value.valueptr)^);
  921. end;
  922. _OP_NOT:
  923. begin
  924. if isBoolean then
  925. result:=texprvalue.create_bool(not asBool)
  926. else
  927. begin
  928. error('Boolean', 'NOT');
  929. result:=texprvalue.create_error;
  930. end;
  931. end;
  932. _OP_OR:
  933. begin
  934. if isBoolean then
  935. if v.isBoolean then
  936. result:=texprvalue.create_bool(asBool or v.asBool)
  937. else
  938. begin
  939. v.error('Boolean','OR');
  940. result:=texprvalue.create_error;
  941. end
  942. else
  943. begin
  944. error('Boolean','OR');
  945. result:=texprvalue.create_error;
  946. end;
  947. end;
  948. _OP_AND:
  949. begin
  950. if isBoolean then
  951. if v.isBoolean then
  952. result:=texprvalue.create_bool(asBool and v.asBool)
  953. else
  954. begin
  955. v.error('Boolean','AND');
  956. result:=texprvalue.create_error;
  957. end
  958. else
  959. begin
  960. error('Boolean','AND');
  961. result:=texprvalue.create_error;
  962. end;
  963. end;
  964. _EQ,_NE,_LT,_GT,_GTE,_LTE,_PLUS,_MINUS,_STAR,_SLASH:
  965. if check_compatbile then
  966. begin
  967. if (is_ordinal(def) and is_ordinal(v.def)) then
  968. begin
  969. lv:=value.valueord;
  970. rv:=v.value.valueord;
  971. case op of
  972. _EQ:
  973. result:=texprvalue.create_bool(lv=rv);
  974. _NE:
  975. result:=texprvalue.create_bool(lv<>rv);
  976. _LT:
  977. result:=texprvalue.create_bool(lv<rv);
  978. _GT:
  979. result:=texprvalue.create_bool(lv>rv);
  980. _GTE:
  981. result:=texprvalue.create_bool(lv>=rv);
  982. _LTE:
  983. result:=texprvalue.create_bool(lv<=rv);
  984. _PLUS:
  985. result:=texprvalue.create_ord(lv+rv);
  986. _MINUS:
  987. result:=texprvalue.create_ord(lv-rv);
  988. _STAR:
  989. result:=texprvalue.create_ord(lv*rv);
  990. _SLASH:
  991. result:=texprvalue.create_real(lv/rv);
  992. end;
  993. end
  994. else
  995. if (is_fpu(def) or is_ordinal(def)) and
  996. (is_fpu(v.def) or is_ordinal(v.def)) then
  997. begin
  998. if is_fpu(def) then
  999. lvd:=pbestreal(value.valueptr)^
  1000. else
  1001. lvd:=value.valueord;
  1002. if is_fpu(v.def) then
  1003. rvd:=pbestreal(v.value.valueptr)^
  1004. else
  1005. rvd:=v.value.valueord;
  1006. case op of
  1007. _EQ:
  1008. result:=texprvalue.create_bool(lvd=rvd);
  1009. _NE:
  1010. result:=texprvalue.create_bool(lvd<>rvd);
  1011. _LT:
  1012. result:=texprvalue.create_bool(lvd<rvd);
  1013. _GT:
  1014. result:=texprvalue.create_bool(lvd>rvd);
  1015. _GTE:
  1016. result:=texprvalue.create_bool(lvd>=rvd);
  1017. _LTE:
  1018. result:=texprvalue.create_bool(lvd<=rvd);
  1019. _PLUS:
  1020. result:=texprvalue.create_real(lvd+rvd);
  1021. _MINUS:
  1022. result:=texprvalue.create_real(lvd-rvd);
  1023. _STAR:
  1024. result:=texprvalue.create_real(lvd*rvd);
  1025. _SLASH:
  1026. result:=texprvalue.create_real(lvd/rvd);
  1027. end;
  1028. end
  1029. else
  1030. begin
  1031. lvs:=asStr;
  1032. rvs:=v.asStr;
  1033. case op of
  1034. _EQ:
  1035. result:=texprvalue.create_bool(lvs=rvs);
  1036. _NE:
  1037. result:=texprvalue.create_bool(lvs<>rvs);
  1038. _LT:
  1039. result:=texprvalue.create_bool(lvs<rvs);
  1040. _GT:
  1041. result:=texprvalue.create_bool(lvs>rvs);
  1042. _GTE:
  1043. result:=texprvalue.create_bool(lvs>=rvs);
  1044. _LTE:
  1045. result:=texprvalue.create_bool(lvs<=rvs);
  1046. _PLUS:
  1047. result:=texprvalue.create_str(lvs+rvs);
  1048. _MINUS, _STAR, _SLASH:
  1049. begin
  1050. Message(parser_e_illegal_expression);
  1051. result:=texprvalue.create_error;
  1052. end;
  1053. end;
  1054. end;
  1055. end
  1056. else
  1057. result:=texprvalue.create_error;
  1058. end;
  1059. end;
  1060. procedure texprvalue.error(expecteddef, place: string);
  1061. begin
  1062. Message3(scan_e_compile_time_typeerror,
  1063. expecteddef,
  1064. def.typename,
  1065. place
  1066. );
  1067. end;
  1068. function texprvalue.isBoolean: Boolean;
  1069. var
  1070. i: integer;
  1071. begin
  1072. result:=is_boolean(def);
  1073. if not result and is_integer(def) then
  1074. begin
  1075. i:=asInt;
  1076. result:=(i=0)or(i=1);
  1077. end;
  1078. end;
  1079. function texprvalue.asBool: Boolean;
  1080. begin
  1081. result:=value.valueord<>0;
  1082. end;
  1083. function texprvalue.asInt: Integer;
  1084. begin
  1085. result:=value.valueord.svalue;
  1086. end;
  1087. function texprvalue.asStr: String;
  1088. var
  1089. b:byte;
  1090. begin
  1091. case consttyp of
  1092. constord:
  1093. result:=tostr(value.valueord);
  1094. conststring,
  1095. constresourcestring:
  1096. SetString(result,pchar(value.valueptr),value.len);
  1097. constreal:
  1098. str(pbestreal(value.valueptr)^,result);
  1099. constset:
  1100. begin
  1101. result:=',';
  1102. for b:=0 to 255 do
  1103. if b in pconstset(value.valueptr)^ then
  1104. result:=result+tostr(b)+',';
  1105. end;
  1106. end;
  1107. end;
  1108. destructor texprvalue.destroy;
  1109. begin
  1110. case consttyp of
  1111. conststring,
  1112. constresourcestring :
  1113. freemem(pchar(value.valueptr),value.len+1);
  1114. constwstring :
  1115. donewidestring(pcompilerwidestring(value.valueptr));
  1116. constreal :
  1117. dispose(pbestreal(value.valueptr));
  1118. constset :
  1119. dispose(pnormalset(value.valueptr));
  1120. constguid :
  1121. dispose(pguid(value.valueptr));
  1122. end;
  1123. inherited destroy;
  1124. end;
  1125. const
  1126. preproc_operators=[_EQ,_NE,_LT,_GT,_LTE,_GTE,_MINUS,_PLUS,_STAR,_SLASH,_OP_IN,_OP_AND,_OP_OR];
  1127. function preproc_comp_expr:texprvalue;
  1128. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean):texprvalue; forward;
  1129. procedure preproc_consume(t:ttoken);
  1130. begin
  1131. if t<>current_scanner.preproc_token then
  1132. Message(scan_e_preproc_syntax_error);
  1133. current_scanner.preproc_token:=current_scanner.readpreproc;
  1134. end;
  1135. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;out tokentoconsume:ttoken):boolean;
  1136. var
  1137. hmodule: tmodule;
  1138. ns:ansistring;
  1139. nssym:tsym;
  1140. begin
  1141. result:=false;
  1142. tokentoconsume:=_ID;
  1143. if assigned(srsym) and (srsym.typ in [unitsym,namespacesym]) then
  1144. begin
  1145. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  1146. internalerror(200501154);
  1147. { only allow unit.symbol access if the name was
  1148. found in the current module
  1149. we can use iscurrentunit because generic specializations does not
  1150. change current_unit variable }
  1151. hmodule:=find_module_from_symtable(srsym.Owner);
  1152. if not Assigned(hmodule) then
  1153. internalerror(201001120);
  1154. if hmodule.unit_index=current_filepos.moduleindex then
  1155. begin
  1156. preproc_consume(_POINT);
  1157. current_scanner.skipspace;
  1158. if srsym.typ=namespacesym then
  1159. begin
  1160. ns:=srsym.name;
  1161. nssym:=srsym;
  1162. while assigned(srsym) and (srsym.typ=namespacesym) do
  1163. begin
  1164. { we have a namespace. the next identifier should be either a namespace or a unit }
  1165. searchsym_in_module(hmodule,ns+'.'+current_scanner.preproc_pattern,srsym,srsymtable);
  1166. if assigned(srsym) and (srsym.typ in [namespacesym,unitsym]) then
  1167. begin
  1168. ns:=ns+'.'+current_scanner.preproc_pattern;
  1169. nssym:=srsym;
  1170. preproc_consume(_ID);
  1171. current_scanner.skipspace;
  1172. preproc_consume(_POINT);
  1173. current_scanner.skipspace;
  1174. end;
  1175. end;
  1176. { check if there is a hidden unit with this pattern in the namespace }
  1177. if not assigned(srsym) and
  1178. assigned(nssym) and (nssym.typ=namespacesym) and assigned(tnamespacesym(nssym).unitsym) then
  1179. srsym:=tnamespacesym(nssym).unitsym;
  1180. if assigned(srsym) and (srsym.typ<>unitsym) then
  1181. internalerror(201108260);
  1182. if not assigned(srsym) then
  1183. begin
  1184. result:=true;
  1185. srsymtable:=nil;
  1186. exit;
  1187. end;
  1188. end;
  1189. case current_scanner.preproc_token of
  1190. _ID:
  1191. { system.char? (char=widechar comes from the implicit
  1192. uuchar unit -> override) }
  1193. if (current_scanner.preproc_pattern='CHAR') and
  1194. (tmodule(tunitsym(srsym).module).globalsymtable=systemunit) then
  1195. begin
  1196. if m_default_unicodestring in current_settings.modeswitches then
  1197. searchsym_in_module(tunitsym(srsym).module,'WIDECHAR',srsym,srsymtable)
  1198. else
  1199. searchsym_in_module(tunitsym(srsym).module,'ANSICHAR',srsym,srsymtable)
  1200. end
  1201. else
  1202. searchsym_in_module(tunitsym(srsym).module,current_scanner.preproc_pattern,srsym,srsymtable);
  1203. _STRING:
  1204. begin
  1205. { system.string? }
  1206. if tmodule(tunitsym(srsym).module).globalsymtable=systemunit then
  1207. begin
  1208. if cs_refcountedstrings in current_settings.localswitches then
  1209. begin
  1210. if m_default_unicodestring in current_settings.modeswitches then
  1211. searchsym_in_module(tunitsym(srsym).module,'UNICODESTRING',srsym,srsymtable)
  1212. else
  1213. searchsym_in_module(tunitsym(srsym).module,'ANSISTRING',srsym,srsymtable)
  1214. end
  1215. else
  1216. searchsym_in_module(tunitsym(srsym).module,'SHORTSTRING',srsym,srsymtable);
  1217. tokentoconsume:=_STRING;
  1218. end;
  1219. end
  1220. end;
  1221. end
  1222. else
  1223. begin
  1224. srsym:=nil;
  1225. srsymtable:=nil;
  1226. end;
  1227. result:=true;
  1228. end;
  1229. end;
  1230. procedure try_consume_nestedsym(var srsym:tsym;var srsymtable:TSymtable);
  1231. var
  1232. def:tdef;
  1233. tokentoconsume:ttoken;
  1234. found:boolean;
  1235. begin
  1236. found:=try_consume_unitsym(srsym,srsymtable,tokentoconsume);
  1237. if found then
  1238. begin
  1239. preproc_consume(tokentoconsume);
  1240. current_scanner.skipspace;
  1241. end;
  1242. while (current_scanner.preproc_token=_POINT) do
  1243. begin
  1244. if assigned(srsym)and(srsym.typ=typesym) then
  1245. begin
  1246. def:=ttypesym(srsym).typedef;
  1247. if is_class_or_object(def) or is_record(def) or is_java_class_or_interface(def) then
  1248. begin
  1249. preproc_consume(_POINT);
  1250. current_scanner.skipspace;
  1251. if def.typ=objectdef then
  1252. found:=searchsym_in_class(tobjectdef(def),tobjectdef(def),current_scanner.preproc_pattern,srsym,srsymtable,true)
  1253. else
  1254. found:=searchsym_in_record(trecorddef(def),current_scanner.preproc_pattern,srsym,srsymtable);
  1255. if not found then
  1256. begin
  1257. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  1258. exit;
  1259. end;
  1260. preproc_consume(_ID);
  1261. current_scanner.skipspace;
  1262. end
  1263. else
  1264. begin
  1265. Message(sym_e_type_must_be_rec_or_object_or_class);
  1266. exit;
  1267. end;
  1268. end
  1269. else
  1270. begin
  1271. Message(type_e_type_id_expected);
  1272. exit;
  1273. end;
  1274. end;
  1275. end;
  1276. function preproc_substitutedtoken(eval : Boolean): texprvalue;
  1277. { Currently this parses identifiers as well as numbers.
  1278. The result from this procedure can either be that the token
  1279. itself is a value, or that it is a compile time variable/macro,
  1280. which then is substituted for another value (for macros
  1281. recursivelly substituted).}
  1282. var
  1283. hs,pp: string;
  1284. mac: tmacro;
  1285. macrocount,
  1286. len: integer;
  1287. begin
  1288. pp:=current_scanner.preproc_pattern;
  1289. if not eval then
  1290. begin
  1291. result:=texprvalue.create_str(pp);
  1292. exit;
  1293. end;
  1294. mac:= nil;
  1295. { Substitue macros and compiler variables with their content/value.
  1296. For real macros also do recursive substitution. }
  1297. macrocount:=0;
  1298. repeat
  1299. mac:=tmacro(search_macro(pp));
  1300. inc(macrocount);
  1301. if macrocount>max_macro_nesting then
  1302. begin
  1303. Message(scan_w_macro_too_deep);
  1304. break;
  1305. end;
  1306. if assigned(mac) and mac.defined then
  1307. if assigned(mac.buftext) then
  1308. begin
  1309. if mac.buflen>255 then
  1310. begin
  1311. len:=255;
  1312. Message(scan_w_macro_cut_after_255_chars);
  1313. end
  1314. else
  1315. len:=mac.buflen;
  1316. hs[0]:=char(len);
  1317. move(mac.buftext^,hs[1],len);
  1318. pp:=upcase(hs);
  1319. mac.is_used:=true;
  1320. end
  1321. else
  1322. begin
  1323. Message1(scan_e_error_macro_lacks_value, pp);
  1324. break;
  1325. end
  1326. else
  1327. begin
  1328. break;
  1329. end;
  1330. if mac.is_compiler_var then
  1331. break;
  1332. until false;
  1333. { At this point, result do contain the value. Do some decoding and
  1334. determine the type.}
  1335. result:=texprvalue.try_parse_number(pp);
  1336. if not assigned(result) then
  1337. begin
  1338. if assigned(mac) and (pp='FALSE') then
  1339. result:=texprvalue.create_bool(false)
  1340. else if assigned(mac) and (pp='TRUE') then
  1341. result:=texprvalue.create_bool(true)
  1342. else if (m_mac in current_settings.modeswitches) and
  1343. (not assigned(mac) or not mac.defined) and
  1344. (macrocount = 1) then
  1345. begin
  1346. {Errors in mode mac is issued here. For non macpas modes there is
  1347. more liberty, but the error will eventually be caught at a later stage.}
  1348. Message1(scan_e_error_macro_undefined, pp);
  1349. result:=texprvalue.create_str(pp); { just to have something }
  1350. end
  1351. else
  1352. result:=texprvalue.create_str(pp);
  1353. end;
  1354. end;
  1355. function preproc_factor(eval: Boolean):texprvalue;
  1356. var
  1357. hs,countstr,storedpattern: string;
  1358. mac: tmacro;
  1359. srsym : tsym;
  1360. srsymtable : TSymtable;
  1361. hdef : TDef;
  1362. l : longint;
  1363. hasKlammer: Boolean;
  1364. exprvalue:texprvalue;
  1365. ns:tnormalset;
  1366. begin
  1367. result:=nil;
  1368. if current_scanner.preproc_token=_ID then
  1369. begin
  1370. if current_scanner.preproc_pattern='DEFINED' then
  1371. begin
  1372. preproc_consume(_ID);
  1373. current_scanner.skipspace;
  1374. if current_scanner.preproc_token =_LKLAMMER then
  1375. begin
  1376. preproc_consume(_LKLAMMER);
  1377. current_scanner.skipspace;
  1378. hasKlammer:= true;
  1379. end
  1380. else if (m_mac in current_settings.modeswitches) then
  1381. hasKlammer:= false
  1382. else
  1383. Message(scan_e_error_in_preproc_expr);
  1384. if current_scanner.preproc_token =_ID then
  1385. begin
  1386. hs := current_scanner.preproc_pattern;
  1387. mac := tmacro(search_macro(hs));
  1388. if assigned(mac) and mac.defined then
  1389. begin
  1390. result:=texprvalue.create_bool(true);
  1391. mac.is_used:=true;
  1392. end
  1393. else
  1394. result:=texprvalue.create_bool(false);
  1395. preproc_consume(_ID);
  1396. current_scanner.skipspace;
  1397. end
  1398. else
  1399. Message(scan_e_error_in_preproc_expr);
  1400. if hasKlammer then
  1401. if current_scanner.preproc_token =_RKLAMMER then
  1402. preproc_consume(_RKLAMMER)
  1403. else
  1404. Message(scan_e_error_in_preproc_expr);
  1405. end
  1406. else
  1407. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  1408. begin
  1409. preproc_consume(_ID);
  1410. current_scanner.skipspace;
  1411. if current_scanner.preproc_token =_ID then
  1412. begin
  1413. hs := current_scanner.preproc_pattern;
  1414. mac := tmacro(search_macro(hs));
  1415. if assigned(mac) then
  1416. begin
  1417. result:=texprvalue.create_bool(false);
  1418. mac.is_used:=true;
  1419. end
  1420. else
  1421. result:=texprvalue.create_bool(true);
  1422. preproc_consume(_ID);
  1423. current_scanner.skipspace;
  1424. end
  1425. else
  1426. Message(scan_e_error_in_preproc_expr);
  1427. end
  1428. else
  1429. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  1430. begin
  1431. preproc_consume(_ID);
  1432. current_scanner.skipspace;
  1433. if current_scanner.preproc_token =_LKLAMMER then
  1434. begin
  1435. preproc_consume(_LKLAMMER);
  1436. current_scanner.skipspace;
  1437. end
  1438. else
  1439. Message(scan_e_error_in_preproc_expr);
  1440. if not (current_scanner.preproc_token = _ID) then
  1441. Message(scan_e_error_in_preproc_expr);
  1442. hs:=current_scanner.preproc_pattern;
  1443. if (length(hs) > 1) then
  1444. {This is allowed in Metrowerks Pascal}
  1445. Message(scan_e_error_in_preproc_expr)
  1446. else
  1447. begin
  1448. if CheckSwitch(hs[1],'+') then
  1449. result:=texprvalue.create_bool(true)
  1450. else
  1451. result:=texprvalue.create_bool(false);
  1452. end;
  1453. preproc_consume(_ID);
  1454. current_scanner.skipspace;
  1455. if current_scanner.preproc_token =_RKLAMMER then
  1456. preproc_consume(_RKLAMMER)
  1457. else
  1458. Message(scan_e_error_in_preproc_expr);
  1459. end
  1460. else
  1461. if current_scanner.preproc_pattern='SIZEOF' then
  1462. begin
  1463. preproc_consume(_ID);
  1464. current_scanner.skipspace;
  1465. if current_scanner.preproc_token =_LKLAMMER then
  1466. begin
  1467. preproc_consume(_LKLAMMER);
  1468. current_scanner.skipspace;
  1469. end
  1470. else
  1471. Message(scan_e_preproc_syntax_error);
  1472. storedpattern:=current_scanner.preproc_pattern;
  1473. preproc_consume(_ID);
  1474. current_scanner.skipspace;
  1475. if eval then
  1476. if searchsym(storedpattern,srsym,srsymtable) then
  1477. begin
  1478. try_consume_nestedsym(srsym,srsymtable);
  1479. l:=0;
  1480. if assigned(srsym) then
  1481. case srsym.typ of
  1482. staticvarsym,
  1483. localvarsym,
  1484. paravarsym :
  1485. l:=tabstractvarsym(srsym).getsize;
  1486. typesym:
  1487. l:=ttypesym(srsym).typedef.size;
  1488. else
  1489. Message(scan_e_error_in_preproc_expr);
  1490. end;
  1491. result:=texprvalue.create_int(l);
  1492. end
  1493. else
  1494. Message1(sym_e_id_not_found,storedpattern);
  1495. if current_scanner.preproc_token =_RKLAMMER then
  1496. preproc_consume(_RKLAMMER)
  1497. else
  1498. Message(scan_e_preproc_syntax_error);
  1499. end
  1500. else
  1501. if current_scanner.preproc_pattern='HIGH' then
  1502. begin
  1503. preproc_consume(_ID);
  1504. current_scanner.skipspace;
  1505. if current_scanner.preproc_token =_LKLAMMER then
  1506. begin
  1507. preproc_consume(_LKLAMMER);
  1508. current_scanner.skipspace;
  1509. end
  1510. else
  1511. Message(scan_e_preproc_syntax_error);
  1512. storedpattern:=current_scanner.preproc_pattern;
  1513. preproc_consume(_ID);
  1514. current_scanner.skipspace;
  1515. if eval then
  1516. if searchsym(storedpattern,srsym,srsymtable) then
  1517. begin
  1518. try_consume_nestedsym(srsym,srsymtable);
  1519. hdef:=nil;
  1520. hs:='';
  1521. l:=0;
  1522. if assigned(srsym) then
  1523. case srsym.typ of
  1524. staticvarsym,
  1525. localvarsym,
  1526. paravarsym :
  1527. hdef:=tabstractvarsym(srsym).vardef;
  1528. typesym:
  1529. hdef:=ttypesym(srsym).typedef;
  1530. else
  1531. Message(scan_e_error_in_preproc_expr);
  1532. end;
  1533. if assigned(hdef) then
  1534. begin
  1535. if hdef.typ=setdef then
  1536. hdef:=tsetdef(hdef).elementdef;
  1537. case hdef.typ of
  1538. orddef:
  1539. with torddef(hdef).high do
  1540. if signed then
  1541. result:=texprvalue.create_int(svalue)
  1542. else
  1543. result:=texprvalue.create_uint(uvalue);
  1544. enumdef:
  1545. result:=texprvalue.create_int(tenumdef(hdef).maxval);
  1546. arraydef:
  1547. if is_open_array(hdef) or is_array_of_const(hdef) or is_dynamic_array(hdef) then
  1548. Message(type_e_mismatch)
  1549. else
  1550. result:=texprvalue.create_int(tarraydef(hdef).highrange);
  1551. stringdef:
  1552. if is_open_string(hdef) or is_ansistring(hdef) or is_wide_or_unicode_string(hdef) then
  1553. Message(type_e_mismatch)
  1554. else
  1555. result:=texprvalue.create_int(tstringdef(hdef).len);
  1556. else
  1557. Message(type_e_mismatch);
  1558. end;
  1559. end;
  1560. end
  1561. else
  1562. Message1(sym_e_id_not_found,storedpattern);
  1563. if current_scanner.preproc_token =_RKLAMMER then
  1564. preproc_consume(_RKLAMMER)
  1565. else
  1566. Message(scan_e_preproc_syntax_error);
  1567. end
  1568. else
  1569. if current_scanner.preproc_pattern='DECLARED' then
  1570. begin
  1571. preproc_consume(_ID);
  1572. current_scanner.skipspace;
  1573. if current_scanner.preproc_token =_LKLAMMER then
  1574. begin
  1575. preproc_consume(_LKLAMMER);
  1576. current_scanner.skipspace;
  1577. end
  1578. else
  1579. Message(scan_e_error_in_preproc_expr);
  1580. if current_scanner.preproc_token =_ID then
  1581. begin
  1582. hs := upper(current_scanner.preproc_pattern);
  1583. preproc_consume(_ID);
  1584. current_scanner.skipspace;
  1585. if current_scanner.preproc_token in [_LT,_LSHARPBRACKET] then
  1586. begin
  1587. l:=1;
  1588. preproc_consume(current_scanner.preproc_token);
  1589. current_scanner.skipspace;
  1590. while current_scanner.preproc_token=_COMMA do
  1591. begin
  1592. inc(l);
  1593. preproc_consume(_COMMA);
  1594. current_scanner.skipspace;
  1595. end;
  1596. if not (current_scanner.preproc_token in [_GT,_RSHARPBRACKET]) then
  1597. Message(scan_e_error_in_preproc_expr)
  1598. else
  1599. preproc_consume(current_scanner.preproc_token);
  1600. str(l,countstr);
  1601. hs:=hs+'$'+countstr;
  1602. end
  1603. else
  1604. { special case: <> }
  1605. if current_scanner.preproc_token=_NE then
  1606. begin
  1607. hs:=hs+'$1';
  1608. preproc_consume(_NE);
  1609. end;
  1610. current_scanner.skipspace;
  1611. if searchsym(hs,srsym,srsymtable) then
  1612. begin
  1613. { TSomeGeneric<...> also adds a TSomeGeneric symbol }
  1614. if (sp_generic_dummy in srsym.symoptions) and
  1615. (srsym.typ=typesym) and
  1616. (
  1617. { mode delphi}
  1618. (ttypesym(srsym).typedef.typ in [undefineddef,errordef]) or
  1619. { non-delphi modes }
  1620. (df_generic in ttypesym(srsym).typedef.defoptions)
  1621. ) then
  1622. result:=texprvalue.create_bool(false)
  1623. else
  1624. result:=texprvalue.create_bool(true);
  1625. end
  1626. else
  1627. result:=texprvalue.create_bool(false);
  1628. end
  1629. else
  1630. Message(scan_e_error_in_preproc_expr);
  1631. if current_scanner.preproc_token =_RKLAMMER then
  1632. preproc_consume(_RKLAMMER)
  1633. else
  1634. Message(scan_e_error_in_preproc_expr);
  1635. end
  1636. else
  1637. if current_scanner.preproc_pattern='NOT' then
  1638. begin
  1639. preproc_consume(_ID);
  1640. exprvalue:=preproc_factor(eval);
  1641. if eval then
  1642. result:=exprvalue.evaluate(nil,_OP_NOT)
  1643. else
  1644. result:=texprvalue.create_bool(false); {Just to have something}
  1645. exprvalue.free;
  1646. end
  1647. else
  1648. if (current_scanner.preproc_pattern='TRUE') then
  1649. begin
  1650. result:=texprvalue.create_bool(true);
  1651. preproc_consume(_ID);
  1652. end
  1653. else
  1654. if (current_scanner.preproc_pattern='FALSE') then
  1655. begin
  1656. result:=texprvalue.create_bool(false);
  1657. preproc_consume(_ID);
  1658. end
  1659. else
  1660. begin
  1661. result:=preproc_substitutedtoken(eval);
  1662. { Default is to return the original symbol }
  1663. storedpattern:=current_scanner.preproc_pattern;
  1664. preproc_consume(_ID);
  1665. current_scanner.skipspace;
  1666. if eval and (result.consttyp=conststring) then
  1667. if searchsym(storedpattern,srsym,srsymtable) then
  1668. begin
  1669. try_consume_nestedsym(srsym,srsymtable);
  1670. if assigned(srsym) then
  1671. case srsym.typ of
  1672. constsym :
  1673. begin
  1674. result.free;
  1675. result:=texprvalue.create_const(tconstsym(srsym));
  1676. end;
  1677. enumsym :
  1678. begin
  1679. result.free;
  1680. result:=texprvalue.create_int(tenumsym(srsym).value);
  1681. end;
  1682. end;
  1683. end;
  1684. end
  1685. end
  1686. else if current_scanner.preproc_token =_LKLAMMER then
  1687. begin
  1688. preproc_consume(_LKLAMMER);
  1689. result:=preproc_sub_expr(opcompare,true);
  1690. preproc_consume(_RKLAMMER);
  1691. end
  1692. else if current_scanner.preproc_token = _LECKKLAMMER then
  1693. begin
  1694. preproc_consume(_LECKKLAMMER);
  1695. ns:=[];
  1696. while current_scanner.preproc_token in [_ID,_INTCONST] do
  1697. begin
  1698. exprvalue:=preproc_factor(eval);
  1699. include(ns,exprvalue.asInt);
  1700. if current_scanner.preproc_token = _COMMA then
  1701. preproc_consume(_COMMA);
  1702. end;
  1703. // TODO Add check of setElemType
  1704. preproc_consume(_RECKKLAMMER);
  1705. result:=texprvalue.create_set(ns);
  1706. end
  1707. else if current_scanner.preproc_token = _INTCONST then
  1708. begin
  1709. result:=texprvalue.try_parse_number(current_scanner.preproc_pattern);
  1710. if not assigned(result) then
  1711. begin
  1712. Message(parser_e_invalid_integer);
  1713. result:=texprvalue.create_int(1);
  1714. end;
  1715. preproc_consume(_INTCONST);
  1716. end
  1717. else if current_scanner.preproc_token = _REALNUMBER then
  1718. begin
  1719. result:=texprvalue.try_parse_real(current_scanner.preproc_pattern);
  1720. if not assigned(result) then
  1721. begin
  1722. Message(parser_e_error_in_real);
  1723. result:=texprvalue.create_real(1.0);
  1724. end;
  1725. preproc_consume(_REALNUMBER);
  1726. end
  1727. else
  1728. Message(scan_e_error_in_preproc_expr);
  1729. if not assigned(result) then
  1730. result:=texprvalue.create_error;
  1731. end;
  1732. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean): texprvalue;
  1733. var
  1734. hs1,hs2: texprvalue;
  1735. op: ttoken;
  1736. begin
  1737. if pred_level=highest_precedence then
  1738. result:=preproc_factor(eval)
  1739. else
  1740. result:=preproc_sub_expr(succ(pred_level),eval);
  1741. repeat
  1742. op:=current_scanner.preproc_token;
  1743. if (op in preproc_operators) and
  1744. (op in operator_levels[pred_level]) then
  1745. begin
  1746. hs1:=result;
  1747. preproc_consume(op);
  1748. if (op=_OP_OR)and hs1.isBoolean and hs1.asBool then
  1749. begin
  1750. { stop evaluation the rest of expression }
  1751. result:=texprvalue.create_bool(true);
  1752. if pred_level=highest_precedence then
  1753. hs2:=preproc_factor(false)
  1754. else
  1755. hs2:=preproc_sub_expr(succ(pred_level),false);
  1756. end
  1757. else if (op=_OP_AND)and hs1.isBoolean and not hs1.asBool then
  1758. begin
  1759. { stop evaluation the rest of expression }
  1760. result:=texprvalue.create_bool(false);
  1761. if pred_level=highest_precedence then
  1762. hs2:=preproc_factor(false)
  1763. else
  1764. hs2:=preproc_sub_expr(succ(pred_level),false);
  1765. end
  1766. else
  1767. begin
  1768. if pred_level=highest_precedence then
  1769. hs2:=preproc_factor(eval)
  1770. else
  1771. hs2:=preproc_sub_expr(succ(pred_level),eval);
  1772. if eval then
  1773. result:=hs1.evaluate(hs2,op)
  1774. else
  1775. result:=texprvalue.create_bool(false); {Just to have something}
  1776. end;
  1777. hs1.free;
  1778. hs2.free;
  1779. end
  1780. else
  1781. break;
  1782. until false;
  1783. end;
  1784. begin
  1785. current_scanner.skipspace;
  1786. { start preproc expression scanner }
  1787. current_scanner.preproc_token:=current_scanner.readpreproc;
  1788. preproc_comp_expr:=preproc_sub_expr(opcompare,true);
  1789. end;
  1790. function boolean_compile_time_expr(var valuedescr: string): Boolean;
  1791. var
  1792. hs: texprvalue;
  1793. begin
  1794. hs:=preproc_comp_expr;
  1795. if hs.isBoolean then
  1796. result:=hs.asBool
  1797. else
  1798. begin
  1799. hs.error('Boolean', 'IF or ELSEIF');
  1800. result:=false;
  1801. end;
  1802. valuedescr:=hs.asStr;
  1803. hs.free;
  1804. end;
  1805. procedure dir_if;
  1806. begin
  1807. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  1808. end;
  1809. procedure dir_elseif;
  1810. begin
  1811. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  1812. end;
  1813. procedure dir_define_impl(macstyle: boolean);
  1814. var
  1815. hs : string;
  1816. bracketcount : longint;
  1817. mac : tmacro;
  1818. macropos : longint;
  1819. macrobuffer : pmacrobuffer;
  1820. begin
  1821. current_scanner.skipspace;
  1822. hs:=current_scanner.readid;
  1823. mac:=tmacro(search_macro(hs));
  1824. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  1825. begin
  1826. mac:=tmacro.create(hs);
  1827. mac.defined:=true;
  1828. current_module.localmacrosymtable.insert(mac);
  1829. end
  1830. else
  1831. begin
  1832. mac.defined:=true;
  1833. mac.is_compiler_var:=false;
  1834. { delete old definition }
  1835. if assigned(mac.buftext) then
  1836. begin
  1837. freemem(mac.buftext,mac.buflen);
  1838. mac.buftext:=nil;
  1839. end;
  1840. end;
  1841. Message1(parser_c_macro_defined,mac.name);
  1842. mac.is_used:=true;
  1843. if (cs_support_macro in current_settings.moduleswitches) then
  1844. begin
  1845. current_scanner.skipspace;
  1846. if not macstyle then
  1847. begin
  1848. { may be a macro? }
  1849. if c <> ':' then
  1850. exit;
  1851. current_scanner.readchar;
  1852. if c <> '=' then
  1853. exit;
  1854. current_scanner.readchar;
  1855. current_scanner.skipspace;
  1856. end;
  1857. { key words are never substituted }
  1858. if is_keyword(hs) then
  1859. Message(scan_e_keyword_cant_be_a_macro);
  1860. new(macrobuffer);
  1861. macropos:=0;
  1862. { parse macro, brackets are counted so it's possible
  1863. to have a $ifdef etc. in the macro }
  1864. bracketcount:=0;
  1865. repeat
  1866. case c of
  1867. '}' :
  1868. if (bracketcount=0) then
  1869. break
  1870. else
  1871. dec(bracketcount);
  1872. '{' :
  1873. inc(bracketcount);
  1874. #10,#13 :
  1875. current_scanner.linebreak;
  1876. #26 :
  1877. current_scanner.end_of_file;
  1878. end;
  1879. macrobuffer^[macropos]:=c;
  1880. inc(macropos);
  1881. if macropos>=maxmacrolen then
  1882. Message(scan_f_macro_buffer_overflow);
  1883. current_scanner.readchar;
  1884. until false;
  1885. { free buffer of macro ?}
  1886. if assigned(mac.buftext) then
  1887. freemem(mac.buftext,mac.buflen);
  1888. { get new mem }
  1889. getmem(mac.buftext,macropos);
  1890. mac.buflen:=macropos;
  1891. { copy the text }
  1892. move(macrobuffer^,mac.buftext^,macropos);
  1893. dispose(macrobuffer);
  1894. end
  1895. else
  1896. begin
  1897. { check if there is an assignment, then we need to give a
  1898. warning }
  1899. current_scanner.skipspace;
  1900. if c=':' then
  1901. begin
  1902. current_scanner.readchar;
  1903. if c='=' then
  1904. Message(scan_w_macro_support_turned_off);
  1905. end;
  1906. end;
  1907. end;
  1908. procedure dir_define;
  1909. begin
  1910. dir_define_impl(false);
  1911. end;
  1912. procedure dir_definec;
  1913. begin
  1914. dir_define_impl(true);
  1915. end;
  1916. procedure dir_setc;
  1917. var
  1918. hs : string;
  1919. mac : tmacro;
  1920. l : longint;
  1921. w : integer;
  1922. exprvalue: texprvalue;
  1923. begin
  1924. current_scanner.skipspace;
  1925. hs:=current_scanner.readid;
  1926. mac:=tmacro(search_macro(hs));
  1927. if not assigned(mac) or
  1928. (mac.owner <> current_module.localmacrosymtable) then
  1929. begin
  1930. mac:=tmacro.create(hs);
  1931. mac.defined:=true;
  1932. mac.is_compiler_var:=true;
  1933. current_module.localmacrosymtable.insert(mac);
  1934. end
  1935. else
  1936. begin
  1937. mac.defined:=true;
  1938. mac.is_compiler_var:=true;
  1939. { delete old definition }
  1940. if assigned(mac.buftext) then
  1941. begin
  1942. freemem(mac.buftext,mac.buflen);
  1943. mac.buftext:=nil;
  1944. end;
  1945. end;
  1946. Message1(parser_c_macro_defined,mac.name);
  1947. mac.is_used:=true;
  1948. { key words are never substituted }
  1949. if is_keyword(hs) then
  1950. Message(scan_e_keyword_cant_be_a_macro);
  1951. { macro assignment can be both := and = }
  1952. current_scanner.skipspace;
  1953. if c=':' then
  1954. current_scanner.readchar;
  1955. if c='=' then
  1956. begin
  1957. current_scanner.readchar;
  1958. exprvalue:=preproc_comp_expr;
  1959. if not is_boolean(exprvalue.def) and
  1960. not is_integer(exprvalue.def) then
  1961. exprvalue.error('Boolean, Integer', 'SETC');
  1962. hs:=exprvalue.asStr;
  1963. if length(hs) <> 0 then
  1964. begin
  1965. {If we are absolutely shure it is boolean, translate
  1966. to TRUE/FALSE to increase possibility to do future type check}
  1967. if exprvalue.isBoolean then
  1968. begin
  1969. if exprvalue.asBool then
  1970. hs:='TRUE'
  1971. else
  1972. hs:='FALSE';
  1973. end;
  1974. Message2(parser_c_macro_set_to,mac.name,hs);
  1975. { free buffer of macro ?}
  1976. if assigned(mac.buftext) then
  1977. freemem(mac.buftext,mac.buflen);
  1978. { get new mem }
  1979. getmem(mac.buftext,length(hs));
  1980. mac.buflen:=length(hs);
  1981. { copy the text }
  1982. move(hs[1],mac.buftext^,mac.buflen);
  1983. end
  1984. else
  1985. Message(scan_e_preproc_syntax_error);
  1986. exprvalue.free;
  1987. end
  1988. else
  1989. Message(scan_e_preproc_syntax_error);
  1990. end;
  1991. procedure dir_undef;
  1992. var
  1993. hs : string;
  1994. mac : tmacro;
  1995. begin
  1996. current_scanner.skipspace;
  1997. hs:=current_scanner.readid;
  1998. mac:=tmacro(search_macro(hs));
  1999. if not assigned(mac) or
  2000. (mac.owner <> current_module.localmacrosymtable) then
  2001. begin
  2002. mac:=tmacro.create(hs);
  2003. mac.defined:=false;
  2004. current_module.localmacrosymtable.insert(mac);
  2005. end
  2006. else
  2007. begin
  2008. mac.defined:=false;
  2009. mac.is_compiler_var:=false;
  2010. { delete old definition }
  2011. if assigned(mac.buftext) then
  2012. begin
  2013. freemem(mac.buftext,mac.buflen);
  2014. mac.buftext:=nil;
  2015. end;
  2016. end;
  2017. Message1(parser_c_macro_undefined,mac.name);
  2018. mac.is_used:=true;
  2019. end;
  2020. procedure dir_include;
  2021. function findincludefile(const path,name:TCmdStr;var foundfile:TCmdStr):boolean;
  2022. var
  2023. found : boolean;
  2024. hpath : TCmdStr;
  2025. begin
  2026. (* look for the include file
  2027. If path was absolute and specified as part of {$I } then
  2028. 1. specified path
  2029. else
  2030. 1. path of current inputfile,current dir
  2031. 2. local includepath
  2032. 3. global includepath
  2033. -- Check mantis #13461 before changing this *)
  2034. found:=false;
  2035. foundfile:='';
  2036. hpath:='';
  2037. if path_absolute(path) then
  2038. begin
  2039. found:=FindFile(name,path,true,foundfile);
  2040. end
  2041. else
  2042. begin
  2043. hpath:=current_scanner.inputfile.path+';'+CurDirRelPath(source_info);
  2044. found:=FindFile(path+name, hpath,true,foundfile);
  2045. if not found then
  2046. found:=current_module.localincludesearchpath.FindFile(path+name,true,foundfile);
  2047. if not found then
  2048. found:=includesearchpath.FindFile(path+name,true,foundfile);
  2049. end;
  2050. result:=found;
  2051. end;
  2052. var
  2053. foundfile : TCmdStr;
  2054. path,
  2055. name,
  2056. hs : tpathstr;
  2057. args : string;
  2058. hp : tinputfile;
  2059. found : boolean;
  2060. macroIsString : boolean;
  2061. begin
  2062. current_scanner.skipspace;
  2063. args:=current_scanner.readcomment;
  2064. hs:=GetToken(args,' ');
  2065. if hs='' then
  2066. exit;
  2067. if (hs[1]='%') then
  2068. begin
  2069. { case insensitive }
  2070. hs:=upper(hs);
  2071. { remove %'s }
  2072. Delete(hs,1,1);
  2073. if hs[length(hs)]='%' then
  2074. Delete(hs,length(hs),1);
  2075. { save old }
  2076. path:=hs;
  2077. { first check for internal macros }
  2078. macroIsString:=true;
  2079. if hs='TIME' then
  2080. hs:=gettimestr
  2081. else
  2082. if hs='DATE' then
  2083. hs:=getdatestr
  2084. else
  2085. if hs='FILE' then
  2086. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex)
  2087. else
  2088. if hs='LINE' then
  2089. hs:=tostr(current_filepos.line)
  2090. else
  2091. if hs='LINENUM' then
  2092. begin
  2093. hs:=tostr(current_filepos.line);
  2094. macroIsString:=false;
  2095. end
  2096. else
  2097. if hs='FPCVERSION' then
  2098. hs:=version_string
  2099. else
  2100. if hs='FPCDATE' then
  2101. hs:=date_string
  2102. else
  2103. if hs='FPCTARGET' then
  2104. hs:=target_cpu_string
  2105. else
  2106. if hs='FPCTARGETCPU' then
  2107. hs:=target_cpu_string
  2108. else
  2109. if hs='FPCTARGETOS' then
  2110. hs:=target_info.shortname
  2111. else
  2112. hs:=GetEnvironmentVariable(hs);
  2113. if hs='' then
  2114. Message1(scan_w_include_env_not_found,path);
  2115. { make it a stringconst }
  2116. if macroIsString then
  2117. hs:=''''+hs+'''';
  2118. current_scanner.substitutemacro(path,@hs[1],length(hs),
  2119. current_scanner.line_no,current_scanner.inputfile.ref_index);
  2120. end
  2121. else
  2122. begin
  2123. hs:=FixFileName(hs);
  2124. path:=ExtractFilePath(hs);
  2125. name:=ExtractFileName(hs);
  2126. { Special case for Delphi compatibility: '*' has to be replaced
  2127. by the file name of the current source file. }
  2128. if (length(name)>=1) and
  2129. (name[1]='*') then
  2130. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  2131. { try to find the file }
  2132. found:=findincludefile(path,name,foundfile);
  2133. if (not found) and (ExtractFileExt(name)='') then
  2134. begin
  2135. { try default extensions .inc , .pp and .pas }
  2136. if (not found) then
  2137. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  2138. if (not found) then
  2139. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  2140. if (not found) then
  2141. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  2142. end;
  2143. if current_scanner.inputfilecount<max_include_nesting then
  2144. begin
  2145. inc(current_scanner.inputfilecount);
  2146. { we need to reread the current char }
  2147. dec(current_scanner.inputpointer);
  2148. { shutdown current file }
  2149. current_scanner.tempcloseinputfile;
  2150. { load new file }
  2151. hp:=do_openinputfile(foundfile);
  2152. hp.inc_path:=path;
  2153. current_scanner.addfile(hp);
  2154. current_module.sourcefiles.register_file(hp);
  2155. if (not found) then
  2156. Message1(scan_f_cannot_open_includefile,hs);
  2157. if (not current_scanner.openinputfile) then
  2158. Message1(scan_f_cannot_open_includefile,hs);
  2159. Message1(scan_t_start_include_file,current_scanner.inputfile.path+current_scanner.inputfile.name);
  2160. current_scanner.reload;
  2161. end
  2162. else
  2163. Message(scan_f_include_deep_ten);
  2164. end;
  2165. end;
  2166. {*****************************************************************************
  2167. Preprocessor writing
  2168. *****************************************************************************}
  2169. {$ifdef PREPROCWRITE}
  2170. constructor tpreprocfile.create(const fn:string);
  2171. begin
  2172. { open outputfile }
  2173. assign(f,fn);
  2174. {$push}{$I-}
  2175. rewrite(f);
  2176. {$pop}
  2177. if ioresult<>0 then
  2178. Comment(V_Fatal,'can''t create file '+fn);
  2179. getmem(buf,preprocbufsize);
  2180. settextbuf(f,buf^,preprocbufsize);
  2181. { reset }
  2182. eolfound:=false;
  2183. spacefound:=false;
  2184. end;
  2185. destructor tpreprocfile.destroy;
  2186. begin
  2187. close(f);
  2188. freemem(buf,preprocbufsize);
  2189. end;
  2190. procedure tpreprocfile.add(const s:string);
  2191. begin
  2192. write(f,s);
  2193. end;
  2194. procedure tpreprocfile.addspace;
  2195. begin
  2196. if eolfound then
  2197. begin
  2198. writeln(f,'');
  2199. eolfound:=false;
  2200. spacefound:=false;
  2201. end
  2202. else
  2203. if spacefound then
  2204. begin
  2205. write(f,' ');
  2206. spacefound:=false;
  2207. end;
  2208. end;
  2209. {$endif PREPROCWRITE}
  2210. {*****************************************************************************
  2211. TPreProcStack
  2212. *****************************************************************************}
  2213. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  2214. begin
  2215. accept:=a;
  2216. typ:=atyp;
  2217. next:=n;
  2218. end;
  2219. {*****************************************************************************
  2220. TReplayStack
  2221. *****************************************************************************}
  2222. constructor treplaystack.Create(atoken:ttoken;asettings:tsettings;
  2223. atokenbuf:tdynamicarray;anext:treplaystack);
  2224. begin
  2225. token:=atoken;
  2226. settings:=asettings;
  2227. tokenbuf:=atokenbuf;
  2228. next:=anext;
  2229. end;
  2230. {*****************************************************************************
  2231. TDirectiveItem
  2232. *****************************************************************************}
  2233. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2234. begin
  2235. inherited Create(AList,n);
  2236. is_conditional:=false;
  2237. proc:=p;
  2238. end;
  2239. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2240. begin
  2241. inherited Create(AList,n);
  2242. is_conditional:=true;
  2243. proc:=p;
  2244. end;
  2245. {****************************************************************************
  2246. TSCANNERFILE
  2247. ****************************************************************************}
  2248. constructor tscannerfile.create(const fn:string; is_macro: boolean = false);
  2249. begin
  2250. inputfile:=do_openinputfile(fn);
  2251. if is_macro then
  2252. inputfile.is_macro:=true;
  2253. if assigned(current_module) then
  2254. current_module.sourcefiles.register_file(inputfile);
  2255. { reset localinput }
  2256. c:=#0;
  2257. inputbuffer:=nil;
  2258. inputpointer:=nil;
  2259. inputstart:=0;
  2260. { reset scanner }
  2261. preprocstack:=nil;
  2262. replaystack:=nil;
  2263. comment_level:=0;
  2264. yylexcount:=0;
  2265. block_type:=bt_general;
  2266. line_no:=0;
  2267. lastlinepos:=0;
  2268. lasttokenpos:=0;
  2269. nexttokenpos:=0;
  2270. lasttoken:=NOTOKEN;
  2271. nexttoken:=NOTOKEN;
  2272. lastasmgetchar:=#0;
  2273. ignoredirectives:=TFPHashList.Create;
  2274. in_asm_string:=false;
  2275. end;
  2276. procedure tscannerfile.firstfile;
  2277. begin
  2278. { load block }
  2279. if not openinputfile then
  2280. Message1(scan_f_cannot_open_input,inputfile.name);
  2281. reload;
  2282. end;
  2283. destructor tscannerfile.destroy;
  2284. begin
  2285. if assigned(current_module) and
  2286. (current_module.state=ms_compiled) and
  2287. (status.errorcount=0) then
  2288. checkpreprocstack
  2289. else
  2290. begin
  2291. while assigned(preprocstack) do
  2292. poppreprocstack;
  2293. end;
  2294. while assigned(replaystack) do
  2295. popreplaystack;
  2296. if not inputfile.closed then
  2297. closeinputfile;
  2298. if inputfile.is_macro then
  2299. inputfile.free;
  2300. ignoredirectives.free;
  2301. end;
  2302. function tscannerfile.openinputfile:boolean;
  2303. begin
  2304. openinputfile:=inputfile.open;
  2305. { load buffer }
  2306. inputbuffer:=inputfile.buf;
  2307. inputpointer:=inputfile.buf;
  2308. inputstart:=inputfile.bufstart;
  2309. { line }
  2310. line_no:=0;
  2311. lastlinepos:=0;
  2312. lasttokenpos:=0;
  2313. nexttokenpos:=0;
  2314. end;
  2315. procedure tscannerfile.closeinputfile;
  2316. begin
  2317. inputfile.close;
  2318. { reset buffer }
  2319. inputbuffer:=nil;
  2320. inputpointer:=nil;
  2321. inputstart:=0;
  2322. { reset line }
  2323. line_no:=0;
  2324. lastlinepos:=0;
  2325. lasttokenpos:=0;
  2326. nexttokenpos:=0;
  2327. end;
  2328. function tscannerfile.tempopeninputfile:boolean;
  2329. begin
  2330. if inputfile.is_macro then
  2331. exit;
  2332. tempopeninputfile:=inputfile.tempopen;
  2333. { reload buffer }
  2334. inputbuffer:=inputfile.buf;
  2335. inputpointer:=inputfile.buf;
  2336. inputstart:=inputfile.bufstart;
  2337. end;
  2338. procedure tscannerfile.tempcloseinputfile;
  2339. begin
  2340. if inputfile.closed or inputfile.is_macro then
  2341. exit;
  2342. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  2343. inputfile.tempclose;
  2344. { reset buffer }
  2345. inputbuffer:=nil;
  2346. inputpointer:=nil;
  2347. inputstart:=0;
  2348. end;
  2349. procedure tscannerfile.saveinputfile;
  2350. begin
  2351. inputfile.saveinputpointer:=inputpointer;
  2352. inputfile.savelastlinepos:=lastlinepos;
  2353. inputfile.saveline_no:=line_no;
  2354. end;
  2355. procedure tscannerfile.restoreinputfile;
  2356. begin
  2357. inputbuffer:=inputfile.buf;
  2358. inputpointer:=inputfile.saveinputpointer;
  2359. lastlinepos:=inputfile.savelastlinepos;
  2360. line_no:=inputfile.saveline_no;
  2361. if not inputfile.is_macro then
  2362. parser_current_file:=inputfile.name;
  2363. end;
  2364. procedure tscannerfile.nextfile;
  2365. var
  2366. to_dispose : tinputfile;
  2367. begin
  2368. if assigned(inputfile.next) then
  2369. begin
  2370. if inputfile.is_macro then
  2371. to_dispose:=inputfile
  2372. else
  2373. begin
  2374. to_dispose:=nil;
  2375. dec(inputfilecount);
  2376. end;
  2377. { we can allways close the file, no ? }
  2378. inputfile.close;
  2379. inputfile:=inputfile.next;
  2380. if assigned(to_dispose) then
  2381. to_dispose.free;
  2382. restoreinputfile;
  2383. end;
  2384. end;
  2385. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  2386. begin
  2387. if not assigned(buf) then
  2388. internalerror(200511172);
  2389. if assigned(recordtokenbuf) then
  2390. internalerror(200511173);
  2391. recordtokenbuf:=buf;
  2392. fillchar(last_settings,sizeof(last_settings),0);
  2393. last_message:=nil;
  2394. fillchar(last_filepos,sizeof(last_filepos),0);
  2395. end;
  2396. procedure tscannerfile.stoprecordtokens;
  2397. begin
  2398. if not assigned(recordtokenbuf) then
  2399. internalerror(200511174);
  2400. recordtokenbuf:=nil;
  2401. end;
  2402. procedure tscannerfile.writetoken(t : ttoken);
  2403. var
  2404. b : byte;
  2405. begin
  2406. if ord(t)>$7f then
  2407. begin
  2408. b:=(ord(t) shr 8) or $80;
  2409. recordtokenbuf.write(b,1);
  2410. end;
  2411. b:=ord(t) and $ff;
  2412. recordtokenbuf.write(b,1);
  2413. end;
  2414. procedure tscannerfile.tokenwritesizeint(val : asizeint);
  2415. begin
  2416. {$ifdef FPC_BIG_ENDIAN}
  2417. val:=swapendian(val);
  2418. {$endif}
  2419. recordtokenbuf.write(val,sizeof(asizeint));
  2420. end;
  2421. procedure tscannerfile.tokenwritelongint(val : longint);
  2422. begin
  2423. {$ifdef FPC_BIG_ENDIAN}
  2424. val:=swapendian(val);
  2425. {$endif}
  2426. recordtokenbuf.write(val,sizeof(longint));
  2427. end;
  2428. procedure tscannerfile.tokenwriteshortint(val : shortint);
  2429. begin
  2430. {$ifdef FPC_BIG_ENDIAN}
  2431. val:=swapendian(val);
  2432. {$endif}
  2433. recordtokenbuf.write(val,sizeof(shortint));
  2434. end;
  2435. procedure tscannerfile.tokenwriteword(val : word);
  2436. begin
  2437. {$ifdef FPC_BIG_ENDIAN}
  2438. val:=swapendian(val);
  2439. {$endif}
  2440. recordtokenbuf.write(val,sizeof(word));
  2441. end;
  2442. procedure tscannerfile.tokenwritelongword(val : longword);
  2443. begin
  2444. {$ifdef FPC_BIG_ENDIAN}
  2445. val:=swapendian(val);
  2446. {$endif}
  2447. recordtokenbuf.write(val,sizeof(longword));
  2448. end;
  2449. function tscannerfile.tokenreadsizeint : asizeint;
  2450. var
  2451. val : asizeint;
  2452. begin
  2453. replaytokenbuf.read(val,sizeof(asizeint));
  2454. {$ifdef FPC_BIG_ENDIAN}
  2455. val:=swapendian(val);
  2456. {$endif}
  2457. result:=val;
  2458. end;
  2459. function tscannerfile.tokenreadlongword : longword;
  2460. var
  2461. val : longword;
  2462. begin
  2463. replaytokenbuf.read(val,sizeof(longword));
  2464. {$ifdef FPC_BIG_ENDIAN}
  2465. val:=swapendian(val);
  2466. {$endif}
  2467. result:=val;
  2468. end;
  2469. function tscannerfile.tokenreadlongint : longint;
  2470. var
  2471. val : longint;
  2472. begin
  2473. replaytokenbuf.read(val,sizeof(longint));
  2474. {$ifdef FPC_BIG_ENDIAN}
  2475. val:=swapendian(val);
  2476. {$endif}
  2477. result:=val;
  2478. end;
  2479. function tscannerfile.tokenreadshortint : shortint;
  2480. var
  2481. val : shortint;
  2482. begin
  2483. replaytokenbuf.read(val,sizeof(shortint));
  2484. result:=val;
  2485. end;
  2486. function tscannerfile.tokenreadbyte : byte;
  2487. var
  2488. val : byte;
  2489. begin
  2490. replaytokenbuf.read(val,sizeof(byte));
  2491. result:=val;
  2492. end;
  2493. function tscannerfile.tokenreadsmallint : smallint;
  2494. var
  2495. val : smallint;
  2496. begin
  2497. replaytokenbuf.read(val,sizeof(smallint));
  2498. {$ifdef FPC_BIG_ENDIAN}
  2499. val:=swapendian(val);
  2500. {$endif}
  2501. result:=val;
  2502. end;
  2503. function tscannerfile.tokenreadword : word;
  2504. var
  2505. val : word;
  2506. begin
  2507. replaytokenbuf.read(val,sizeof(word));
  2508. {$ifdef FPC_BIG_ENDIAN}
  2509. val:=swapendian(val);
  2510. {$endif}
  2511. result:=val;
  2512. end;
  2513. function tscannerfile.tokenreadenum(size : longint) : longword;
  2514. begin
  2515. if size=1 then
  2516. result:=tokenreadbyte
  2517. else if size=2 then
  2518. result:=tokenreadword
  2519. else if size=4 then
  2520. result:=tokenreadlongword;
  2521. end;
  2522. procedure tscannerfile.tokenreadset(var b;size : longint);
  2523. {$ifdef FPC_BIG_ENDIAN}
  2524. var
  2525. i : longint;
  2526. {$endif}
  2527. begin
  2528. replaytokenbuf.read(b,size);
  2529. {$ifdef FPC_BIG_ENDIAN}
  2530. for i:=0 to size-1 do
  2531. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  2532. {$endif}
  2533. end;
  2534. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  2535. begin
  2536. recordtokenbuf.write(b,size);
  2537. end;
  2538. procedure tscannerfile.tokenwriteset(var b;size : longint);
  2539. {$ifdef FPC_BIG_ENDIAN}
  2540. var
  2541. i: longint;
  2542. tmpset: array[0..31] of byte;
  2543. {$endif}
  2544. begin
  2545. {$ifdef FPC_BIG_ENDIAN}
  2546. for i:=0 to size-1 do
  2547. tmpset[i]:=reverse_byte(Pbyte(@b)[i]);
  2548. recordtokenbuf.write(tmpset,size);
  2549. {$else}
  2550. recordtokenbuf.write(b,size);
  2551. {$endif}
  2552. end;
  2553. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  2554. { This procedure
  2555. needs to be changed whenever
  2556. globals.tsettings type is changed,
  2557. the problem is that no error will appear
  2558. before tests with generics are tested. PM }
  2559. var
  2560. startpos, endpos : longword;
  2561. begin
  2562. { WARNING all those fields need to be in the correct
  2563. order otherwise cross_endian PPU reading will fail }
  2564. startpos:=replaytokenbuf.pos;
  2565. with asettings do
  2566. begin
  2567. alignment.procalign:=tokenreadlongint;
  2568. alignment.loopalign:=tokenreadlongint;
  2569. alignment.jumpalign:=tokenreadlongint;
  2570. alignment.constalignmin:=tokenreadlongint;
  2571. alignment.constalignmax:=tokenreadlongint;
  2572. alignment.varalignmin:=tokenreadlongint;
  2573. alignment.varalignmax:=tokenreadlongint;
  2574. alignment.localalignmin:=tokenreadlongint;
  2575. alignment.localalignmax:=tokenreadlongint;
  2576. alignment.recordalignmin:=tokenreadlongint;
  2577. alignment.recordalignmax:=tokenreadlongint;
  2578. alignment.maxCrecordalign:=tokenreadlongint;
  2579. tokenreadset(globalswitches,sizeof(globalswitches));
  2580. tokenreadset(targetswitches,sizeof(targetswitches));
  2581. tokenreadset(moduleswitches,sizeof(moduleswitches));
  2582. tokenreadset(localswitches,sizeof(localswitches));
  2583. tokenreadset(modeswitches,sizeof(modeswitches));
  2584. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  2585. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2586. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2587. tokenreadset(debugswitches,sizeof(debugswitches));
  2588. { 0: old behaviour for sets <=256 elements
  2589. >0: round to this size }
  2590. setalloc:=tokenreadshortint;
  2591. packenum:=tokenreadshortint;
  2592. packrecords:=tokenreadshortint;
  2593. maxfpuregisters:=tokenreadshortint;
  2594. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2595. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2596. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  2597. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  2598. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  2599. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  2600. { tstringencoding is word type,
  2601. thus this should be OK here }
  2602. sourcecodepage:=tstringEncoding(tokenreadword);
  2603. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  2604. disabledircache:=boolean(tokenreadbyte);
  2605. {$if defined(ARM) or defined(AVR)}
  2606. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)));
  2607. {$endif defined(ARM) or defined(AVR)}
  2608. endpos:=replaytokenbuf.pos;
  2609. if endpos-startpos<>expected_size then
  2610. Comment(V_Error,'Wrong size of Settings read-in');
  2611. end;
  2612. end;
  2613. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  2614. { This procedure
  2615. needs to be changed whenever
  2616. globals.tsettings type is changed,
  2617. the problem is that no error will appear
  2618. before tests with generics are tested. PM }
  2619. var
  2620. sizepos, startpos, endpos : longword;
  2621. begin
  2622. { WARNING all those fields need to be in the correct
  2623. order otherwise cross_endian PPU reading will fail }
  2624. sizepos:=recordtokenbuf.pos;
  2625. size:=0;
  2626. tokenwritesizeint(size);
  2627. startpos:=recordtokenbuf.pos;
  2628. with asettings do
  2629. begin
  2630. tokenwritelongint(alignment.procalign);
  2631. tokenwritelongint(alignment.loopalign);
  2632. tokenwritelongint(alignment.jumpalign);
  2633. tokenwritelongint(alignment.constalignmin);
  2634. tokenwritelongint(alignment.constalignmax);
  2635. tokenwritelongint(alignment.varalignmin);
  2636. tokenwritelongint(alignment.varalignmax);
  2637. tokenwritelongint(alignment.localalignmin);
  2638. tokenwritelongint(alignment.localalignmax);
  2639. tokenwritelongint(alignment.recordalignmin);
  2640. tokenwritelongint(alignment.recordalignmax);
  2641. tokenwritelongint(alignment.maxCrecordalign);
  2642. tokenwriteset(globalswitches,sizeof(globalswitches));
  2643. tokenwriteset(targetswitches,sizeof(targetswitches));
  2644. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  2645. tokenwriteset(localswitches,sizeof(localswitches));
  2646. tokenwriteset(modeswitches,sizeof(modeswitches));
  2647. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  2648. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2649. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2650. tokenwriteset(debugswitches,sizeof(debugswitches));
  2651. { 0: old behaviour for sets <=256 elements
  2652. >0: round to this size }
  2653. tokenwriteshortint(setalloc);
  2654. tokenwriteshortint(packenum);
  2655. tokenwriteshortint(packrecords);
  2656. tokenwriteshortint(maxfpuregisters);
  2657. tokenwriteenum(cputype,sizeof(tcputype));
  2658. tokenwriteenum(optimizecputype,sizeof(tcputype));
  2659. tokenwriteenum(fputype,sizeof(tfputype));
  2660. tokenwriteenum(asmmode,sizeof(tasmmode));
  2661. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  2662. tokenwriteenum(defproccall,sizeof(tproccalloption));
  2663. { tstringencoding is word type,
  2664. thus this should be OK here }
  2665. tokenwriteword(sourcecodepage);
  2666. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  2667. recordtokenbuf.write(byte(disabledircache),1);
  2668. {$if defined(ARM) or defined(AVR)}
  2669. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  2670. {$endif defined(ARM) or defined(AVR)}
  2671. endpos:=recordtokenbuf.pos;
  2672. size:=endpos-startpos;
  2673. recordtokenbuf.seek(sizepos);
  2674. tokenwritesizeint(size);
  2675. recordtokenbuf.seek(endpos);
  2676. end;
  2677. end;
  2678. procedure tscannerfile.recordtoken;
  2679. var
  2680. t : ttoken;
  2681. s : tspecialgenerictoken;
  2682. len,msgnb,copy_size : asizeint;
  2683. val : longint;
  2684. b : byte;
  2685. pmsg : pmessagestaterecord;
  2686. begin
  2687. if not assigned(recordtokenbuf) then
  2688. internalerror(200511176);
  2689. t:=_GENERICSPECIALTOKEN;
  2690. { settings changed? }
  2691. { last field pmessage is handled separately below in
  2692. ST_LOADMESSAGES }
  2693. if CompareByte(current_settings,last_settings,
  2694. sizeof(current_settings)-sizeof(pointer))<>0 then
  2695. begin
  2696. { use a special token to record it }
  2697. s:=ST_LOADSETTINGS;
  2698. writetoken(t);
  2699. recordtokenbuf.write(s,1);
  2700. copy_size:=sizeof(current_settings)-sizeof(pointer);
  2701. tokenwritesettings(current_settings,copy_size);
  2702. last_settings:=current_settings;
  2703. end;
  2704. if current_settings.pmessage<>last_message then
  2705. begin
  2706. { use a special token to record it }
  2707. s:=ST_LOADMESSAGES;
  2708. writetoken(t);
  2709. recordtokenbuf.write(s,1);
  2710. msgnb:=0;
  2711. pmsg:=current_settings.pmessage;
  2712. while assigned(pmsg) do
  2713. begin
  2714. if msgnb=high(asizeint) then
  2715. { Too many messages }
  2716. internalerror(2011090401);
  2717. inc(msgnb);
  2718. pmsg:=pmsg^.next;
  2719. end;
  2720. tokenwritesizeint(msgnb);
  2721. pmsg:=current_settings.pmessage;
  2722. while assigned(pmsg) do
  2723. begin
  2724. { What about endianess here?}
  2725. { SB: this is handled by tokenreadlongint }
  2726. val:=pmsg^.value;
  2727. tokenwritelongint(val);
  2728. val:=ord(pmsg^.state);
  2729. tokenwritelongint(val);
  2730. pmsg:=pmsg^.next;
  2731. end;
  2732. last_message:=current_settings.pmessage;
  2733. end;
  2734. { file pos changes? }
  2735. if current_tokenpos.line<>last_filepos.line then
  2736. begin
  2737. s:=ST_LINE;
  2738. writetoken(t);
  2739. recordtokenbuf.write(s,1);
  2740. tokenwritelongint(current_tokenpos.line);
  2741. last_filepos.line:=current_tokenpos.line;
  2742. end;
  2743. if current_tokenpos.column<>last_filepos.column then
  2744. begin
  2745. s:=ST_COLUMN;
  2746. writetoken(t);
  2747. { can the column be written packed? }
  2748. if current_tokenpos.column<$80 then
  2749. begin
  2750. b:=$80 or current_tokenpos.column;
  2751. recordtokenbuf.write(b,1);
  2752. end
  2753. else
  2754. begin
  2755. recordtokenbuf.write(s,1);
  2756. tokenwriteword(current_tokenpos.column);
  2757. end;
  2758. last_filepos.column:=current_tokenpos.column;
  2759. end;
  2760. if current_tokenpos.fileindex<>last_filepos.fileindex then
  2761. begin
  2762. s:=ST_FILEINDEX;
  2763. writetoken(t);
  2764. recordtokenbuf.write(s,1);
  2765. tokenwriteword(current_tokenpos.fileindex);
  2766. last_filepos.fileindex:=current_tokenpos.fileindex;
  2767. end;
  2768. writetoken(token);
  2769. if token<>_GENERICSPECIALTOKEN then
  2770. writetoken(idtoken);
  2771. case token of
  2772. _CWCHAR,
  2773. _CWSTRING :
  2774. begin
  2775. tokenwritesizeint(patternw^.len);
  2776. if patternw^.len>0 then
  2777. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2778. end;
  2779. _CSTRING:
  2780. begin
  2781. len:=length(cstringpattern);
  2782. tokenwritesizeint(len);
  2783. if len>0 then
  2784. recordtokenbuf.write(cstringpattern[1],len);
  2785. end;
  2786. _CCHAR,
  2787. _INTCONST,
  2788. _REALNUMBER :
  2789. begin
  2790. { pexpr.pas messes with pattern in case of negative integer consts,
  2791. see around line 2562 the comment of JM; remove the - before recording it
  2792. (FK)
  2793. }
  2794. if (token=_INTCONST) and (pattern[1]='-') then
  2795. delete(pattern,1,1);
  2796. recordtokenbuf.write(pattern[0],1);
  2797. recordtokenbuf.write(pattern[1],length(pattern));
  2798. end;
  2799. _ID :
  2800. begin
  2801. recordtokenbuf.write(orgpattern[0],1);
  2802. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  2803. end;
  2804. end;
  2805. end;
  2806. procedure tscannerfile.startreplaytokens(buf:tdynamicarray);
  2807. begin
  2808. if not assigned(buf) then
  2809. internalerror(200511175);
  2810. { save current token }
  2811. if token in [_CWCHAR,_CWSTRING,_CCHAR,_CSTRING,_INTCONST,_REALNUMBER,_ID] then
  2812. internalerror(200511178);
  2813. replaystack:=treplaystack.create(token,current_settings,
  2814. replaytokenbuf,replaystack);
  2815. if assigned(inputpointer) then
  2816. dec(inputpointer);
  2817. { install buffer }
  2818. replaytokenbuf:=buf;
  2819. { reload next token }
  2820. replaytokenbuf.seek(0);
  2821. replaytoken;
  2822. end;
  2823. function tscannerfile.readtoken: ttoken;
  2824. var
  2825. b,b2 : byte;
  2826. begin
  2827. replaytokenbuf.read(b,1);
  2828. if (b and $80)<>0 then
  2829. begin
  2830. replaytokenbuf.read(b2,1);
  2831. result:=ttoken(((b and $7f) shl 8) or b2);
  2832. end
  2833. else
  2834. result:=ttoken(b);
  2835. end;
  2836. procedure tscannerfile.replaytoken;
  2837. var
  2838. wlen,mesgnb,copy_size : asizeint;
  2839. specialtoken : tspecialgenerictoken;
  2840. i : byte;
  2841. pmsg,prevmsg : pmessagestaterecord;
  2842. begin
  2843. if not assigned(replaytokenbuf) then
  2844. internalerror(200511177);
  2845. { End of replay buffer? Then load the next char from the file again }
  2846. if replaytokenbuf.pos>=replaytokenbuf.size then
  2847. begin
  2848. token:=replaystack.token;
  2849. replaytokenbuf:=replaystack.tokenbuf;
  2850. { restore compiler settings }
  2851. current_settings:=replaystack.settings;
  2852. popreplaystack;
  2853. if assigned(inputpointer) then
  2854. begin
  2855. c:=inputpointer^;
  2856. inc(inputpointer);
  2857. end;
  2858. exit;
  2859. end;
  2860. repeat
  2861. { load token from the buffer }
  2862. token:=readtoken;
  2863. if token<>_GENERICSPECIALTOKEN then
  2864. idtoken:=readtoken
  2865. else
  2866. idtoken:=_NOID;
  2867. case token of
  2868. _CWCHAR,
  2869. _CWSTRING :
  2870. begin
  2871. wlen:=tokenreadsizeint;
  2872. setlengthwidestring(patternw,wlen);
  2873. if wlen>0 then
  2874. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2875. orgpattern:='';
  2876. pattern:='';
  2877. cstringpattern:='';
  2878. end;
  2879. _CSTRING:
  2880. begin
  2881. wlen:=tokenreadsizeint;
  2882. if wlen>0 then
  2883. begin
  2884. setlength(cstringpattern,wlen);
  2885. replaytokenbuf.read(cstringpattern[1],wlen);
  2886. end
  2887. else
  2888. cstringpattern:='';
  2889. orgpattern:='';
  2890. pattern:='';
  2891. end;
  2892. _CCHAR,
  2893. _INTCONST,
  2894. _REALNUMBER :
  2895. begin
  2896. replaytokenbuf.read(pattern[0],1);
  2897. replaytokenbuf.read(pattern[1],length(pattern));
  2898. orgpattern:='';
  2899. end;
  2900. _ID :
  2901. begin
  2902. replaytokenbuf.read(orgpattern[0],1);
  2903. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  2904. pattern:=upper(orgpattern);
  2905. end;
  2906. _GENERICSPECIALTOKEN:
  2907. begin
  2908. replaytokenbuf.read(specialtoken,1);
  2909. { packed column? }
  2910. if (ord(specialtoken) and $80)<>0 then
  2911. begin
  2912. current_tokenpos.column:=ord(specialtoken) and $7f;
  2913. current_filepos:=current_tokenpos;
  2914. end
  2915. else
  2916. case specialtoken of
  2917. ST_LOADSETTINGS:
  2918. begin
  2919. copy_size:=tokenreadsizeint;
  2920. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  2921. // internalerror(2011090501);
  2922. {
  2923. replaytokenbuf.read(current_settings,copy_size);
  2924. }
  2925. tokenreadsettings(current_settings,copy_size);
  2926. end;
  2927. ST_LOADMESSAGES:
  2928. begin
  2929. current_settings.pmessage:=nil;
  2930. mesgnb:=tokenreadsizeint;
  2931. if mesgnb>0 then
  2932. Comment(V_Error,'Message recordind not yet supported');
  2933. for i:=1 to mesgnb do
  2934. begin
  2935. new(pmsg);
  2936. if i=1 then
  2937. begin
  2938. current_settings.pmessage:=pmsg;
  2939. prevmsg:=nil;
  2940. end
  2941. else
  2942. prevmsg^.next:=pmsg;
  2943. pmsg^.value:=tokenreadlongint;
  2944. pmsg^.state:=tmsgstate(tokenreadlongint);
  2945. pmsg^.next:=nil;
  2946. prevmsg:=pmsg;
  2947. end;
  2948. end;
  2949. ST_LINE:
  2950. begin
  2951. current_tokenpos.line:=tokenreadlongint;
  2952. current_filepos:=current_tokenpos;
  2953. end;
  2954. ST_COLUMN:
  2955. begin
  2956. current_tokenpos.column:=tokenreadword;
  2957. current_filepos:=current_tokenpos;
  2958. end;
  2959. ST_FILEINDEX:
  2960. begin
  2961. current_tokenpos.fileindex:=tokenreadword;
  2962. current_filepos:=current_tokenpos;
  2963. end;
  2964. else
  2965. internalerror(2006103010);
  2966. end;
  2967. continue;
  2968. end;
  2969. end;
  2970. break;
  2971. until false;
  2972. end;
  2973. procedure tscannerfile.addfile(hp:tinputfile);
  2974. begin
  2975. saveinputfile;
  2976. { add to list }
  2977. hp.next:=inputfile;
  2978. inputfile:=hp;
  2979. { load new inputfile }
  2980. restoreinputfile;
  2981. end;
  2982. procedure tscannerfile.reload;
  2983. begin
  2984. with inputfile do
  2985. begin
  2986. { when nothing more to read then leave immediatly, so we
  2987. don't change the current_filepos and leave it point to the last
  2988. char }
  2989. if (c=#26) and (not assigned(next)) then
  2990. exit;
  2991. repeat
  2992. { still more to read?, then change the #0 to a space so its seen
  2993. as a seperator, this can't be used for macro's which can change
  2994. the place of the #0 in the buffer with tempopen }
  2995. if (c=#0) and (bufsize>0) and
  2996. not(inputfile.is_macro) and
  2997. (inputpointer-inputbuffer<bufsize) then
  2998. begin
  2999. c:=' ';
  3000. inc(inputpointer);
  3001. exit;
  3002. end;
  3003. { can we read more from this file ? }
  3004. if (c<>#26) and (not endoffile) then
  3005. begin
  3006. readbuf;
  3007. inputpointer:=buf;
  3008. inputbuffer:=buf;
  3009. inputstart:=bufstart;
  3010. { first line? }
  3011. if line_no=0 then
  3012. begin
  3013. c:=inputpointer^;
  3014. { eat utf-8 signature? }
  3015. if (ord(inputpointer^)=$ef) and
  3016. (ord((inputpointer+1)^)=$bb) and
  3017. (ord((inputpointer+2)^)=$bf) then
  3018. begin
  3019. (* we don't support including files with an UTF-8 bom
  3020. inside another file that wasn't encoded as UTF-8
  3021. already (we don't support {$codepage xxx} switches in
  3022. the middle of a file either) *)
  3023. if (current_settings.sourcecodepage<>CP_UTF8) and
  3024. not current_module.in_global then
  3025. Message(scanner_f_illegal_utf8_bom);
  3026. inc(inputpointer,3);
  3027. message(scan_c_switching_to_utf8);
  3028. current_settings.sourcecodepage:=CP_UTF8;
  3029. include(current_settings.moduleswitches,cs_explicit_codepage);
  3030. end;
  3031. line_no:=1;
  3032. if cs_asm_source in current_settings.globalswitches then
  3033. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3034. end;
  3035. end
  3036. else
  3037. begin
  3038. { load eof position in tokenpos/current_filepos }
  3039. gettokenpos;
  3040. { close file }
  3041. closeinputfile;
  3042. { no next module, than EOF }
  3043. if not assigned(inputfile.next) then
  3044. begin
  3045. c:=#26;
  3046. exit;
  3047. end;
  3048. { load next file and reopen it }
  3049. nextfile;
  3050. tempopeninputfile;
  3051. { status }
  3052. Message1(scan_t_back_in,inputfile.name);
  3053. end;
  3054. { load next char }
  3055. c:=inputpointer^;
  3056. inc(inputpointer);
  3057. until c<>#0; { if also end, then reload again }
  3058. end;
  3059. end;
  3060. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  3061. var
  3062. hp : tinputfile;
  3063. begin
  3064. { save old postion }
  3065. dec(inputpointer);
  3066. tempcloseinputfile;
  3067. { create macro 'file' }
  3068. { use special name to dispose after !! }
  3069. hp:=do_openinputfile('_Macro_.'+macname);
  3070. addfile(hp);
  3071. with inputfile do
  3072. begin
  3073. setmacro(p,len);
  3074. { local buffer }
  3075. inputbuffer:=buf;
  3076. inputpointer:=buf;
  3077. inputstart:=bufstart;
  3078. ref_index:=fileindex;
  3079. end;
  3080. { reset line }
  3081. line_no:=line;
  3082. lastlinepos:=0;
  3083. lasttokenpos:=0;
  3084. nexttokenpos:=0;
  3085. { load new c }
  3086. c:=inputpointer^;
  3087. inc(inputpointer);
  3088. end;
  3089. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3090. begin
  3091. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3092. filepos.line:=line_no;
  3093. filepos.column:=tokenpos-lastlinepos;
  3094. filepos.fileindex:=inputfile.ref_index;
  3095. filepos.moduleindex:=current_module.unit_index;
  3096. end;
  3097. procedure tscannerfile.gettokenpos;
  3098. { load the values of tokenpos and lasttokenpos }
  3099. begin
  3100. do_gettokenpos(lasttokenpos,current_tokenpos);
  3101. current_filepos:=current_tokenpos;
  3102. end;
  3103. procedure tscannerfile.cachenexttokenpos;
  3104. begin
  3105. do_gettokenpos(nexttokenpos,next_filepos);
  3106. end;
  3107. procedure tscannerfile.setnexttoken;
  3108. begin
  3109. token:=nexttoken;
  3110. nexttoken:=NOTOKEN;
  3111. lasttokenpos:=nexttokenpos;
  3112. current_tokenpos:=next_filepos;
  3113. current_filepos:=current_tokenpos;
  3114. nexttokenpos:=0;
  3115. end;
  3116. procedure tscannerfile.savetokenpos;
  3117. begin
  3118. oldlasttokenpos:=lasttokenpos;
  3119. oldcurrent_filepos:=current_filepos;
  3120. oldcurrent_tokenpos:=current_tokenpos;
  3121. end;
  3122. procedure tscannerfile.restoretokenpos;
  3123. begin
  3124. lasttokenpos:=oldlasttokenpos;
  3125. current_filepos:=oldcurrent_filepos;
  3126. current_tokenpos:=oldcurrent_tokenpos;
  3127. end;
  3128. procedure tscannerfile.inc_comment_level;
  3129. begin
  3130. if (m_nested_comment in current_settings.modeswitches) then
  3131. inc(comment_level)
  3132. else
  3133. comment_level:=1;
  3134. if (comment_level>1) then
  3135. begin
  3136. savetokenpos;
  3137. gettokenpos; { update for warning }
  3138. Message1(scan_w_comment_level,tostr(comment_level));
  3139. restoretokenpos;
  3140. end;
  3141. end;
  3142. procedure tscannerfile.dec_comment_level;
  3143. begin
  3144. if (m_nested_comment in current_settings.modeswitches) then
  3145. dec(comment_level)
  3146. else
  3147. comment_level:=0;
  3148. end;
  3149. procedure tscannerfile.linebreak;
  3150. var
  3151. cur : char;
  3152. begin
  3153. with inputfile do
  3154. begin
  3155. if (byte(inputpointer^)=0) and not(endoffile) then
  3156. begin
  3157. cur:=c;
  3158. reload;
  3159. if byte(cur)+byte(c)<>23 then
  3160. dec(inputpointer);
  3161. end
  3162. else
  3163. begin
  3164. { Support all combination of #10 and #13 as line break }
  3165. if (byte(inputpointer^)+byte(c)=23) then
  3166. inc(inputpointer);
  3167. end;
  3168. { Always return #10 as line break }
  3169. c:=#10;
  3170. { increase line counters }
  3171. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3172. inc(line_no);
  3173. { update linebuffer }
  3174. if cs_asm_source in current_settings.globalswitches then
  3175. inputfile.setline(line_no,lastlinepos);
  3176. { update for status and call the show status routine,
  3177. but don't touch current_filepos ! }
  3178. savetokenpos;
  3179. gettokenpos; { update for v_status }
  3180. inc(status.compiledlines);
  3181. ShowStatus;
  3182. restoretokenpos;
  3183. end;
  3184. end;
  3185. procedure tscannerfile.illegal_char(c:char);
  3186. var
  3187. s : string;
  3188. begin
  3189. if c in [#32..#255] then
  3190. s:=''''+c+''''
  3191. else
  3192. s:='#'+tostr(ord(c));
  3193. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3194. end;
  3195. procedure tscannerfile.end_of_file;
  3196. begin
  3197. checkpreprocstack;
  3198. Message(scan_f_end_of_file);
  3199. end;
  3200. {-------------------------------------------
  3201. IF Conditional Handling
  3202. -------------------------------------------}
  3203. procedure tscannerfile.checkpreprocstack;
  3204. begin
  3205. { check for missing ifdefs }
  3206. while assigned(preprocstack) do
  3207. begin
  3208. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3209. preprocstack.owner.inputfile.name,tostr(preprocstack.line_nb));
  3210. poppreprocstack;
  3211. end;
  3212. end;
  3213. procedure tscannerfile.poppreprocstack;
  3214. var
  3215. hp : tpreprocstack;
  3216. begin
  3217. if assigned(preprocstack) then
  3218. begin
  3219. Message1(scan_c_endif_found,preprocstack.name);
  3220. hp:=preprocstack.next;
  3221. preprocstack.free;
  3222. preprocstack:=hp;
  3223. end
  3224. else
  3225. Message(scan_e_endif_without_if);
  3226. end;
  3227. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  3228. var
  3229. condition: Boolean;
  3230. valuedescr: String;
  3231. begin
  3232. if (preprocstack=nil) or preprocstack.accept then
  3233. condition:=compile_time_predicate(valuedescr)
  3234. else
  3235. begin
  3236. condition:= false;
  3237. valuedescr:= '';
  3238. end;
  3239. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  3240. preprocstack.name:=valuedescr;
  3241. preprocstack.line_nb:=line_no;
  3242. preprocstack.owner:=self;
  3243. if preprocstack.accept then
  3244. Message2(messid,preprocstack.name,'accepted')
  3245. else
  3246. Message2(messid,preprocstack.name,'rejected');
  3247. end;
  3248. procedure tscannerfile.elsepreprocstack;
  3249. begin
  3250. if assigned(preprocstack) and
  3251. (preprocstack.typ<>pp_else) then
  3252. begin
  3253. if (preprocstack.typ=pp_elseif) then
  3254. preprocstack.accept:=false
  3255. else
  3256. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  3257. preprocstack.accept:=not preprocstack.accept;
  3258. preprocstack.typ:=pp_else;
  3259. preprocstack.line_nb:=line_no;
  3260. if preprocstack.accept then
  3261. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3262. else
  3263. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3264. end
  3265. else
  3266. Message(scan_e_endif_without_if);
  3267. end;
  3268. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  3269. var
  3270. valuedescr: String;
  3271. begin
  3272. if assigned(preprocstack) and
  3273. (preprocstack.typ in [pp_if,pp_elseif]) then
  3274. begin
  3275. { when the branch is accepted we use pp_elseif so we know that
  3276. all the next branches need to be rejected. when this branch is still
  3277. not accepted then leave it at pp_if }
  3278. if (preprocstack.typ=pp_elseif) then
  3279. preprocstack.accept:=false
  3280. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  3281. begin
  3282. preprocstack.accept:=false;
  3283. preprocstack.typ:=pp_elseif;
  3284. end
  3285. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  3286. and compile_time_predicate(valuedescr) then
  3287. begin
  3288. preprocstack.name:=valuedescr;
  3289. preprocstack.accept:=true;
  3290. preprocstack.typ:=pp_elseif;
  3291. end;
  3292. preprocstack.line_nb:=line_no;
  3293. if preprocstack.accept then
  3294. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3295. else
  3296. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3297. end
  3298. else
  3299. Message(scan_e_endif_without_if);
  3300. end;
  3301. procedure tscannerfile.popreplaystack;
  3302. var
  3303. hp : treplaystack;
  3304. begin
  3305. if assigned(replaystack) then
  3306. begin
  3307. hp:=replaystack.next;
  3308. replaystack.free;
  3309. replaystack:=hp;
  3310. end;
  3311. end;
  3312. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  3313. begin
  3314. savetokenpos;
  3315. repeat
  3316. current_scanner.gettokenpos;
  3317. Message1(scan_d_handling_switch,'$'+p.name);
  3318. p.proc();
  3319. { accept the text ? }
  3320. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  3321. break
  3322. else
  3323. begin
  3324. current_scanner.gettokenpos;
  3325. Message(scan_c_skipping_until);
  3326. repeat
  3327. current_scanner.skipuntildirective;
  3328. if not (m_mac in current_settings.modeswitches) then
  3329. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  3330. else
  3331. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  3332. until assigned(p) and (p.is_conditional);
  3333. current_scanner.gettokenpos;
  3334. end;
  3335. until false;
  3336. restoretokenpos;
  3337. end;
  3338. procedure tscannerfile.handledirectives;
  3339. var
  3340. t : tdirectiveitem;
  3341. hs : string;
  3342. begin
  3343. gettokenpos;
  3344. readchar; {Remove the $}
  3345. hs:=readid;
  3346. { handle empty directive }
  3347. if hs='' then
  3348. begin
  3349. Message1(scan_w_illegal_switch,'$');
  3350. exit;
  3351. end;
  3352. {$ifdef PREPROCWRITE}
  3353. if parapreprocess then
  3354. begin
  3355. t:=Get_Directive(hs);
  3356. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  3357. begin
  3358. preprocfile^.AddSpace;
  3359. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  3360. exit;
  3361. end;
  3362. end;
  3363. {$endif PREPROCWRITE}
  3364. { skip this directive? }
  3365. if (ignoredirectives.find(hs)<>nil) then
  3366. begin
  3367. if (comment_level>0) then
  3368. readcomment;
  3369. { we've read the whole comment }
  3370. aktcommentstyle:=comment_none;
  3371. exit;
  3372. end;
  3373. { Check for compiler switches }
  3374. while (length(hs)=1) and (c in ['-','+']) do
  3375. begin
  3376. Message1(scan_d_handling_switch,'$'+hs+c);
  3377. HandleSwitch(hs[1],c);
  3378. current_scanner.readchar; {Remove + or -}
  3379. if c=',' then
  3380. begin
  3381. current_scanner.readchar; {Remove , }
  3382. { read next switch, support $v+,$+}
  3383. hs:=current_scanner.readid;
  3384. if (hs='') then
  3385. begin
  3386. if (c='$') and (m_fpc in current_settings.modeswitches) then
  3387. begin
  3388. current_scanner.readchar; { skip $ }
  3389. hs:=current_scanner.readid;
  3390. end;
  3391. if (hs='') then
  3392. Message1(scan_w_illegal_directive,'$'+c);
  3393. end;
  3394. end
  3395. else
  3396. hs:='';
  3397. end;
  3398. { directives may follow switches after a , }
  3399. if hs<>'' then
  3400. begin
  3401. if not (m_mac in current_settings.modeswitches) then
  3402. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  3403. else
  3404. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  3405. if assigned(t) then
  3406. begin
  3407. if t.is_conditional then
  3408. handleconditional(t)
  3409. else
  3410. begin
  3411. Message1(scan_d_handling_switch,'$'+hs);
  3412. t.proc();
  3413. end;
  3414. end
  3415. else
  3416. begin
  3417. current_scanner.ignoredirectives.Add(hs,nil);
  3418. Message1(scan_w_illegal_directive,'$'+hs);
  3419. end;
  3420. { conditionals already read the comment }
  3421. if (current_scanner.comment_level>0) then
  3422. current_scanner.readcomment;
  3423. { we've read the whole comment }
  3424. aktcommentstyle:=comment_none;
  3425. end;
  3426. end;
  3427. procedure tscannerfile.readchar;
  3428. begin
  3429. c:=inputpointer^;
  3430. if c=#0 then
  3431. reload
  3432. else
  3433. inc(inputpointer);
  3434. end;
  3435. procedure tscannerfile.readstring;
  3436. var
  3437. i : longint;
  3438. err : boolean;
  3439. begin
  3440. err:=false;
  3441. i:=0;
  3442. repeat
  3443. case c of
  3444. '_',
  3445. '0'..'9',
  3446. 'A'..'Z' :
  3447. begin
  3448. if i<255 then
  3449. begin
  3450. inc(i);
  3451. orgpattern[i]:=c;
  3452. pattern[i]:=c;
  3453. end
  3454. else
  3455. begin
  3456. if not err then
  3457. begin
  3458. Message(scan_e_string_exceeds_255_chars);
  3459. err:=true;
  3460. end;
  3461. end;
  3462. c:=inputpointer^;
  3463. inc(inputpointer);
  3464. end;
  3465. 'a'..'z' :
  3466. begin
  3467. if i<255 then
  3468. begin
  3469. inc(i);
  3470. orgpattern[i]:=c;
  3471. pattern[i]:=chr(ord(c)-32)
  3472. end
  3473. else
  3474. begin
  3475. if not err then
  3476. begin
  3477. Message(scan_e_string_exceeds_255_chars);
  3478. err:=true;
  3479. end;
  3480. end;
  3481. c:=inputpointer^;
  3482. inc(inputpointer);
  3483. end;
  3484. #0 :
  3485. reload;
  3486. else
  3487. break;
  3488. end;
  3489. until false;
  3490. orgpattern[0]:=chr(i);
  3491. pattern[0]:=chr(i);
  3492. end;
  3493. procedure tscannerfile.readnumber;
  3494. var
  3495. base,
  3496. i : longint;
  3497. begin
  3498. case c of
  3499. '%' :
  3500. begin
  3501. readchar;
  3502. base:=2;
  3503. pattern[1]:='%';
  3504. i:=1;
  3505. end;
  3506. '&' :
  3507. begin
  3508. readchar;
  3509. base:=8;
  3510. pattern[1]:='&';
  3511. i:=1;
  3512. end;
  3513. '$' :
  3514. begin
  3515. readchar;
  3516. base:=16;
  3517. pattern[1]:='$';
  3518. i:=1;
  3519. end;
  3520. else
  3521. begin
  3522. base:=10;
  3523. i:=0;
  3524. end;
  3525. end;
  3526. while ((base>=10) and (c in ['0'..'9'])) or
  3527. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  3528. ((base=8) and (c in ['0'..'7'])) or
  3529. ((base=2) and (c in ['0'..'1'])) do
  3530. begin
  3531. if i<255 then
  3532. begin
  3533. inc(i);
  3534. pattern[i]:=c;
  3535. end;
  3536. readchar;
  3537. end;
  3538. pattern[0]:=chr(i);
  3539. end;
  3540. function tscannerfile.readid:string;
  3541. begin
  3542. readstring;
  3543. readid:=pattern;
  3544. end;
  3545. function tscannerfile.readval:longint;
  3546. var
  3547. l : longint;
  3548. w : integer;
  3549. begin
  3550. readnumber;
  3551. val(pattern,l,w);
  3552. readval:=l;
  3553. end;
  3554. function tscannerfile.readcomment:string;
  3555. var
  3556. i : longint;
  3557. begin
  3558. i:=0;
  3559. repeat
  3560. case c of
  3561. '{' :
  3562. begin
  3563. if aktcommentstyle=comment_tp then
  3564. inc_comment_level;
  3565. end;
  3566. '}' :
  3567. begin
  3568. if aktcommentstyle=comment_tp then
  3569. begin
  3570. readchar;
  3571. dec_comment_level;
  3572. if comment_level=0 then
  3573. break
  3574. else
  3575. continue;
  3576. end;
  3577. end;
  3578. '*' :
  3579. begin
  3580. if aktcommentstyle=comment_oldtp then
  3581. begin
  3582. readchar;
  3583. if c=')' then
  3584. begin
  3585. readchar;
  3586. dec_comment_level;
  3587. break;
  3588. end
  3589. else
  3590. { Add both characters !!}
  3591. if (i<255) then
  3592. begin
  3593. inc(i);
  3594. readcomment[i]:='*';
  3595. if (i<255) then
  3596. begin
  3597. inc(i);
  3598. readcomment[i]:=c;
  3599. end;
  3600. end;
  3601. end
  3602. else
  3603. { Not old TP comment, so add...}
  3604. begin
  3605. if (i<255) then
  3606. begin
  3607. inc(i);
  3608. readcomment[i]:='*';
  3609. end;
  3610. end;
  3611. end;
  3612. #10,#13 :
  3613. linebreak;
  3614. #26 :
  3615. end_of_file;
  3616. else
  3617. begin
  3618. if (i<255) then
  3619. begin
  3620. inc(i);
  3621. readcomment[i]:=c;
  3622. end;
  3623. end;
  3624. end;
  3625. readchar;
  3626. until false;
  3627. readcomment[0]:=chr(i);
  3628. end;
  3629. function tscannerfile.readquotedstring:string;
  3630. var
  3631. i : longint;
  3632. msgwritten : boolean;
  3633. begin
  3634. i:=0;
  3635. msgwritten:=false;
  3636. if (c='''') then
  3637. begin
  3638. repeat
  3639. readchar;
  3640. case c of
  3641. #26 :
  3642. end_of_file;
  3643. #10,#13 :
  3644. Message(scan_f_string_exceeds_line);
  3645. '''' :
  3646. begin
  3647. readchar;
  3648. if c<>'''' then
  3649. break;
  3650. end;
  3651. end;
  3652. if i<255 then
  3653. begin
  3654. inc(i);
  3655. result[i]:=c;
  3656. end
  3657. else
  3658. begin
  3659. if not msgwritten then
  3660. begin
  3661. Message(scan_e_string_exceeds_255_chars);
  3662. msgwritten:=true;
  3663. end;
  3664. end;
  3665. until false;
  3666. end;
  3667. result[0]:=chr(i);
  3668. end;
  3669. function tscannerfile.readstate:char;
  3670. var
  3671. state : char;
  3672. begin
  3673. state:=' ';
  3674. if c=' ' then
  3675. begin
  3676. current_scanner.skipspace;
  3677. current_scanner.readid;
  3678. if pattern='ON' then
  3679. state:='+'
  3680. else
  3681. if pattern='OFF' then
  3682. state:='-';
  3683. end
  3684. else
  3685. state:=c;
  3686. if not (state in ['+','-']) then
  3687. Message(scan_e_wrong_switch_toggle);
  3688. readstate:=state;
  3689. end;
  3690. function tscannerfile.readstatedefault:char;
  3691. var
  3692. state : char;
  3693. begin
  3694. state:=' ';
  3695. if c=' ' then
  3696. begin
  3697. current_scanner.skipspace;
  3698. current_scanner.readid;
  3699. if pattern='ON' then
  3700. state:='+'
  3701. else
  3702. if pattern='OFF' then
  3703. state:='-'
  3704. else
  3705. if pattern='DEFAULT' then
  3706. state:='*';
  3707. end
  3708. else
  3709. state:=c;
  3710. if not (state in ['+','-','*']) then
  3711. Message(scan_e_wrong_switch_toggle_default);
  3712. readstatedefault:=state;
  3713. end;
  3714. procedure tscannerfile.skipspace;
  3715. begin
  3716. repeat
  3717. case c of
  3718. #26 :
  3719. begin
  3720. reload;
  3721. if (c=#26) and not assigned(inputfile.next) then
  3722. break;
  3723. continue;
  3724. end;
  3725. #10,
  3726. #13 :
  3727. linebreak;
  3728. #9,#11,#12,' ' :
  3729. ;
  3730. else
  3731. break;
  3732. end;
  3733. readchar;
  3734. until false;
  3735. end;
  3736. procedure tscannerfile.skipuntildirective;
  3737. var
  3738. found : longint;
  3739. next_char_loaded : boolean;
  3740. begin
  3741. found:=0;
  3742. next_char_loaded:=false;
  3743. repeat
  3744. case c of
  3745. #10,
  3746. #13 :
  3747. linebreak;
  3748. #26 :
  3749. begin
  3750. reload;
  3751. if (c=#26) and not assigned(inputfile.next) then
  3752. end_of_file;
  3753. continue;
  3754. end;
  3755. '{' :
  3756. begin
  3757. if (aktcommentstyle in [comment_tp,comment_none]) then
  3758. begin
  3759. aktcommentstyle:=comment_tp;
  3760. if (comment_level=0) then
  3761. found:=1;
  3762. inc_comment_level;
  3763. end;
  3764. end;
  3765. '*' :
  3766. begin
  3767. if (aktcommentstyle=comment_oldtp) then
  3768. begin
  3769. readchar;
  3770. if c=')' then
  3771. begin
  3772. dec_comment_level;
  3773. found:=0;
  3774. aktcommentstyle:=comment_none;
  3775. end
  3776. else
  3777. next_char_loaded:=true;
  3778. end
  3779. else
  3780. found := 0;
  3781. end;
  3782. '}' :
  3783. begin
  3784. if (aktcommentstyle=comment_tp) then
  3785. begin
  3786. dec_comment_level;
  3787. if (comment_level=0) then
  3788. aktcommentstyle:=comment_none;
  3789. found:=0;
  3790. end;
  3791. end;
  3792. '$' :
  3793. begin
  3794. if found=1 then
  3795. found:=2;
  3796. end;
  3797. '''' :
  3798. if (aktcommentstyle=comment_none) then
  3799. begin
  3800. repeat
  3801. readchar;
  3802. case c of
  3803. #26 :
  3804. end_of_file;
  3805. #10,#13 :
  3806. break;
  3807. '''' :
  3808. begin
  3809. readchar;
  3810. if c<>'''' then
  3811. begin
  3812. next_char_loaded:=true;
  3813. break;
  3814. end;
  3815. end;
  3816. end;
  3817. until false;
  3818. end;
  3819. '(' :
  3820. begin
  3821. if (aktcommentstyle=comment_none) then
  3822. begin
  3823. readchar;
  3824. if c='*' then
  3825. begin
  3826. readchar;
  3827. if c='$' then
  3828. begin
  3829. found:=2;
  3830. inc_comment_level;
  3831. aktcommentstyle:=comment_oldtp;
  3832. end
  3833. else
  3834. begin
  3835. skipoldtpcomment;
  3836. next_char_loaded:=true;
  3837. end;
  3838. end
  3839. else
  3840. next_char_loaded:=true;
  3841. end
  3842. else
  3843. found:=0;
  3844. end;
  3845. '/' :
  3846. begin
  3847. if (aktcommentstyle=comment_none) then
  3848. begin
  3849. readchar;
  3850. if c='/' then
  3851. skipdelphicomment;
  3852. next_char_loaded:=true;
  3853. end
  3854. else
  3855. found:=0;
  3856. end;
  3857. else
  3858. found:=0;
  3859. end;
  3860. if next_char_loaded then
  3861. next_char_loaded:=false
  3862. else
  3863. readchar;
  3864. until (found=2);
  3865. end;
  3866. {****************************************************************************
  3867. Comment Handling
  3868. ****************************************************************************}
  3869. procedure tscannerfile.skipcomment;
  3870. begin
  3871. aktcommentstyle:=comment_tp;
  3872. readchar;
  3873. inc_comment_level;
  3874. { handle compiler switches }
  3875. if (c='$') then
  3876. handledirectives;
  3877. { handle_switches can dec comment_level, }
  3878. while (comment_level>0) do
  3879. begin
  3880. case c of
  3881. '{' :
  3882. inc_comment_level;
  3883. '}' :
  3884. dec_comment_level;
  3885. #10,#13 :
  3886. linebreak;
  3887. #26 :
  3888. begin
  3889. reload;
  3890. if (c=#26) and not assigned(inputfile.next) then
  3891. end_of_file;
  3892. continue;
  3893. end;
  3894. end;
  3895. readchar;
  3896. end;
  3897. aktcommentstyle:=comment_none;
  3898. end;
  3899. procedure tscannerfile.skipdelphicomment;
  3900. begin
  3901. aktcommentstyle:=comment_delphi;
  3902. inc_comment_level;
  3903. readchar;
  3904. { this is not supported }
  3905. if c='$' then
  3906. Message(scan_w_wrong_styled_switch);
  3907. { skip comment }
  3908. while not (c in [#10,#13,#26]) do
  3909. readchar;
  3910. dec_comment_level;
  3911. aktcommentstyle:=comment_none;
  3912. end;
  3913. procedure tscannerfile.skipoldtpcomment;
  3914. var
  3915. found : longint;
  3916. begin
  3917. aktcommentstyle:=comment_oldtp;
  3918. inc_comment_level;
  3919. { only load a char if last already processed,
  3920. was cause of bug1634 PM }
  3921. if c=#0 then
  3922. readchar;
  3923. { this is now supported }
  3924. if (c='$') then
  3925. handledirectives;
  3926. { skip comment }
  3927. while (comment_level>0) do
  3928. begin
  3929. found:=0;
  3930. repeat
  3931. case c of
  3932. #26 :
  3933. begin
  3934. reload;
  3935. if (c=#26) and not assigned(inputfile.next) then
  3936. end_of_file;
  3937. continue;
  3938. end;
  3939. #10,#13 :
  3940. begin
  3941. if found=4 then
  3942. inc_comment_level;
  3943. linebreak;
  3944. found:=0;
  3945. end;
  3946. '*' :
  3947. begin
  3948. if found=3 then
  3949. found:=4
  3950. else
  3951. found:=1;
  3952. end;
  3953. ')' :
  3954. begin
  3955. if found in [1,4] then
  3956. begin
  3957. dec_comment_level;
  3958. if comment_level=0 then
  3959. found:=2
  3960. else
  3961. found:=0;
  3962. end
  3963. else
  3964. found:=0;
  3965. end;
  3966. '(' :
  3967. begin
  3968. if found=4 then
  3969. inc_comment_level;
  3970. found:=3;
  3971. end;
  3972. else
  3973. begin
  3974. if found=4 then
  3975. inc_comment_level;
  3976. found:=0;
  3977. end;
  3978. end;
  3979. readchar;
  3980. until (found=2);
  3981. end;
  3982. aktcommentstyle:=comment_none;
  3983. end;
  3984. {****************************************************************************
  3985. Token Scanner
  3986. ****************************************************************************}
  3987. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  3988. var
  3989. code : integer;
  3990. len,
  3991. low,high,mid : longint;
  3992. w : word;
  3993. m : longint;
  3994. mac : tmacro;
  3995. asciinr : string[33];
  3996. iswidestring : boolean;
  3997. label
  3998. exit_label;
  3999. begin
  4000. flushpendingswitchesstate;
  4001. { record tokens? }
  4002. if allowrecordtoken and
  4003. assigned(recordtokenbuf) then
  4004. recordtoken;
  4005. { replay tokens? }
  4006. if assigned(replaytokenbuf) then
  4007. begin
  4008. replaytoken;
  4009. goto exit_label;
  4010. end;
  4011. { was there already a token read, then return that token }
  4012. if nexttoken<>NOTOKEN then
  4013. begin
  4014. setnexttoken;
  4015. goto exit_label;
  4016. end;
  4017. { Skip all spaces and comments }
  4018. repeat
  4019. case c of
  4020. '{' :
  4021. skipcomment;
  4022. #26 :
  4023. begin
  4024. reload;
  4025. if (c=#26) and not assigned(inputfile.next) then
  4026. break;
  4027. end;
  4028. ' ',#9..#13 :
  4029. begin
  4030. {$ifdef PREPROCWRITE}
  4031. if parapreprocess then
  4032. begin
  4033. if c=#10 then
  4034. preprocfile.eolfound:=true
  4035. else
  4036. preprocfile.spacefound:=true;
  4037. end;
  4038. {$endif PREPROCWRITE}
  4039. skipspace;
  4040. end
  4041. else
  4042. break;
  4043. end;
  4044. until false;
  4045. { Save current token position, for EOF its already loaded }
  4046. if c<>#26 then
  4047. gettokenpos;
  4048. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  4049. if c in ['A'..'Z','a'..'z','_'] then
  4050. begin
  4051. readstring;
  4052. token:=_ID;
  4053. idtoken:=_ID;
  4054. { keyword or any other known token,
  4055. pattern is always uppercased }
  4056. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4057. begin
  4058. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4059. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4060. while low<high do
  4061. begin
  4062. mid:=(high+low+1) shr 1;
  4063. if pattern<tokeninfo^[ttoken(mid)].str then
  4064. high:=mid-1
  4065. else
  4066. low:=mid;
  4067. end;
  4068. with tokeninfo^[ttoken(high)] do
  4069. if pattern=str then
  4070. begin
  4071. if (keyword*current_settings.modeswitches)<>[] then
  4072. if op=NOTOKEN then
  4073. token:=ttoken(high)
  4074. else
  4075. token:=op;
  4076. idtoken:=ttoken(high);
  4077. end;
  4078. end;
  4079. { Only process identifiers and not keywords }
  4080. if token=_ID then
  4081. begin
  4082. { this takes some time ... }
  4083. if (cs_support_macro in current_settings.moduleswitches) then
  4084. begin
  4085. mac:=tmacro(search_macro(pattern));
  4086. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  4087. begin
  4088. if yylexcount<max_macro_nesting then
  4089. begin
  4090. mac.is_used:=true;
  4091. inc(yylexcount);
  4092. substitutemacro(pattern,mac.buftext,mac.buflen,
  4093. mac.fileinfo.line,mac.fileinfo.fileindex);
  4094. { handle empty macros }
  4095. if c=#0 then
  4096. reload;
  4097. readtoken(false);
  4098. { that's all folks }
  4099. dec(yylexcount);
  4100. exit;
  4101. end
  4102. else
  4103. Message(scan_w_macro_too_deep);
  4104. end;
  4105. end;
  4106. end;
  4107. { return token }
  4108. goto exit_label;
  4109. end
  4110. else
  4111. begin
  4112. idtoken:=_NOID;
  4113. case c of
  4114. '$' :
  4115. begin
  4116. readnumber;
  4117. token:=_INTCONST;
  4118. goto exit_label;
  4119. end;
  4120. '%' :
  4121. begin
  4122. if not(m_fpc in current_settings.modeswitches) then
  4123. Illegal_Char(c)
  4124. else
  4125. begin
  4126. readnumber;
  4127. token:=_INTCONST;
  4128. goto exit_label;
  4129. end;
  4130. end;
  4131. '&' :
  4132. begin
  4133. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  4134. begin
  4135. readnumber;
  4136. if length(pattern)=1 then
  4137. begin
  4138. readstring;
  4139. token:=_ID;
  4140. idtoken:=_ID;
  4141. end
  4142. else
  4143. token:=_INTCONST;
  4144. goto exit_label;
  4145. end
  4146. else if m_mac in current_settings.modeswitches then
  4147. begin
  4148. readchar;
  4149. token:=_AMPERSAND;
  4150. goto exit_label;
  4151. end
  4152. else
  4153. Illegal_Char(c);
  4154. end;
  4155. '0'..'9' :
  4156. begin
  4157. readnumber;
  4158. if (c in ['.','e','E']) then
  4159. begin
  4160. { first check for a . }
  4161. if c='.' then
  4162. begin
  4163. cachenexttokenpos;
  4164. readchar;
  4165. { is it a .. from a range? }
  4166. case c of
  4167. '.' :
  4168. begin
  4169. readchar;
  4170. token:=_INTCONST;
  4171. nexttoken:=_POINTPOINT;
  4172. goto exit_label;
  4173. end;
  4174. ')' :
  4175. begin
  4176. readchar;
  4177. token:=_INTCONST;
  4178. nexttoken:=_RECKKLAMMER;
  4179. goto exit_label;
  4180. end;
  4181. '0'..'9' :
  4182. begin
  4183. { insert the number after the . }
  4184. pattern:=pattern+'.';
  4185. while c in ['0'..'9'] do
  4186. begin
  4187. pattern:=pattern+c;
  4188. readchar;
  4189. end;
  4190. end;
  4191. else
  4192. begin
  4193. token:=_INTCONST;
  4194. nexttoken:=_POINT;
  4195. goto exit_label;
  4196. end;
  4197. end;
  4198. end;
  4199. { E can also follow after a point is scanned }
  4200. if c in ['e','E'] then
  4201. begin
  4202. pattern:=pattern+'E';
  4203. readchar;
  4204. if c in ['-','+'] then
  4205. begin
  4206. pattern:=pattern+c;
  4207. readchar;
  4208. end;
  4209. if not(c in ['0'..'9']) then
  4210. Illegal_Char(c);
  4211. while c in ['0'..'9'] do
  4212. begin
  4213. pattern:=pattern+c;
  4214. readchar;
  4215. end;
  4216. end;
  4217. token:=_REALNUMBER;
  4218. goto exit_label;
  4219. end;
  4220. token:=_INTCONST;
  4221. goto exit_label;
  4222. end;
  4223. ';' :
  4224. begin
  4225. readchar;
  4226. token:=_SEMICOLON;
  4227. goto exit_label;
  4228. end;
  4229. '[' :
  4230. begin
  4231. readchar;
  4232. token:=_LECKKLAMMER;
  4233. goto exit_label;
  4234. end;
  4235. ']' :
  4236. begin
  4237. readchar;
  4238. token:=_RECKKLAMMER;
  4239. goto exit_label;
  4240. end;
  4241. '(' :
  4242. begin
  4243. readchar;
  4244. case c of
  4245. '*' :
  4246. begin
  4247. c:=#0;{Signal skipoldtpcomment to reload a char }
  4248. skipoldtpcomment;
  4249. readtoken(false);
  4250. exit;
  4251. end;
  4252. '.' :
  4253. begin
  4254. readchar;
  4255. token:=_LECKKLAMMER;
  4256. goto exit_label;
  4257. end;
  4258. end;
  4259. token:=_LKLAMMER;
  4260. goto exit_label;
  4261. end;
  4262. ')' :
  4263. begin
  4264. readchar;
  4265. token:=_RKLAMMER;
  4266. goto exit_label;
  4267. end;
  4268. '+' :
  4269. begin
  4270. readchar;
  4271. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4272. begin
  4273. readchar;
  4274. token:=_PLUSASN;
  4275. goto exit_label;
  4276. end;
  4277. token:=_PLUS;
  4278. goto exit_label;
  4279. end;
  4280. '-' :
  4281. begin
  4282. readchar;
  4283. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4284. begin
  4285. readchar;
  4286. token:=_MINUSASN;
  4287. goto exit_label;
  4288. end;
  4289. token:=_MINUS;
  4290. goto exit_label;
  4291. end;
  4292. ':' :
  4293. begin
  4294. readchar;
  4295. if c='=' then
  4296. begin
  4297. readchar;
  4298. token:=_ASSIGNMENT;
  4299. goto exit_label;
  4300. end;
  4301. token:=_COLON;
  4302. goto exit_label;
  4303. end;
  4304. '*' :
  4305. begin
  4306. readchar;
  4307. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4308. begin
  4309. readchar;
  4310. token:=_STARASN;
  4311. end
  4312. else
  4313. if c='*' then
  4314. begin
  4315. readchar;
  4316. token:=_STARSTAR;
  4317. end
  4318. else
  4319. token:=_STAR;
  4320. goto exit_label;
  4321. end;
  4322. '/' :
  4323. begin
  4324. readchar;
  4325. case c of
  4326. '=' :
  4327. begin
  4328. if (cs_support_c_operators in current_settings.moduleswitches) then
  4329. begin
  4330. readchar;
  4331. token:=_SLASHASN;
  4332. goto exit_label;
  4333. end;
  4334. end;
  4335. '/' :
  4336. begin
  4337. skipdelphicomment;
  4338. readtoken(false);
  4339. exit;
  4340. end;
  4341. end;
  4342. token:=_SLASH;
  4343. goto exit_label;
  4344. end;
  4345. '|' :
  4346. if m_mac in current_settings.modeswitches then
  4347. begin
  4348. readchar;
  4349. token:=_PIPE;
  4350. goto exit_label;
  4351. end
  4352. else
  4353. Illegal_Char(c);
  4354. '=' :
  4355. begin
  4356. readchar;
  4357. token:=_EQ;
  4358. goto exit_label;
  4359. end;
  4360. '.' :
  4361. begin
  4362. readchar;
  4363. case c of
  4364. '.' :
  4365. begin
  4366. readchar;
  4367. case c of
  4368. '.' :
  4369. begin
  4370. readchar;
  4371. token:=_POINTPOINTPOINT;
  4372. goto exit_label;
  4373. end;
  4374. else
  4375. begin
  4376. token:=_POINTPOINT;
  4377. goto exit_label;
  4378. end;
  4379. end;
  4380. end;
  4381. ')' :
  4382. begin
  4383. readchar;
  4384. token:=_RECKKLAMMER;
  4385. goto exit_label;
  4386. end;
  4387. end;
  4388. token:=_POINT;
  4389. goto exit_label;
  4390. end;
  4391. '@' :
  4392. begin
  4393. readchar;
  4394. token:=_KLAMMERAFFE;
  4395. goto exit_label;
  4396. end;
  4397. ',' :
  4398. begin
  4399. readchar;
  4400. token:=_COMMA;
  4401. goto exit_label;
  4402. end;
  4403. '''','#','^' :
  4404. begin
  4405. len:=0;
  4406. cstringpattern:='';
  4407. iswidestring:=false;
  4408. if c='^' then
  4409. begin
  4410. readchar;
  4411. c:=upcase(c);
  4412. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  4413. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  4414. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  4415. begin
  4416. token:=_CARET;
  4417. goto exit_label;
  4418. end
  4419. else
  4420. begin
  4421. inc(len);
  4422. setlength(cstringpattern,256);
  4423. if c<#64 then
  4424. cstringpattern[len]:=chr(ord(c)+64)
  4425. else
  4426. cstringpattern[len]:=chr(ord(c)-64);
  4427. readchar;
  4428. end;
  4429. end;
  4430. repeat
  4431. case c of
  4432. '#' :
  4433. begin
  4434. readchar; { read # }
  4435. case c of
  4436. '$':
  4437. begin
  4438. readchar; { read leading $ }
  4439. asciinr:='$';
  4440. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=5) do
  4441. begin
  4442. asciinr:=asciinr+c;
  4443. readchar;
  4444. end;
  4445. end;
  4446. '&':
  4447. begin
  4448. readchar; { read leading $ }
  4449. asciinr:='&';
  4450. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=7) do
  4451. begin
  4452. asciinr:=asciinr+c;
  4453. readchar;
  4454. end;
  4455. end;
  4456. '%':
  4457. begin
  4458. readchar; { read leading $ }
  4459. asciinr:='%';
  4460. while (upcase(c) in ['0','1']) and (length(asciinr)<=17) do
  4461. begin
  4462. asciinr:=asciinr+c;
  4463. readchar;
  4464. end;
  4465. end;
  4466. else
  4467. begin
  4468. asciinr:='';
  4469. while (c in ['0'..'9']) and (length(asciinr)<=5) do
  4470. begin
  4471. asciinr:=asciinr+c;
  4472. readchar;
  4473. end;
  4474. end;
  4475. end;
  4476. val(asciinr,m,code);
  4477. if (asciinr='') or (code<>0) then
  4478. Message(scan_e_illegal_char_const)
  4479. else if (m<0) or (m>255) or (length(asciinr)>3) then
  4480. begin
  4481. if (m>=0) and (m<=65535) then
  4482. begin
  4483. if not iswidestring then
  4484. begin
  4485. if len>0 then
  4486. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4487. else
  4488. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4489. iswidestring:=true;
  4490. len:=0;
  4491. end;
  4492. concatwidestringchar(patternw,tcompilerwidechar(m));
  4493. end
  4494. else
  4495. Message(scan_e_illegal_char_const)
  4496. end
  4497. else if iswidestring then
  4498. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  4499. else
  4500. begin
  4501. if len>=length(cstringpattern) then
  4502. setlength(cstringpattern,length(cstringpattern)+256);
  4503. inc(len);
  4504. cstringpattern[len]:=chr(m);
  4505. end;
  4506. end;
  4507. '''' :
  4508. begin
  4509. repeat
  4510. readchar;
  4511. case c of
  4512. #26 :
  4513. end_of_file;
  4514. #10,#13 :
  4515. Message(scan_f_string_exceeds_line);
  4516. '''' :
  4517. begin
  4518. readchar;
  4519. if c<>'''' then
  4520. break;
  4521. end;
  4522. end;
  4523. { interpret as utf-8 string? }
  4524. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  4525. begin
  4526. { convert existing string to an utf-8 string }
  4527. if not iswidestring then
  4528. begin
  4529. if len>0 then
  4530. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4531. else
  4532. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4533. iswidestring:=true;
  4534. len:=0;
  4535. end;
  4536. { four or more chars aren't handled }
  4537. if (ord(c) and $f0)=$f0 then
  4538. message(scan_e_utf8_bigger_than_65535)
  4539. { three chars }
  4540. else if (ord(c) and $e0)=$e0 then
  4541. begin
  4542. w:=ord(c) and $f;
  4543. readchar;
  4544. if (ord(c) and $c0)<>$80 then
  4545. message(scan_e_utf8_malformed);
  4546. w:=(w shl 6) or (ord(c) and $3f);
  4547. readchar;
  4548. if (ord(c) and $c0)<>$80 then
  4549. message(scan_e_utf8_malformed);
  4550. w:=(w shl 6) or (ord(c) and $3f);
  4551. concatwidestringchar(patternw,w);
  4552. end
  4553. { two chars }
  4554. else if (ord(c) and $c0)<>0 then
  4555. begin
  4556. w:=ord(c) and $1f;
  4557. readchar;
  4558. if (ord(c) and $c0)<>$80 then
  4559. message(scan_e_utf8_malformed);
  4560. w:=(w shl 6) or (ord(c) and $3f);
  4561. concatwidestringchar(patternw,w);
  4562. end
  4563. { illegal }
  4564. else if (ord(c) and $80)<>0 then
  4565. message(scan_e_utf8_malformed)
  4566. else
  4567. concatwidestringchar(patternw,tcompilerwidechar(c))
  4568. end
  4569. else if iswidestring then
  4570. begin
  4571. if current_settings.sourcecodepage=CP_UTF8 then
  4572. concatwidestringchar(patternw,ord(c))
  4573. else
  4574. concatwidestringchar(patternw,asciichar2unicode(c))
  4575. end
  4576. else
  4577. begin
  4578. if len>=length(cstringpattern) then
  4579. setlength(cstringpattern,length(cstringpattern)+256);
  4580. inc(len);
  4581. cstringpattern[len]:=c;
  4582. end;
  4583. until false;
  4584. end;
  4585. '^' :
  4586. begin
  4587. readchar;
  4588. c:=upcase(c);
  4589. if c<#64 then
  4590. c:=chr(ord(c)+64)
  4591. else
  4592. c:=chr(ord(c)-64);
  4593. if iswidestring then
  4594. concatwidestringchar(patternw,asciichar2unicode(c))
  4595. else
  4596. begin
  4597. if len>=length(cstringpattern) then
  4598. setlength(cstringpattern,length(cstringpattern)+256);
  4599. inc(len);
  4600. cstringpattern[len]:=c;
  4601. end;
  4602. readchar;
  4603. end;
  4604. else
  4605. break;
  4606. end;
  4607. until false;
  4608. { strings with length 1 become const chars }
  4609. if iswidestring then
  4610. begin
  4611. if patternw^.len=1 then
  4612. token:=_CWCHAR
  4613. else
  4614. token:=_CWSTRING;
  4615. end
  4616. else
  4617. begin
  4618. setlength(cstringpattern,len);
  4619. if length(cstringpattern)=1 then
  4620. begin
  4621. token:=_CCHAR;
  4622. pattern:=cstringpattern;
  4623. end
  4624. else
  4625. token:=_CSTRING;
  4626. end;
  4627. goto exit_label;
  4628. end;
  4629. '>' :
  4630. begin
  4631. readchar;
  4632. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4633. token:=_RSHARPBRACKET
  4634. else
  4635. begin
  4636. case c of
  4637. '=' :
  4638. begin
  4639. readchar;
  4640. token:=_GTE;
  4641. goto exit_label;
  4642. end;
  4643. '>' :
  4644. begin
  4645. readchar;
  4646. token:=_OP_SHR;
  4647. goto exit_label;
  4648. end;
  4649. '<' :
  4650. begin { >< is for a symetric diff for sets }
  4651. readchar;
  4652. token:=_SYMDIF;
  4653. goto exit_label;
  4654. end;
  4655. end;
  4656. token:=_GT;
  4657. end;
  4658. goto exit_label;
  4659. end;
  4660. '<' :
  4661. begin
  4662. readchar;
  4663. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4664. token:=_LSHARPBRACKET
  4665. else
  4666. begin
  4667. case c of
  4668. '>' :
  4669. begin
  4670. readchar;
  4671. token:=_NE;
  4672. goto exit_label;
  4673. end;
  4674. '=' :
  4675. begin
  4676. readchar;
  4677. token:=_LTE;
  4678. goto exit_label;
  4679. end;
  4680. '<' :
  4681. begin
  4682. readchar;
  4683. token:=_OP_SHL;
  4684. goto exit_label;
  4685. end;
  4686. end;
  4687. token:=_LT;
  4688. end;
  4689. goto exit_label;
  4690. end;
  4691. #26 :
  4692. begin
  4693. token:=_EOF;
  4694. checkpreprocstack;
  4695. goto exit_label;
  4696. end;
  4697. else
  4698. Illegal_Char(c);
  4699. end;
  4700. end;
  4701. exit_label:
  4702. lasttoken:=token;
  4703. end;
  4704. function tscannerfile.readpreproc:ttoken;
  4705. var
  4706. low,high,mid: longint;
  4707. optoken: ttoken;
  4708. begin
  4709. skipspace;
  4710. case c of
  4711. '_',
  4712. 'A'..'Z',
  4713. 'a'..'z' :
  4714. begin
  4715. readstring;
  4716. optoken:=_ID;
  4717. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4718. begin
  4719. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4720. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4721. while low<high do
  4722. begin
  4723. mid:=(high+low+1) shr 1;
  4724. if pattern<tokeninfo^[ttoken(mid)].str then
  4725. high:=mid-1
  4726. else
  4727. low:=mid;
  4728. end;
  4729. with tokeninfo^[ttoken(high)] do
  4730. if pattern=str then
  4731. begin
  4732. if (keyword*current_settings.modeswitches)<>[] then
  4733. if op=NOTOKEN then
  4734. optoken:=ttoken(high)
  4735. else
  4736. optoken:=op;
  4737. end;
  4738. if not (optoken in preproc_operators) then
  4739. optoken:=_ID;
  4740. end;
  4741. current_scanner.preproc_pattern:=pattern;
  4742. readpreproc:=optoken;
  4743. end;
  4744. '0'..'9' :
  4745. begin
  4746. readnumber;
  4747. if (c in ['.','e','E']) then
  4748. begin
  4749. { first check for a . }
  4750. if c='.' then
  4751. begin
  4752. readchar;
  4753. if c in ['0'..'9'] then
  4754. begin
  4755. { insert the number after the . }
  4756. pattern:=pattern+'.';
  4757. while c in ['0'..'9'] do
  4758. begin
  4759. pattern:=pattern+c;
  4760. readchar;
  4761. end;
  4762. end
  4763. else
  4764. Illegal_Char(c);
  4765. end;
  4766. { E can also follow after a point is scanned }
  4767. if c in ['e','E'] then
  4768. begin
  4769. pattern:=pattern+'E';
  4770. readchar;
  4771. if c in ['-','+'] then
  4772. begin
  4773. pattern:=pattern+c;
  4774. readchar;
  4775. end;
  4776. if not(c in ['0'..'9']) then
  4777. Illegal_Char(c);
  4778. while c in ['0'..'9'] do
  4779. begin
  4780. pattern:=pattern+c;
  4781. readchar;
  4782. end;
  4783. end;
  4784. readpreproc:=_REALNUMBER;
  4785. end
  4786. else
  4787. readpreproc:=_INTCONST;
  4788. current_scanner.preproc_pattern:=pattern;
  4789. end;
  4790. '$','%':
  4791. begin
  4792. readnumber;
  4793. current_scanner.preproc_pattern:=pattern;
  4794. readpreproc:=_INTCONST;
  4795. end;
  4796. '&' :
  4797. begin
  4798. readnumber;
  4799. if length(pattern)=1 then
  4800. begin
  4801. readstring;
  4802. readpreproc:=_ID;
  4803. end
  4804. else
  4805. readpreproc:=_INTCONST;
  4806. current_scanner.preproc_pattern:=pattern;
  4807. end;
  4808. '.' :
  4809. begin
  4810. readchar;
  4811. readpreproc:=_POINT;
  4812. end;
  4813. ',' :
  4814. begin
  4815. readchar;
  4816. readpreproc:=_COMMA;
  4817. end;
  4818. '}' :
  4819. begin
  4820. readpreproc:=_END;
  4821. end;
  4822. '(' :
  4823. begin
  4824. readchar;
  4825. readpreproc:=_LKLAMMER;
  4826. end;
  4827. ')' :
  4828. begin
  4829. readchar;
  4830. readpreproc:=_RKLAMMER;
  4831. end;
  4832. '[' :
  4833. begin
  4834. readchar;
  4835. readpreproc:=_LECKKLAMMER;
  4836. end;
  4837. ']' :
  4838. begin
  4839. readchar;
  4840. readpreproc:=_RECKKLAMMER;
  4841. end;
  4842. '+' :
  4843. begin
  4844. readchar;
  4845. readpreproc:=_PLUS;
  4846. end;
  4847. '-' :
  4848. begin
  4849. readchar;
  4850. readpreproc:=_MINUS;
  4851. end;
  4852. '*' :
  4853. begin
  4854. readchar;
  4855. readpreproc:=_STAR;
  4856. end;
  4857. '/' :
  4858. begin
  4859. readchar;
  4860. readpreproc:=_SLASH;
  4861. end;
  4862. '=' :
  4863. begin
  4864. readchar;
  4865. readpreproc:=_EQ;
  4866. end;
  4867. '>' :
  4868. begin
  4869. readchar;
  4870. if c='=' then
  4871. begin
  4872. readchar;
  4873. readpreproc:=_GTE;
  4874. end
  4875. else
  4876. readpreproc:=_GT;
  4877. end;
  4878. '<' :
  4879. begin
  4880. readchar;
  4881. case c of
  4882. '>' :
  4883. begin
  4884. readchar;
  4885. readpreproc:=_NE;
  4886. end;
  4887. '=' :
  4888. begin
  4889. readchar;
  4890. readpreproc:=_LTE;
  4891. end;
  4892. else
  4893. readpreproc:=_LT;
  4894. end;
  4895. end;
  4896. #26 :
  4897. begin
  4898. readpreproc:=_EOF;
  4899. checkpreprocstack;
  4900. end;
  4901. else
  4902. Illegal_Char(c);
  4903. end;
  4904. end;
  4905. function tscannerfile.asmgetcharstart : char;
  4906. begin
  4907. { return first the character already
  4908. available in c }
  4909. lastasmgetchar:=c;
  4910. result:=asmgetchar;
  4911. end;
  4912. function tscannerfile.asmgetchar : char;
  4913. begin
  4914. if lastasmgetchar<>#0 then
  4915. begin
  4916. c:=lastasmgetchar;
  4917. lastasmgetchar:=#0;
  4918. end
  4919. else
  4920. readchar;
  4921. if in_asm_string then
  4922. begin
  4923. asmgetchar:=c;
  4924. exit;
  4925. end;
  4926. repeat
  4927. case c of
  4928. // the { ... } is used in ARM assembler to define register sets, so we can't used
  4929. // it as comment, either (* ... *), /* ... */ or // ... should be used instead.
  4930. // But compiler directives {$...} are allowed in ARM assembler.
  4931. '{' :
  4932. begin
  4933. {$ifdef arm}
  4934. readchar;
  4935. dec(inputpointer);
  4936. if c<>'$' then
  4937. begin
  4938. asmgetchar:='{';
  4939. exit;
  4940. end
  4941. else
  4942. {$endif arm}
  4943. skipcomment;
  4944. end;
  4945. #10,#13 :
  4946. begin
  4947. linebreak;
  4948. asmgetchar:=c;
  4949. exit;
  4950. end;
  4951. #26 :
  4952. begin
  4953. reload;
  4954. if (c=#26) and not assigned(inputfile.next) then
  4955. end_of_file;
  4956. continue;
  4957. end;
  4958. '/' :
  4959. begin
  4960. readchar;
  4961. if c='/' then
  4962. skipdelphicomment
  4963. else
  4964. begin
  4965. asmgetchar:='/';
  4966. lastasmgetchar:=c;
  4967. exit;
  4968. end;
  4969. end;
  4970. '(' :
  4971. begin
  4972. readchar;
  4973. if c='*' then
  4974. begin
  4975. c:=#0;{Signal skipoldtpcomment to reload a char }
  4976. skipoldtpcomment;
  4977. end
  4978. else
  4979. begin
  4980. asmgetchar:='(';
  4981. lastasmgetchar:=c;
  4982. exit;
  4983. end;
  4984. end;
  4985. else
  4986. begin
  4987. asmgetchar:=c;
  4988. exit;
  4989. end;
  4990. end;
  4991. until false;
  4992. end;
  4993. {*****************************************************************************
  4994. Helpers
  4995. *****************************************************************************}
  4996. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  4997. begin
  4998. if dm in [directive_all, directive_turbo] then
  4999. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  5000. if dm in [directive_all, directive_mac] then
  5001. tdirectiveitem.create(mac_scannerdirectives,s,p);
  5002. end;
  5003. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5004. begin
  5005. if dm in [directive_all, directive_turbo] then
  5006. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  5007. if dm in [directive_all, directive_mac] then
  5008. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  5009. end;
  5010. {*****************************************************************************
  5011. Initialization
  5012. *****************************************************************************}
  5013. procedure InitScanner;
  5014. begin
  5015. InitWideString(patternw);
  5016. turbo_scannerdirectives:=TFPHashObjectList.Create;
  5017. mac_scannerdirectives:=TFPHashObjectList.Create;
  5018. { Common directives and conditionals }
  5019. AddDirective('I',directive_all, @dir_include);
  5020. AddDirective('DEFINE',directive_all, @dir_define);
  5021. AddDirective('UNDEF',directive_all, @dir_undef);
  5022. AddConditional('IF',directive_all, @dir_if);
  5023. AddConditional('IFDEF',directive_all, @dir_ifdef);
  5024. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  5025. AddConditional('ELSE',directive_all, @dir_else);
  5026. AddConditional('ELSEIF',directive_all, @dir_elseif);
  5027. AddConditional('ENDIF',directive_all, @dir_endif);
  5028. { Directives and conditionals for all modes except mode macpas}
  5029. AddDirective('INCLUDE',directive_turbo, @dir_include);
  5030. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  5031. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  5032. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  5033. AddConditional('IFEND',directive_turbo, @dir_endif);
  5034. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  5035. { Directives and conditionals for mode macpas: }
  5036. AddDirective('SETC',directive_mac, @dir_setc);
  5037. AddDirective('DEFINEC',directive_mac, @dir_definec);
  5038. AddDirective('UNDEFC',directive_mac, @dir_undef);
  5039. AddConditional('IFC',directive_mac, @dir_if);
  5040. AddConditional('ELSEC',directive_mac, @dir_else);
  5041. AddConditional('ELIFC',directive_mac, @dir_elseif);
  5042. AddConditional('ENDC',directive_mac, @dir_endif);
  5043. end;
  5044. procedure DoneScanner;
  5045. begin
  5046. turbo_scannerdirectives.Free;
  5047. mac_scannerdirectives.Free;
  5048. DoneWideString(patternw);
  5049. end;
  5050. end.