scanner.pas 193 KB

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