gdscript_parser.cpp 228 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610
  1. /*************************************************************************/
  2. /* gdscript_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript_parser.h"
  31. #include "core/core_string_names.h"
  32. #include "core/engine.h"
  33. #include "core/project_settings.h"
  34. #include "core/reference.h"
  35. #include "gdscript.h"
  36. #include "io/resource_loader.h"
  37. #include "os/file_access.h"
  38. #include "print_string.h"
  39. #include "script_language.h"
  40. template <class T>
  41. T *GDScriptParser::alloc_node() {
  42. T *t = memnew(T);
  43. t->next = list;
  44. list = t;
  45. if (!head)
  46. head = t;
  47. t->line = tokenizer->get_token_line();
  48. t->column = tokenizer->get_token_column();
  49. return t;
  50. }
  51. bool GDScriptParser::_end_statement() {
  52. if (tokenizer->get_token() == GDScriptTokenizer::TK_SEMICOLON) {
  53. tokenizer->advance();
  54. return true; //handle next
  55. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE || tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  56. return true; //will be handled properly
  57. }
  58. return false;
  59. }
  60. bool GDScriptParser::_enter_indent_block(BlockNode *p_block) {
  61. if (tokenizer->get_token() != GDScriptTokenizer::TK_COLON) {
  62. // report location at the previous token (on the previous line)
  63. int error_line = tokenizer->get_token_line(-1);
  64. int error_column = tokenizer->get_token_column(-1);
  65. _set_error("':' expected at end of line.", error_line, error_column);
  66. return false;
  67. }
  68. tokenizer->advance();
  69. if (tokenizer->get_token() != GDScriptTokenizer::TK_NEWLINE) {
  70. // be more python-like
  71. int current = tab_level.back()->get();
  72. tab_level.push_back(current);
  73. return true;
  74. //_set_error("newline expected after ':'.");
  75. //return false;
  76. }
  77. while (true) {
  78. if (tokenizer->get_token() != GDScriptTokenizer::TK_NEWLINE) {
  79. return false; //wtf
  80. } else if (tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE) {
  81. int indent = tokenizer->get_token_line_indent();
  82. int current = tab_level.back()->get();
  83. if (indent <= current) {
  84. return false;
  85. }
  86. tab_level.push_back(indent);
  87. tokenizer->advance();
  88. return true;
  89. } else if (p_block) {
  90. NewLineNode *nl = alloc_node<NewLineNode>();
  91. nl->line = tokenizer->get_token_line();
  92. p_block->statements.push_back(nl);
  93. }
  94. tokenizer->advance(); // go to next newline
  95. }
  96. }
  97. bool GDScriptParser::_parse_arguments(Node *p_parent, Vector<Node *> &p_args, bool p_static, bool p_can_codecomplete) {
  98. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  99. tokenizer->advance();
  100. } else {
  101. parenthesis++;
  102. int argidx = 0;
  103. while (true) {
  104. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  105. _make_completable_call(argidx);
  106. completion_node = p_parent;
  107. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING && tokenizer->get_token(1) == GDScriptTokenizer::TK_CURSOR) {
  108. //completing a string argument..
  109. completion_cursor = tokenizer->get_token_constant();
  110. _make_completable_call(argidx);
  111. completion_node = p_parent;
  112. tokenizer->advance(1);
  113. return false;
  114. }
  115. Node *arg = _parse_expression(p_parent, p_static);
  116. if (!arg)
  117. return false;
  118. p_args.push_back(arg);
  119. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  120. tokenizer->advance();
  121. break;
  122. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  123. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  124. _set_error("Expression expected");
  125. return false;
  126. }
  127. tokenizer->advance();
  128. argidx++;
  129. } else {
  130. // something is broken
  131. _set_error("Expected ',' or ')'");
  132. return false;
  133. }
  134. }
  135. parenthesis--;
  136. }
  137. return true;
  138. }
  139. void GDScriptParser::_make_completable_call(int p_arg) {
  140. completion_cursor = StringName();
  141. completion_type = COMPLETION_CALL_ARGUMENTS;
  142. completion_class = current_class;
  143. completion_function = current_function;
  144. completion_line = tokenizer->get_token_line();
  145. completion_argument = p_arg;
  146. completion_block = current_block;
  147. completion_found = true;
  148. tokenizer->advance();
  149. }
  150. bool GDScriptParser::_get_completable_identifier(CompletionType p_type, StringName &identifier) {
  151. identifier = StringName();
  152. if (tokenizer->is_token_literal()) {
  153. identifier = tokenizer->get_token_literal();
  154. tokenizer->advance();
  155. }
  156. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  157. completion_cursor = identifier;
  158. completion_type = p_type;
  159. completion_class = current_class;
  160. completion_function = current_function;
  161. completion_line = tokenizer->get_token_line();
  162. completion_block = current_block;
  163. completion_found = true;
  164. completion_ident_is_call = false;
  165. tokenizer->advance();
  166. if (tokenizer->is_token_literal()) {
  167. identifier = identifier.operator String() + tokenizer->get_token_literal().operator String();
  168. tokenizer->advance();
  169. }
  170. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  171. completion_ident_is_call = true;
  172. }
  173. return true;
  174. }
  175. return false;
  176. }
  177. GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_static, bool p_allow_assign, bool p_parsing_constant) {
  178. //Vector<Node*> expressions;
  179. //Vector<OperatorNode::Operator> operators;
  180. Vector<Expression> expression;
  181. Node *expr = NULL;
  182. int op_line = tokenizer->get_token_line(); // when operators are created at the bottom, the line might have been changed (\n found)
  183. while (true) {
  184. /*****************/
  185. /* Parse Operand */
  186. /*****************/
  187. if (parenthesis > 0) {
  188. //remove empty space (only allowed if inside parenthesis
  189. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  190. tokenizer->advance();
  191. }
  192. }
  193. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  194. //subexpression ()
  195. tokenizer->advance();
  196. parenthesis++;
  197. Node *subexpr = _parse_expression(p_parent, p_static, p_allow_assign, p_parsing_constant);
  198. parenthesis--;
  199. if (!subexpr)
  200. return NULL;
  201. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  202. _set_error("Expected ')' in expression");
  203. return NULL;
  204. }
  205. tokenizer->advance();
  206. expr = subexpr;
  207. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_DOLLAR) {
  208. tokenizer->advance();
  209. String path;
  210. bool need_identifier = true;
  211. bool done = false;
  212. int line = tokenizer->get_token_line();
  213. while (!done) {
  214. switch (tokenizer->get_token()) {
  215. case GDScriptTokenizer::TK_CURSOR: {
  216. completion_cursor = StringName();
  217. completion_type = COMPLETION_GET_NODE;
  218. completion_class = current_class;
  219. completion_function = current_function;
  220. completion_line = tokenizer->get_token_line();
  221. completion_cursor = path;
  222. completion_argument = 0;
  223. completion_block = current_block;
  224. completion_found = true;
  225. tokenizer->advance();
  226. } break;
  227. case GDScriptTokenizer::TK_CONSTANT: {
  228. if (!need_identifier) {
  229. done = true;
  230. break;
  231. }
  232. if (tokenizer->get_token_constant().get_type() != Variant::STRING) {
  233. _set_error("Expected string constant or identifier after '$' or '/'.");
  234. return NULL;
  235. }
  236. path += String(tokenizer->get_token_constant());
  237. tokenizer->advance();
  238. need_identifier = false;
  239. } break;
  240. case GDScriptTokenizer::TK_OP_DIV: {
  241. if (need_identifier) {
  242. done = true;
  243. break;
  244. }
  245. path += "/";
  246. tokenizer->advance();
  247. need_identifier = true;
  248. } break;
  249. default: {
  250. // Instead of checking for TK_IDENTIFIER, we check with is_token_literal, as this allows us to use match/sync/etc. as a name
  251. if (need_identifier && tokenizer->is_token_literal()) {
  252. path += String(tokenizer->get_token_literal());
  253. tokenizer->advance();
  254. need_identifier = false;
  255. } else {
  256. done = true;
  257. }
  258. break;
  259. }
  260. }
  261. }
  262. if (path == "") {
  263. _set_error("Path expected after $.");
  264. return NULL;
  265. }
  266. OperatorNode *op = alloc_node<OperatorNode>();
  267. op->op = OperatorNode::OP_CALL;
  268. op->line = line;
  269. op->arguments.push_back(alloc_node<SelfNode>());
  270. op->arguments[0]->line = line;
  271. IdentifierNode *funcname = alloc_node<IdentifierNode>();
  272. funcname->name = "get_node";
  273. funcname->line = line;
  274. op->arguments.push_back(funcname);
  275. ConstantNode *nodepath = alloc_node<ConstantNode>();
  276. nodepath->value = NodePath(StringName(path));
  277. nodepath->datatype = _type_from_variant(nodepath->value);
  278. nodepath->line = line;
  279. op->arguments.push_back(nodepath);
  280. expr = op;
  281. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  282. tokenizer->advance();
  283. continue; //no point in cursor in the middle of expression
  284. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT) {
  285. //constant defined by tokenizer
  286. ConstantNode *constant = alloc_node<ConstantNode>();
  287. constant->value = tokenizer->get_token_constant();
  288. constant->datatype = _type_from_variant(constant->value);
  289. tokenizer->advance();
  290. expr = constant;
  291. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_PI) {
  292. //constant defined by tokenizer
  293. ConstantNode *constant = alloc_node<ConstantNode>();
  294. constant->value = Math_PI;
  295. constant->datatype = _type_from_variant(constant->value);
  296. tokenizer->advance();
  297. expr = constant;
  298. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_TAU) {
  299. //constant defined by tokenizer
  300. ConstantNode *constant = alloc_node<ConstantNode>();
  301. constant->value = Math_TAU;
  302. constant->datatype = _type_from_variant(constant->value);
  303. tokenizer->advance();
  304. expr = constant;
  305. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_INF) {
  306. //constant defined by tokenizer
  307. ConstantNode *constant = alloc_node<ConstantNode>();
  308. constant->value = Math_INF;
  309. constant->datatype = _type_from_variant(constant->value);
  310. tokenizer->advance();
  311. expr = constant;
  312. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_NAN) {
  313. //constant defined by tokenizer
  314. ConstantNode *constant = alloc_node<ConstantNode>();
  315. constant->value = Math_NAN;
  316. constant->datatype = _type_from_variant(constant->value);
  317. tokenizer->advance();
  318. expr = constant;
  319. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_PRELOAD) {
  320. //constant defined by tokenizer
  321. tokenizer->advance();
  322. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  323. _set_error("Expected '(' after 'preload'");
  324. return NULL;
  325. }
  326. tokenizer->advance();
  327. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  328. completion_cursor = StringName();
  329. completion_node = p_parent;
  330. completion_type = COMPLETION_RESOURCE_PATH;
  331. completion_class = current_class;
  332. completion_function = current_function;
  333. completion_line = tokenizer->get_token_line();
  334. completion_block = current_block;
  335. completion_argument = 0;
  336. completion_found = true;
  337. tokenizer->advance();
  338. }
  339. String path;
  340. bool found_constant = false;
  341. bool valid = false;
  342. ConstantNode *cn;
  343. Node *subexpr = _parse_and_reduce_expression(p_parent, p_static);
  344. if (subexpr) {
  345. if (subexpr->type == Node::TYPE_CONSTANT) {
  346. cn = static_cast<ConstantNode *>(subexpr);
  347. found_constant = true;
  348. }
  349. if (subexpr->type == Node::TYPE_IDENTIFIER) {
  350. IdentifierNode *in = static_cast<IdentifierNode *>(subexpr);
  351. // Try to find the constant expression by the identifier
  352. if (current_class->constant_expressions.has(in->name)) {
  353. Node *cn_exp = current_class->constant_expressions[in->name].expression;
  354. if (cn_exp->type == Node::TYPE_CONSTANT) {
  355. cn = static_cast<ConstantNode *>(cn_exp);
  356. found_constant = true;
  357. }
  358. }
  359. }
  360. if (found_constant && cn->value.get_type() == Variant::STRING) {
  361. valid = true;
  362. path = (String)cn->value;
  363. }
  364. }
  365. if (!valid) {
  366. _set_error("expected string constant as 'preload' argument.");
  367. return NULL;
  368. }
  369. if (!path.is_abs_path() && base_path != "")
  370. path = base_path + "/" + path;
  371. path = path.replace("///", "//").simplify_path();
  372. if (path == self_path) {
  373. _set_error("Can't preload itself (use 'get_script()').");
  374. return NULL;
  375. }
  376. Ref<Resource> res;
  377. if (!validating) {
  378. //this can be too slow for just validating code
  379. if (for_completion && ScriptCodeCompletionCache::get_singleton() && FileAccess::exists(path)) {
  380. res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
  381. } else if (!for_completion || FileAccess::exists(path)) {
  382. res = ResourceLoader::load(path);
  383. }
  384. } else {
  385. if (!FileAccess::exists(path)) {
  386. _set_error("Can't preload resource at path: " + path);
  387. return NULL;
  388. } else if (ScriptCodeCompletionCache::get_singleton()) {
  389. res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
  390. }
  391. }
  392. if (!res.is_valid()) {
  393. _set_error("Can't preload resource at path: " + path);
  394. return NULL;
  395. }
  396. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  397. _set_error("Expected ')' after 'preload' path");
  398. return NULL;
  399. }
  400. Ref<GDScript> gds = res;
  401. if (gds.is_valid() && !gds->is_valid()) {
  402. _set_error("Could not fully preload the script, possible cyclic reference or compilation error.");
  403. return NULL;
  404. }
  405. tokenizer->advance();
  406. ConstantNode *constant = alloc_node<ConstantNode>();
  407. constant->value = res;
  408. constant->datatype = _type_from_variant(constant->value);
  409. expr = constant;
  410. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_YIELD) {
  411. if (!current_function) {
  412. _set_error("yield() can only be used inside function blocks.");
  413. return NULL;
  414. }
  415. current_function->has_yield = true;
  416. tokenizer->advance();
  417. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  418. _set_error("Expected '(' after 'yield'");
  419. return NULL;
  420. }
  421. tokenizer->advance();
  422. OperatorNode *yield = alloc_node<OperatorNode>();
  423. yield->op = OperatorNode::OP_YIELD;
  424. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  425. tokenizer->advance();
  426. }
  427. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  428. expr = yield;
  429. tokenizer->advance();
  430. } else {
  431. parenthesis++;
  432. Node *object = _parse_and_reduce_expression(p_parent, p_static);
  433. if (!object)
  434. return NULL;
  435. yield->arguments.push_back(object);
  436. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  437. _set_error("Expected ',' after first argument of 'yield'");
  438. return NULL;
  439. }
  440. tokenizer->advance();
  441. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  442. completion_cursor = StringName();
  443. completion_node = object;
  444. completion_type = COMPLETION_YIELD;
  445. completion_class = current_class;
  446. completion_function = current_function;
  447. completion_line = tokenizer->get_token_line();
  448. completion_argument = 0;
  449. completion_block = current_block;
  450. completion_found = true;
  451. tokenizer->advance();
  452. }
  453. Node *signal = _parse_and_reduce_expression(p_parent, p_static);
  454. if (!signal)
  455. return NULL;
  456. yield->arguments.push_back(signal);
  457. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  458. _set_error("Expected ')' after second argument of 'yield'");
  459. return NULL;
  460. }
  461. parenthesis--;
  462. tokenizer->advance();
  463. expr = yield;
  464. }
  465. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_SELF) {
  466. if (p_static) {
  467. _set_error("'self'' not allowed in static function or constant expression");
  468. return NULL;
  469. }
  470. //constant defined by tokenizer
  471. SelfNode *self = alloc_node<SelfNode>();
  472. tokenizer->advance();
  473. expr = self;
  474. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) {
  475. Variant::Type bi_type = tokenizer->get_token_type();
  476. tokenizer->advance(2);
  477. StringName identifier;
  478. if (_get_completable_identifier(COMPLETION_BUILT_IN_TYPE_CONSTANT, identifier)) {
  479. completion_built_in_constant = bi_type;
  480. }
  481. if (identifier == StringName()) {
  482. _set_error("Built-in type constant or static function expected after '.'");
  483. return NULL;
  484. }
  485. if (!Variant::has_numeric_constant(bi_type, identifier)) {
  486. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN &&
  487. Variant::is_method_const(bi_type, identifier) &&
  488. Variant::get_method_return_type(bi_type, identifier) == bi_type) {
  489. tokenizer->advance();
  490. OperatorNode *construct = alloc_node<OperatorNode>();
  491. construct->op = OperatorNode::OP_CALL;
  492. TypeNode *tn = alloc_node<TypeNode>();
  493. tn->vtype = bi_type;
  494. construct->arguments.push_back(tn);
  495. OperatorNode *op = alloc_node<OperatorNode>();
  496. op->op = OperatorNode::OP_CALL;
  497. op->arguments.push_back(construct);
  498. IdentifierNode *id = alloc_node<IdentifierNode>();
  499. id->name = identifier;
  500. op->arguments.push_back(id);
  501. if (!_parse_arguments(op, op->arguments, p_static, true))
  502. return NULL;
  503. expr = op;
  504. } else {
  505. _set_error("Static constant '" + identifier.operator String() + "' not present in built-in type " + Variant::get_type_name(bi_type) + ".");
  506. return NULL;
  507. }
  508. } else {
  509. ConstantNode *cn = alloc_node<ConstantNode>();
  510. cn->value = Variant::get_numeric_constant_value(bi_type, identifier);
  511. cn->datatype = _type_from_variant(cn->value);
  512. expr = cn;
  513. }
  514. } else if (tokenizer->get_token(1) == GDScriptTokenizer::TK_PARENTHESIS_OPEN && tokenizer->is_token_literal()) {
  515. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  516. //function or constructor
  517. OperatorNode *op = alloc_node<OperatorNode>();
  518. op->op = OperatorNode::OP_CALL;
  519. //Do a quick Array and Dictionary Check. Replace if either require no arguments.
  520. bool replaced = false;
  521. if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE) {
  522. Variant::Type ct = tokenizer->get_token_type();
  523. if (p_parsing_constant == false) {
  524. if (ct == Variant::ARRAY) {
  525. if (tokenizer->get_token(2) == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  526. ArrayNode *arr = alloc_node<ArrayNode>();
  527. expr = arr;
  528. replaced = true;
  529. tokenizer->advance(3);
  530. }
  531. }
  532. if (ct == Variant::DICTIONARY) {
  533. if (tokenizer->get_token(2) == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  534. DictionaryNode *dict = alloc_node<DictionaryNode>();
  535. expr = dict;
  536. replaced = true;
  537. tokenizer->advance(3);
  538. }
  539. }
  540. }
  541. if (!replaced) {
  542. TypeNode *tn = alloc_node<TypeNode>();
  543. tn->vtype = tokenizer->get_token_type();
  544. op->arguments.push_back(tn);
  545. tokenizer->advance(2);
  546. }
  547. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_FUNC) {
  548. BuiltInFunctionNode *bn = alloc_node<BuiltInFunctionNode>();
  549. bn->function = tokenizer->get_token_built_in_func();
  550. op->arguments.push_back(bn);
  551. tokenizer->advance(2);
  552. } else {
  553. SelfNode *self = alloc_node<SelfNode>();
  554. op->arguments.push_back(self);
  555. StringName identifier;
  556. if (_get_completable_identifier(COMPLETION_FUNCTION, identifier)) {
  557. }
  558. IdentifierNode *id = alloc_node<IdentifierNode>();
  559. id->name = identifier;
  560. op->arguments.push_back(id);
  561. tokenizer->advance(1);
  562. }
  563. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  564. _make_completable_call(0);
  565. completion_node = op;
  566. }
  567. if (!replaced) {
  568. if (!_parse_arguments(op, op->arguments, p_static, true))
  569. return NULL;
  570. expr = op;
  571. }
  572. } else if (tokenizer->is_token_literal(0, true)) {
  573. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  574. //identifier (reference)
  575. const ClassNode *cln = current_class;
  576. bool bfn = false;
  577. StringName identifier;
  578. int id_line = tokenizer->get_token_line();
  579. if (_get_completable_identifier(COMPLETION_IDENTIFIER, identifier)) {
  580. }
  581. BlockNode *b = current_block;
  582. while (b) {
  583. if (b->variables.has(identifier)) {
  584. IdentifierNode *id = alloc_node<IdentifierNode>();
  585. LocalVarNode *lv = b->variables[identifier];
  586. id->name = identifier;
  587. id->declared_block = b;
  588. id->line = id_line;
  589. expr = id;
  590. bfn = true;
  591. switch (tokenizer->get_token()) {
  592. case GDScriptTokenizer::TK_OP_ASSIGN_ADD:
  593. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_AND:
  594. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_OR:
  595. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_XOR:
  596. case GDScriptTokenizer::TK_OP_ASSIGN_DIV:
  597. case GDScriptTokenizer::TK_OP_ASSIGN_MOD:
  598. case GDScriptTokenizer::TK_OP_ASSIGN_MUL:
  599. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_LEFT:
  600. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_RIGHT:
  601. case GDScriptTokenizer::TK_OP_ASSIGN_SUB: {
  602. if (lv->assignments == 0 && !lv->datatype.has_type) {
  603. _set_error("Using assignment with operation on a variable that was never assigned.");
  604. return NULL;
  605. }
  606. } // fallthrough
  607. case GDScriptTokenizer::TK_OP_ASSIGN: {
  608. lv->assignments += 1;
  609. }
  610. }
  611. break;
  612. }
  613. b = b->parent_block;
  614. }
  615. if (!bfn && p_parsing_constant) {
  616. if (cln->constant_expressions.has(identifier)) {
  617. expr = cln->constant_expressions[identifier].expression;
  618. bfn = true;
  619. } else if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) {
  620. //check from constants
  621. ConstantNode *constant = alloc_node<ConstantNode>();
  622. constant->value = GDScriptLanguage::get_singleton()->get_global_array()[GDScriptLanguage::get_singleton()->get_global_map()[identifier]];
  623. constant->datatype = _type_from_variant(constant->value);
  624. constant->line = id_line;
  625. expr = constant;
  626. bfn = true;
  627. }
  628. if (!bfn && GDScriptLanguage::get_singleton()->get_named_globals_map().has(identifier)) {
  629. //check from singletons
  630. ConstantNode *constant = alloc_node<ConstantNode>();
  631. constant->value = GDScriptLanguage::get_singleton()->get_named_globals_map()[identifier];
  632. expr = constant;
  633. bfn = true;
  634. }
  635. }
  636. if (!bfn) {
  637. IdentifierNode *id = alloc_node<IdentifierNode>();
  638. id->name = identifier;
  639. id->line = id_line;
  640. expr = id;
  641. }
  642. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ADD || tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB || tokenizer->get_token() == GDScriptTokenizer::TK_OP_NOT || tokenizer->get_token() == GDScriptTokenizer::TK_OP_BIT_INVERT) {
  643. //single prefix operators like !expr +expr -expr ++expr --expr
  644. alloc_node<OperatorNode>();
  645. Expression e;
  646. e.is_op = true;
  647. switch (tokenizer->get_token()) {
  648. case GDScriptTokenizer::TK_OP_ADD: e.op = OperatorNode::OP_POS; break;
  649. case GDScriptTokenizer::TK_OP_SUB: e.op = OperatorNode::OP_NEG; break;
  650. case GDScriptTokenizer::TK_OP_NOT: e.op = OperatorNode::OP_NOT; break;
  651. case GDScriptTokenizer::TK_OP_BIT_INVERT: e.op = OperatorNode::OP_BIT_INVERT; break;
  652. default: {}
  653. }
  654. tokenizer->advance();
  655. if (e.op != OperatorNode::OP_NOT && tokenizer->get_token() == GDScriptTokenizer::TK_OP_NOT) {
  656. _set_error("Misplaced 'not'.");
  657. return NULL;
  658. }
  659. expression.push_back(e);
  660. continue; //only exception, must continue...
  661. /*
  662. Node *subexpr=_parse_expression(op,p_static);
  663. if (!subexpr)
  664. return NULL;
  665. op->arguments.push_back(subexpr);
  666. expr=op;*/
  667. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_OPEN) {
  668. // array
  669. tokenizer->advance();
  670. ArrayNode *arr = alloc_node<ArrayNode>();
  671. bool expecting_comma = false;
  672. while (true) {
  673. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  674. _set_error("Unterminated array");
  675. return NULL;
  676. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  677. tokenizer->advance();
  678. break;
  679. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  680. tokenizer->advance(); //ignore newline
  681. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  682. if (!expecting_comma) {
  683. _set_error("expression or ']' expected");
  684. return NULL;
  685. }
  686. expecting_comma = false;
  687. tokenizer->advance(); //ignore newline
  688. } else {
  689. //parse expression
  690. if (expecting_comma) {
  691. _set_error("',' or ']' expected");
  692. return NULL;
  693. }
  694. Node *n = _parse_expression(arr, p_static, p_allow_assign, p_parsing_constant);
  695. if (!n)
  696. return NULL;
  697. arr->elements.push_back(n);
  698. expecting_comma = true;
  699. }
  700. }
  701. expr = arr;
  702. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_OPEN) {
  703. // array
  704. tokenizer->advance();
  705. DictionaryNode *dict = alloc_node<DictionaryNode>();
  706. enum DictExpect {
  707. DICT_EXPECT_KEY,
  708. DICT_EXPECT_COLON,
  709. DICT_EXPECT_VALUE,
  710. DICT_EXPECT_COMMA
  711. };
  712. Node *key = NULL;
  713. Set<Variant> keys;
  714. DictExpect expecting = DICT_EXPECT_KEY;
  715. while (true) {
  716. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  717. _set_error("Unterminated dictionary");
  718. return NULL;
  719. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  720. if (expecting == DICT_EXPECT_COLON) {
  721. _set_error("':' expected");
  722. return NULL;
  723. }
  724. if (expecting == DICT_EXPECT_VALUE) {
  725. _set_error("value expected");
  726. return NULL;
  727. }
  728. tokenizer->advance();
  729. break;
  730. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  731. tokenizer->advance(); //ignore newline
  732. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  733. if (expecting == DICT_EXPECT_KEY) {
  734. _set_error("key or '}' expected");
  735. return NULL;
  736. }
  737. if (expecting == DICT_EXPECT_VALUE) {
  738. _set_error("value expected");
  739. return NULL;
  740. }
  741. if (expecting == DICT_EXPECT_COLON) {
  742. _set_error("':' expected");
  743. return NULL;
  744. }
  745. expecting = DICT_EXPECT_KEY;
  746. tokenizer->advance(); //ignore newline
  747. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  748. if (expecting == DICT_EXPECT_KEY) {
  749. _set_error("key or '}' expected");
  750. return NULL;
  751. }
  752. if (expecting == DICT_EXPECT_VALUE) {
  753. _set_error("value expected");
  754. return NULL;
  755. }
  756. if (expecting == DICT_EXPECT_COMMA) {
  757. _set_error("',' or '}' expected");
  758. return NULL;
  759. }
  760. expecting = DICT_EXPECT_VALUE;
  761. tokenizer->advance(); //ignore newline
  762. } else {
  763. if (expecting == DICT_EXPECT_COMMA) {
  764. _set_error("',' or '}' expected");
  765. return NULL;
  766. }
  767. if (expecting == DICT_EXPECT_COLON) {
  768. _set_error("':' expected");
  769. return NULL;
  770. }
  771. if (expecting == DICT_EXPECT_KEY) {
  772. if (tokenizer->is_token_literal() && tokenizer->get_token(1) == GDScriptTokenizer::TK_OP_ASSIGN) {
  773. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  774. //lua style identifier, easier to write
  775. ConstantNode *cn = alloc_node<ConstantNode>();
  776. cn->value = tokenizer->get_token_literal();
  777. cn->datatype = _type_from_variant(cn->value);
  778. key = cn;
  779. tokenizer->advance(2);
  780. expecting = DICT_EXPECT_VALUE;
  781. } else {
  782. //python/js style more flexible
  783. key = _parse_expression(dict, p_static, p_allow_assign, p_parsing_constant);
  784. if (!key)
  785. return NULL;
  786. expecting = DICT_EXPECT_COLON;
  787. }
  788. }
  789. if (expecting == DICT_EXPECT_VALUE) {
  790. Node *value = _parse_expression(dict, p_static, p_allow_assign, p_parsing_constant);
  791. if (!value)
  792. return NULL;
  793. expecting = DICT_EXPECT_COMMA;
  794. if (key->type == GDScriptParser::Node::TYPE_CONSTANT) {
  795. Variant const &keyName = static_cast<const GDScriptParser::ConstantNode *>(key)->value;
  796. if (keys.has(keyName)) {
  797. _set_error("Duplicate key found in Dictionary literal");
  798. return NULL;
  799. }
  800. keys.insert(keyName);
  801. }
  802. DictionaryNode::Pair pair;
  803. pair.key = key;
  804. pair.value = value;
  805. dict->elements.push_back(pair);
  806. key = NULL;
  807. }
  808. }
  809. }
  810. expr = dict;
  811. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD && (tokenizer->is_token_literal(1) || tokenizer->get_token(1) == GDScriptTokenizer::TK_CURSOR) && tokenizer->get_token(2) == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  812. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  813. // parent call
  814. tokenizer->advance(); //goto identifier
  815. OperatorNode *op = alloc_node<OperatorNode>();
  816. op->op = OperatorNode::OP_PARENT_CALL;
  817. /*SelfNode *self = alloc_node<SelfNode>();
  818. op->arguments.push_back(self);
  819. forbidden for now */
  820. StringName identifier;
  821. if (_get_completable_identifier(COMPLETION_PARENT_FUNCTION, identifier)) {
  822. //indexing stuff
  823. }
  824. IdentifierNode *id = alloc_node<IdentifierNode>();
  825. id->name = identifier;
  826. op->arguments.push_back(id);
  827. tokenizer->advance(1);
  828. if (!_parse_arguments(op, op->arguments, p_static))
  829. return NULL;
  830. expr = op;
  831. } else {
  832. //find list [ or find dictionary {
  833. //print_line("found bug?");
  834. _set_error("Error parsing expression, misplaced: " + String(tokenizer->get_token_name(tokenizer->get_token())));
  835. return NULL; //nothing
  836. }
  837. if (!expr) {
  838. ERR_EXPLAIN("GDScriptParser bug, couldn't figure out what expression is...");
  839. ERR_FAIL_COND_V(!expr, NULL);
  840. }
  841. /******************/
  842. /* Parse Indexing */
  843. /******************/
  844. while (true) {
  845. //expressions can be indexed any number of times
  846. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
  847. //indexing using "."
  848. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_CURSOR && !tokenizer->is_token_literal(1)) {
  849. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  850. _set_error("Expected identifier as member");
  851. return NULL;
  852. } else if (tokenizer->get_token(2) == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  853. //call!!
  854. OperatorNode *op = alloc_node<OperatorNode>();
  855. op->op = OperatorNode::OP_CALL;
  856. tokenizer->advance();
  857. IdentifierNode *id = alloc_node<IdentifierNode>();
  858. if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_FUNC) {
  859. //small hack so built in funcs don't obfuscate methods
  860. id->name = GDScriptFunctions::get_func_name(tokenizer->get_token_built_in_func());
  861. tokenizer->advance();
  862. } else {
  863. StringName identifier;
  864. if (_get_completable_identifier(COMPLETION_METHOD, identifier)) {
  865. completion_node = op;
  866. //indexing stuff
  867. }
  868. id->name = identifier;
  869. }
  870. op->arguments.push_back(expr); // call what
  871. op->arguments.push_back(id); // call func
  872. //get arguments
  873. tokenizer->advance(1);
  874. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  875. _make_completable_call(0);
  876. completion_node = op;
  877. }
  878. if (!_parse_arguments(op, op->arguments, p_static, true))
  879. return NULL;
  880. expr = op;
  881. } else {
  882. //simple indexing!
  883. OperatorNode *op = alloc_node<OperatorNode>();
  884. op->op = OperatorNode::OP_INDEX_NAMED;
  885. tokenizer->advance();
  886. StringName identifier;
  887. if (_get_completable_identifier(COMPLETION_INDEX, identifier)) {
  888. if (identifier == StringName()) {
  889. identifier = "@temp"; //so it parses allright
  890. }
  891. completion_node = op;
  892. //indexing stuff
  893. }
  894. IdentifierNode *id = alloc_node<IdentifierNode>();
  895. id->name = identifier;
  896. op->arguments.push_back(expr);
  897. op->arguments.push_back(id);
  898. expr = op;
  899. }
  900. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_OPEN) {
  901. //indexing using "[]"
  902. OperatorNode *op = alloc_node<OperatorNode>();
  903. op->op = OperatorNode::OP_INDEX;
  904. tokenizer->advance(1);
  905. Node *subexpr = _parse_expression(op, p_static, p_allow_assign, p_parsing_constant);
  906. if (!subexpr) {
  907. return NULL;
  908. }
  909. if (tokenizer->get_token() != GDScriptTokenizer::TK_BRACKET_CLOSE) {
  910. _set_error("Expected ']'");
  911. return NULL;
  912. }
  913. op->arguments.push_back(expr);
  914. op->arguments.push_back(subexpr);
  915. tokenizer->advance(1);
  916. expr = op;
  917. } else
  918. break;
  919. }
  920. /*****************/
  921. /* Parse Casting */
  922. /*****************/
  923. bool has_casting = expr->type == Node::TYPE_CAST;
  924. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_AS) {
  925. if (has_casting) {
  926. _set_error("Unexpected 'as'.");
  927. return NULL;
  928. }
  929. CastNode *cn = alloc_node<CastNode>();
  930. if (!_parse_type(cn->cast_type)) {
  931. _set_error("Expected type after 'as'.");
  932. return NULL;
  933. }
  934. has_casting = true;
  935. cn->source_node = expr;
  936. expr = cn;
  937. }
  938. /******************/
  939. /* Parse Operator */
  940. /******************/
  941. if (parenthesis > 0) {
  942. //remove empty space (only allowed if inside parenthesis
  943. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  944. tokenizer->advance();
  945. }
  946. }
  947. Expression e;
  948. e.is_op = false;
  949. e.node = expr;
  950. expression.push_back(e);
  951. // determine which operator is next
  952. OperatorNode::Operator op;
  953. bool valid = true;
  954. //assign, if allowed is only allowed on the first operator
  955. #define _VALIDATE_ASSIGN \
  956. if (!p_allow_assign || has_casting) { \
  957. _set_error("Unexpected assign."); \
  958. return NULL; \
  959. } \
  960. p_allow_assign = false;
  961. switch (tokenizer->get_token()) { //see operator
  962. case GDScriptTokenizer::TK_OP_IN: op = OperatorNode::OP_IN; break;
  963. case GDScriptTokenizer::TK_OP_EQUAL: op = OperatorNode::OP_EQUAL; break;
  964. case GDScriptTokenizer::TK_OP_NOT_EQUAL: op = OperatorNode::OP_NOT_EQUAL; break;
  965. case GDScriptTokenizer::TK_OP_LESS: op = OperatorNode::OP_LESS; break;
  966. case GDScriptTokenizer::TK_OP_LESS_EQUAL: op = OperatorNode::OP_LESS_EQUAL; break;
  967. case GDScriptTokenizer::TK_OP_GREATER: op = OperatorNode::OP_GREATER; break;
  968. case GDScriptTokenizer::TK_OP_GREATER_EQUAL: op = OperatorNode::OP_GREATER_EQUAL; break;
  969. case GDScriptTokenizer::TK_OP_AND: op = OperatorNode::OP_AND; break;
  970. case GDScriptTokenizer::TK_OP_OR: op = OperatorNode::OP_OR; break;
  971. case GDScriptTokenizer::TK_OP_ADD: op = OperatorNode::OP_ADD; break;
  972. case GDScriptTokenizer::TK_OP_SUB: op = OperatorNode::OP_SUB; break;
  973. case GDScriptTokenizer::TK_OP_MUL: op = OperatorNode::OP_MUL; break;
  974. case GDScriptTokenizer::TK_OP_DIV: op = OperatorNode::OP_DIV; break;
  975. case GDScriptTokenizer::TK_OP_MOD:
  976. op = OperatorNode::OP_MOD;
  977. break;
  978. //case GDScriptTokenizer::TK_OP_NEG: op=OperatorNode::OP_NEG ; break;
  979. case GDScriptTokenizer::TK_OP_SHIFT_LEFT: op = OperatorNode::OP_SHIFT_LEFT; break;
  980. case GDScriptTokenizer::TK_OP_SHIFT_RIGHT: op = OperatorNode::OP_SHIFT_RIGHT; break;
  981. case GDScriptTokenizer::TK_OP_ASSIGN: {
  982. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN;
  983. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_CURSOR) {
  984. //code complete assignment
  985. completion_type = COMPLETION_ASSIGN;
  986. completion_node = expr;
  987. completion_class = current_class;
  988. completion_function = current_function;
  989. completion_line = tokenizer->get_token_line();
  990. completion_block = current_block;
  991. completion_found = true;
  992. tokenizer->advance();
  993. }
  994. } break;
  995. case GDScriptTokenizer::TK_OP_ASSIGN_ADD: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_ADD; break;
  996. case GDScriptTokenizer::TK_OP_ASSIGN_SUB: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SUB; break;
  997. case GDScriptTokenizer::TK_OP_ASSIGN_MUL: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_MUL; break;
  998. case GDScriptTokenizer::TK_OP_ASSIGN_DIV: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_DIV; break;
  999. case GDScriptTokenizer::TK_OP_ASSIGN_MOD: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_MOD; break;
  1000. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_LEFT: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SHIFT_LEFT; break;
  1001. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_RIGHT: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SHIFT_RIGHT; break;
  1002. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_AND: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_AND; break;
  1003. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_OR: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_OR; break;
  1004. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_XOR: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_XOR; break;
  1005. case GDScriptTokenizer::TK_OP_BIT_AND: op = OperatorNode::OP_BIT_AND; break;
  1006. case GDScriptTokenizer::TK_OP_BIT_OR: op = OperatorNode::OP_BIT_OR; break;
  1007. case GDScriptTokenizer::TK_OP_BIT_XOR: op = OperatorNode::OP_BIT_XOR; break;
  1008. case GDScriptTokenizer::TK_PR_IS: op = OperatorNode::OP_IS; break;
  1009. case GDScriptTokenizer::TK_CF_IF: op = OperatorNode::OP_TERNARY_IF; break;
  1010. case GDScriptTokenizer::TK_CF_ELSE: op = OperatorNode::OP_TERNARY_ELSE; break;
  1011. default: valid = false; break;
  1012. }
  1013. if (valid) {
  1014. e.is_op = true;
  1015. e.op = op;
  1016. expression.push_back(e);
  1017. tokenizer->advance();
  1018. } else {
  1019. break;
  1020. }
  1021. }
  1022. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  1023. while (expression.size() > 1) {
  1024. int next_op = -1;
  1025. int min_priority = 0xFFFFF;
  1026. bool is_unary = false;
  1027. bool is_ternary = false;
  1028. for (int i = 0; i < expression.size(); i++) {
  1029. if (!expression[i].is_op) {
  1030. continue;
  1031. }
  1032. int priority;
  1033. bool unary = false;
  1034. bool ternary = false;
  1035. bool error = false;
  1036. bool right_to_left = false;
  1037. switch (expression[i].op) {
  1038. case OperatorNode::OP_IS:
  1039. priority = -1;
  1040. break; //before anything
  1041. case OperatorNode::OP_BIT_INVERT:
  1042. priority = 0;
  1043. unary = true;
  1044. break;
  1045. case OperatorNode::OP_NEG:
  1046. priority = 1;
  1047. unary = true;
  1048. break;
  1049. case OperatorNode::OP_POS:
  1050. priority = 1;
  1051. unary = true;
  1052. break;
  1053. case OperatorNode::OP_MUL: priority = 2; break;
  1054. case OperatorNode::OP_DIV: priority = 2; break;
  1055. case OperatorNode::OP_MOD: priority = 2; break;
  1056. case OperatorNode::OP_ADD: priority = 3; break;
  1057. case OperatorNode::OP_SUB: priority = 3; break;
  1058. case OperatorNode::OP_SHIFT_LEFT: priority = 4; break;
  1059. case OperatorNode::OP_SHIFT_RIGHT: priority = 4; break;
  1060. case OperatorNode::OP_BIT_AND: priority = 5; break;
  1061. case OperatorNode::OP_BIT_XOR: priority = 6; break;
  1062. case OperatorNode::OP_BIT_OR: priority = 7; break;
  1063. case OperatorNode::OP_LESS: priority = 8; break;
  1064. case OperatorNode::OP_LESS_EQUAL: priority = 8; break;
  1065. case OperatorNode::OP_GREATER: priority = 8; break;
  1066. case OperatorNode::OP_GREATER_EQUAL: priority = 8; break;
  1067. case OperatorNode::OP_EQUAL: priority = 8; break;
  1068. case OperatorNode::OP_NOT_EQUAL: priority = 8; break;
  1069. case OperatorNode::OP_IN: priority = 10; break;
  1070. case OperatorNode::OP_NOT:
  1071. priority = 11;
  1072. unary = true;
  1073. break;
  1074. case OperatorNode::OP_AND: priority = 12; break;
  1075. case OperatorNode::OP_OR: priority = 13; break;
  1076. case OperatorNode::OP_TERNARY_IF:
  1077. priority = 14;
  1078. ternary = true;
  1079. right_to_left = true;
  1080. break;
  1081. case OperatorNode::OP_TERNARY_ELSE:
  1082. priority = 14;
  1083. error = true;
  1084. // Rigth-to-left should be false in this case, otherwise it would always error.
  1085. break;
  1086. case OperatorNode::OP_ASSIGN: priority = 15; break;
  1087. case OperatorNode::OP_ASSIGN_ADD: priority = 15; break;
  1088. case OperatorNode::OP_ASSIGN_SUB: priority = 15; break;
  1089. case OperatorNode::OP_ASSIGN_MUL: priority = 15; break;
  1090. case OperatorNode::OP_ASSIGN_DIV: priority = 15; break;
  1091. case OperatorNode::OP_ASSIGN_MOD: priority = 15; break;
  1092. case OperatorNode::OP_ASSIGN_SHIFT_LEFT: priority = 15; break;
  1093. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT: priority = 15; break;
  1094. case OperatorNode::OP_ASSIGN_BIT_AND: priority = 15; break;
  1095. case OperatorNode::OP_ASSIGN_BIT_OR: priority = 15; break;
  1096. case OperatorNode::OP_ASSIGN_BIT_XOR: priority = 15; break;
  1097. default: {
  1098. _set_error("GDScriptParser bug, invalid operator in expression: " + itos(expression[i].op));
  1099. return NULL;
  1100. }
  1101. }
  1102. if (priority < min_priority || (right_to_left && priority == min_priority)) {
  1103. // < is used for left to right (default)
  1104. // <= is used for right to left
  1105. if (error) {
  1106. _set_error("Unexpected operator");
  1107. return NULL;
  1108. }
  1109. next_op = i;
  1110. min_priority = priority;
  1111. is_unary = unary;
  1112. is_ternary = ternary;
  1113. }
  1114. }
  1115. if (next_op == -1) {
  1116. _set_error("Yet another parser bug....");
  1117. ERR_FAIL_COND_V(next_op == -1, NULL);
  1118. }
  1119. // OK! create operator..
  1120. if (is_unary) {
  1121. int expr_pos = next_op;
  1122. while (expression[expr_pos].is_op) {
  1123. expr_pos++;
  1124. if (expr_pos == expression.size()) {
  1125. //can happen..
  1126. _set_error("Unexpected end of expression...");
  1127. return NULL;
  1128. }
  1129. }
  1130. //consecutively do unary opeators
  1131. for (int i = expr_pos - 1; i >= next_op; i--) {
  1132. OperatorNode *op = alloc_node<OperatorNode>();
  1133. op->op = expression[i].op;
  1134. op->arguments.push_back(expression[i + 1].node);
  1135. op->line = op_line; //line might have been changed from a \n
  1136. expression[i].is_op = false;
  1137. expression[i].node = op;
  1138. expression.remove(i + 1);
  1139. }
  1140. } else if (is_ternary) {
  1141. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  1142. _set_error("Parser bug...");
  1143. ERR_FAIL_V(NULL);
  1144. }
  1145. if (next_op >= (expression.size() - 2) || expression[next_op + 2].op != OperatorNode::OP_TERNARY_ELSE) {
  1146. _set_error("Expected else after ternary if.");
  1147. return NULL;
  1148. }
  1149. if (next_op >= (expression.size() - 3)) {
  1150. _set_error("Expected value after ternary else.");
  1151. return NULL;
  1152. }
  1153. OperatorNode *op = alloc_node<OperatorNode>();
  1154. op->op = expression[next_op].op;
  1155. op->line = op_line; //line might have been changed from a \n
  1156. if (expression[next_op - 1].is_op) {
  1157. _set_error("Parser bug...");
  1158. ERR_FAIL_V(NULL);
  1159. }
  1160. if (expression[next_op + 1].is_op) {
  1161. // this is not invalid and can really appear
  1162. // but it becomes invalid anyway because no binary op
  1163. // can be followed by a unary op in a valid combination,
  1164. // due to how precedence works, unaries will always disappear first
  1165. _set_error("Unexpected two consecutive operators after ternary if.");
  1166. return NULL;
  1167. }
  1168. if (expression[next_op + 3].is_op) {
  1169. // this is not invalid and can really appear
  1170. // but it becomes invalid anyway because no binary op
  1171. // can be followed by a unary op in a valid combination,
  1172. // due to how precedence works, unaries will always disappear first
  1173. _set_error("Unexpected two consecutive operators after ternary else.");
  1174. return NULL;
  1175. }
  1176. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as first
  1177. op->arguments.push_back(expression[next_op - 1].node); //left expression goes as when-true
  1178. op->arguments.push_back(expression[next_op + 3].node); //expression after next goes as when-false
  1179. //replace all 3 nodes by this operator and make it an expression
  1180. expression[next_op - 1].node = op;
  1181. expression.remove(next_op);
  1182. expression.remove(next_op);
  1183. expression.remove(next_op);
  1184. expression.remove(next_op);
  1185. } else {
  1186. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  1187. _set_error("Parser bug...");
  1188. ERR_FAIL_V(NULL);
  1189. }
  1190. OperatorNode *op = alloc_node<OperatorNode>();
  1191. op->op = expression[next_op].op;
  1192. op->line = op_line; //line might have been changed from a \n
  1193. if (expression[next_op - 1].is_op) {
  1194. _set_error("Parser bug...");
  1195. ERR_FAIL_V(NULL);
  1196. }
  1197. if (expression[next_op + 1].is_op) {
  1198. // this is not invalid and can really appear
  1199. // but it becomes invalid anyway because no binary op
  1200. // can be followed by a unary op in a valid combination,
  1201. // due to how precedence works, unaries will always disappear first
  1202. _set_error("Unexpected two consecutive operators.");
  1203. return NULL;
  1204. }
  1205. op->arguments.push_back(expression[next_op - 1].node); //expression goes as left
  1206. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as right
  1207. //replace all 3 nodes by this operator and make it an expression
  1208. expression[next_op - 1].node = op;
  1209. expression.remove(next_op);
  1210. expression.remove(next_op);
  1211. }
  1212. }
  1213. return expression[0].node;
  1214. }
  1215. GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to_const) {
  1216. switch (p_node->type) {
  1217. case Node::TYPE_BUILT_IN_FUNCTION: {
  1218. //many may probably be optimizable
  1219. return p_node;
  1220. } break;
  1221. case Node::TYPE_ARRAY: {
  1222. ArrayNode *an = static_cast<ArrayNode *>(p_node);
  1223. bool all_constants = true;
  1224. for (int i = 0; i < an->elements.size(); i++) {
  1225. an->elements[i] = _reduce_expression(an->elements[i], p_to_const);
  1226. if (an->elements[i]->type != Node::TYPE_CONSTANT)
  1227. all_constants = false;
  1228. }
  1229. if (all_constants && p_to_const) {
  1230. //reduce constant array expression
  1231. ConstantNode *cn = alloc_node<ConstantNode>();
  1232. Array arr;
  1233. arr.resize(an->elements.size());
  1234. for (int i = 0; i < an->elements.size(); i++) {
  1235. ConstantNode *acn = static_cast<ConstantNode *>(an->elements[i]);
  1236. arr[i] = acn->value;
  1237. }
  1238. cn->value = arr;
  1239. cn->datatype = _type_from_variant(cn->value);
  1240. return cn;
  1241. }
  1242. return an;
  1243. } break;
  1244. case Node::TYPE_DICTIONARY: {
  1245. DictionaryNode *dn = static_cast<DictionaryNode *>(p_node);
  1246. bool all_constants = true;
  1247. for (int i = 0; i < dn->elements.size(); i++) {
  1248. dn->elements[i].key = _reduce_expression(dn->elements[i].key, p_to_const);
  1249. if (dn->elements[i].key->type != Node::TYPE_CONSTANT)
  1250. all_constants = false;
  1251. dn->elements[i].value = _reduce_expression(dn->elements[i].value, p_to_const);
  1252. if (dn->elements[i].value->type != Node::TYPE_CONSTANT)
  1253. all_constants = false;
  1254. }
  1255. if (all_constants && p_to_const) {
  1256. //reduce constant array expression
  1257. ConstantNode *cn = alloc_node<ConstantNode>();
  1258. Dictionary dict;
  1259. for (int i = 0; i < dn->elements.size(); i++) {
  1260. ConstantNode *key_c = static_cast<ConstantNode *>(dn->elements[i].key);
  1261. ConstantNode *value_c = static_cast<ConstantNode *>(dn->elements[i].value);
  1262. dict[key_c->value] = value_c->value;
  1263. }
  1264. cn->value = dict;
  1265. cn->datatype = _type_from_variant(cn->value);
  1266. return cn;
  1267. }
  1268. return dn;
  1269. } break;
  1270. case Node::TYPE_OPERATOR: {
  1271. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  1272. bool all_constants = true;
  1273. int last_not_constant = -1;
  1274. for (int i = 0; i < op->arguments.size(); i++) {
  1275. op->arguments[i] = _reduce_expression(op->arguments[i], p_to_const);
  1276. if (op->arguments[i]->type != Node::TYPE_CONSTANT) {
  1277. all_constants = false;
  1278. last_not_constant = i;
  1279. }
  1280. }
  1281. if (op->op == OperatorNode::OP_IS) {
  1282. //nothing much
  1283. return op;
  1284. }
  1285. if (op->op == OperatorNode::OP_PARENT_CALL) {
  1286. //nothing much
  1287. return op;
  1288. } else if (op->op == OperatorNode::OP_CALL) {
  1289. //can reduce base type constructors
  1290. if ((op->arguments[0]->type == Node::TYPE_TYPE || (op->arguments[0]->type == Node::TYPE_BUILT_IN_FUNCTION && GDScriptFunctions::is_deterministic(static_cast<BuiltInFunctionNode *>(op->arguments[0])->function))) && last_not_constant == 0) {
  1291. //native type constructor or intrinsic function
  1292. const Variant **vptr = NULL;
  1293. Vector<Variant *> ptrs;
  1294. if (op->arguments.size() > 1) {
  1295. ptrs.resize(op->arguments.size() - 1);
  1296. for (int i = 0; i < ptrs.size(); i++) {
  1297. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i + 1]);
  1298. ptrs[i] = &cn->value;
  1299. }
  1300. vptr = (const Variant **)&ptrs[0];
  1301. }
  1302. Variant::CallError ce;
  1303. Variant v;
  1304. if (op->arguments[0]->type == Node::TYPE_TYPE) {
  1305. TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
  1306. v = Variant::construct(tn->vtype, vptr, ptrs.size(), ce);
  1307. } else {
  1308. GDScriptFunctions::Function func = static_cast<BuiltInFunctionNode *>(op->arguments[0])->function;
  1309. GDScriptFunctions::call(func, vptr, ptrs.size(), v, ce);
  1310. }
  1311. if (ce.error != Variant::CallError::CALL_OK) {
  1312. String errwhere;
  1313. if (op->arguments[0]->type == Node::TYPE_TYPE) {
  1314. TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
  1315. errwhere = "'" + Variant::get_type_name(tn->vtype) + "' constructor";
  1316. } else {
  1317. GDScriptFunctions::Function func = static_cast<BuiltInFunctionNode *>(op->arguments[0])->function;
  1318. errwhere = String("'") + GDScriptFunctions::get_func_name(func) + "' intrinsic function";
  1319. }
  1320. switch (ce.error) {
  1321. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1322. _set_error("Invalid argument (#" + itos(ce.argument + 1) + ") for " + errwhere + ".");
  1323. } break;
  1324. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1325. _set_error("Too many arguments for " + errwhere + ".");
  1326. } break;
  1327. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  1328. _set_error("Too few arguments for " + errwhere + ".");
  1329. } break;
  1330. default: {
  1331. _set_error("Invalid arguments for " + errwhere + ".");
  1332. } break;
  1333. }
  1334. error_line = op->line;
  1335. return p_node;
  1336. }
  1337. ConstantNode *cn = alloc_node<ConstantNode>();
  1338. cn->value = v;
  1339. cn->datatype = _type_from_variant(v);
  1340. return cn;
  1341. } else if (op->arguments[0]->type == Node::TYPE_BUILT_IN_FUNCTION && last_not_constant == 0) {
  1342. }
  1343. return op; //don't reduce yet
  1344. } else if (op->op == OperatorNode::OP_YIELD) {
  1345. return op;
  1346. } else if (op->op == OperatorNode::OP_INDEX) {
  1347. //can reduce indices into constant arrays or dictionaries
  1348. if (all_constants) {
  1349. ConstantNode *ca = static_cast<ConstantNode *>(op->arguments[0]);
  1350. ConstantNode *cb = static_cast<ConstantNode *>(op->arguments[1]);
  1351. bool valid;
  1352. Variant v = ca->value.get(cb->value, &valid);
  1353. if (!valid) {
  1354. _set_error("invalid index in constant expression");
  1355. error_line = op->line;
  1356. return op;
  1357. }
  1358. ConstantNode *cn = alloc_node<ConstantNode>();
  1359. cn->value = v;
  1360. cn->datatype = _type_from_variant(v);
  1361. return cn;
  1362. }
  1363. return op;
  1364. } else if (op->op == OperatorNode::OP_INDEX_NAMED) {
  1365. if (op->arguments[0]->type == Node::TYPE_CONSTANT && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  1366. ConstantNode *ca = static_cast<ConstantNode *>(op->arguments[0]);
  1367. IdentifierNode *ib = static_cast<IdentifierNode *>(op->arguments[1]);
  1368. bool valid;
  1369. Variant v = ca->value.get_named(ib->name, &valid);
  1370. if (!valid) {
  1371. _set_error("invalid index '" + String(ib->name) + "' in constant expression");
  1372. error_line = op->line;
  1373. return op;
  1374. }
  1375. ConstantNode *cn = alloc_node<ConstantNode>();
  1376. cn->value = v;
  1377. cn->datatype = _type_from_variant(v);
  1378. return cn;
  1379. }
  1380. return op;
  1381. }
  1382. //validate assignment (don't assign to constant expression
  1383. switch (op->op) {
  1384. case OperatorNode::OP_ASSIGN:
  1385. case OperatorNode::OP_ASSIGN_ADD:
  1386. case OperatorNode::OP_ASSIGN_SUB:
  1387. case OperatorNode::OP_ASSIGN_MUL:
  1388. case OperatorNode::OP_ASSIGN_DIV:
  1389. case OperatorNode::OP_ASSIGN_MOD:
  1390. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  1391. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  1392. case OperatorNode::OP_ASSIGN_BIT_AND:
  1393. case OperatorNode::OP_ASSIGN_BIT_OR:
  1394. case OperatorNode::OP_ASSIGN_BIT_XOR: {
  1395. if (op->arguments[0]->type == Node::TYPE_CONSTANT) {
  1396. _set_error("Can't assign to constant", tokenizer->get_token_line() - 1);
  1397. error_line = op->line;
  1398. return op;
  1399. }
  1400. if (op->arguments[0]->type == Node::TYPE_OPERATOR) {
  1401. OperatorNode *on = static_cast<OperatorNode *>(op->arguments[0]);
  1402. if (on->op != OperatorNode::OP_INDEX && on->op != OperatorNode::OP_INDEX_NAMED) {
  1403. _set_error("Can't assign to an expression", tokenizer->get_token_line() - 1);
  1404. error_line = op->line;
  1405. return op;
  1406. }
  1407. }
  1408. } break;
  1409. default: { break; }
  1410. }
  1411. //now se if all are constants
  1412. if (!all_constants)
  1413. return op; //nothing to reduce from here on
  1414. #define _REDUCE_UNARY(m_vop) \
  1415. bool valid = false; \
  1416. Variant res; \
  1417. Variant::evaluate(m_vop, static_cast<ConstantNode *>(op->arguments[0])->value, Variant(), res, valid); \
  1418. if (!valid) { \
  1419. _set_error("Invalid operand for unary operator"); \
  1420. error_line = op->line; \
  1421. return p_node; \
  1422. } \
  1423. ConstantNode *cn = alloc_node<ConstantNode>(); \
  1424. cn->value = res; \
  1425. cn->datatype = _type_from_variant(res); \
  1426. return cn;
  1427. #define _REDUCE_BINARY(m_vop) \
  1428. bool valid = false; \
  1429. Variant res; \
  1430. Variant::evaluate(m_vop, static_cast<ConstantNode *>(op->arguments[0])->value, static_cast<ConstantNode *>(op->arguments[1])->value, res, valid); \
  1431. if (!valid) { \
  1432. _set_error("Invalid operands for operator"); \
  1433. error_line = op->line; \
  1434. return p_node; \
  1435. } \
  1436. ConstantNode *cn = alloc_node<ConstantNode>(); \
  1437. cn->value = res; \
  1438. cn->datatype = _type_from_variant(res); \
  1439. return cn;
  1440. switch (op->op) {
  1441. //unary operators
  1442. case OperatorNode::OP_NEG: {
  1443. _REDUCE_UNARY(Variant::OP_NEGATE);
  1444. } break;
  1445. case OperatorNode::OP_POS: {
  1446. _REDUCE_UNARY(Variant::OP_POSITIVE);
  1447. } break;
  1448. case OperatorNode::OP_NOT: {
  1449. _REDUCE_UNARY(Variant::OP_NOT);
  1450. } break;
  1451. case OperatorNode::OP_BIT_INVERT: {
  1452. _REDUCE_UNARY(Variant::OP_BIT_NEGATE);
  1453. } break;
  1454. //binary operators (in precedence order)
  1455. case OperatorNode::OP_IN: {
  1456. _REDUCE_BINARY(Variant::OP_IN);
  1457. } break;
  1458. case OperatorNode::OP_EQUAL: {
  1459. _REDUCE_BINARY(Variant::OP_EQUAL);
  1460. } break;
  1461. case OperatorNode::OP_NOT_EQUAL: {
  1462. _REDUCE_BINARY(Variant::OP_NOT_EQUAL);
  1463. } break;
  1464. case OperatorNode::OP_LESS: {
  1465. _REDUCE_BINARY(Variant::OP_LESS);
  1466. } break;
  1467. case OperatorNode::OP_LESS_EQUAL: {
  1468. _REDUCE_BINARY(Variant::OP_LESS_EQUAL);
  1469. } break;
  1470. case OperatorNode::OP_GREATER: {
  1471. _REDUCE_BINARY(Variant::OP_GREATER);
  1472. } break;
  1473. case OperatorNode::OP_GREATER_EQUAL: {
  1474. _REDUCE_BINARY(Variant::OP_GREATER_EQUAL);
  1475. } break;
  1476. case OperatorNode::OP_AND: {
  1477. _REDUCE_BINARY(Variant::OP_AND);
  1478. } break;
  1479. case OperatorNode::OP_OR: {
  1480. _REDUCE_BINARY(Variant::OP_OR);
  1481. } break;
  1482. case OperatorNode::OP_ADD: {
  1483. _REDUCE_BINARY(Variant::OP_ADD);
  1484. } break;
  1485. case OperatorNode::OP_SUB: {
  1486. _REDUCE_BINARY(Variant::OP_SUBTRACT);
  1487. } break;
  1488. case OperatorNode::OP_MUL: {
  1489. _REDUCE_BINARY(Variant::OP_MULTIPLY);
  1490. } break;
  1491. case OperatorNode::OP_DIV: {
  1492. _REDUCE_BINARY(Variant::OP_DIVIDE);
  1493. } break;
  1494. case OperatorNode::OP_MOD: {
  1495. _REDUCE_BINARY(Variant::OP_MODULE);
  1496. } break;
  1497. case OperatorNode::OP_SHIFT_LEFT: {
  1498. _REDUCE_BINARY(Variant::OP_SHIFT_LEFT);
  1499. } break;
  1500. case OperatorNode::OP_SHIFT_RIGHT: {
  1501. _REDUCE_BINARY(Variant::OP_SHIFT_RIGHT);
  1502. } break;
  1503. case OperatorNode::OP_BIT_AND: {
  1504. _REDUCE_BINARY(Variant::OP_BIT_AND);
  1505. } break;
  1506. case OperatorNode::OP_BIT_OR: {
  1507. _REDUCE_BINARY(Variant::OP_BIT_OR);
  1508. } break;
  1509. case OperatorNode::OP_BIT_XOR: {
  1510. _REDUCE_BINARY(Variant::OP_BIT_XOR);
  1511. } break;
  1512. case OperatorNode::OP_TERNARY_IF: {
  1513. if (static_cast<ConstantNode *>(op->arguments[0])->value.booleanize()) {
  1514. return op->arguments[1];
  1515. } else {
  1516. return op->arguments[2];
  1517. }
  1518. } break;
  1519. default: { ERR_FAIL_V(op); }
  1520. }
  1521. ERR_FAIL_V(op);
  1522. } break;
  1523. default: {
  1524. return p_node;
  1525. } break;
  1526. }
  1527. }
  1528. GDScriptParser::Node *GDScriptParser::_parse_and_reduce_expression(Node *p_parent, bool p_static, bool p_reduce_const, bool p_allow_assign) {
  1529. Node *expr = _parse_expression(p_parent, p_static, p_allow_assign, p_reduce_const);
  1530. if (!expr || error_set)
  1531. return NULL;
  1532. expr = _reduce_expression(expr, p_reduce_const);
  1533. if (!expr || error_set)
  1534. return NULL;
  1535. return expr;
  1536. }
  1537. bool GDScriptParser::_recover_from_completion() {
  1538. if (!completion_found) {
  1539. return false; //can't recover if no completion
  1540. }
  1541. //skip stuff until newline
  1542. while (tokenizer->get_token() != GDScriptTokenizer::TK_NEWLINE && tokenizer->get_token() != GDScriptTokenizer::TK_EOF && tokenizer->get_token() != GDScriptTokenizer::TK_ERROR) {
  1543. tokenizer->advance();
  1544. }
  1545. completion_found = false;
  1546. error_set = false;
  1547. if (tokenizer->get_token() == GDScriptTokenizer::TK_ERROR) {
  1548. error_set = true;
  1549. }
  1550. return true;
  1551. }
  1552. GDScriptParser::PatternNode *GDScriptParser::_parse_pattern(bool p_static) {
  1553. PatternNode *pattern = alloc_node<PatternNode>();
  1554. GDScriptTokenizer::Token token = tokenizer->get_token();
  1555. if (error_set)
  1556. return NULL;
  1557. if (token == GDScriptTokenizer::TK_EOF) {
  1558. return NULL;
  1559. }
  1560. switch (token) {
  1561. // array
  1562. case GDScriptTokenizer::TK_BRACKET_OPEN: {
  1563. tokenizer->advance();
  1564. pattern->pt_type = GDScriptParser::PatternNode::PT_ARRAY;
  1565. while (true) {
  1566. if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1567. tokenizer->advance();
  1568. break;
  1569. }
  1570. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) {
  1571. // match everything
  1572. tokenizer->advance(2);
  1573. PatternNode *sub_pattern = alloc_node<PatternNode>();
  1574. sub_pattern->pt_type = GDScriptParser::PatternNode::PT_IGNORE_REST;
  1575. pattern->array.push_back(sub_pattern);
  1576. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA && tokenizer->get_token(1) == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1577. tokenizer->advance(2);
  1578. break;
  1579. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1580. tokenizer->advance(1);
  1581. break;
  1582. } else {
  1583. _set_error("'..' pattern only allowed at the end of an array pattern");
  1584. return NULL;
  1585. }
  1586. }
  1587. PatternNode *sub_pattern = _parse_pattern(p_static);
  1588. if (!sub_pattern) {
  1589. return NULL;
  1590. }
  1591. pattern->array.push_back(sub_pattern);
  1592. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  1593. tokenizer->advance();
  1594. continue;
  1595. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1596. tokenizer->advance();
  1597. break;
  1598. } else {
  1599. _set_error("Not a valid pattern");
  1600. return NULL;
  1601. }
  1602. }
  1603. } break;
  1604. // bind
  1605. case GDScriptTokenizer::TK_PR_VAR: {
  1606. tokenizer->advance();
  1607. pattern->pt_type = GDScriptParser::PatternNode::PT_BIND;
  1608. pattern->bind = tokenizer->get_token_identifier();
  1609. // Check if binding is already used
  1610. if (current_block->variables.has(pattern->bind)) {
  1611. _set_error("Binding name of '" + pattern->bind.operator String() + "' was already used in the pattern.");
  1612. return NULL;
  1613. }
  1614. // Create local variable for proper identifier detection later
  1615. LocalVarNode *lv = alloc_node<LocalVarNode>();
  1616. lv->name = pattern->bind;
  1617. current_block->variables.insert(lv->name, lv);
  1618. tokenizer->advance();
  1619. } break;
  1620. // dictionary
  1621. case GDScriptTokenizer::TK_CURLY_BRACKET_OPEN: {
  1622. tokenizer->advance();
  1623. pattern->pt_type = GDScriptParser::PatternNode::PT_DICTIONARY;
  1624. while (true) {
  1625. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1626. tokenizer->advance();
  1627. break;
  1628. }
  1629. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) {
  1630. // match everything
  1631. tokenizer->advance(2);
  1632. PatternNode *sub_pattern = alloc_node<PatternNode>();
  1633. sub_pattern->pt_type = PatternNode::PT_IGNORE_REST;
  1634. pattern->array.push_back(sub_pattern);
  1635. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA && tokenizer->get_token(1) == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1636. tokenizer->advance(2);
  1637. break;
  1638. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1639. tokenizer->advance(1);
  1640. break;
  1641. } else {
  1642. _set_error("'..' pattern only allowed at the end of a dictionary pattern");
  1643. return NULL;
  1644. }
  1645. }
  1646. Node *key = _parse_and_reduce_expression(pattern, p_static);
  1647. if (!key) {
  1648. _set_error("Not a valid key in pattern");
  1649. return NULL;
  1650. }
  1651. if (key->type != GDScriptParser::Node::TYPE_CONSTANT) {
  1652. _set_error("Not a constant expression as key");
  1653. return NULL;
  1654. }
  1655. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  1656. tokenizer->advance();
  1657. PatternNode *value = _parse_pattern(p_static);
  1658. if (!value) {
  1659. _set_error("Expected pattern in dictionary value");
  1660. return NULL;
  1661. }
  1662. pattern->dictionary.insert(static_cast<ConstantNode *>(key), value);
  1663. } else {
  1664. pattern->dictionary.insert(static_cast<ConstantNode *>(key), NULL);
  1665. }
  1666. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  1667. tokenizer->advance();
  1668. continue;
  1669. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1670. tokenizer->advance();
  1671. break;
  1672. } else {
  1673. _set_error("Not a valid pattern");
  1674. return NULL;
  1675. }
  1676. }
  1677. } break;
  1678. case GDScriptTokenizer::TK_WILDCARD: {
  1679. tokenizer->advance();
  1680. pattern->pt_type = PatternNode::PT_WILDCARD;
  1681. } break;
  1682. // all the constants like strings and numbers
  1683. default: {
  1684. Node *value = _parse_and_reduce_expression(pattern, p_static);
  1685. if (!value) {
  1686. _set_error("Expect constant expression or variables in a pattern");
  1687. return NULL;
  1688. }
  1689. if (value->type == Node::TYPE_OPERATOR) {
  1690. // Maybe it's SomeEnum.VALUE
  1691. Node *current_value = value;
  1692. while (current_value->type == Node::TYPE_OPERATOR) {
  1693. OperatorNode *op_node = static_cast<OperatorNode *>(current_value);
  1694. if (op_node->op != OperatorNode::OP_INDEX_NAMED) {
  1695. _set_error("Invalid operator in pattern. Only index (`A.B`) is allowed");
  1696. return NULL;
  1697. }
  1698. current_value = op_node->arguments[0];
  1699. }
  1700. if (current_value->type != Node::TYPE_IDENTIFIER) {
  1701. _set_error("Only constant expression or variables allowed in a pattern");
  1702. return NULL;
  1703. }
  1704. } else if (value->type != Node::TYPE_IDENTIFIER && value->type != Node::TYPE_CONSTANT) {
  1705. _set_error("Only constant expressions or variables allowed in a pattern");
  1706. return NULL;
  1707. }
  1708. pattern->pt_type = PatternNode::PT_CONSTANT;
  1709. pattern->constant = value;
  1710. } break;
  1711. }
  1712. return pattern;
  1713. }
  1714. void GDScriptParser::_parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static) {
  1715. int indent_level = tab_level.back()->get();
  1716. while (true) {
  1717. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline())
  1718. ;
  1719. // GDScriptTokenizer::Token token = tokenizer->get_token();
  1720. if (error_set)
  1721. return;
  1722. if (indent_level > tab_level.back()->get()) {
  1723. return; // go back a level
  1724. }
  1725. if (pending_newline != -1) {
  1726. pending_newline = -1;
  1727. }
  1728. PatternBranchNode *branch = alloc_node<PatternBranchNode>();
  1729. branch->body = alloc_node<BlockNode>();
  1730. branch->body->parent_block = p_block;
  1731. p_block->sub_blocks.push_back(branch->body);
  1732. current_block = branch->body;
  1733. branch->patterns.push_back(_parse_pattern(p_static));
  1734. if (!branch->patterns[0]) {
  1735. return;
  1736. }
  1737. bool has_binding = branch->patterns[0]->pt_type == PatternNode::PT_BIND;
  1738. bool catch_all = has_binding || branch->patterns[0]->pt_type == PatternNode::PT_WILDCARD;
  1739. while (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  1740. tokenizer->advance();
  1741. branch->patterns.push_back(_parse_pattern(p_static));
  1742. if (!branch->patterns[branch->patterns.size() - 1]) {
  1743. return;
  1744. }
  1745. PatternNode::PatternType pt = branch->patterns[branch->patterns.size() - 1]->pt_type;
  1746. if (pt == PatternNode::PT_BIND) {
  1747. _set_error("Cannot use bindings with multipattern.");
  1748. return;
  1749. }
  1750. catch_all = catch_all || pt == PatternNode::PT_WILDCARD;
  1751. }
  1752. if (!_enter_indent_block()) {
  1753. _set_error("Expected block in pattern branch");
  1754. return;
  1755. }
  1756. _parse_block(branch->body, p_static);
  1757. current_block = p_block;
  1758. if (catch_all && branch->body->has_return) {
  1759. p_block->has_return = true;
  1760. }
  1761. p_branches.push_back(branch);
  1762. }
  1763. }
  1764. void GDScriptParser::_generate_pattern(PatternNode *p_pattern, Node *p_node_to_match, Node *&p_resulting_node, Map<StringName, Node *> &p_bindings) {
  1765. const DataType &to_match_type = p_node_to_match->get_datatype();
  1766. switch (p_pattern->pt_type) {
  1767. case PatternNode::PT_CONSTANT: {
  1768. DataType pattern_type = _reduce_node_type(p_pattern->constant);
  1769. if (error_set) {
  1770. return;
  1771. }
  1772. OperatorNode *type_comp = NULL;
  1773. // static type check if possible
  1774. if (pattern_type.has_type && to_match_type.has_type) {
  1775. if (!_is_type_compatible(to_match_type, pattern_type) && !_is_type_compatible(pattern_type, to_match_type)) {
  1776. _set_error("Pattern type (" + pattern_type.to_string() + ") is not compatible with the type of the value to match (" + to_match_type.to_string() + ").",
  1777. p_pattern->line);
  1778. return;
  1779. }
  1780. } else {
  1781. // runtime typecheck
  1782. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1783. typeof_node->function = GDScriptFunctions::TYPE_OF;
  1784. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1785. typeof_match_value->op = OperatorNode::OP_CALL;
  1786. typeof_match_value->arguments.push_back(typeof_node);
  1787. typeof_match_value->arguments.push_back(p_node_to_match);
  1788. OperatorNode *typeof_pattern_value = alloc_node<OperatorNode>();
  1789. typeof_pattern_value->op = OperatorNode::OP_CALL;
  1790. typeof_pattern_value->arguments.push_back(typeof_node);
  1791. typeof_pattern_value->arguments.push_back(p_pattern->constant);
  1792. type_comp = alloc_node<OperatorNode>();
  1793. type_comp->op = OperatorNode::OP_EQUAL;
  1794. type_comp->arguments.push_back(typeof_match_value);
  1795. type_comp->arguments.push_back(typeof_pattern_value);
  1796. }
  1797. // compare the actual values
  1798. OperatorNode *value_comp = alloc_node<OperatorNode>();
  1799. value_comp->op = OperatorNode::OP_EQUAL;
  1800. value_comp->arguments.push_back(p_pattern->constant);
  1801. value_comp->arguments.push_back(p_node_to_match);
  1802. if (type_comp) {
  1803. OperatorNode *full_comparison = alloc_node<OperatorNode>();
  1804. full_comparison->op = OperatorNode::OP_AND;
  1805. full_comparison->arguments.push_back(type_comp);
  1806. full_comparison->arguments.push_back(value_comp);
  1807. p_resulting_node = full_comparison;
  1808. } else {
  1809. p_resulting_node = value_comp;
  1810. }
  1811. } break;
  1812. case PatternNode::PT_BIND: {
  1813. p_bindings[p_pattern->bind] = p_node_to_match;
  1814. // a bind always matches
  1815. ConstantNode *true_value = alloc_node<ConstantNode>();
  1816. true_value->value = Variant(true);
  1817. p_resulting_node = true_value;
  1818. } break;
  1819. case PatternNode::PT_ARRAY: {
  1820. bool open_ended = false;
  1821. if (p_pattern->array.size() > 0) {
  1822. if (p_pattern->array[p_pattern->array.size() - 1]->pt_type == PatternNode::PT_IGNORE_REST) {
  1823. open_ended = true;
  1824. }
  1825. }
  1826. // typeof(value_to_match) == TYPE_ARRAY && value_to_match.size() >= length
  1827. // typeof(value_to_match) == TYPE_ARRAY && value_to_match.size() == length
  1828. {
  1829. OperatorNode *type_comp = NULL;
  1830. // static type check if possible
  1831. if (to_match_type.has_type) {
  1832. // must be an array
  1833. if (to_match_type.kind != DataType::BUILTIN || to_match_type.builtin_type != Variant::ARRAY) {
  1834. _set_error("Cannot match an array pattern with a non-array expression.", p_pattern->line);
  1835. return;
  1836. }
  1837. } else {
  1838. // runtime typecheck
  1839. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1840. typeof_node->function = GDScriptFunctions::TYPE_OF;
  1841. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1842. typeof_match_value->op = OperatorNode::OP_CALL;
  1843. typeof_match_value->arguments.push_back(typeof_node);
  1844. typeof_match_value->arguments.push_back(p_node_to_match);
  1845. IdentifierNode *typeof_array = alloc_node<IdentifierNode>();
  1846. typeof_array->name = "TYPE_ARRAY";
  1847. type_comp = alloc_node<OperatorNode>();
  1848. type_comp->op = OperatorNode::OP_EQUAL;
  1849. type_comp->arguments.push_back(typeof_match_value);
  1850. type_comp->arguments.push_back(typeof_array);
  1851. }
  1852. // size
  1853. ConstantNode *length = alloc_node<ConstantNode>();
  1854. length->value = Variant(open_ended ? p_pattern->array.size() - 1 : p_pattern->array.size());
  1855. OperatorNode *call = alloc_node<OperatorNode>();
  1856. call->op = OperatorNode::OP_CALL;
  1857. call->arguments.push_back(p_node_to_match);
  1858. IdentifierNode *size = alloc_node<IdentifierNode>();
  1859. size->name = "size";
  1860. call->arguments.push_back(size);
  1861. OperatorNode *length_comparison = alloc_node<OperatorNode>();
  1862. length_comparison->op = open_ended ? OperatorNode::OP_GREATER_EQUAL : OperatorNode::OP_EQUAL;
  1863. length_comparison->arguments.push_back(call);
  1864. length_comparison->arguments.push_back(length);
  1865. if (type_comp) {
  1866. OperatorNode *type_and_length_comparison = alloc_node<OperatorNode>();
  1867. type_and_length_comparison->op = OperatorNode::OP_AND;
  1868. type_and_length_comparison->arguments.push_back(type_comp);
  1869. type_and_length_comparison->arguments.push_back(length_comparison);
  1870. p_resulting_node = type_and_length_comparison;
  1871. } else {
  1872. p_resulting_node = length_comparison;
  1873. }
  1874. }
  1875. for (int i = 0; i < p_pattern->array.size(); i++) {
  1876. PatternNode *pattern = p_pattern->array[i];
  1877. Node *condition = NULL;
  1878. ConstantNode *index = alloc_node<ConstantNode>();
  1879. index->value = Variant(i);
  1880. OperatorNode *indexed_value = alloc_node<OperatorNode>();
  1881. indexed_value->op = OperatorNode::OP_INDEX;
  1882. indexed_value->arguments.push_back(p_node_to_match);
  1883. indexed_value->arguments.push_back(index);
  1884. _generate_pattern(pattern, indexed_value, condition, p_bindings);
  1885. // concatenate all the patterns with &&
  1886. OperatorNode *and_node = alloc_node<OperatorNode>();
  1887. and_node->op = OperatorNode::OP_AND;
  1888. and_node->arguments.push_back(p_resulting_node);
  1889. and_node->arguments.push_back(condition);
  1890. p_resulting_node = and_node;
  1891. }
  1892. } break;
  1893. case PatternNode::PT_DICTIONARY: {
  1894. bool open_ended = false;
  1895. if (p_pattern->array.size() > 0) {
  1896. open_ended = true;
  1897. }
  1898. // typeof(value_to_match) == TYPE_DICTIONARY && value_to_match.size() >= length
  1899. // typeof(value_to_match) == TYPE_DICTIONARY && value_to_match.size() == length
  1900. {
  1901. OperatorNode *type_comp = NULL;
  1902. // static type check if possible
  1903. if (to_match_type.has_type) {
  1904. // must be an dictionary
  1905. if (to_match_type.kind != DataType::BUILTIN || to_match_type.builtin_type != Variant::DICTIONARY) {
  1906. _set_error("Cannot match an dictionary pattern with a non-dictionary expression.", p_pattern->line);
  1907. return;
  1908. }
  1909. } else {
  1910. // runtime typecheck
  1911. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1912. typeof_node->function = GDScriptFunctions::TYPE_OF;
  1913. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1914. typeof_match_value->op = OperatorNode::OP_CALL;
  1915. typeof_match_value->arguments.push_back(typeof_node);
  1916. typeof_match_value->arguments.push_back(p_node_to_match);
  1917. IdentifierNode *typeof_dictionary = alloc_node<IdentifierNode>();
  1918. typeof_dictionary->name = "TYPE_DICTIONARY";
  1919. type_comp = alloc_node<OperatorNode>();
  1920. type_comp->op = OperatorNode::OP_EQUAL;
  1921. type_comp->arguments.push_back(typeof_match_value);
  1922. type_comp->arguments.push_back(typeof_dictionary);
  1923. }
  1924. // size
  1925. ConstantNode *length = alloc_node<ConstantNode>();
  1926. length->value = Variant(open_ended ? p_pattern->dictionary.size() - 1 : p_pattern->dictionary.size());
  1927. OperatorNode *call = alloc_node<OperatorNode>();
  1928. call->op = OperatorNode::OP_CALL;
  1929. call->arguments.push_back(p_node_to_match);
  1930. IdentifierNode *size = alloc_node<IdentifierNode>();
  1931. size->name = "size";
  1932. call->arguments.push_back(size);
  1933. OperatorNode *length_comparison = alloc_node<OperatorNode>();
  1934. length_comparison->op = open_ended ? OperatorNode::OP_GREATER_EQUAL : OperatorNode::OP_EQUAL;
  1935. length_comparison->arguments.push_back(call);
  1936. length_comparison->arguments.push_back(length);
  1937. if (type_comp) {
  1938. OperatorNode *type_and_length_comparison = alloc_node<OperatorNode>();
  1939. type_and_length_comparison->op = OperatorNode::OP_AND;
  1940. type_and_length_comparison->arguments.push_back(type_comp);
  1941. type_and_length_comparison->arguments.push_back(length_comparison);
  1942. p_resulting_node = type_and_length_comparison;
  1943. } else {
  1944. p_resulting_node = length_comparison;
  1945. }
  1946. }
  1947. for (Map<ConstantNode *, PatternNode *>::Element *e = p_pattern->dictionary.front(); e; e = e->next()) {
  1948. Node *condition = NULL;
  1949. // chech for has, then for pattern
  1950. IdentifierNode *has = alloc_node<IdentifierNode>();
  1951. has->name = "has";
  1952. OperatorNode *has_call = alloc_node<OperatorNode>();
  1953. has_call->op = OperatorNode::OP_CALL;
  1954. has_call->arguments.push_back(p_node_to_match);
  1955. has_call->arguments.push_back(has);
  1956. has_call->arguments.push_back(e->key());
  1957. if (e->value()) {
  1958. OperatorNode *indexed_value = alloc_node<OperatorNode>();
  1959. indexed_value->op = OperatorNode::OP_INDEX;
  1960. indexed_value->arguments.push_back(p_node_to_match);
  1961. indexed_value->arguments.push_back(e->key());
  1962. _generate_pattern(e->value(), indexed_value, condition, p_bindings);
  1963. OperatorNode *has_and_pattern = alloc_node<OperatorNode>();
  1964. has_and_pattern->op = OperatorNode::OP_AND;
  1965. has_and_pattern->arguments.push_back(has_call);
  1966. has_and_pattern->arguments.push_back(condition);
  1967. condition = has_and_pattern;
  1968. } else {
  1969. condition = has_call;
  1970. }
  1971. // concatenate all the patterns with &&
  1972. OperatorNode *and_node = alloc_node<OperatorNode>();
  1973. and_node->op = OperatorNode::OP_AND;
  1974. and_node->arguments.push_back(p_resulting_node);
  1975. and_node->arguments.push_back(condition);
  1976. p_resulting_node = and_node;
  1977. }
  1978. } break;
  1979. case PatternNode::PT_IGNORE_REST:
  1980. case PatternNode::PT_WILDCARD: {
  1981. // simply generate a `true`
  1982. ConstantNode *true_value = alloc_node<ConstantNode>();
  1983. true_value->value = Variant(true);
  1984. p_resulting_node = true_value;
  1985. } break;
  1986. default: {
  1987. } break;
  1988. }
  1989. }
  1990. void GDScriptParser::_transform_match_statment(MatchNode *p_match_statement) {
  1991. IdentifierNode *id = alloc_node<IdentifierNode>();
  1992. id->name = "#match_value";
  1993. id->line = p_match_statement->line;
  1994. id->datatype = _reduce_node_type(p_match_statement->val_to_match);
  1995. if (error_set) {
  1996. return;
  1997. }
  1998. for (int i = 0; i < p_match_statement->branches.size(); i++) {
  1999. PatternBranchNode *branch = p_match_statement->branches[i];
  2000. MatchNode::CompiledPatternBranch compiled_branch;
  2001. compiled_branch.compiled_pattern = NULL;
  2002. Map<StringName, Node *> binding;
  2003. for (int j = 0; j < branch->patterns.size(); j++) {
  2004. PatternNode *pattern = branch->patterns[j];
  2005. Map<StringName, Node *> bindings;
  2006. Node *resulting_node = NULL;
  2007. _generate_pattern(pattern, id, resulting_node, bindings);
  2008. if (!resulting_node) {
  2009. return;
  2010. }
  2011. if (!binding.empty() && !bindings.empty()) {
  2012. _set_error("Multipatterns can't contain bindings");
  2013. return;
  2014. } else {
  2015. binding = bindings;
  2016. }
  2017. // Result is always a boolean
  2018. DataType resulting_node_type;
  2019. resulting_node_type.has_type = true;
  2020. resulting_node_type.is_constant = true;
  2021. resulting_node_type.kind = DataType::BUILTIN;
  2022. resulting_node_type.builtin_type = Variant::BOOL;
  2023. resulting_node->set_datatype(resulting_node_type);
  2024. if (compiled_branch.compiled_pattern) {
  2025. OperatorNode *or_node = alloc_node<OperatorNode>();
  2026. or_node->op = OperatorNode::OP_OR;
  2027. or_node->arguments.push_back(compiled_branch.compiled_pattern);
  2028. or_node->arguments.push_back(resulting_node);
  2029. compiled_branch.compiled_pattern = or_node;
  2030. } else {
  2031. // single pattern | first one
  2032. compiled_branch.compiled_pattern = resulting_node;
  2033. }
  2034. }
  2035. // prepare the body ...hehe
  2036. for (Map<StringName, Node *>::Element *e = binding.front(); e; e = e->next()) {
  2037. if (!branch->body->variables.has(e->key())) {
  2038. _set_error("Parser bug: missing pattern bind variable.", branch->line);
  2039. ERR_FAIL();
  2040. }
  2041. LocalVarNode *local_var = branch->body->variables[e->key()];
  2042. local_var->assign = e->value();
  2043. local_var->set_datatype(local_var->assign->get_datatype());
  2044. IdentifierNode *id = alloc_node<IdentifierNode>();
  2045. id->name = local_var->name;
  2046. id->declared_block = branch->body;
  2047. id->set_datatype(local_var->assign->get_datatype());
  2048. OperatorNode *op = alloc_node<OperatorNode>();
  2049. op->op = OperatorNode::OP_ASSIGN;
  2050. op->arguments.push_back(id);
  2051. op->arguments.push_back(local_var->assign);
  2052. branch->body->statements.push_front(op);
  2053. branch->body->statements.push_front(local_var);
  2054. }
  2055. compiled_branch.body = branch->body;
  2056. p_match_statement->compiled_pattern_branches.push_back(compiled_branch);
  2057. }
  2058. }
  2059. void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
  2060. int indent_level = tab_level.back()->get();
  2061. #ifdef DEBUG_ENABLED
  2062. NewLineNode *nl = alloc_node<NewLineNode>();
  2063. nl->line = tokenizer->get_token_line();
  2064. p_block->statements.push_back(nl);
  2065. #endif
  2066. bool is_first_line = true;
  2067. while (true) {
  2068. if (!is_first_line && tab_level.back()->prev() && tab_level.back()->prev()->get() == indent_level) {
  2069. // pythonic single-line expression, don't parse future lines
  2070. tab_level.pop_back();
  2071. p_block->end_line = tokenizer->get_token_line();
  2072. return;
  2073. }
  2074. is_first_line = false;
  2075. GDScriptTokenizer::Token token = tokenizer->get_token();
  2076. if (error_set)
  2077. return;
  2078. if (indent_level > tab_level.back()->get()) {
  2079. p_block->end_line = tokenizer->get_token_line();
  2080. return; //go back a level
  2081. }
  2082. if (pending_newline != -1) {
  2083. NewLineNode *nl = alloc_node<NewLineNode>();
  2084. nl->line = pending_newline;
  2085. p_block->statements.push_back(nl);
  2086. pending_newline = -1;
  2087. }
  2088. switch (token) {
  2089. case GDScriptTokenizer::TK_EOF:
  2090. case GDScriptTokenizer::TK_ERROR:
  2091. case GDScriptTokenizer::TK_NEWLINE:
  2092. case GDScriptTokenizer::TK_CF_PASS: {
  2093. // will check later
  2094. } break;
  2095. default: {
  2096. // TODO: Make this a warning
  2097. /*if (p_block->has_return) {
  2098. _set_error("Unreacheable code.");
  2099. return;
  2100. }*/
  2101. } break;
  2102. }
  2103. switch (token) {
  2104. case GDScriptTokenizer::TK_EOF:
  2105. p_block->end_line = tokenizer->get_token_line();
  2106. case GDScriptTokenizer::TK_ERROR: {
  2107. return; //go back
  2108. //end of file!
  2109. } break;
  2110. case GDScriptTokenizer::TK_NEWLINE: {
  2111. if (!_parse_newline()) {
  2112. if (!error_set) {
  2113. p_block->end_line = tokenizer->get_token_line();
  2114. pending_newline = p_block->end_line;
  2115. }
  2116. return;
  2117. }
  2118. NewLineNode *nl = alloc_node<NewLineNode>();
  2119. nl->line = tokenizer->get_token_line();
  2120. p_block->statements.push_back(nl);
  2121. } break;
  2122. case GDScriptTokenizer::TK_CF_PASS: {
  2123. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_SEMICOLON && tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE && tokenizer->get_token(1) != GDScriptTokenizer::TK_EOF) {
  2124. _set_error("Expected ';' or <NewLine>.");
  2125. return;
  2126. }
  2127. tokenizer->advance();
  2128. if (tokenizer->get_token() == GDScriptTokenizer::TK_SEMICOLON) {
  2129. // Ignore semicolon after 'pass'
  2130. tokenizer->advance();
  2131. }
  2132. } break;
  2133. case GDScriptTokenizer::TK_PR_VAR: {
  2134. //variale declaration and (eventual) initialization
  2135. tokenizer->advance();
  2136. int var_line = tokenizer->get_token_line();
  2137. if (!tokenizer->is_token_literal(0, true)) {
  2138. _set_error("Expected identifier for local variable name.");
  2139. return;
  2140. }
  2141. StringName n = tokenizer->get_token_literal();
  2142. tokenizer->advance();
  2143. if (current_function) {
  2144. for (int i = 0; i < current_function->arguments.size(); i++) {
  2145. if (n == current_function->arguments[i]) {
  2146. _set_error("Variable '" + String(n) + "' already defined in the scope (at line: " + itos(current_function->line) + ").");
  2147. return;
  2148. }
  2149. }
  2150. }
  2151. BlockNode *check_block = p_block;
  2152. while (check_block) {
  2153. if (check_block->variables.has(n)) {
  2154. _set_error("Variable '" + String(n) + "' already defined in the scope (at line: " + itos(check_block->variables[n]->line) + ").");
  2155. return;
  2156. }
  2157. check_block = check_block->parent_block;
  2158. }
  2159. //must know when the local variable is declared
  2160. LocalVarNode *lv = alloc_node<LocalVarNode>();
  2161. lv->name = n;
  2162. lv->line = var_line;
  2163. p_block->statements.push_back(lv);
  2164. Node *assigned = NULL;
  2165. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  2166. if (!_parse_type(lv->datatype)) {
  2167. _set_error("Expected type for variable.");
  2168. return;
  2169. }
  2170. }
  2171. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  2172. tokenizer->advance();
  2173. Node *subexpr = _parse_and_reduce_expression(p_block, p_static);
  2174. if (!subexpr) {
  2175. if (_recover_from_completion()) {
  2176. break;
  2177. }
  2178. return;
  2179. }
  2180. lv->assignments++;
  2181. assigned = subexpr;
  2182. } else {
  2183. ConstantNode *c = alloc_node<ConstantNode>();
  2184. if (lv->datatype.has_type && lv->datatype.kind == DataType::BUILTIN) {
  2185. Variant::CallError err;
  2186. c->value = Variant::construct(lv->datatype.builtin_type, NULL, 0, err);
  2187. } else {
  2188. c->value = Variant();
  2189. }
  2190. c->line = var_line;
  2191. assigned = c;
  2192. }
  2193. //must be added later, to avoid self-referencing.
  2194. p_block->variables.insert(n, lv);
  2195. IdentifierNode *id = alloc_node<IdentifierNode>();
  2196. id->name = n;
  2197. id->declared_block = p_block;
  2198. id->line = var_line;
  2199. OperatorNode *op = alloc_node<OperatorNode>();
  2200. op->op = OperatorNode::OP_ASSIGN;
  2201. op->arguments.push_back(id);
  2202. op->arguments.push_back(assigned);
  2203. op->line = var_line;
  2204. p_block->statements.push_back(op);
  2205. lv->assign_op = op;
  2206. lv->assign = assigned;
  2207. if (!_end_statement()) {
  2208. _set_error("Expected end of statement (var)");
  2209. return;
  2210. }
  2211. } break;
  2212. case GDScriptTokenizer::TK_CF_IF: {
  2213. tokenizer->advance();
  2214. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2215. if (!condition) {
  2216. if (_recover_from_completion()) {
  2217. break;
  2218. }
  2219. return;
  2220. }
  2221. ControlFlowNode *cf_if = alloc_node<ControlFlowNode>();
  2222. cf_if->cf_type = ControlFlowNode::CF_IF;
  2223. cf_if->arguments.push_back(condition);
  2224. cf_if->body = alloc_node<BlockNode>();
  2225. cf_if->body->parent_block = p_block;
  2226. cf_if->body->if_condition = condition; //helps code completion
  2227. p_block->sub_blocks.push_back(cf_if->body);
  2228. if (!_enter_indent_block(cf_if->body)) {
  2229. _set_error("Expected indented block after 'if'");
  2230. p_block->end_line = tokenizer->get_token_line();
  2231. return;
  2232. }
  2233. current_block = cf_if->body;
  2234. _parse_block(cf_if->body, p_static);
  2235. current_block = p_block;
  2236. if (error_set)
  2237. return;
  2238. p_block->statements.push_back(cf_if);
  2239. bool all_have_return = cf_if->body->has_return;
  2240. bool have_else = false;
  2241. while (true) {
  2242. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline())
  2243. ;
  2244. if (tab_level.back()->get() < indent_level) { //not at current indent level
  2245. p_block->end_line = tokenizer->get_token_line();
  2246. return;
  2247. }
  2248. if (tokenizer->get_token() == GDScriptTokenizer::TK_CF_ELIF) {
  2249. if (tab_level.back()->get() > indent_level) {
  2250. _set_error("Invalid indent");
  2251. return;
  2252. }
  2253. tokenizer->advance();
  2254. cf_if->body_else = alloc_node<BlockNode>();
  2255. cf_if->body_else->parent_block = p_block;
  2256. p_block->sub_blocks.push_back(cf_if->body_else);
  2257. ControlFlowNode *cf_else = alloc_node<ControlFlowNode>();
  2258. cf_else->cf_type = ControlFlowNode::CF_IF;
  2259. //condition
  2260. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2261. if (!condition) {
  2262. if (_recover_from_completion()) {
  2263. break;
  2264. }
  2265. return;
  2266. }
  2267. cf_else->arguments.push_back(condition);
  2268. cf_else->cf_type = ControlFlowNode::CF_IF;
  2269. cf_if->body_else->statements.push_back(cf_else);
  2270. cf_if = cf_else;
  2271. cf_if->body = alloc_node<BlockNode>();
  2272. cf_if->body->parent_block = p_block;
  2273. p_block->sub_blocks.push_back(cf_if->body);
  2274. if (!_enter_indent_block(cf_if->body)) {
  2275. _set_error("Expected indented block after 'elif'");
  2276. p_block->end_line = tokenizer->get_token_line();
  2277. return;
  2278. }
  2279. current_block = cf_else->body;
  2280. _parse_block(cf_else->body, p_static);
  2281. current_block = p_block;
  2282. if (error_set)
  2283. return;
  2284. all_have_return = all_have_return && cf_else->body->has_return;
  2285. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CF_ELSE) {
  2286. if (tab_level.back()->get() > indent_level) {
  2287. _set_error("Invalid indent");
  2288. return;
  2289. }
  2290. tokenizer->advance();
  2291. cf_if->body_else = alloc_node<BlockNode>();
  2292. cf_if->body_else->parent_block = p_block;
  2293. p_block->sub_blocks.push_back(cf_if->body_else);
  2294. if (!_enter_indent_block(cf_if->body_else)) {
  2295. _set_error("Expected indented block after 'else'");
  2296. p_block->end_line = tokenizer->get_token_line();
  2297. return;
  2298. }
  2299. current_block = cf_if->body_else;
  2300. _parse_block(cf_if->body_else, p_static);
  2301. current_block = p_block;
  2302. if (error_set)
  2303. return;
  2304. all_have_return = all_have_return && cf_if->body_else->has_return;
  2305. have_else = true;
  2306. break; //after else, exit
  2307. } else
  2308. break;
  2309. }
  2310. cf_if->body->has_return = all_have_return;
  2311. // If there's no else block, path out of the if might not have a return
  2312. p_block->has_return = all_have_return && have_else;
  2313. } break;
  2314. case GDScriptTokenizer::TK_CF_WHILE: {
  2315. tokenizer->advance();
  2316. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2317. if (!condition) {
  2318. if (_recover_from_completion()) {
  2319. break;
  2320. }
  2321. return;
  2322. }
  2323. ControlFlowNode *cf_while = alloc_node<ControlFlowNode>();
  2324. cf_while->cf_type = ControlFlowNode::CF_WHILE;
  2325. cf_while->arguments.push_back(condition);
  2326. cf_while->body = alloc_node<BlockNode>();
  2327. cf_while->body->parent_block = p_block;
  2328. p_block->sub_blocks.push_back(cf_while->body);
  2329. if (!_enter_indent_block(cf_while->body)) {
  2330. _set_error("Expected indented block after 'while'");
  2331. p_block->end_line = tokenizer->get_token_line();
  2332. return;
  2333. }
  2334. current_block = cf_while->body;
  2335. _parse_block(cf_while->body, p_static);
  2336. current_block = p_block;
  2337. if (error_set)
  2338. return;
  2339. p_block->has_return = cf_while->body->has_return;
  2340. p_block->statements.push_back(cf_while);
  2341. } break;
  2342. case GDScriptTokenizer::TK_CF_FOR: {
  2343. tokenizer->advance();
  2344. if (!tokenizer->is_token_literal(0, true)) {
  2345. _set_error("identifier expected after 'for'");
  2346. }
  2347. IdentifierNode *id = alloc_node<IdentifierNode>();
  2348. id->name = tokenizer->get_token_identifier();
  2349. tokenizer->advance();
  2350. if (tokenizer->get_token() != GDScriptTokenizer::TK_OP_IN) {
  2351. _set_error("'in' expected after identifier");
  2352. return;
  2353. }
  2354. tokenizer->advance();
  2355. Node *container = _parse_and_reduce_expression(p_block, p_static);
  2356. if (!container) {
  2357. if (_recover_from_completion()) {
  2358. break;
  2359. }
  2360. return;
  2361. }
  2362. DataType iter_type;
  2363. iter_type.is_constant = true;
  2364. if (container->type == Node::TYPE_OPERATOR) {
  2365. OperatorNode *op = static_cast<OperatorNode *>(container);
  2366. if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_BUILT_IN_FUNCTION && static_cast<BuiltInFunctionNode *>(op->arguments[0])->function == GDScriptFunctions::GEN_RANGE) {
  2367. //iterating a range, so see if range() can be optimized without allocating memory, by replacing it by vectors (which can work as iterable too!)
  2368. Vector<Node *> args;
  2369. Vector<double> constants;
  2370. bool constant = false;
  2371. for (int i = 1; i < op->arguments.size(); i++) {
  2372. args.push_back(op->arguments[i]);
  2373. if (constant && op->arguments[i]->type == Node::TYPE_CONSTANT) {
  2374. ConstantNode *c = static_cast<ConstantNode *>(op->arguments[i]);
  2375. if (c->value.get_type() == Variant::REAL || c->value.get_type() == Variant::INT) {
  2376. constants.push_back(c->value);
  2377. constant = true;
  2378. }
  2379. } else {
  2380. constant = false;
  2381. }
  2382. }
  2383. if (args.size() > 0 && args.size() < 4) {
  2384. if (constant) {
  2385. ConstantNode *cn = alloc_node<ConstantNode>();
  2386. switch (args.size()) {
  2387. case 1: cn->value = (int)constants[0]; break;
  2388. case 2: cn->value = Vector2(constants[0], constants[1]); break;
  2389. case 3: cn->value = Vector3(constants[0], constants[1], constants[2]); break;
  2390. }
  2391. cn->datatype = _type_from_variant(cn->value);
  2392. container = cn;
  2393. } else {
  2394. OperatorNode *on = alloc_node<OperatorNode>();
  2395. on->op = OperatorNode::OP_CALL;
  2396. TypeNode *tn = alloc_node<TypeNode>();
  2397. on->arguments.push_back(tn);
  2398. switch (args.size()) {
  2399. case 1: tn->vtype = Variant::INT; break;
  2400. case 2: tn->vtype = Variant::VECTOR2; break;
  2401. case 3: tn->vtype = Variant::VECTOR3; break;
  2402. }
  2403. for (int i = 0; i < args.size(); i++) {
  2404. on->arguments.push_back(args[i]);
  2405. }
  2406. container = on;
  2407. }
  2408. }
  2409. iter_type.has_type = true;
  2410. iter_type.kind = DataType::BUILTIN;
  2411. iter_type.builtin_type = Variant::INT;
  2412. }
  2413. }
  2414. ControlFlowNode *cf_for = alloc_node<ControlFlowNode>();
  2415. cf_for->cf_type = ControlFlowNode::CF_FOR;
  2416. cf_for->arguments.push_back(id);
  2417. cf_for->arguments.push_back(container);
  2418. cf_for->body = alloc_node<BlockNode>();
  2419. cf_for->body->parent_block = p_block;
  2420. p_block->sub_blocks.push_back(cf_for->body);
  2421. if (!_enter_indent_block(cf_for->body)) {
  2422. _set_error("Expected indented block after 'for'");
  2423. p_block->end_line = tokenizer->get_token_line();
  2424. return;
  2425. }
  2426. current_block = cf_for->body;
  2427. // this is for checking variable for redefining
  2428. // inside this _parse_block
  2429. LocalVarNode *lv = alloc_node<LocalVarNode>();
  2430. lv->name = id->name;
  2431. lv->line = id->line;
  2432. lv->assignments++;
  2433. id->declared_block = cf_for->body;
  2434. lv->set_datatype(iter_type);
  2435. id->set_datatype(iter_type);
  2436. cf_for->body->variables.insert(id->name, lv);
  2437. _parse_block(cf_for->body, p_static);
  2438. current_block = p_block;
  2439. if (error_set)
  2440. return;
  2441. p_block->has_return = cf_for->body->has_return;
  2442. p_block->statements.push_back(cf_for);
  2443. } break;
  2444. case GDScriptTokenizer::TK_CF_CONTINUE: {
  2445. tokenizer->advance();
  2446. ControlFlowNode *cf_continue = alloc_node<ControlFlowNode>();
  2447. cf_continue->cf_type = ControlFlowNode::CF_CONTINUE;
  2448. p_block->statements.push_back(cf_continue);
  2449. if (!_end_statement()) {
  2450. _set_error("Expected end of statement (continue)");
  2451. return;
  2452. }
  2453. } break;
  2454. case GDScriptTokenizer::TK_CF_BREAK: {
  2455. tokenizer->advance();
  2456. ControlFlowNode *cf_break = alloc_node<ControlFlowNode>();
  2457. cf_break->cf_type = ControlFlowNode::CF_BREAK;
  2458. p_block->statements.push_back(cf_break);
  2459. if (!_end_statement()) {
  2460. _set_error("Expected end of statement (break)");
  2461. return;
  2462. }
  2463. } break;
  2464. case GDScriptTokenizer::TK_CF_RETURN: {
  2465. tokenizer->advance();
  2466. ControlFlowNode *cf_return = alloc_node<ControlFlowNode>();
  2467. cf_return->cf_type = ControlFlowNode::CF_RETURN;
  2468. cf_return->line = tokenizer->get_token_line(-1);
  2469. if (tokenizer->get_token() == GDScriptTokenizer::TK_SEMICOLON || tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE || tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  2470. //expect end of statement
  2471. p_block->statements.push_back(cf_return);
  2472. if (!_end_statement()) {
  2473. return;
  2474. }
  2475. } else {
  2476. //expect expression
  2477. Node *retexpr = _parse_and_reduce_expression(p_block, p_static);
  2478. if (!retexpr) {
  2479. if (_recover_from_completion()) {
  2480. break;
  2481. }
  2482. return;
  2483. }
  2484. cf_return->arguments.push_back(retexpr);
  2485. p_block->statements.push_back(cf_return);
  2486. if (!_end_statement()) {
  2487. _set_error("Expected end of statement after return expression.");
  2488. return;
  2489. }
  2490. }
  2491. p_block->has_return = true;
  2492. } break;
  2493. case GDScriptTokenizer::TK_CF_MATCH: {
  2494. tokenizer->advance();
  2495. MatchNode *match_node = alloc_node<MatchNode>();
  2496. Node *val_to_match = _parse_and_reduce_expression(p_block, p_static);
  2497. if (!val_to_match) {
  2498. if (_recover_from_completion()) {
  2499. break;
  2500. }
  2501. return;
  2502. }
  2503. match_node->val_to_match = val_to_match;
  2504. if (!_enter_indent_block()) {
  2505. _set_error("Expected indented pattern matching block after 'match'");
  2506. return;
  2507. }
  2508. BlockNode *compiled_branches = alloc_node<BlockNode>();
  2509. compiled_branches->parent_block = p_block;
  2510. compiled_branches->parent_class = p_block->parent_class;
  2511. p_block->sub_blocks.push_back(compiled_branches);
  2512. _parse_pattern_block(compiled_branches, match_node->branches, p_static);
  2513. if (error_set) return;
  2514. ControlFlowNode *match_cf_node = alloc_node<ControlFlowNode>();
  2515. match_cf_node->cf_type = ControlFlowNode::CF_MATCH;
  2516. match_cf_node->match = match_node;
  2517. match_cf_node->body = compiled_branches;
  2518. p_block->has_return = p_block->has_return || compiled_branches->has_return;
  2519. p_block->statements.push_back(match_cf_node);
  2520. _end_statement();
  2521. } break;
  2522. case GDScriptTokenizer::TK_PR_ASSERT: {
  2523. tokenizer->advance();
  2524. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2525. if (!condition) {
  2526. if (_recover_from_completion()) {
  2527. break;
  2528. }
  2529. return;
  2530. }
  2531. AssertNode *an = alloc_node<AssertNode>();
  2532. an->condition = condition;
  2533. p_block->statements.push_back(an);
  2534. if (!_end_statement()) {
  2535. _set_error("Expected end of statement after assert.");
  2536. return;
  2537. }
  2538. } break;
  2539. case GDScriptTokenizer::TK_PR_BREAKPOINT: {
  2540. tokenizer->advance();
  2541. BreakpointNode *bn = alloc_node<BreakpointNode>();
  2542. p_block->statements.push_back(bn);
  2543. if (!_end_statement()) {
  2544. _set_error("Expected end of statement after breakpoint.");
  2545. return;
  2546. }
  2547. } break;
  2548. default: {
  2549. Node *expression = _parse_and_reduce_expression(p_block, p_static, false, true);
  2550. if (!expression) {
  2551. if (_recover_from_completion()) {
  2552. break;
  2553. }
  2554. return;
  2555. }
  2556. p_block->statements.push_back(expression);
  2557. if (!_end_statement()) {
  2558. _set_error("Expected end of statement after expression.");
  2559. return;
  2560. }
  2561. } break;
  2562. }
  2563. }
  2564. }
  2565. bool GDScriptParser::_parse_newline() {
  2566. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_EOF && tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE) {
  2567. int indent = tokenizer->get_token_line_indent();
  2568. int current_indent = tab_level.back()->get();
  2569. if (indent > current_indent) {
  2570. _set_error("Unexpected indent.");
  2571. return false;
  2572. }
  2573. if (indent < current_indent) {
  2574. while (indent < current_indent) {
  2575. //exit block
  2576. if (tab_level.size() == 1) {
  2577. _set_error("Invalid indent. BUG?");
  2578. return false;
  2579. }
  2580. tab_level.pop_back();
  2581. if (tab_level.back()->get() < indent) {
  2582. _set_error("Unindent does not match any outer indentation level.");
  2583. return false;
  2584. }
  2585. current_indent = tab_level.back()->get();
  2586. }
  2587. tokenizer->advance();
  2588. return false;
  2589. }
  2590. }
  2591. tokenizer->advance();
  2592. return true;
  2593. }
  2594. void GDScriptParser::_parse_extends(ClassNode *p_class) {
  2595. if (p_class->extends_used) {
  2596. _set_error("'extends' already used for this class.");
  2597. return;
  2598. }
  2599. if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty()) {
  2600. _set_error("'extends' must be used before anything else.");
  2601. return;
  2602. }
  2603. p_class->extends_used = true;
  2604. tokenizer->advance();
  2605. if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token_type() == Variant::OBJECT) {
  2606. p_class->extends_class.push_back(Variant::get_type_name(Variant::OBJECT));
  2607. tokenizer->advance();
  2608. return;
  2609. }
  2610. // see if inheritance happens from a file
  2611. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT) {
  2612. Variant constant = tokenizer->get_token_constant();
  2613. if (constant.get_type() != Variant::STRING) {
  2614. _set_error("'extends' constant must be a string.");
  2615. return;
  2616. }
  2617. p_class->extends_file = constant;
  2618. tokenizer->advance();
  2619. if (tokenizer->get_token() != GDScriptTokenizer::TK_PERIOD) {
  2620. return;
  2621. } else
  2622. tokenizer->advance();
  2623. }
  2624. while (true) {
  2625. switch (tokenizer->get_token()) {
  2626. case GDScriptTokenizer::TK_IDENTIFIER: {
  2627. StringName identifier = tokenizer->get_token_identifier();
  2628. p_class->extends_class.push_back(identifier);
  2629. } break;
  2630. case GDScriptTokenizer::TK_PERIOD:
  2631. break;
  2632. default: {
  2633. _set_error("Invalid 'extends' syntax, expected string constant (path) and/or identifier (parent class).");
  2634. return;
  2635. }
  2636. }
  2637. tokenizer->advance(1);
  2638. switch (tokenizer->get_token()) {
  2639. case GDScriptTokenizer::TK_IDENTIFIER:
  2640. case GDScriptTokenizer::TK_PERIOD:
  2641. continue;
  2642. default:
  2643. return;
  2644. }
  2645. }
  2646. }
  2647. void GDScriptParser::_parse_class(ClassNode *p_class) {
  2648. int indent_level = tab_level.back()->get();
  2649. while (true) {
  2650. GDScriptTokenizer::Token token = tokenizer->get_token();
  2651. if (error_set)
  2652. return;
  2653. if (indent_level > tab_level.back()->get()) {
  2654. p_class->end_line = tokenizer->get_token_line();
  2655. return; //go back a level
  2656. }
  2657. switch (token) {
  2658. case GDScriptTokenizer::TK_EOF:
  2659. p_class->end_line = tokenizer->get_token_line();
  2660. case GDScriptTokenizer::TK_ERROR: {
  2661. return; //go back
  2662. //end of file!
  2663. } break;
  2664. case GDScriptTokenizer::TK_NEWLINE: {
  2665. if (!_parse_newline()) {
  2666. if (!error_set) {
  2667. p_class->end_line = tokenizer->get_token_line();
  2668. }
  2669. return;
  2670. }
  2671. } break;
  2672. case GDScriptTokenizer::TK_PR_EXTENDS: {
  2673. _parse_extends(p_class);
  2674. if (error_set)
  2675. return;
  2676. if (!_end_statement()) {
  2677. _set_error("Expected end of statement after extends");
  2678. return;
  2679. }
  2680. } break;
  2681. case GDScriptTokenizer::TK_PR_CLASS_NAME: {
  2682. if (p_class->owner) {
  2683. _set_error("'class_name' is only valid for the main class namespace.");
  2684. return;
  2685. }
  2686. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_IDENTIFIER) {
  2687. _set_error("'class_name' syntax: 'class_name <UniqueName>'");
  2688. return;
  2689. }
  2690. p_class->name = tokenizer->get_token_identifier(1);
  2691. if (self_path != String() && ScriptServer::is_global_class(p_class->name) && ScriptServer::get_global_class_path(p_class->name) != self_path) {
  2692. _set_error("Unique global class '" + p_class->name + "' already exists at path: " + ScriptServer::get_global_class_path(p_class->name));
  2693. return;
  2694. }
  2695. if (ClassDB::class_exists(p_class->name)) {
  2696. _set_error("Class '" + p_class->name + "' shadows a native class.");
  2697. return;
  2698. }
  2699. tokenizer->advance(2);
  2700. } break;
  2701. case GDScriptTokenizer::TK_PR_TOOL: {
  2702. if (p_class->tool) {
  2703. _set_error("tool used more than once");
  2704. return;
  2705. }
  2706. p_class->tool = true;
  2707. tokenizer->advance();
  2708. } break;
  2709. case GDScriptTokenizer::TK_PR_CLASS: {
  2710. //class inside class :D
  2711. StringName name;
  2712. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_IDENTIFIER) {
  2713. _set_error("'class' syntax: 'class <Name>:' or 'class <Name> extends <BaseClass>:'");
  2714. return;
  2715. }
  2716. name = tokenizer->get_token_identifier(1);
  2717. tokenizer->advance(2);
  2718. // Check if name is shadowing something else
  2719. if (ClassDB::class_exists(name) || ClassDB::class_exists("_" + name.operator String())) {
  2720. _set_error("Class '" + String(name) + "' shadows a native class.");
  2721. return;
  2722. }
  2723. if (ScriptServer::is_global_class(name)) {
  2724. _set_error("Can't override name of unique global class '" + name + "' already exists at path: " + ScriptServer::get_global_class_path(p_class->name));
  2725. return;
  2726. }
  2727. ClassNode *outer_class = p_class;
  2728. while (outer_class) {
  2729. for (int i = 0; i < outer_class->subclasses.size(); i++) {
  2730. if (outer_class->subclasses[i]->name == name) {
  2731. _set_error("Another class named '" + String(name) + "' already exists in this scope (at line " + itos(outer_class->subclasses[i]->line) + ").");
  2732. return;
  2733. }
  2734. }
  2735. if (outer_class->constant_expressions.has(name)) {
  2736. _set_error("A constant named '" + String(name) + "' already exists in the outer class scope (at line" + itos(outer_class->constant_expressions[name].expression->line) + ").");
  2737. return;
  2738. }
  2739. outer_class = outer_class->owner;
  2740. }
  2741. ClassNode *newclass = alloc_node<ClassNode>();
  2742. newclass->initializer = alloc_node<BlockNode>();
  2743. newclass->initializer->parent_class = newclass;
  2744. newclass->ready = alloc_node<BlockNode>();
  2745. newclass->ready->parent_class = newclass;
  2746. newclass->name = name;
  2747. newclass->owner = p_class;
  2748. p_class->subclasses.push_back(newclass);
  2749. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_EXTENDS) {
  2750. _parse_extends(newclass);
  2751. if (error_set)
  2752. return;
  2753. }
  2754. if (!_enter_indent_block()) {
  2755. _set_error("Indented block expected.");
  2756. return;
  2757. }
  2758. current_class = newclass;
  2759. _parse_class(newclass);
  2760. current_class = p_class;
  2761. } break;
  2762. /* this is for functions....
  2763. case GDScriptTokenizer::TK_CF_PASS: {
  2764. tokenizer->advance(1);
  2765. } break;
  2766. */
  2767. case GDScriptTokenizer::TK_PR_STATIC: {
  2768. tokenizer->advance();
  2769. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  2770. _set_error("Expected 'func'.");
  2771. return;
  2772. }
  2773. }; //fallthrough to function
  2774. case GDScriptTokenizer::TK_PR_FUNCTION: {
  2775. bool _static = false;
  2776. pending_newline = -1;
  2777. if (tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_STATIC) {
  2778. _static = true;
  2779. }
  2780. tokenizer->advance();
  2781. StringName name;
  2782. if (_get_completable_identifier(COMPLETION_VIRTUAL_FUNC, name)) {
  2783. }
  2784. if (name == StringName()) {
  2785. _set_error("Expected identifier after 'func' (syntax: 'func <identifier>([arguments]):' ).");
  2786. return;
  2787. }
  2788. for (int i = 0; i < p_class->functions.size(); i++) {
  2789. if (p_class->functions[i]->name == name) {
  2790. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->functions[i]->line) + ").");
  2791. }
  2792. }
  2793. for (int i = 0; i < p_class->static_functions.size(); i++) {
  2794. if (p_class->static_functions[i]->name == name) {
  2795. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->static_functions[i]->line) + ").");
  2796. }
  2797. }
  2798. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2799. _set_error("Expected '(' after identifier (syntax: 'func <identifier>([arguments]):' ).");
  2800. return;
  2801. }
  2802. tokenizer->advance();
  2803. Vector<StringName> arguments;
  2804. Vector<DataType> argument_types;
  2805. Vector<Node *> default_values;
  2806. int fnline = tokenizer->get_token_line();
  2807. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2808. //has arguments
  2809. bool defaulting = false;
  2810. while (true) {
  2811. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2812. tokenizer->advance();
  2813. continue;
  2814. }
  2815. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_VAR) {
  2816. tokenizer->advance(); //var before the identifier is allowed
  2817. }
  2818. if (!tokenizer->is_token_literal(0, true)) {
  2819. _set_error("Expected identifier for argument.");
  2820. return;
  2821. }
  2822. StringName argname = tokenizer->get_token_identifier();
  2823. arguments.push_back(argname);
  2824. tokenizer->advance();
  2825. DataType argtype;
  2826. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  2827. if (!_parse_type(argtype)) {
  2828. _set_error("Expected type for argument.");
  2829. return;
  2830. }
  2831. }
  2832. argument_types.push_back(argtype);
  2833. if (defaulting && tokenizer->get_token() != GDScriptTokenizer::TK_OP_ASSIGN) {
  2834. _set_error("Default parameter expected.");
  2835. return;
  2836. }
  2837. //tokenizer->advance();
  2838. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  2839. defaulting = true;
  2840. tokenizer->advance(1);
  2841. Node *defval = _parse_and_reduce_expression(p_class, _static);
  2842. if (!defval || error_set)
  2843. return;
  2844. OperatorNode *on = alloc_node<OperatorNode>();
  2845. on->op = OperatorNode::OP_ASSIGN;
  2846. on->line = fnline;
  2847. IdentifierNode *in = alloc_node<IdentifierNode>();
  2848. in->name = argname;
  2849. in->line = fnline;
  2850. on->arguments.push_back(in);
  2851. on->arguments.push_back(defval);
  2852. /* no ..
  2853. if (defval->type!=Node::TYPE_CONSTANT) {
  2854. _set_error("default argument must be constant");
  2855. }
  2856. */
  2857. default_values.push_back(on);
  2858. }
  2859. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2860. tokenizer->advance();
  2861. }
  2862. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2863. tokenizer->advance();
  2864. continue;
  2865. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2866. _set_error("Expected ',' or ')'.");
  2867. return;
  2868. }
  2869. break;
  2870. }
  2871. }
  2872. tokenizer->advance();
  2873. BlockNode *block = alloc_node<BlockNode>();
  2874. block->parent_class = p_class;
  2875. if (name == "_init") {
  2876. if (_static) {
  2877. _set_error("Constructor cannot be static.");
  2878. return;
  2879. }
  2880. if (p_class->extends_used) {
  2881. OperatorNode *cparent = alloc_node<OperatorNode>();
  2882. cparent->op = OperatorNode::OP_PARENT_CALL;
  2883. block->statements.push_back(cparent);
  2884. IdentifierNode *id = alloc_node<IdentifierNode>();
  2885. id->name = "_init";
  2886. cparent->arguments.push_back(id);
  2887. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
  2888. tokenizer->advance();
  2889. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2890. _set_error("expected '(' for parent constructor arguments.");
  2891. return;
  2892. }
  2893. tokenizer->advance();
  2894. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2895. //has arguments
  2896. parenthesis++;
  2897. while (true) {
  2898. Node *arg = _parse_and_reduce_expression(p_class, _static);
  2899. cparent->arguments.push_back(arg);
  2900. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2901. tokenizer->advance();
  2902. continue;
  2903. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2904. _set_error("Expected ',' or ')'.");
  2905. return;
  2906. }
  2907. break;
  2908. }
  2909. parenthesis--;
  2910. }
  2911. tokenizer->advance();
  2912. }
  2913. } else {
  2914. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
  2915. _set_error("Parent constructor call found for a class without inheritance.");
  2916. return;
  2917. }
  2918. }
  2919. }
  2920. DataType return_type;
  2921. if (tokenizer->get_token() == GDScriptTokenizer::TK_FORWARD_ARROW) {
  2922. if (!_parse_type(return_type, true)) {
  2923. _set_error("Expected return type for function.");
  2924. return;
  2925. }
  2926. }
  2927. if (!_enter_indent_block(block)) {
  2928. _set_error("Indented block expected.");
  2929. return;
  2930. }
  2931. FunctionNode *function = alloc_node<FunctionNode>();
  2932. function->name = name;
  2933. function->return_type = return_type;
  2934. function->arguments = arguments;
  2935. function->argument_types = argument_types;
  2936. function->default_values = default_values;
  2937. function->_static = _static;
  2938. function->line = fnline;
  2939. function->rpc_mode = rpc_mode;
  2940. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  2941. if (_static)
  2942. p_class->static_functions.push_back(function);
  2943. else
  2944. p_class->functions.push_back(function);
  2945. current_function = function;
  2946. function->body = block;
  2947. current_block = block;
  2948. _parse_block(block, _static);
  2949. current_block = NULL;
  2950. //arguments
  2951. } break;
  2952. case GDScriptTokenizer::TK_PR_SIGNAL: {
  2953. tokenizer->advance();
  2954. if (!tokenizer->is_token_literal()) {
  2955. _set_error("Expected identifier after 'signal'.");
  2956. return;
  2957. }
  2958. ClassNode::Signal sig;
  2959. sig.name = tokenizer->get_token_identifier();
  2960. tokenizer->advance();
  2961. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2962. tokenizer->advance();
  2963. while (true) {
  2964. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2965. tokenizer->advance();
  2966. continue;
  2967. }
  2968. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2969. tokenizer->advance();
  2970. break;
  2971. }
  2972. if (!tokenizer->is_token_literal(0, true)) {
  2973. _set_error("Expected identifier in signal argument.");
  2974. return;
  2975. }
  2976. sig.arguments.push_back(tokenizer->get_token_identifier());
  2977. tokenizer->advance();
  2978. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2979. tokenizer->advance();
  2980. }
  2981. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2982. tokenizer->advance();
  2983. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2984. _set_error("Expected ',' or ')' after signal parameter identifier.");
  2985. return;
  2986. }
  2987. }
  2988. }
  2989. p_class->_signals.push_back(sig);
  2990. if (!_end_statement()) {
  2991. _set_error("Expected end of statement (signal)");
  2992. return;
  2993. }
  2994. } break;
  2995. case GDScriptTokenizer::TK_PR_EXPORT: {
  2996. tokenizer->advance();
  2997. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2998. tokenizer->advance();
  2999. String hint_prefix = "";
  3000. bool is_arrayed = false;
  3001. while (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE &&
  3002. tokenizer->get_token_type() == Variant::ARRAY &&
  3003. tokenizer->get_token(1) == GDScriptTokenizer::TK_COMMA) {
  3004. tokenizer->advance(); // Array
  3005. tokenizer->advance(); // Comma
  3006. if (is_arrayed) {
  3007. hint_prefix += itos(Variant::ARRAY) + ":";
  3008. } else {
  3009. is_arrayed = true;
  3010. }
  3011. }
  3012. if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE) {
  3013. Variant::Type type = tokenizer->get_token_type();
  3014. if (type == Variant::NIL) {
  3015. _set_error("Can't export null type.");
  3016. return;
  3017. }
  3018. if (type == Variant::OBJECT) {
  3019. _set_error("Can't export raw object type.");
  3020. return;
  3021. }
  3022. current_export.type = type;
  3023. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3024. tokenizer->advance();
  3025. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3026. // hint expected next!
  3027. tokenizer->advance();
  3028. switch (type) {
  3029. case Variant::INT: {
  3030. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  3031. //current_export.hint=PROPERTY_HINT_ALL_FLAGS;
  3032. tokenizer->advance();
  3033. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3034. break;
  3035. }
  3036. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3037. _set_error("Expected ')' or ',' in bit flags hint.");
  3038. return;
  3039. }
  3040. current_export.hint = PROPERTY_HINT_FLAGS;
  3041. tokenizer->advance();
  3042. bool first = true;
  3043. while (true) {
  3044. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3045. current_export = PropertyInfo();
  3046. _set_error("Expected a string constant in named bit flags hint.");
  3047. return;
  3048. }
  3049. String c = tokenizer->get_token_constant();
  3050. if (!first)
  3051. current_export.hint_string += ",";
  3052. else
  3053. first = false;
  3054. current_export.hint_string += c.xml_escape();
  3055. tokenizer->advance();
  3056. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3057. break;
  3058. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3059. current_export = PropertyInfo();
  3060. _set_error("Expected ')' or ',' in named bit flags hint.");
  3061. return;
  3062. }
  3063. tokenizer->advance();
  3064. }
  3065. break;
  3066. }
  3067. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3068. //enumeration
  3069. current_export.hint = PROPERTY_HINT_ENUM;
  3070. bool first = true;
  3071. while (true) {
  3072. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3073. current_export = PropertyInfo();
  3074. _set_error("Expected a string constant in enumeration hint.");
  3075. return;
  3076. }
  3077. String c = tokenizer->get_token_constant();
  3078. if (!first)
  3079. current_export.hint_string += ",";
  3080. else
  3081. first = false;
  3082. current_export.hint_string += c.xml_escape();
  3083. tokenizer->advance();
  3084. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3085. break;
  3086. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3087. current_export = PropertyInfo();
  3088. _set_error("Expected ')' or ',' in enumeration hint.");
  3089. return;
  3090. }
  3091. tokenizer->advance();
  3092. }
  3093. break;
  3094. }
  3095. }; //fallthrough to use the same
  3096. case Variant::REAL: {
  3097. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EASE") {
  3098. current_export.hint = PROPERTY_HINT_EXP_EASING;
  3099. tokenizer->advance();
  3100. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3101. _set_error("Expected ')' in hint.");
  3102. return;
  3103. }
  3104. break;
  3105. }
  3106. // range
  3107. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EXP") {
  3108. current_export.hint = PROPERTY_HINT_EXP_RANGE;
  3109. tokenizer->advance();
  3110. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3111. break;
  3112. else if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3113. _set_error("Expected ')' or ',' in exponential range hint.");
  3114. return;
  3115. }
  3116. tokenizer->advance();
  3117. } else
  3118. current_export.hint = PROPERTY_HINT_RANGE;
  3119. float sign = 1.0;
  3120. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  3121. sign = -1;
  3122. tokenizer->advance();
  3123. }
  3124. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  3125. current_export = PropertyInfo();
  3126. _set_error("Expected a range in numeric hint.");
  3127. return;
  3128. }
  3129. current_export.hint_string = rtos(sign * double(tokenizer->get_token_constant()));
  3130. tokenizer->advance();
  3131. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3132. current_export.hint_string = "0," + current_export.hint_string;
  3133. break;
  3134. }
  3135. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3136. current_export = PropertyInfo();
  3137. _set_error("Expected ',' or ')' in numeric range hint.");
  3138. return;
  3139. }
  3140. tokenizer->advance();
  3141. sign = 1.0;
  3142. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  3143. sign = -1;
  3144. tokenizer->advance();
  3145. }
  3146. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  3147. current_export = PropertyInfo();
  3148. _set_error("Expected a number as upper bound in numeric range hint.");
  3149. return;
  3150. }
  3151. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  3152. tokenizer->advance();
  3153. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3154. break;
  3155. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3156. current_export = PropertyInfo();
  3157. _set_error("Expected ',' or ')' in numeric range hint.");
  3158. return;
  3159. }
  3160. tokenizer->advance();
  3161. sign = 1.0;
  3162. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  3163. sign = -1;
  3164. tokenizer->advance();
  3165. }
  3166. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  3167. current_export = PropertyInfo();
  3168. _set_error("Expected a number as step in numeric range hint.");
  3169. return;
  3170. }
  3171. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  3172. tokenizer->advance();
  3173. } break;
  3174. case Variant::STRING: {
  3175. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3176. //enumeration
  3177. current_export.hint = PROPERTY_HINT_ENUM;
  3178. bool first = true;
  3179. while (true) {
  3180. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3181. current_export = PropertyInfo();
  3182. _set_error("Expected a string constant in enumeration hint.");
  3183. return;
  3184. }
  3185. String c = tokenizer->get_token_constant();
  3186. if (!first)
  3187. current_export.hint_string += ",";
  3188. else
  3189. first = false;
  3190. current_export.hint_string += c.xml_escape();
  3191. tokenizer->advance();
  3192. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3193. break;
  3194. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3195. current_export = PropertyInfo();
  3196. _set_error("Expected ')' or ',' in enumeration hint.");
  3197. return;
  3198. }
  3199. tokenizer->advance();
  3200. }
  3201. break;
  3202. }
  3203. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "DIR") {
  3204. tokenizer->advance();
  3205. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3206. current_export.hint = PROPERTY_HINT_DIR;
  3207. else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3208. tokenizer->advance();
  3209. if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier() == "GLOBAL")) {
  3210. _set_error("Expected 'GLOBAL' after comma in directory hint.");
  3211. return;
  3212. }
  3213. if (!p_class->tool) {
  3214. _set_error("Global filesystem hints may only be used in tool scripts.");
  3215. return;
  3216. }
  3217. current_export.hint = PROPERTY_HINT_GLOBAL_DIR;
  3218. tokenizer->advance();
  3219. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3220. _set_error("Expected ')' in hint.");
  3221. return;
  3222. }
  3223. } else {
  3224. _set_error("Expected ')' or ',' in hint.");
  3225. return;
  3226. }
  3227. break;
  3228. }
  3229. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FILE") {
  3230. current_export.hint = PROPERTY_HINT_FILE;
  3231. tokenizer->advance();
  3232. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3233. tokenizer->advance();
  3234. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "GLOBAL") {
  3235. if (!p_class->tool) {
  3236. _set_error("Global filesystem hints may only be used in tool scripts.");
  3237. return;
  3238. }
  3239. current_export.hint = PROPERTY_HINT_GLOBAL_FILE;
  3240. tokenizer->advance();
  3241. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3242. break;
  3243. else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA)
  3244. tokenizer->advance();
  3245. else {
  3246. _set_error("Expected ')' or ',' in hint.");
  3247. return;
  3248. }
  3249. }
  3250. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3251. if (current_export.hint == PROPERTY_HINT_GLOBAL_FILE)
  3252. _set_error("Expected string constant with filter");
  3253. else
  3254. _set_error("Expected 'GLOBAL' or string constant with filter");
  3255. return;
  3256. }
  3257. current_export.hint_string = tokenizer->get_token_constant();
  3258. tokenizer->advance();
  3259. }
  3260. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3261. _set_error("Expected ')' in hint.");
  3262. return;
  3263. }
  3264. break;
  3265. }
  3266. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "MULTILINE") {
  3267. current_export.hint = PROPERTY_HINT_MULTILINE_TEXT;
  3268. tokenizer->advance();
  3269. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3270. _set_error("Expected ')' in hint.");
  3271. return;
  3272. }
  3273. break;
  3274. }
  3275. } break;
  3276. case Variant::COLOR: {
  3277. if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER) {
  3278. current_export = PropertyInfo();
  3279. _set_error("Color type hint expects RGB or RGBA as hints");
  3280. return;
  3281. }
  3282. String identifier = tokenizer->get_token_identifier();
  3283. if (identifier == "RGB") {
  3284. current_export.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  3285. } else if (identifier == "RGBA") {
  3286. //none
  3287. } else {
  3288. current_export = PropertyInfo();
  3289. _set_error("Color type hint expects RGB or RGBA as hints");
  3290. return;
  3291. }
  3292. tokenizer->advance();
  3293. } break;
  3294. default: {
  3295. current_export = PropertyInfo();
  3296. _set_error("Type '" + Variant::get_type_name(type) + "' can't take hints.");
  3297. return;
  3298. } break;
  3299. }
  3300. }
  3301. } else {
  3302. parenthesis++;
  3303. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3304. if (!subexpr) {
  3305. if (_recover_from_completion()) {
  3306. break;
  3307. }
  3308. return;
  3309. }
  3310. parenthesis--;
  3311. if (subexpr->type != Node::TYPE_CONSTANT) {
  3312. current_export = PropertyInfo();
  3313. _set_error("Expected a constant expression.");
  3314. }
  3315. Variant constant = static_cast<ConstantNode *>(subexpr)->value;
  3316. if (constant.get_type() == Variant::OBJECT) {
  3317. GDScriptNativeClass *native_class = Object::cast_to<GDScriptNativeClass>(constant);
  3318. if (native_class && ClassDB::is_parent_class(native_class->get_name(), "Resource")) {
  3319. current_export.type = Variant::OBJECT;
  3320. current_export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3321. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3322. current_export.hint_string = native_class->get_name();
  3323. } else {
  3324. current_export = PropertyInfo();
  3325. _set_error("Export hint not a resource type.");
  3326. }
  3327. } else if (constant.get_type() == Variant::DICTIONARY) {
  3328. // Enumeration
  3329. bool is_flags = false;
  3330. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3331. tokenizer->advance();
  3332. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  3333. is_flags = true;
  3334. tokenizer->advance();
  3335. } else {
  3336. current_export = PropertyInfo();
  3337. _set_error("Expected 'FLAGS' after comma.");
  3338. }
  3339. }
  3340. current_export.type = Variant::INT;
  3341. current_export.hint = is_flags ? PROPERTY_HINT_FLAGS : PROPERTY_HINT_ENUM;
  3342. Dictionary enum_values = constant;
  3343. List<Variant> keys;
  3344. enum_values.get_key_list(&keys);
  3345. bool first = true;
  3346. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  3347. if (enum_values[E->get()].get_type() == Variant::INT) {
  3348. if (!first)
  3349. current_export.hint_string += ",";
  3350. else
  3351. first = false;
  3352. current_export.hint_string += E->get().operator String().camelcase_to_underscore(true).capitalize().xml_escape();
  3353. if (!is_flags) {
  3354. current_export.hint_string += ":";
  3355. current_export.hint_string += enum_values[E->get()].operator String().xml_escape();
  3356. }
  3357. }
  3358. }
  3359. } else {
  3360. current_export = PropertyInfo();
  3361. _set_error("Expected type for export.");
  3362. return;
  3363. }
  3364. }
  3365. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3366. current_export = PropertyInfo();
  3367. _set_error("Expected ')' or ',' after export hint.");
  3368. return;
  3369. }
  3370. if (is_arrayed) {
  3371. hint_prefix += itos(current_export.type);
  3372. if (current_export.hint) {
  3373. hint_prefix += "/" + itos(current_export.hint);
  3374. }
  3375. current_export.hint_string = hint_prefix + ":" + current_export.hint_string;
  3376. current_export.hint = PROPERTY_HINT_TYPE_STRING;
  3377. current_export.type = Variant::ARRAY;
  3378. }
  3379. tokenizer->advance();
  3380. }
  3381. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_ONREADY && tokenizer->get_token() != GDScriptTokenizer::TK_PR_REMOTE && tokenizer->get_token() != GDScriptTokenizer::TK_PR_MASTER && tokenizer->get_token() != GDScriptTokenizer::TK_PR_SLAVE && tokenizer->get_token() != GDScriptTokenizer::TK_PR_SYNC && tokenizer->get_token() != GDScriptTokenizer::TK_PR_REMOTESYNC && tokenizer->get_token() != GDScriptTokenizer::TK_PR_MASTERSYNC && tokenizer->get_token() != GDScriptTokenizer::TK_PR_SLAVESYNC) {
  3382. current_export = PropertyInfo();
  3383. _set_error("Expected 'var', 'onready', 'remote', 'master', 'slave', 'sync', 'remotesync', 'mastersync', 'slavesync'.");
  3384. return;
  3385. }
  3386. continue;
  3387. } break;
  3388. case GDScriptTokenizer::TK_PR_ONREADY: {
  3389. //may be fallthrough from export, ignore if so
  3390. tokenizer->advance();
  3391. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3392. _set_error("Expected 'var'.");
  3393. return;
  3394. }
  3395. continue;
  3396. } break;
  3397. case GDScriptTokenizer::TK_PR_REMOTE: {
  3398. //may be fallthrough from export, ignore if so
  3399. tokenizer->advance();
  3400. if (current_export.type) {
  3401. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3402. _set_error("Expected 'var'.");
  3403. return;
  3404. }
  3405. } else {
  3406. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3407. _set_error("Expected 'var' or 'func'.");
  3408. return;
  3409. }
  3410. }
  3411. rpc_mode = MultiplayerAPI::RPC_MODE_REMOTE;
  3412. continue;
  3413. } break;
  3414. case GDScriptTokenizer::TK_PR_MASTER: {
  3415. //may be fallthrough from export, ignore if so
  3416. tokenizer->advance();
  3417. if (current_export.type) {
  3418. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3419. _set_error("Expected 'var'.");
  3420. return;
  3421. }
  3422. } else {
  3423. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3424. _set_error("Expected 'var' or 'func'.");
  3425. return;
  3426. }
  3427. }
  3428. rpc_mode = MultiplayerAPI::RPC_MODE_MASTER;
  3429. continue;
  3430. } break;
  3431. case GDScriptTokenizer::TK_PR_SLAVE: {
  3432. //may be fallthrough from export, ignore if so
  3433. tokenizer->advance();
  3434. if (current_export.type) {
  3435. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3436. _set_error("Expected 'var'.");
  3437. return;
  3438. }
  3439. } else {
  3440. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3441. _set_error("Expected 'var' or 'func'.");
  3442. return;
  3443. }
  3444. }
  3445. rpc_mode = MultiplayerAPI::RPC_MODE_SLAVE;
  3446. continue;
  3447. } break;
  3448. case GDScriptTokenizer::TK_PR_REMOTESYNC:
  3449. case GDScriptTokenizer::TK_PR_SYNC: {
  3450. //may be fallthrough from export, ignore if so
  3451. tokenizer->advance();
  3452. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3453. if (current_export.type)
  3454. _set_error("Expected 'var'.");
  3455. else
  3456. _set_error("Expected 'var' or 'func'.");
  3457. return;
  3458. }
  3459. rpc_mode = MultiplayerAPI::RPC_MODE_SYNC;
  3460. continue;
  3461. } break;
  3462. case GDScriptTokenizer::TK_PR_MASTERSYNC: {
  3463. //may be fallthrough from export, ignore if so
  3464. tokenizer->advance();
  3465. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3466. if (current_export.type)
  3467. _set_error("Expected 'var'.");
  3468. else
  3469. _set_error("Expected 'var' or 'func'.");
  3470. return;
  3471. }
  3472. rpc_mode = MultiplayerAPI::RPC_MODE_MASTERSYNC;
  3473. continue;
  3474. } break;
  3475. case GDScriptTokenizer::TK_PR_SLAVESYNC: {
  3476. //may be fallthrough from export, ignore if so
  3477. tokenizer->advance();
  3478. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3479. if (current_export.type)
  3480. _set_error("Expected 'var'.");
  3481. else
  3482. _set_error("Expected 'var' or 'func'.");
  3483. return;
  3484. }
  3485. rpc_mode = MultiplayerAPI::RPC_MODE_SLAVESYNC;
  3486. continue;
  3487. } break;
  3488. case GDScriptTokenizer::TK_PR_VAR: {
  3489. //variale declaration and (eventual) initialization
  3490. ClassNode::Member member;
  3491. bool autoexport = tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_EXPORT;
  3492. if (current_export.type != Variant::NIL) {
  3493. member._export = current_export;
  3494. current_export = PropertyInfo();
  3495. }
  3496. bool onready = tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_ONREADY;
  3497. tokenizer->advance();
  3498. if (!tokenizer->is_token_literal(0, true)) {
  3499. _set_error("Expected identifier for member variable name.");
  3500. return;
  3501. }
  3502. member.identifier = tokenizer->get_token_literal();
  3503. member.expression = NULL;
  3504. member._export.name = member.identifier;
  3505. member.line = tokenizer->get_token_line();
  3506. member.rpc_mode = rpc_mode;
  3507. if (current_class->constant_expressions.has(member.identifier)) {
  3508. _set_error("A constant named '" + String(member.identifier) + "' alread exists in this class (at line: " +
  3509. itos(current_class->constant_expressions[member.identifier].expression->line) + ").");
  3510. return;
  3511. }
  3512. for (int i = 0; i < current_class->variables.size(); i++) {
  3513. if (current_class->variables[i].identifier == member.identifier) {
  3514. _set_error("Variable '" + String(member.identifier) + "' alread exists in this class (at line: " +
  3515. itos(current_class->variables[i].line) + ").");
  3516. return;
  3517. }
  3518. }
  3519. tokenizer->advance();
  3520. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  3521. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  3522. if (!_parse_type(member.data_type)) {
  3523. _set_error("Expected type for class variable.");
  3524. return;
  3525. }
  3526. }
  3527. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  3528. #ifdef DEBUG_ENABLED
  3529. int line = tokenizer->get_token_line();
  3530. #endif
  3531. tokenizer->advance();
  3532. Node *subexpr = _parse_and_reduce_expression(p_class, false, autoexport || member._export.type != Variant::NIL);
  3533. if (!subexpr) {
  3534. if (_recover_from_completion()) {
  3535. break;
  3536. }
  3537. return;
  3538. }
  3539. //discourage common error
  3540. if (!onready && subexpr->type == Node::TYPE_OPERATOR) {
  3541. OperatorNode *op = static_cast<OperatorNode *>(subexpr);
  3542. if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_SELF && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  3543. IdentifierNode *id = static_cast<IdentifierNode *>(op->arguments[1]);
  3544. if (id->name == "get_node") {
  3545. _set_error("Use 'onready var " + String(member.identifier) + " = get_node(..)' instead");
  3546. return;
  3547. }
  3548. }
  3549. }
  3550. member.expression = subexpr;
  3551. if (autoexport && !member.data_type.has_type) {
  3552. if (subexpr->type != Node::TYPE_CONSTANT) {
  3553. _set_error("Type-less export needs a constant expression assigned to infer type.");
  3554. return;
  3555. }
  3556. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  3557. if (cn->value.get_type() == Variant::NIL) {
  3558. _set_error("Can't accept a null constant expression for inferring export type.");
  3559. return;
  3560. }
  3561. member._export.type = cn->value.get_type();
  3562. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3563. if (cn->value.get_type() == Variant::OBJECT) {
  3564. Object *obj = cn->value;
  3565. Resource *res = Object::cast_to<Resource>(obj);
  3566. if (res == NULL) {
  3567. _set_error("Exported constant not a type or resource.");
  3568. return;
  3569. }
  3570. member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3571. member._export.hint_string = res->get_class();
  3572. }
  3573. }
  3574. #ifdef TOOLS_ENABLED
  3575. if (subexpr->type == Node::TYPE_CONSTANT && member._export.type != Variant::NIL) {
  3576. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  3577. if (cn->value.get_type() != Variant::NIL) {
  3578. member.default_value = cn->value;
  3579. }
  3580. }
  3581. #endif
  3582. IdentifierNode *id = alloc_node<IdentifierNode>();
  3583. id->name = member.identifier;
  3584. OperatorNode *op = alloc_node<OperatorNode>();
  3585. op->op = OperatorNode::OP_INIT_ASSIGN;
  3586. op->arguments.push_back(id);
  3587. op->arguments.push_back(subexpr);
  3588. #ifdef DEBUG_ENABLED
  3589. NewLineNode *nl = alloc_node<NewLineNode>();
  3590. nl->line = line;
  3591. if (onready)
  3592. p_class->ready->statements.push_back(nl);
  3593. else
  3594. p_class->initializer->statements.push_back(nl);
  3595. #endif
  3596. if (onready)
  3597. p_class->ready->statements.push_back(op);
  3598. else
  3599. p_class->initializer->statements.push_back(op);
  3600. member.initial_assignment = op;
  3601. } else {
  3602. if (autoexport && !member.data_type.has_type) {
  3603. _set_error("Type-less export needs a constant expression assigned to infer type.");
  3604. return;
  3605. }
  3606. }
  3607. if (autoexport && member.data_type.has_type) {
  3608. if (member.data_type.kind == DataType::BUILTIN) {
  3609. member._export.type = member.data_type.builtin_type;
  3610. } else if (member.data_type.kind == DataType::NATIVE) {
  3611. if (ClassDB::is_parent_class(member.data_type.native_type, "Resource")) {
  3612. member._export.type = Variant::OBJECT;
  3613. member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3614. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3615. member._export.class_name = member.data_type.native_type;
  3616. } else {
  3617. _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line);
  3618. return;
  3619. }
  3620. } else {
  3621. _set_error("Invalid export type. Only built-in and native resource types can be exported.", member.line);
  3622. return;
  3623. }
  3624. }
  3625. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_SETGET) {
  3626. tokenizer->advance();
  3627. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3628. //just comma means using only getter
  3629. if (!tokenizer->is_token_literal()) {
  3630. _set_error("Expected identifier for setter function after 'setget'.");
  3631. }
  3632. member.setter = tokenizer->get_token_literal();
  3633. tokenizer->advance();
  3634. }
  3635. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3636. //there is a getter
  3637. tokenizer->advance();
  3638. if (!tokenizer->is_token_literal()) {
  3639. _set_error("Expected identifier for getter function after ','.");
  3640. }
  3641. member.getter = tokenizer->get_token_literal();
  3642. tokenizer->advance();
  3643. }
  3644. }
  3645. p_class->variables.push_back(member);
  3646. if (!_end_statement()) {
  3647. _set_error("Expected end of statement (continue)");
  3648. return;
  3649. }
  3650. } break;
  3651. case GDScriptTokenizer::TK_PR_CONST: {
  3652. // constant declaration and initialization
  3653. ClassNode::Constant constant;
  3654. tokenizer->advance();
  3655. if (!tokenizer->is_token_literal(0, true)) {
  3656. _set_error("Expected name (identifier) for constant.");
  3657. return;
  3658. }
  3659. StringName const_id = tokenizer->get_token_literal();
  3660. int line = tokenizer->get_token_line();
  3661. if (current_class->constant_expressions.has(const_id)) {
  3662. _set_error("Constant '" + String(const_id) + "' alread exists in this class (at line: " +
  3663. itos(current_class->constant_expressions[const_id].expression->line) + ").");
  3664. return;
  3665. }
  3666. for (int i = 0; i < current_class->variables.size(); i++) {
  3667. if (current_class->variables[i].identifier == const_id) {
  3668. _set_error("A variable named '" + String(const_id) + "' alread exists in this class (at line: " +
  3669. itos(current_class->variables[i].line) + ").");
  3670. return;
  3671. }
  3672. }
  3673. tokenizer->advance();
  3674. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  3675. if (!_parse_type(constant.type)) {
  3676. _set_error("Expected type for class constant.");
  3677. return;
  3678. }
  3679. }
  3680. if (tokenizer->get_token() != GDScriptTokenizer::TK_OP_ASSIGN) {
  3681. _set_error("Constant expects assignment.");
  3682. return;
  3683. }
  3684. tokenizer->advance();
  3685. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3686. if (!subexpr) {
  3687. if (_recover_from_completion()) {
  3688. break;
  3689. }
  3690. return;
  3691. }
  3692. if (subexpr->type != Node::TYPE_CONSTANT) {
  3693. _set_error("Expected constant expression", line);
  3694. return;
  3695. }
  3696. subexpr->line = line;
  3697. constant.expression = subexpr;
  3698. p_class->constant_expressions.insert(const_id, constant);
  3699. if (!_end_statement()) {
  3700. _set_error("Expected end of statement (constant)", line);
  3701. return;
  3702. }
  3703. } break;
  3704. case GDScriptTokenizer::TK_PR_ENUM: {
  3705. //multiple constant declarations..
  3706. int last_assign = -1; // Incremented by 1 right before the assingment.
  3707. String enum_name;
  3708. Dictionary enum_dict;
  3709. tokenizer->advance();
  3710. if (tokenizer->is_token_literal(0, true)) {
  3711. enum_name = tokenizer->get_token_literal();
  3712. tokenizer->advance();
  3713. }
  3714. if (tokenizer->get_token() != GDScriptTokenizer::TK_CURLY_BRACKET_OPEN) {
  3715. _set_error("Expected '{' in enum declaration");
  3716. return;
  3717. }
  3718. tokenizer->advance();
  3719. while (true) {
  3720. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  3721. tokenizer->advance(); // Ignore newlines
  3722. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  3723. tokenizer->advance();
  3724. break; // End of enum
  3725. } else if (!tokenizer->is_token_literal(0, true)) {
  3726. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  3727. _set_error("Unexpected end of file.");
  3728. } else {
  3729. _set_error(String("Unexpected ") + GDScriptTokenizer::get_token_name(tokenizer->get_token()) + ", expected identifier");
  3730. }
  3731. return;
  3732. } else { // tokenizer->is_token_literal(0, true)
  3733. ClassNode::Constant constant;
  3734. StringName const_id = tokenizer->get_token_literal();
  3735. tokenizer->advance();
  3736. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  3737. tokenizer->advance();
  3738. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3739. if (!subexpr) {
  3740. if (_recover_from_completion()) {
  3741. break;
  3742. }
  3743. return;
  3744. }
  3745. if (subexpr->type != Node::TYPE_CONSTANT) {
  3746. _set_error("Expected constant expression");
  3747. return;
  3748. }
  3749. ConstantNode *subexpr_const = static_cast<ConstantNode *>(subexpr);
  3750. if (subexpr_const->value.get_type() != Variant::INT) {
  3751. _set_error("Expected an int value for enum");
  3752. return;
  3753. }
  3754. last_assign = subexpr_const->value;
  3755. constant.expression = subexpr_const;
  3756. } else {
  3757. last_assign = last_assign + 1;
  3758. ConstantNode *cn = alloc_node<ConstantNode>();
  3759. cn->value = last_assign;
  3760. cn->datatype = _type_from_variant(cn->value);
  3761. constant.expression = cn;
  3762. }
  3763. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3764. tokenizer->advance();
  3765. }
  3766. if (enum_name != "") {
  3767. const ConstantNode *cn = static_cast<const ConstantNode *>(constant.expression);
  3768. enum_dict[const_id] = cn->value;
  3769. }
  3770. constant.type.has_type = true;
  3771. constant.type.kind = DataType::BUILTIN;
  3772. constant.type.builtin_type = Variant::INT;
  3773. p_class->constant_expressions.insert(const_id, constant);
  3774. }
  3775. }
  3776. if (enum_name != "") {
  3777. ClassNode::Constant enum_constant;
  3778. ConstantNode *cn = alloc_node<ConstantNode>();
  3779. cn->value = enum_dict;
  3780. cn->datatype = _type_from_variant(cn->value);
  3781. enum_constant.expression = cn;
  3782. enum_constant.type = cn->datatype;
  3783. p_class->constant_expressions.insert(enum_name, enum_constant);
  3784. }
  3785. if (!_end_statement()) {
  3786. _set_error("Expected end of statement (enum)");
  3787. return;
  3788. }
  3789. } break;
  3790. case GDScriptTokenizer::TK_CONSTANT: {
  3791. if (tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3792. tokenizer->advance();
  3793. // Ignore
  3794. } else {
  3795. _set_error(String() + "Unexpected constant of type: " + Variant::get_type_name(tokenizer->get_token_constant().get_type()));
  3796. return;
  3797. }
  3798. } break;
  3799. default: {
  3800. _set_error(String() + "Unexpected token: " + tokenizer->get_token_name(tokenizer->get_token()) + ":" + tokenizer->get_token_identifier());
  3801. return;
  3802. } break;
  3803. }
  3804. }
  3805. }
  3806. void GDScriptParser::_determine_inheritance(ClassNode *p_class) {
  3807. if (p_class->extends_used) {
  3808. //do inheritance
  3809. String path = p_class->extends_file;
  3810. Ref<GDScript> script;
  3811. StringName native;
  3812. ClassNode *base_class = NULL;
  3813. if (path != "") {
  3814. //path (and optionally subclasses)
  3815. if (path.is_rel_path()) {
  3816. String base = self_path;
  3817. if (base == "" || base.is_rel_path()) {
  3818. _set_error("Could not resolve relative path for parent class: " + path, p_class->line);
  3819. return;
  3820. }
  3821. path = base.get_base_dir().plus_file(path).simplify_path();
  3822. }
  3823. script = ResourceLoader::load(path);
  3824. if (script.is_null()) {
  3825. _set_error("Could not load base class: " + path, p_class->line);
  3826. return;
  3827. }
  3828. if (!script->is_valid()) {
  3829. _set_error("Script not fully loaded (cyclic preload?): " + path, p_class->line);
  3830. return;
  3831. }
  3832. if (p_class->extends_class.size()) {
  3833. for (int i = 0; i < p_class->extends_class.size(); i++) {
  3834. String sub = p_class->extends_class[i];
  3835. if (script->get_subclasses().has(sub)) {
  3836. Ref<Script> subclass = script->get_subclasses()[sub]; //avoid reference from disappearing
  3837. script = subclass;
  3838. } else {
  3839. _set_error("Could not find subclass: " + sub, p_class->line);
  3840. return;
  3841. }
  3842. }
  3843. }
  3844. } else {
  3845. if (p_class->extends_class.size() == 0) {
  3846. _set_error("Parser bug: undecidable inheritance.", p_class->line);
  3847. ERR_FAIL();
  3848. }
  3849. //look around for the subclasses
  3850. int extend_iter = 1;
  3851. String base = p_class->extends_class[0];
  3852. ClassNode *p = p_class->owner;
  3853. Ref<GDScript> base_script;
  3854. if (ScriptServer::is_global_class(base)) {
  3855. base_script = ResourceLoader::load(ScriptServer::get_global_class_path(base));
  3856. if (!base_script.is_valid()) {
  3857. _set_error("Class '" + base + "' could not be fully loaded (script error or cyclic inheritance).", p_class->line);
  3858. return;
  3859. }
  3860. p = NULL;
  3861. }
  3862. while (p) {
  3863. bool found = false;
  3864. for (int i = 0; i < p->subclasses.size(); i++) {
  3865. if (p->subclasses[i]->name == base) {
  3866. ClassNode *test = p->subclasses[i];
  3867. while (test) {
  3868. if (test == p_class) {
  3869. _set_error("Cyclic inheritance.", test->line);
  3870. return;
  3871. }
  3872. if (test->base_type.kind == DataType::CLASS) {
  3873. test = test->base_type.class_type;
  3874. } else {
  3875. break;
  3876. }
  3877. }
  3878. found = true;
  3879. if (extend_iter < p_class->extends_class.size()) {
  3880. // Keep looking at current classes if possible
  3881. base = p_class->extends_class[extend_iter++];
  3882. p = p->subclasses[i];
  3883. } else {
  3884. base_class = p->subclasses[i];
  3885. }
  3886. break;
  3887. }
  3888. }
  3889. if (base_class) break;
  3890. if (found) continue;
  3891. if (p->constant_expressions.has(base)) {
  3892. if (!p->constant_expressions[base].expression->type == Node::TYPE_CONSTANT) {
  3893. _set_error("Could not resolve constant '" + base + "'.", p_class->line);
  3894. return;
  3895. }
  3896. const ConstantNode *cn = static_cast<const ConstantNode *>(p->constant_expressions[base].expression);
  3897. base_script = cn->value;
  3898. if (base_script.is_null()) {
  3899. _set_error("Constant is not a class: " + base, p_class->line);
  3900. return;
  3901. }
  3902. break;
  3903. }
  3904. p = p->owner;
  3905. }
  3906. if (base_script.is_valid()) {
  3907. String ident = base;
  3908. for (int i = extend_iter; i < p_class->extends_class.size(); i++) {
  3909. String subclass = p_class->extends_class[i];
  3910. ident += ("." + subclass);
  3911. if (base_script->get_subclasses().has(subclass)) {
  3912. base_script = base_script->get_subclasses()[subclass];
  3913. } else if (base_script->get_constants().has(subclass)) {
  3914. Ref<GDScript> new_base_class = base_script->get_constants()[subclass];
  3915. if (new_base_class.is_null()) {
  3916. _set_error("Constant is not a class: " + ident, p_class->line);
  3917. return;
  3918. }
  3919. base_script = new_base_class;
  3920. } else {
  3921. _set_error("Could not find subclass: " + ident, p_class->line);
  3922. return;
  3923. }
  3924. }
  3925. script = base_script;
  3926. } else if (!base_class) {
  3927. if (p_class->extends_class.size() > 1) {
  3928. _set_error("Invalid inheritance (unknown class + subclasses)", p_class->line);
  3929. return;
  3930. }
  3931. //if not found, try engine classes
  3932. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  3933. _set_error("Unknown class: '" + base + "'", p_class->line);
  3934. return;
  3935. }
  3936. native = base;
  3937. }
  3938. }
  3939. if (base_class) {
  3940. p_class->base_type.has_type = true;
  3941. p_class->base_type.kind = DataType::CLASS;
  3942. p_class->base_type.class_type = base_class;
  3943. } else if (script.is_valid()) {
  3944. p_class->base_type.has_type = true;
  3945. p_class->base_type.kind = DataType::GDSCRIPT;
  3946. p_class->base_type.script_type = script;
  3947. p_class->base_type.native_type = script->get_instance_base_type();
  3948. } else if (native != StringName()) {
  3949. p_class->base_type.has_type = true;
  3950. p_class->base_type.kind = DataType::NATIVE;
  3951. p_class->base_type.native_type = native;
  3952. } else {
  3953. _set_error("Could not determine inheritance", p_class->line);
  3954. return;
  3955. }
  3956. } else {
  3957. // without extends, implicitly extend Reference
  3958. p_class->base_type.has_type = true;
  3959. p_class->base_type.kind = DataType::NATIVE;
  3960. p_class->base_type.native_type = "Reference";
  3961. }
  3962. // Recursively determine subclasses
  3963. for (int i = 0; i < p_class->subclasses.size(); i++) {
  3964. _determine_inheritance(p_class->subclasses[i]);
  3965. }
  3966. }
  3967. String GDScriptParser::DataType::to_string() const {
  3968. if (!has_type) return "var";
  3969. switch (kind) {
  3970. case BUILTIN: {
  3971. if (builtin_type == Variant::NIL) return "null";
  3972. return Variant::get_type_name(builtin_type);
  3973. } break;
  3974. case NATIVE: {
  3975. if (is_meta_type) {
  3976. return "GDScriptNativeClass";
  3977. }
  3978. return native_type.operator String();
  3979. } break;
  3980. case GDSCRIPT: {
  3981. Ref<GDScript> gds = script_type;
  3982. const String &gds_class = gds->get_script_class_name();
  3983. if (!gds_class.empty()) {
  3984. return gds_class;
  3985. }
  3986. } // fallthrough
  3987. case SCRIPT: {
  3988. if (is_meta_type) {
  3989. return script_type->get_class_name().operator String();
  3990. }
  3991. String name = script_type->get_name();
  3992. if (name != String()) {
  3993. return name;
  3994. }
  3995. name = script_type->get_path().get_file();
  3996. if (name != String()) {
  3997. return name;
  3998. }
  3999. return native_type.operator String();
  4000. } break;
  4001. case CLASS: {
  4002. ERR_FAIL_COND_V(!class_type, String());
  4003. if (is_meta_type) {
  4004. return "GDScript";
  4005. }
  4006. if (class_type->name == StringName()) {
  4007. return "self";
  4008. }
  4009. return class_type->name.operator String();
  4010. } break;
  4011. }
  4012. return "Unresolved";
  4013. }
  4014. bool GDScriptParser::_parse_type(DataType &r_type, bool p_can_be_void) {
  4015. tokenizer->advance();
  4016. r_type.has_type = true;
  4017. bool finished = false;
  4018. bool can_index = false;
  4019. String full_name;
  4020. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  4021. completion_cursor = StringName();
  4022. completion_type = COMPLETION_TYPE_HINT;
  4023. completion_class = current_class;
  4024. completion_function = current_function;
  4025. completion_line = tokenizer->get_token_line();
  4026. completion_argument = 0;
  4027. completion_block = current_block;
  4028. completion_found = true;
  4029. completion_ident_is_call = p_can_be_void;
  4030. tokenizer->advance();
  4031. }
  4032. switch (tokenizer->get_token()) {
  4033. case GDScriptTokenizer::TK_PR_VOID: {
  4034. if (!p_can_be_void) {
  4035. return false;
  4036. }
  4037. r_type.kind = DataType::BUILTIN;
  4038. r_type.builtin_type = Variant::NIL;
  4039. } break;
  4040. case GDScriptTokenizer::TK_BUILT_IN_TYPE: {
  4041. r_type.builtin_type = tokenizer->get_token_type();
  4042. if (tokenizer->get_token_type() == Variant::OBJECT) {
  4043. r_type.kind = DataType::NATIVE;
  4044. r_type.native_type = "Object";
  4045. } else {
  4046. r_type.kind = DataType::BUILTIN;
  4047. }
  4048. } break;
  4049. case GDScriptTokenizer::TK_IDENTIFIER: {
  4050. r_type.native_type = tokenizer->get_token_identifier();
  4051. if (ClassDB::class_exists(r_type.native_type) || ClassDB::class_exists("_" + r_type.native_type.operator String())) {
  4052. r_type.kind = DataType::NATIVE;
  4053. } else {
  4054. r_type.kind = DataType::UNRESOLVED;
  4055. can_index = true;
  4056. full_name = r_type.native_type;
  4057. }
  4058. } break;
  4059. default: {
  4060. return false;
  4061. }
  4062. }
  4063. tokenizer->advance();
  4064. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  4065. completion_cursor = r_type.native_type;
  4066. completion_type = COMPLETION_TYPE_HINT;
  4067. completion_class = current_class;
  4068. completion_function = current_function;
  4069. completion_line = tokenizer->get_token_line();
  4070. completion_argument = 0;
  4071. completion_block = current_block;
  4072. completion_found = true;
  4073. completion_ident_is_call = p_can_be_void;
  4074. tokenizer->advance();
  4075. }
  4076. if (can_index) {
  4077. while (!finished) {
  4078. switch (tokenizer->get_token()) {
  4079. case GDScriptTokenizer::TK_PERIOD: {
  4080. if (!can_index) {
  4081. _set_error("Unexpected '.'.");
  4082. return false;
  4083. }
  4084. can_index = false;
  4085. tokenizer->advance();
  4086. } break;
  4087. case GDScriptTokenizer::TK_IDENTIFIER: {
  4088. if (can_index) {
  4089. _set_error("Unexpected identifier.");
  4090. return false;
  4091. }
  4092. StringName id;
  4093. bool has_completion = _get_completable_identifier(COMPLETION_TYPE_HINT_INDEX, id);
  4094. if (id == StringName()) {
  4095. id = "@temp";
  4096. }
  4097. full_name += "." + id.operator String();
  4098. can_index = true;
  4099. if (has_completion) {
  4100. completion_cursor = full_name;
  4101. }
  4102. } break;
  4103. default: {
  4104. finished = true;
  4105. } break;
  4106. }
  4107. }
  4108. if (tokenizer->get_token(-1) == GDScriptTokenizer::TK_PERIOD) {
  4109. _set_error("Expected subclass identifier.");
  4110. return false;
  4111. }
  4112. r_type.native_type = full_name;
  4113. }
  4114. return true;
  4115. }
  4116. GDScriptParser::DataType GDScriptParser::_resolve_type(const DataType &p_source, int p_line) {
  4117. if (!p_source.has_type) return p_source;
  4118. if (p_source.kind != DataType::UNRESOLVED) return p_source;
  4119. Vector<String> full_name = p_source.native_type.operator String().split(".", false);
  4120. int name_part = 0;
  4121. DataType result;
  4122. result.has_type = true;
  4123. while (name_part < full_name.size()) {
  4124. bool found = false;
  4125. StringName id = full_name[name_part];
  4126. DataType base_type = result;
  4127. ClassNode *p = NULL;
  4128. if (name_part == 0) {
  4129. if (ScriptServer::is_global_class(id)) {
  4130. String script_path = ScriptServer::get_global_class_path(id);
  4131. if (script_path == self_path) {
  4132. result.kind = DataType::CLASS;
  4133. result.class_type = current_class;
  4134. } else {
  4135. Ref<Script> script = ResourceLoader::load(script_path);
  4136. Ref<GDScript> gds = script;
  4137. if (gds.is_valid()) {
  4138. if (!gds->is_valid()) {
  4139. _set_error("Class '" + id + "' could not be fully loaded (script error or cyclic inheritance).", p_line);
  4140. return DataType();
  4141. }
  4142. result.kind = DataType::GDSCRIPT;
  4143. result.script_type = gds;
  4144. } else if (script.is_valid()) {
  4145. result.kind = DataType::SCRIPT;
  4146. result.script_type = script;
  4147. } else {
  4148. _set_error("Class '" + id + "' was found in global scope but its script could not be loaded.", p_line);
  4149. return DataType();
  4150. }
  4151. }
  4152. name_part++;
  4153. continue;
  4154. } else {
  4155. p = current_class;
  4156. }
  4157. } else if (base_type.kind == DataType::CLASS) {
  4158. p = base_type.class_type;
  4159. }
  4160. while (p) {
  4161. if (p->constant_expressions.has(id)) {
  4162. if (p->constant_expressions[id].expression->type != Node::TYPE_CONSTANT) {
  4163. _set_error("Parser bug: unresolved constant.", p_line);
  4164. ERR_FAIL_V(result);
  4165. }
  4166. const ConstantNode *cn = static_cast<const ConstantNode *>(p->constant_expressions[id].expression);
  4167. Ref<GDScript> gds = cn->value;
  4168. if (gds.is_valid()) {
  4169. result.kind = DataType::GDSCRIPT;
  4170. result.script_type = gds;
  4171. found = true;
  4172. } else {
  4173. Ref<Script> scr = cn->value;
  4174. if (scr.is_valid()) {
  4175. result.kind = DataType::SCRIPT;
  4176. result.script_type = scr;
  4177. found = true;
  4178. }
  4179. }
  4180. break;
  4181. }
  4182. // Inner classes
  4183. ClassNode *outer_class = p;
  4184. while (outer_class) {
  4185. for (int i = 0; i < outer_class->subclasses.size(); i++) {
  4186. if (outer_class->subclasses[i] == p) {
  4187. continue;
  4188. }
  4189. if (outer_class->subclasses[i]->name == id) {
  4190. found = true;
  4191. result.kind = DataType::CLASS;
  4192. result.class_type = outer_class->subclasses[i];
  4193. break;
  4194. }
  4195. }
  4196. if (found) {
  4197. break;
  4198. }
  4199. outer_class = outer_class->owner;
  4200. }
  4201. if (!found && p->base_type.kind == DataType::CLASS) {
  4202. p = p->base_type.class_type;
  4203. } else {
  4204. base_type = p->base_type;
  4205. break;
  4206. }
  4207. }
  4208. // Still look for class constants in parent script
  4209. if (!found && (base_type.kind == DataType::GDSCRIPT || base_type.kind == DataType::SCRIPT)) {
  4210. Ref<Script> scr = base_type.script_type;
  4211. ERR_FAIL_COND_V(scr.is_null(), result);
  4212. Map<StringName, Variant> constants;
  4213. scr->get_constants(&constants);
  4214. if (constants.has(id)) {
  4215. Ref<GDScript> gds = constants[id];
  4216. if (gds.is_valid()) {
  4217. result.kind = DataType::GDSCRIPT;
  4218. result.script_type = gds;
  4219. found = true;
  4220. } else {
  4221. Ref<Script> scr = constants[id];
  4222. if (scr.is_valid()) {
  4223. result.kind = DataType::SCRIPT;
  4224. result.script_type = scr;
  4225. found = true;
  4226. }
  4227. }
  4228. }
  4229. }
  4230. if (!found && !for_completion) {
  4231. String base;
  4232. if (name_part == 0) {
  4233. base = "self";
  4234. } else {
  4235. base = result.to_string();
  4236. }
  4237. _set_error("Identifier '" + String(id) + "' is not a valid type (not a script or class), or could not be found on base '" +
  4238. base + "'.",
  4239. p_line);
  4240. return DataType();
  4241. }
  4242. name_part++;
  4243. }
  4244. return result;
  4245. }
  4246. GDScriptParser::DataType GDScriptParser::_type_from_variant(const Variant &p_value) const {
  4247. DataType result;
  4248. result.has_type = true;
  4249. result.is_constant = true;
  4250. result.kind = DataType::BUILTIN;
  4251. result.builtin_type = p_value.get_type();
  4252. if (result.builtin_type == Variant::OBJECT) {
  4253. Object *obj = p_value.operator Object *();
  4254. if (!obj) {
  4255. return DataType();
  4256. }
  4257. result.native_type = obj->get_class_name();
  4258. Ref<Script> scr = p_value;
  4259. if (scr.is_valid()) {
  4260. result.is_meta_type = true;
  4261. } else {
  4262. result.is_meta_type = false;
  4263. scr = obj->get_script();
  4264. }
  4265. if (scr.is_valid()) {
  4266. result.script_type = scr;
  4267. Ref<GDScript> gds = scr;
  4268. if (gds.is_valid()) {
  4269. result.kind = DataType::GDSCRIPT;
  4270. } else {
  4271. result.kind = DataType::SCRIPT;
  4272. }
  4273. result.native_type = scr->get_instance_base_type();
  4274. } else {
  4275. result.kind = DataType::NATIVE;
  4276. }
  4277. }
  4278. return result;
  4279. }
  4280. GDScriptParser::DataType GDScriptParser::_type_from_property(const PropertyInfo &p_property, bool p_nil_is_variant) const {
  4281. DataType ret;
  4282. if (p_property.type == Variant::NIL && (p_nil_is_variant || (p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  4283. // Variant
  4284. return ret;
  4285. }
  4286. ret.has_type = true;
  4287. ret.builtin_type = p_property.type;
  4288. if (p_property.type == Variant::OBJECT) {
  4289. ret.kind = DataType::NATIVE;
  4290. ret.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  4291. } else {
  4292. ret.kind = DataType::BUILTIN;
  4293. }
  4294. return ret;
  4295. }
  4296. GDScriptParser::DataType GDScriptParser::_type_from_gdtype(const GDScriptDataType &p_gdtype) const {
  4297. DataType result;
  4298. if (!p_gdtype.has_type) {
  4299. return result;
  4300. }
  4301. result.has_type = true;
  4302. result.builtin_type = p_gdtype.builtin_type;
  4303. result.native_type = p_gdtype.native_type;
  4304. result.script_type = p_gdtype.script_type;
  4305. switch (p_gdtype.kind) {
  4306. case GDScriptDataType::BUILTIN: {
  4307. result.kind = DataType::BUILTIN;
  4308. } break;
  4309. case GDScriptDataType::NATIVE: {
  4310. result.kind = DataType::NATIVE;
  4311. } break;
  4312. case GDScriptDataType::GDSCRIPT: {
  4313. result.kind = DataType::GDSCRIPT;
  4314. } break;
  4315. case GDScriptDataType::SCRIPT: {
  4316. result.kind = DataType::SCRIPT;
  4317. } break;
  4318. }
  4319. return result;
  4320. }
  4321. GDScriptParser::DataType GDScriptParser::_get_operation_type(const Variant::Operator p_op, const DataType &p_a, const DataType &p_b, bool &r_valid) const {
  4322. if (!p_a.has_type || !p_b.has_type) {
  4323. r_valid = true;
  4324. return DataType();
  4325. }
  4326. Variant::Type a_type = p_a.kind == DataType::BUILTIN ? p_a.builtin_type : Variant::OBJECT;
  4327. Variant::Type b_type = p_b.kind == DataType::BUILTIN ? p_b.builtin_type : Variant::OBJECT;
  4328. Variant a;
  4329. REF a_ref;
  4330. if (a_type == Variant::OBJECT) {
  4331. a_ref.instance();
  4332. a = a_ref;
  4333. } else {
  4334. Variant::CallError err;
  4335. a = Variant::construct(a_type, NULL, 0, err);
  4336. if (err.error != Variant::CallError::CALL_OK) {
  4337. r_valid = false;
  4338. return DataType();
  4339. }
  4340. }
  4341. Variant b;
  4342. REF b_ref;
  4343. if (b_type == Variant::OBJECT) {
  4344. b_ref.instance();
  4345. b = b_ref;
  4346. } else {
  4347. Variant::CallError err;
  4348. b = Variant::construct(b_type, NULL, 0, err);
  4349. if (err.error != Variant::CallError::CALL_OK) {
  4350. r_valid = false;
  4351. return DataType();
  4352. }
  4353. }
  4354. // Avoid division by zero
  4355. if (a_type == Variant::INT || a_type == Variant::REAL) {
  4356. Variant::evaluate(Variant::OP_ADD, a, 1, a, r_valid);
  4357. }
  4358. if (b_type == Variant::INT || b_type == Variant::REAL) {
  4359. Variant::evaluate(Variant::OP_ADD, b, 1, b, r_valid);
  4360. }
  4361. Variant ret;
  4362. Variant::evaluate(p_op, a, b, ret, r_valid);
  4363. if (r_valid) {
  4364. return _type_from_variant(ret);
  4365. }
  4366. return DataType();
  4367. }
  4368. Variant::Operator GDScriptParser::_get_variant_operation(const OperatorNode::Operator &p_op) const {
  4369. switch (p_op) {
  4370. case OperatorNode::OP_NEG: {
  4371. return Variant::OP_NEGATE;
  4372. } break;
  4373. case OperatorNode::OP_POS: {
  4374. return Variant::OP_POSITIVE;
  4375. } break;
  4376. case OperatorNode::OP_NOT: {
  4377. return Variant::OP_NOT;
  4378. } break;
  4379. case OperatorNode::OP_BIT_INVERT: {
  4380. return Variant::OP_BIT_NEGATE;
  4381. } break;
  4382. case OperatorNode::OP_IN: {
  4383. return Variant::OP_IN;
  4384. } break;
  4385. case OperatorNode::OP_EQUAL: {
  4386. return Variant::OP_EQUAL;
  4387. } break;
  4388. case OperatorNode::OP_NOT_EQUAL: {
  4389. return Variant::OP_NOT_EQUAL;
  4390. } break;
  4391. case OperatorNode::OP_LESS: {
  4392. return Variant::OP_LESS;
  4393. } break;
  4394. case OperatorNode::OP_LESS_EQUAL: {
  4395. return Variant::OP_LESS_EQUAL;
  4396. } break;
  4397. case OperatorNode::OP_GREATER: {
  4398. return Variant::OP_GREATER;
  4399. } break;
  4400. case OperatorNode::OP_GREATER_EQUAL: {
  4401. return Variant::OP_GREATER_EQUAL;
  4402. } break;
  4403. case OperatorNode::OP_AND: {
  4404. return Variant::OP_AND;
  4405. } break;
  4406. case OperatorNode::OP_OR: {
  4407. return Variant::OP_OR;
  4408. } break;
  4409. case OperatorNode::OP_ASSIGN_ADD:
  4410. case OperatorNode::OP_ADD: {
  4411. return Variant::OP_ADD;
  4412. } break;
  4413. case OperatorNode::OP_ASSIGN_SUB:
  4414. case OperatorNode::OP_SUB: {
  4415. return Variant::OP_SUBTRACT;
  4416. } break;
  4417. case OperatorNode::OP_ASSIGN_MUL:
  4418. case OperatorNode::OP_MUL: {
  4419. return Variant::OP_MULTIPLY;
  4420. } break;
  4421. case OperatorNode::OP_ASSIGN_DIV:
  4422. case OperatorNode::OP_DIV: {
  4423. return Variant::OP_DIVIDE;
  4424. } break;
  4425. case OperatorNode::OP_ASSIGN_MOD:
  4426. case OperatorNode::OP_MOD: {
  4427. return Variant::OP_MODULE;
  4428. } break;
  4429. case OperatorNode::OP_ASSIGN_BIT_AND:
  4430. case OperatorNode::OP_BIT_AND: {
  4431. return Variant::OP_BIT_AND;
  4432. } break;
  4433. case OperatorNode::OP_ASSIGN_BIT_OR:
  4434. case OperatorNode::OP_BIT_OR: {
  4435. return Variant::OP_BIT_OR;
  4436. } break;
  4437. case OperatorNode::OP_ASSIGN_BIT_XOR:
  4438. case OperatorNode::OP_BIT_XOR: {
  4439. return Variant::OP_BIT_XOR;
  4440. } break;
  4441. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  4442. case OperatorNode::OP_SHIFT_LEFT: {
  4443. return Variant::OP_SHIFT_LEFT;
  4444. }
  4445. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  4446. case OperatorNode::OP_SHIFT_RIGHT: {
  4447. return Variant::OP_SHIFT_RIGHT;
  4448. }
  4449. default: {
  4450. return Variant::OP_MAX;
  4451. } break;
  4452. }
  4453. }
  4454. bool GDScriptParser::_is_type_compatible(const DataType &p_container, const DataType &p_expression, bool p_allow_implicit_conversion) const {
  4455. // Ignore for completion
  4456. if (!check_types || for_completion) {
  4457. return true;
  4458. }
  4459. // Can't test if not all have type
  4460. if (!p_container.has_type || !p_expression.has_type) {
  4461. return true;
  4462. }
  4463. // Should never get here unresolved
  4464. ERR_FAIL_COND_V(p_container.kind == DataType::UNRESOLVED, false);
  4465. ERR_FAIL_COND_V(p_expression.kind == DataType::UNRESOLVED, false);
  4466. if (p_container.kind == DataType::BUILTIN && p_expression.kind == DataType::BUILTIN) {
  4467. bool valid = p_container.builtin_type == p_expression.builtin_type;
  4468. if (p_allow_implicit_conversion) {
  4469. valid = valid || (p_container.builtin_type == Variant::INT && p_expression.builtin_type == Variant::REAL);
  4470. valid = valid || (p_container.builtin_type == Variant::REAL && p_expression.builtin_type == Variant::INT);
  4471. valid = valid || (p_container.builtin_type == Variant::STRING && p_expression.builtin_type == Variant::NODE_PATH);
  4472. valid = valid || (p_container.builtin_type == Variant::NODE_PATH && p_expression.builtin_type == Variant::STRING);
  4473. valid = valid || (p_container.builtin_type == Variant::BOOL && p_expression.builtin_type == Variant::REAL);
  4474. valid = valid || (p_container.builtin_type == Variant::BOOL && p_expression.builtin_type == Variant::INT);
  4475. valid = valid || (p_container.builtin_type == Variant::INT && p_expression.builtin_type == Variant::BOOL);
  4476. valid = valid || (p_container.builtin_type == Variant::REAL && p_expression.builtin_type == Variant::BOOL);
  4477. }
  4478. return valid;
  4479. }
  4480. if (p_container.kind == DataType::BUILTIN || (p_expression.kind == DataType::BUILTIN && p_expression.builtin_type != Variant::NIL)) {
  4481. // Can't mix built-ins with objects
  4482. return false;
  4483. }
  4484. // From now on everything is objects, check polymorphism
  4485. // The container must be the same class or a superclass of the expression
  4486. if (p_expression.kind == DataType::BUILTIN && p_expression.builtin_type == Variant::NIL) {
  4487. // Null can be assigned to object types
  4488. return true;
  4489. }
  4490. StringName expr_native;
  4491. Ref<Script> expr_script;
  4492. ClassNode *expr_class = NULL;
  4493. switch (p_expression.kind) {
  4494. case DataType::NATIVE: {
  4495. if (p_container.kind != DataType::NATIVE) {
  4496. // Non-native type can't be a superclass of a native type
  4497. return false;
  4498. }
  4499. if (p_expression.is_meta_type) {
  4500. expr_native = GDScriptNativeClass::get_class_static();
  4501. } else {
  4502. expr_native = p_expression.native_type;
  4503. }
  4504. } break;
  4505. case DataType::SCRIPT:
  4506. case DataType::GDSCRIPT: {
  4507. if (p_container.kind == DataType::CLASS) {
  4508. // This cannot be resolved without cyclic dependencies, so just bail out
  4509. return false;
  4510. }
  4511. if (p_expression.is_meta_type) {
  4512. expr_native = p_expression.script_type->get_class_name();
  4513. } else {
  4514. expr_script = p_expression.script_type;
  4515. expr_native = expr_script->get_instance_base_type();
  4516. }
  4517. } break;
  4518. case DataType::CLASS: {
  4519. if (p_expression.is_meta_type) {
  4520. expr_native = GDScript::get_class_static();
  4521. } else {
  4522. expr_class = p_expression.class_type;
  4523. ClassNode *base = expr_class;
  4524. while (base->base_type.kind == DataType::CLASS) {
  4525. base = base->base_type.class_type;
  4526. }
  4527. expr_native = base->base_type.native_type;
  4528. expr_script = base->base_type.script_type;
  4529. }
  4530. }
  4531. }
  4532. switch (p_container.kind) {
  4533. case DataType::NATIVE: {
  4534. if (p_container.is_meta_type) {
  4535. return ClassDB::is_parent_class(expr_native, GDScriptNativeClass::get_class_static());
  4536. } else {
  4537. return ClassDB::is_parent_class(expr_native, p_container.native_type);
  4538. }
  4539. } break;
  4540. case DataType::SCRIPT:
  4541. case DataType::GDSCRIPT: {
  4542. if (p_container.is_meta_type) {
  4543. return ClassDB::is_parent_class(expr_native, GDScript::get_class_static());
  4544. }
  4545. if (expr_class == head && p_container.script_type->get_path() == self_path) {
  4546. // Special case: container is self script and expression is self
  4547. return true;
  4548. }
  4549. while (expr_script.is_valid()) {
  4550. if (expr_script == p_container.script_type) {
  4551. return true;
  4552. }
  4553. expr_script = expr_script->get_base_script();
  4554. }
  4555. return false;
  4556. } break;
  4557. case DataType::CLASS: {
  4558. if (p_container.is_meta_type) {
  4559. return ClassDB::is_parent_class(expr_native, GDScript::get_class_static());
  4560. }
  4561. if (p_container.class_type == head && expr_script.is_valid() && expr_script->get_path() == self_path) {
  4562. // Special case: container is self and expression is self script
  4563. return true;
  4564. }
  4565. while (expr_class) {
  4566. if (expr_class == p_container.class_type) {
  4567. return true;
  4568. }
  4569. expr_class = expr_class->base_type.class_type;
  4570. }
  4571. return false;
  4572. }
  4573. }
  4574. return false;
  4575. }
  4576. GDScriptParser::DataType GDScriptParser::_reduce_node_type(Node *p_node) {
  4577. if (p_node->get_datatype().has_type) {
  4578. return p_node->get_datatype();
  4579. }
  4580. DataType node_type;
  4581. switch (p_node->type) {
  4582. case Node::TYPE_CONSTANT: {
  4583. node_type = _type_from_variant(static_cast<ConstantNode *>(p_node)->value);
  4584. } break;
  4585. case Node::TYPE_ARRAY: {
  4586. node_type.has_type = true;
  4587. node_type.kind = DataType::BUILTIN;
  4588. node_type.builtin_type = Variant::ARRAY;
  4589. } break;
  4590. case Node::TYPE_DICTIONARY: {
  4591. node_type.has_type = true;
  4592. node_type.kind = DataType::BUILTIN;
  4593. node_type.builtin_type = Variant::DICTIONARY;
  4594. } break;
  4595. case Node::TYPE_SELF: {
  4596. node_type.has_type = true;
  4597. node_type.kind = DataType::CLASS;
  4598. node_type.class_type = current_class;
  4599. } break;
  4600. case Node::TYPE_IDENTIFIER: {
  4601. IdentifierNode *id = static_cast<IdentifierNode *>(p_node);
  4602. if (id->declared_block) {
  4603. node_type = id->declared_block->variables[id->name]->get_datatype();
  4604. } else if (id->name == "#match_value") {
  4605. // It's a special id just for the match statetement, ignore
  4606. break;
  4607. } else if (current_function && current_function->arguments.find(id->name) >= 0) {
  4608. int idx = current_function->arguments.find(id->name);
  4609. node_type = current_function->argument_types[idx];
  4610. } else {
  4611. node_type = _reduce_identifier_type(NULL, id->name, id->line);
  4612. }
  4613. } break;
  4614. case Node::TYPE_CAST: {
  4615. CastNode *cn = static_cast<CastNode *>(p_node);
  4616. DataType source_type = _reduce_node_type(cn->source_node);
  4617. cn->cast_type = _resolve_type(cn->cast_type, cn->line);
  4618. if (source_type.has_type) {
  4619. bool valid = false;
  4620. if (check_types) {
  4621. if (cn->cast_type.kind == DataType::BUILTIN && source_type.kind == DataType::BUILTIN) {
  4622. valid = Variant::can_convert(source_type.builtin_type, cn->cast_type.builtin_type);
  4623. }
  4624. if (cn->cast_type.kind != DataType::BUILTIN && source_type.kind != DataType::BUILTIN) {
  4625. valid = _is_type_compatible(cn->cast_type, source_type) || _is_type_compatible(source_type, cn->cast_type);
  4626. }
  4627. if (!valid) {
  4628. _set_error("Invalid cast. Cannot convert from '" + source_type.to_string() +
  4629. "' to '" + cn->cast_type.to_string() + "'.",
  4630. cn->line);
  4631. return DataType();
  4632. }
  4633. }
  4634. }
  4635. node_type = cn->cast_type;
  4636. } break;
  4637. case Node::TYPE_OPERATOR: {
  4638. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  4639. switch (op->op) {
  4640. case OperatorNode::OP_CALL:
  4641. case OperatorNode::OP_PARENT_CALL: {
  4642. node_type = _reduce_function_call_type(op);
  4643. } break;
  4644. case OperatorNode::OP_YIELD: {
  4645. if (op->arguments.size() == 2) {
  4646. DataType base_type = _reduce_node_type(op->arguments[0]);
  4647. DataType signal_type = _reduce_node_type(op->arguments[1]);
  4648. // TODO: Check if signal exists when it's a constant
  4649. if (base_type.has_type && base_type.kind == DataType::BUILTIN && base_type.builtin_type != Variant::NIL && base_type.builtin_type != Variant::OBJECT) {
  4650. _set_error("First argument of 'yield()' must be an object.", op->line);
  4651. return DataType();
  4652. }
  4653. if (signal_type.has_type && (signal_type.kind != DataType::BUILTIN || signal_type.builtin_type != Variant::STRING)) {
  4654. _set_error("Second argument of 'yield()' must be a string.", op->line);
  4655. return DataType();
  4656. }
  4657. }
  4658. // yield can return anything
  4659. node_type.has_type = false;
  4660. } break;
  4661. case OperatorNode::OP_IS: {
  4662. if (op->arguments.size() != 2) {
  4663. _set_error("Parser bug: binary operation without 2 arguments.", op->line);
  4664. ERR_FAIL_V(DataType());
  4665. }
  4666. DataType value_type = _reduce_node_type(op->arguments[0]);
  4667. DataType type_type = _reduce_node_type(op->arguments[1]);
  4668. if (check_types && type_type.has_type) {
  4669. if (!type_type.is_meta_type && (type_type.kind != DataType::NATIVE || !ClassDB::is_parent_class(type_type.native_type, "Script"))) {
  4670. _set_error("Invalid 'is' test: right operand is not a type (not a native type nor a script).", op->line);
  4671. return DataType();
  4672. }
  4673. type_type.is_meta_type = false; // Test the actual type
  4674. if (!_is_type_compatible(type_type, value_type) && !_is_type_compatible(value_type, type_type)) {
  4675. // TODO: Make this a warning?
  4676. _set_error("A value of type '" + value_type.to_string() + "' will never be an instance of '" + type_type.to_string() + "'.", op->line);
  4677. return DataType();
  4678. }
  4679. }
  4680. node_type.has_type = true;
  4681. node_type.is_constant = true;
  4682. node_type.is_meta_type = false;
  4683. node_type.kind = DataType::BUILTIN;
  4684. node_type.builtin_type = Variant::BOOL;
  4685. } break;
  4686. // Unary operators
  4687. case OperatorNode::OP_NEG:
  4688. case OperatorNode::OP_POS:
  4689. case OperatorNode::OP_NOT:
  4690. case OperatorNode::OP_BIT_INVERT: {
  4691. DataType argument_type = _reduce_node_type(op->arguments[0]);
  4692. if (!argument_type.has_type) {
  4693. break;
  4694. }
  4695. Variant::Operator var_op = _get_variant_operation(op->op);
  4696. bool valid = false;
  4697. node_type = _get_operation_type(var_op, argument_type, argument_type, valid);
  4698. if (check_types && !valid) {
  4699. _set_error("Invalid operand type ('" + argument_type.to_string() +
  4700. "') to unary operator '" + Variant::get_operator_name(var_op) + "'.",
  4701. op->line, op->column);
  4702. return DataType();
  4703. }
  4704. } break;
  4705. // Binary operators
  4706. case OperatorNode::OP_IN:
  4707. case OperatorNode::OP_EQUAL:
  4708. case OperatorNode::OP_NOT_EQUAL:
  4709. case OperatorNode::OP_LESS:
  4710. case OperatorNode::OP_LESS_EQUAL:
  4711. case OperatorNode::OP_GREATER:
  4712. case OperatorNode::OP_GREATER_EQUAL:
  4713. case OperatorNode::OP_AND:
  4714. case OperatorNode::OP_OR:
  4715. case OperatorNode::OP_ADD:
  4716. case OperatorNode::OP_SUB:
  4717. case OperatorNode::OP_MUL:
  4718. case OperatorNode::OP_DIV:
  4719. case OperatorNode::OP_MOD:
  4720. case OperatorNode::OP_SHIFT_LEFT:
  4721. case OperatorNode::OP_SHIFT_RIGHT:
  4722. case OperatorNode::OP_BIT_AND:
  4723. case OperatorNode::OP_BIT_OR:
  4724. case OperatorNode::OP_BIT_XOR: {
  4725. if (op->arguments.size() != 2) {
  4726. _set_error("Parser bug: binary operation without 2 arguments.", op->line);
  4727. ERR_FAIL_V(DataType());
  4728. }
  4729. DataType argument_a_type = _reduce_node_type(op->arguments[0]);
  4730. DataType argument_b_type = _reduce_node_type(op->arguments[1]);
  4731. if (!argument_a_type.has_type || !argument_b_type.has_type) {
  4732. break;
  4733. }
  4734. Variant::Operator var_op = _get_variant_operation(op->op);
  4735. bool valid = false;
  4736. node_type = _get_operation_type(var_op, argument_a_type, argument_b_type, valid);
  4737. if (check_types && !valid) {
  4738. _set_error("Invalid operand types ('" + argument_a_type.to_string() + "' and '" +
  4739. argument_b_type.to_string() + "') to operator '" + Variant::get_operator_name(var_op) + "'.",
  4740. op->line, op->column);
  4741. return DataType();
  4742. }
  4743. } break;
  4744. // Ternary operators
  4745. case OperatorNode::OP_TERNARY_IF: {
  4746. if (op->arguments.size() != 3) {
  4747. _set_error("Parser bug: ternary operation without 3 arguments");
  4748. ERR_FAIL_V(DataType());
  4749. }
  4750. DataType true_type = _reduce_node_type(op->arguments[1]);
  4751. DataType false_type = _reduce_node_type(op->arguments[2]);
  4752. // If types are equal, then the expression is of the same type
  4753. // If they are compatible, return the broader type
  4754. if (true_type == false_type || _is_type_compatible(true_type, false_type)) {
  4755. node_type = true_type;
  4756. } else if (_is_type_compatible(false_type, true_type)) {
  4757. node_type = false_type;
  4758. }
  4759. // TODO: Warn if types aren't compatible
  4760. } break;
  4761. // Assignment should never happen within an expression
  4762. case OperatorNode::OP_ASSIGN:
  4763. case OperatorNode::OP_ASSIGN_ADD:
  4764. case OperatorNode::OP_ASSIGN_SUB:
  4765. case OperatorNode::OP_ASSIGN_MUL:
  4766. case OperatorNode::OP_ASSIGN_DIV:
  4767. case OperatorNode::OP_ASSIGN_MOD:
  4768. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  4769. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  4770. case OperatorNode::OP_ASSIGN_BIT_AND:
  4771. case OperatorNode::OP_ASSIGN_BIT_OR:
  4772. case OperatorNode::OP_ASSIGN_BIT_XOR:
  4773. case OperatorNode::OP_INIT_ASSIGN: {
  4774. _set_error("Assignment inside expression is not allowed (parser bug?).", op->line);
  4775. return DataType();
  4776. } break;
  4777. case OperatorNode::OP_INDEX_NAMED: {
  4778. if (op->arguments.size() != 2) {
  4779. _set_error("Parser bug: named index with invalid arguments.", op->line);
  4780. ERR_FAIL_V(DataType());
  4781. }
  4782. if (op->arguments[1]->type != Node::TYPE_IDENTIFIER) {
  4783. _set_error("Parser bug: named index without identifier argument.", op->line);
  4784. ERR_FAIL_V(DataType());
  4785. }
  4786. DataType base_type = _reduce_node_type(op->arguments[0]);
  4787. IdentifierNode *member_id = static_cast<IdentifierNode *>(op->arguments[1]);
  4788. if (base_type.has_type) {
  4789. if (check_types && base_type.kind == DataType::BUILTIN) {
  4790. // Variant type, just test if it's possible
  4791. DataType result;
  4792. switch (base_type.builtin_type) {
  4793. case Variant::NIL:
  4794. case Variant::DICTIONARY: {
  4795. result.has_type = false;
  4796. } break;
  4797. default: {
  4798. Variant::CallError err;
  4799. Variant temp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  4800. bool valid = false;
  4801. Variant res = temp.get(member_id->name.operator String(), &valid);
  4802. if (valid) {
  4803. result = _type_from_variant(res);
  4804. } else if (check_types) {
  4805. _set_error("Can't get index '" + String(member_id->name.operator String()) + "' on base '" +
  4806. base_type.to_string() + "'.",
  4807. op->line);
  4808. return DataType();
  4809. }
  4810. } break;
  4811. }
  4812. result.is_constant = false;
  4813. node_type = result;
  4814. } else {
  4815. node_type = _reduce_identifier_type(&base_type, member_id->name, op->line);
  4816. }
  4817. }
  4818. if (error_set) {
  4819. return DataType();
  4820. }
  4821. } break;
  4822. case OperatorNode::OP_INDEX: {
  4823. if (op->arguments[1]->type == Node::TYPE_CONSTANT) {
  4824. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[1]);
  4825. if (cn->value.get_type() == Variant::STRING) {
  4826. // Treat this as named indexing
  4827. IdentifierNode *id = alloc_node<IdentifierNode>();
  4828. id->name = cn->value.operator StringName();
  4829. op->op = OperatorNode::OP_INDEX_NAMED;
  4830. op->arguments[1] = id;
  4831. return _reduce_node_type(op);
  4832. }
  4833. }
  4834. DataType base_type = _reduce_node_type(op->arguments[0]);
  4835. DataType index_type = _reduce_node_type(op->arguments[1]);
  4836. if (!base_type.has_type) {
  4837. break;
  4838. }
  4839. if (check_types && index_type.has_type) {
  4840. if (base_type.kind == DataType::BUILTIN) {
  4841. // Check if indexing is valid
  4842. bool error = index_type.kind != DataType::BUILTIN;
  4843. if (!error) {
  4844. switch (base_type.builtin_type) {
  4845. // Expect int or real as index
  4846. case Variant::POOL_BYTE_ARRAY:
  4847. case Variant::POOL_COLOR_ARRAY:
  4848. case Variant::POOL_INT_ARRAY:
  4849. case Variant::POOL_REAL_ARRAY:
  4850. case Variant::POOL_STRING_ARRAY:
  4851. case Variant::POOL_VECTOR2_ARRAY:
  4852. case Variant::POOL_VECTOR3_ARRAY:
  4853. case Variant::ARRAY:
  4854. case Variant::STRING: {
  4855. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::REAL;
  4856. } break;
  4857. // Expect String only
  4858. case Variant::RECT2:
  4859. case Variant::PLANE:
  4860. case Variant::QUAT:
  4861. case Variant::AABB:
  4862. case Variant::OBJECT: {
  4863. error = index_type.builtin_type != Variant::STRING;
  4864. } break;
  4865. // Expect String or number
  4866. case Variant::VECTOR2:
  4867. case Variant::VECTOR3:
  4868. case Variant::TRANSFORM2D:
  4869. case Variant::BASIS:
  4870. case Variant::TRANSFORM: {
  4871. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::REAL &&
  4872. index_type.builtin_type != Variant::STRING;
  4873. } break;
  4874. // Expect String or int
  4875. case Variant::COLOR: {
  4876. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING;
  4877. } break;
  4878. }
  4879. }
  4880. if (error) {
  4881. _set_error("Invalid index type (" + index_type.to_string() + ") for base '" + base_type.to_string() + "'.",
  4882. op->line);
  4883. return DataType();
  4884. }
  4885. if (op->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT) {
  4886. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[1]);
  4887. // Index is a constant, just try it if possible
  4888. switch (base_type.builtin_type) {
  4889. // Arrays/string have variable indexing, can't test directly
  4890. case Variant::STRING:
  4891. case Variant::ARRAY:
  4892. case Variant::DICTIONARY:
  4893. case Variant::POOL_BYTE_ARRAY:
  4894. case Variant::POOL_COLOR_ARRAY:
  4895. case Variant::POOL_INT_ARRAY:
  4896. case Variant::POOL_REAL_ARRAY:
  4897. case Variant::POOL_STRING_ARRAY:
  4898. case Variant::POOL_VECTOR2_ARRAY:
  4899. case Variant::POOL_VECTOR3_ARRAY: {
  4900. break;
  4901. }
  4902. default: {
  4903. Variant::CallError err;
  4904. Variant temp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  4905. bool valid = false;
  4906. Variant res = temp.get(cn->value, &valid);
  4907. if (valid) {
  4908. node_type = _type_from_variant(res);
  4909. node_type.is_constant = false;
  4910. } else if (check_types) {
  4911. _set_error("Can't get index '" + String(cn->value) + "' on base '" +
  4912. base_type.to_string() + "'.",
  4913. op->line);
  4914. return DataType();
  4915. }
  4916. } break;
  4917. }
  4918. }
  4919. } else if (!for_completion && (index_type.kind != DataType::BUILTIN || index_type.builtin_type != Variant::STRING)) {
  4920. _set_error("Only strings can be used as index in the base type '" + base_type.to_string() + "'.", op->line);
  4921. return DataType();
  4922. }
  4923. }
  4924. if (check_types && !node_type.has_type) {
  4925. // Can infer indexing type for some variant types
  4926. DataType result;
  4927. result.has_type = true;
  4928. result.kind = DataType::BUILTIN;
  4929. switch (base_type.builtin_type) {
  4930. // Can't index at all
  4931. case Variant::NIL:
  4932. case Variant::BOOL:
  4933. case Variant::INT:
  4934. case Variant::REAL:
  4935. case Variant::NODE_PATH:
  4936. case Variant::_RID: {
  4937. _set_error("Can't index on a value of type '" + base_type.to_string() + "'.", op->line);
  4938. return DataType();
  4939. } break;
  4940. // Return int
  4941. case Variant::POOL_BYTE_ARRAY:
  4942. case Variant::POOL_INT_ARRAY: {
  4943. result.builtin_type = Variant::INT;
  4944. } break;
  4945. // Return real
  4946. case Variant::POOL_REAL_ARRAY:
  4947. case Variant::VECTOR2:
  4948. case Variant::VECTOR3:
  4949. case Variant::QUAT: {
  4950. result.builtin_type = Variant::REAL;
  4951. } break;
  4952. // Return color
  4953. case Variant::POOL_COLOR_ARRAY: {
  4954. result.builtin_type = Variant::COLOR;
  4955. } break;
  4956. // Return string
  4957. case Variant::POOL_STRING_ARRAY:
  4958. case Variant::STRING: {
  4959. result.builtin_type = Variant::STRING;
  4960. } break;
  4961. // Return Vector2
  4962. case Variant::POOL_VECTOR2_ARRAY:
  4963. case Variant::TRANSFORM2D:
  4964. case Variant::RECT2: {
  4965. result.builtin_type = Variant::VECTOR2;
  4966. } break;
  4967. // Return Vector3
  4968. case Variant::POOL_VECTOR3_ARRAY:
  4969. case Variant::AABB:
  4970. case Variant::BASIS: {
  4971. result.builtin_type = Variant::VECTOR3;
  4972. } break;
  4973. // Depends on the index
  4974. case Variant::TRANSFORM:
  4975. case Variant::PLANE:
  4976. case Variant::COLOR:
  4977. default: {
  4978. result.has_type = false;
  4979. } break;
  4980. }
  4981. node_type = result;
  4982. }
  4983. } break;
  4984. default: {
  4985. _set_error("Parser bug: unhandled operation.", op->line);
  4986. ERR_FAIL_V(DataType());
  4987. }
  4988. }
  4989. } break;
  4990. }
  4991. p_node->set_datatype(_resolve_type(node_type, p_node->line));
  4992. return node_type;
  4993. }
  4994. bool GDScriptParser::_get_function_signature(DataType &p_base_type, const StringName &p_function, DataType &r_return_type, List<DataType> &r_arg_types, int &r_default_arg_count, bool &r_static, bool &r_vararg) const {
  4995. r_static = false;
  4996. r_default_arg_count = 0;
  4997. DataType original_type = p_base_type;
  4998. ClassNode *base = NULL;
  4999. FunctionNode *callee = NULL;
  5000. if (p_base_type.kind == DataType::CLASS) {
  5001. base = p_base_type.class_type;
  5002. }
  5003. // Look up the current file (parse tree)
  5004. while (!callee && base) {
  5005. for (int i = 0; i < base->static_functions.size(); i++) {
  5006. FunctionNode *func = base->static_functions[i];
  5007. if (p_function == func->name) {
  5008. r_static = true;
  5009. callee = func;
  5010. break;
  5011. }
  5012. }
  5013. if (!callee && !p_base_type.is_meta_type) {
  5014. for (int i = 0; i < base->functions.size(); i++) {
  5015. FunctionNode *func = base->functions[i];
  5016. if (p_function == func->name) {
  5017. callee = func;
  5018. break;
  5019. }
  5020. }
  5021. }
  5022. p_base_type = base->base_type;
  5023. if (p_base_type.kind == DataType::CLASS) {
  5024. base = p_base_type.class_type;
  5025. } else {
  5026. break;
  5027. }
  5028. }
  5029. if (callee) {
  5030. r_return_type = callee->get_datatype();
  5031. for (int i = 0; i < callee->argument_types.size(); i++) {
  5032. r_arg_types.push_back(callee->argument_types[i]);
  5033. }
  5034. r_default_arg_count = callee->default_values.size();
  5035. return true;
  5036. }
  5037. // Nothing in current file, check parent script
  5038. Ref<GDScript> base_gdscript;
  5039. Ref<Script> base_script;
  5040. StringName native;
  5041. if (p_base_type.kind == DataType::GDSCRIPT) {
  5042. base_gdscript = p_base_type.script_type;
  5043. } else if (p_base_type.kind == DataType::SCRIPT) {
  5044. base_script = p_base_type.script_type;
  5045. } else if (p_base_type.kind == DataType::NATIVE) {
  5046. native = p_base_type.native_type;
  5047. }
  5048. while (base_gdscript.is_valid()) {
  5049. native = base_gdscript->get_instance_base_type();
  5050. Map<StringName, GDScriptFunction *> funcs = base_gdscript->get_member_functions();
  5051. if (funcs.has(p_function)) {
  5052. GDScriptFunction *f = funcs[p_function];
  5053. r_static = f->is_static();
  5054. r_default_arg_count = f->get_default_argument_count();
  5055. r_return_type = _type_from_gdtype(f->get_return_type());
  5056. for (int i = 0; i < f->get_argument_count(); i++) {
  5057. r_arg_types.push_back(_type_from_gdtype(f->get_argument_type(i)));
  5058. }
  5059. return true;
  5060. }
  5061. base_gdscript = base_gdscript->get_base_script();
  5062. }
  5063. while (base_script.is_valid()) {
  5064. native = base_script->get_instance_base_type();
  5065. MethodInfo mi = base_script->get_method_info(p_function);
  5066. if (!(mi == MethodInfo())) {
  5067. r_return_type = _type_from_property(mi.return_val, false);
  5068. r_default_arg_count = mi.default_arguments.size();
  5069. for (List<PropertyInfo>::Element *E = mi.arguments.front(); E; E = E->next()) {
  5070. r_arg_types.push_back(_type_from_property(E->get()));
  5071. }
  5072. return true;
  5073. }
  5074. base_script = base_script->get_base_script();
  5075. }
  5076. #ifdef DEBUG_METHODS_ENABLED
  5077. // Only native remains
  5078. if (!ClassDB::class_exists(native)) {
  5079. native = "_" + native.operator String();
  5080. }
  5081. if (!ClassDB::class_exists(native)) {
  5082. if (!check_types) return false;
  5083. ERR_EXPLAIN("Parser bug: Class '" + String(native) + "' not found.");
  5084. ERR_FAIL_V(false);
  5085. }
  5086. MethodBind *method = ClassDB::get_method(native, p_function);
  5087. if (!method) {
  5088. // Try virtual methods
  5089. List<MethodInfo> virtuals;
  5090. ClassDB::get_virtual_methods(native, &virtuals);
  5091. for (const List<MethodInfo>::Element *E = virtuals.front(); E; E = E->next()) {
  5092. const MethodInfo &mi = E->get();
  5093. if (mi.name == p_function) {
  5094. r_default_arg_count = mi.default_arguments.size();
  5095. for (const List<PropertyInfo>::Element *pi = mi.arguments.front(); pi; pi = pi->next()) {
  5096. r_arg_types.push_back(_type_from_property(pi->get()));
  5097. }
  5098. r_return_type = _type_from_property(mi.return_val, false);
  5099. r_vararg = mi.flags & METHOD_FLAG_VARARG;
  5100. return true;
  5101. }
  5102. }
  5103. // If the base is a script, it might be trying to access members of the Script class itself
  5104. if (original_type.is_meta_type && !(p_function == "new") && (original_type.kind == DataType::SCRIPT || original_type.kind == DataType::GDSCRIPT)) {
  5105. method = ClassDB::get_method(original_type.script_type->get_class_name(), p_function);
  5106. if (method) {
  5107. r_static = true;
  5108. } else {
  5109. // Try virtual methods of the script type
  5110. virtuals.clear();
  5111. ClassDB::get_virtual_methods(original_type.script_type->get_class_name(), &virtuals);
  5112. for (const List<MethodInfo>::Element *E = virtuals.front(); E; E = E->next()) {
  5113. const MethodInfo &mi = E->get();
  5114. if (mi.name == p_function) {
  5115. r_default_arg_count = mi.default_arguments.size();
  5116. for (const List<PropertyInfo>::Element *pi = mi.arguments.front(); pi; pi = pi->next()) {
  5117. r_arg_types.push_back(_type_from_property(pi->get()));
  5118. }
  5119. r_return_type = _type_from_property(mi.return_val, false);
  5120. r_static = true;
  5121. r_vararg = mi.flags & METHOD_FLAG_VARARG;
  5122. return true;
  5123. }
  5124. }
  5125. return false;
  5126. }
  5127. } else {
  5128. return false;
  5129. }
  5130. }
  5131. r_default_arg_count = method->get_default_argument_count();
  5132. r_return_type = _type_from_property(method->get_return_info(), false);
  5133. r_vararg = method->is_vararg();
  5134. for (int i = 0; i < method->get_argument_count(); i++) {
  5135. r_arg_types.push_back(_type_from_property(method->get_argument_info(i)));
  5136. }
  5137. return true;
  5138. #else
  5139. return false;
  5140. #endif
  5141. }
  5142. GDScriptParser::DataType GDScriptParser::_reduce_function_call_type(const OperatorNode *p_call) {
  5143. if (p_call->arguments.size() < 1) {
  5144. _set_error("Parser bug: function call without enough arguments.", p_call->line);
  5145. ERR_FAIL_V(DataType());
  5146. }
  5147. DataType return_type;
  5148. List<DataType> arg_types;
  5149. int default_args_count = 0;
  5150. int arg_count = p_call->arguments.size();
  5151. String callee_name;
  5152. bool is_vararg = false;
  5153. switch (p_call->arguments[0]->type) {
  5154. case GDScriptParser::Node::TYPE_TYPE: {
  5155. // Built-in constructor, special case
  5156. TypeNode *tn = static_cast<TypeNode *>(p_call->arguments[0]);
  5157. Vector<DataType> par_types;
  5158. par_types.resize(p_call->arguments.size() - 1);
  5159. for (int i = 1; i < p_call->arguments.size(); i++) {
  5160. par_types[i - 1] = _reduce_node_type(p_call->arguments[i]);
  5161. }
  5162. if (error_set) return DataType();
  5163. bool match = false;
  5164. List<MethodInfo> constructors;
  5165. Variant::get_constructor_list(tn->vtype, &constructors);
  5166. PropertyInfo return_type;
  5167. for (List<MethodInfo>::Element *E = constructors.front(); E; E = E->next()) {
  5168. MethodInfo &mi = E->get();
  5169. if (p_call->arguments.size() - 1 < mi.arguments.size() - mi.default_arguments.size()) {
  5170. continue;
  5171. }
  5172. if (p_call->arguments.size() - 1 > mi.arguments.size()) {
  5173. continue;
  5174. }
  5175. bool types_match = true;
  5176. for (int i = 0; i < par_types.size(); i++) {
  5177. DataType arg_type;
  5178. if (mi.arguments[i].type != Variant::NIL) {
  5179. arg_type.has_type = true;
  5180. arg_type.kind = mi.arguments[i].type == Variant::OBJECT ? DataType::NATIVE : DataType::BUILTIN;
  5181. arg_type.builtin_type = mi.arguments[i].type;
  5182. arg_type.native_type = mi.arguments[i].class_name;
  5183. }
  5184. if (!_is_type_compatible(arg_type, par_types[i], true)) {
  5185. types_match = false;
  5186. break;
  5187. }
  5188. }
  5189. if (types_match) {
  5190. match = true;
  5191. return_type = mi.return_val;
  5192. break;
  5193. }
  5194. }
  5195. if (match) {
  5196. return _type_from_property(return_type, false);
  5197. } else if (check_types) {
  5198. String err = "No constructor of '";
  5199. err += Variant::get_type_name(tn->vtype);
  5200. err += "' matches the signature '";
  5201. err += Variant::get_type_name(tn->vtype) + "(";
  5202. for (int i = 0; i < par_types.size(); i++) {
  5203. if (i > 0) err += ", ";
  5204. err += par_types[i].to_string();
  5205. }
  5206. err += ")'.";
  5207. _set_error(err, p_call->line, p_call->column);
  5208. return DataType();
  5209. }
  5210. return DataType();
  5211. } break;
  5212. case GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION: {
  5213. BuiltInFunctionNode *func = static_cast<BuiltInFunctionNode *>(p_call->arguments[0]);
  5214. MethodInfo mi = GDScriptFunctions::get_info(func->function);
  5215. return_type = _type_from_property(mi.return_val, false);
  5216. // Check arguments
  5217. is_vararg = mi.flags & METHOD_FLAG_VARARG;
  5218. default_args_count = mi.default_arguments.size();
  5219. callee_name = mi.name;
  5220. arg_count -= 1;
  5221. // Check each argument type
  5222. for (List<PropertyInfo>::Element *E = mi.arguments.front(); E; E = E->next()) {
  5223. arg_types.push_back(_type_from_property(E->get()));
  5224. }
  5225. } break;
  5226. default: {
  5227. if (p_call->op == OperatorNode::OP_CALL && p_call->arguments.size() < 2) {
  5228. _set_error("Parser bug: self method call without enough arguments.", p_call->line);
  5229. ERR_FAIL_V(DataType());
  5230. }
  5231. int arg_id = p_call->op == OperatorNode::OP_CALL ? 1 : 0;
  5232. if (p_call->arguments[arg_id]->type != Node::TYPE_IDENTIFIER) {
  5233. _set_error("Parser bug: invalid function call argument.", p_call->line);
  5234. ERR_FAIL_V(DataType());
  5235. }
  5236. IdentifierNode *func_id = static_cast<IdentifierNode *>(p_call->arguments[arg_id]);
  5237. callee_name = func_id->name;
  5238. arg_count -= 1 + arg_id;
  5239. DataType base_type;
  5240. if (p_call->op == OperatorNode::OP_PARENT_CALL) {
  5241. base_type = current_class->base_type;
  5242. } else {
  5243. base_type = _reduce_node_type(p_call->arguments[0]);
  5244. }
  5245. if (!base_type.has_type || (base_type.kind == DataType::BUILTIN && base_type.builtin_type == Variant::NIL)) {
  5246. return DataType();
  5247. }
  5248. if (base_type.kind == DataType::BUILTIN) {
  5249. Variant::CallError err;
  5250. Variant tmp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  5251. if (check_types) {
  5252. if (!tmp.has_method(callee_name)) {
  5253. _set_error("Method '" + String(callee_name) + "()' is not declared in base '" + base_type.to_string() + "'.", p_call->line);
  5254. return DataType();
  5255. }
  5256. default_args_count = Variant::get_method_default_arguments(base_type.builtin_type, callee_name).size();
  5257. const Vector<Variant::Type> &var_arg_types = Variant::get_method_argument_types(base_type.builtin_type, callee_name);
  5258. for (int i = 0; i < var_arg_types.size(); i++) {
  5259. DataType argtype;
  5260. if (var_arg_types[i] != Variant::NIL) {
  5261. argtype.has_type = true;
  5262. argtype.kind = DataType::BUILTIN;
  5263. argtype.builtin_type = var_arg_types[i];
  5264. }
  5265. arg_types.push_back(argtype);
  5266. }
  5267. }
  5268. return_type.has_type = true;
  5269. return_type.kind = DataType::BUILTIN;
  5270. return_type.builtin_type = Variant::get_method_return_type(base_type.builtin_type, callee_name);
  5271. break;
  5272. }
  5273. DataType original_type = base_type;
  5274. bool is_initializer = callee_name == "new";
  5275. bool is_static = false;
  5276. bool valid = false;
  5277. if (is_initializer && original_type.is_meta_type) {
  5278. // Try to check it as initializer
  5279. base_type = original_type;
  5280. callee_name = "_init";
  5281. base_type.is_meta_type = false;
  5282. valid = _get_function_signature(base_type, callee_name, return_type, arg_types,
  5283. default_args_count, is_static, is_vararg);
  5284. if (valid) {
  5285. return_type = original_type;
  5286. return_type.is_meta_type = false;
  5287. }
  5288. }
  5289. if (!valid) {
  5290. base_type = original_type;
  5291. return_type = DataType();
  5292. valid = _get_function_signature(base_type, callee_name, return_type, arg_types,
  5293. default_args_count, is_static, is_vararg);
  5294. }
  5295. String base_name;
  5296. if (p_call->arguments[0]->type == Node::TYPE_SELF) {
  5297. base_name = current_class->name == StringName() ? "self" : current_class->name;
  5298. } else {
  5299. base_name = original_type.to_string();
  5300. }
  5301. if (!valid) {
  5302. if (p_call->arguments[0]->type == Node::TYPE_SELF) {
  5303. _set_error("Method '" + callee_name + "' is not declared in the current class.", p_call->line);
  5304. return DataType();
  5305. }
  5306. _mark_line_as_unsafe(p_call->line);
  5307. return DataType();
  5308. }
  5309. if (current_function && !for_completion && !is_static && p_call->arguments[0]->type == Node::TYPE_SELF && current_function->_static) {
  5310. if (current_function && current_function->_static && p_call->arguments[0]->type == Node::TYPE_SELF) {
  5311. _set_error("Can't call non-static function from a static function.", p_call->line);
  5312. return DataType();
  5313. }
  5314. }
  5315. if (check_types && !is_static && !is_initializer && base_type.is_meta_type) {
  5316. _set_error("Non-static function '" + String(callee_name) + "' can only be called from an instance.", p_call->line);
  5317. return DataType();
  5318. }
  5319. } break;
  5320. }
  5321. if (!check_types) {
  5322. return return_type;
  5323. }
  5324. if (arg_count < arg_types.size() - default_args_count) {
  5325. _set_error("Too few arguments for '" + callee_name + "()' call. Expected at least " + itos(arg_types.size() - default_args_count) + ".", p_call->line);
  5326. return return_type;
  5327. }
  5328. if (!is_vararg && arg_count > arg_types.size()) {
  5329. _set_error("Too many arguments for '" + callee_name + "()' call. Expected at most " + itos(arg_types.size()) + ".", p_call->line);
  5330. return return_type;
  5331. }
  5332. int arg_diff = p_call->arguments.size() - arg_count;
  5333. for (int i = arg_diff; i < p_call->arguments.size(); i++) {
  5334. DataType par_type = _reduce_node_type(p_call->arguments[i]);
  5335. if ((i - arg_diff) >= arg_types.size()) {
  5336. continue;
  5337. }
  5338. if (!_is_type_compatible(arg_types[i - arg_diff], par_type, true)) {
  5339. _set_error("At '" + callee_name + "()' call, argument " + itos(i - arg_diff + 1) + ". Assigned type (" +
  5340. par_type.to_string() + ") doesn't match the function argument's type (" +
  5341. arg_types[i - arg_diff].to_string() + ").",
  5342. p_call->line);
  5343. return DataType();
  5344. }
  5345. }
  5346. return return_type;
  5347. }
  5348. bool GDScriptParser::_get_member_type(const DataType &p_base_type, const StringName &p_member, DataType &r_member_type) const {
  5349. DataType base_type = p_base_type;
  5350. // Check classes in current file
  5351. ClassNode *base = NULL;
  5352. if (base_type.kind == DataType::CLASS) {
  5353. base = base_type.class_type;
  5354. }
  5355. while (base) {
  5356. if (base->constant_expressions.has(p_member)) {
  5357. r_member_type = base->constant_expressions[p_member].expression->get_datatype();
  5358. return true;
  5359. }
  5360. if (!base_type.is_meta_type) {
  5361. for (int i = 0; i < base->variables.size(); i++) {
  5362. ClassNode::Member m = base->variables[i];
  5363. if (m.identifier == p_member) {
  5364. r_member_type = m.data_type;
  5365. return true;
  5366. }
  5367. }
  5368. } else {
  5369. for (int i = 0; i < base->subclasses.size(); i++) {
  5370. ClassNode *c = base->subclasses[i];
  5371. if (c->name == p_member) {
  5372. DataType class_type;
  5373. class_type.has_type = true;
  5374. class_type.is_constant = true;
  5375. class_type.is_meta_type = true;
  5376. class_type.kind = DataType::CLASS;
  5377. class_type.class_type = c;
  5378. r_member_type = class_type;
  5379. return true;
  5380. }
  5381. }
  5382. }
  5383. base_type = base->base_type;
  5384. if (base_type.kind == DataType::CLASS) {
  5385. base = base_type.class_type;
  5386. } else {
  5387. break;
  5388. }
  5389. }
  5390. Ref<GDScript> gds;
  5391. if (base_type.kind == DataType::GDSCRIPT) {
  5392. gds = base_type.script_type;
  5393. }
  5394. Ref<Script> scr;
  5395. if (base_type.kind == DataType::SCRIPT) {
  5396. scr = base_type.script_type;
  5397. }
  5398. StringName native;
  5399. if (base_type.kind == DataType::NATIVE) {
  5400. native = base_type.native_type;
  5401. }
  5402. // Check GDScripts
  5403. while (gds.is_valid()) {
  5404. if (gds->get_constants().has(p_member)) {
  5405. Variant c = gds->get_constants()[p_member];
  5406. r_member_type = _type_from_variant(c);
  5407. return true;
  5408. }
  5409. if (!base_type.is_meta_type) {
  5410. if (gds->get_members().has(p_member)) {
  5411. r_member_type = _type_from_gdtype(gds->get_member_type(p_member));
  5412. return true;
  5413. }
  5414. }
  5415. native = gds->get_instance_base_type();
  5416. if (gds->get_base_script().is_valid()) {
  5417. gds = gds->get_base_script();
  5418. scr = gds->get_base_script();
  5419. bool is_meta = base_type.is_meta_type;
  5420. base_type = _type_from_variant(scr.operator Variant());
  5421. base_type.is_meta_type = is_meta;
  5422. } else {
  5423. break;
  5424. }
  5425. }
  5426. // Check other script types
  5427. while (scr.is_valid()) {
  5428. Map<StringName, Variant> constants;
  5429. scr->get_constants(&constants);
  5430. if (constants.has(p_member)) {
  5431. r_member_type = _type_from_variant(constants[p_member]);
  5432. return true;
  5433. }
  5434. List<PropertyInfo> properties;
  5435. scr->get_script_property_list(&properties);
  5436. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  5437. if (E->get().name == p_member) {
  5438. r_member_type = _type_from_property(E->get());
  5439. return true;
  5440. }
  5441. }
  5442. base_type = _type_from_variant(scr.operator Variant());
  5443. native = scr->get_instance_base_type();
  5444. scr = scr->get_base_script();
  5445. }
  5446. // Check ClassDB
  5447. if (!ClassDB::class_exists(native)) {
  5448. native = "_" + native.operator String();
  5449. }
  5450. if (!ClassDB::class_exists(native)) {
  5451. if (!check_types) return false;
  5452. ERR_EXPLAIN("Parser bug: Class '" + String(native) + "' not found.");
  5453. ERR_FAIL_V(false);
  5454. }
  5455. bool valid = false;
  5456. ClassDB::get_integer_constant(native, p_member, &valid);
  5457. if (valid) {
  5458. DataType ct;
  5459. ct.has_type = true;
  5460. ct.is_constant = true;
  5461. ct.kind = DataType::BUILTIN;
  5462. ct.builtin_type = Variant::INT;
  5463. r_member_type = ct;
  5464. return true;
  5465. }
  5466. if (!base_type.is_meta_type) {
  5467. List<PropertyInfo> properties;
  5468. ClassDB::get_property_list(native, &properties);
  5469. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  5470. if (E->get().name == p_member) {
  5471. // Check if a getter exists
  5472. StringName getter_name = ClassDB::get_property_getter(native, p_member);
  5473. if (getter_name != StringName()) {
  5474. // Use the getter return type
  5475. #ifdef DEBUG_METHODS_ENABLED
  5476. MethodBind *getter_method = ClassDB::get_method(native, getter_name);
  5477. if (getter_method) {
  5478. r_member_type = _type_from_property(getter_method->get_return_info());
  5479. } else {
  5480. r_member_type = DataType();
  5481. }
  5482. #else
  5483. r_member_type = DataType();
  5484. #endif
  5485. } else {
  5486. r_member_type = _type_from_property(E->get());
  5487. }
  5488. return true;
  5489. }
  5490. }
  5491. }
  5492. // If the base is a script, it might be trying to access members of the Script class itself
  5493. if (p_base_type.is_meta_type && (p_base_type.kind == DataType::SCRIPT || p_base_type.kind == DataType::GDSCRIPT)) {
  5494. native = p_base_type.script_type->get_class_name();
  5495. ClassDB::get_integer_constant(native, p_member, &valid);
  5496. if (valid) {
  5497. DataType ct;
  5498. ct.has_type = true;
  5499. ct.is_constant = true;
  5500. ct.kind = DataType::BUILTIN;
  5501. ct.builtin_type = Variant::INT;
  5502. r_member_type = ct;
  5503. return true;
  5504. }
  5505. List<PropertyInfo> properties;
  5506. ClassDB::get_property_list(native, &properties);
  5507. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  5508. if (E->get().name == p_member) {
  5509. // Check if a getter exists
  5510. StringName getter_name = ClassDB::get_property_getter(native, p_member);
  5511. if (getter_name != StringName()) {
  5512. // Use the getter return type
  5513. #ifdef DEBUG_METHODS_ENABLED
  5514. MethodBind *getter_method = ClassDB::get_method(native, getter_name);
  5515. if (getter_method) {
  5516. r_member_type = _type_from_property(getter_method->get_return_info());
  5517. } else {
  5518. r_member_type = DataType();
  5519. }
  5520. #else
  5521. r_member_type = DataType();
  5522. #endif
  5523. } else {
  5524. r_member_type = _type_from_property(E->get());
  5525. }
  5526. return true;
  5527. }
  5528. }
  5529. }
  5530. return false;
  5531. }
  5532. GDScriptParser::DataType GDScriptParser::_reduce_identifier_type(const DataType *p_base_type, const StringName &p_identifier, int p_line) {
  5533. if (p_base_type && !p_base_type->has_type) {
  5534. return DataType();
  5535. }
  5536. DataType base_type;
  5537. // Check classes in current file
  5538. ClassNode *base = NULL;
  5539. if (!p_base_type) {
  5540. // Possibly this is a global, check first
  5541. if (ClassDB::class_exists(p_identifier) || ClassDB::class_exists("_" + p_identifier.operator String())) {
  5542. DataType result;
  5543. result.has_type = true;
  5544. result.is_constant = true;
  5545. result.is_meta_type = true;
  5546. if (Engine::get_singleton()->has_singleton(p_identifier) || Engine::get_singleton()->has_singleton("_" + p_identifier.operator String())) {
  5547. result.is_meta_type = false;
  5548. }
  5549. result.kind = DataType::NATIVE;
  5550. result.native_type = p_identifier;
  5551. return result;
  5552. }
  5553. ClassNode *outer_class = current_class;
  5554. while (outer_class) {
  5555. if (outer_class->name == p_identifier) {
  5556. DataType result;
  5557. result.has_type = true;
  5558. result.is_constant = true;
  5559. result.is_meta_type = true;
  5560. result.kind = DataType::CLASS;
  5561. result.class_type = outer_class;
  5562. return result;
  5563. }
  5564. for (int i = 0; i < outer_class->subclasses.size(); i++) {
  5565. if (outer_class->subclasses[i] == current_class) {
  5566. continue;
  5567. }
  5568. if (outer_class->subclasses[i]->name == p_identifier) {
  5569. DataType result;
  5570. result.has_type = true;
  5571. result.is_constant = true;
  5572. result.is_meta_type = true;
  5573. result.kind = DataType::CLASS;
  5574. result.class_type = outer_class->subclasses[i];
  5575. return result;
  5576. }
  5577. }
  5578. outer_class = outer_class->owner;
  5579. }
  5580. if (ScriptServer::is_global_class(p_identifier)) {
  5581. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier));
  5582. if (scr.is_valid()) {
  5583. DataType result;
  5584. result.has_type = true;
  5585. result.script_type = scr;
  5586. result.is_meta_type = true;
  5587. Ref<GDScript> gds = scr;
  5588. if (gds.is_valid()) {
  5589. if (!gds->is_valid()) {
  5590. _set_error("Class '" + p_identifier + "' could not be fully loaded (script error or cyclic inheritance).");
  5591. return DataType();
  5592. }
  5593. result.kind = DataType::GDSCRIPT;
  5594. } else {
  5595. result.kind = DataType::SCRIPT;
  5596. }
  5597. return result;
  5598. }
  5599. _set_error("Class '" + p_identifier + "' was found in global scope but its script could not be loaded.");
  5600. return DataType();
  5601. }
  5602. if (GDScriptLanguage::get_singleton()->get_global_map().has(p_identifier)) {
  5603. int idx = GDScriptLanguage::get_singleton()->get_global_map()[p_identifier];
  5604. Variant g = GDScriptLanguage::get_singleton()->get_global_array()[idx];
  5605. return _type_from_variant(g);
  5606. }
  5607. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(p_identifier)) {
  5608. Variant g = GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier];
  5609. return _type_from_variant(g);
  5610. }
  5611. // Non-tool singletons aren't loaded, check project settings
  5612. List<PropertyInfo> props;
  5613. ProjectSettings::get_singleton()->get_property_list(&props);
  5614. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  5615. String s = E->get().name;
  5616. if (!s.begins_with("autoload/")) {
  5617. continue;
  5618. }
  5619. String name = s.get_slice("/", 1);
  5620. if (name == p_identifier) {
  5621. String script = ProjectSettings::get_singleton()->get(s);
  5622. if (script.begins_with("*")) {
  5623. script = script.right(1);
  5624. }
  5625. if (!script.begins_with("res://")) {
  5626. script = "res://" + script;
  5627. }
  5628. Ref<Script> singleton = ResourceLoader::load(script);
  5629. if (singleton.is_valid()) {
  5630. DataType result;
  5631. result.has_type = true;
  5632. result.script_type = singleton;
  5633. Ref<GDScript> gds = singleton;
  5634. if (gds.is_valid()) {
  5635. if (!gds->is_valid()) {
  5636. _set_error("Couldn't fully load singleton script '" + p_identifier + "' (possible cyclic reference or parse error).", p_line);
  5637. return DataType();
  5638. }
  5639. result.kind = DataType::GDSCRIPT;
  5640. } else {
  5641. result.kind = DataType::SCRIPT;
  5642. }
  5643. }
  5644. }
  5645. }
  5646. // Nothing found, keep looking in local scope
  5647. base = current_class;
  5648. base_type.has_type = true;
  5649. base_type.is_constant = true;
  5650. base_type.kind = DataType::CLASS;
  5651. base_type.class_type = base;
  5652. } else {
  5653. base_type = *p_base_type;
  5654. if (base_type.kind == DataType::CLASS) {
  5655. base = base_type.class_type;
  5656. }
  5657. }
  5658. DataType member_type;
  5659. if (_get_member_type(base_type, p_identifier, member_type)) {
  5660. return member_type;
  5661. }
  5662. if (!check_types) {
  5663. return DataType();
  5664. }
  5665. if (!p_base_type) {
  5666. _set_error("Identifier '" + String(p_identifier) + "' is not declared in the current scope.", p_line);
  5667. } else {
  5668. _set_error("Identifier '" + String(p_identifier) + "' is not declared in base '" + p_base_type->to_string() + "'.", p_line);
  5669. }
  5670. return DataType();
  5671. }
  5672. void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
  5673. // Constants
  5674. for (Map<StringName, ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
  5675. ClassNode::Constant &c = E->get();
  5676. DataType cont = _resolve_type(c.type, c.expression->line);
  5677. DataType expr = _resolve_type(c.expression->get_datatype(), c.expression->line);
  5678. if (!_is_type_compatible(cont, expr)) {
  5679. _set_error("Constant value type (" + expr.to_string() + ") is not compatible with declared type (" + cont.to_string() + ").",
  5680. c.expression->line);
  5681. return;
  5682. }
  5683. expr.is_constant = true;
  5684. c.type = expr;
  5685. c.expression->set_datatype(expr);
  5686. }
  5687. // Function declarations
  5688. for (int i = 0; i < p_class->static_functions.size(); i++) {
  5689. _check_function_types(p_class->static_functions[i]);
  5690. if (error_set) return;
  5691. }
  5692. for (int i = 0; i < p_class->functions.size(); i++) {
  5693. _check_function_types(p_class->functions[i]);
  5694. if (error_set) return;
  5695. }
  5696. // Class variables
  5697. for (int i = 0; i < p_class->variables.size(); i++) {
  5698. ClassNode::Member &v = p_class->variables[i];
  5699. DataType tmp;
  5700. if (_get_member_type(p_class->base_type, v.identifier, tmp)) {
  5701. _set_error("Member '" + String(v.identifier) + "' already exists in parent class.", v.line);
  5702. return;
  5703. }
  5704. if (!v.data_type.has_type) continue;
  5705. v.data_type = _resolve_type(v.data_type, v.line);
  5706. if (v.expression) {
  5707. DataType expr_type = _reduce_node_type(v.expression);
  5708. if (!_is_type_compatible(v.data_type, expr_type)) {
  5709. // Try with implicit conversion
  5710. if (v.data_type.kind != DataType::BUILTIN || !_is_type_compatible(v.data_type, expr_type, true)) {
  5711. _set_error("Assigned expression type (" + expr_type.to_string() + ") doesn't match the variable's type (" +
  5712. v.data_type.to_string() + ").",
  5713. v.line);
  5714. return;
  5715. }
  5716. // Replace assigment with implict conversion
  5717. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  5718. convert->line = v.line;
  5719. convert->function = GDScriptFunctions::TYPE_CONVERT;
  5720. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  5721. tgt_type->line = v.line;
  5722. tgt_type->value = (int)v.data_type.builtin_type;
  5723. OperatorNode *convert_call = alloc_node<OperatorNode>();
  5724. convert_call->line = v.line;
  5725. convert_call->op = OperatorNode::OP_CALL;
  5726. convert_call->arguments.push_back(convert);
  5727. convert_call->arguments.push_back(v.expression);
  5728. convert_call->arguments.push_back(tgt_type);
  5729. v.expression = convert_call;
  5730. v.initial_assignment->arguments[1] = convert_call;
  5731. }
  5732. } else if (v.data_type.has_type && v.data_type.kind == DataType::BUILTIN) {
  5733. // Create default value based on the type
  5734. IdentifierNode *id = alloc_node<IdentifierNode>();
  5735. id->line = v.line;
  5736. id->name = v.identifier;
  5737. ConstantNode *init = alloc_node<ConstantNode>();
  5738. init->line = v.line;
  5739. Variant::CallError err;
  5740. init->value = Variant::construct(v.data_type.builtin_type, NULL, 0, err);
  5741. OperatorNode *op = alloc_node<OperatorNode>();
  5742. op->line = v.line;
  5743. op->op = OperatorNode::OP_INIT_ASSIGN;
  5744. op->arguments.push_back(id);
  5745. op->arguments.push_back(init);
  5746. p_class->initializer->statements.push_front(op);
  5747. v.initial_assignment = op;
  5748. #ifdef DEBUG_ENABLED
  5749. NewLineNode *nl = alloc_node<NewLineNode>();
  5750. nl->line = v.line - 1;
  5751. p_class->initializer->statements.push_front(nl);
  5752. #endif
  5753. }
  5754. // Check export hint
  5755. if (v._export.type != Variant::NIL) {
  5756. DataType export_type = _type_from_property(v._export);
  5757. if (!_is_type_compatible(v.data_type, export_type, true)) {
  5758. _set_error("Export hint type (" + export_type.to_string() + ") doesn't match the variable's type (" +
  5759. v.data_type.to_string() + ").",
  5760. v.line);
  5761. return;
  5762. }
  5763. }
  5764. // Setter and getter
  5765. if (v.setter == StringName() && v.getter == StringName()) continue;
  5766. bool found_getter = false;
  5767. bool found_setter = false;
  5768. for (int j = 0; j < p_class->functions.size(); j++) {
  5769. if (v.setter == p_class->functions[j]->name) {
  5770. found_setter = true;
  5771. FunctionNode *setter = p_class->functions[j];
  5772. if (setter->arguments.size() != 1) {
  5773. _set_error("Setter function needs to receive exactly 1 argument. See '" + setter->name +
  5774. "()' definition at line " + itos(setter->line) + ".",
  5775. v.line);
  5776. return;
  5777. }
  5778. if (!_is_type_compatible(v.data_type, setter->argument_types[0])) {
  5779. _set_error("Setter argument type (" + setter->argument_types[0].to_string() +
  5780. ") doesn't match the variable's type (" + v.data_type.to_string() + "). See '" +
  5781. setter->name + "()' definition at line " + itos(setter->line) + ".",
  5782. v.line);
  5783. return;
  5784. }
  5785. continue;
  5786. }
  5787. if (v.getter == p_class->functions[j]->name) {
  5788. found_getter = true;
  5789. FunctionNode *getter = p_class->functions[j];
  5790. if (getter->arguments.size() != 0) {
  5791. _set_error("Getter function can't receive arguments. See '" + getter->name +
  5792. "()' definition at line " + itos(getter->line) + ".",
  5793. v.line);
  5794. return;
  5795. }
  5796. if (!_is_type_compatible(v.data_type, getter->get_datatype())) {
  5797. _set_error("Getter return type (" + getter->get_datatype().to_string() +
  5798. ") doesn't match the variable's type (" + v.data_type.to_string() +
  5799. "). See '" + getter->name + "()' definition at line " + itos(getter->line) + ".",
  5800. v.line);
  5801. return;
  5802. }
  5803. }
  5804. if (found_getter && found_setter) break;
  5805. }
  5806. if ((found_getter || v.getter == StringName()) && (found_setter || v.setter == StringName())) continue;
  5807. // Check for static functions
  5808. for (int j = 0; j < p_class->static_functions.size(); j++) {
  5809. if (v.setter == p_class->static_functions[j]->name) {
  5810. FunctionNode *setter = p_class->static_functions[j];
  5811. _set_error("Setter can't be a static function. See '" + setter->name + "()' definition at line " + itos(setter->line) + ".", v.line);
  5812. return;
  5813. }
  5814. if (v.getter == p_class->static_functions[j]->name) {
  5815. FunctionNode *getter = p_class->static_functions[j];
  5816. _set_error("Getter can't be a static function. See '" + getter->name + "()' definition at line " + itos(getter->line) + ".", v.line);
  5817. return;
  5818. }
  5819. }
  5820. if (!found_setter && v.setter != StringName()) {
  5821. _set_error("Setter function is not defined.", v.line);
  5822. return;
  5823. }
  5824. if (!found_getter && v.getter != StringName()) {
  5825. _set_error("Getter function is not defined.", v.line);
  5826. return;
  5827. }
  5828. }
  5829. // Inner classes
  5830. for (int i = 0; i < p_class->subclasses.size(); i++) {
  5831. current_class = p_class->subclasses[i];
  5832. _check_class_level_types(current_class);
  5833. if (error_set) return;
  5834. current_class = p_class;
  5835. }
  5836. }
  5837. void GDScriptParser::_check_function_types(FunctionNode *p_function) {
  5838. p_function->return_type = _resolve_type(p_function->return_type, p_function->line);
  5839. // Arguments
  5840. int defaults_ofs = p_function->arguments.size() - p_function->default_values.size();
  5841. for (int i = 0; i < p_function->arguments.size(); i++) {
  5842. // Resolve types
  5843. p_function->argument_types[i] = _resolve_type(p_function->argument_types[i], p_function->line);
  5844. if (i >= defaults_ofs) {
  5845. if (p_function->default_values[i - defaults_ofs]->type != Node::TYPE_OPERATOR) {
  5846. _set_error("Parser bug: invalid argument default value.", p_function->line, p_function->column);
  5847. return;
  5848. }
  5849. OperatorNode *op = static_cast<OperatorNode *>(p_function->default_values[i - defaults_ofs]);
  5850. if (op->op != OperatorNode::OP_ASSIGN || op->arguments.size() != 2) {
  5851. _set_error("Parser bug: invalid argument default value operation.", p_function->line);
  5852. return;
  5853. }
  5854. DataType def_type = _reduce_node_type(op->arguments[1]);
  5855. if (!_is_type_compatible(p_function->argument_types[i], def_type, true)) {
  5856. String arg_name = p_function->arguments[i];
  5857. _set_error("Value type (" + def_type.to_string() + ") doesn't match the type of argument '" +
  5858. arg_name + "' (" + p_function->arguments[i] + ")",
  5859. p_function->line);
  5860. }
  5861. }
  5862. }
  5863. if (!(p_function->name == "_init")) {
  5864. // Signature for the initializer may vary
  5865. DataType return_type;
  5866. List<DataType> arg_types;
  5867. int default_arg_count = 0;
  5868. bool _static = false;
  5869. bool vararg = false;
  5870. DataType base_type = current_class->base_type;
  5871. if (_get_function_signature(base_type, p_function->name, return_type, arg_types, default_arg_count, _static, vararg)) {
  5872. bool valid = _static == p_function->_static;
  5873. valid = valid && return_type == p_function->return_type;
  5874. valid = valid && p_function->default_values.size() >= default_arg_count;
  5875. valid = valid && arg_types.size() == p_function->arguments.size();
  5876. int i = 0;
  5877. for (List<DataType>::Element *E = arg_types.front(); valid && E; E = E->next()) {
  5878. valid = valid && E->get() == p_function->argument_types[i++];
  5879. }
  5880. if (!valid) {
  5881. _set_error("Function signature doesn't match the parent.", p_function->line);
  5882. return;
  5883. }
  5884. }
  5885. } else {
  5886. if (p_function->return_type.has_type && (p_function->return_type.kind != DataType::BUILTIN || p_function->return_type.builtin_type != Variant::NIL)) {
  5887. _set_error("Constructor cannot return a value.", p_function->line);
  5888. return;
  5889. }
  5890. }
  5891. if (p_function->return_type.has_type && (p_function->return_type.kind != DataType::BUILTIN || p_function->return_type.builtin_type != Variant::NIL)) {
  5892. if (!p_function->body->has_return) {
  5893. _set_error("Non-void function must return a value in all possible paths.", p_function->line);
  5894. return;
  5895. }
  5896. }
  5897. if (p_function->has_yield) {
  5898. // yield() will make the function return a GDScriptFunctionState, so the type is ambiguous
  5899. p_function->return_type.has_type = false;
  5900. }
  5901. }
  5902. void GDScriptParser::_check_class_blocks_types(ClassNode *p_class) {
  5903. // Function blocks
  5904. for (int i = 0; i < p_class->static_functions.size(); i++) {
  5905. current_function = p_class->static_functions[i];
  5906. current_block = current_function->body;
  5907. _check_block_types(current_block);
  5908. current_block = NULL;
  5909. current_function = NULL;
  5910. if (error_set) return;
  5911. }
  5912. for (int i = 0; i < p_class->functions.size(); i++) {
  5913. current_function = p_class->functions[i];
  5914. current_block = current_function->body;
  5915. _check_block_types(current_block);
  5916. current_block = NULL;
  5917. current_function = NULL;
  5918. if (error_set) return;
  5919. }
  5920. // Inner classes
  5921. for (int i = 0; i < p_class->subclasses.size(); i++) {
  5922. current_class = p_class->subclasses[i];
  5923. _check_class_blocks_types(current_class);
  5924. if (error_set) return;
  5925. current_class = p_class;
  5926. }
  5927. }
  5928. void GDScriptParser::_check_block_types(BlockNode *p_block) {
  5929. // Check each statement
  5930. for (List<Node *>::Element *E = p_block->statements.front(); E; E = E->next()) {
  5931. Node *statement = E->get();
  5932. switch (statement->type) {
  5933. case Node::TYPE_NEWLINE:
  5934. case Node::TYPE_BREAKPOINT:
  5935. case Node::TYPE_ASSERT: {
  5936. // Nothing to do
  5937. } break;
  5938. case Node::TYPE_LOCAL_VAR: {
  5939. LocalVarNode *lv = static_cast<LocalVarNode *>(statement);
  5940. lv->datatype = _resolve_type(lv->datatype, lv->line);
  5941. if (lv->assign) {
  5942. DataType assign_type = _reduce_node_type(lv->assign);
  5943. if (!_is_type_compatible(lv->datatype, assign_type)) {
  5944. // Try implict conversion
  5945. if (lv->datatype.kind != DataType::BUILTIN || !_is_type_compatible(lv->datatype, assign_type, true)) {
  5946. _set_error("Assigned value type (" + assign_type.to_string() + ") doesn't match the variable's type (" +
  5947. lv->datatype.to_string() + ").",
  5948. lv->line);
  5949. return;
  5950. }
  5951. // Replace assigment with implict conversion
  5952. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  5953. convert->line = lv->line;
  5954. convert->function = GDScriptFunctions::TYPE_CONVERT;
  5955. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  5956. tgt_type->line = lv->line;
  5957. tgt_type->value = (int)lv->datatype.builtin_type;
  5958. OperatorNode *convert_call = alloc_node<OperatorNode>();
  5959. convert_call->line = lv->line;
  5960. convert_call->op = OperatorNode::OP_CALL;
  5961. convert_call->arguments.push_back(convert);
  5962. convert_call->arguments.push_back(lv->assign);
  5963. convert_call->arguments.push_back(tgt_type);
  5964. lv->assign = convert_call;
  5965. lv->assign_op->arguments[1] = convert_call;
  5966. }
  5967. }
  5968. // TODO: Make a warning
  5969. /*
  5970. if (lv->assignments == 0) {
  5971. _set_error("Variable '" + String(lv->name) + "' is never assigned.", lv->line);
  5972. return;
  5973. }
  5974. */
  5975. } break;
  5976. case Node::TYPE_OPERATOR: {
  5977. OperatorNode *op = static_cast<OperatorNode *>(statement);
  5978. switch (op->op) {
  5979. case OperatorNode::OP_ASSIGN:
  5980. case OperatorNode::OP_ASSIGN_ADD:
  5981. case OperatorNode::OP_ASSIGN_SUB:
  5982. case OperatorNode::OP_ASSIGN_MUL:
  5983. case OperatorNode::OP_ASSIGN_DIV:
  5984. case OperatorNode::OP_ASSIGN_MOD:
  5985. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  5986. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  5987. case OperatorNode::OP_ASSIGN_BIT_AND:
  5988. case OperatorNode::OP_ASSIGN_BIT_OR:
  5989. case OperatorNode::OP_ASSIGN_BIT_XOR: {
  5990. if (op->arguments.size() < 2) {
  5991. _set_error("Parser bug: operation without enough arguments.", op->line, op->column);
  5992. return;
  5993. }
  5994. DataType lh_type = _reduce_node_type(op->arguments[0]);
  5995. if (error_set) {
  5996. return;
  5997. }
  5998. if (!lh_type.has_type) {
  5999. break;
  6000. }
  6001. if (lh_type.is_constant) {
  6002. _set_error("Cannot assign a new value to a constant.", op->line);
  6003. return;
  6004. }
  6005. DataType rh_type;
  6006. if (op->op != OperatorNode::OP_ASSIGN) {
  6007. // Validate operation
  6008. DataType arg_type = _reduce_node_type(op->arguments[1]);
  6009. if (!arg_type.has_type) {
  6010. break;
  6011. }
  6012. Variant::Operator oper = _get_variant_operation(op->op);
  6013. bool valid = false;
  6014. rh_type = _get_operation_type(oper, lh_type, arg_type, valid);
  6015. if (!valid) {
  6016. _set_error("Invalid operand types ('" + lh_type.to_string() + "' and '" + arg_type.to_string() +
  6017. "') to assignment operator '" + Variant::get_operator_name(oper) + "'.",
  6018. op->line);
  6019. return;
  6020. }
  6021. } else {
  6022. rh_type = _reduce_node_type(op->arguments[1]);
  6023. }
  6024. if (!_is_type_compatible(lh_type, rh_type)) {
  6025. // Try implict conversion
  6026. if (lh_type.kind != DataType::BUILTIN || !_is_type_compatible(lh_type, rh_type, true)) {
  6027. _set_error("Assigned value type (" + rh_type.to_string() + ") doesn't match the variable's type (" +
  6028. lh_type.to_string() + ").",
  6029. op->line);
  6030. return;
  6031. }
  6032. // Replace assigment with implict conversion
  6033. BuiltInFunctionNode *convert = alloc_node<BuiltInFunctionNode>();
  6034. convert->line = op->line;
  6035. convert->function = GDScriptFunctions::TYPE_CONVERT;
  6036. ConstantNode *tgt_type = alloc_node<ConstantNode>();
  6037. tgt_type->line = op->line;
  6038. tgt_type->value = (int)lh_type.builtin_type;
  6039. OperatorNode *convert_call = alloc_node<OperatorNode>();
  6040. convert_call->line = op->line;
  6041. convert_call->op = OperatorNode::OP_CALL;
  6042. convert_call->arguments.push_back(convert);
  6043. convert_call->arguments.push_back(op->arguments[1]);
  6044. convert_call->arguments.push_back(tgt_type);
  6045. op->arguments[1] = convert_call;
  6046. }
  6047. } break;
  6048. case OperatorNode::OP_CALL:
  6049. case OperatorNode::OP_PARENT_CALL: {
  6050. _reduce_function_call_type(op);
  6051. if (error_set) return;
  6052. } break;
  6053. default: {
  6054. // TODO: Make this a warning
  6055. /*_set_error("Standalone expression, nothing is done in this line.", statement->line);
  6056. return; */
  6057. }
  6058. }
  6059. } break;
  6060. case Node::TYPE_CONTROL_FLOW: {
  6061. ControlFlowNode *cf = static_cast<ControlFlowNode *>(statement);
  6062. switch (cf->cf_type) {
  6063. case ControlFlowNode::CF_RETURN: {
  6064. DataType function_type = current_function->get_datatype();
  6065. if (!function_type.has_type) break;
  6066. if (function_type.kind == DataType::BUILTIN && function_type.builtin_type == Variant::NIL) {
  6067. // Return void, should not have arguments
  6068. if (cf->arguments.size() > 0) {
  6069. _set_error("Void function cannot return a value.", cf->line, cf->column);
  6070. return;
  6071. }
  6072. } else {
  6073. // Return something, cannot be empty
  6074. if (cf->arguments.size() == 0) {
  6075. _set_error("Non-void function must return a value.", cf->line, cf->column);
  6076. return;
  6077. }
  6078. DataType ret_type = _reduce_node_type(cf->arguments[0]);
  6079. if (error_set) {
  6080. return;
  6081. }
  6082. if (!_is_type_compatible(function_type, ret_type)) {
  6083. _set_error("Returned value type (" + ret_type.to_string() + ") doesn't match the function return type (" +
  6084. function_type.to_string() + ").",
  6085. cf->line, cf->column);
  6086. return;
  6087. }
  6088. }
  6089. } break;
  6090. case ControlFlowNode::CF_MATCH: {
  6091. MatchNode *match_node = cf->match;
  6092. _transform_match_statment(match_node);
  6093. } break;
  6094. }
  6095. } break;
  6096. case Node::TYPE_CONSTANT: {
  6097. ConstantNode *cn = static_cast<ConstantNode *>(statement);
  6098. // Strings are fine since they can be multiline comments
  6099. if (cn->value.get_type() == Variant::STRING) {
  6100. break;
  6101. }
  6102. } // falthrough
  6103. default: {
  6104. // TODO: Make this a warning
  6105. /* _set_error("Standalone expression, nothing is done in this line.", statement->line);
  6106. return; */
  6107. }
  6108. }
  6109. }
  6110. // Parse sub blocks
  6111. for (int i = 0; i < p_block->sub_blocks.size(); i++) {
  6112. current_block = p_block->sub_blocks[i];
  6113. _check_block_types(current_block);
  6114. current_block = p_block;
  6115. if (error_set) return;
  6116. }
  6117. }
  6118. void GDScriptParser::_set_error(const String &p_error, int p_line, int p_column) {
  6119. if (error_set)
  6120. return; //allow no further errors
  6121. error = p_error;
  6122. error_line = p_line < 0 ? tokenizer->get_token_line() : p_line;
  6123. error_column = p_column < 0 ? tokenizer->get_token_column() : p_column;
  6124. error_set = true;
  6125. }
  6126. String GDScriptParser::get_error() const {
  6127. return error;
  6128. }
  6129. int GDScriptParser::get_error_line() const {
  6130. return error_line;
  6131. }
  6132. int GDScriptParser::get_error_column() const {
  6133. return error_column;
  6134. }
  6135. Error GDScriptParser::_parse(const String &p_base_path) {
  6136. base_path = p_base_path;
  6137. //assume class
  6138. ClassNode *main_class = alloc_node<ClassNode>();
  6139. main_class->initializer = alloc_node<BlockNode>();
  6140. main_class->initializer->parent_class = main_class;
  6141. main_class->ready = alloc_node<BlockNode>();
  6142. main_class->ready->parent_class = main_class;
  6143. current_class = main_class;
  6144. _parse_class(main_class);
  6145. if (tokenizer->get_token() == GDScriptTokenizer::TK_ERROR) {
  6146. error_set = false;
  6147. _set_error("Parse Error: " + tokenizer->get_token_error());
  6148. }
  6149. if (error_set) {
  6150. return ERR_PARSE_ERROR;
  6151. }
  6152. _determine_inheritance(main_class);
  6153. if (error_set) {
  6154. return ERR_PARSE_ERROR;
  6155. }
  6156. current_class = main_class;
  6157. current_function = NULL;
  6158. current_block = NULL;
  6159. if (for_completion) check_types = false;
  6160. if (check_types) {
  6161. // Resolve all class-level stuff before getting into function blocks
  6162. _check_class_level_types(main_class);
  6163. if (error_set) {
  6164. return ERR_PARSE_ERROR;
  6165. }
  6166. }
  6167. // Resolve the function blocks
  6168. _check_class_blocks_types(main_class);
  6169. if (error_set) {
  6170. return ERR_PARSE_ERROR;
  6171. }
  6172. return OK;
  6173. }
  6174. Error GDScriptParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path, const String &p_self_path) {
  6175. clear();
  6176. self_path = p_self_path;
  6177. GDScriptTokenizerBuffer *tb = memnew(GDScriptTokenizerBuffer);
  6178. tb->set_code_buffer(p_bytecode);
  6179. tokenizer = tb;
  6180. Error ret = _parse(p_base_path);
  6181. memdelete(tb);
  6182. tokenizer = NULL;
  6183. return ret;
  6184. }
  6185. Error GDScriptParser::parse(const String &p_code, const String &p_base_path, bool p_just_validate, const String &p_self_path, bool p_for_completion) {
  6186. clear();
  6187. self_path = p_self_path;
  6188. GDScriptTokenizerText *tt = memnew(GDScriptTokenizerText);
  6189. tt->set_code(p_code);
  6190. validating = p_just_validate;
  6191. for_completion = p_for_completion;
  6192. tokenizer = tt;
  6193. Error ret = _parse(p_base_path);
  6194. memdelete(tt);
  6195. tokenizer = NULL;
  6196. return ret;
  6197. }
  6198. bool GDScriptParser::is_tool_script() const {
  6199. return (head && head->type == Node::TYPE_CLASS && static_cast<const ClassNode *>(head)->tool);
  6200. }
  6201. const GDScriptParser::Node *GDScriptParser::get_parse_tree() const {
  6202. return head;
  6203. }
  6204. void GDScriptParser::clear() {
  6205. while (list) {
  6206. Node *l = list;
  6207. list = list->next;
  6208. memdelete(l);
  6209. }
  6210. head = NULL;
  6211. list = NULL;
  6212. completion_type = COMPLETION_NONE;
  6213. completion_node = NULL;
  6214. completion_class = NULL;
  6215. completion_function = NULL;
  6216. completion_block = NULL;
  6217. current_block = NULL;
  6218. current_class = NULL;
  6219. completion_found = false;
  6220. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  6221. current_function = NULL;
  6222. validating = false;
  6223. for_completion = false;
  6224. error_set = false;
  6225. tab_level.clear();
  6226. tab_level.push_back(0);
  6227. error_line = 0;
  6228. error_column = 0;
  6229. pending_newline = -1;
  6230. parenthesis = 0;
  6231. current_export.type = Variant::NIL;
  6232. check_types = true;
  6233. error = "";
  6234. }
  6235. GDScriptParser::CompletionType GDScriptParser::get_completion_type() {
  6236. return completion_type;
  6237. }
  6238. StringName GDScriptParser::get_completion_cursor() {
  6239. return completion_cursor;
  6240. }
  6241. int GDScriptParser::get_completion_line() {
  6242. return completion_line;
  6243. }
  6244. Variant::Type GDScriptParser::get_completion_built_in_constant() {
  6245. return completion_built_in_constant;
  6246. }
  6247. GDScriptParser::Node *GDScriptParser::get_completion_node() {
  6248. return completion_node;
  6249. }
  6250. GDScriptParser::BlockNode *GDScriptParser::get_completion_block() {
  6251. return completion_block;
  6252. }
  6253. GDScriptParser::ClassNode *GDScriptParser::get_completion_class() {
  6254. return completion_class;
  6255. }
  6256. GDScriptParser::FunctionNode *GDScriptParser::get_completion_function() {
  6257. return completion_function;
  6258. }
  6259. int GDScriptParser::get_completion_argument_index() {
  6260. return completion_argument;
  6261. }
  6262. int GDScriptParser::get_completion_identifier_is_function() {
  6263. return completion_ident_is_call;
  6264. }
  6265. GDScriptParser::GDScriptParser() {
  6266. head = NULL;
  6267. list = NULL;
  6268. tokenizer = NULL;
  6269. pending_newline = -1;
  6270. clear();
  6271. }
  6272. GDScriptParser::~GDScriptParser() {
  6273. clear();
  6274. }