scanner.pas 195 KB

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