scanner.pas 188 KB

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