2
0

scanner.pas 192 KB

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