gdscript_parser.cpp 144 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936
  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 "gdscript.h"
  32. #include "io/resource_loader.h"
  33. #include "os/file_access.h"
  34. #include "print_string.h"
  35. #include "script_language.h"
  36. template <class T>
  37. T *GDScriptParser::alloc_node() {
  38. T *t = memnew(T);
  39. t->next = list;
  40. list = t;
  41. if (!head)
  42. head = t;
  43. t->line = tokenizer->get_token_line();
  44. t->column = tokenizer->get_token_column();
  45. return t;
  46. }
  47. bool GDScriptParser::_end_statement() {
  48. if (tokenizer->get_token() == GDScriptTokenizer::TK_SEMICOLON) {
  49. tokenizer->advance();
  50. return true; //handle next
  51. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE || tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  52. return true; //will be handled properly
  53. }
  54. return false;
  55. }
  56. bool GDScriptParser::_enter_indent_block(BlockNode *p_block) {
  57. if (tokenizer->get_token() != GDScriptTokenizer::TK_COLON) {
  58. // report location at the previous token (on the previous line)
  59. int error_line = tokenizer->get_token_line(-1);
  60. int error_column = tokenizer->get_token_column(-1);
  61. _set_error("':' expected at end of line.", error_line, error_column);
  62. return false;
  63. }
  64. tokenizer->advance();
  65. if (tokenizer->get_token() != GDScriptTokenizer::TK_NEWLINE) {
  66. // be more python-like
  67. int current = tab_level.back()->get();
  68. tab_level.push_back(current);
  69. return true;
  70. //_set_error("newline expected after ':'.");
  71. //return false;
  72. }
  73. while (true) {
  74. if (tokenizer->get_token() != GDScriptTokenizer::TK_NEWLINE) {
  75. return false; //wtf
  76. } else if (tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE) {
  77. int indent = tokenizer->get_token_line_indent();
  78. int current = tab_level.back()->get();
  79. if (indent <= current) {
  80. return false;
  81. }
  82. tab_level.push_back(indent);
  83. tokenizer->advance();
  84. return true;
  85. } else if (p_block) {
  86. NewLineNode *nl = alloc_node<NewLineNode>();
  87. nl->line = tokenizer->get_token_line();
  88. p_block->statements.push_back(nl);
  89. }
  90. tokenizer->advance(); // go to next newline
  91. }
  92. }
  93. bool GDScriptParser::_parse_arguments(Node *p_parent, Vector<Node *> &p_args, bool p_static, bool p_can_codecomplete) {
  94. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  95. tokenizer->advance();
  96. } else {
  97. parenthesis++;
  98. int argidx = 0;
  99. while (true) {
  100. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  101. _make_completable_call(argidx);
  102. completion_node = p_parent;
  103. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING && tokenizer->get_token(1) == GDScriptTokenizer::TK_CURSOR) {
  104. //completing a string argument..
  105. completion_cursor = tokenizer->get_token_constant();
  106. _make_completable_call(argidx);
  107. completion_node = p_parent;
  108. tokenizer->advance(1);
  109. return false;
  110. }
  111. Node *arg = _parse_expression(p_parent, p_static);
  112. if (!arg)
  113. return false;
  114. p_args.push_back(arg);
  115. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  116. tokenizer->advance();
  117. break;
  118. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  119. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  120. _set_error("Expression expected");
  121. return false;
  122. }
  123. tokenizer->advance();
  124. argidx++;
  125. } else {
  126. // something is broken
  127. _set_error("Expected ',' or ')'");
  128. return false;
  129. }
  130. }
  131. parenthesis--;
  132. }
  133. return true;
  134. }
  135. void GDScriptParser::_make_completable_call(int p_arg) {
  136. completion_cursor = StringName();
  137. completion_type = COMPLETION_CALL_ARGUMENTS;
  138. completion_class = current_class;
  139. completion_function = current_function;
  140. completion_line = tokenizer->get_token_line();
  141. completion_argument = p_arg;
  142. completion_block = current_block;
  143. completion_found = true;
  144. tokenizer->advance();
  145. }
  146. bool GDScriptParser::_get_completable_identifier(CompletionType p_type, StringName &identifier) {
  147. identifier = StringName();
  148. if (tokenizer->is_token_literal()) {
  149. identifier = tokenizer->get_token_literal();
  150. tokenizer->advance();
  151. }
  152. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  153. completion_cursor = identifier;
  154. completion_type = p_type;
  155. completion_class = current_class;
  156. completion_function = current_function;
  157. completion_line = tokenizer->get_token_line();
  158. completion_block = current_block;
  159. completion_found = true;
  160. completion_ident_is_call = false;
  161. tokenizer->advance();
  162. if (tokenizer->is_token_literal()) {
  163. identifier = identifier.operator String() + tokenizer->get_token_literal().operator String();
  164. tokenizer->advance();
  165. }
  166. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  167. completion_ident_is_call = true;
  168. }
  169. return true;
  170. }
  171. return false;
  172. }
  173. GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_static, bool p_allow_assign, bool p_parsing_constant) {
  174. //Vector<Node*> expressions;
  175. //Vector<OperatorNode::Operator> operators;
  176. Vector<Expression> expression;
  177. Node *expr = NULL;
  178. int op_line = tokenizer->get_token_line(); // when operators are created at the bottom, the line might have been changed (\n found)
  179. while (true) {
  180. /*****************/
  181. /* Parse Operand */
  182. /*****************/
  183. if (parenthesis > 0) {
  184. //remove empty space (only allowed if inside parenthesis
  185. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  186. tokenizer->advance();
  187. }
  188. }
  189. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  190. //subexpression ()
  191. tokenizer->advance();
  192. parenthesis++;
  193. Node *subexpr = _parse_expression(p_parent, p_static, p_allow_assign, p_parsing_constant);
  194. parenthesis--;
  195. if (!subexpr)
  196. return NULL;
  197. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  198. _set_error("Expected ')' in expression");
  199. return NULL;
  200. }
  201. tokenizer->advance();
  202. expr = subexpr;
  203. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_DOLLAR) {
  204. tokenizer->advance();
  205. String path;
  206. bool need_identifier = true;
  207. bool done = false;
  208. while (!done) {
  209. switch (tokenizer->get_token()) {
  210. case GDScriptTokenizer::TK_CURSOR: {
  211. completion_cursor = StringName();
  212. completion_type = COMPLETION_GET_NODE;
  213. completion_class = current_class;
  214. completion_function = current_function;
  215. completion_line = tokenizer->get_token_line();
  216. completion_cursor = path;
  217. completion_argument = 0;
  218. completion_block = current_block;
  219. completion_found = true;
  220. tokenizer->advance();
  221. } break;
  222. case GDScriptTokenizer::TK_CONSTANT: {
  223. if (!need_identifier) {
  224. done = true;
  225. break;
  226. }
  227. if (tokenizer->get_token_constant().get_type() != Variant::STRING) {
  228. _set_error("Expected string constant or identifier after '$' or '/'.");
  229. return NULL;
  230. }
  231. path += String(tokenizer->get_token_constant());
  232. tokenizer->advance();
  233. need_identifier = false;
  234. } break;
  235. case GDScriptTokenizer::TK_OP_DIV: {
  236. if (need_identifier) {
  237. done = true;
  238. break;
  239. }
  240. path += "/";
  241. tokenizer->advance();
  242. need_identifier = true;
  243. } break;
  244. default: {
  245. // Instead of checking for TK_IDENTIFIER, we check with is_token_literal, as this allows us to use match/sync/etc. as a name
  246. if (need_identifier && tokenizer->is_token_literal()) {
  247. path += String(tokenizer->get_token_literal());
  248. tokenizer->advance();
  249. need_identifier = false;
  250. } else {
  251. done = true;
  252. }
  253. break;
  254. }
  255. }
  256. }
  257. if (path == "") {
  258. _set_error("Path expected after $.");
  259. return NULL;
  260. }
  261. OperatorNode *op = alloc_node<OperatorNode>();
  262. op->op = OperatorNode::OP_CALL;
  263. op->arguments.push_back(alloc_node<SelfNode>());
  264. IdentifierNode *funcname = alloc_node<IdentifierNode>();
  265. funcname->name = "get_node";
  266. op->arguments.push_back(funcname);
  267. ConstantNode *nodepath = alloc_node<ConstantNode>();
  268. nodepath->value = NodePath(StringName(path));
  269. op->arguments.push_back(nodepath);
  270. expr = op;
  271. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  272. tokenizer->advance();
  273. continue; //no point in cursor in the middle of expression
  274. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT) {
  275. //constant defined by tokenizer
  276. ConstantNode *constant = alloc_node<ConstantNode>();
  277. constant->value = tokenizer->get_token_constant();
  278. tokenizer->advance();
  279. expr = constant;
  280. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_PI) {
  281. //constant defined by tokenizer
  282. ConstantNode *constant = alloc_node<ConstantNode>();
  283. constant->value = Math_PI;
  284. tokenizer->advance();
  285. expr = constant;
  286. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_TAU) {
  287. //constant defined by tokenizer
  288. ConstantNode *constant = alloc_node<ConstantNode>();
  289. constant->value = Math_TAU;
  290. tokenizer->advance();
  291. expr = constant;
  292. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_INF) {
  293. //constant defined by tokenizer
  294. ConstantNode *constant = alloc_node<ConstantNode>();
  295. constant->value = Math_INF;
  296. tokenizer->advance();
  297. expr = constant;
  298. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CONST_NAN) {
  299. //constant defined by tokenizer
  300. ConstantNode *constant = alloc_node<ConstantNode>();
  301. constant->value = Math_NAN;
  302. tokenizer->advance();
  303. expr = constant;
  304. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_PRELOAD) {
  305. //constant defined by tokenizer
  306. tokenizer->advance();
  307. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  308. _set_error("Expected '(' after 'preload'");
  309. return NULL;
  310. }
  311. tokenizer->advance();
  312. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  313. completion_cursor = StringName();
  314. completion_node = p_parent;
  315. completion_type = COMPLETION_RESOURCE_PATH;
  316. completion_class = current_class;
  317. completion_function = current_function;
  318. completion_line = tokenizer->get_token_line();
  319. completion_block = current_block;
  320. completion_argument = 0;
  321. completion_found = true;
  322. tokenizer->advance();
  323. }
  324. String path;
  325. bool found_constant = false;
  326. bool valid = false;
  327. ConstantNode *cn;
  328. Node *subexpr = _parse_and_reduce_expression(p_parent, p_static);
  329. if (subexpr) {
  330. if (subexpr->type == Node::TYPE_CONSTANT) {
  331. cn = static_cast<ConstantNode *>(subexpr);
  332. found_constant = true;
  333. }
  334. if (subexpr->type == Node::TYPE_IDENTIFIER) {
  335. IdentifierNode *in = static_cast<IdentifierNode *>(subexpr);
  336. Vector<ClassNode::Constant> ce = current_class->constant_expressions;
  337. // Try to find the constant expression by the identifier
  338. for (int i = 0; i < ce.size(); ++i) {
  339. if (ce[i].identifier == in->name) {
  340. if (ce[i].expression->type == Node::TYPE_CONSTANT) {
  341. cn = static_cast<ConstantNode *>(ce[i].expression);
  342. found_constant = true;
  343. }
  344. }
  345. }
  346. }
  347. if (found_constant && cn->value.get_type() == Variant::STRING) {
  348. valid = true;
  349. path = (String)cn->value;
  350. }
  351. }
  352. if (!valid) {
  353. _set_error("expected string constant as 'preload' argument.");
  354. return NULL;
  355. }
  356. if (!path.is_abs_path() && base_path != "")
  357. path = base_path + "/" + path;
  358. path = path.replace("///", "//").simplify_path();
  359. if (path == self_path) {
  360. _set_error("Can't preload itself (use 'get_script()').");
  361. return NULL;
  362. }
  363. Ref<Resource> res;
  364. if (!validating) {
  365. //this can be too slow for just validating code
  366. if (for_completion && ScriptCodeCompletionCache::get_singleton() && FileAccess::exists(path)) {
  367. res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
  368. } else if (!for_completion || FileAccess::exists(path)) {
  369. res = ResourceLoader::load(path);
  370. }
  371. } else {
  372. if (!FileAccess::exists(path)) {
  373. _set_error("Can't preload resource at path: " + path);
  374. return NULL;
  375. } else if (ScriptCodeCompletionCache::get_singleton()) {
  376. res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
  377. }
  378. }
  379. if (!res.is_valid()) {
  380. _set_error("Can't preload resource at path: " + path);
  381. return NULL;
  382. }
  383. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  384. _set_error("Expected ')' after 'preload' path");
  385. return NULL;
  386. }
  387. tokenizer->advance();
  388. ConstantNode *constant = alloc_node<ConstantNode>();
  389. constant->value = res;
  390. expr = constant;
  391. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_YIELD) {
  392. //constant defined by tokenizer
  393. tokenizer->advance();
  394. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  395. _set_error("Expected '(' after 'yield'");
  396. return NULL;
  397. }
  398. tokenizer->advance();
  399. OperatorNode *yield = alloc_node<OperatorNode>();
  400. yield->op = OperatorNode::OP_YIELD;
  401. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  402. tokenizer->advance();
  403. }
  404. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  405. expr = yield;
  406. tokenizer->advance();
  407. } else {
  408. parenthesis++;
  409. Node *object = _parse_and_reduce_expression(p_parent, p_static);
  410. if (!object)
  411. return NULL;
  412. yield->arguments.push_back(object);
  413. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  414. _set_error("Expected ',' after first argument of 'yield'");
  415. return NULL;
  416. }
  417. tokenizer->advance();
  418. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  419. completion_cursor = StringName();
  420. completion_node = object;
  421. completion_type = COMPLETION_YIELD;
  422. completion_class = current_class;
  423. completion_function = current_function;
  424. completion_line = tokenizer->get_token_line();
  425. completion_argument = 0;
  426. completion_block = current_block;
  427. completion_found = true;
  428. tokenizer->advance();
  429. }
  430. Node *signal = _parse_and_reduce_expression(p_parent, p_static);
  431. if (!signal)
  432. return NULL;
  433. yield->arguments.push_back(signal);
  434. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  435. _set_error("Expected ')' after second argument of 'yield'");
  436. return NULL;
  437. }
  438. parenthesis--;
  439. tokenizer->advance();
  440. expr = yield;
  441. }
  442. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_SELF) {
  443. if (p_static) {
  444. _set_error("'self'' not allowed in static function or constant expression");
  445. return NULL;
  446. }
  447. //constant defined by tokenizer
  448. SelfNode *self = alloc_node<SelfNode>();
  449. tokenizer->advance();
  450. expr = self;
  451. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) {
  452. Variant::Type bi_type = tokenizer->get_token_type();
  453. tokenizer->advance(2);
  454. StringName identifier;
  455. if (_get_completable_identifier(COMPLETION_BUILT_IN_TYPE_CONSTANT, identifier)) {
  456. completion_built_in_constant = bi_type;
  457. }
  458. if (identifier == StringName()) {
  459. _set_error("Built-in type constant or static function expected after '.'");
  460. return NULL;
  461. }
  462. if (!Variant::has_numeric_constant(bi_type, identifier)) {
  463. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN &&
  464. Variant::is_method_const(bi_type, identifier) &&
  465. Variant::get_method_return_type(bi_type, identifier) == bi_type) {
  466. tokenizer->advance();
  467. OperatorNode *construct = alloc_node<OperatorNode>();
  468. construct->op = OperatorNode::OP_CALL;
  469. TypeNode *tn = alloc_node<TypeNode>();
  470. tn->vtype = bi_type;
  471. construct->arguments.push_back(tn);
  472. OperatorNode *op = alloc_node<OperatorNode>();
  473. op->op = OperatorNode::OP_CALL;
  474. op->arguments.push_back(construct);
  475. IdentifierNode *id = alloc_node<IdentifierNode>();
  476. id->name = identifier;
  477. op->arguments.push_back(id);
  478. if (!_parse_arguments(op, op->arguments, p_static, true))
  479. return NULL;
  480. expr = op;
  481. } else {
  482. _set_error("Static constant '" + identifier.operator String() + "' not present in built-in type " + Variant::get_type_name(bi_type) + ".");
  483. return NULL;
  484. }
  485. } else {
  486. ConstantNode *cn = alloc_node<ConstantNode>();
  487. cn->value = Variant::get_numeric_constant_value(bi_type, identifier);
  488. expr = cn;
  489. }
  490. } else if (tokenizer->get_token(1) == GDScriptTokenizer::TK_PARENTHESIS_OPEN && tokenizer->is_token_literal()) {
  491. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  492. //function or constructor
  493. OperatorNode *op = alloc_node<OperatorNode>();
  494. op->op = OperatorNode::OP_CALL;
  495. //Do a quick Array and Dictionary Check. Replace if either require no arguments.
  496. bool replaced = false;
  497. if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE) {
  498. Variant::Type ct = tokenizer->get_token_type();
  499. if (p_parsing_constant == false) {
  500. if (ct == Variant::ARRAY) {
  501. if (tokenizer->get_token(2) == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  502. ArrayNode *arr = alloc_node<ArrayNode>();
  503. expr = arr;
  504. replaced = true;
  505. tokenizer->advance(3);
  506. }
  507. }
  508. if (ct == Variant::DICTIONARY) {
  509. if (tokenizer->get_token(2) == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  510. DictionaryNode *dict = alloc_node<DictionaryNode>();
  511. expr = dict;
  512. replaced = true;
  513. tokenizer->advance(3);
  514. }
  515. }
  516. }
  517. if (!replaced) {
  518. TypeNode *tn = alloc_node<TypeNode>();
  519. tn->vtype = tokenizer->get_token_type();
  520. op->arguments.push_back(tn);
  521. tokenizer->advance(2);
  522. }
  523. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_FUNC) {
  524. BuiltInFunctionNode *bn = alloc_node<BuiltInFunctionNode>();
  525. bn->function = tokenizer->get_token_built_in_func();
  526. op->arguments.push_back(bn);
  527. tokenizer->advance(2);
  528. } else {
  529. SelfNode *self = alloc_node<SelfNode>();
  530. op->arguments.push_back(self);
  531. StringName identifier;
  532. if (_get_completable_identifier(COMPLETION_FUNCTION, identifier)) {
  533. }
  534. IdentifierNode *id = alloc_node<IdentifierNode>();
  535. id->name = identifier;
  536. op->arguments.push_back(id);
  537. tokenizer->advance(1);
  538. }
  539. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  540. _make_completable_call(0);
  541. completion_node = op;
  542. }
  543. if (!replaced) {
  544. if (!_parse_arguments(op, op->arguments, p_static, true))
  545. return NULL;
  546. expr = op;
  547. }
  548. } else if (tokenizer->is_token_literal(0, true)) {
  549. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  550. //identifier (reference)
  551. const ClassNode *cln = current_class;
  552. bool bfn = false;
  553. StringName identifier;
  554. if (_get_completable_identifier(COMPLETION_IDENTIFIER, identifier)) {
  555. }
  556. if (p_parsing_constant) {
  557. for (int i = 0; i < cln->constant_expressions.size(); ++i) {
  558. if (cln->constant_expressions[i].identifier == identifier) {
  559. expr = cln->constant_expressions[i].expression;
  560. bfn = true;
  561. break;
  562. }
  563. }
  564. if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) {
  565. //check from constants
  566. ConstantNode *constant = alloc_node<ConstantNode>();
  567. constant->value = GDScriptLanguage::get_singleton()->get_global_array()[GDScriptLanguage::get_singleton()->get_global_map()[identifier]];
  568. expr = constant;
  569. bfn = true;
  570. }
  571. if (!bfn && GDScriptLanguage::get_singleton()->get_named_globals_map().has(identifier)) {
  572. //check from singletons
  573. ConstantNode *constant = alloc_node<ConstantNode>();
  574. constant->value = GDScriptLanguage::get_singleton()->get_named_globals_map()[identifier];
  575. expr = constant;
  576. bfn = true;
  577. }
  578. }
  579. if (!bfn) {
  580. IdentifierNode *id = alloc_node<IdentifierNode>();
  581. id->name = identifier;
  582. expr = id;
  583. }
  584. } 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) {
  585. //single prefix operators like !expr +expr -expr ++expr --expr
  586. alloc_node<OperatorNode>();
  587. Expression e;
  588. e.is_op = true;
  589. switch (tokenizer->get_token()) {
  590. case GDScriptTokenizer::TK_OP_ADD: e.op = OperatorNode::OP_POS; break;
  591. case GDScriptTokenizer::TK_OP_SUB: e.op = OperatorNode::OP_NEG; break;
  592. case GDScriptTokenizer::TK_OP_NOT: e.op = OperatorNode::OP_NOT; break;
  593. case GDScriptTokenizer::TK_OP_BIT_INVERT: e.op = OperatorNode::OP_BIT_INVERT; break;
  594. default: {}
  595. }
  596. tokenizer->advance();
  597. if (e.op != OperatorNode::OP_NOT && tokenizer->get_token() == GDScriptTokenizer::TK_OP_NOT) {
  598. _set_error("Misplaced 'not'.");
  599. return NULL;
  600. }
  601. expression.push_back(e);
  602. continue; //only exception, must continue...
  603. /*
  604. Node *subexpr=_parse_expression(op,p_static);
  605. if (!subexpr)
  606. return NULL;
  607. op->arguments.push_back(subexpr);
  608. expr=op;*/
  609. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_OPEN) {
  610. // array
  611. tokenizer->advance();
  612. ArrayNode *arr = alloc_node<ArrayNode>();
  613. bool expecting_comma = false;
  614. while (true) {
  615. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  616. _set_error("Unterminated array");
  617. return NULL;
  618. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  619. tokenizer->advance();
  620. break;
  621. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  622. tokenizer->advance(); //ignore newline
  623. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  624. if (!expecting_comma) {
  625. _set_error("expression or ']' expected");
  626. return NULL;
  627. }
  628. expecting_comma = false;
  629. tokenizer->advance(); //ignore newline
  630. } else {
  631. //parse expression
  632. if (expecting_comma) {
  633. _set_error("',' or ']' expected");
  634. return NULL;
  635. }
  636. Node *n = _parse_expression(arr, p_static, p_allow_assign, p_parsing_constant);
  637. if (!n)
  638. return NULL;
  639. arr->elements.push_back(n);
  640. expecting_comma = true;
  641. }
  642. }
  643. expr = arr;
  644. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_OPEN) {
  645. // array
  646. tokenizer->advance();
  647. DictionaryNode *dict = alloc_node<DictionaryNode>();
  648. enum DictExpect {
  649. DICT_EXPECT_KEY,
  650. DICT_EXPECT_COLON,
  651. DICT_EXPECT_VALUE,
  652. DICT_EXPECT_COMMA
  653. };
  654. Node *key = NULL;
  655. Set<Variant> keys;
  656. DictExpect expecting = DICT_EXPECT_KEY;
  657. while (true) {
  658. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  659. _set_error("Unterminated dictionary");
  660. return NULL;
  661. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  662. if (expecting == DICT_EXPECT_COLON) {
  663. _set_error("':' expected");
  664. return NULL;
  665. }
  666. if (expecting == DICT_EXPECT_VALUE) {
  667. _set_error("value expected");
  668. return NULL;
  669. }
  670. tokenizer->advance();
  671. break;
  672. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  673. tokenizer->advance(); //ignore newline
  674. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  675. if (expecting == DICT_EXPECT_KEY) {
  676. _set_error("key or '}' expected");
  677. return NULL;
  678. }
  679. if (expecting == DICT_EXPECT_VALUE) {
  680. _set_error("value expected");
  681. return NULL;
  682. }
  683. if (expecting == DICT_EXPECT_COLON) {
  684. _set_error("':' expected");
  685. return NULL;
  686. }
  687. expecting = DICT_EXPECT_KEY;
  688. tokenizer->advance(); //ignore newline
  689. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  690. if (expecting == DICT_EXPECT_KEY) {
  691. _set_error("key or '}' expected");
  692. return NULL;
  693. }
  694. if (expecting == DICT_EXPECT_VALUE) {
  695. _set_error("value expected");
  696. return NULL;
  697. }
  698. if (expecting == DICT_EXPECT_COMMA) {
  699. _set_error("',' or '}' expected");
  700. return NULL;
  701. }
  702. expecting = DICT_EXPECT_VALUE;
  703. tokenizer->advance(); //ignore newline
  704. } else {
  705. if (expecting == DICT_EXPECT_COMMA) {
  706. _set_error("',' or '}' expected");
  707. return NULL;
  708. }
  709. if (expecting == DICT_EXPECT_COLON) {
  710. _set_error("':' expected");
  711. return NULL;
  712. }
  713. if (expecting == DICT_EXPECT_KEY) {
  714. if (tokenizer->is_token_literal() && tokenizer->get_token(1) == GDScriptTokenizer::TK_OP_ASSIGN) {
  715. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  716. //lua style identifier, easier to write
  717. ConstantNode *cn = alloc_node<ConstantNode>();
  718. cn->value = tokenizer->get_token_literal();
  719. key = cn;
  720. tokenizer->advance(2);
  721. expecting = DICT_EXPECT_VALUE;
  722. } else {
  723. //python/js style more flexible
  724. key = _parse_expression(dict, p_static, p_allow_assign, p_parsing_constant);
  725. if (!key)
  726. return NULL;
  727. expecting = DICT_EXPECT_COLON;
  728. }
  729. }
  730. if (expecting == DICT_EXPECT_VALUE) {
  731. Node *value = _parse_expression(dict, p_static, p_allow_assign, p_parsing_constant);
  732. if (!value)
  733. return NULL;
  734. expecting = DICT_EXPECT_COMMA;
  735. if (key->type == GDScriptParser::Node::TYPE_CONSTANT) {
  736. Variant const &keyName = static_cast<const GDScriptParser::ConstantNode *>(key)->value;
  737. if (keys.has(keyName)) {
  738. _set_error("Duplicate key found in Dictionary literal");
  739. return NULL;
  740. }
  741. keys.insert(keyName);
  742. }
  743. DictionaryNode::Pair pair;
  744. pair.key = key;
  745. pair.value = value;
  746. dict->elements.push_back(pair);
  747. key = NULL;
  748. }
  749. }
  750. }
  751. expr = dict;
  752. } 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) {
  753. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  754. // parent call
  755. tokenizer->advance(); //goto identifier
  756. OperatorNode *op = alloc_node<OperatorNode>();
  757. op->op = OperatorNode::OP_PARENT_CALL;
  758. /*SelfNode *self = alloc_node<SelfNode>();
  759. op->arguments.push_back(self);
  760. forbidden for now */
  761. StringName identifier;
  762. if (_get_completable_identifier(COMPLETION_PARENT_FUNCTION, identifier)) {
  763. //indexing stuff
  764. }
  765. IdentifierNode *id = alloc_node<IdentifierNode>();
  766. id->name = identifier;
  767. op->arguments.push_back(id);
  768. tokenizer->advance(1);
  769. if (!_parse_arguments(op, op->arguments, p_static))
  770. return NULL;
  771. expr = op;
  772. } else {
  773. //find list [ or find dictionary {
  774. //print_line("found bug?");
  775. _set_error("Error parsing expression, misplaced: " + String(tokenizer->get_token_name(tokenizer->get_token())));
  776. return NULL; //nothing
  777. }
  778. if (!expr) {
  779. ERR_EXPLAIN("GDScriptParser bug, couldn't figure out what expression is...");
  780. ERR_FAIL_COND_V(!expr, NULL);
  781. }
  782. /******************/
  783. /* Parse Indexing */
  784. /******************/
  785. while (true) {
  786. //expressions can be indexed any number of times
  787. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
  788. //indexing using "."
  789. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_CURSOR && !tokenizer->is_token_literal(1)) {
  790. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  791. _set_error("Expected identifier as member");
  792. return NULL;
  793. } else if (tokenizer->get_token(2) == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  794. //call!!
  795. OperatorNode *op = alloc_node<OperatorNode>();
  796. op->op = OperatorNode::OP_CALL;
  797. tokenizer->advance();
  798. IdentifierNode *id = alloc_node<IdentifierNode>();
  799. if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_FUNC) {
  800. //small hack so built in funcs don't obfuscate methods
  801. id->name = GDScriptFunctions::get_func_name(tokenizer->get_token_built_in_func());
  802. tokenizer->advance();
  803. } else {
  804. StringName identifier;
  805. if (_get_completable_identifier(COMPLETION_METHOD, identifier)) {
  806. completion_node = op;
  807. //indexing stuff
  808. }
  809. id->name = identifier;
  810. }
  811. op->arguments.push_back(expr); // call what
  812. op->arguments.push_back(id); // call func
  813. //get arguments
  814. tokenizer->advance(1);
  815. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURSOR) {
  816. _make_completable_call(0);
  817. completion_node = op;
  818. }
  819. if (!_parse_arguments(op, op->arguments, p_static, true))
  820. return NULL;
  821. expr = op;
  822. } else {
  823. //simple indexing!
  824. OperatorNode *op = alloc_node<OperatorNode>();
  825. op->op = OperatorNode::OP_INDEX_NAMED;
  826. tokenizer->advance();
  827. StringName identifier;
  828. if (_get_completable_identifier(COMPLETION_INDEX, identifier)) {
  829. if (identifier == StringName()) {
  830. identifier = "@temp"; //so it parses allright
  831. }
  832. completion_node = op;
  833. //indexing stuff
  834. }
  835. IdentifierNode *id = alloc_node<IdentifierNode>();
  836. id->name = identifier;
  837. op->arguments.push_back(expr);
  838. op->arguments.push_back(id);
  839. expr = op;
  840. }
  841. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_OPEN) {
  842. //indexing using "[]"
  843. OperatorNode *op = alloc_node<OperatorNode>();
  844. op->op = OperatorNode::OP_INDEX;
  845. tokenizer->advance(1);
  846. Node *subexpr = _parse_expression(op, p_static, p_allow_assign, p_parsing_constant);
  847. if (!subexpr) {
  848. return NULL;
  849. }
  850. if (tokenizer->get_token() != GDScriptTokenizer::TK_BRACKET_CLOSE) {
  851. _set_error("Expected ']'");
  852. return NULL;
  853. }
  854. op->arguments.push_back(expr);
  855. op->arguments.push_back(subexpr);
  856. tokenizer->advance(1);
  857. expr = op;
  858. } else
  859. break;
  860. }
  861. /*****************/
  862. /* Parse Casting */
  863. /*****************/
  864. bool has_casting = expr->type == Node::TYPE_CAST;
  865. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_AS) {
  866. if (has_casting) {
  867. _set_error("Unexpected 'as'.");
  868. return NULL;
  869. }
  870. CastNode *cn = alloc_node<CastNode>();
  871. DataType casttype;
  872. if (!_parse_type(casttype)) {
  873. _set_error("Expected type after 'as'.");
  874. return NULL;
  875. }
  876. has_casting = true;
  877. cn->source_node = expr;
  878. expr = cn;
  879. }
  880. /******************/
  881. /* Parse Operator */
  882. /******************/
  883. if (parenthesis > 0) {
  884. //remove empty space (only allowed if inside parenthesis
  885. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  886. tokenizer->advance();
  887. }
  888. }
  889. Expression e;
  890. e.is_op = false;
  891. e.node = expr;
  892. expression.push_back(e);
  893. // determine which operator is next
  894. OperatorNode::Operator op;
  895. bool valid = true;
  896. //assign, if allowed is only allowed on the first operator
  897. #define _VALIDATE_ASSIGN \
  898. if (!p_allow_assign || has_casting) { \
  899. _set_error("Unexpected assign."); \
  900. return NULL; \
  901. } \
  902. p_allow_assign = false;
  903. switch (tokenizer->get_token()) { //see operator
  904. case GDScriptTokenizer::TK_OP_IN: op = OperatorNode::OP_IN; break;
  905. case GDScriptTokenizer::TK_OP_EQUAL: op = OperatorNode::OP_EQUAL; break;
  906. case GDScriptTokenizer::TK_OP_NOT_EQUAL: op = OperatorNode::OP_NOT_EQUAL; break;
  907. case GDScriptTokenizer::TK_OP_LESS: op = OperatorNode::OP_LESS; break;
  908. case GDScriptTokenizer::TK_OP_LESS_EQUAL: op = OperatorNode::OP_LESS_EQUAL; break;
  909. case GDScriptTokenizer::TK_OP_GREATER: op = OperatorNode::OP_GREATER; break;
  910. case GDScriptTokenizer::TK_OP_GREATER_EQUAL: op = OperatorNode::OP_GREATER_EQUAL; break;
  911. case GDScriptTokenizer::TK_OP_AND: op = OperatorNode::OP_AND; break;
  912. case GDScriptTokenizer::TK_OP_OR: op = OperatorNode::OP_OR; break;
  913. case GDScriptTokenizer::TK_OP_ADD: op = OperatorNode::OP_ADD; break;
  914. case GDScriptTokenizer::TK_OP_SUB: op = OperatorNode::OP_SUB; break;
  915. case GDScriptTokenizer::TK_OP_MUL: op = OperatorNode::OP_MUL; break;
  916. case GDScriptTokenizer::TK_OP_DIV: op = OperatorNode::OP_DIV; break;
  917. case GDScriptTokenizer::TK_OP_MOD:
  918. op = OperatorNode::OP_MOD;
  919. break;
  920. //case GDScriptTokenizer::TK_OP_NEG: op=OperatorNode::OP_NEG ; break;
  921. case GDScriptTokenizer::TK_OP_SHIFT_LEFT: op = OperatorNode::OP_SHIFT_LEFT; break;
  922. case GDScriptTokenizer::TK_OP_SHIFT_RIGHT: op = OperatorNode::OP_SHIFT_RIGHT; break;
  923. case GDScriptTokenizer::TK_OP_ASSIGN: {
  924. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN;
  925. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_CURSOR) {
  926. //code complete assignment
  927. completion_type = COMPLETION_ASSIGN;
  928. completion_node = expr;
  929. completion_class = current_class;
  930. completion_function = current_function;
  931. completion_line = tokenizer->get_token_line();
  932. completion_block = current_block;
  933. completion_found = true;
  934. tokenizer->advance();
  935. }
  936. } break;
  937. case GDScriptTokenizer::TK_OP_ASSIGN_ADD: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_ADD; break;
  938. case GDScriptTokenizer::TK_OP_ASSIGN_SUB: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SUB; break;
  939. case GDScriptTokenizer::TK_OP_ASSIGN_MUL: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_MUL; break;
  940. case GDScriptTokenizer::TK_OP_ASSIGN_DIV: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_DIV; break;
  941. case GDScriptTokenizer::TK_OP_ASSIGN_MOD: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_MOD; break;
  942. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_LEFT: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SHIFT_LEFT; break;
  943. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_RIGHT: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SHIFT_RIGHT; break;
  944. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_AND: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_AND; break;
  945. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_OR: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_OR; break;
  946. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_XOR: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_XOR; break;
  947. case GDScriptTokenizer::TK_OP_BIT_AND: op = OperatorNode::OP_BIT_AND; break;
  948. case GDScriptTokenizer::TK_OP_BIT_OR: op = OperatorNode::OP_BIT_OR; break;
  949. case GDScriptTokenizer::TK_OP_BIT_XOR: op = OperatorNode::OP_BIT_XOR; break;
  950. case GDScriptTokenizer::TK_PR_IS: op = OperatorNode::OP_IS; break;
  951. case GDScriptTokenizer::TK_CF_IF: op = OperatorNode::OP_TERNARY_IF; break;
  952. case GDScriptTokenizer::TK_CF_ELSE: op = OperatorNode::OP_TERNARY_ELSE; break;
  953. default: valid = false; break;
  954. }
  955. if (valid) {
  956. e.is_op = true;
  957. e.op = op;
  958. expression.push_back(e);
  959. tokenizer->advance();
  960. } else {
  961. break;
  962. }
  963. }
  964. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  965. while (expression.size() > 1) {
  966. int next_op = -1;
  967. int min_priority = 0xFFFFF;
  968. bool is_unary = false;
  969. bool is_ternary = false;
  970. for (int i = 0; i < expression.size(); i++) {
  971. if (!expression[i].is_op) {
  972. continue;
  973. }
  974. int priority;
  975. bool unary = false;
  976. bool ternary = false;
  977. bool error = false;
  978. bool right_to_left = false;
  979. switch (expression[i].op) {
  980. case OperatorNode::OP_IS:
  981. priority = -1;
  982. break; //before anything
  983. case OperatorNode::OP_BIT_INVERT:
  984. priority = 0;
  985. unary = true;
  986. break;
  987. case OperatorNode::OP_NEG:
  988. priority = 1;
  989. unary = true;
  990. break;
  991. case OperatorNode::OP_POS:
  992. priority = 1;
  993. unary = true;
  994. break;
  995. case OperatorNode::OP_MUL: priority = 2; break;
  996. case OperatorNode::OP_DIV: priority = 2; break;
  997. case OperatorNode::OP_MOD: priority = 2; break;
  998. case OperatorNode::OP_ADD: priority = 3; break;
  999. case OperatorNode::OP_SUB: priority = 3; break;
  1000. case OperatorNode::OP_SHIFT_LEFT: priority = 4; break;
  1001. case OperatorNode::OP_SHIFT_RIGHT: priority = 4; break;
  1002. case OperatorNode::OP_BIT_AND: priority = 5; break;
  1003. case OperatorNode::OP_BIT_XOR: priority = 6; break;
  1004. case OperatorNode::OP_BIT_OR: priority = 7; break;
  1005. case OperatorNode::OP_LESS: priority = 8; break;
  1006. case OperatorNode::OP_LESS_EQUAL: priority = 8; break;
  1007. case OperatorNode::OP_GREATER: priority = 8; break;
  1008. case OperatorNode::OP_GREATER_EQUAL: priority = 8; break;
  1009. case OperatorNode::OP_EQUAL: priority = 8; break;
  1010. case OperatorNode::OP_NOT_EQUAL: priority = 8; break;
  1011. case OperatorNode::OP_IN: priority = 10; break;
  1012. case OperatorNode::OP_NOT:
  1013. priority = 11;
  1014. unary = true;
  1015. break;
  1016. case OperatorNode::OP_AND: priority = 12; break;
  1017. case OperatorNode::OP_OR: priority = 13; break;
  1018. case OperatorNode::OP_TERNARY_IF:
  1019. priority = 14;
  1020. ternary = true;
  1021. right_to_left = true;
  1022. break;
  1023. case OperatorNode::OP_TERNARY_ELSE:
  1024. priority = 14;
  1025. error = true;
  1026. // Rigth-to-left should be false in this case, otherwise it would always error.
  1027. break;
  1028. case OperatorNode::OP_ASSIGN: priority = 15; break;
  1029. case OperatorNode::OP_ASSIGN_ADD: priority = 15; break;
  1030. case OperatorNode::OP_ASSIGN_SUB: priority = 15; break;
  1031. case OperatorNode::OP_ASSIGN_MUL: priority = 15; break;
  1032. case OperatorNode::OP_ASSIGN_DIV: priority = 15; break;
  1033. case OperatorNode::OP_ASSIGN_MOD: priority = 15; break;
  1034. case OperatorNode::OP_ASSIGN_SHIFT_LEFT: priority = 15; break;
  1035. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT: priority = 15; break;
  1036. case OperatorNode::OP_ASSIGN_BIT_AND: priority = 15; break;
  1037. case OperatorNode::OP_ASSIGN_BIT_OR: priority = 15; break;
  1038. case OperatorNode::OP_ASSIGN_BIT_XOR: priority = 15; break;
  1039. default: {
  1040. _set_error("GDScriptParser bug, invalid operator in expression: " + itos(expression[i].op));
  1041. return NULL;
  1042. }
  1043. }
  1044. if (priority < min_priority || (right_to_left && priority == min_priority)) {
  1045. // < is used for left to right (default)
  1046. // <= is used for right to left
  1047. if (error) {
  1048. _set_error("Unexpected operator");
  1049. return NULL;
  1050. }
  1051. next_op = i;
  1052. min_priority = priority;
  1053. is_unary = unary;
  1054. is_ternary = ternary;
  1055. }
  1056. }
  1057. if (next_op == -1) {
  1058. _set_error("Yet another parser bug....");
  1059. ERR_FAIL_COND_V(next_op == -1, NULL);
  1060. }
  1061. // OK! create operator..
  1062. if (is_unary) {
  1063. int expr_pos = next_op;
  1064. while (expression[expr_pos].is_op) {
  1065. expr_pos++;
  1066. if (expr_pos == expression.size()) {
  1067. //can happen..
  1068. _set_error("Unexpected end of expression...");
  1069. return NULL;
  1070. }
  1071. }
  1072. //consecutively do unary opeators
  1073. for (int i = expr_pos - 1; i >= next_op; i--) {
  1074. OperatorNode *op = alloc_node<OperatorNode>();
  1075. op->op = expression[i].op;
  1076. op->arguments.push_back(expression[i + 1].node);
  1077. op->line = op_line; //line might have been changed from a \n
  1078. expression[i].is_op = false;
  1079. expression[i].node = op;
  1080. expression.remove(i + 1);
  1081. }
  1082. } else if (is_ternary) {
  1083. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  1084. _set_error("Parser bug...");
  1085. ERR_FAIL_V(NULL);
  1086. }
  1087. if (next_op >= (expression.size() - 2) || expression[next_op + 2].op != OperatorNode::OP_TERNARY_ELSE) {
  1088. _set_error("Expected else after ternary if.");
  1089. ERR_FAIL_V(NULL);
  1090. }
  1091. if (next_op >= (expression.size() - 3)) {
  1092. _set_error("Expected value after ternary else.");
  1093. ERR_FAIL_V(NULL);
  1094. }
  1095. OperatorNode *op = alloc_node<OperatorNode>();
  1096. op->op = expression[next_op].op;
  1097. op->line = op_line; //line might have been changed from a \n
  1098. if (expression[next_op - 1].is_op) {
  1099. _set_error("Parser bug...");
  1100. ERR_FAIL_V(NULL);
  1101. }
  1102. if (expression[next_op + 1].is_op) {
  1103. // this is not invalid and can really appear
  1104. // but it becomes invalid anyway because no binary op
  1105. // can be followed by a unary op in a valid combination,
  1106. // due to how precedence works, unaries will always disappear first
  1107. _set_error("Unexpected two consecutive operators after ternary if.");
  1108. return NULL;
  1109. }
  1110. if (expression[next_op + 3].is_op) {
  1111. // this is not invalid and can really appear
  1112. // but it becomes invalid anyway because no binary op
  1113. // can be followed by a unary op in a valid combination,
  1114. // due to how precedence works, unaries will always disappear first
  1115. _set_error("Unexpected two consecutive operators after ternary else.");
  1116. return NULL;
  1117. }
  1118. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as first
  1119. op->arguments.push_back(expression[next_op - 1].node); //left expression goes as when-true
  1120. op->arguments.push_back(expression[next_op + 3].node); //expression after next goes as when-false
  1121. //replace all 3 nodes by this operator and make it an expression
  1122. expression[next_op - 1].node = op;
  1123. expression.remove(next_op);
  1124. expression.remove(next_op);
  1125. expression.remove(next_op);
  1126. expression.remove(next_op);
  1127. } else {
  1128. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  1129. _set_error("Parser bug...");
  1130. ERR_FAIL_V(NULL);
  1131. }
  1132. OperatorNode *op = alloc_node<OperatorNode>();
  1133. op->op = expression[next_op].op;
  1134. op->line = op_line; //line might have been changed from a \n
  1135. if (expression[next_op - 1].is_op) {
  1136. _set_error("Parser bug...");
  1137. ERR_FAIL_V(NULL);
  1138. }
  1139. if (expression[next_op + 1].is_op) {
  1140. // this is not invalid and can really appear
  1141. // but it becomes invalid anyway because no binary op
  1142. // can be followed by a unary op in a valid combination,
  1143. // due to how precedence works, unaries will always disappear first
  1144. _set_error("Unexpected two consecutive operators.");
  1145. return NULL;
  1146. }
  1147. op->arguments.push_back(expression[next_op - 1].node); //expression goes as left
  1148. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as right
  1149. //replace all 3 nodes by this operator and make it an expression
  1150. expression[next_op - 1].node = op;
  1151. expression.remove(next_op);
  1152. expression.remove(next_op);
  1153. }
  1154. }
  1155. return expression[0].node;
  1156. }
  1157. GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to_const) {
  1158. switch (p_node->type) {
  1159. case Node::TYPE_BUILT_IN_FUNCTION: {
  1160. //many may probably be optimizable
  1161. return p_node;
  1162. } break;
  1163. case Node::TYPE_ARRAY: {
  1164. ArrayNode *an = static_cast<ArrayNode *>(p_node);
  1165. bool all_constants = true;
  1166. for (int i = 0; i < an->elements.size(); i++) {
  1167. an->elements[i] = _reduce_expression(an->elements[i], p_to_const);
  1168. if (an->elements[i]->type != Node::TYPE_CONSTANT)
  1169. all_constants = false;
  1170. }
  1171. if (all_constants && p_to_const) {
  1172. //reduce constant array expression
  1173. ConstantNode *cn = alloc_node<ConstantNode>();
  1174. Array arr;
  1175. //print_line("mk array "+itos(!p_to_const));
  1176. arr.resize(an->elements.size());
  1177. for (int i = 0; i < an->elements.size(); i++) {
  1178. ConstantNode *acn = static_cast<ConstantNode *>(an->elements[i]);
  1179. arr[i] = acn->value;
  1180. }
  1181. cn->value = arr;
  1182. return cn;
  1183. }
  1184. return an;
  1185. } break;
  1186. case Node::TYPE_DICTIONARY: {
  1187. DictionaryNode *dn = static_cast<DictionaryNode *>(p_node);
  1188. bool all_constants = true;
  1189. for (int i = 0; i < dn->elements.size(); i++) {
  1190. dn->elements[i].key = _reduce_expression(dn->elements[i].key, p_to_const);
  1191. if (dn->elements[i].key->type != Node::TYPE_CONSTANT)
  1192. all_constants = false;
  1193. dn->elements[i].value = _reduce_expression(dn->elements[i].value, p_to_const);
  1194. if (dn->elements[i].value->type != Node::TYPE_CONSTANT)
  1195. all_constants = false;
  1196. }
  1197. if (all_constants && p_to_const) {
  1198. //reduce constant array expression
  1199. ConstantNode *cn = alloc_node<ConstantNode>();
  1200. Dictionary dict;
  1201. for (int i = 0; i < dn->elements.size(); i++) {
  1202. ConstantNode *key_c = static_cast<ConstantNode *>(dn->elements[i].key);
  1203. ConstantNode *value_c = static_cast<ConstantNode *>(dn->elements[i].value);
  1204. dict[key_c->value] = value_c->value;
  1205. }
  1206. cn->value = dict;
  1207. return cn;
  1208. }
  1209. return dn;
  1210. } break;
  1211. case Node::TYPE_OPERATOR: {
  1212. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  1213. bool all_constants = true;
  1214. int last_not_constant = -1;
  1215. for (int i = 0; i < op->arguments.size(); i++) {
  1216. op->arguments[i] = _reduce_expression(op->arguments[i], p_to_const);
  1217. if (op->arguments[i]->type != Node::TYPE_CONSTANT) {
  1218. all_constants = false;
  1219. last_not_constant = i;
  1220. }
  1221. }
  1222. if (op->op == OperatorNode::OP_IS) {
  1223. //nothing much
  1224. return op;
  1225. }
  1226. if (op->op == OperatorNode::OP_PARENT_CALL) {
  1227. //nothing much
  1228. return op;
  1229. } else if (op->op == OperatorNode::OP_CALL) {
  1230. //can reduce base type constructors
  1231. 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) {
  1232. //native type constructor or intrinsic function
  1233. const Variant **vptr = NULL;
  1234. Vector<Variant *> ptrs;
  1235. if (op->arguments.size() > 1) {
  1236. ptrs.resize(op->arguments.size() - 1);
  1237. for (int i = 0; i < ptrs.size(); i++) {
  1238. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i + 1]);
  1239. ptrs[i] = &cn->value;
  1240. }
  1241. vptr = (const Variant **)&ptrs[0];
  1242. }
  1243. Variant::CallError ce;
  1244. Variant v;
  1245. if (op->arguments[0]->type == Node::TYPE_TYPE) {
  1246. TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
  1247. v = Variant::construct(tn->vtype, vptr, ptrs.size(), ce);
  1248. } else {
  1249. GDScriptFunctions::Function func = static_cast<BuiltInFunctionNode *>(op->arguments[0])->function;
  1250. GDScriptFunctions::call(func, vptr, ptrs.size(), v, ce);
  1251. }
  1252. if (ce.error != Variant::CallError::CALL_OK) {
  1253. String errwhere;
  1254. if (op->arguments[0]->type == Node::TYPE_TYPE) {
  1255. TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
  1256. errwhere = "'" + Variant::get_type_name(tn->vtype) + "' constructor";
  1257. } else {
  1258. GDScriptFunctions::Function func = static_cast<BuiltInFunctionNode *>(op->arguments[0])->function;
  1259. errwhere = String("'") + GDScriptFunctions::get_func_name(func) + "' intrinsic function";
  1260. }
  1261. switch (ce.error) {
  1262. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1263. _set_error("Invalid argument (#" + itos(ce.argument + 1) + ") for " + errwhere + ".");
  1264. } break;
  1265. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1266. _set_error("Too many arguments for " + errwhere + ".");
  1267. } break;
  1268. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  1269. _set_error("Too few arguments for " + errwhere + ".");
  1270. } break;
  1271. default: {
  1272. _set_error("Invalid arguments for " + errwhere + ".");
  1273. } break;
  1274. }
  1275. error_line = op->line;
  1276. return p_node;
  1277. }
  1278. ConstantNode *cn = alloc_node<ConstantNode>();
  1279. cn->value = v;
  1280. return cn;
  1281. } else if (op->arguments[0]->type == Node::TYPE_BUILT_IN_FUNCTION && last_not_constant == 0) {
  1282. }
  1283. return op; //don't reduce yet
  1284. } else if (op->op == OperatorNode::OP_YIELD) {
  1285. return op;
  1286. } else if (op->op == OperatorNode::OP_INDEX) {
  1287. //can reduce indices into constant arrays or dictionaries
  1288. if (all_constants) {
  1289. ConstantNode *ca = static_cast<ConstantNode *>(op->arguments[0]);
  1290. ConstantNode *cb = static_cast<ConstantNode *>(op->arguments[1]);
  1291. bool valid;
  1292. Variant v = ca->value.get(cb->value, &valid);
  1293. if (!valid) {
  1294. _set_error("invalid index in constant expression");
  1295. error_line = op->line;
  1296. return op;
  1297. }
  1298. ConstantNode *cn = alloc_node<ConstantNode>();
  1299. cn->value = v;
  1300. return cn;
  1301. } /*else if (op->arguments[0]->type==Node::TYPE_CONSTANT && op->arguments[1]->type==Node::TYPE_IDENTIFIER) {
  1302. ConstantNode *ca = static_cast<ConstantNode*>(op->arguments[0]);
  1303. IdentifierNode *ib = static_cast<IdentifierNode*>(op->arguments[1]);
  1304. bool valid;
  1305. Variant v = ca->value.get_named(ib->name,&valid);
  1306. if (!valid) {
  1307. _set_error("invalid index '"+String(ib->name)+"' in constant expression");
  1308. return op;
  1309. }
  1310. ConstantNode *cn = alloc_node<ConstantNode>();
  1311. cn->value=v;
  1312. return cn;
  1313. }*/
  1314. return op;
  1315. } else if (op->op == OperatorNode::OP_INDEX_NAMED) {
  1316. if (op->arguments[0]->type == Node::TYPE_CONSTANT && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  1317. ConstantNode *ca = static_cast<ConstantNode *>(op->arguments[0]);
  1318. IdentifierNode *ib = static_cast<IdentifierNode *>(op->arguments[1]);
  1319. bool valid;
  1320. Variant v = ca->value.get_named(ib->name, &valid);
  1321. if (!valid) {
  1322. _set_error("invalid index '" + String(ib->name) + "' in constant expression");
  1323. error_line = op->line;
  1324. return op;
  1325. }
  1326. ConstantNode *cn = alloc_node<ConstantNode>();
  1327. cn->value = v;
  1328. return cn;
  1329. }
  1330. return op;
  1331. }
  1332. //validate assignment (don't assign to cosntant expression
  1333. switch (op->op) {
  1334. case OperatorNode::OP_ASSIGN:
  1335. case OperatorNode::OP_ASSIGN_ADD:
  1336. case OperatorNode::OP_ASSIGN_SUB:
  1337. case OperatorNode::OP_ASSIGN_MUL:
  1338. case OperatorNode::OP_ASSIGN_DIV:
  1339. case OperatorNode::OP_ASSIGN_MOD:
  1340. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  1341. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  1342. case OperatorNode::OP_ASSIGN_BIT_AND:
  1343. case OperatorNode::OP_ASSIGN_BIT_OR:
  1344. case OperatorNode::OP_ASSIGN_BIT_XOR: {
  1345. if (op->arguments[0]->type == Node::TYPE_CONSTANT) {
  1346. _set_error("Can't assign to constant", tokenizer->get_token_line() - 1);
  1347. error_line = op->line;
  1348. return op;
  1349. }
  1350. if (op->arguments[0]->type == Node::TYPE_OPERATOR) {
  1351. OperatorNode *on = static_cast<OperatorNode *>(op->arguments[0]);
  1352. if (on->op != OperatorNode::OP_INDEX && on->op != OperatorNode::OP_INDEX_NAMED) {
  1353. _set_error("Can't assign to an expression", tokenizer->get_token_line() - 1);
  1354. error_line = op->line;
  1355. return op;
  1356. }
  1357. }
  1358. } break;
  1359. default: { break; }
  1360. }
  1361. //now se if all are constants
  1362. if (!all_constants)
  1363. return op; //nothing to reduce from here on
  1364. #define _REDUCE_UNARY(m_vop) \
  1365. bool valid = false; \
  1366. Variant res; \
  1367. Variant::evaluate(m_vop, static_cast<ConstantNode *>(op->arguments[0])->value, Variant(), res, valid); \
  1368. if (!valid) { \
  1369. _set_error("Invalid operand for unary operator"); \
  1370. error_line = op->line; \
  1371. return p_node; \
  1372. } \
  1373. ConstantNode *cn = alloc_node<ConstantNode>(); \
  1374. cn->value = res; \
  1375. return cn;
  1376. #define _REDUCE_BINARY(m_vop) \
  1377. bool valid = false; \
  1378. Variant res; \
  1379. Variant::evaluate(m_vop, static_cast<ConstantNode *>(op->arguments[0])->value, static_cast<ConstantNode *>(op->arguments[1])->value, res, valid); \
  1380. if (!valid) { \
  1381. _set_error("Invalid operands for operator"); \
  1382. error_line = op->line; \
  1383. return p_node; \
  1384. } \
  1385. ConstantNode *cn = alloc_node<ConstantNode>(); \
  1386. cn->value = res; \
  1387. return cn;
  1388. switch (op->op) {
  1389. //unary operators
  1390. case OperatorNode::OP_NEG: {
  1391. _REDUCE_UNARY(Variant::OP_NEGATE);
  1392. } break;
  1393. case OperatorNode::OP_POS: {
  1394. _REDUCE_UNARY(Variant::OP_POSITIVE);
  1395. } break;
  1396. case OperatorNode::OP_NOT: {
  1397. _REDUCE_UNARY(Variant::OP_NOT);
  1398. } break;
  1399. case OperatorNode::OP_BIT_INVERT: {
  1400. _REDUCE_UNARY(Variant::OP_BIT_NEGATE);
  1401. } break;
  1402. //binary operators (in precedence order)
  1403. case OperatorNode::OP_IN: {
  1404. _REDUCE_BINARY(Variant::OP_IN);
  1405. } break;
  1406. case OperatorNode::OP_EQUAL: {
  1407. _REDUCE_BINARY(Variant::OP_EQUAL);
  1408. } break;
  1409. case OperatorNode::OP_NOT_EQUAL: {
  1410. _REDUCE_BINARY(Variant::OP_NOT_EQUAL);
  1411. } break;
  1412. case OperatorNode::OP_LESS: {
  1413. _REDUCE_BINARY(Variant::OP_LESS);
  1414. } break;
  1415. case OperatorNode::OP_LESS_EQUAL: {
  1416. _REDUCE_BINARY(Variant::OP_LESS_EQUAL);
  1417. } break;
  1418. case OperatorNode::OP_GREATER: {
  1419. _REDUCE_BINARY(Variant::OP_GREATER);
  1420. } break;
  1421. case OperatorNode::OP_GREATER_EQUAL: {
  1422. _REDUCE_BINARY(Variant::OP_GREATER_EQUAL);
  1423. } break;
  1424. case OperatorNode::OP_AND: {
  1425. _REDUCE_BINARY(Variant::OP_AND);
  1426. } break;
  1427. case OperatorNode::OP_OR: {
  1428. _REDUCE_BINARY(Variant::OP_OR);
  1429. } break;
  1430. case OperatorNode::OP_ADD: {
  1431. _REDUCE_BINARY(Variant::OP_ADD);
  1432. } break;
  1433. case OperatorNode::OP_SUB: {
  1434. _REDUCE_BINARY(Variant::OP_SUBTRACT);
  1435. } break;
  1436. case OperatorNode::OP_MUL: {
  1437. _REDUCE_BINARY(Variant::OP_MULTIPLY);
  1438. } break;
  1439. case OperatorNode::OP_DIV: {
  1440. _REDUCE_BINARY(Variant::OP_DIVIDE);
  1441. } break;
  1442. case OperatorNode::OP_MOD: {
  1443. _REDUCE_BINARY(Variant::OP_MODULE);
  1444. } break;
  1445. case OperatorNode::OP_SHIFT_LEFT: {
  1446. _REDUCE_BINARY(Variant::OP_SHIFT_LEFT);
  1447. } break;
  1448. case OperatorNode::OP_SHIFT_RIGHT: {
  1449. _REDUCE_BINARY(Variant::OP_SHIFT_RIGHT);
  1450. } break;
  1451. case OperatorNode::OP_BIT_AND: {
  1452. _REDUCE_BINARY(Variant::OP_BIT_AND);
  1453. } break;
  1454. case OperatorNode::OP_BIT_OR: {
  1455. _REDUCE_BINARY(Variant::OP_BIT_OR);
  1456. } break;
  1457. case OperatorNode::OP_BIT_XOR: {
  1458. _REDUCE_BINARY(Variant::OP_BIT_XOR);
  1459. } break;
  1460. default: { ERR_FAIL_V(op); }
  1461. }
  1462. ERR_FAIL_V(op);
  1463. } break;
  1464. default: {
  1465. return p_node;
  1466. } break;
  1467. }
  1468. }
  1469. GDScriptParser::Node *GDScriptParser::_parse_and_reduce_expression(Node *p_parent, bool p_static, bool p_reduce_const, bool p_allow_assign) {
  1470. Node *expr = _parse_expression(p_parent, p_static, p_allow_assign, p_reduce_const);
  1471. if (!expr || error_set)
  1472. return NULL;
  1473. expr = _reduce_expression(expr, p_reduce_const);
  1474. if (!expr || error_set)
  1475. return NULL;
  1476. return expr;
  1477. }
  1478. bool GDScriptParser::_recover_from_completion() {
  1479. if (!completion_found) {
  1480. return false; //can't recover if no completion
  1481. }
  1482. //skip stuff until newline
  1483. while (tokenizer->get_token() != GDScriptTokenizer::TK_NEWLINE && tokenizer->get_token() != GDScriptTokenizer::TK_EOF && tokenizer->get_token() != GDScriptTokenizer::TK_ERROR) {
  1484. tokenizer->advance();
  1485. }
  1486. completion_found = false;
  1487. error_set = false;
  1488. if (tokenizer->get_token() == GDScriptTokenizer::TK_ERROR) {
  1489. error_set = true;
  1490. }
  1491. return true;
  1492. }
  1493. GDScriptParser::PatternNode *GDScriptParser::_parse_pattern(bool p_static) {
  1494. PatternNode *pattern = alloc_node<PatternNode>();
  1495. GDScriptTokenizer::Token token = tokenizer->get_token();
  1496. if (error_set)
  1497. return NULL;
  1498. if (token == GDScriptTokenizer::TK_EOF) {
  1499. return NULL;
  1500. }
  1501. switch (token) {
  1502. // array
  1503. case GDScriptTokenizer::TK_BRACKET_OPEN: {
  1504. tokenizer->advance();
  1505. pattern->pt_type = GDScriptParser::PatternNode::PT_ARRAY;
  1506. while (true) {
  1507. if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1508. tokenizer->advance();
  1509. break;
  1510. }
  1511. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) {
  1512. // match everything
  1513. tokenizer->advance(2);
  1514. PatternNode *sub_pattern = alloc_node<PatternNode>();
  1515. sub_pattern->pt_type = GDScriptParser::PatternNode::PT_IGNORE_REST;
  1516. pattern->array.push_back(sub_pattern);
  1517. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA && tokenizer->get_token(1) == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1518. tokenizer->advance(2);
  1519. break;
  1520. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1521. tokenizer->advance(1);
  1522. break;
  1523. } else {
  1524. _set_error("'..' pattern only allowed at the end of an array pattern");
  1525. return NULL;
  1526. }
  1527. }
  1528. PatternNode *sub_pattern = _parse_pattern(p_static);
  1529. if (!sub_pattern) {
  1530. return NULL;
  1531. }
  1532. pattern->array.push_back(sub_pattern);
  1533. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  1534. tokenizer->advance();
  1535. continue;
  1536. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1537. tokenizer->advance();
  1538. break;
  1539. } else {
  1540. _set_error("Not a valid pattern");
  1541. return NULL;
  1542. }
  1543. }
  1544. } break;
  1545. // bind
  1546. case GDScriptTokenizer::TK_PR_VAR: {
  1547. tokenizer->advance();
  1548. pattern->pt_type = GDScriptParser::PatternNode::PT_BIND;
  1549. pattern->bind = tokenizer->get_token_identifier();
  1550. tokenizer->advance();
  1551. } break;
  1552. // dictionary
  1553. case GDScriptTokenizer::TK_CURLY_BRACKET_OPEN: {
  1554. tokenizer->advance();
  1555. pattern->pt_type = GDScriptParser::PatternNode::PT_DICTIONARY;
  1556. while (true) {
  1557. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1558. tokenizer->advance();
  1559. break;
  1560. }
  1561. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) {
  1562. // match everything
  1563. tokenizer->advance(2);
  1564. PatternNode *sub_pattern = alloc_node<PatternNode>();
  1565. sub_pattern->pt_type = PatternNode::PT_IGNORE_REST;
  1566. pattern->array.push_back(sub_pattern);
  1567. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA && tokenizer->get_token(1) == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1568. tokenizer->advance(2);
  1569. break;
  1570. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1571. tokenizer->advance(1);
  1572. break;
  1573. } else {
  1574. _set_error("'..' pattern only allowed at the end of a dictionary pattern");
  1575. return NULL;
  1576. }
  1577. }
  1578. Node *key = _parse_and_reduce_expression(pattern, p_static);
  1579. if (!key) {
  1580. _set_error("Not a valid key in pattern");
  1581. return NULL;
  1582. }
  1583. if (key->type != GDScriptParser::Node::TYPE_CONSTANT) {
  1584. _set_error("Not a constant expression as key");
  1585. return NULL;
  1586. }
  1587. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  1588. tokenizer->advance();
  1589. PatternNode *value = _parse_pattern(p_static);
  1590. if (!value) {
  1591. _set_error("Expected pattern in dictionary value");
  1592. return NULL;
  1593. }
  1594. pattern->dictionary.insert(static_cast<ConstantNode *>(key), value);
  1595. } else {
  1596. pattern->dictionary.insert(static_cast<ConstantNode *>(key), NULL);
  1597. }
  1598. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  1599. tokenizer->advance();
  1600. continue;
  1601. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1602. tokenizer->advance();
  1603. break;
  1604. } else {
  1605. _set_error("Not a valid pattern");
  1606. return NULL;
  1607. }
  1608. }
  1609. } break;
  1610. case GDScriptTokenizer::TK_WILDCARD: {
  1611. tokenizer->advance();
  1612. pattern->pt_type = PatternNode::PT_WILDCARD;
  1613. } break;
  1614. // all the constants like strings and numbers
  1615. default: {
  1616. Node *value = _parse_and_reduce_expression(pattern, p_static);
  1617. if (!value) {
  1618. _set_error("Expect constant expression or variables in a pattern");
  1619. return NULL;
  1620. }
  1621. if (value->type == Node::TYPE_OPERATOR) {
  1622. // Maybe it's SomeEnum.VALUE
  1623. Node *current_value = value;
  1624. while (current_value->type == Node::TYPE_OPERATOR) {
  1625. OperatorNode *op_node = static_cast<OperatorNode *>(current_value);
  1626. if (op_node->op != OperatorNode::OP_INDEX_NAMED) {
  1627. _set_error("Invalid operator in pattern. Only index (`A.B`) is allowed");
  1628. return NULL;
  1629. }
  1630. current_value = op_node->arguments[0];
  1631. }
  1632. if (current_value->type != Node::TYPE_IDENTIFIER) {
  1633. _set_error("Only constant expression or variables allowed in a pattern");
  1634. return NULL;
  1635. }
  1636. } else if (value->type != Node::TYPE_IDENTIFIER && value->type != Node::TYPE_CONSTANT) {
  1637. _set_error("Only constant expressions or variables allowed in a pattern");
  1638. return NULL;
  1639. }
  1640. pattern->pt_type = PatternNode::PT_CONSTANT;
  1641. pattern->constant = value;
  1642. } break;
  1643. }
  1644. return pattern;
  1645. }
  1646. void GDScriptParser::_parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static) {
  1647. int indent_level = tab_level.back()->get();
  1648. while (true) {
  1649. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline())
  1650. ;
  1651. // GDScriptTokenizer::Token token = tokenizer->get_token();
  1652. if (error_set)
  1653. return;
  1654. if (indent_level > tab_level.back()->get()) {
  1655. return; // go back a level
  1656. }
  1657. if (pending_newline != -1) {
  1658. pending_newline = -1;
  1659. }
  1660. PatternBranchNode *branch = alloc_node<PatternBranchNode>();
  1661. branch->patterns.push_back(_parse_pattern(p_static));
  1662. if (!branch->patterns[0]) {
  1663. return;
  1664. }
  1665. while (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  1666. tokenizer->advance();
  1667. branch->patterns.push_back(_parse_pattern(p_static));
  1668. if (!branch->patterns[branch->patterns.size() - 1]) {
  1669. return;
  1670. }
  1671. }
  1672. if (!_enter_indent_block()) {
  1673. _set_error("Expected block in pattern branch");
  1674. return;
  1675. }
  1676. branch->body = alloc_node<BlockNode>();
  1677. branch->body->parent_block = p_block;
  1678. p_block->sub_blocks.push_back(branch->body);
  1679. current_block = branch->body;
  1680. _parse_block(branch->body, p_static);
  1681. current_block = p_block;
  1682. p_branches.push_back(branch);
  1683. }
  1684. }
  1685. void GDScriptParser::_generate_pattern(PatternNode *p_pattern, Node *p_node_to_match, Node *&p_resulting_node, Map<StringName, Node *> &p_bindings) {
  1686. switch (p_pattern->pt_type) {
  1687. case PatternNode::PT_CONSTANT: {
  1688. // typecheck
  1689. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1690. typeof_node->function = GDScriptFunctions::TYPE_OF;
  1691. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1692. typeof_match_value->op = OperatorNode::OP_CALL;
  1693. typeof_match_value->arguments.push_back(typeof_node);
  1694. typeof_match_value->arguments.push_back(p_node_to_match);
  1695. OperatorNode *typeof_pattern_value = alloc_node<OperatorNode>();
  1696. typeof_pattern_value->op = OperatorNode::OP_CALL;
  1697. typeof_pattern_value->arguments.push_back(typeof_node);
  1698. typeof_pattern_value->arguments.push_back(p_pattern->constant);
  1699. OperatorNode *type_comp = alloc_node<OperatorNode>();
  1700. type_comp->op = OperatorNode::OP_EQUAL;
  1701. type_comp->arguments.push_back(typeof_match_value);
  1702. type_comp->arguments.push_back(typeof_pattern_value);
  1703. // comare the actual values
  1704. OperatorNode *value_comp = alloc_node<OperatorNode>();
  1705. value_comp->op = OperatorNode::OP_EQUAL;
  1706. value_comp->arguments.push_back(p_pattern->constant);
  1707. value_comp->arguments.push_back(p_node_to_match);
  1708. OperatorNode *comparison = alloc_node<OperatorNode>();
  1709. comparison->op = OperatorNode::OP_AND;
  1710. comparison->arguments.push_back(type_comp);
  1711. comparison->arguments.push_back(value_comp);
  1712. p_resulting_node = comparison;
  1713. } break;
  1714. case PatternNode::PT_BIND: {
  1715. p_bindings[p_pattern->bind] = p_node_to_match;
  1716. // a bind always matches
  1717. ConstantNode *true_value = alloc_node<ConstantNode>();
  1718. true_value->value = Variant(true);
  1719. p_resulting_node = true_value;
  1720. } break;
  1721. case PatternNode::PT_ARRAY: {
  1722. bool open_ended = false;
  1723. if (p_pattern->array.size() > 0) {
  1724. if (p_pattern->array[p_pattern->array.size() - 1]->pt_type == PatternNode::PT_IGNORE_REST) {
  1725. open_ended = true;
  1726. }
  1727. }
  1728. // typeof(value_to_match) == TYPE_ARRAY && value_to_match.size() >= length
  1729. // typeof(value_to_match) == TYPE_ARRAY && value_to_match.size() == length
  1730. {
  1731. // typecheck
  1732. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1733. typeof_node->function = GDScriptFunctions::TYPE_OF;
  1734. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1735. typeof_match_value->op = OperatorNode::OP_CALL;
  1736. typeof_match_value->arguments.push_back(typeof_node);
  1737. typeof_match_value->arguments.push_back(p_node_to_match);
  1738. IdentifierNode *typeof_array = alloc_node<IdentifierNode>();
  1739. typeof_array->name = "TYPE_ARRAY";
  1740. OperatorNode *type_comp = alloc_node<OperatorNode>();
  1741. type_comp->op = OperatorNode::OP_EQUAL;
  1742. type_comp->arguments.push_back(typeof_match_value);
  1743. type_comp->arguments.push_back(typeof_array);
  1744. // size
  1745. ConstantNode *length = alloc_node<ConstantNode>();
  1746. length->value = Variant(open_ended ? p_pattern->array.size() - 1 : p_pattern->array.size());
  1747. OperatorNode *call = alloc_node<OperatorNode>();
  1748. call->op = OperatorNode::OP_CALL;
  1749. call->arguments.push_back(p_node_to_match);
  1750. IdentifierNode *size = alloc_node<IdentifierNode>();
  1751. size->name = "size";
  1752. call->arguments.push_back(size);
  1753. OperatorNode *length_comparison = alloc_node<OperatorNode>();
  1754. length_comparison->op = open_ended ? OperatorNode::OP_GREATER_EQUAL : OperatorNode::OP_EQUAL;
  1755. length_comparison->arguments.push_back(call);
  1756. length_comparison->arguments.push_back(length);
  1757. OperatorNode *type_and_length_comparison = alloc_node<OperatorNode>();
  1758. type_and_length_comparison->op = OperatorNode::OP_AND;
  1759. type_and_length_comparison->arguments.push_back(type_comp);
  1760. type_and_length_comparison->arguments.push_back(length_comparison);
  1761. p_resulting_node = type_and_length_comparison;
  1762. }
  1763. for (int i = 0; i < p_pattern->array.size(); i++) {
  1764. PatternNode *pattern = p_pattern->array[i];
  1765. Node *condition = NULL;
  1766. ConstantNode *index = alloc_node<ConstantNode>();
  1767. index->value = Variant(i);
  1768. OperatorNode *indexed_value = alloc_node<OperatorNode>();
  1769. indexed_value->op = OperatorNode::OP_INDEX;
  1770. indexed_value->arguments.push_back(p_node_to_match);
  1771. indexed_value->arguments.push_back(index);
  1772. _generate_pattern(pattern, indexed_value, condition, p_bindings);
  1773. // concatenate all the patterns with &&
  1774. OperatorNode *and_node = alloc_node<OperatorNode>();
  1775. and_node->op = OperatorNode::OP_AND;
  1776. and_node->arguments.push_back(p_resulting_node);
  1777. and_node->arguments.push_back(condition);
  1778. p_resulting_node = and_node;
  1779. }
  1780. } break;
  1781. case PatternNode::PT_DICTIONARY: {
  1782. bool open_ended = false;
  1783. if (p_pattern->array.size() > 0) {
  1784. open_ended = true;
  1785. }
  1786. // typeof(value_to_match) == TYPE_DICTIONARY && value_to_match.size() >= length
  1787. // typeof(value_to_match) == TYPE_DICTIONARY && value_to_match.size() == length
  1788. {
  1789. // typecheck
  1790. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1791. typeof_node->function = GDScriptFunctions::TYPE_OF;
  1792. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1793. typeof_match_value->op = OperatorNode::OP_CALL;
  1794. typeof_match_value->arguments.push_back(typeof_node);
  1795. typeof_match_value->arguments.push_back(p_node_to_match);
  1796. IdentifierNode *typeof_dictionary = alloc_node<IdentifierNode>();
  1797. typeof_dictionary->name = "TYPE_DICTIONARY";
  1798. OperatorNode *type_comp = alloc_node<OperatorNode>();
  1799. type_comp->op = OperatorNode::OP_EQUAL;
  1800. type_comp->arguments.push_back(typeof_match_value);
  1801. type_comp->arguments.push_back(typeof_dictionary);
  1802. // size
  1803. ConstantNode *length = alloc_node<ConstantNode>();
  1804. length->value = Variant(open_ended ? p_pattern->dictionary.size() - 1 : p_pattern->dictionary.size());
  1805. OperatorNode *call = alloc_node<OperatorNode>();
  1806. call->op = OperatorNode::OP_CALL;
  1807. call->arguments.push_back(p_node_to_match);
  1808. IdentifierNode *size = alloc_node<IdentifierNode>();
  1809. size->name = "size";
  1810. call->arguments.push_back(size);
  1811. OperatorNode *length_comparison = alloc_node<OperatorNode>();
  1812. length_comparison->op = open_ended ? OperatorNode::OP_GREATER_EQUAL : OperatorNode::OP_EQUAL;
  1813. length_comparison->arguments.push_back(call);
  1814. length_comparison->arguments.push_back(length);
  1815. OperatorNode *type_and_length_comparison = alloc_node<OperatorNode>();
  1816. type_and_length_comparison->op = OperatorNode::OP_AND;
  1817. type_and_length_comparison->arguments.push_back(type_comp);
  1818. type_and_length_comparison->arguments.push_back(length_comparison);
  1819. p_resulting_node = type_and_length_comparison;
  1820. }
  1821. for (Map<ConstantNode *, PatternNode *>::Element *e = p_pattern->dictionary.front(); e; e = e->next()) {
  1822. Node *condition = NULL;
  1823. // chech for has, then for pattern
  1824. IdentifierNode *has = alloc_node<IdentifierNode>();
  1825. has->name = "has";
  1826. OperatorNode *has_call = alloc_node<OperatorNode>();
  1827. has_call->op = OperatorNode::OP_CALL;
  1828. has_call->arguments.push_back(p_node_to_match);
  1829. has_call->arguments.push_back(has);
  1830. has_call->arguments.push_back(e->key());
  1831. if (e->value()) {
  1832. OperatorNode *indexed_value = alloc_node<OperatorNode>();
  1833. indexed_value->op = OperatorNode::OP_INDEX;
  1834. indexed_value->arguments.push_back(p_node_to_match);
  1835. indexed_value->arguments.push_back(e->key());
  1836. _generate_pattern(e->value(), indexed_value, condition, p_bindings);
  1837. OperatorNode *has_and_pattern = alloc_node<OperatorNode>();
  1838. has_and_pattern->op = OperatorNode::OP_AND;
  1839. has_and_pattern->arguments.push_back(has_call);
  1840. has_and_pattern->arguments.push_back(condition);
  1841. condition = has_and_pattern;
  1842. } else {
  1843. condition = has_call;
  1844. }
  1845. // concatenate all the patterns with &&
  1846. OperatorNode *and_node = alloc_node<OperatorNode>();
  1847. and_node->op = OperatorNode::OP_AND;
  1848. and_node->arguments.push_back(p_resulting_node);
  1849. and_node->arguments.push_back(condition);
  1850. p_resulting_node = and_node;
  1851. }
  1852. } break;
  1853. case PatternNode::PT_IGNORE_REST:
  1854. case PatternNode::PT_WILDCARD: {
  1855. // simply generate a `true`
  1856. ConstantNode *true_value = alloc_node<ConstantNode>();
  1857. true_value->value = Variant(true);
  1858. p_resulting_node = true_value;
  1859. } break;
  1860. default: {
  1861. } break;
  1862. }
  1863. }
  1864. void GDScriptParser::_transform_match_statment(BlockNode *p_block, MatchNode *p_match_statement) {
  1865. IdentifierNode *id = alloc_node<IdentifierNode>();
  1866. id->name = "#match_value";
  1867. for (int i = 0; i < p_match_statement->branches.size(); i++) {
  1868. PatternBranchNode *branch = p_match_statement->branches[i];
  1869. MatchNode::CompiledPatternBranch compiled_branch;
  1870. compiled_branch.compiled_pattern = NULL;
  1871. Map<StringName, Node *> binding;
  1872. for (int j = 0; j < branch->patterns.size(); j++) {
  1873. PatternNode *pattern = branch->patterns[j];
  1874. Map<StringName, Node *> bindings;
  1875. Node *resulting_node;
  1876. _generate_pattern(pattern, id, resulting_node, bindings);
  1877. if (!binding.empty() && !bindings.empty()) {
  1878. _set_error("Multipatterns can't contain bindings");
  1879. return;
  1880. } else {
  1881. binding = bindings;
  1882. }
  1883. if (compiled_branch.compiled_pattern) {
  1884. OperatorNode *or_node = alloc_node<OperatorNode>();
  1885. or_node->op = OperatorNode::OP_OR;
  1886. or_node->arguments.push_back(compiled_branch.compiled_pattern);
  1887. or_node->arguments.push_back(resulting_node);
  1888. compiled_branch.compiled_pattern = or_node;
  1889. } else {
  1890. // single pattern | first one
  1891. compiled_branch.compiled_pattern = resulting_node;
  1892. }
  1893. }
  1894. // prepare the body ...hehe
  1895. for (Map<StringName, Node *>::Element *e = binding.front(); e; e = e->next()) {
  1896. LocalVarNode *local_var = alloc_node<LocalVarNode>();
  1897. local_var->name = e->key();
  1898. local_var->assign = e->value();
  1899. IdentifierNode *id = alloc_node<IdentifierNode>();
  1900. id->name = local_var->name;
  1901. OperatorNode *op = alloc_node<OperatorNode>();
  1902. op->op = OperatorNode::OP_ASSIGN;
  1903. op->arguments.push_back(id);
  1904. op->arguments.push_back(local_var->assign);
  1905. branch->body->statements.push_front(op);
  1906. branch->body->statements.push_front(local_var);
  1907. }
  1908. compiled_branch.body = branch->body;
  1909. p_match_statement->compiled_pattern_branches.push_back(compiled_branch);
  1910. }
  1911. }
  1912. void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
  1913. int indent_level = tab_level.back()->get();
  1914. #ifdef DEBUG_ENABLED
  1915. NewLineNode *nl = alloc_node<NewLineNode>();
  1916. nl->line = tokenizer->get_token_line();
  1917. p_block->statements.push_back(nl);
  1918. #endif
  1919. bool is_first_line = true;
  1920. while (true) {
  1921. if (!is_first_line && tab_level.back()->prev() && tab_level.back()->prev()->get() == indent_level) {
  1922. // pythonic single-line expression, don't parse future lines
  1923. tab_level.pop_back();
  1924. p_block->end_line = tokenizer->get_token_line();
  1925. return;
  1926. }
  1927. is_first_line = false;
  1928. GDScriptTokenizer::Token token = tokenizer->get_token();
  1929. if (error_set)
  1930. return;
  1931. if (indent_level > tab_level.back()->get()) {
  1932. p_block->end_line = tokenizer->get_token_line();
  1933. return; //go back a level
  1934. }
  1935. if (pending_newline != -1) {
  1936. NewLineNode *nl = alloc_node<NewLineNode>();
  1937. nl->line = pending_newline;
  1938. p_block->statements.push_back(nl);
  1939. pending_newline = -1;
  1940. }
  1941. switch (token) {
  1942. case GDScriptTokenizer::TK_EOF:
  1943. p_block->end_line = tokenizer->get_token_line();
  1944. case GDScriptTokenizer::TK_ERROR: {
  1945. return; //go back
  1946. //end of file!
  1947. } break;
  1948. case GDScriptTokenizer::TK_NEWLINE: {
  1949. if (!_parse_newline()) {
  1950. if (!error_set) {
  1951. p_block->end_line = tokenizer->get_token_line();
  1952. pending_newline = p_block->end_line;
  1953. }
  1954. return;
  1955. }
  1956. NewLineNode *nl = alloc_node<NewLineNode>();
  1957. nl->line = tokenizer->get_token_line();
  1958. p_block->statements.push_back(nl);
  1959. } break;
  1960. case GDScriptTokenizer::TK_CF_PASS: {
  1961. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_SEMICOLON && tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE && tokenizer->get_token(1) != GDScriptTokenizer::TK_EOF) {
  1962. _set_error("Expected ';' or <NewLine>.");
  1963. return;
  1964. }
  1965. tokenizer->advance();
  1966. if (tokenizer->get_token() == GDScriptTokenizer::TK_SEMICOLON) {
  1967. // Ignore semicolon after 'pass'
  1968. tokenizer->advance();
  1969. }
  1970. } break;
  1971. case GDScriptTokenizer::TK_PR_VAR: {
  1972. //variale declaration and (eventual) initialization
  1973. tokenizer->advance();
  1974. if (!tokenizer->is_token_literal(0, true)) {
  1975. _set_error("Expected identifier for local variable name.");
  1976. return;
  1977. }
  1978. StringName n = tokenizer->get_token_literal();
  1979. tokenizer->advance();
  1980. if (current_function) {
  1981. for (int i = 0; i < current_function->arguments.size(); i++) {
  1982. if (n == current_function->arguments[i]) {
  1983. _set_error("Variable '" + String(n) + "' already defined in the scope (at line: " + itos(current_function->line) + ").");
  1984. return;
  1985. }
  1986. }
  1987. }
  1988. BlockNode *check_block = p_block;
  1989. while (check_block) {
  1990. for (int i = 0; i < check_block->variables.size(); i++) {
  1991. if (n == check_block->variables[i]) {
  1992. _set_error("Variable '" + String(n) + "' already defined in the scope (at line: " + itos(check_block->variable_lines[i]) + ").");
  1993. return;
  1994. }
  1995. }
  1996. check_block = check_block->parent_block;
  1997. }
  1998. int var_line = tokenizer->get_token_line();
  1999. //must know when the local variable is declared
  2000. LocalVarNode *lv = alloc_node<LocalVarNode>();
  2001. lv->name = n;
  2002. p_block->statements.push_back(lv);
  2003. Node *assigned = NULL;
  2004. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  2005. DataType vartype;
  2006. if (!_parse_type(vartype)) {
  2007. _set_error("Expected type for variable.");
  2008. return;
  2009. }
  2010. }
  2011. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  2012. tokenizer->advance();
  2013. Node *subexpr = _parse_and_reduce_expression(p_block, p_static);
  2014. if (!subexpr) {
  2015. if (_recover_from_completion()) {
  2016. break;
  2017. }
  2018. return;
  2019. }
  2020. lv->assign = subexpr;
  2021. assigned = subexpr;
  2022. } else {
  2023. ConstantNode *c = alloc_node<ConstantNode>();
  2024. c->value = Variant();
  2025. assigned = c;
  2026. }
  2027. //must be added later, to avoid self-referencing.
  2028. p_block->variables.push_back(n); //line?
  2029. p_block->variable_lines.push_back(var_line);
  2030. IdentifierNode *id = alloc_node<IdentifierNode>();
  2031. id->name = n;
  2032. OperatorNode *op = alloc_node<OperatorNode>();
  2033. op->op = OperatorNode::OP_ASSIGN;
  2034. op->arguments.push_back(id);
  2035. op->arguments.push_back(assigned);
  2036. p_block->statements.push_back(op);
  2037. if (!_end_statement()) {
  2038. _set_error("Expected end of statement (var)");
  2039. return;
  2040. }
  2041. } break;
  2042. case GDScriptTokenizer::TK_CF_IF: {
  2043. tokenizer->advance();
  2044. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2045. if (!condition) {
  2046. if (_recover_from_completion()) {
  2047. break;
  2048. }
  2049. return;
  2050. }
  2051. ControlFlowNode *cf_if = alloc_node<ControlFlowNode>();
  2052. cf_if->cf_type = ControlFlowNode::CF_IF;
  2053. cf_if->arguments.push_back(condition);
  2054. cf_if->body = alloc_node<BlockNode>();
  2055. cf_if->body->parent_block = p_block;
  2056. cf_if->body->if_condition = condition; //helps code completion
  2057. p_block->sub_blocks.push_back(cf_if->body);
  2058. if (!_enter_indent_block(cf_if->body)) {
  2059. _set_error("Expected indented block after 'if'");
  2060. p_block->end_line = tokenizer->get_token_line();
  2061. return;
  2062. }
  2063. current_block = cf_if->body;
  2064. _parse_block(cf_if->body, p_static);
  2065. current_block = p_block;
  2066. if (error_set)
  2067. return;
  2068. p_block->statements.push_back(cf_if);
  2069. while (true) {
  2070. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline())
  2071. ;
  2072. if (tab_level.back()->get() < indent_level) { //not at current indent level
  2073. p_block->end_line = tokenizer->get_token_line();
  2074. return;
  2075. }
  2076. if (tokenizer->get_token() == GDScriptTokenizer::TK_CF_ELIF) {
  2077. if (tab_level.back()->get() > indent_level) {
  2078. _set_error("Invalid indent");
  2079. return;
  2080. }
  2081. tokenizer->advance();
  2082. cf_if->body_else = alloc_node<BlockNode>();
  2083. cf_if->body_else->parent_block = p_block;
  2084. p_block->sub_blocks.push_back(cf_if->body_else);
  2085. ControlFlowNode *cf_else = alloc_node<ControlFlowNode>();
  2086. cf_else->cf_type = ControlFlowNode::CF_IF;
  2087. //condition
  2088. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2089. if (!condition) {
  2090. if (_recover_from_completion()) {
  2091. break;
  2092. }
  2093. return;
  2094. }
  2095. cf_else->arguments.push_back(condition);
  2096. cf_else->cf_type = ControlFlowNode::CF_IF;
  2097. cf_if->body_else->statements.push_back(cf_else);
  2098. cf_if = cf_else;
  2099. cf_if->body = alloc_node<BlockNode>();
  2100. cf_if->body->parent_block = p_block;
  2101. p_block->sub_blocks.push_back(cf_if->body);
  2102. if (!_enter_indent_block(cf_if->body)) {
  2103. _set_error("Expected indented block after 'elif'");
  2104. p_block->end_line = tokenizer->get_token_line();
  2105. return;
  2106. }
  2107. current_block = cf_else->body;
  2108. _parse_block(cf_else->body, p_static);
  2109. current_block = p_block;
  2110. if (error_set)
  2111. return;
  2112. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CF_ELSE) {
  2113. if (tab_level.back()->get() > indent_level) {
  2114. _set_error("Invalid indent");
  2115. return;
  2116. }
  2117. tokenizer->advance();
  2118. cf_if->body_else = alloc_node<BlockNode>();
  2119. cf_if->body_else->parent_block = p_block;
  2120. p_block->sub_blocks.push_back(cf_if->body_else);
  2121. if (!_enter_indent_block(cf_if->body_else)) {
  2122. _set_error("Expected indented block after 'else'");
  2123. p_block->end_line = tokenizer->get_token_line();
  2124. return;
  2125. }
  2126. current_block = cf_if->body_else;
  2127. _parse_block(cf_if->body_else, p_static);
  2128. current_block = p_block;
  2129. if (error_set)
  2130. return;
  2131. break; //after else, exit
  2132. } else
  2133. break;
  2134. }
  2135. } break;
  2136. case GDScriptTokenizer::TK_CF_WHILE: {
  2137. tokenizer->advance();
  2138. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2139. if (!condition) {
  2140. if (_recover_from_completion()) {
  2141. break;
  2142. }
  2143. return;
  2144. }
  2145. ControlFlowNode *cf_while = alloc_node<ControlFlowNode>();
  2146. cf_while->cf_type = ControlFlowNode::CF_WHILE;
  2147. cf_while->arguments.push_back(condition);
  2148. cf_while->body = alloc_node<BlockNode>();
  2149. cf_while->body->parent_block = p_block;
  2150. p_block->sub_blocks.push_back(cf_while->body);
  2151. if (!_enter_indent_block(cf_while->body)) {
  2152. _set_error("Expected indented block after 'while'");
  2153. p_block->end_line = tokenizer->get_token_line();
  2154. return;
  2155. }
  2156. current_block = cf_while->body;
  2157. _parse_block(cf_while->body, p_static);
  2158. current_block = p_block;
  2159. if (error_set)
  2160. return;
  2161. p_block->statements.push_back(cf_while);
  2162. } break;
  2163. case GDScriptTokenizer::TK_CF_FOR: {
  2164. tokenizer->advance();
  2165. if (!tokenizer->is_token_literal(0, true)) {
  2166. _set_error("identifier expected after 'for'");
  2167. }
  2168. IdentifierNode *id = alloc_node<IdentifierNode>();
  2169. id->name = tokenizer->get_token_identifier();
  2170. tokenizer->advance();
  2171. if (tokenizer->get_token() != GDScriptTokenizer::TK_OP_IN) {
  2172. _set_error("'in' expected after identifier");
  2173. return;
  2174. }
  2175. tokenizer->advance();
  2176. Node *container = _parse_and_reduce_expression(p_block, p_static);
  2177. if (!container) {
  2178. if (_recover_from_completion()) {
  2179. break;
  2180. }
  2181. return;
  2182. }
  2183. if (container->type == Node::TYPE_OPERATOR) {
  2184. OperatorNode *op = static_cast<OperatorNode *>(container);
  2185. 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) {
  2186. //iterating a range, so see if range() can be optimized without allocating memory, by replacing it by vectors (which can work as iterable too!)
  2187. Vector<Node *> args;
  2188. Vector<double> constants;
  2189. bool constant = false;
  2190. for (int i = 1; i < op->arguments.size(); i++) {
  2191. args.push_back(op->arguments[i]);
  2192. if (constant && op->arguments[i]->type == Node::TYPE_CONSTANT) {
  2193. ConstantNode *c = static_cast<ConstantNode *>(op->arguments[i]);
  2194. if (c->value.get_type() == Variant::REAL || c->value.get_type() == Variant::INT) {
  2195. constants.push_back(c->value);
  2196. constant = true;
  2197. }
  2198. } else {
  2199. constant = false;
  2200. }
  2201. }
  2202. if (args.size() > 0 && args.size() < 4) {
  2203. if (constant) {
  2204. ConstantNode *cn = alloc_node<ConstantNode>();
  2205. switch (args.size()) {
  2206. case 1: cn->value = (int)constants[0]; break;
  2207. case 2: cn->value = Vector2(constants[0], constants[1]); break;
  2208. case 3: cn->value = Vector3(constants[0], constants[1], constants[2]); break;
  2209. }
  2210. container = cn;
  2211. } else {
  2212. OperatorNode *on = alloc_node<OperatorNode>();
  2213. on->op = OperatorNode::OP_CALL;
  2214. TypeNode *tn = alloc_node<TypeNode>();
  2215. on->arguments.push_back(tn);
  2216. switch (args.size()) {
  2217. case 1: tn->vtype = Variant::INT; break;
  2218. case 2: tn->vtype = Variant::VECTOR2; break;
  2219. case 3: tn->vtype = Variant::VECTOR3; break;
  2220. }
  2221. for (int i = 0; i < args.size(); i++) {
  2222. on->arguments.push_back(args[i]);
  2223. }
  2224. container = on;
  2225. }
  2226. }
  2227. }
  2228. }
  2229. ControlFlowNode *cf_for = alloc_node<ControlFlowNode>();
  2230. cf_for->cf_type = ControlFlowNode::CF_FOR;
  2231. cf_for->arguments.push_back(id);
  2232. cf_for->arguments.push_back(container);
  2233. cf_for->body = alloc_node<BlockNode>();
  2234. cf_for->body->parent_block = p_block;
  2235. p_block->sub_blocks.push_back(cf_for->body);
  2236. if (!_enter_indent_block(cf_for->body)) {
  2237. _set_error("Expected indented block after 'for'");
  2238. p_block->end_line = tokenizer->get_token_line();
  2239. return;
  2240. }
  2241. current_block = cf_for->body;
  2242. // this is for checking variable for redefining
  2243. // inside this _parse_block
  2244. cf_for->body->variables.push_back(id->name);
  2245. cf_for->body->variable_lines.push_back(id->line);
  2246. _parse_block(cf_for->body, p_static);
  2247. cf_for->body->variables.remove(0);
  2248. cf_for->body->variable_lines.remove(0);
  2249. current_block = p_block;
  2250. if (error_set)
  2251. return;
  2252. p_block->statements.push_back(cf_for);
  2253. } break;
  2254. case GDScriptTokenizer::TK_CF_CONTINUE: {
  2255. tokenizer->advance();
  2256. ControlFlowNode *cf_continue = alloc_node<ControlFlowNode>();
  2257. cf_continue->cf_type = ControlFlowNode::CF_CONTINUE;
  2258. p_block->statements.push_back(cf_continue);
  2259. if (!_end_statement()) {
  2260. _set_error("Expected end of statement (continue)");
  2261. return;
  2262. }
  2263. } break;
  2264. case GDScriptTokenizer::TK_CF_BREAK: {
  2265. tokenizer->advance();
  2266. ControlFlowNode *cf_break = alloc_node<ControlFlowNode>();
  2267. cf_break->cf_type = ControlFlowNode::CF_BREAK;
  2268. p_block->statements.push_back(cf_break);
  2269. if (!_end_statement()) {
  2270. _set_error("Expected end of statement (break)");
  2271. return;
  2272. }
  2273. } break;
  2274. case GDScriptTokenizer::TK_CF_RETURN: {
  2275. tokenizer->advance();
  2276. ControlFlowNode *cf_return = alloc_node<ControlFlowNode>();
  2277. cf_return->cf_type = ControlFlowNode::CF_RETURN;
  2278. if (tokenizer->get_token() == GDScriptTokenizer::TK_SEMICOLON || tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE || tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  2279. //expect end of statement
  2280. p_block->statements.push_back(cf_return);
  2281. if (!_end_statement()) {
  2282. return;
  2283. }
  2284. } else {
  2285. //expect expression
  2286. Node *retexpr = _parse_and_reduce_expression(p_block, p_static);
  2287. if (!retexpr) {
  2288. if (_recover_from_completion()) {
  2289. break;
  2290. }
  2291. return;
  2292. }
  2293. cf_return->arguments.push_back(retexpr);
  2294. p_block->statements.push_back(cf_return);
  2295. if (!_end_statement()) {
  2296. _set_error("Expected end of statement after return expression.");
  2297. return;
  2298. }
  2299. }
  2300. } break;
  2301. case GDScriptTokenizer::TK_CF_MATCH: {
  2302. tokenizer->advance();
  2303. MatchNode *match_node = alloc_node<MatchNode>();
  2304. Node *val_to_match = _parse_and_reduce_expression(p_block, p_static);
  2305. if (!val_to_match) {
  2306. if (_recover_from_completion()) {
  2307. break;
  2308. }
  2309. return;
  2310. }
  2311. match_node->val_to_match = val_to_match;
  2312. if (!_enter_indent_block()) {
  2313. _set_error("Expected indented pattern matching block after 'match'");
  2314. return;
  2315. }
  2316. BlockNode *compiled_branches = alloc_node<BlockNode>();
  2317. compiled_branches->parent_block = p_block;
  2318. compiled_branches->parent_class = p_block->parent_class;
  2319. p_block->sub_blocks.push_back(compiled_branches);
  2320. _parse_pattern_block(compiled_branches, match_node->branches, p_static);
  2321. _transform_match_statment(compiled_branches, match_node);
  2322. ControlFlowNode *match_cf_node = alloc_node<ControlFlowNode>();
  2323. match_cf_node->cf_type = ControlFlowNode::CF_MATCH;
  2324. match_cf_node->match = match_node;
  2325. p_block->statements.push_back(match_cf_node);
  2326. _end_statement();
  2327. } break;
  2328. case GDScriptTokenizer::TK_PR_ASSERT: {
  2329. tokenizer->advance();
  2330. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2331. if (!condition) {
  2332. if (_recover_from_completion()) {
  2333. break;
  2334. }
  2335. return;
  2336. }
  2337. AssertNode *an = alloc_node<AssertNode>();
  2338. an->condition = condition;
  2339. p_block->statements.push_back(an);
  2340. if (!_end_statement()) {
  2341. _set_error("Expected end of statement after assert.");
  2342. return;
  2343. }
  2344. } break;
  2345. case GDScriptTokenizer::TK_PR_BREAKPOINT: {
  2346. tokenizer->advance();
  2347. BreakpointNode *bn = alloc_node<BreakpointNode>();
  2348. p_block->statements.push_back(bn);
  2349. if (!_end_statement()) {
  2350. _set_error("Expected end of statement after breakpoint.");
  2351. return;
  2352. }
  2353. } break;
  2354. default: {
  2355. Node *expression = _parse_and_reduce_expression(p_block, p_static, false, true);
  2356. if (!expression) {
  2357. if (_recover_from_completion()) {
  2358. break;
  2359. }
  2360. return;
  2361. }
  2362. p_block->statements.push_back(expression);
  2363. if (!_end_statement()) {
  2364. _set_error("Expected end of statement after expression.");
  2365. return;
  2366. }
  2367. } break;
  2368. /*
  2369. case GDScriptTokenizer::TK_CF_LOCAL: {
  2370. if (tokenizer->get_token(1)!=GDScriptTokenizer::TK_SEMICOLON && tokenizer->get_token(1)!=GDScriptTokenizer::TK_NEWLINE ) {
  2371. _set_error("Expected ';' or <NewLine>.");
  2372. }
  2373. tokenizer->advance();
  2374. } break;
  2375. */
  2376. }
  2377. }
  2378. }
  2379. bool GDScriptParser::_parse_newline() {
  2380. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_EOF && tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE) {
  2381. int indent = tokenizer->get_token_line_indent();
  2382. int current_indent = tab_level.back()->get();
  2383. if (indent > current_indent) {
  2384. _set_error("Unexpected indent.");
  2385. return false;
  2386. }
  2387. if (indent < current_indent) {
  2388. while (indent < current_indent) {
  2389. //exit block
  2390. if (tab_level.size() == 1) {
  2391. _set_error("Invalid indent. BUG?");
  2392. return false;
  2393. }
  2394. tab_level.pop_back();
  2395. if (tab_level.back()->get() < indent) {
  2396. _set_error("Unindent does not match any outer indentation level.");
  2397. return false;
  2398. }
  2399. current_indent = tab_level.back()->get();
  2400. }
  2401. tokenizer->advance();
  2402. return false;
  2403. }
  2404. }
  2405. tokenizer->advance();
  2406. return true;
  2407. }
  2408. void GDScriptParser::_parse_extends(ClassNode *p_class) {
  2409. if (p_class->extends_used) {
  2410. _set_error("'extends' already used for this class.");
  2411. return;
  2412. }
  2413. if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty()) {
  2414. _set_error("'extends' must be used before anything else.");
  2415. return;
  2416. }
  2417. p_class->extends_used = true;
  2418. tokenizer->advance();
  2419. if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token_type() == Variant::OBJECT) {
  2420. p_class->extends_class.push_back(Variant::get_type_name(Variant::OBJECT));
  2421. tokenizer->advance();
  2422. return;
  2423. }
  2424. // see if inheritance happens from a file
  2425. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT) {
  2426. Variant constant = tokenizer->get_token_constant();
  2427. if (constant.get_type() != Variant::STRING) {
  2428. _set_error("'extends' constant must be a string.");
  2429. return;
  2430. }
  2431. p_class->extends_file = constant;
  2432. tokenizer->advance();
  2433. if (tokenizer->get_token() != GDScriptTokenizer::TK_PERIOD) {
  2434. return;
  2435. } else
  2436. tokenizer->advance();
  2437. }
  2438. while (true) {
  2439. switch (tokenizer->get_token()) {
  2440. case GDScriptTokenizer::TK_IDENTIFIER: {
  2441. StringName identifier = tokenizer->get_token_identifier();
  2442. p_class->extends_class.push_back(identifier);
  2443. } break;
  2444. case GDScriptTokenizer::TK_PERIOD:
  2445. break;
  2446. default: {
  2447. _set_error("Invalid 'extends' syntax, expected string constant (path) and/or identifier (parent class).");
  2448. return;
  2449. }
  2450. }
  2451. tokenizer->advance(1);
  2452. switch (tokenizer->get_token()) {
  2453. case GDScriptTokenizer::TK_IDENTIFIER:
  2454. case GDScriptTokenizer::TK_PERIOD:
  2455. continue;
  2456. default:
  2457. return;
  2458. }
  2459. }
  2460. }
  2461. void GDScriptParser::_parse_class(ClassNode *p_class) {
  2462. int indent_level = tab_level.back()->get();
  2463. while (true) {
  2464. GDScriptTokenizer::Token token = tokenizer->get_token();
  2465. if (error_set)
  2466. return;
  2467. if (indent_level > tab_level.back()->get()) {
  2468. p_class->end_line = tokenizer->get_token_line();
  2469. return; //go back a level
  2470. }
  2471. switch (token) {
  2472. case GDScriptTokenizer::TK_EOF:
  2473. p_class->end_line = tokenizer->get_token_line();
  2474. case GDScriptTokenizer::TK_ERROR: {
  2475. return; //go back
  2476. //end of file!
  2477. } break;
  2478. case GDScriptTokenizer::TK_NEWLINE: {
  2479. if (!_parse_newline()) {
  2480. if (!error_set) {
  2481. p_class->end_line = tokenizer->get_token_line();
  2482. }
  2483. return;
  2484. }
  2485. } break;
  2486. case GDScriptTokenizer::TK_PR_EXTENDS: {
  2487. _parse_extends(p_class);
  2488. if (error_set)
  2489. return;
  2490. if (!_end_statement()) {
  2491. _set_error("Expected end of statement after extends");
  2492. return;
  2493. }
  2494. } break;
  2495. case GDScriptTokenizer::TK_PR_CLASS_NAME: {
  2496. if (p_class->owner) {
  2497. _set_error("'class_name' is only valid for the main class namespace.");
  2498. return;
  2499. }
  2500. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_IDENTIFIER) {
  2501. _set_error("'class_name' syntax: 'class_name <UniqueName>'");
  2502. return;
  2503. }
  2504. p_class->name = tokenizer->get_token_identifier(1);
  2505. if (self_path != String() && ScriptServer::is_global_class(p_class->name) && ScriptServer::get_global_class_path(p_class->name) != self_path) {
  2506. _set_error("Unique global class '" + p_class->name + "' already exists at path: " + ScriptServer::get_global_class_path(p_class->name));
  2507. return;
  2508. }
  2509. tokenizer->advance(2);
  2510. } break;
  2511. case GDScriptTokenizer::TK_PR_TOOL: {
  2512. if (p_class->tool) {
  2513. _set_error("tool used more than once");
  2514. return;
  2515. }
  2516. p_class->tool = true;
  2517. tokenizer->advance();
  2518. } break;
  2519. case GDScriptTokenizer::TK_PR_CLASS: {
  2520. //class inside class :D
  2521. StringName name;
  2522. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_IDENTIFIER) {
  2523. _set_error("'class' syntax: 'class <Name>:' or 'class <Name> extends <BaseClass>:'");
  2524. return;
  2525. }
  2526. name = tokenizer->get_token_identifier(1);
  2527. tokenizer->advance(2);
  2528. // Check if name is shadowing something else
  2529. if (ClassDB::class_exists(name)) {
  2530. _set_error("Class '" + String(name) + "' shadows a native class.");
  2531. return;
  2532. }
  2533. if (ScriptServer::is_global_class(name)) {
  2534. _set_error("Can't override name of unique global class '" + name + "' already exists at path: " + ScriptServer::get_global_class_path(p_class->name));
  2535. return;
  2536. }
  2537. if (class_map.has(name)) {
  2538. _set_error("Class '" + String(name) + "' shadows another class in the file.");
  2539. return;
  2540. }
  2541. for (int i = 0; i < p_class->constant_expressions.size(); i++) {
  2542. if (p_class->constant_expressions[i].identifier == name) {
  2543. _set_error("Class '" + String(name) + "' shadows a constant of the outer class.");
  2544. return;
  2545. }
  2546. }
  2547. ClassNode *newclass = alloc_node<ClassNode>();
  2548. newclass->initializer = alloc_node<BlockNode>();
  2549. newclass->initializer->parent_class = newclass;
  2550. newclass->ready = alloc_node<BlockNode>();
  2551. newclass->ready->parent_class = newclass;
  2552. newclass->name = name;
  2553. newclass->owner = p_class;
  2554. p_class->subclasses.push_back(newclass);
  2555. class_map.insert(name, newclass);
  2556. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_EXTENDS) {
  2557. _parse_extends(newclass);
  2558. if (error_set)
  2559. return;
  2560. }
  2561. if (!_enter_indent_block()) {
  2562. _set_error("Indented block expected.");
  2563. return;
  2564. }
  2565. current_class = newclass;
  2566. _parse_class(newclass);
  2567. current_class = p_class;
  2568. } break;
  2569. /* this is for functions....
  2570. case GDScriptTokenizer::TK_CF_PASS: {
  2571. tokenizer->advance(1);
  2572. } break;
  2573. */
  2574. case GDScriptTokenizer::TK_PR_STATIC: {
  2575. tokenizer->advance();
  2576. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  2577. _set_error("Expected 'func'.");
  2578. return;
  2579. }
  2580. }; //fallthrough to function
  2581. case GDScriptTokenizer::TK_PR_FUNCTION: {
  2582. bool _static = false;
  2583. pending_newline = -1;
  2584. if (tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_STATIC) {
  2585. _static = true;
  2586. }
  2587. tokenizer->advance();
  2588. StringName name;
  2589. if (_get_completable_identifier(COMPLETION_VIRTUAL_FUNC, name)) {
  2590. }
  2591. if (name == StringName()) {
  2592. _set_error("Expected identifier after 'func' (syntax: 'func <identifier>([arguments]):' ).");
  2593. return;
  2594. }
  2595. for (int i = 0; i < p_class->functions.size(); i++) {
  2596. if (p_class->functions[i]->name == name) {
  2597. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->functions[i]->line) + ").");
  2598. }
  2599. }
  2600. for (int i = 0; i < p_class->static_functions.size(); i++) {
  2601. if (p_class->static_functions[i]->name == name) {
  2602. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->static_functions[i]->line) + ").");
  2603. }
  2604. }
  2605. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2606. _set_error("Expected '(' after identifier (syntax: 'func <identifier>([arguments]):' ).");
  2607. return;
  2608. }
  2609. tokenizer->advance();
  2610. Vector<StringName> arguments;
  2611. Vector<Node *> default_values;
  2612. int fnline = tokenizer->get_token_line();
  2613. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2614. //has arguments
  2615. bool defaulting = false;
  2616. while (true) {
  2617. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2618. tokenizer->advance();
  2619. continue;
  2620. }
  2621. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_VAR) {
  2622. tokenizer->advance(); //var before the identifier is allowed
  2623. }
  2624. if (!tokenizer->is_token_literal(0, true)) {
  2625. _set_error("Expected identifier for argument.");
  2626. return;
  2627. }
  2628. StringName argname = tokenizer->get_token_identifier();
  2629. arguments.push_back(argname);
  2630. tokenizer->advance();
  2631. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  2632. DataType argtype;
  2633. if (!_parse_type(argtype)) {
  2634. _set_error("Expected type for argument.");
  2635. return;
  2636. }
  2637. }
  2638. if (defaulting && tokenizer->get_token() != GDScriptTokenizer::TK_OP_ASSIGN) {
  2639. _set_error("Default parameter expected.");
  2640. return;
  2641. }
  2642. //tokenizer->advance();
  2643. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  2644. defaulting = true;
  2645. tokenizer->advance(1);
  2646. Node *defval = _parse_and_reduce_expression(p_class, _static);
  2647. if (!defval || error_set)
  2648. return;
  2649. OperatorNode *on = alloc_node<OperatorNode>();
  2650. on->op = OperatorNode::OP_ASSIGN;
  2651. IdentifierNode *in = alloc_node<IdentifierNode>();
  2652. in->name = argname;
  2653. on->arguments.push_back(in);
  2654. on->arguments.push_back(defval);
  2655. /* no ..
  2656. if (defval->type!=Node::TYPE_CONSTANT) {
  2657. _set_error("default argument must be constant");
  2658. }
  2659. */
  2660. default_values.push_back(on);
  2661. }
  2662. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2663. tokenizer->advance();
  2664. }
  2665. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2666. tokenizer->advance();
  2667. continue;
  2668. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2669. _set_error("Expected ',' or ')'.");
  2670. return;
  2671. }
  2672. break;
  2673. }
  2674. }
  2675. tokenizer->advance();
  2676. BlockNode *block = alloc_node<BlockNode>();
  2677. block->parent_class = p_class;
  2678. if (name == "_init") {
  2679. if (p_class->extends_used) {
  2680. OperatorNode *cparent = alloc_node<OperatorNode>();
  2681. cparent->op = OperatorNode::OP_PARENT_CALL;
  2682. block->statements.push_back(cparent);
  2683. IdentifierNode *id = alloc_node<IdentifierNode>();
  2684. id->name = "_init";
  2685. cparent->arguments.push_back(id);
  2686. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
  2687. tokenizer->advance();
  2688. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2689. _set_error("expected '(' for parent constructor arguments.");
  2690. }
  2691. tokenizer->advance();
  2692. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2693. //has arguments
  2694. parenthesis++;
  2695. while (true) {
  2696. Node *arg = _parse_and_reduce_expression(p_class, _static);
  2697. cparent->arguments.push_back(arg);
  2698. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2699. tokenizer->advance();
  2700. continue;
  2701. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2702. _set_error("Expected ',' or ')'.");
  2703. return;
  2704. }
  2705. break;
  2706. }
  2707. parenthesis--;
  2708. }
  2709. tokenizer->advance();
  2710. }
  2711. } else {
  2712. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
  2713. _set_error("Parent constructor call found for a class without inheritance.");
  2714. return;
  2715. }
  2716. }
  2717. }
  2718. if (tokenizer->get_token() == GDScriptTokenizer::TK_FORWARD_ARROW) {
  2719. DataType rettype;
  2720. if (!_parse_type(rettype, true)) {
  2721. _set_error("Expected return type for function.");
  2722. return;
  2723. }
  2724. }
  2725. if (!_enter_indent_block(block)) {
  2726. _set_error("Indented block expected.");
  2727. return;
  2728. }
  2729. FunctionNode *function = alloc_node<FunctionNode>();
  2730. function->name = name;
  2731. function->arguments = arguments;
  2732. function->default_values = default_values;
  2733. function->_static = _static;
  2734. function->line = fnline;
  2735. function->rpc_mode = rpc_mode;
  2736. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  2737. if (_static)
  2738. p_class->static_functions.push_back(function);
  2739. else
  2740. p_class->functions.push_back(function);
  2741. current_function = function;
  2742. function->body = block;
  2743. current_block = block;
  2744. _parse_block(block, _static);
  2745. current_block = NULL;
  2746. //arguments
  2747. } break;
  2748. case GDScriptTokenizer::TK_PR_SIGNAL: {
  2749. tokenizer->advance();
  2750. if (!tokenizer->is_token_literal()) {
  2751. _set_error("Expected identifier after 'signal'.");
  2752. return;
  2753. }
  2754. ClassNode::Signal sig;
  2755. sig.name = tokenizer->get_token_identifier();
  2756. tokenizer->advance();
  2757. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2758. tokenizer->advance();
  2759. while (true) {
  2760. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2761. tokenizer->advance();
  2762. continue;
  2763. }
  2764. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2765. tokenizer->advance();
  2766. break;
  2767. }
  2768. if (!tokenizer->is_token_literal(0, true)) {
  2769. _set_error("Expected identifier in signal argument.");
  2770. return;
  2771. }
  2772. sig.arguments.push_back(tokenizer->get_token_identifier());
  2773. tokenizer->advance();
  2774. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2775. tokenizer->advance();
  2776. }
  2777. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2778. tokenizer->advance();
  2779. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2780. _set_error("Expected ',' or ')' after signal parameter identifier.");
  2781. return;
  2782. }
  2783. }
  2784. }
  2785. p_class->_signals.push_back(sig);
  2786. if (!_end_statement()) {
  2787. _set_error("Expected end of statement (signal)");
  2788. return;
  2789. }
  2790. } break;
  2791. case GDScriptTokenizer::TK_PR_EXPORT: {
  2792. tokenizer->advance();
  2793. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2794. tokenizer->advance();
  2795. String hint_prefix = "";
  2796. bool is_arrayed = false;
  2797. while (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE &&
  2798. tokenizer->get_token_type() == Variant::ARRAY &&
  2799. tokenizer->get_token(1) == GDScriptTokenizer::TK_COMMA) {
  2800. tokenizer->advance(); // Array
  2801. tokenizer->advance(); // Comma
  2802. if (is_arrayed) {
  2803. hint_prefix += itos(Variant::ARRAY) + ":";
  2804. } else {
  2805. is_arrayed = true;
  2806. }
  2807. }
  2808. if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE) {
  2809. Variant::Type type = tokenizer->get_token_type();
  2810. if (type == Variant::NIL) {
  2811. _set_error("Can't export null type.");
  2812. return;
  2813. }
  2814. if (type == Variant::OBJECT) {
  2815. _set_error("Can't export raw object type.");
  2816. return;
  2817. }
  2818. current_export.type = type;
  2819. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  2820. tokenizer->advance();
  2821. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2822. // hint expected next!
  2823. tokenizer->advance();
  2824. switch (type) {
  2825. case Variant::INT: {
  2826. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  2827. //current_export.hint=PROPERTY_HINT_ALL_FLAGS;
  2828. tokenizer->advance();
  2829. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2830. break;
  2831. }
  2832. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  2833. _set_error("Expected ')' or ',' in bit flags hint.");
  2834. return;
  2835. }
  2836. current_export.hint = PROPERTY_HINT_FLAGS;
  2837. tokenizer->advance();
  2838. bool first = true;
  2839. while (true) {
  2840. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2841. current_export = PropertyInfo();
  2842. _set_error("Expected a string constant in named bit flags hint.");
  2843. return;
  2844. }
  2845. String c = tokenizer->get_token_constant();
  2846. if (!first)
  2847. current_export.hint_string += ",";
  2848. else
  2849. first = false;
  2850. current_export.hint_string += c.xml_escape();
  2851. tokenizer->advance();
  2852. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  2853. break;
  2854. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  2855. current_export = PropertyInfo();
  2856. _set_error("Expected ')' or ',' in named bit flags hint.");
  2857. return;
  2858. }
  2859. tokenizer->advance();
  2860. }
  2861. break;
  2862. }
  2863. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  2864. //enumeration
  2865. current_export.hint = PROPERTY_HINT_ENUM;
  2866. bool first = true;
  2867. while (true) {
  2868. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2869. current_export = PropertyInfo();
  2870. _set_error("Expected a string constant in enumeration hint.");
  2871. return;
  2872. }
  2873. String c = tokenizer->get_token_constant();
  2874. if (!first)
  2875. current_export.hint_string += ",";
  2876. else
  2877. first = false;
  2878. current_export.hint_string += c.xml_escape();
  2879. tokenizer->advance();
  2880. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  2881. break;
  2882. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  2883. current_export = PropertyInfo();
  2884. _set_error("Expected ')' or ',' in enumeration hint.");
  2885. return;
  2886. }
  2887. tokenizer->advance();
  2888. }
  2889. break;
  2890. }
  2891. }; //fallthrough to use the same
  2892. case Variant::REAL: {
  2893. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EASE") {
  2894. current_export.hint = PROPERTY_HINT_EXP_EASING;
  2895. tokenizer->advance();
  2896. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2897. _set_error("Expected ')' in hint.");
  2898. return;
  2899. }
  2900. break;
  2901. }
  2902. // range
  2903. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EXP") {
  2904. current_export.hint = PROPERTY_HINT_EXP_RANGE;
  2905. tokenizer->advance();
  2906. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  2907. break;
  2908. else if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  2909. _set_error("Expected ')' or ',' in exponential range hint.");
  2910. return;
  2911. }
  2912. tokenizer->advance();
  2913. } else
  2914. current_export.hint = PROPERTY_HINT_RANGE;
  2915. float sign = 1.0;
  2916. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  2917. sign = -1;
  2918. tokenizer->advance();
  2919. }
  2920. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2921. current_export = PropertyInfo();
  2922. _set_error("Expected a range in numeric hint.");
  2923. return;
  2924. }
  2925. current_export.hint_string = rtos(sign * double(tokenizer->get_token_constant()));
  2926. tokenizer->advance();
  2927. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2928. current_export.hint_string = "0," + current_export.hint_string;
  2929. break;
  2930. }
  2931. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  2932. current_export = PropertyInfo();
  2933. _set_error("Expected ',' or ')' in numeric range hint.");
  2934. return;
  2935. }
  2936. tokenizer->advance();
  2937. sign = 1.0;
  2938. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  2939. sign = -1;
  2940. tokenizer->advance();
  2941. }
  2942. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2943. current_export = PropertyInfo();
  2944. _set_error("Expected a number as upper bound in numeric range hint.");
  2945. return;
  2946. }
  2947. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  2948. tokenizer->advance();
  2949. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  2950. break;
  2951. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  2952. current_export = PropertyInfo();
  2953. _set_error("Expected ',' or ')' in numeric range hint.");
  2954. return;
  2955. }
  2956. tokenizer->advance();
  2957. sign = 1.0;
  2958. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  2959. sign = -1;
  2960. tokenizer->advance();
  2961. }
  2962. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2963. current_export = PropertyInfo();
  2964. _set_error("Expected a number as step in numeric range hint.");
  2965. return;
  2966. }
  2967. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  2968. tokenizer->advance();
  2969. } break;
  2970. case Variant::STRING: {
  2971. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  2972. //enumeration
  2973. current_export.hint = PROPERTY_HINT_ENUM;
  2974. bool first = true;
  2975. while (true) {
  2976. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2977. current_export = PropertyInfo();
  2978. _set_error("Expected a string constant in enumeration hint.");
  2979. return;
  2980. }
  2981. String c = tokenizer->get_token_constant();
  2982. if (!first)
  2983. current_export.hint_string += ",";
  2984. else
  2985. first = false;
  2986. current_export.hint_string += c.xml_escape();
  2987. tokenizer->advance();
  2988. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  2989. break;
  2990. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  2991. current_export = PropertyInfo();
  2992. _set_error("Expected ')' or ',' in enumeration hint.");
  2993. return;
  2994. }
  2995. tokenizer->advance();
  2996. }
  2997. break;
  2998. }
  2999. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "DIR") {
  3000. tokenizer->advance();
  3001. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3002. current_export.hint = PROPERTY_HINT_DIR;
  3003. else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3004. tokenizer->advance();
  3005. if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier() == "GLOBAL")) {
  3006. _set_error("Expected 'GLOBAL' after comma in directory hint.");
  3007. return;
  3008. }
  3009. if (!p_class->tool) {
  3010. _set_error("Global filesystem hints may only be used in tool scripts.");
  3011. return;
  3012. }
  3013. current_export.hint = PROPERTY_HINT_GLOBAL_DIR;
  3014. tokenizer->advance();
  3015. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3016. _set_error("Expected ')' in hint.");
  3017. return;
  3018. }
  3019. } else {
  3020. _set_error("Expected ')' or ',' in hint.");
  3021. return;
  3022. }
  3023. break;
  3024. }
  3025. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FILE") {
  3026. current_export.hint = PROPERTY_HINT_FILE;
  3027. tokenizer->advance();
  3028. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3029. tokenizer->advance();
  3030. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "GLOBAL") {
  3031. if (!p_class->tool) {
  3032. _set_error("Global filesystem hints may only be used in tool scripts.");
  3033. return;
  3034. }
  3035. current_export.hint = PROPERTY_HINT_GLOBAL_FILE;
  3036. tokenizer->advance();
  3037. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  3038. break;
  3039. else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA)
  3040. tokenizer->advance();
  3041. else {
  3042. _set_error("Expected ')' or ',' in hint.");
  3043. return;
  3044. }
  3045. }
  3046. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  3047. if (current_export.hint == PROPERTY_HINT_GLOBAL_FILE)
  3048. _set_error("Expected string constant with filter");
  3049. else
  3050. _set_error("Expected 'GLOBAL' or string constant with filter");
  3051. return;
  3052. }
  3053. current_export.hint_string = tokenizer->get_token_constant();
  3054. tokenizer->advance();
  3055. }
  3056. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3057. _set_error("Expected ')' in hint.");
  3058. return;
  3059. }
  3060. break;
  3061. }
  3062. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "MULTILINE") {
  3063. current_export.hint = PROPERTY_HINT_MULTILINE_TEXT;
  3064. tokenizer->advance();
  3065. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3066. _set_error("Expected ')' in hint.");
  3067. return;
  3068. }
  3069. break;
  3070. }
  3071. } break;
  3072. case Variant::COLOR: {
  3073. if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER) {
  3074. current_export = PropertyInfo();
  3075. _set_error("Color type hint expects RGB or RGBA as hints");
  3076. return;
  3077. }
  3078. String identifier = tokenizer->get_token_identifier();
  3079. if (identifier == "RGB") {
  3080. current_export.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  3081. } else if (identifier == "RGBA") {
  3082. //none
  3083. } else {
  3084. current_export = PropertyInfo();
  3085. _set_error("Color type hint expects RGB or RGBA as hints");
  3086. return;
  3087. }
  3088. tokenizer->advance();
  3089. } break;
  3090. default: {
  3091. current_export = PropertyInfo();
  3092. _set_error("Type '" + Variant::get_type_name(type) + "' can't take hints.");
  3093. return;
  3094. } break;
  3095. }
  3096. }
  3097. } else {
  3098. parenthesis++;
  3099. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3100. if (!subexpr) {
  3101. if (_recover_from_completion()) {
  3102. break;
  3103. }
  3104. return;
  3105. }
  3106. parenthesis--;
  3107. if (subexpr->type != Node::TYPE_CONSTANT) {
  3108. current_export = PropertyInfo();
  3109. _set_error("Expected a constant expression.");
  3110. }
  3111. Variant constant = static_cast<ConstantNode *>(subexpr)->value;
  3112. if (constant.get_type() == Variant::OBJECT) {
  3113. GDScriptNativeClass *native_class = Object::cast_to<GDScriptNativeClass>(constant);
  3114. if (native_class && ClassDB::is_parent_class(native_class->get_name(), "Resource")) {
  3115. current_export.type = Variant::OBJECT;
  3116. current_export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3117. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3118. current_export.hint_string = native_class->get_name();
  3119. } else {
  3120. current_export = PropertyInfo();
  3121. _set_error("Export hint not a resource type.");
  3122. }
  3123. } else if (constant.get_type() == Variant::DICTIONARY) {
  3124. // Enumeration
  3125. bool is_flags = false;
  3126. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3127. tokenizer->advance();
  3128. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  3129. is_flags = true;
  3130. tokenizer->advance();
  3131. } else {
  3132. current_export = PropertyInfo();
  3133. _set_error("Expected 'FLAGS' after comma.");
  3134. }
  3135. }
  3136. current_export.type = Variant::INT;
  3137. current_export.hint = is_flags ? PROPERTY_HINT_FLAGS : PROPERTY_HINT_ENUM;
  3138. Dictionary enum_values = constant;
  3139. List<Variant> keys;
  3140. enum_values.get_key_list(&keys);
  3141. bool first = true;
  3142. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  3143. if (enum_values[E->get()].get_type() == Variant::INT) {
  3144. if (!first)
  3145. current_export.hint_string += ",";
  3146. else
  3147. first = false;
  3148. current_export.hint_string += E->get().operator String().camelcase_to_underscore(true).capitalize().xml_escape();
  3149. if (!is_flags) {
  3150. current_export.hint_string += ":";
  3151. current_export.hint_string += enum_values[E->get()].operator String().xml_escape();
  3152. }
  3153. }
  3154. }
  3155. } else {
  3156. current_export = PropertyInfo();
  3157. _set_error("Expected type for export.");
  3158. return;
  3159. }
  3160. }
  3161. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3162. current_export = PropertyInfo();
  3163. _set_error("Expected ')' or ',' after export hint.");
  3164. return;
  3165. }
  3166. if (is_arrayed) {
  3167. hint_prefix += itos(current_export.type);
  3168. if (current_export.hint) {
  3169. hint_prefix += "/" + itos(current_export.hint);
  3170. }
  3171. current_export.hint_string = hint_prefix + ":" + current_export.hint_string;
  3172. current_export.hint = PROPERTY_HINT_TYPE_STRING;
  3173. current_export.type = Variant::ARRAY;
  3174. }
  3175. tokenizer->advance();
  3176. }
  3177. 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) {
  3178. current_export = PropertyInfo();
  3179. _set_error("Expected 'var', 'onready', 'remote', 'master', 'slave', 'sync', 'remotesync', 'mastersync', 'slavesync'.");
  3180. return;
  3181. }
  3182. continue;
  3183. } break;
  3184. case GDScriptTokenizer::TK_PR_ONREADY: {
  3185. //may be fallthrough from export, ignore if so
  3186. tokenizer->advance();
  3187. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3188. _set_error("Expected 'var'.");
  3189. return;
  3190. }
  3191. continue;
  3192. } break;
  3193. case GDScriptTokenizer::TK_PR_REMOTE: {
  3194. //may be fallthrough from export, ignore if so
  3195. tokenizer->advance();
  3196. if (current_export.type) {
  3197. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3198. _set_error("Expected 'var'.");
  3199. return;
  3200. }
  3201. } else {
  3202. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3203. _set_error("Expected 'var' or 'func'.");
  3204. return;
  3205. }
  3206. }
  3207. rpc_mode = MultiplayerAPI::RPC_MODE_REMOTE;
  3208. continue;
  3209. } break;
  3210. case GDScriptTokenizer::TK_PR_MASTER: {
  3211. //may be fallthrough from export, ignore if so
  3212. tokenizer->advance();
  3213. if (current_export.type) {
  3214. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3215. _set_error("Expected 'var'.");
  3216. return;
  3217. }
  3218. } else {
  3219. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3220. _set_error("Expected 'var' or 'func'.");
  3221. return;
  3222. }
  3223. }
  3224. rpc_mode = MultiplayerAPI::RPC_MODE_MASTER;
  3225. continue;
  3226. } break;
  3227. case GDScriptTokenizer::TK_PR_SLAVE: {
  3228. //may be fallthrough from export, ignore if so
  3229. tokenizer->advance();
  3230. if (current_export.type) {
  3231. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3232. _set_error("Expected 'var'.");
  3233. return;
  3234. }
  3235. } else {
  3236. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3237. _set_error("Expected 'var' or 'func'.");
  3238. return;
  3239. }
  3240. }
  3241. rpc_mode = MultiplayerAPI::RPC_MODE_SLAVE;
  3242. continue;
  3243. } break;
  3244. case GDScriptTokenizer::TK_PR_REMOTESYNC:
  3245. case GDScriptTokenizer::TK_PR_SYNC: {
  3246. //may be fallthrough from export, ignore if so
  3247. tokenizer->advance();
  3248. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3249. if (current_export.type)
  3250. _set_error("Expected 'var'.");
  3251. else
  3252. _set_error("Expected 'var' or 'func'.");
  3253. return;
  3254. }
  3255. rpc_mode = MultiplayerAPI::RPC_MODE_SYNC;
  3256. continue;
  3257. } break;
  3258. case GDScriptTokenizer::TK_PR_MASTERSYNC: {
  3259. //may be fallthrough from export, ignore if so
  3260. tokenizer->advance();
  3261. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3262. if (current_export.type)
  3263. _set_error("Expected 'var'.");
  3264. else
  3265. _set_error("Expected 'var' or 'func'.");
  3266. return;
  3267. }
  3268. rpc_mode = MultiplayerAPI::RPC_MODE_MASTERSYNC;
  3269. continue;
  3270. } break;
  3271. case GDScriptTokenizer::TK_PR_SLAVESYNC: {
  3272. //may be fallthrough from export, ignore if so
  3273. tokenizer->advance();
  3274. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3275. if (current_export.type)
  3276. _set_error("Expected 'var'.");
  3277. else
  3278. _set_error("Expected 'var' or 'func'.");
  3279. return;
  3280. }
  3281. rpc_mode = MultiplayerAPI::RPC_MODE_SLAVESYNC;
  3282. continue;
  3283. } break;
  3284. case GDScriptTokenizer::TK_PR_VAR: {
  3285. //variale declaration and (eventual) initialization
  3286. ClassNode::Member member;
  3287. bool autoexport = tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_EXPORT;
  3288. if (current_export.type != Variant::NIL) {
  3289. member._export = current_export;
  3290. current_export = PropertyInfo();
  3291. }
  3292. bool onready = tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_ONREADY;
  3293. tokenizer->advance();
  3294. if (!tokenizer->is_token_literal(0, true)) {
  3295. _set_error("Expected identifier for member variable name.");
  3296. return;
  3297. }
  3298. member.identifier = tokenizer->get_token_literal();
  3299. member.expression = NULL;
  3300. member._export.name = member.identifier;
  3301. member.line = tokenizer->get_token_line();
  3302. member.rpc_mode = rpc_mode;
  3303. tokenizer->advance();
  3304. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  3305. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  3306. DataType vartype;
  3307. if (!_parse_type(vartype)) {
  3308. _set_error("Expected type for class variable.");
  3309. return;
  3310. }
  3311. }
  3312. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  3313. #ifdef DEBUG_ENABLED
  3314. int line = tokenizer->get_token_line();
  3315. #endif
  3316. tokenizer->advance();
  3317. Node *subexpr = _parse_and_reduce_expression(p_class, false, autoexport || member._export.type != Variant::NIL);
  3318. if (!subexpr) {
  3319. if (_recover_from_completion()) {
  3320. break;
  3321. }
  3322. return;
  3323. }
  3324. //discourage common error
  3325. if (!onready && subexpr->type == Node::TYPE_OPERATOR) {
  3326. OperatorNode *op = static_cast<OperatorNode *>(subexpr);
  3327. if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_SELF && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  3328. IdentifierNode *id = static_cast<IdentifierNode *>(op->arguments[1]);
  3329. if (id->name == "get_node") {
  3330. _set_error("Use 'onready var " + String(member.identifier) + " = get_node(..)' instead");
  3331. return;
  3332. }
  3333. }
  3334. }
  3335. member.expression = subexpr;
  3336. if (autoexport) {
  3337. if (1) /*(subexpr->type==Node::TYPE_ARRAY) {
  3338. member._export.type=Variant::ARRAY;
  3339. } else if (subexpr->type==Node::TYPE_DICTIONARY) {
  3340. member._export.type=Variant::DICTIONARY;
  3341. } else*/
  3342. {
  3343. if (subexpr->type != Node::TYPE_CONSTANT) {
  3344. _set_error("Type-less export needs a constant expression assigned to infer type.");
  3345. return;
  3346. }
  3347. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  3348. if (cn->value.get_type() == Variant::NIL) {
  3349. _set_error("Can't accept a null constant expression for inferring export type.");
  3350. return;
  3351. }
  3352. member._export.type = cn->value.get_type();
  3353. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3354. if (cn->value.get_type() == Variant::OBJECT) {
  3355. Object *obj = cn->value;
  3356. Resource *res = Object::cast_to<Resource>(obj);
  3357. if (res == NULL) {
  3358. _set_error("Exported constant not a type or resource.");
  3359. return;
  3360. }
  3361. member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3362. member._export.hint_string = res->get_class();
  3363. }
  3364. }
  3365. }
  3366. #ifdef TOOLS_ENABLED
  3367. if (subexpr->type == Node::TYPE_CONSTANT && member._export.type != Variant::NIL) {
  3368. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  3369. if (cn->value.get_type() != Variant::NIL) {
  3370. member.default_value = cn->value;
  3371. }
  3372. }
  3373. #endif
  3374. IdentifierNode *id = alloc_node<IdentifierNode>();
  3375. id->name = member.identifier;
  3376. OperatorNode *op = alloc_node<OperatorNode>();
  3377. op->op = OperatorNode::OP_INIT_ASSIGN;
  3378. op->arguments.push_back(id);
  3379. op->arguments.push_back(subexpr);
  3380. #ifdef DEBUG_ENABLED
  3381. NewLineNode *nl = alloc_node<NewLineNode>();
  3382. nl->line = line;
  3383. if (onready)
  3384. p_class->ready->statements.push_back(nl);
  3385. else
  3386. p_class->initializer->statements.push_back(nl);
  3387. #endif
  3388. if (onready)
  3389. p_class->ready->statements.push_back(op);
  3390. else
  3391. p_class->initializer->statements.push_back(op);
  3392. } else {
  3393. if (autoexport) {
  3394. _set_error("Type-less export needs a constant expression assigned to infer type.");
  3395. return;
  3396. }
  3397. }
  3398. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_SETGET) {
  3399. tokenizer->advance();
  3400. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3401. //just comma means using only getter
  3402. if (!tokenizer->is_token_literal()) {
  3403. _set_error("Expected identifier for setter function after 'setget'.");
  3404. }
  3405. member.setter = tokenizer->get_token_literal();
  3406. tokenizer->advance();
  3407. }
  3408. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3409. //there is a getter
  3410. tokenizer->advance();
  3411. if (!tokenizer->is_token_literal()) {
  3412. _set_error("Expected identifier for getter function after ','.");
  3413. }
  3414. member.getter = tokenizer->get_token_literal();
  3415. tokenizer->advance();
  3416. }
  3417. }
  3418. p_class->variables.push_back(member);
  3419. if (!_end_statement()) {
  3420. _set_error("Expected end of statement (continue)");
  3421. return;
  3422. }
  3423. } break;
  3424. case GDScriptTokenizer::TK_PR_CONST: {
  3425. //variale declaration and (eventual) initialization
  3426. ClassNode::Constant constant;
  3427. tokenizer->advance();
  3428. if (!tokenizer->is_token_literal(0, true)) {
  3429. _set_error("Expected name (identifier) for constant.");
  3430. return;
  3431. }
  3432. constant.identifier = tokenizer->get_token_literal();
  3433. tokenizer->advance();
  3434. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  3435. DataType consttype;
  3436. if (!_parse_type(consttype)) {
  3437. _set_error("Expected type for class constant.");
  3438. return;
  3439. }
  3440. }
  3441. if (tokenizer->get_token() != GDScriptTokenizer::TK_OP_ASSIGN) {
  3442. _set_error("Constant expects assignment.");
  3443. return;
  3444. }
  3445. int line = tokenizer->get_token_line();
  3446. tokenizer->advance();
  3447. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3448. if (!subexpr) {
  3449. if (_recover_from_completion()) {
  3450. break;
  3451. }
  3452. return;
  3453. }
  3454. if (subexpr->type != Node::TYPE_CONSTANT) {
  3455. _set_error("Expected constant expression", line);
  3456. return;
  3457. }
  3458. constant.expression = subexpr;
  3459. p_class->constant_expressions.push_back(constant);
  3460. if (!_end_statement()) {
  3461. _set_error("Expected end of statement (constant)", line);
  3462. return;
  3463. }
  3464. } break;
  3465. case GDScriptTokenizer::TK_PR_ENUM: {
  3466. //multiple constant declarations..
  3467. int last_assign = -1; // Incremented by 1 right before the assingment.
  3468. String enum_name;
  3469. Dictionary enum_dict;
  3470. tokenizer->advance();
  3471. if (tokenizer->is_token_literal(0, true)) {
  3472. enum_name = tokenizer->get_token_literal();
  3473. tokenizer->advance();
  3474. }
  3475. if (tokenizer->get_token() != GDScriptTokenizer::TK_CURLY_BRACKET_OPEN) {
  3476. _set_error("Expected '{' in enum declaration");
  3477. return;
  3478. }
  3479. tokenizer->advance();
  3480. while (true) {
  3481. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  3482. tokenizer->advance(); // Ignore newlines
  3483. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  3484. tokenizer->advance();
  3485. break; // End of enum
  3486. } else if (!tokenizer->is_token_literal(0, true)) {
  3487. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  3488. _set_error("Unexpected end of file.");
  3489. } else {
  3490. _set_error(String("Unexpected ") + GDScriptTokenizer::get_token_name(tokenizer->get_token()) + ", expected identifier");
  3491. }
  3492. return;
  3493. } else { // tokenizer->is_token_literal(0, true)
  3494. ClassNode::Constant constant;
  3495. constant.identifier = tokenizer->get_token_literal();
  3496. tokenizer->advance();
  3497. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  3498. tokenizer->advance();
  3499. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3500. if (!subexpr) {
  3501. if (_recover_from_completion()) {
  3502. break;
  3503. }
  3504. return;
  3505. }
  3506. if (subexpr->type != Node::TYPE_CONSTANT) {
  3507. _set_error("Expected constant expression");
  3508. return;
  3509. }
  3510. ConstantNode *subexpr_const = static_cast<ConstantNode *>(subexpr);
  3511. if (subexpr_const->value.get_type() != Variant::INT) {
  3512. _set_error("Expected an int value for enum");
  3513. return;
  3514. }
  3515. last_assign = subexpr_const->value;
  3516. constant.expression = subexpr_const;
  3517. } else {
  3518. last_assign = last_assign + 1;
  3519. ConstantNode *cn = alloc_node<ConstantNode>();
  3520. cn->value = last_assign;
  3521. constant.expression = cn;
  3522. }
  3523. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3524. tokenizer->advance();
  3525. }
  3526. if (enum_name != "") {
  3527. const ConstantNode *cn = static_cast<const ConstantNode *>(constant.expression);
  3528. enum_dict[constant.identifier] = cn->value;
  3529. }
  3530. p_class->constant_expressions.push_back(constant);
  3531. }
  3532. }
  3533. if (enum_name != "") {
  3534. ClassNode::Constant enum_constant;
  3535. enum_constant.identifier = enum_name;
  3536. ConstantNode *cn = alloc_node<ConstantNode>();
  3537. cn->value = enum_dict;
  3538. enum_constant.expression = cn;
  3539. p_class->constant_expressions.push_back(enum_constant);
  3540. }
  3541. if (!_end_statement()) {
  3542. _set_error("Expected end of statement (enum)");
  3543. return;
  3544. }
  3545. } break;
  3546. case GDScriptTokenizer::TK_CONSTANT: {
  3547. if (tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3548. tokenizer->advance();
  3549. // Ignore
  3550. } else {
  3551. _set_error(String() + "Unexpected constant of type: " + Variant::get_type_name(tokenizer->get_token_constant().get_type()));
  3552. return;
  3553. }
  3554. } break;
  3555. default: {
  3556. _set_error(String() + "Unexpected token: " + tokenizer->get_token_name(tokenizer->get_token()) + ":" + tokenizer->get_token_identifier());
  3557. return;
  3558. } break;
  3559. }
  3560. }
  3561. }
  3562. void GDScriptParser::_determine_inheritance(ClassNode *p_class) {
  3563. if (p_class->extends_used) {
  3564. //do inheritance
  3565. String path = p_class->extends_file;
  3566. Ref<GDScript> script;
  3567. StringName native;
  3568. ClassNode *base_class = NULL;
  3569. if (path != "") {
  3570. //path (and optionally subclasses)
  3571. if (path.is_rel_path()) {
  3572. String base = self_path;
  3573. if (base == "" || base.is_rel_path()) {
  3574. _set_error("Could not resolve relative path for parent class: " + path, p_class->line);
  3575. return;
  3576. }
  3577. path = base.get_base_dir().plus_file(path).simplify_path();
  3578. }
  3579. script = ResourceLoader::load(path);
  3580. if (script.is_null()) {
  3581. _set_error("Could not load base class: " + path, p_class->line);
  3582. return;
  3583. }
  3584. if (!script->is_valid()) {
  3585. _set_error("Script not fully loaded (cyclic preload?): " + path, p_class->line);
  3586. return;
  3587. }
  3588. if (p_class->extends_class.size()) {
  3589. for (int i = 0; i < p_class->extends_class.size(); i++) {
  3590. String sub = p_class->extends_class[i];
  3591. if (script->get_subclasses().has(sub)) {
  3592. Ref<Script> subclass = script->get_subclasses()[sub]; //avoid reference from disappearing
  3593. script = subclass;
  3594. } else {
  3595. _set_error("Could not find subclass: " + sub, p_class->line);
  3596. return;
  3597. }
  3598. }
  3599. }
  3600. } else {
  3601. if (p_class->extends_class.size() == 0) {
  3602. _set_error("Parser bug: undecidable inheritance.", p_class->line);
  3603. ERR_FAIL();
  3604. }
  3605. //look around for the subclasses
  3606. int extend_iter = 1;
  3607. String base = p_class->extends_class[0];
  3608. ClassNode *p = p_class->owner;
  3609. Ref<GDScript> base_script;
  3610. if (ScriptServer::is_global_class(base)) {
  3611. base_script = ResourceLoader::load(ScriptServer::get_global_class_path(base));
  3612. if (!base_script.is_valid()) {
  3613. _set_error("Class '" + base + "' could not be fully loaded (script error or cyclic inheritance).", p_class->line);
  3614. return;
  3615. }
  3616. p = NULL;
  3617. }
  3618. while (p) {
  3619. bool found = false;
  3620. for (int i = 0; i < p->subclasses.size(); i++) {
  3621. if (p->subclasses[i]->name == base) {
  3622. ClassNode *test = p->subclasses[i];
  3623. while (test) {
  3624. if (test == p_class) {
  3625. _set_error("Cyclic inheritance.", test->line);
  3626. return;
  3627. }
  3628. if (test->base_type.kind == DataType::CLASS) {
  3629. test = test->base_type.class_type;
  3630. } else {
  3631. break;
  3632. }
  3633. }
  3634. found = true;
  3635. if (extend_iter < p_class->extends_class.size()) {
  3636. // Keep looking at current classes if possible
  3637. base = p_class->extends_class[extend_iter++];
  3638. p = p->subclasses[i];
  3639. } else {
  3640. base_class = p->subclasses[i];
  3641. }
  3642. break;
  3643. }
  3644. }
  3645. if (base_class) break;
  3646. if (found) continue;
  3647. for (int i = 0; i < p->constant_expressions.size(); i++) {
  3648. if (p->constant_expressions[i].identifier == base) {
  3649. if (!p->constant_expressions[i].expression->type == Node::TYPE_CONSTANT) {
  3650. _set_error("Could not resolve constant '" + base + "'.", p_class->line);
  3651. return;
  3652. }
  3653. const ConstantNode *cn = static_cast<const ConstantNode *>(p->constant_expressions[i].expression);
  3654. base_script = cn->value;
  3655. if (base_script.is_null()) {
  3656. _set_error("Constant is not a class: " + base, p_class->line);
  3657. return;
  3658. }
  3659. found = true;
  3660. }
  3661. }
  3662. if (found) break;
  3663. p = p->owner;
  3664. }
  3665. if (base_script.is_valid()) {
  3666. String ident = base;
  3667. for (int i = extend_iter; i < p_class->extends_class.size(); i++) {
  3668. String subclass = p_class->extends_class[i];
  3669. ident += ("." + subclass);
  3670. if (base_script->get_subclasses().has(subclass)) {
  3671. base_script = base_script->get_subclasses()[subclass];
  3672. } else if (base_script->get_constants().has(subclass)) {
  3673. Ref<GDScript> new_base_class = base_script->get_constants()[subclass];
  3674. if (new_base_class.is_null()) {
  3675. _set_error("Constant is not a class: " + ident, p_class->line);
  3676. return;
  3677. }
  3678. base_script = new_base_class;
  3679. } else {
  3680. _set_error("Could not find subclass: " + ident, p_class->line);
  3681. return;
  3682. }
  3683. }
  3684. script = base_script;
  3685. } else if (!base_class) {
  3686. if (p_class->extends_class.size() > 1) {
  3687. _set_error("Invalid inheritance (unknown class + subclasses)", p_class->line);
  3688. return;
  3689. }
  3690. //if not found, try engine classes
  3691. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  3692. _set_error("Unknown class: '" + base + "'", p_class->line);
  3693. return;
  3694. }
  3695. native = base;
  3696. }
  3697. }
  3698. if (base_class) {
  3699. p_class->base_type.has_type = true;
  3700. p_class->base_type.kind = DataType::CLASS;
  3701. p_class->base_type.class_type = base_class;
  3702. } else if (script.is_valid()) {
  3703. p_class->base_type.has_type = true;
  3704. p_class->base_type.kind = DataType::GDSCRIPT;
  3705. p_class->base_type.script_type = script;
  3706. p_class->base_type.native_type = script->get_instance_base_type();
  3707. } else if (native != StringName()) {
  3708. p_class->base_type.has_type = true;
  3709. p_class->base_type.kind = DataType::NATIVE;
  3710. p_class->base_type.native_type = native;
  3711. } else {
  3712. _set_error("Could not determine inheritance", p_class->line);
  3713. return;
  3714. }
  3715. } else {
  3716. // without extends, implicitly extend Reference
  3717. p_class->base_type.has_type = true;
  3718. p_class->base_type.kind = DataType::NATIVE;
  3719. p_class->base_type.native_type = "Reference";
  3720. }
  3721. // Recursively determine subclasses
  3722. for (int i = 0; i < p_class->subclasses.size(); i++) {
  3723. _determine_inheritance(p_class->subclasses[i]);
  3724. }
  3725. }
  3726. bool GDScriptParser::_parse_type(DataType &r_type, bool p_can_be_void) {
  3727. tokenizer->advance();
  3728. r_type.has_type = true;
  3729. switch (tokenizer->get_token()) {
  3730. case GDScriptTokenizer::TK_IDENTIFIER: {
  3731. StringName id = tokenizer->get_token_identifier();
  3732. } break;
  3733. case GDScriptTokenizer::TK_BUILT_IN_TYPE: {
  3734. } break;
  3735. case GDScriptTokenizer::TK_PR_VOID: {
  3736. if (!p_can_be_void) {
  3737. return false;
  3738. }
  3739. } break;
  3740. default: {
  3741. return false;
  3742. }
  3743. }
  3744. tokenizer->advance();
  3745. return true;
  3746. }
  3747. void GDScriptParser::_set_error(const String &p_error, int p_line, int p_column) {
  3748. if (error_set)
  3749. return; //allow no further errors
  3750. error = p_error;
  3751. error_line = p_line < 0 ? tokenizer->get_token_line() : p_line;
  3752. error_column = p_column < 0 ? tokenizer->get_token_column() : p_column;
  3753. error_set = true;
  3754. }
  3755. String GDScriptParser::get_error() const {
  3756. return error;
  3757. }
  3758. int GDScriptParser::get_error_line() const {
  3759. return error_line;
  3760. }
  3761. int GDScriptParser::get_error_column() const {
  3762. return error_column;
  3763. }
  3764. Error GDScriptParser::_parse(const String &p_base_path) {
  3765. base_path = p_base_path;
  3766. //assume class
  3767. ClassNode *main_class = alloc_node<ClassNode>();
  3768. main_class->initializer = alloc_node<BlockNode>();
  3769. main_class->initializer->parent_class = main_class;
  3770. main_class->ready = alloc_node<BlockNode>();
  3771. main_class->ready->parent_class = main_class;
  3772. current_class = main_class;
  3773. _parse_class(main_class);
  3774. if (tokenizer->get_token() == GDScriptTokenizer::TK_ERROR) {
  3775. error_set = false;
  3776. _set_error("Parse Error: " + tokenizer->get_token_error());
  3777. }
  3778. if (error_set) {
  3779. return ERR_PARSE_ERROR;
  3780. }
  3781. _determine_inheritance(main_class);
  3782. if (error_set) {
  3783. return ERR_PARSE_ERROR;
  3784. }
  3785. return OK;
  3786. }
  3787. Error GDScriptParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path, const String &p_self_path) {
  3788. clear();
  3789. self_path = p_self_path;
  3790. GDScriptTokenizerBuffer *tb = memnew(GDScriptTokenizerBuffer);
  3791. tb->set_code_buffer(p_bytecode);
  3792. tokenizer = tb;
  3793. Error ret = _parse(p_base_path);
  3794. memdelete(tb);
  3795. tokenizer = NULL;
  3796. return ret;
  3797. }
  3798. 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) {
  3799. clear();
  3800. self_path = p_self_path;
  3801. GDScriptTokenizerText *tt = memnew(GDScriptTokenizerText);
  3802. tt->set_code(p_code);
  3803. validating = p_just_validate;
  3804. for_completion = p_for_completion;
  3805. tokenizer = tt;
  3806. Error ret = _parse(p_base_path);
  3807. memdelete(tt);
  3808. tokenizer = NULL;
  3809. return ret;
  3810. }
  3811. bool GDScriptParser::is_tool_script() const {
  3812. return (head && head->type == Node::TYPE_CLASS && static_cast<const ClassNode *>(head)->tool);
  3813. }
  3814. const GDScriptParser::Node *GDScriptParser::get_parse_tree() const {
  3815. return head;
  3816. }
  3817. void GDScriptParser::clear() {
  3818. while (list) {
  3819. Node *l = list;
  3820. list = list->next;
  3821. memdelete(l);
  3822. }
  3823. head = NULL;
  3824. list = NULL;
  3825. completion_type = COMPLETION_NONE;
  3826. completion_node = NULL;
  3827. completion_class = NULL;
  3828. completion_function = NULL;
  3829. completion_block = NULL;
  3830. current_block = NULL;
  3831. current_class = NULL;
  3832. completion_found = false;
  3833. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  3834. current_function = NULL;
  3835. validating = false;
  3836. for_completion = false;
  3837. error_set = false;
  3838. tab_level.clear();
  3839. tab_level.push_back(0);
  3840. error_line = 0;
  3841. error_column = 0;
  3842. pending_newline = -1;
  3843. parenthesis = 0;
  3844. current_export.type = Variant::NIL;
  3845. error = "";
  3846. }
  3847. GDScriptParser::CompletionType GDScriptParser::get_completion_type() {
  3848. return completion_type;
  3849. }
  3850. StringName GDScriptParser::get_completion_cursor() {
  3851. return completion_cursor;
  3852. }
  3853. int GDScriptParser::get_completion_line() {
  3854. return completion_line;
  3855. }
  3856. Variant::Type GDScriptParser::get_completion_built_in_constant() {
  3857. return completion_built_in_constant;
  3858. }
  3859. GDScriptParser::Node *GDScriptParser::get_completion_node() {
  3860. return completion_node;
  3861. }
  3862. GDScriptParser::BlockNode *GDScriptParser::get_completion_block() {
  3863. return completion_block;
  3864. }
  3865. GDScriptParser::ClassNode *GDScriptParser::get_completion_class() {
  3866. return completion_class;
  3867. }
  3868. GDScriptParser::FunctionNode *GDScriptParser::get_completion_function() {
  3869. return completion_function;
  3870. }
  3871. int GDScriptParser::get_completion_argument_index() {
  3872. return completion_argument;
  3873. }
  3874. int GDScriptParser::get_completion_identifier_is_function() {
  3875. return completion_ident_is_call;
  3876. }
  3877. GDScriptParser::GDScriptParser() {
  3878. head = NULL;
  3879. list = NULL;
  3880. tokenizer = NULL;
  3881. pending_newline = -1;
  3882. clear();
  3883. }
  3884. GDScriptParser::~GDScriptParser() {
  3885. clear();
  3886. }