2
0

scanner.pas 194 KB

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