scanner.pas 191 KB

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