scanner.pas 198 KB

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