scanner.pas 193 KB

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