2
0

gdscript_parser.cpp 228 KB

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