scanner.pas 186 KB

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