scanner.pas 181 KB

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