scanner.pas 194 KB

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