scanner.pas 194 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777
  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. begin
  1474. if not eval then
  1475. begin
  1476. result:=texprvalue.create_str(searchstr);
  1477. exit;
  1478. end;
  1479. mac:=nil;
  1480. { Substitue macros and compiler variables with their content/value.
  1481. For real macros also do recursive substitution. }
  1482. macrocount:=0;
  1483. repeat
  1484. mac:=tmacro(search_macro(searchstr));
  1485. inc(macrocount);
  1486. if macrocount>max_macro_nesting then
  1487. begin
  1488. Message(scan_w_macro_too_deep);
  1489. break;
  1490. end;
  1491. if assigned(mac) and mac.defined then
  1492. if assigned(mac.buftext) then
  1493. begin
  1494. if mac.buflen>255 then
  1495. begin
  1496. len:=255;
  1497. Message(scan_w_macro_cut_after_255_chars);
  1498. end
  1499. else
  1500. len:=mac.buflen;
  1501. hs[0]:=char(len);
  1502. move(mac.buftext^,hs[1],len);
  1503. searchstr:=upcase(hs);
  1504. mac.is_used:=true;
  1505. end
  1506. else
  1507. begin
  1508. Message1(scan_e_error_macro_lacks_value,searchstr);
  1509. break;
  1510. end
  1511. else
  1512. break;
  1513. if mac.is_compiler_var then
  1514. break;
  1515. until false;
  1516. { At this point, result do contain the value. Do some decoding and
  1517. determine the type.}
  1518. result:=texprvalue.try_parse_number(searchstr);
  1519. if not assigned(result) then
  1520. begin
  1521. if assigned(mac) and (searchstr='FALSE') then
  1522. result:=texprvalue.create_bool(false)
  1523. else if assigned(mac) and (searchstr='TRUE') then
  1524. result:=texprvalue.create_bool(true)
  1525. else if (m_mac in current_settings.modeswitches) and
  1526. (not assigned(mac) or not mac.defined) and
  1527. (macrocount = 1) then
  1528. begin
  1529. {Errors in mode mac is issued here. For non macpas modes there is
  1530. more liberty, but the error will eventually be caught at a later stage.}
  1531. Message1(scan_e_error_macro_undefined,searchstr);
  1532. result:=texprvalue.create_str(searchstr); { just to have something }
  1533. end
  1534. else
  1535. result:=texprvalue.create_str(searchstr);
  1536. end;
  1537. end;
  1538. function preproc_factor(eval: Boolean):texprvalue;
  1539. var
  1540. hs,countstr,storedpattern: string;
  1541. mac: tmacro;
  1542. srsym : tsym;
  1543. srsymtable : TSymtable;
  1544. hdef : TDef;
  1545. l : longint;
  1546. hasKlammer: Boolean;
  1547. exprvalue:texprvalue;
  1548. ns:tnormalset;
  1549. begin
  1550. result:=nil;
  1551. hasKlammer:=false;
  1552. if current_scanner.preproc_token=_ID then
  1553. begin
  1554. if current_scanner.preproc_pattern='DEFINED' then
  1555. begin
  1556. preproc_consume(_ID);
  1557. current_scanner.skipspace;
  1558. if current_scanner.preproc_token =_LKLAMMER then
  1559. begin
  1560. preproc_consume(_LKLAMMER);
  1561. current_scanner.skipspace;
  1562. hasKlammer:= true;
  1563. end
  1564. else if (m_mac in current_settings.modeswitches) then
  1565. hasKlammer:= false
  1566. else
  1567. Message(scan_e_error_in_preproc_expr);
  1568. if current_scanner.preproc_token =_ID then
  1569. begin
  1570. hs := current_scanner.preproc_pattern;
  1571. mac := tmacro(search_macro(hs));
  1572. if assigned(mac) and mac.defined then
  1573. begin
  1574. result:=texprvalue.create_bool(true);
  1575. mac.is_used:=true;
  1576. end
  1577. else
  1578. result:=texprvalue.create_bool(false);
  1579. preproc_consume(_ID);
  1580. current_scanner.skipspace;
  1581. end
  1582. else
  1583. Message(scan_e_error_in_preproc_expr);
  1584. if hasKlammer then
  1585. if current_scanner.preproc_token =_RKLAMMER then
  1586. preproc_consume(_RKLAMMER)
  1587. else
  1588. Message(scan_e_error_in_preproc_expr);
  1589. end
  1590. else
  1591. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  1592. begin
  1593. preproc_consume(_ID);
  1594. current_scanner.skipspace;
  1595. if current_scanner.preproc_token =_ID then
  1596. begin
  1597. hs := current_scanner.preproc_pattern;
  1598. mac := tmacro(search_macro(hs));
  1599. if assigned(mac) then
  1600. begin
  1601. result:=texprvalue.create_bool(false);
  1602. mac.is_used:=true;
  1603. end
  1604. else
  1605. result:=texprvalue.create_bool(true);
  1606. preproc_consume(_ID);
  1607. current_scanner.skipspace;
  1608. end
  1609. else
  1610. Message(scan_e_error_in_preproc_expr);
  1611. end
  1612. else
  1613. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  1614. begin
  1615. preproc_consume(_ID);
  1616. current_scanner.skipspace;
  1617. if current_scanner.preproc_token =_LKLAMMER then
  1618. begin
  1619. preproc_consume(_LKLAMMER);
  1620. current_scanner.skipspace;
  1621. end
  1622. else
  1623. Message(scan_e_error_in_preproc_expr);
  1624. if not (current_scanner.preproc_token = _ID) then
  1625. Message(scan_e_error_in_preproc_expr);
  1626. hs:=current_scanner.preproc_pattern;
  1627. if (length(hs) > 1) then
  1628. {This is allowed in Metrowerks Pascal}
  1629. Message(scan_e_error_in_preproc_expr)
  1630. else
  1631. begin
  1632. if CheckSwitch(hs[1],'+') then
  1633. result:=texprvalue.create_bool(true)
  1634. else
  1635. result:=texprvalue.create_bool(false);
  1636. end;
  1637. preproc_consume(_ID);
  1638. current_scanner.skipspace;
  1639. if current_scanner.preproc_token =_RKLAMMER then
  1640. preproc_consume(_RKLAMMER)
  1641. else
  1642. Message(scan_e_error_in_preproc_expr);
  1643. end
  1644. else
  1645. if current_scanner.preproc_pattern='SIZEOF' then
  1646. begin
  1647. preproc_consume(_ID);
  1648. current_scanner.skipspace;
  1649. if current_scanner.preproc_token =_LKLAMMER then
  1650. begin
  1651. preproc_consume(_LKLAMMER);
  1652. current_scanner.skipspace;
  1653. end
  1654. else
  1655. Message(scan_e_preproc_syntax_error);
  1656. storedpattern:=current_scanner.preproc_pattern;
  1657. preproc_consume(_ID);
  1658. current_scanner.skipspace;
  1659. if eval then
  1660. if searchsym(storedpattern,srsym,srsymtable) then
  1661. begin
  1662. try_consume_nestedsym(srsym,srsymtable);
  1663. l:=0;
  1664. if assigned(srsym) then
  1665. case srsym.typ of
  1666. staticvarsym,
  1667. localvarsym,
  1668. paravarsym :
  1669. l:=tabstractvarsym(srsym).getsize;
  1670. typesym:
  1671. l:=ttypesym(srsym).typedef.size;
  1672. else
  1673. Message(scan_e_error_in_preproc_expr);
  1674. end;
  1675. result:=texprvalue.create_int(l);
  1676. end
  1677. else
  1678. Message1(sym_e_id_not_found,storedpattern);
  1679. if current_scanner.preproc_token =_RKLAMMER then
  1680. preproc_consume(_RKLAMMER)
  1681. else
  1682. Message(scan_e_preproc_syntax_error);
  1683. end
  1684. else
  1685. if current_scanner.preproc_pattern='HIGH' then
  1686. begin
  1687. preproc_consume(_ID);
  1688. current_scanner.skipspace;
  1689. if current_scanner.preproc_token =_LKLAMMER then
  1690. begin
  1691. preproc_consume(_LKLAMMER);
  1692. current_scanner.skipspace;
  1693. end
  1694. else
  1695. Message(scan_e_preproc_syntax_error);
  1696. storedpattern:=current_scanner.preproc_pattern;
  1697. preproc_consume(_ID);
  1698. current_scanner.skipspace;
  1699. if eval then
  1700. if searchsym(storedpattern,srsym,srsymtable) then
  1701. begin
  1702. try_consume_nestedsym(srsym,srsymtable);
  1703. hdef:=nil;
  1704. hs:='';
  1705. l:=0;
  1706. if assigned(srsym) then
  1707. case srsym.typ of
  1708. staticvarsym,
  1709. localvarsym,
  1710. paravarsym :
  1711. hdef:=tabstractvarsym(srsym).vardef;
  1712. typesym:
  1713. hdef:=ttypesym(srsym).typedef;
  1714. else
  1715. Message(scan_e_error_in_preproc_expr);
  1716. end;
  1717. if assigned(hdef) then
  1718. begin
  1719. if hdef.typ=setdef then
  1720. hdef:=tsetdef(hdef).elementdef;
  1721. case hdef.typ of
  1722. orddef:
  1723. with torddef(hdef).high do
  1724. if signed then
  1725. result:=texprvalue.create_int(svalue)
  1726. else
  1727. result:=texprvalue.create_uint(uvalue);
  1728. enumdef:
  1729. result:=texprvalue.create_int(tenumdef(hdef).maxval);
  1730. arraydef:
  1731. if is_open_array(hdef) or is_array_of_const(hdef) or is_dynamic_array(hdef) then
  1732. Message(type_e_mismatch)
  1733. else
  1734. result:=texprvalue.create_int(tarraydef(hdef).highrange);
  1735. stringdef:
  1736. if is_open_string(hdef) or is_ansistring(hdef) or is_wide_or_unicode_string(hdef) then
  1737. Message(type_e_mismatch)
  1738. else
  1739. result:=texprvalue.create_int(tstringdef(hdef).len);
  1740. else
  1741. Message(type_e_mismatch);
  1742. end;
  1743. end;
  1744. end
  1745. else
  1746. Message1(sym_e_id_not_found,storedpattern);
  1747. if current_scanner.preproc_token =_RKLAMMER then
  1748. preproc_consume(_RKLAMMER)
  1749. else
  1750. Message(scan_e_preproc_syntax_error);
  1751. end
  1752. else
  1753. if current_scanner.preproc_pattern='DECLARED' then
  1754. begin
  1755. preproc_consume(_ID);
  1756. current_scanner.skipspace;
  1757. if current_scanner.preproc_token =_LKLAMMER then
  1758. begin
  1759. preproc_consume(_LKLAMMER);
  1760. current_scanner.skipspace;
  1761. end
  1762. else
  1763. Message(scan_e_error_in_preproc_expr);
  1764. if current_scanner.preproc_token =_ID then
  1765. begin
  1766. hs := upper(current_scanner.preproc_pattern);
  1767. preproc_consume(_ID);
  1768. current_scanner.skipspace;
  1769. if current_scanner.preproc_token in [_LT,_LSHARPBRACKET] then
  1770. begin
  1771. l:=1;
  1772. preproc_consume(current_scanner.preproc_token);
  1773. current_scanner.skipspace;
  1774. while current_scanner.preproc_token=_COMMA do
  1775. begin
  1776. inc(l);
  1777. preproc_consume(_COMMA);
  1778. current_scanner.skipspace;
  1779. end;
  1780. if not (current_scanner.preproc_token in [_GT,_RSHARPBRACKET]) then
  1781. Message(scan_e_error_in_preproc_expr)
  1782. else
  1783. preproc_consume(current_scanner.preproc_token);
  1784. str(l,countstr);
  1785. hs:=hs+'$'+countstr;
  1786. end
  1787. else
  1788. { special case: <> }
  1789. if current_scanner.preproc_token=_NE then
  1790. begin
  1791. hs:=hs+'$1';
  1792. preproc_consume(_NE);
  1793. end;
  1794. current_scanner.skipspace;
  1795. if searchsym(hs,srsym,srsymtable) then
  1796. begin
  1797. { TSomeGeneric<...> also adds a TSomeGeneric symbol }
  1798. if (sp_generic_dummy in srsym.symoptions) and
  1799. (srsym.typ=typesym) and
  1800. (
  1801. { mode delphi}
  1802. (ttypesym(srsym).typedef.typ in [undefineddef,errordef]) or
  1803. { non-delphi modes }
  1804. (df_generic in ttypesym(srsym).typedef.defoptions)
  1805. ) then
  1806. result:=texprvalue.create_bool(false)
  1807. else
  1808. result:=texprvalue.create_bool(true);
  1809. end
  1810. else
  1811. result:=texprvalue.create_bool(false);
  1812. end
  1813. else
  1814. Message(scan_e_error_in_preproc_expr);
  1815. if current_scanner.preproc_token =_RKLAMMER then
  1816. preproc_consume(_RKLAMMER)
  1817. else
  1818. Message(scan_e_error_in_preproc_expr);
  1819. end
  1820. else
  1821. if current_scanner.preproc_pattern='ORD' then
  1822. begin
  1823. preproc_consume(_ID);
  1824. current_scanner.skipspace;
  1825. if current_scanner.preproc_token =_LKLAMMER then
  1826. begin
  1827. preproc_consume(_LKLAMMER);
  1828. current_scanner.skipspace;
  1829. end
  1830. else
  1831. Message(scan_e_preproc_syntax_error);
  1832. exprvalue:=preproc_factor(eval);
  1833. if eval then
  1834. begin
  1835. if is_ordinal(exprvalue.def) then
  1836. result:=texprvalue.create_int(exprvalue.asInt)
  1837. else
  1838. begin
  1839. exprvalue.error('Ordinal','ORD');
  1840. result:=texprvalue.create_int(0);
  1841. end;
  1842. end
  1843. else
  1844. result:=texprvalue.create_int(0);
  1845. exprvalue.free;
  1846. if current_scanner.preproc_token =_RKLAMMER then
  1847. preproc_consume(_RKLAMMER)
  1848. else
  1849. Message(scan_e_error_in_preproc_expr);
  1850. end
  1851. else
  1852. if current_scanner.preproc_pattern='NOT' then
  1853. begin
  1854. preproc_consume(_ID);
  1855. exprvalue:=preproc_factor(eval);
  1856. if eval then
  1857. result:=exprvalue.evaluate(nil,_OP_NOT)
  1858. else
  1859. result:=texprvalue.create_bool(false); {Just to have something}
  1860. exprvalue.free;
  1861. end
  1862. else
  1863. if (current_scanner.preproc_pattern='TRUE') then
  1864. begin
  1865. result:=texprvalue.create_bool(true);
  1866. preproc_consume(_ID);
  1867. end
  1868. else
  1869. if (current_scanner.preproc_pattern='FALSE') then
  1870. begin
  1871. result:=texprvalue.create_bool(false);
  1872. preproc_consume(_ID);
  1873. end
  1874. else
  1875. begin
  1876. storedpattern:=current_scanner.preproc_pattern;
  1877. preproc_consume(_ID);
  1878. current_scanner.skipspace;
  1879. { first look for a macros/int/float }
  1880. result:=preproc_substitutedtoken(storedpattern,eval);
  1881. if eval and (result.consttyp=conststring) then
  1882. begin
  1883. if searchsym(storedpattern,srsym,srsymtable) then
  1884. begin
  1885. try_consume_nestedsym(srsym,srsymtable);
  1886. if assigned(srsym) then
  1887. case srsym.typ of
  1888. constsym:
  1889. begin
  1890. result.free;
  1891. result:=texprvalue.create_const(tconstsym(srsym));
  1892. end;
  1893. enumsym:
  1894. begin
  1895. result.free;
  1896. result:=texprvalue.create_int(tenumsym(srsym).value);
  1897. end;
  1898. end;
  1899. end
  1900. end
  1901. { skip id(<expr>) if expression must not be evaluated }
  1902. else if not(eval) and (result.consttyp=conststring) then
  1903. begin
  1904. if current_scanner.preproc_token =_LKLAMMER then
  1905. begin
  1906. preproc_consume(_LKLAMMER);
  1907. current_scanner.skipspace;
  1908. result:=preproc_factor(false);
  1909. if current_scanner.preproc_token =_RKLAMMER then
  1910. preproc_consume(_RKLAMMER)
  1911. else
  1912. Message(scan_e_error_in_preproc_expr);
  1913. end;
  1914. end;
  1915. end
  1916. end
  1917. else if current_scanner.preproc_token =_LKLAMMER then
  1918. begin
  1919. preproc_consume(_LKLAMMER);
  1920. result:=preproc_sub_expr(opcompare,eval);
  1921. preproc_consume(_RKLAMMER);
  1922. end
  1923. else if current_scanner.preproc_token = _LECKKLAMMER then
  1924. begin
  1925. preproc_consume(_LECKKLAMMER);
  1926. ns:=[];
  1927. while current_scanner.preproc_token in [_ID,_INTCONST] do
  1928. begin
  1929. exprvalue:=preproc_factor(eval);
  1930. include(ns,exprvalue.asInt);
  1931. if current_scanner.preproc_token = _COMMA then
  1932. preproc_consume(_COMMA);
  1933. end;
  1934. // TODO Add check of setElemType
  1935. preproc_consume(_RECKKLAMMER);
  1936. result:=texprvalue.create_set(ns);
  1937. end
  1938. else if current_scanner.preproc_token = _INTCONST then
  1939. begin
  1940. result:=texprvalue.try_parse_number(current_scanner.preproc_pattern);
  1941. if not assigned(result) then
  1942. begin
  1943. Message(parser_e_invalid_integer);
  1944. result:=texprvalue.create_int(1);
  1945. end;
  1946. preproc_consume(_INTCONST);
  1947. end
  1948. else if current_scanner.preproc_token = _CSTRING then
  1949. begin
  1950. result:=texprvalue.create_str(current_scanner.preproc_pattern);
  1951. preproc_consume(_CSTRING);
  1952. end
  1953. else if current_scanner.preproc_token = _REALNUMBER then
  1954. begin
  1955. result:=texprvalue.try_parse_real(current_scanner.preproc_pattern);
  1956. if not assigned(result) then
  1957. begin
  1958. Message(parser_e_error_in_real);
  1959. result:=texprvalue.create_real(1.0);
  1960. end;
  1961. preproc_consume(_REALNUMBER);
  1962. end
  1963. else
  1964. Message(scan_e_error_in_preproc_expr);
  1965. if not assigned(result) then
  1966. result:=texprvalue.create_error;
  1967. end;
  1968. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean): texprvalue;
  1969. var
  1970. hs1,hs2: texprvalue;
  1971. op: ttoken;
  1972. begin
  1973. if pred_level=highest_precedence then
  1974. result:=preproc_factor(eval)
  1975. else
  1976. result:=preproc_sub_expr(succ(pred_level),eval);
  1977. repeat
  1978. op:=current_scanner.preproc_token;
  1979. if (op in preproc_operators) and
  1980. (op in operator_levels[pred_level]) then
  1981. begin
  1982. hs1:=result;
  1983. preproc_consume(op);
  1984. if (op=_OP_OR) and hs1.isBoolean and hs1.asBool then
  1985. begin
  1986. { stop evaluation the rest of expression }
  1987. result:=texprvalue.create_bool(true);
  1988. if pred_level=highest_precedence then
  1989. hs2:=preproc_factor(false)
  1990. else
  1991. hs2:=preproc_sub_expr(succ(pred_level),false);
  1992. end
  1993. else if (op=_OP_AND) and hs1.isBoolean and not hs1.asBool then
  1994. begin
  1995. { stop evaluation the rest of expression }
  1996. result:=texprvalue.create_bool(false);
  1997. if pred_level=highest_precedence then
  1998. hs2:=preproc_factor(false)
  1999. else
  2000. hs2:=preproc_sub_expr(succ(pred_level),false);
  2001. end
  2002. else
  2003. begin
  2004. if pred_level=highest_precedence then
  2005. hs2:=preproc_factor(eval)
  2006. else
  2007. hs2:=preproc_sub_expr(succ(pred_level),eval);
  2008. if eval then
  2009. result:=hs1.evaluate(hs2,op)
  2010. else
  2011. result:=texprvalue.create_bool(false); {Just to have something}
  2012. end;
  2013. hs1.free;
  2014. hs2.free;
  2015. end
  2016. else
  2017. break;
  2018. until false;
  2019. end;
  2020. begin
  2021. current_scanner.in_preproc_comp_expr:=true;
  2022. current_scanner.skipspace;
  2023. { start preproc expression scanner }
  2024. current_scanner.preproc_token:=current_scanner.readpreproc;
  2025. preproc_comp_expr:=preproc_sub_expr(opcompare,true);
  2026. current_scanner.in_preproc_comp_expr:=false;
  2027. end;
  2028. function boolean_compile_time_expr(var valuedescr: string): Boolean;
  2029. var
  2030. hs: texprvalue;
  2031. begin
  2032. hs:=preproc_comp_expr;
  2033. if hs.isBoolean then
  2034. result:=hs.asBool
  2035. else
  2036. begin
  2037. hs.error('Boolean', 'IF or ELSEIF');
  2038. result:=false;
  2039. end;
  2040. valuedescr:=hs.asStr;
  2041. hs.free;
  2042. end;
  2043. procedure dir_if;
  2044. begin
  2045. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  2046. end;
  2047. procedure dir_elseif;
  2048. begin
  2049. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  2050. end;
  2051. procedure dir_define_impl(macstyle: boolean);
  2052. var
  2053. hs : string;
  2054. bracketcount : longint;
  2055. mac : tmacro;
  2056. macropos : longint;
  2057. macrobuffer : pmacrobuffer;
  2058. begin
  2059. current_scanner.skipspace;
  2060. hs:=current_scanner.readid;
  2061. mac:=tmacro(search_macro(hs));
  2062. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  2063. begin
  2064. mac:=tmacro.create(hs);
  2065. mac.defined:=true;
  2066. current_module.localmacrosymtable.insert(mac);
  2067. end
  2068. else
  2069. begin
  2070. mac.defined:=true;
  2071. mac.is_compiler_var:=false;
  2072. { delete old definition }
  2073. if assigned(mac.buftext) then
  2074. begin
  2075. freemem(mac.buftext,mac.buflen);
  2076. mac.buftext:=nil;
  2077. end;
  2078. end;
  2079. Message1(parser_c_macro_defined,mac.name);
  2080. mac.is_used:=true;
  2081. if (cs_support_macro in current_settings.moduleswitches) then
  2082. begin
  2083. current_scanner.skipspace;
  2084. if not macstyle then
  2085. begin
  2086. { may be a macro? }
  2087. if c <> ':' then
  2088. exit;
  2089. current_scanner.readchar;
  2090. if c <> '=' then
  2091. exit;
  2092. current_scanner.readchar;
  2093. current_scanner.skipspace;
  2094. end;
  2095. { key words are never substituted }
  2096. if is_keyword(hs) then
  2097. Message(scan_e_keyword_cant_be_a_macro);
  2098. new(macrobuffer);
  2099. macropos:=0;
  2100. { parse macro, brackets are counted so it's possible
  2101. to have a $ifdef etc. in the macro }
  2102. bracketcount:=0;
  2103. repeat
  2104. case c of
  2105. '}' :
  2106. if (bracketcount=0) then
  2107. break
  2108. else
  2109. dec(bracketcount);
  2110. '{' :
  2111. inc(bracketcount);
  2112. #10,#13 :
  2113. current_scanner.linebreak;
  2114. #26 :
  2115. current_scanner.end_of_file;
  2116. end;
  2117. macrobuffer^[macropos]:=c;
  2118. inc(macropos);
  2119. if macropos>=maxmacrolen then
  2120. Message(scan_f_macro_buffer_overflow);
  2121. current_scanner.readchar;
  2122. until false;
  2123. { free buffer of macro ?}
  2124. if assigned(mac.buftext) then
  2125. freemem(mac.buftext,mac.buflen);
  2126. { get new mem }
  2127. getmem(mac.buftext,macropos);
  2128. mac.buflen:=macropos;
  2129. { copy the text }
  2130. move(macrobuffer^,mac.buftext^,macropos);
  2131. dispose(macrobuffer);
  2132. end
  2133. else
  2134. begin
  2135. { check if there is an assignment, then we need to give a
  2136. warning }
  2137. current_scanner.skipspace;
  2138. if c=':' then
  2139. begin
  2140. current_scanner.readchar;
  2141. if c='=' then
  2142. Message(scan_w_macro_support_turned_off);
  2143. end;
  2144. end;
  2145. end;
  2146. procedure dir_define;
  2147. begin
  2148. dir_define_impl(false);
  2149. end;
  2150. procedure dir_definec;
  2151. begin
  2152. dir_define_impl(true);
  2153. end;
  2154. procedure dir_setc;
  2155. var
  2156. hs : string;
  2157. mac : tmacro;
  2158. exprvalue: texprvalue;
  2159. begin
  2160. current_scanner.skipspace;
  2161. hs:=current_scanner.readid;
  2162. mac:=tmacro(search_macro(hs));
  2163. if not assigned(mac) or
  2164. (mac.owner <> current_module.localmacrosymtable) then
  2165. begin
  2166. mac:=tmacro.create(hs);
  2167. mac.defined:=true;
  2168. mac.is_compiler_var:=true;
  2169. current_module.localmacrosymtable.insert(mac);
  2170. end
  2171. else
  2172. begin
  2173. mac.defined:=true;
  2174. mac.is_compiler_var:=true;
  2175. { delete old definition }
  2176. if assigned(mac.buftext) then
  2177. begin
  2178. freemem(mac.buftext,mac.buflen);
  2179. mac.buftext:=nil;
  2180. end;
  2181. end;
  2182. Message1(parser_c_macro_defined,mac.name);
  2183. mac.is_used:=true;
  2184. { key words are never substituted }
  2185. if is_keyword(hs) then
  2186. Message(scan_e_keyword_cant_be_a_macro);
  2187. { macro assignment can be both := and = }
  2188. current_scanner.skipspace;
  2189. if c=':' then
  2190. current_scanner.readchar;
  2191. if c='=' then
  2192. begin
  2193. current_scanner.readchar;
  2194. exprvalue:=preproc_comp_expr;
  2195. if not is_boolean(exprvalue.def) and
  2196. not is_integer(exprvalue.def) then
  2197. exprvalue.error('Boolean, Integer', 'SETC');
  2198. hs:=exprvalue.asStr;
  2199. if length(hs) <> 0 then
  2200. begin
  2201. {If we are absolutely shure it is boolean, translate
  2202. to TRUE/FALSE to increase possibility to do future type check}
  2203. if exprvalue.isBoolean then
  2204. begin
  2205. if exprvalue.asBool then
  2206. hs:='TRUE'
  2207. else
  2208. hs:='FALSE';
  2209. end;
  2210. Message2(parser_c_macro_set_to,mac.name,hs);
  2211. { free buffer of macro ?}
  2212. if assigned(mac.buftext) then
  2213. freemem(mac.buftext,mac.buflen);
  2214. { get new mem }
  2215. getmem(mac.buftext,length(hs));
  2216. mac.buflen:=length(hs);
  2217. { copy the text }
  2218. move(hs[1],mac.buftext^,mac.buflen);
  2219. end
  2220. else
  2221. Message(scan_e_preproc_syntax_error);
  2222. exprvalue.free;
  2223. end
  2224. else
  2225. Message(scan_e_preproc_syntax_error);
  2226. end;
  2227. procedure dir_undef;
  2228. var
  2229. hs : string;
  2230. mac : tmacro;
  2231. begin
  2232. current_scanner.skipspace;
  2233. hs:=current_scanner.readid;
  2234. mac:=tmacro(search_macro(hs));
  2235. if not assigned(mac) or
  2236. (mac.owner <> current_module.localmacrosymtable) then
  2237. begin
  2238. mac:=tmacro.create(hs);
  2239. mac.defined:=false;
  2240. current_module.localmacrosymtable.insert(mac);
  2241. end
  2242. else
  2243. begin
  2244. mac.defined:=false;
  2245. mac.is_compiler_var:=false;
  2246. { delete old definition }
  2247. if assigned(mac.buftext) then
  2248. begin
  2249. freemem(mac.buftext,mac.buflen);
  2250. mac.buftext:=nil;
  2251. end;
  2252. end;
  2253. Message1(parser_c_macro_undefined,mac.name);
  2254. mac.is_used:=true;
  2255. end;
  2256. procedure dir_include;
  2257. function findincludefile(const path,name:TCmdStr;var foundfile:TCmdStr):boolean;
  2258. var
  2259. found : boolean;
  2260. hpath : TCmdStr;
  2261. begin
  2262. (* look for the include file
  2263. If path was absolute and specified as part of {$I } then
  2264. 1. specified path
  2265. else
  2266. 1. path of current inputfile,current dir
  2267. 2. local includepath
  2268. 3. global includepath
  2269. -- Check mantis #13461 before changing this *)
  2270. found:=false;
  2271. foundfile:='';
  2272. hpath:='';
  2273. if path_absolute(path) then
  2274. begin
  2275. found:=FindFile(name,path,true,foundfile);
  2276. end
  2277. else
  2278. begin
  2279. hpath:=current_scanner.inputfile.path+';'+CurDirRelPath(source_info);
  2280. found:=FindFile(path+name, hpath,true,foundfile);
  2281. if not found then
  2282. found:=current_module.localincludesearchpath.FindFile(path+name,true,foundfile);
  2283. if not found then
  2284. found:=includesearchpath.FindFile(path+name,true,foundfile);
  2285. end;
  2286. result:=found;
  2287. end;
  2288. var
  2289. foundfile : TCmdStr;
  2290. path,
  2291. name,
  2292. hs : tpathstr;
  2293. args : string;
  2294. hp : tinputfile;
  2295. found : boolean;
  2296. macroIsString : boolean;
  2297. begin
  2298. current_scanner.skipspace;
  2299. args:=current_scanner.readcomment;
  2300. hs:=GetToken(args,' ');
  2301. if hs='' then
  2302. exit;
  2303. if (hs[1]='%') then
  2304. begin
  2305. { case insensitive }
  2306. hs:=upper(hs);
  2307. { remove %'s }
  2308. Delete(hs,1,1);
  2309. if hs[length(hs)]='%' then
  2310. Delete(hs,length(hs),1);
  2311. { save old }
  2312. path:=hs;
  2313. { first check for internal macros }
  2314. macroIsString:=true;
  2315. case hs of
  2316. 'TIME':
  2317. if timestr<>'' then
  2318. hs:=timestr
  2319. else
  2320. hs:=gettimestr;
  2321. 'DATE':
  2322. if datestr<>'' then
  2323. hs:=datestr
  2324. else
  2325. hs:=getdatestr;
  2326. 'DATEYEAR':
  2327. begin
  2328. hs:=tostr(startsystime.Year);
  2329. macroIsString:=false;
  2330. end;
  2331. 'DATEMONTH':
  2332. begin
  2333. hs:=tostr(startsystime.Month);
  2334. macroIsString:=false;
  2335. end;
  2336. 'DATEDAY':
  2337. begin
  2338. hs:=tostr(startsystime.Day);
  2339. macroIsString:=false;
  2340. end;
  2341. 'TIMEHOUR':
  2342. begin
  2343. hs:=tostr(startsystime.Hour);
  2344. macroIsString:=false;
  2345. end;
  2346. 'TIMEMINUTE':
  2347. begin
  2348. hs:=tostr(startsystime.Minute);
  2349. macroIsString:=false;
  2350. end;
  2351. 'TIMESECOND':
  2352. begin
  2353. hs:=tostr(startsystime.Second);
  2354. macroIsString:=false;
  2355. end;
  2356. 'FILE':
  2357. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex);
  2358. 'LINE':
  2359. hs:=tostr(current_filepos.line);
  2360. 'LINENUM':
  2361. begin
  2362. hs:=tostr(current_filepos.line);
  2363. macroIsString:=false;
  2364. end;
  2365. 'FPCVERSION':
  2366. hs:=version_string;
  2367. 'FPCDATE':
  2368. hs:=date_string;
  2369. 'FPCTARGET':
  2370. hs:=target_cpu_string;
  2371. 'FPCTARGETCPU':
  2372. hs:=target_cpu_string;
  2373. 'FPCTARGETOS':
  2374. hs:=target_info.shortname;
  2375. 'CURRENTROUTINE':
  2376. hs:=current_procinfo.procdef.procsym.RealName;
  2377. else
  2378. hs:=GetEnvironmentVariable(hs);
  2379. end;
  2380. if hs='' then
  2381. Message1(scan_w_include_env_not_found,path);
  2382. { make it a stringconst }
  2383. if macroIsString then
  2384. hs:=''''+hs+'''';
  2385. current_scanner.substitutemacro(path,@hs[1],length(hs),
  2386. current_scanner.line_no,current_scanner.inputfile.ref_index);
  2387. end
  2388. else
  2389. begin
  2390. hs:=FixFileName(hs);
  2391. path:=ExtractFilePath(hs);
  2392. name:=ExtractFileName(hs);
  2393. { Special case for Delphi compatibility: '*' has to be replaced
  2394. by the file name of the current source file. }
  2395. if (length(name)>=1) and
  2396. (name[1]='*') then
  2397. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  2398. { try to find the file }
  2399. found:=findincludefile(path,name,foundfile);
  2400. if (not found) and (ExtractFileExt(name)='') then
  2401. begin
  2402. { try default extensions .inc , .pp and .pas }
  2403. if (not found) then
  2404. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  2405. if (not found) then
  2406. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  2407. if (not found) then
  2408. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  2409. end;
  2410. { if the name ends in dot, try without the dot }
  2411. if (not found) and (ExtractFileExt(name)=ExtensionSeparator) and (Length(name)>=2) then
  2412. found:=findincludefile(path,Copy(name,1,Length(name)-1),foundfile);
  2413. if current_scanner.inputfilecount<max_include_nesting then
  2414. begin
  2415. inc(current_scanner.inputfilecount);
  2416. { we need to reread the current char }
  2417. dec(current_scanner.inputpointer);
  2418. { reset c }
  2419. c:=#0;
  2420. { shutdown current file }
  2421. current_scanner.tempcloseinputfile;
  2422. { load new file }
  2423. hp:=do_openinputfile(foundfile);
  2424. hp.inc_path:=path;
  2425. current_scanner.addfile(hp);
  2426. current_module.sourcefiles.register_file(hp);
  2427. if (not found) then
  2428. Message1(scan_f_cannot_open_includefile,hs);
  2429. if (not current_scanner.openinputfile) then
  2430. Message1(scan_f_cannot_open_includefile,hs);
  2431. Message1(scan_t_start_include_file,current_scanner.inputfile.path+current_scanner.inputfile.name);
  2432. current_scanner.reload;
  2433. end
  2434. else
  2435. Message(scan_f_include_deep_ten);
  2436. end;
  2437. end;
  2438. {*****************************************************************************
  2439. Preprocessor writing
  2440. *****************************************************************************}
  2441. {$ifdef PREPROCWRITE}
  2442. constructor tpreprocfile.create(const fn:string);
  2443. begin
  2444. inherited create;
  2445. { open outputfile }
  2446. assign(f,fn);
  2447. {$push}{$I-}
  2448. rewrite(f);
  2449. {$pop}
  2450. if ioresult<>0 then
  2451. Comment(V_Fatal,'can''t create file '+fn);
  2452. getmem(buf,preprocbufsize);
  2453. settextbuf(f,buf^,preprocbufsize);
  2454. { reset }
  2455. eolfound:=false;
  2456. spacefound:=false;
  2457. end;
  2458. destructor tpreprocfile.destroy;
  2459. begin
  2460. close(f);
  2461. freemem(buf,preprocbufsize);
  2462. end;
  2463. procedure tpreprocfile.add(const s:string);
  2464. begin
  2465. write(f,s);
  2466. end;
  2467. procedure tpreprocfile.addspace;
  2468. begin
  2469. if eolfound then
  2470. begin
  2471. writeln(f,'');
  2472. eolfound:=false;
  2473. spacefound:=false;
  2474. end
  2475. else
  2476. if spacefound then
  2477. begin
  2478. write(f,' ');
  2479. spacefound:=false;
  2480. end;
  2481. end;
  2482. {$endif PREPROCWRITE}
  2483. {*****************************************************************************
  2484. TPreProcStack
  2485. *****************************************************************************}
  2486. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  2487. begin
  2488. accept:=a;
  2489. typ:=atyp;
  2490. next:=n;
  2491. end;
  2492. {*****************************************************************************
  2493. TReplayStack
  2494. *****************************************************************************}
  2495. constructor treplaystack.Create(atoken:ttoken;aidtoken:ttoken;
  2496. const aorgpattern,apattern:string;const acstringpattern:ansistring;
  2497. apatternw:pcompilerwidestring;asettings:tsettings;
  2498. atokenbuf:tdynamicarray;change_endian:boolean;anext:treplaystack);
  2499. begin
  2500. token:=atoken;
  2501. idtoken:=aidtoken;
  2502. orgpattern:=aorgpattern;
  2503. pattern:=apattern;
  2504. cstringpattern:=acstringpattern;
  2505. initwidestring(patternw);
  2506. if assigned(apatternw) then
  2507. begin
  2508. setlengthwidestring(patternw,apatternw^.len);
  2509. move(apatternw^.data^,patternw^.data^,apatternw^.len*sizeof(tcompilerwidechar));
  2510. end;
  2511. settings:=asettings;
  2512. tokenbuf:=atokenbuf;
  2513. tokenbuf_needs_swapping:=change_endian;
  2514. next:=anext;
  2515. end;
  2516. destructor treplaystack.destroy;
  2517. begin
  2518. donewidestring(patternw);
  2519. end;
  2520. {*****************************************************************************
  2521. TDirectiveItem
  2522. *****************************************************************************}
  2523. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2524. begin
  2525. inherited Create(AList,n);
  2526. is_conditional:=false;
  2527. proc:=p;
  2528. end;
  2529. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2530. begin
  2531. inherited Create(AList,n);
  2532. is_conditional:=true;
  2533. proc:=p;
  2534. end;
  2535. {****************************************************************************
  2536. TSCANNERFILE
  2537. ****************************************************************************}
  2538. constructor tscannerfile.create(const fn:string; is_macro: boolean = false);
  2539. begin
  2540. inputfile:=do_openinputfile(fn);
  2541. if is_macro then
  2542. inputfile.is_macro:=true;
  2543. if assigned(current_module) then
  2544. current_module.sourcefiles.register_file(inputfile);
  2545. { reset localinput }
  2546. c:=#0;
  2547. inputbuffer:=nil;
  2548. inputpointer:=nil;
  2549. inputstart:=0;
  2550. { reset scanner }
  2551. preprocstack:=nil;
  2552. replaystack:=nil;
  2553. comment_level:=0;
  2554. yylexcount:=0;
  2555. block_type:=bt_general;
  2556. line_no:=0;
  2557. lastlinepos:=0;
  2558. lasttokenpos:=0;
  2559. nexttokenpos:=0;
  2560. lasttoken:=NOTOKEN;
  2561. nexttoken:=NOTOKEN;
  2562. ignoredirectives:=TFPHashList.Create;
  2563. change_endian_for_replay:=false;
  2564. end;
  2565. procedure tscannerfile.firstfile;
  2566. begin
  2567. { load block }
  2568. if not openinputfile then
  2569. Message1(scan_f_cannot_open_input,inputfile.name);
  2570. reload;
  2571. end;
  2572. destructor tscannerfile.destroy;
  2573. begin
  2574. if assigned(current_module) and
  2575. (current_module.state=ms_compiled) and
  2576. (status.errorcount=0) then
  2577. checkpreprocstack
  2578. else
  2579. begin
  2580. while assigned(preprocstack) do
  2581. poppreprocstack;
  2582. end;
  2583. while assigned(replaystack) do
  2584. popreplaystack;
  2585. if not inputfile.closed then
  2586. closeinputfile;
  2587. if inputfile.is_macro then
  2588. inputfile.free;
  2589. ignoredirectives.free;
  2590. end;
  2591. function tscannerfile.openinputfile:boolean;
  2592. begin
  2593. openinputfile:=inputfile.open;
  2594. { load buffer }
  2595. inputbuffer:=inputfile.buf;
  2596. inputpointer:=inputfile.buf;
  2597. inputstart:=inputfile.bufstart;
  2598. { line }
  2599. line_no:=0;
  2600. lastlinepos:=0;
  2601. lasttokenpos:=0;
  2602. nexttokenpos:=0;
  2603. end;
  2604. procedure tscannerfile.closeinputfile;
  2605. begin
  2606. inputfile.close;
  2607. { reset buffer }
  2608. inputbuffer:=nil;
  2609. inputpointer:=nil;
  2610. inputstart:=0;
  2611. { reset line }
  2612. line_no:=0;
  2613. lastlinepos:=0;
  2614. lasttokenpos:=0;
  2615. nexttokenpos:=0;
  2616. end;
  2617. function tscannerfile.tempopeninputfile:boolean;
  2618. begin
  2619. tempopeninputfile:=false;
  2620. if inputfile.is_macro then
  2621. exit;
  2622. tempopeninputfile:=inputfile.tempopen;
  2623. { reload buffer }
  2624. inputbuffer:=inputfile.buf;
  2625. inputpointer:=inputfile.buf;
  2626. inputstart:=inputfile.bufstart;
  2627. end;
  2628. procedure tscannerfile.tempcloseinputfile;
  2629. begin
  2630. if inputfile.closed or inputfile.is_macro then
  2631. exit;
  2632. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  2633. inputfile.tempclose;
  2634. { reset buffer }
  2635. inputbuffer:=nil;
  2636. inputpointer:=nil;
  2637. inputstart:=0;
  2638. end;
  2639. procedure tscannerfile.saveinputfile;
  2640. begin
  2641. inputfile.saveinputpointer:=inputpointer;
  2642. inputfile.savelastlinepos:=lastlinepos;
  2643. inputfile.saveline_no:=line_no;
  2644. end;
  2645. procedure tscannerfile.restoreinputfile;
  2646. begin
  2647. inputbuffer:=inputfile.buf;
  2648. inputpointer:=inputfile.saveinputpointer;
  2649. lastlinepos:=inputfile.savelastlinepos;
  2650. line_no:=inputfile.saveline_no;
  2651. if not inputfile.is_macro then
  2652. parser_current_file:=inputfile.name;
  2653. end;
  2654. procedure tscannerfile.nextfile;
  2655. var
  2656. to_dispose : tinputfile;
  2657. begin
  2658. if assigned(inputfile.next) then
  2659. begin
  2660. if inputfile.is_macro then
  2661. to_dispose:=inputfile
  2662. else
  2663. begin
  2664. to_dispose:=nil;
  2665. dec(inputfilecount);
  2666. end;
  2667. { we can allways close the file, no ? }
  2668. inputfile.close;
  2669. inputfile:=inputfile.next;
  2670. if assigned(to_dispose) then
  2671. to_dispose.free;
  2672. restoreinputfile;
  2673. end;
  2674. end;
  2675. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  2676. begin
  2677. if not assigned(buf) then
  2678. internalerror(200511172);
  2679. if assigned(recordtokenbuf) then
  2680. internalerror(200511173);
  2681. recordtokenbuf:=buf;
  2682. fillchar(last_settings,sizeof(last_settings),0);
  2683. last_message:=nil;
  2684. fillchar(last_filepos,sizeof(last_filepos),0);
  2685. end;
  2686. procedure tscannerfile.stoprecordtokens;
  2687. begin
  2688. if not assigned(recordtokenbuf) then
  2689. internalerror(200511174);
  2690. recordtokenbuf:=nil;
  2691. end;
  2692. function tscannerfile.is_recording_tokens: boolean;
  2693. begin
  2694. result:=assigned(recordtokenbuf);
  2695. end;
  2696. procedure tscannerfile.writetoken(t : ttoken);
  2697. var
  2698. b : byte;
  2699. begin
  2700. if ord(t)>$7f then
  2701. begin
  2702. b:=(ord(t) shr 8) or $80;
  2703. recordtokenbuf.write(b,1);
  2704. end;
  2705. b:=ord(t) and $ff;
  2706. recordtokenbuf.write(b,1);
  2707. end;
  2708. procedure tscannerfile.tokenwritesizeint(val : asizeint);
  2709. begin
  2710. recordtokenbuf.write(val,sizeof(asizeint));
  2711. end;
  2712. procedure tscannerfile.tokenwritelongint(val : longint);
  2713. begin
  2714. recordtokenbuf.write(val,sizeof(longint));
  2715. end;
  2716. procedure tscannerfile.tokenwriteshortint(val : shortint);
  2717. begin
  2718. recordtokenbuf.write(val,sizeof(shortint));
  2719. end;
  2720. procedure tscannerfile.tokenwriteword(val : word);
  2721. begin
  2722. recordtokenbuf.write(val,sizeof(word));
  2723. end;
  2724. procedure tscannerfile.tokenwritelongword(val : longword);
  2725. begin
  2726. recordtokenbuf.write(val,sizeof(longword));
  2727. end;
  2728. function tscannerfile.tokenreadsizeint : asizeint;
  2729. var
  2730. val : asizeint;
  2731. begin
  2732. replaytokenbuf.read(val,sizeof(asizeint));
  2733. if change_endian_for_replay then
  2734. val:=swapendian(val);
  2735. result:=val;
  2736. end;
  2737. function tscannerfile.tokenreadlongword : longword;
  2738. var
  2739. val : longword;
  2740. begin
  2741. replaytokenbuf.read(val,sizeof(longword));
  2742. if change_endian_for_replay then
  2743. val:=swapendian(val);
  2744. result:=val;
  2745. end;
  2746. function tscannerfile.tokenreadlongint : longint;
  2747. var
  2748. val : longint;
  2749. begin
  2750. replaytokenbuf.read(val,sizeof(longint));
  2751. if change_endian_for_replay then
  2752. val:=swapendian(val);
  2753. result:=val;
  2754. end;
  2755. function tscannerfile.tokenreadshortint : shortint;
  2756. var
  2757. val : shortint;
  2758. begin
  2759. replaytokenbuf.read(val,sizeof(shortint));
  2760. result:=val;
  2761. end;
  2762. function tscannerfile.tokenreadbyte : byte;
  2763. var
  2764. val : byte;
  2765. begin
  2766. replaytokenbuf.read(val,sizeof(byte));
  2767. result:=val;
  2768. end;
  2769. function tscannerfile.tokenreadsmallint : smallint;
  2770. var
  2771. val : smallint;
  2772. begin
  2773. replaytokenbuf.read(val,sizeof(smallint));
  2774. if change_endian_for_replay then
  2775. val:=swapendian(val);
  2776. result:=val;
  2777. end;
  2778. function tscannerfile.tokenreadword : word;
  2779. var
  2780. val : word;
  2781. begin
  2782. replaytokenbuf.read(val,sizeof(word));
  2783. if change_endian_for_replay then
  2784. val:=swapendian(val);
  2785. result:=val;
  2786. end;
  2787. function tscannerfile.tokenreadenum(size : longint) : longword;
  2788. begin
  2789. if size=1 then
  2790. result:=tokenreadbyte
  2791. else if size=2 then
  2792. result:=tokenreadword
  2793. else if size=4 then
  2794. result:=tokenreadlongword
  2795. else
  2796. internalerror(2013112901);
  2797. end;
  2798. procedure tscannerfile.tokenreadset(var b;size : longint);
  2799. var
  2800. i : longint;
  2801. begin
  2802. replaytokenbuf.read(b,size);
  2803. if change_endian_for_replay then
  2804. for i:=0 to size-1 do
  2805. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  2806. end;
  2807. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  2808. begin
  2809. recordtokenbuf.write(b,size);
  2810. end;
  2811. procedure tscannerfile.tokenwriteset(var b;size : longint);
  2812. begin
  2813. recordtokenbuf.write(b,size);
  2814. end;
  2815. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  2816. { This procedure
  2817. needs to be changed whenever
  2818. globals.tsettings type is changed,
  2819. the problem is that no error will appear
  2820. before tests with generics are tested. PM }
  2821. var
  2822. startpos, endpos : longword;
  2823. begin
  2824. { WARNING all those fields need to be in the correct
  2825. order otherwise cross_endian PPU reading will fail }
  2826. startpos:=replaytokenbuf.pos;
  2827. with asettings do
  2828. begin
  2829. alignment.procalign:=tokenreadlongint;
  2830. alignment.loopalign:=tokenreadlongint;
  2831. alignment.jumpalign:=tokenreadlongint;
  2832. alignment.constalignmin:=tokenreadlongint;
  2833. alignment.constalignmax:=tokenreadlongint;
  2834. alignment.varalignmin:=tokenreadlongint;
  2835. alignment.varalignmax:=tokenreadlongint;
  2836. alignment.localalignmin:=tokenreadlongint;
  2837. alignment.localalignmax:=tokenreadlongint;
  2838. alignment.recordalignmin:=tokenreadlongint;
  2839. alignment.recordalignmax:=tokenreadlongint;
  2840. alignment.maxCrecordalign:=tokenreadlongint;
  2841. tokenreadset(globalswitches,sizeof(globalswitches));
  2842. tokenreadset(targetswitches,sizeof(targetswitches));
  2843. tokenreadset(moduleswitches,sizeof(moduleswitches));
  2844. tokenreadset(localswitches,sizeof(localswitches));
  2845. tokenreadset(modeswitches,sizeof(modeswitches));
  2846. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  2847. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2848. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2849. tokenreadset(debugswitches,sizeof(debugswitches));
  2850. { 0: old behaviour for sets <=256 elements
  2851. >0: round to this size }
  2852. setalloc:=tokenreadshortint;
  2853. packenum:=tokenreadshortint;
  2854. packrecords:=tokenreadshortint;
  2855. maxfpuregisters:=tokenreadshortint;
  2856. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2857. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2858. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  2859. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  2860. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  2861. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  2862. { tstringencoding is word type,
  2863. thus this should be OK here }
  2864. sourcecodepage:=tstringEncoding(tokenreadword);
  2865. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  2866. disabledircache:=boolean(tokenreadbyte);
  2867. { TH: Since the field was conditional originally, it was not stored in PPUs. }
  2868. { While adding ControllerSupport constant, I decided not to store ct_none }
  2869. { on targets not supporting controllers, but this might be changed here and }
  2870. { in tokenwritesettings in the future to unify the PPU structure and handling }
  2871. { of this field in the compiler. }
  2872. {$PUSH}
  2873. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  2874. if ControllerSupport then
  2875. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)))
  2876. else
  2877. ControllerType:=ct_none;
  2878. {$POP}
  2879. endpos:=replaytokenbuf.pos;
  2880. if endpos-startpos<>expected_size then
  2881. Comment(V_Error,'Wrong size of Settings read-in');
  2882. end;
  2883. end;
  2884. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  2885. { This procedure
  2886. needs to be changed whenever
  2887. globals.tsettings type is changed,
  2888. the problem is that no error will appear
  2889. before tests with generics are tested. PM }
  2890. var
  2891. sizepos, startpos, endpos : longword;
  2892. begin
  2893. { WARNING all those fields need to be in the correct
  2894. order otherwise cross_endian PPU reading will fail }
  2895. sizepos:=recordtokenbuf.pos;
  2896. size:=0;
  2897. tokenwritesizeint(size);
  2898. startpos:=recordtokenbuf.pos;
  2899. with asettings do
  2900. begin
  2901. tokenwritelongint(alignment.procalign);
  2902. tokenwritelongint(alignment.loopalign);
  2903. tokenwritelongint(alignment.jumpalign);
  2904. tokenwritelongint(alignment.constalignmin);
  2905. tokenwritelongint(alignment.constalignmax);
  2906. tokenwritelongint(alignment.varalignmin);
  2907. tokenwritelongint(alignment.varalignmax);
  2908. tokenwritelongint(alignment.localalignmin);
  2909. tokenwritelongint(alignment.localalignmax);
  2910. tokenwritelongint(alignment.recordalignmin);
  2911. tokenwritelongint(alignment.recordalignmax);
  2912. tokenwritelongint(alignment.maxCrecordalign);
  2913. tokenwriteset(globalswitches,sizeof(globalswitches));
  2914. tokenwriteset(targetswitches,sizeof(targetswitches));
  2915. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  2916. tokenwriteset(localswitches,sizeof(localswitches));
  2917. tokenwriteset(modeswitches,sizeof(modeswitches));
  2918. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  2919. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2920. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2921. tokenwriteset(debugswitches,sizeof(debugswitches));
  2922. { 0: old behaviour for sets <=256 elements
  2923. >0: round to this size }
  2924. tokenwriteshortint(setalloc);
  2925. tokenwriteshortint(packenum);
  2926. tokenwriteshortint(packrecords);
  2927. tokenwriteshortint(maxfpuregisters);
  2928. tokenwriteenum(cputype,sizeof(tcputype));
  2929. tokenwriteenum(optimizecputype,sizeof(tcputype));
  2930. tokenwriteenum(fputype,sizeof(tfputype));
  2931. tokenwriteenum(asmmode,sizeof(tasmmode));
  2932. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  2933. tokenwriteenum(defproccall,sizeof(tproccalloption));
  2934. { tstringencoding is word type,
  2935. thus this should be OK here }
  2936. tokenwriteword(sourcecodepage);
  2937. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  2938. recordtokenbuf.write(byte(disabledircache),1);
  2939. { TH: See note about controllertype field in tokenreadsettings. }
  2940. {$PUSH}
  2941. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  2942. if ControllerSupport then
  2943. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  2944. {$POP}
  2945. endpos:=recordtokenbuf.pos;
  2946. size:=endpos-startpos;
  2947. recordtokenbuf.seek(sizepos);
  2948. tokenwritesizeint(size);
  2949. recordtokenbuf.seek(endpos);
  2950. end;
  2951. end;
  2952. procedure tscannerfile.recordtoken;
  2953. var
  2954. t : ttoken;
  2955. s : tspecialgenerictoken;
  2956. len,msgnb,copy_size : asizeint;
  2957. val : longint;
  2958. b : byte;
  2959. pmsg : pmessagestaterecord;
  2960. begin
  2961. if not assigned(recordtokenbuf) then
  2962. internalerror(200511176);
  2963. t:=_GENERICSPECIALTOKEN;
  2964. { settings changed? }
  2965. { last field pmessage is handled separately below in
  2966. ST_LOADMESSAGES }
  2967. if CompareByte(current_settings,last_settings,
  2968. sizeof(current_settings)-sizeof(pointer))<>0 then
  2969. begin
  2970. { use a special token to record it }
  2971. s:=ST_LOADSETTINGS;
  2972. writetoken(t);
  2973. recordtokenbuf.write(s,1);
  2974. copy_size:=sizeof(current_settings)-sizeof(pointer);
  2975. tokenwritesettings(current_settings,copy_size);
  2976. last_settings:=current_settings;
  2977. end;
  2978. if current_settings.pmessage<>last_message then
  2979. begin
  2980. { use a special token to record it }
  2981. s:=ST_LOADMESSAGES;
  2982. writetoken(t);
  2983. recordtokenbuf.write(s,1);
  2984. msgnb:=0;
  2985. pmsg:=current_settings.pmessage;
  2986. while assigned(pmsg) do
  2987. begin
  2988. if msgnb=high(asizeint) then
  2989. { Too many messages }
  2990. internalerror(2011090401);
  2991. inc(msgnb);
  2992. pmsg:=pmsg^.next;
  2993. end;
  2994. tokenwritesizeint(msgnb);
  2995. pmsg:=current_settings.pmessage;
  2996. while assigned(pmsg) do
  2997. begin
  2998. { What about endianess here?}
  2999. { SB: this is handled by tokenreadlongint }
  3000. val:=pmsg^.value;
  3001. tokenwritelongint(val);
  3002. val:=ord(pmsg^.state);
  3003. tokenwritelongint(val);
  3004. pmsg:=pmsg^.next;
  3005. end;
  3006. last_message:=current_settings.pmessage;
  3007. end;
  3008. { file pos changes? }
  3009. if current_tokenpos.line<>last_filepos.line then
  3010. begin
  3011. s:=ST_LINE;
  3012. writetoken(t);
  3013. recordtokenbuf.write(s,1);
  3014. tokenwritelongint(current_tokenpos.line);
  3015. last_filepos.line:=current_tokenpos.line;
  3016. end;
  3017. if current_tokenpos.column<>last_filepos.column then
  3018. begin
  3019. s:=ST_COLUMN;
  3020. writetoken(t);
  3021. { can the column be written packed? }
  3022. if current_tokenpos.column<$80 then
  3023. begin
  3024. b:=$80 or current_tokenpos.column;
  3025. recordtokenbuf.write(b,1);
  3026. end
  3027. else
  3028. begin
  3029. recordtokenbuf.write(s,1);
  3030. tokenwriteword(current_tokenpos.column);
  3031. end;
  3032. last_filepos.column:=current_tokenpos.column;
  3033. end;
  3034. if current_tokenpos.fileindex<>last_filepos.fileindex then
  3035. begin
  3036. s:=ST_FILEINDEX;
  3037. writetoken(t);
  3038. recordtokenbuf.write(s,1);
  3039. tokenwriteword(current_tokenpos.fileindex);
  3040. last_filepos.fileindex:=current_tokenpos.fileindex;
  3041. end;
  3042. writetoken(token);
  3043. if token<>_GENERICSPECIALTOKEN then
  3044. writetoken(idtoken);
  3045. case token of
  3046. _CWCHAR,
  3047. _CWSTRING :
  3048. begin
  3049. tokenwritesizeint(patternw^.len);
  3050. if patternw^.len>0 then
  3051. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3052. end;
  3053. _CSTRING:
  3054. begin
  3055. len:=length(cstringpattern);
  3056. tokenwritesizeint(len);
  3057. if len>0 then
  3058. recordtokenbuf.write(cstringpattern[1],len);
  3059. end;
  3060. _CCHAR,
  3061. _INTCONST,
  3062. _REALNUMBER :
  3063. begin
  3064. { pexpr.pas messes with pattern in case of negative integer consts,
  3065. see around line 2562 the comment of JM; remove the - before recording it
  3066. (FK)
  3067. }
  3068. if (token=_INTCONST) and (pattern[1]='-') then
  3069. delete(pattern,1,1);
  3070. recordtokenbuf.write(pattern[0],1);
  3071. recordtokenbuf.write(pattern[1],length(pattern));
  3072. end;
  3073. _ID :
  3074. begin
  3075. recordtokenbuf.write(orgpattern[0],1);
  3076. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  3077. end;
  3078. end;
  3079. end;
  3080. procedure tscannerfile.startreplaytokens(buf:tdynamicarray; change_endian:boolean);
  3081. begin
  3082. if not assigned(buf) then
  3083. internalerror(200511175);
  3084. { save current scanner state }
  3085. replaystack:=treplaystack.create(token,idtoken,orgpattern,pattern,
  3086. cstringpattern,patternw,current_settings,replaytokenbuf,change_endian_for_replay,replaystack);
  3087. if assigned(inputpointer) then
  3088. dec(inputpointer);
  3089. { install buffer }
  3090. replaytokenbuf:=buf;
  3091. { Initialize value of change_endian_for_replay variable }
  3092. change_endian_for_replay:=change_endian;
  3093. { reload next token }
  3094. replaytokenbuf.seek(0);
  3095. replaytoken;
  3096. end;
  3097. function tscannerfile.readtoken: ttoken;
  3098. var
  3099. b,b2 : byte;
  3100. begin
  3101. replaytokenbuf.read(b,1);
  3102. if (b and $80)<>0 then
  3103. begin
  3104. replaytokenbuf.read(b2,1);
  3105. result:=ttoken(((b and $7f) shl 8) or b2);
  3106. end
  3107. else
  3108. result:=ttoken(b);
  3109. end;
  3110. procedure tscannerfile.replaytoken;
  3111. var
  3112. wlen,mesgnb,copy_size : asizeint;
  3113. specialtoken : tspecialgenerictoken;
  3114. i : byte;
  3115. pmsg,prevmsg : pmessagestaterecord;
  3116. begin
  3117. if not assigned(replaytokenbuf) then
  3118. internalerror(200511177);
  3119. { End of replay buffer? Then load the next char from the file again }
  3120. if replaytokenbuf.pos>=replaytokenbuf.size then
  3121. begin
  3122. token:=replaystack.token;
  3123. idtoken:=replaystack.idtoken;
  3124. pattern:=replaystack.pattern;
  3125. orgpattern:=replaystack.orgpattern;
  3126. setlengthwidestring(patternw,replaystack.patternw^.len);
  3127. move(replaystack.patternw^.data^,patternw^.data^,replaystack.patternw^.len*sizeof(tcompilerwidechar));
  3128. cstringpattern:=replaystack.cstringpattern;
  3129. replaytokenbuf:=replaystack.tokenbuf;
  3130. change_endian_for_replay:=replaystack.tokenbuf_needs_swapping;
  3131. { restore compiler settings }
  3132. current_settings:=replaystack.settings;
  3133. popreplaystack;
  3134. if assigned(inputpointer) then
  3135. begin
  3136. c:=inputpointer^;
  3137. inc(inputpointer);
  3138. end;
  3139. exit;
  3140. end;
  3141. repeat
  3142. { load token from the buffer }
  3143. token:=readtoken;
  3144. if token<>_GENERICSPECIALTOKEN then
  3145. idtoken:=readtoken
  3146. else
  3147. idtoken:=_NOID;
  3148. case token of
  3149. _CWCHAR,
  3150. _CWSTRING :
  3151. begin
  3152. wlen:=tokenreadsizeint;
  3153. setlengthwidestring(patternw,wlen);
  3154. if wlen>0 then
  3155. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3156. orgpattern:='';
  3157. pattern:='';
  3158. cstringpattern:='';
  3159. end;
  3160. _CSTRING:
  3161. begin
  3162. wlen:=tokenreadsizeint;
  3163. if wlen>0 then
  3164. begin
  3165. setlength(cstringpattern,wlen);
  3166. replaytokenbuf.read(cstringpattern[1],wlen);
  3167. end
  3168. else
  3169. cstringpattern:='';
  3170. orgpattern:='';
  3171. pattern:='';
  3172. end;
  3173. _CCHAR,
  3174. _INTCONST,
  3175. _REALNUMBER :
  3176. begin
  3177. replaytokenbuf.read(pattern[0],1);
  3178. replaytokenbuf.read(pattern[1],length(pattern));
  3179. orgpattern:='';
  3180. end;
  3181. _ID :
  3182. begin
  3183. replaytokenbuf.read(orgpattern[0],1);
  3184. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  3185. pattern:=upper(orgpattern);
  3186. end;
  3187. _GENERICSPECIALTOKEN:
  3188. begin
  3189. replaytokenbuf.read(specialtoken,1);
  3190. { packed column? }
  3191. if (ord(specialtoken) and $80)<>0 then
  3192. begin
  3193. current_tokenpos.column:=ord(specialtoken) and $7f;
  3194. current_filepos:=current_tokenpos;
  3195. end
  3196. else
  3197. case specialtoken of
  3198. ST_LOADSETTINGS:
  3199. begin
  3200. copy_size:=tokenreadsizeint;
  3201. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  3202. // internalerror(2011090501);
  3203. {
  3204. replaytokenbuf.read(current_settings,copy_size);
  3205. }
  3206. tokenreadsettings(current_settings,copy_size);
  3207. end;
  3208. ST_LOADMESSAGES:
  3209. begin
  3210. current_settings.pmessage:=nil;
  3211. mesgnb:=tokenreadsizeint;
  3212. prevmsg:=nil;
  3213. for i:=1 to mesgnb do
  3214. begin
  3215. new(pmsg);
  3216. if i=1 then
  3217. current_settings.pmessage:=pmsg
  3218. else
  3219. prevmsg^.next:=pmsg;
  3220. pmsg^.value:=tokenreadlongint;
  3221. pmsg^.state:=tmsgstate(tokenreadlongint);
  3222. pmsg^.next:=nil;
  3223. prevmsg:=pmsg;
  3224. end;
  3225. end;
  3226. ST_LINE:
  3227. begin
  3228. current_tokenpos.line:=tokenreadlongint;
  3229. current_filepos:=current_tokenpos;
  3230. end;
  3231. ST_COLUMN:
  3232. begin
  3233. current_tokenpos.column:=tokenreadword;
  3234. current_filepos:=current_tokenpos;
  3235. end;
  3236. ST_FILEINDEX:
  3237. begin
  3238. current_tokenpos.fileindex:=tokenreadword;
  3239. current_filepos:=current_tokenpos;
  3240. end;
  3241. else
  3242. internalerror(2006103010);
  3243. end;
  3244. continue;
  3245. end;
  3246. end;
  3247. break;
  3248. until false;
  3249. end;
  3250. procedure tscannerfile.addfile(hp:tinputfile);
  3251. begin
  3252. saveinputfile;
  3253. { add to list }
  3254. hp.next:=inputfile;
  3255. inputfile:=hp;
  3256. { load new inputfile }
  3257. restoreinputfile;
  3258. end;
  3259. procedure tscannerfile.reload;
  3260. begin
  3261. with inputfile do
  3262. begin
  3263. { when nothing more to read then leave immediatly, so we
  3264. don't change the current_filepos and leave it point to the last
  3265. char }
  3266. if (c=#26) and (not assigned(next)) then
  3267. exit;
  3268. repeat
  3269. { still more to read?, then change the #0 to a space so its seen
  3270. as a seperator, this can't be used for macro's which can change
  3271. the place of the #0 in the buffer with tempopen }
  3272. if (c=#0) and (bufsize>0) and
  3273. not(inputfile.is_macro) and
  3274. (inputpointer-inputbuffer<bufsize) then
  3275. begin
  3276. c:=' ';
  3277. inc(inputpointer);
  3278. exit;
  3279. end;
  3280. { can we read more from this file ? }
  3281. if (c<>#26) and (not endoffile) then
  3282. begin
  3283. readbuf;
  3284. inputpointer:=buf;
  3285. inputbuffer:=buf;
  3286. inputstart:=bufstart;
  3287. { first line? }
  3288. if line_no=0 then
  3289. begin
  3290. c:=inputpointer^;
  3291. { eat utf-8 signature? }
  3292. if (bufsize>2) and
  3293. (ord(inputpointer^)=$ef) and
  3294. (ord((inputpointer+1)^)=$bb) and
  3295. (ord((inputpointer+2)^)=$bf) then
  3296. begin
  3297. (* we don't support including files with an UTF-8 bom
  3298. inside another file that wasn't encoded as UTF-8
  3299. already (we don't support {$codepage xxx} switches in
  3300. the middle of a file either) *)
  3301. if (current_settings.sourcecodepage<>CP_UTF8) and
  3302. not current_module.in_global then
  3303. Message(scanner_f_illegal_utf8_bom);
  3304. inc(inputpointer,3);
  3305. message(scan_c_switching_to_utf8);
  3306. current_settings.sourcecodepage:=CP_UTF8;
  3307. exclude(current_settings.moduleswitches,cs_system_codepage);
  3308. include(current_settings.moduleswitches,cs_explicit_codepage);
  3309. end;
  3310. line_no:=1;
  3311. if cs_asm_source in current_settings.globalswitches then
  3312. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3313. end;
  3314. end
  3315. else
  3316. begin
  3317. { load eof position in tokenpos/current_filepos }
  3318. gettokenpos;
  3319. { close file }
  3320. closeinputfile;
  3321. { no next module, than EOF }
  3322. if not assigned(inputfile.next) then
  3323. begin
  3324. c:=#26;
  3325. exit;
  3326. end;
  3327. { load next file and reopen it }
  3328. nextfile;
  3329. tempopeninputfile;
  3330. { status }
  3331. Message1(scan_t_back_in,inputfile.name);
  3332. end;
  3333. { load next char }
  3334. c:=inputpointer^;
  3335. inc(inputpointer);
  3336. until c<>#0; { if also end, then reload again }
  3337. end;
  3338. end;
  3339. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  3340. var
  3341. hp : tinputfile;
  3342. begin
  3343. { save old postion }
  3344. dec(inputpointer);
  3345. tempcloseinputfile;
  3346. { create macro 'file' }
  3347. { use special name to dispose after !! }
  3348. hp:=do_openinputfile('_Macro_.'+macname);
  3349. addfile(hp);
  3350. with inputfile do
  3351. begin
  3352. setmacro(p,len);
  3353. { local buffer }
  3354. inputbuffer:=buf;
  3355. inputpointer:=buf;
  3356. inputstart:=bufstart;
  3357. ref_index:=fileindex;
  3358. end;
  3359. { reset line }
  3360. line_no:=line;
  3361. lastlinepos:=0;
  3362. lasttokenpos:=0;
  3363. nexttokenpos:=0;
  3364. { load new c }
  3365. c:=inputpointer^;
  3366. inc(inputpointer);
  3367. end;
  3368. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3369. begin
  3370. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3371. filepos.line:=line_no;
  3372. filepos.column:=tokenpos-lastlinepos;
  3373. filepos.fileindex:=inputfile.ref_index;
  3374. filepos.moduleindex:=current_module.unit_index;
  3375. end;
  3376. procedure tscannerfile.gettokenpos;
  3377. { load the values of tokenpos and lasttokenpos }
  3378. begin
  3379. do_gettokenpos(lasttokenpos,current_tokenpos);
  3380. current_filepos:=current_tokenpos;
  3381. end;
  3382. procedure tscannerfile.cachenexttokenpos;
  3383. begin
  3384. do_gettokenpos(nexttokenpos,next_filepos);
  3385. end;
  3386. procedure tscannerfile.setnexttoken;
  3387. begin
  3388. token:=nexttoken;
  3389. nexttoken:=NOTOKEN;
  3390. lasttokenpos:=nexttokenpos;
  3391. current_tokenpos:=next_filepos;
  3392. current_filepos:=current_tokenpos;
  3393. nexttokenpos:=0;
  3394. end;
  3395. procedure tscannerfile.savetokenpos;
  3396. begin
  3397. oldlasttokenpos:=lasttokenpos;
  3398. oldcurrent_filepos:=current_filepos;
  3399. oldcurrent_tokenpos:=current_tokenpos;
  3400. end;
  3401. procedure tscannerfile.restoretokenpos;
  3402. begin
  3403. lasttokenpos:=oldlasttokenpos;
  3404. current_filepos:=oldcurrent_filepos;
  3405. current_tokenpos:=oldcurrent_tokenpos;
  3406. end;
  3407. procedure tscannerfile.inc_comment_level;
  3408. begin
  3409. if (m_nested_comment in current_settings.modeswitches) then
  3410. inc(comment_level)
  3411. else
  3412. comment_level:=1;
  3413. if (comment_level>1) then
  3414. begin
  3415. savetokenpos;
  3416. gettokenpos; { update for warning }
  3417. Message1(scan_w_comment_level,tostr(comment_level));
  3418. restoretokenpos;
  3419. end;
  3420. end;
  3421. procedure tscannerfile.dec_comment_level;
  3422. begin
  3423. if (m_nested_comment in current_settings.modeswitches) then
  3424. dec(comment_level)
  3425. else
  3426. comment_level:=0;
  3427. end;
  3428. procedure tscannerfile.linebreak;
  3429. var
  3430. cur : char;
  3431. begin
  3432. with inputfile do
  3433. begin
  3434. if (byte(inputpointer^)=0) and not(endoffile) then
  3435. begin
  3436. cur:=c;
  3437. reload;
  3438. if byte(cur)+byte(c)<>23 then
  3439. dec(inputpointer);
  3440. end
  3441. else
  3442. begin
  3443. { Support all combination of #10 and #13 as line break }
  3444. if (byte(inputpointer^)+byte(c)=23) then
  3445. inc(inputpointer);
  3446. end;
  3447. { Always return #10 as line break }
  3448. c:=#10;
  3449. { increase line counters }
  3450. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3451. inc(line_no);
  3452. { update linebuffer }
  3453. if cs_asm_source in current_settings.globalswitches then
  3454. inputfile.setline(line_no,lastlinepos);
  3455. { update for status and call the show status routine,
  3456. but don't touch current_filepos ! }
  3457. savetokenpos;
  3458. gettokenpos; { update for v_status }
  3459. inc(status.compiledlines);
  3460. ShowStatus;
  3461. restoretokenpos;
  3462. end;
  3463. end;
  3464. procedure tscannerfile.illegal_char(c:char);
  3465. var
  3466. s : string;
  3467. begin
  3468. if c in [#32..#255] then
  3469. s:=''''+c+''''
  3470. else
  3471. s:='#'+tostr(ord(c));
  3472. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3473. end;
  3474. procedure tscannerfile.end_of_file;
  3475. begin
  3476. checkpreprocstack;
  3477. Message(scan_f_end_of_file);
  3478. end;
  3479. {-------------------------------------------
  3480. IF Conditional Handling
  3481. -------------------------------------------}
  3482. procedure tscannerfile.checkpreprocstack;
  3483. begin
  3484. { check for missing ifdefs }
  3485. while assigned(preprocstack) do
  3486. begin
  3487. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3488. current_module.sourcefiles.get_file_name(preprocstack.fileindex),
  3489. tostr(preprocstack.line_nb));
  3490. poppreprocstack;
  3491. end;
  3492. end;
  3493. procedure tscannerfile.poppreprocstack;
  3494. var
  3495. hp : tpreprocstack;
  3496. begin
  3497. if assigned(preprocstack) then
  3498. begin
  3499. Message1(scan_c_endif_found,preprocstack.name);
  3500. hp:=preprocstack.next;
  3501. preprocstack.free;
  3502. preprocstack:=hp;
  3503. end
  3504. else
  3505. Message(scan_e_endif_without_if);
  3506. end;
  3507. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  3508. var
  3509. condition: Boolean;
  3510. valuedescr: String;
  3511. begin
  3512. if (preprocstack=nil) or preprocstack.accept then
  3513. condition:=compile_time_predicate(valuedescr)
  3514. else
  3515. begin
  3516. condition:= false;
  3517. valuedescr:= '';
  3518. end;
  3519. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  3520. preprocstack.name:=valuedescr;
  3521. preprocstack.line_nb:=line_no;
  3522. preprocstack.fileindex:=current_filepos.fileindex;
  3523. if preprocstack.accept then
  3524. Message2(messid,preprocstack.name,'accepted')
  3525. else
  3526. Message2(messid,preprocstack.name,'rejected');
  3527. end;
  3528. procedure tscannerfile.elsepreprocstack;
  3529. begin
  3530. if assigned(preprocstack) and
  3531. (preprocstack.typ<>pp_else) then
  3532. begin
  3533. if (preprocstack.typ=pp_elseif) then
  3534. preprocstack.accept:=false
  3535. else
  3536. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  3537. preprocstack.accept:=not preprocstack.accept;
  3538. preprocstack.typ:=pp_else;
  3539. preprocstack.line_nb:=line_no;
  3540. preprocstack.fileindex:=current_filepos.fileindex;
  3541. if preprocstack.accept then
  3542. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3543. else
  3544. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3545. end
  3546. else
  3547. Message(scan_e_endif_without_if);
  3548. end;
  3549. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  3550. var
  3551. valuedescr: String;
  3552. begin
  3553. if assigned(preprocstack) and
  3554. (preprocstack.typ in [pp_if,pp_elseif]) then
  3555. begin
  3556. { when the branch is accepted we use pp_elseif so we know that
  3557. all the next branches need to be rejected. when this branch is still
  3558. not accepted then leave it at pp_if }
  3559. if (preprocstack.typ=pp_elseif) then
  3560. preprocstack.accept:=false
  3561. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  3562. begin
  3563. preprocstack.accept:=false;
  3564. preprocstack.typ:=pp_elseif;
  3565. end
  3566. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  3567. and compile_time_predicate(valuedescr) then
  3568. begin
  3569. preprocstack.name:=valuedescr;
  3570. preprocstack.accept:=true;
  3571. preprocstack.typ:=pp_elseif;
  3572. end;
  3573. preprocstack.line_nb:=line_no;
  3574. preprocstack.fileindex:=current_filepos.fileindex;
  3575. if preprocstack.accept then
  3576. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3577. else
  3578. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3579. end
  3580. else
  3581. Message(scan_e_endif_without_if);
  3582. end;
  3583. procedure tscannerfile.popreplaystack;
  3584. var
  3585. hp : treplaystack;
  3586. begin
  3587. if assigned(replaystack) then
  3588. begin
  3589. hp:=replaystack.next;
  3590. replaystack.free;
  3591. replaystack:=hp;
  3592. end;
  3593. end;
  3594. function tscannerfile.replay_stack_depth:longint;
  3595. var
  3596. tmp: treplaystack;
  3597. begin
  3598. result:=0;
  3599. tmp:=replaystack;
  3600. while assigned(tmp) do
  3601. begin
  3602. inc(result);
  3603. tmp:=tmp.next;
  3604. end;
  3605. end;
  3606. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  3607. begin
  3608. savetokenpos;
  3609. repeat
  3610. current_scanner.gettokenpos;
  3611. Message1(scan_d_handling_switch,'$'+p.name);
  3612. p.proc();
  3613. { accept the text ? }
  3614. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  3615. break
  3616. else
  3617. begin
  3618. current_scanner.gettokenpos;
  3619. Message(scan_c_skipping_until);
  3620. repeat
  3621. current_scanner.skipuntildirective;
  3622. if not (m_mac in current_settings.modeswitches) then
  3623. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  3624. else
  3625. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  3626. until assigned(p) and (p.is_conditional);
  3627. current_scanner.gettokenpos;
  3628. end;
  3629. until false;
  3630. restoretokenpos;
  3631. end;
  3632. procedure tscannerfile.handledirectives;
  3633. var
  3634. t : tdirectiveitem;
  3635. hs : string;
  3636. begin
  3637. gettokenpos;
  3638. readchar; {Remove the $}
  3639. hs:=readid;
  3640. { handle empty directive }
  3641. if hs='' then
  3642. begin
  3643. Message1(scan_w_illegal_switch,'$');
  3644. exit;
  3645. end;
  3646. {$ifdef PREPROCWRITE}
  3647. if parapreprocess then
  3648. begin
  3649. if not (m_mac in current_settings.modeswitches) then
  3650. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  3651. else
  3652. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  3653. if assigned(t) and not(t.is_conditional) then
  3654. begin
  3655. preprocfile.AddSpace;
  3656. preprocfile.Add('{$'+hs+current_scanner.readcomment+'}');
  3657. exit;
  3658. end;
  3659. end;
  3660. {$endif PREPROCWRITE}
  3661. { skip this directive? }
  3662. if (ignoredirectives.find(hs)<>nil) then
  3663. begin
  3664. if (comment_level>0) then
  3665. readcomment;
  3666. { we've read the whole comment }
  3667. current_commentstyle:=comment_none;
  3668. exit;
  3669. end;
  3670. { Check for compiler switches }
  3671. while (length(hs)=1) and (c in ['-','+']) do
  3672. begin
  3673. Message1(scan_d_handling_switch,'$'+hs+c);
  3674. HandleSwitch(hs[1],c);
  3675. current_scanner.readchar; {Remove + or -}
  3676. if c=',' then
  3677. begin
  3678. current_scanner.readchar; {Remove , }
  3679. { read next switch, support $v+,$+}
  3680. hs:=current_scanner.readid;
  3681. if (hs='') then
  3682. begin
  3683. if (c='$') and (m_fpc in current_settings.modeswitches) then
  3684. begin
  3685. current_scanner.readchar; { skip $ }
  3686. hs:=current_scanner.readid;
  3687. end;
  3688. if (hs='') then
  3689. Message1(scan_w_illegal_directive,'$'+c);
  3690. end;
  3691. end
  3692. else
  3693. hs:='';
  3694. end;
  3695. { directives may follow switches after a , }
  3696. if hs<>'' then
  3697. begin
  3698. if not (m_mac in current_settings.modeswitches) then
  3699. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  3700. else
  3701. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  3702. if assigned(t) then
  3703. begin
  3704. if t.is_conditional then
  3705. handleconditional(t)
  3706. else
  3707. begin
  3708. Message1(scan_d_handling_switch,'$'+hs);
  3709. t.proc();
  3710. end;
  3711. end
  3712. else
  3713. begin
  3714. current_scanner.ignoredirectives.Add(hs,nil);
  3715. Message1(scan_w_illegal_directive,'$'+hs);
  3716. end;
  3717. { conditionals already read the comment }
  3718. if (current_scanner.comment_level>0) then
  3719. current_scanner.readcomment;
  3720. { we've read the whole comment }
  3721. current_commentstyle:=comment_none;
  3722. end;
  3723. end;
  3724. procedure tscannerfile.readchar;
  3725. begin
  3726. c:=inputpointer^;
  3727. if c=#0 then
  3728. reload
  3729. else
  3730. inc(inputpointer);
  3731. end;
  3732. procedure tscannerfile.readstring;
  3733. var
  3734. i : longint;
  3735. err : boolean;
  3736. begin
  3737. err:=false;
  3738. i:=0;
  3739. repeat
  3740. case c of
  3741. '_',
  3742. '0'..'9',
  3743. 'A'..'Z' :
  3744. begin
  3745. if i<255 then
  3746. begin
  3747. inc(i);
  3748. orgpattern[i]:=c;
  3749. pattern[i]:=c;
  3750. end
  3751. else
  3752. begin
  3753. if not err then
  3754. begin
  3755. Message(scan_e_string_exceeds_255_chars);
  3756. err:=true;
  3757. end;
  3758. end;
  3759. c:=inputpointer^;
  3760. inc(inputpointer);
  3761. end;
  3762. 'a'..'z' :
  3763. begin
  3764. if i<255 then
  3765. begin
  3766. inc(i);
  3767. orgpattern[i]:=c;
  3768. pattern[i]:=chr(ord(c)-32)
  3769. end
  3770. else
  3771. begin
  3772. if not err then
  3773. begin
  3774. Message(scan_e_string_exceeds_255_chars);
  3775. err:=true;
  3776. end;
  3777. end;
  3778. c:=inputpointer^;
  3779. inc(inputpointer);
  3780. end;
  3781. #0 :
  3782. reload;
  3783. else
  3784. break;
  3785. end;
  3786. until false;
  3787. orgpattern[0]:=chr(i);
  3788. pattern[0]:=chr(i);
  3789. end;
  3790. procedure tscannerfile.readnumber;
  3791. var
  3792. base,
  3793. i : longint;
  3794. begin
  3795. case c of
  3796. '%' :
  3797. begin
  3798. readchar;
  3799. base:=2;
  3800. pattern[1]:='%';
  3801. i:=1;
  3802. end;
  3803. '&' :
  3804. begin
  3805. readchar;
  3806. base:=8;
  3807. pattern[1]:='&';
  3808. i:=1;
  3809. end;
  3810. '$' :
  3811. begin
  3812. readchar;
  3813. base:=16;
  3814. pattern[1]:='$';
  3815. i:=1;
  3816. end;
  3817. else
  3818. begin
  3819. base:=10;
  3820. i:=0;
  3821. end;
  3822. end;
  3823. while ((base>=10) and (c in ['0'..'9'])) or
  3824. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  3825. ((base=8) and (c in ['0'..'7'])) or
  3826. ((base=2) and (c in ['0'..'1'])) do
  3827. begin
  3828. if i<255 then
  3829. begin
  3830. inc(i);
  3831. pattern[i]:=c;
  3832. end;
  3833. readchar;
  3834. end;
  3835. pattern[0]:=chr(i);
  3836. end;
  3837. function tscannerfile.readid:string;
  3838. begin
  3839. readstring;
  3840. readid:=pattern;
  3841. end;
  3842. function tscannerfile.readval:longint;
  3843. var
  3844. l : longint;
  3845. w : integer;
  3846. begin
  3847. readnumber;
  3848. val(pattern,l,w);
  3849. readval:=l;
  3850. end;
  3851. function tscannerfile.readcomment:string;
  3852. var
  3853. i : longint;
  3854. begin
  3855. i:=0;
  3856. repeat
  3857. case c of
  3858. '{' :
  3859. begin
  3860. if current_commentstyle=comment_tp then
  3861. inc_comment_level;
  3862. end;
  3863. '}' :
  3864. begin
  3865. if current_commentstyle=comment_tp then
  3866. begin
  3867. readchar;
  3868. dec_comment_level;
  3869. if comment_level=0 then
  3870. break
  3871. else
  3872. continue;
  3873. end;
  3874. end;
  3875. '*' :
  3876. begin
  3877. if current_commentstyle=comment_oldtp then
  3878. begin
  3879. readchar;
  3880. if c=')' then
  3881. begin
  3882. readchar;
  3883. dec_comment_level;
  3884. break;
  3885. end
  3886. else
  3887. { Add both characters !!}
  3888. if (i<255) then
  3889. begin
  3890. inc(i);
  3891. readcomment[i]:='*';
  3892. if (i<255) then
  3893. begin
  3894. inc(i);
  3895. readcomment[i]:=c;
  3896. end;
  3897. end;
  3898. end
  3899. else
  3900. { Not old TP comment, so add...}
  3901. begin
  3902. if (i<255) then
  3903. begin
  3904. inc(i);
  3905. readcomment[i]:='*';
  3906. end;
  3907. end;
  3908. end;
  3909. #10,#13 :
  3910. linebreak;
  3911. #26 :
  3912. end_of_file;
  3913. else
  3914. begin
  3915. if (i<255) then
  3916. begin
  3917. inc(i);
  3918. readcomment[i]:=c;
  3919. end;
  3920. end;
  3921. end;
  3922. readchar;
  3923. until false;
  3924. readcomment[0]:=chr(i);
  3925. end;
  3926. function tscannerfile.readquotedstring:string;
  3927. var
  3928. i : longint;
  3929. msgwritten : boolean;
  3930. begin
  3931. i:=0;
  3932. msgwritten:=false;
  3933. if (c='''') then
  3934. begin
  3935. repeat
  3936. readchar;
  3937. case c of
  3938. #26 :
  3939. end_of_file;
  3940. #10,#13 :
  3941. Message(scan_f_string_exceeds_line);
  3942. '''' :
  3943. begin
  3944. readchar;
  3945. if c<>'''' then
  3946. break;
  3947. end;
  3948. end;
  3949. if i<255 then
  3950. begin
  3951. inc(i);
  3952. result[i]:=c;
  3953. end
  3954. else
  3955. begin
  3956. if not msgwritten then
  3957. begin
  3958. Message(scan_e_string_exceeds_255_chars);
  3959. msgwritten:=true;
  3960. end;
  3961. end;
  3962. until false;
  3963. end;
  3964. result[0]:=chr(i);
  3965. end;
  3966. function tscannerfile.readstate:char;
  3967. var
  3968. state : char;
  3969. begin
  3970. state:=' ';
  3971. if c=' ' then
  3972. begin
  3973. current_scanner.skipspace;
  3974. current_scanner.readid;
  3975. if pattern='ON' then
  3976. state:='+'
  3977. else
  3978. if pattern='OFF' then
  3979. state:='-';
  3980. end
  3981. else
  3982. state:=c;
  3983. if not (state in ['+','-']) then
  3984. Message(scan_e_wrong_switch_toggle);
  3985. readstate:=state;
  3986. end;
  3987. function tscannerfile.readoptionalstate(fallback:char):char;
  3988. var
  3989. state : char;
  3990. begin
  3991. state:=' ';
  3992. if c=' ' then
  3993. begin
  3994. current_scanner.skipspace;
  3995. if c in ['*','}'] then
  3996. state:=fallback
  3997. else
  3998. begin
  3999. current_scanner.readid;
  4000. if pattern='ON' then
  4001. state:='+'
  4002. else
  4003. if pattern='OFF' then
  4004. state:='-';
  4005. end;
  4006. end
  4007. else
  4008. if c in ['*','}'] then
  4009. state:=fallback
  4010. else
  4011. state:=c;
  4012. if not (state in ['+','-']) then
  4013. Message(scan_e_wrong_switch_toggle);
  4014. readoptionalstate:=state;
  4015. end;
  4016. function tscannerfile.readstatedefault:char;
  4017. var
  4018. state : char;
  4019. begin
  4020. state:=' ';
  4021. if c=' ' then
  4022. begin
  4023. current_scanner.skipspace;
  4024. current_scanner.readid;
  4025. if pattern='ON' then
  4026. state:='+'
  4027. else
  4028. if pattern='OFF' then
  4029. state:='-'
  4030. else
  4031. if pattern='DEFAULT' then
  4032. state:='*';
  4033. end
  4034. else
  4035. state:=c;
  4036. if not (state in ['+','-','*']) then
  4037. Message(scan_e_wrong_switch_toggle_default);
  4038. readstatedefault:=state;
  4039. end;
  4040. procedure tscannerfile.skipspace;
  4041. begin
  4042. repeat
  4043. case c of
  4044. #26 :
  4045. begin
  4046. reload;
  4047. if (c=#26) and not assigned(inputfile.next) then
  4048. break;
  4049. continue;
  4050. end;
  4051. #10,
  4052. #13 :
  4053. linebreak;
  4054. #9,#11,#12,' ' :
  4055. ;
  4056. else
  4057. break;
  4058. end;
  4059. readchar;
  4060. until false;
  4061. end;
  4062. procedure tscannerfile.skipuntildirective;
  4063. var
  4064. found : longint;
  4065. next_char_loaded : boolean;
  4066. begin
  4067. found:=0;
  4068. next_char_loaded:=false;
  4069. repeat
  4070. case c of
  4071. #10,
  4072. #13 :
  4073. linebreak;
  4074. #26 :
  4075. begin
  4076. reload;
  4077. if (c=#26) and not assigned(inputfile.next) then
  4078. end_of_file;
  4079. continue;
  4080. end;
  4081. '{' :
  4082. begin
  4083. if (current_commentstyle in [comment_tp,comment_none]) then
  4084. begin
  4085. current_commentstyle:=comment_tp;
  4086. if (comment_level=0) then
  4087. found:=1;
  4088. inc_comment_level;
  4089. end;
  4090. end;
  4091. '*' :
  4092. begin
  4093. if (current_commentstyle=comment_oldtp) then
  4094. begin
  4095. readchar;
  4096. if c=')' then
  4097. begin
  4098. dec_comment_level;
  4099. found:=0;
  4100. current_commentstyle:=comment_none;
  4101. end
  4102. else
  4103. next_char_loaded:=true;
  4104. end
  4105. else
  4106. found := 0;
  4107. end;
  4108. '}' :
  4109. begin
  4110. if (current_commentstyle=comment_tp) then
  4111. begin
  4112. dec_comment_level;
  4113. if (comment_level=0) then
  4114. current_commentstyle:=comment_none;
  4115. found:=0;
  4116. end;
  4117. end;
  4118. '$' :
  4119. begin
  4120. if found=1 then
  4121. found:=2;
  4122. end;
  4123. '''' :
  4124. if (current_commentstyle=comment_none) then
  4125. begin
  4126. repeat
  4127. readchar;
  4128. case c of
  4129. #26 :
  4130. end_of_file;
  4131. #10,#13 :
  4132. break;
  4133. '''' :
  4134. begin
  4135. readchar;
  4136. if c<>'''' then
  4137. begin
  4138. next_char_loaded:=true;
  4139. break;
  4140. end;
  4141. end;
  4142. end;
  4143. until false;
  4144. end;
  4145. '(' :
  4146. begin
  4147. if (current_commentstyle=comment_none) then
  4148. begin
  4149. readchar;
  4150. if c='*' then
  4151. begin
  4152. readchar;
  4153. if c='$' then
  4154. begin
  4155. found:=2;
  4156. inc_comment_level;
  4157. current_commentstyle:=comment_oldtp;
  4158. end
  4159. else
  4160. begin
  4161. skipoldtpcomment(false);
  4162. next_char_loaded:=true;
  4163. end;
  4164. end
  4165. else
  4166. next_char_loaded:=true;
  4167. end
  4168. else
  4169. found:=0;
  4170. end;
  4171. '/' :
  4172. begin
  4173. if (current_commentstyle=comment_none) then
  4174. begin
  4175. readchar;
  4176. if c='/' then
  4177. skipdelphicomment;
  4178. next_char_loaded:=true;
  4179. end
  4180. else
  4181. found:=0;
  4182. end;
  4183. else
  4184. found:=0;
  4185. end;
  4186. if next_char_loaded then
  4187. next_char_loaded:=false
  4188. else
  4189. readchar;
  4190. until (found=2);
  4191. end;
  4192. {****************************************************************************
  4193. Comment Handling
  4194. ****************************************************************************}
  4195. procedure tscannerfile.skipcomment(read_first_char:boolean);
  4196. begin
  4197. current_commentstyle:=comment_tp;
  4198. if read_first_char then
  4199. readchar;
  4200. inc_comment_level;
  4201. { handle compiler switches }
  4202. if (c='$') then
  4203. handledirectives;
  4204. { handle_switches can dec comment_level, }
  4205. while (comment_level>0) do
  4206. begin
  4207. case c of
  4208. '{' :
  4209. inc_comment_level;
  4210. '}' :
  4211. dec_comment_level;
  4212. #10,#13 :
  4213. linebreak;
  4214. #26 :
  4215. begin
  4216. reload;
  4217. if (c=#26) and not assigned(inputfile.next) then
  4218. end_of_file;
  4219. continue;
  4220. end;
  4221. end;
  4222. readchar;
  4223. end;
  4224. current_commentstyle:=comment_none;
  4225. end;
  4226. procedure tscannerfile.skipdelphicomment;
  4227. begin
  4228. current_commentstyle:=comment_delphi;
  4229. inc_comment_level;
  4230. readchar;
  4231. { this is not supported }
  4232. if c='$' then
  4233. Message(scan_w_wrong_styled_switch);
  4234. { skip comment }
  4235. while not (c in [#10,#13,#26]) do
  4236. readchar;
  4237. dec_comment_level;
  4238. current_commentstyle:=comment_none;
  4239. end;
  4240. procedure tscannerfile.skipoldtpcomment(read_first_char:boolean);
  4241. var
  4242. found : longint;
  4243. begin
  4244. current_commentstyle:=comment_oldtp;
  4245. inc_comment_level;
  4246. { only load a char if last already processed,
  4247. was cause of bug1634 PM }
  4248. if read_first_char then
  4249. readchar;
  4250. { this is now supported }
  4251. if (c='$') then
  4252. handledirectives;
  4253. { skip comment }
  4254. while (comment_level>0) do
  4255. begin
  4256. found:=0;
  4257. repeat
  4258. case c of
  4259. #26 :
  4260. begin
  4261. reload;
  4262. if (c=#26) and not assigned(inputfile.next) then
  4263. end_of_file;
  4264. continue;
  4265. end;
  4266. #10,#13 :
  4267. begin
  4268. if found=4 then
  4269. inc_comment_level;
  4270. linebreak;
  4271. found:=0;
  4272. end;
  4273. '*' :
  4274. begin
  4275. if found=3 then
  4276. found:=4
  4277. else
  4278. begin
  4279. if found=4 then
  4280. inc_comment_level;
  4281. found:=1;
  4282. end;
  4283. end;
  4284. ')' :
  4285. begin
  4286. if found in [1,4] then
  4287. begin
  4288. dec_comment_level;
  4289. if comment_level=0 then
  4290. found:=2
  4291. else
  4292. found:=0;
  4293. end
  4294. else
  4295. found:=0;
  4296. end;
  4297. '(' :
  4298. begin
  4299. if found=4 then
  4300. inc_comment_level;
  4301. found:=3;
  4302. end;
  4303. else
  4304. begin
  4305. if found=4 then
  4306. inc_comment_level;
  4307. found:=0;
  4308. end;
  4309. end;
  4310. readchar;
  4311. until (found=2);
  4312. end;
  4313. current_commentstyle:=comment_none;
  4314. end;
  4315. {****************************************************************************
  4316. Token Scanner
  4317. ****************************************************************************}
  4318. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  4319. var
  4320. code : integer;
  4321. d : cardinal;
  4322. len,
  4323. low,high,mid : longint;
  4324. w : word;
  4325. m : longint;
  4326. mac : tmacro;
  4327. asciinr : string[33];
  4328. iswidestring : boolean;
  4329. label
  4330. exit_label;
  4331. begin
  4332. flushpendingswitchesstate;
  4333. { record tokens? }
  4334. if allowrecordtoken and
  4335. assigned(recordtokenbuf) then
  4336. recordtoken;
  4337. { replay tokens? }
  4338. if assigned(replaytokenbuf) then
  4339. begin
  4340. replaytoken;
  4341. goto exit_label;
  4342. end;
  4343. { was there already a token read, then return that token }
  4344. if nexttoken<>NOTOKEN then
  4345. begin
  4346. setnexttoken;
  4347. goto exit_label;
  4348. end;
  4349. { Skip all spaces and comments }
  4350. repeat
  4351. case c of
  4352. '{' :
  4353. skipcomment(true);
  4354. #26 :
  4355. begin
  4356. reload;
  4357. if (c=#26) and not assigned(inputfile.next) then
  4358. break;
  4359. end;
  4360. ' ',#9..#13 :
  4361. begin
  4362. {$ifdef PREPROCWRITE}
  4363. if parapreprocess then
  4364. begin
  4365. if c=#10 then
  4366. preprocfile.eolfound:=true
  4367. else
  4368. preprocfile.spacefound:=true;
  4369. end;
  4370. {$endif PREPROCWRITE}
  4371. skipspace;
  4372. end
  4373. else
  4374. break;
  4375. end;
  4376. until false;
  4377. { Save current token position, for EOF its already loaded }
  4378. if c<>#26 then
  4379. gettokenpos;
  4380. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  4381. if c in ['A'..'Z','a'..'z','_'] then
  4382. begin
  4383. readstring;
  4384. token:=_ID;
  4385. idtoken:=_ID;
  4386. { keyword or any other known token,
  4387. pattern is always uppercased }
  4388. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4389. begin
  4390. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4391. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4392. while low<high do
  4393. begin
  4394. mid:=(high+low+1) shr 1;
  4395. if pattern<tokeninfo^[ttoken(mid)].str then
  4396. high:=mid-1
  4397. else
  4398. low:=mid;
  4399. end;
  4400. with tokeninfo^[ttoken(high)] do
  4401. if pattern=str then
  4402. begin
  4403. if (keyword*current_settings.modeswitches)<>[] then
  4404. if op=NOTOKEN then
  4405. token:=ttoken(high)
  4406. else
  4407. token:=op;
  4408. idtoken:=ttoken(high);
  4409. end;
  4410. end;
  4411. { Only process identifiers and not keywords }
  4412. if token=_ID then
  4413. begin
  4414. { this takes some time ... }
  4415. if (cs_support_macro in current_settings.moduleswitches) then
  4416. begin
  4417. mac:=tmacro(search_macro(pattern));
  4418. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  4419. begin
  4420. if yylexcount<max_macro_nesting then
  4421. begin
  4422. mac.is_used:=true;
  4423. inc(yylexcount);
  4424. substitutemacro(pattern,mac.buftext,mac.buflen,
  4425. mac.fileinfo.line,mac.fileinfo.fileindex);
  4426. { handle empty macros }
  4427. if c=#0 then
  4428. reload;
  4429. readtoken(false);
  4430. { that's all folks }
  4431. dec(yylexcount);
  4432. exit;
  4433. end
  4434. else
  4435. Message(scan_w_macro_too_deep);
  4436. end;
  4437. end;
  4438. end;
  4439. { return token }
  4440. goto exit_label;
  4441. end
  4442. else
  4443. begin
  4444. idtoken:=_NOID;
  4445. case c of
  4446. '$' :
  4447. begin
  4448. readnumber;
  4449. token:=_INTCONST;
  4450. goto exit_label;
  4451. end;
  4452. '%' :
  4453. begin
  4454. if not(m_fpc in current_settings.modeswitches) then
  4455. Illegal_Char(c)
  4456. else
  4457. begin
  4458. readnumber;
  4459. token:=_INTCONST;
  4460. goto exit_label;
  4461. end;
  4462. end;
  4463. '&' :
  4464. begin
  4465. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  4466. begin
  4467. readnumber;
  4468. if length(pattern)=1 then
  4469. begin
  4470. { does really an identifier follow? }
  4471. if not (c in ['_','A'..'Z','a'..'z']) then
  4472. message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  4473. readstring;
  4474. token:=_ID;
  4475. idtoken:=_ID;
  4476. end
  4477. else
  4478. token:=_INTCONST;
  4479. goto exit_label;
  4480. end
  4481. else if m_mac in current_settings.modeswitches then
  4482. begin
  4483. readchar;
  4484. token:=_AMPERSAND;
  4485. goto exit_label;
  4486. end
  4487. else
  4488. Illegal_Char(c);
  4489. end;
  4490. '0'..'9' :
  4491. begin
  4492. readnumber;
  4493. if (c in ['.','e','E']) then
  4494. begin
  4495. { first check for a . }
  4496. if c='.' then
  4497. begin
  4498. cachenexttokenpos;
  4499. readchar;
  4500. { is it a .. from a range? }
  4501. case c of
  4502. '.' :
  4503. begin
  4504. readchar;
  4505. token:=_INTCONST;
  4506. nexttoken:=_POINTPOINT;
  4507. goto exit_label;
  4508. end;
  4509. ')' :
  4510. begin
  4511. readchar;
  4512. token:=_INTCONST;
  4513. nexttoken:=_RECKKLAMMER;
  4514. goto exit_label;
  4515. end;
  4516. '0'..'9' :
  4517. begin
  4518. { insert the number after the . }
  4519. pattern:=pattern+'.';
  4520. while c in ['0'..'9'] do
  4521. begin
  4522. pattern:=pattern+c;
  4523. readchar;
  4524. end;
  4525. end;
  4526. else
  4527. begin
  4528. token:=_INTCONST;
  4529. nexttoken:=_POINT;
  4530. goto exit_label;
  4531. end;
  4532. end;
  4533. end;
  4534. { E can also follow after a point is scanned }
  4535. if c in ['e','E'] then
  4536. begin
  4537. pattern:=pattern+'E';
  4538. readchar;
  4539. if c in ['-','+'] then
  4540. begin
  4541. pattern:=pattern+c;
  4542. readchar;
  4543. end;
  4544. if not(c in ['0'..'9']) then
  4545. Illegal_Char(c);
  4546. while c in ['0'..'9'] do
  4547. begin
  4548. pattern:=pattern+c;
  4549. readchar;
  4550. end;
  4551. end;
  4552. token:=_REALNUMBER;
  4553. goto exit_label;
  4554. end;
  4555. token:=_INTCONST;
  4556. goto exit_label;
  4557. end;
  4558. ';' :
  4559. begin
  4560. readchar;
  4561. token:=_SEMICOLON;
  4562. goto exit_label;
  4563. end;
  4564. '[' :
  4565. begin
  4566. readchar;
  4567. token:=_LECKKLAMMER;
  4568. goto exit_label;
  4569. end;
  4570. ']' :
  4571. begin
  4572. readchar;
  4573. token:=_RECKKLAMMER;
  4574. goto exit_label;
  4575. end;
  4576. '(' :
  4577. begin
  4578. readchar;
  4579. case c of
  4580. '*' :
  4581. begin
  4582. skipoldtpcomment(true);
  4583. readtoken(false);
  4584. exit;
  4585. end;
  4586. '.' :
  4587. begin
  4588. readchar;
  4589. token:=_LECKKLAMMER;
  4590. goto exit_label;
  4591. end;
  4592. end;
  4593. token:=_LKLAMMER;
  4594. goto exit_label;
  4595. end;
  4596. ')' :
  4597. begin
  4598. readchar;
  4599. token:=_RKLAMMER;
  4600. goto exit_label;
  4601. end;
  4602. '+' :
  4603. begin
  4604. readchar;
  4605. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4606. begin
  4607. readchar;
  4608. token:=_PLUSASN;
  4609. goto exit_label;
  4610. end;
  4611. token:=_PLUS;
  4612. goto exit_label;
  4613. end;
  4614. '-' :
  4615. begin
  4616. readchar;
  4617. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4618. begin
  4619. readchar;
  4620. token:=_MINUSASN;
  4621. goto exit_label;
  4622. end;
  4623. token:=_MINUS;
  4624. goto exit_label;
  4625. end;
  4626. ':' :
  4627. begin
  4628. readchar;
  4629. if c='=' then
  4630. begin
  4631. readchar;
  4632. token:=_ASSIGNMENT;
  4633. goto exit_label;
  4634. end;
  4635. token:=_COLON;
  4636. goto exit_label;
  4637. end;
  4638. '*' :
  4639. begin
  4640. readchar;
  4641. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4642. begin
  4643. readchar;
  4644. token:=_STARASN;
  4645. end
  4646. else
  4647. if c='*' then
  4648. begin
  4649. readchar;
  4650. token:=_STARSTAR;
  4651. end
  4652. else
  4653. token:=_STAR;
  4654. goto exit_label;
  4655. end;
  4656. '/' :
  4657. begin
  4658. readchar;
  4659. case c of
  4660. '=' :
  4661. begin
  4662. if (cs_support_c_operators in current_settings.moduleswitches) then
  4663. begin
  4664. readchar;
  4665. token:=_SLASHASN;
  4666. goto exit_label;
  4667. end;
  4668. end;
  4669. '/' :
  4670. begin
  4671. skipdelphicomment;
  4672. readtoken(false);
  4673. exit;
  4674. end;
  4675. end;
  4676. token:=_SLASH;
  4677. goto exit_label;
  4678. end;
  4679. '|' :
  4680. if m_mac in current_settings.modeswitches then
  4681. begin
  4682. readchar;
  4683. token:=_PIPE;
  4684. goto exit_label;
  4685. end
  4686. else
  4687. Illegal_Char(c);
  4688. '=' :
  4689. begin
  4690. readchar;
  4691. token:=_EQ;
  4692. goto exit_label;
  4693. end;
  4694. '.' :
  4695. begin
  4696. readchar;
  4697. case c of
  4698. '.' :
  4699. begin
  4700. readchar;
  4701. case c of
  4702. '.' :
  4703. begin
  4704. readchar;
  4705. token:=_POINTPOINTPOINT;
  4706. goto exit_label;
  4707. end;
  4708. else
  4709. begin
  4710. token:=_POINTPOINT;
  4711. goto exit_label;
  4712. end;
  4713. end;
  4714. end;
  4715. ')' :
  4716. begin
  4717. readchar;
  4718. token:=_RECKKLAMMER;
  4719. goto exit_label;
  4720. end;
  4721. end;
  4722. token:=_POINT;
  4723. goto exit_label;
  4724. end;
  4725. '@' :
  4726. begin
  4727. readchar;
  4728. token:=_KLAMMERAFFE;
  4729. goto exit_label;
  4730. end;
  4731. ',' :
  4732. begin
  4733. readchar;
  4734. token:=_COMMA;
  4735. goto exit_label;
  4736. end;
  4737. '''','#','^' :
  4738. begin
  4739. len:=0;
  4740. cstringpattern:='';
  4741. iswidestring:=false;
  4742. if c='^' then
  4743. begin
  4744. readchar;
  4745. c:=upcase(c);
  4746. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  4747. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  4748. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  4749. begin
  4750. token:=_CARET;
  4751. goto exit_label;
  4752. end
  4753. else
  4754. begin
  4755. inc(len);
  4756. setlength(cstringpattern,256);
  4757. if c<#64 then
  4758. cstringpattern[len]:=chr(ord(c)+64)
  4759. else
  4760. cstringpattern[len]:=chr(ord(c)-64);
  4761. readchar;
  4762. end;
  4763. end;
  4764. repeat
  4765. case c of
  4766. '#' :
  4767. begin
  4768. readchar; { read # }
  4769. case c of
  4770. '$':
  4771. begin
  4772. readchar; { read leading $ }
  4773. asciinr:='$';
  4774. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=7) do
  4775. begin
  4776. asciinr:=asciinr+c;
  4777. readchar;
  4778. end;
  4779. end;
  4780. '&':
  4781. begin
  4782. readchar; { read leading $ }
  4783. asciinr:='&';
  4784. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=8) do
  4785. begin
  4786. asciinr:=asciinr+c;
  4787. readchar;
  4788. end;
  4789. end;
  4790. '%':
  4791. begin
  4792. readchar; { read leading $ }
  4793. asciinr:='%';
  4794. while (upcase(c) in ['0','1']) and (length(asciinr)<=22) do
  4795. begin
  4796. asciinr:=asciinr+c;
  4797. readchar;
  4798. end;
  4799. end;
  4800. else
  4801. begin
  4802. asciinr:='';
  4803. while (c in ['0'..'9']) and (length(asciinr)<=8) do
  4804. begin
  4805. asciinr:=asciinr+c;
  4806. readchar;
  4807. end;
  4808. end;
  4809. end;
  4810. val(asciinr,m,code);
  4811. if (asciinr='') or (code<>0) then
  4812. Message(scan_e_illegal_char_const)
  4813. else if (m<0) or (m>255) or (length(asciinr)>3) then
  4814. begin
  4815. if (m>=0) and (m<=$10FFFF) then
  4816. begin
  4817. if not iswidestring then
  4818. begin
  4819. if len>0 then
  4820. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4821. else
  4822. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4823. iswidestring:=true;
  4824. len:=0;
  4825. end;
  4826. if m<=$FFFF then
  4827. concatwidestringchar(patternw,tcompilerwidechar(m))
  4828. else
  4829. begin
  4830. { split into surrogate pair }
  4831. dec(m,$10000);
  4832. concatwidestringchar(patternw,tcompilerwidechar((m shr 10) + $D800));
  4833. concatwidestringchar(patternw,tcompilerwidechar((m and $3FF) + $DC00));
  4834. end;
  4835. end
  4836. else
  4837. Message(scan_e_illegal_char_const)
  4838. end
  4839. else if iswidestring then
  4840. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  4841. else
  4842. begin
  4843. if len>=length(cstringpattern) then
  4844. setlength(cstringpattern,length(cstringpattern)+256);
  4845. inc(len);
  4846. cstringpattern[len]:=chr(m);
  4847. end;
  4848. end;
  4849. '''' :
  4850. begin
  4851. repeat
  4852. readchar;
  4853. case c of
  4854. #26 :
  4855. end_of_file;
  4856. #10,#13 :
  4857. Message(scan_f_string_exceeds_line);
  4858. '''' :
  4859. begin
  4860. readchar;
  4861. if c<>'''' then
  4862. break;
  4863. end;
  4864. end;
  4865. { interpret as utf-8 string? }
  4866. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  4867. begin
  4868. { convert existing string to an utf-8 string }
  4869. if not iswidestring then
  4870. begin
  4871. if len>0 then
  4872. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4873. else
  4874. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4875. iswidestring:=true;
  4876. len:=0;
  4877. end;
  4878. { four chars }
  4879. if (ord(c) and $f0)=$f0 then
  4880. begin
  4881. { this always represents a surrogate pair, so
  4882. read as 32-bit value and then split into
  4883. the corresponding pair of two wchars }
  4884. d:=ord(c) and $f;
  4885. readchar;
  4886. if (ord(c) and $c0)<>$80 then
  4887. message(scan_e_utf8_malformed);
  4888. d:=(d shl 6) or (ord(c) and $3f);
  4889. readchar;
  4890. if (ord(c) and $c0)<>$80 then
  4891. message(scan_e_utf8_malformed);
  4892. d:=(d shl 6) or (ord(c) and $3f);
  4893. readchar;
  4894. if (ord(c) and $c0)<>$80 then
  4895. message(scan_e_utf8_malformed);
  4896. d:=(d shl 6) or (ord(c) and $3f);
  4897. if d<$10000 then
  4898. message(scan_e_utf8_malformed);
  4899. d:=d-$10000;
  4900. { high surrogate }
  4901. w:=$d800+(d shr 10);
  4902. concatwidestringchar(patternw,w);
  4903. { low surrogate }
  4904. w:=$dc00+(d and $3ff);
  4905. concatwidestringchar(patternw,w);
  4906. end
  4907. { three chars }
  4908. else if (ord(c) and $e0)=$e0 then
  4909. begin
  4910. w:=ord(c) and $f;
  4911. readchar;
  4912. if (ord(c) and $c0)<>$80 then
  4913. message(scan_e_utf8_malformed);
  4914. w:=(w shl 6) or (ord(c) and $3f);
  4915. readchar;
  4916. if (ord(c) and $c0)<>$80 then
  4917. message(scan_e_utf8_malformed);
  4918. w:=(w shl 6) or (ord(c) and $3f);
  4919. concatwidestringchar(patternw,w);
  4920. end
  4921. { two chars }
  4922. else if (ord(c) and $c0)<>0 then
  4923. begin
  4924. w:=ord(c) and $1f;
  4925. readchar;
  4926. if (ord(c) and $c0)<>$80 then
  4927. message(scan_e_utf8_malformed);
  4928. w:=(w shl 6) or (ord(c) and $3f);
  4929. concatwidestringchar(patternw,w);
  4930. end
  4931. { illegal }
  4932. else if (ord(c) and $80)<>0 then
  4933. message(scan_e_utf8_malformed)
  4934. else
  4935. concatwidestringchar(patternw,tcompilerwidechar(c))
  4936. end
  4937. else if iswidestring then
  4938. begin
  4939. if current_settings.sourcecodepage=CP_UTF8 then
  4940. concatwidestringchar(patternw,ord(c))
  4941. else
  4942. concatwidestringchar(patternw,asciichar2unicode(c))
  4943. end
  4944. else
  4945. begin
  4946. if len>=length(cstringpattern) then
  4947. setlength(cstringpattern,length(cstringpattern)+256);
  4948. inc(len);
  4949. cstringpattern[len]:=c;
  4950. end;
  4951. until false;
  4952. end;
  4953. '^' :
  4954. begin
  4955. readchar;
  4956. c:=upcase(c);
  4957. if c<#64 then
  4958. c:=chr(ord(c)+64)
  4959. else
  4960. c:=chr(ord(c)-64);
  4961. if iswidestring then
  4962. concatwidestringchar(patternw,asciichar2unicode(c))
  4963. else
  4964. begin
  4965. if len>=length(cstringpattern) then
  4966. setlength(cstringpattern,length(cstringpattern)+256);
  4967. inc(len);
  4968. cstringpattern[len]:=c;
  4969. end;
  4970. readchar;
  4971. end;
  4972. else
  4973. break;
  4974. end;
  4975. until false;
  4976. { strings with length 1 become const chars }
  4977. if iswidestring then
  4978. begin
  4979. if patternw^.len=1 then
  4980. token:=_CWCHAR
  4981. else
  4982. token:=_CWSTRING;
  4983. end
  4984. else
  4985. begin
  4986. setlength(cstringpattern,len);
  4987. if length(cstringpattern)=1 then
  4988. begin
  4989. token:=_CCHAR;
  4990. pattern:=cstringpattern;
  4991. end
  4992. else
  4993. token:=_CSTRING;
  4994. end;
  4995. goto exit_label;
  4996. end;
  4997. '>' :
  4998. begin
  4999. readchar;
  5000. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5001. token:=_RSHARPBRACKET
  5002. else
  5003. begin
  5004. case c of
  5005. '=' :
  5006. begin
  5007. readchar;
  5008. token:=_GTE;
  5009. goto exit_label;
  5010. end;
  5011. '>' :
  5012. begin
  5013. readchar;
  5014. token:=_OP_SHR;
  5015. goto exit_label;
  5016. end;
  5017. '<' :
  5018. begin { >< is for a symetric diff for sets }
  5019. readchar;
  5020. token:=_SYMDIF;
  5021. goto exit_label;
  5022. end;
  5023. end;
  5024. token:=_GT;
  5025. end;
  5026. goto exit_label;
  5027. end;
  5028. '<' :
  5029. begin
  5030. readchar;
  5031. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5032. token:=_LSHARPBRACKET
  5033. else
  5034. begin
  5035. case c of
  5036. '>' :
  5037. begin
  5038. readchar;
  5039. token:=_NE;
  5040. goto exit_label;
  5041. end;
  5042. '=' :
  5043. begin
  5044. readchar;
  5045. token:=_LTE;
  5046. goto exit_label;
  5047. end;
  5048. '<' :
  5049. begin
  5050. readchar;
  5051. token:=_OP_SHL;
  5052. goto exit_label;
  5053. end;
  5054. end;
  5055. token:=_LT;
  5056. end;
  5057. goto exit_label;
  5058. end;
  5059. #26 :
  5060. begin
  5061. token:=_EOF;
  5062. checkpreprocstack;
  5063. goto exit_label;
  5064. end;
  5065. else
  5066. Illegal_Char(c);
  5067. end;
  5068. end;
  5069. exit_label:
  5070. lasttoken:=token;
  5071. end;
  5072. function tscannerfile.readpreproc:ttoken;
  5073. var
  5074. low,high,mid: longint;
  5075. optoken: ttoken;
  5076. begin
  5077. skipspace;
  5078. case c of
  5079. '_',
  5080. 'A'..'Z',
  5081. 'a'..'z' :
  5082. begin
  5083. readstring;
  5084. optoken:=_ID;
  5085. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  5086. begin
  5087. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  5088. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  5089. while low<high do
  5090. begin
  5091. mid:=(high+low+1) shr 1;
  5092. if pattern<tokeninfo^[ttoken(mid)].str then
  5093. high:=mid-1
  5094. else
  5095. low:=mid;
  5096. end;
  5097. with tokeninfo^[ttoken(high)] do
  5098. if pattern=str then
  5099. begin
  5100. if (keyword*current_settings.modeswitches)<>[] then
  5101. if op=NOTOKEN then
  5102. optoken:=ttoken(high)
  5103. else
  5104. optoken:=op;
  5105. end;
  5106. if not (optoken in preproc_operators) then
  5107. optoken:=_ID;
  5108. end;
  5109. current_scanner.preproc_pattern:=pattern;
  5110. readpreproc:=optoken;
  5111. end;
  5112. '''' :
  5113. begin
  5114. readquotedstring;
  5115. current_scanner.preproc_pattern:=cstringpattern;
  5116. readpreproc:=_CSTRING;
  5117. end;
  5118. '0'..'9' :
  5119. begin
  5120. readnumber;
  5121. if (c in ['.','e','E']) then
  5122. begin
  5123. { first check for a . }
  5124. if c='.' then
  5125. begin
  5126. readchar;
  5127. if c in ['0'..'9'] then
  5128. begin
  5129. { insert the number after the . }
  5130. pattern:=pattern+'.';
  5131. while c in ['0'..'9'] do
  5132. begin
  5133. pattern:=pattern+c;
  5134. readchar;
  5135. end;
  5136. end
  5137. else
  5138. Illegal_Char(c);
  5139. end;
  5140. { E can also follow after a point is scanned }
  5141. if c in ['e','E'] then
  5142. begin
  5143. pattern:=pattern+'E';
  5144. readchar;
  5145. if c in ['-','+'] then
  5146. begin
  5147. pattern:=pattern+c;
  5148. readchar;
  5149. end;
  5150. if not(c in ['0'..'9']) then
  5151. Illegal_Char(c);
  5152. while c in ['0'..'9'] do
  5153. begin
  5154. pattern:=pattern+c;
  5155. readchar;
  5156. end;
  5157. end;
  5158. readpreproc:=_REALNUMBER;
  5159. end
  5160. else
  5161. readpreproc:=_INTCONST;
  5162. current_scanner.preproc_pattern:=pattern;
  5163. end;
  5164. '$','%':
  5165. begin
  5166. readnumber;
  5167. current_scanner.preproc_pattern:=pattern;
  5168. readpreproc:=_INTCONST;
  5169. end;
  5170. '&' :
  5171. begin
  5172. readnumber;
  5173. if length(pattern)=1 then
  5174. begin
  5175. readstring;
  5176. readpreproc:=_ID;
  5177. end
  5178. else
  5179. readpreproc:=_INTCONST;
  5180. current_scanner.preproc_pattern:=pattern;
  5181. end;
  5182. '.' :
  5183. begin
  5184. readchar;
  5185. readpreproc:=_POINT;
  5186. end;
  5187. ',' :
  5188. begin
  5189. readchar;
  5190. readpreproc:=_COMMA;
  5191. end;
  5192. '}' :
  5193. begin
  5194. readpreproc:=_END;
  5195. end;
  5196. '(' :
  5197. begin
  5198. readchar;
  5199. readpreproc:=_LKLAMMER;
  5200. end;
  5201. ')' :
  5202. begin
  5203. readchar;
  5204. readpreproc:=_RKLAMMER;
  5205. end;
  5206. '[' :
  5207. begin
  5208. readchar;
  5209. readpreproc:=_LECKKLAMMER;
  5210. end;
  5211. ']' :
  5212. begin
  5213. readchar;
  5214. readpreproc:=_RECKKLAMMER;
  5215. end;
  5216. '+' :
  5217. begin
  5218. readchar;
  5219. readpreproc:=_PLUS;
  5220. end;
  5221. '-' :
  5222. begin
  5223. readchar;
  5224. readpreproc:=_MINUS;
  5225. end;
  5226. '*' :
  5227. begin
  5228. readchar;
  5229. readpreproc:=_STAR;
  5230. end;
  5231. '/' :
  5232. begin
  5233. readchar;
  5234. readpreproc:=_SLASH;
  5235. end;
  5236. '=' :
  5237. begin
  5238. readchar;
  5239. readpreproc:=_EQ;
  5240. end;
  5241. '>' :
  5242. begin
  5243. readchar;
  5244. if c='=' then
  5245. begin
  5246. readchar;
  5247. readpreproc:=_GTE;
  5248. end
  5249. else
  5250. readpreproc:=_GT;
  5251. end;
  5252. '<' :
  5253. begin
  5254. readchar;
  5255. case c of
  5256. '>' :
  5257. begin
  5258. readchar;
  5259. readpreproc:=_NE;
  5260. end;
  5261. '=' :
  5262. begin
  5263. readchar;
  5264. readpreproc:=_LTE;
  5265. end;
  5266. else
  5267. readpreproc:=_LT;
  5268. end;
  5269. end;
  5270. #26 :
  5271. begin
  5272. readpreproc:=_EOF;
  5273. checkpreprocstack;
  5274. end;
  5275. else
  5276. begin
  5277. Illegal_Char(c);
  5278. readpreproc:=NOTOKEN;
  5279. end;
  5280. end;
  5281. end;
  5282. function tscannerfile.readpreprocint(var value:int64;const place:string):boolean;
  5283. var
  5284. hs : texprvalue;
  5285. begin
  5286. hs:=preproc_comp_expr;
  5287. if hs.isInt then
  5288. begin
  5289. value:=hs.asInt64;
  5290. result:=true;
  5291. end
  5292. else
  5293. begin
  5294. hs.error('Integer',place);
  5295. result:=false;
  5296. end;
  5297. hs.free;
  5298. end;
  5299. function tscannerfile.asmgetchar : char;
  5300. begin
  5301. readchar;
  5302. repeat
  5303. case c of
  5304. #26 :
  5305. begin
  5306. reload;
  5307. if (c=#26) and not assigned(inputfile.next) then
  5308. end_of_file;
  5309. continue;
  5310. end;
  5311. else
  5312. begin
  5313. asmgetchar:=c;
  5314. exit;
  5315. end;
  5316. end;
  5317. until false;
  5318. end;
  5319. {*****************************************************************************
  5320. Helpers
  5321. *****************************************************************************}
  5322. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5323. begin
  5324. if dm in [directive_all, directive_turbo] then
  5325. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  5326. if dm in [directive_all, directive_mac] then
  5327. tdirectiveitem.create(mac_scannerdirectives,s,p);
  5328. end;
  5329. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5330. begin
  5331. if dm in [directive_all, directive_turbo] then
  5332. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  5333. if dm in [directive_all, directive_mac] then
  5334. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  5335. end;
  5336. {*****************************************************************************
  5337. Initialization
  5338. *****************************************************************************}
  5339. procedure InitScanner;
  5340. begin
  5341. InitWideString(patternw);
  5342. turbo_scannerdirectives:=TFPHashObjectList.Create;
  5343. mac_scannerdirectives:=TFPHashObjectList.Create;
  5344. { Common directives and conditionals }
  5345. AddDirective('I',directive_all, @dir_include);
  5346. AddDirective('DEFINE',directive_all, @dir_define);
  5347. AddDirective('UNDEF',directive_all, @dir_undef);
  5348. AddConditional('IF',directive_all, @dir_if);
  5349. AddConditional('IFDEF',directive_all, @dir_ifdef);
  5350. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  5351. AddConditional('ELSE',directive_all, @dir_else);
  5352. AddConditional('ELSEIF',directive_all, @dir_elseif);
  5353. AddConditional('ENDIF',directive_all, @dir_endif);
  5354. { Directives and conditionals for all modes except mode macpas}
  5355. AddDirective('INCLUDE',directive_turbo, @dir_include);
  5356. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  5357. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  5358. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  5359. AddConditional('IFEND',directive_turbo, @dir_endif);
  5360. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  5361. { Directives and conditionals for mode macpas: }
  5362. AddDirective('SETC',directive_mac, @dir_setc);
  5363. AddDirective('DEFINEC',directive_mac, @dir_definec);
  5364. AddDirective('UNDEFC',directive_mac, @dir_undef);
  5365. AddConditional('IFC',directive_mac, @dir_if);
  5366. AddConditional('ELSEC',directive_mac, @dir_else);
  5367. AddConditional('ELIFC',directive_mac, @dir_elseif);
  5368. AddConditional('ENDC',directive_mac, @dir_endif);
  5369. end;
  5370. procedure DoneScanner;
  5371. begin
  5372. turbo_scannerdirectives.Free;
  5373. mac_scannerdirectives.Free;
  5374. DoneWideString(patternw);
  5375. end;
  5376. end.