scanner.pas 192 KB

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