scanner.pas 188 KB

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