scanner.pas 197 KB

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