gdscript_parser.cpp 135 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591
  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 Operator */
  863. /******************/
  864. if (parenthesis > 0) {
  865. //remove empty space (only allowed if inside parenthesis
  866. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  867. tokenizer->advance();
  868. }
  869. }
  870. Expression e;
  871. e.is_op = false;
  872. e.node = expr;
  873. expression.push_back(e);
  874. // determine which operator is next
  875. OperatorNode::Operator op;
  876. bool valid = true;
  877. //assign, if allowed is only allowed on the first operator
  878. #define _VALIDATE_ASSIGN \
  879. if (!p_allow_assign) { \
  880. _set_error("Unexpected assign."); \
  881. return NULL; \
  882. } \
  883. p_allow_assign = false;
  884. switch (tokenizer->get_token()) { //see operator
  885. case GDScriptTokenizer::TK_OP_IN: op = OperatorNode::OP_IN; break;
  886. case GDScriptTokenizer::TK_OP_EQUAL: op = OperatorNode::OP_EQUAL; break;
  887. case GDScriptTokenizer::TK_OP_NOT_EQUAL: op = OperatorNode::OP_NOT_EQUAL; break;
  888. case GDScriptTokenizer::TK_OP_LESS: op = OperatorNode::OP_LESS; break;
  889. case GDScriptTokenizer::TK_OP_LESS_EQUAL: op = OperatorNode::OP_LESS_EQUAL; break;
  890. case GDScriptTokenizer::TK_OP_GREATER: op = OperatorNode::OP_GREATER; break;
  891. case GDScriptTokenizer::TK_OP_GREATER_EQUAL: op = OperatorNode::OP_GREATER_EQUAL; break;
  892. case GDScriptTokenizer::TK_OP_AND: op = OperatorNode::OP_AND; break;
  893. case GDScriptTokenizer::TK_OP_OR: op = OperatorNode::OP_OR; break;
  894. case GDScriptTokenizer::TK_OP_ADD: op = OperatorNode::OP_ADD; break;
  895. case GDScriptTokenizer::TK_OP_SUB: op = OperatorNode::OP_SUB; break;
  896. case GDScriptTokenizer::TK_OP_MUL: op = OperatorNode::OP_MUL; break;
  897. case GDScriptTokenizer::TK_OP_DIV: op = OperatorNode::OP_DIV; break;
  898. case GDScriptTokenizer::TK_OP_MOD:
  899. op = OperatorNode::OP_MOD;
  900. break;
  901. //case GDScriptTokenizer::TK_OP_NEG: op=OperatorNode::OP_NEG ; break;
  902. case GDScriptTokenizer::TK_OP_SHIFT_LEFT: op = OperatorNode::OP_SHIFT_LEFT; break;
  903. case GDScriptTokenizer::TK_OP_SHIFT_RIGHT: op = OperatorNode::OP_SHIFT_RIGHT; break;
  904. case GDScriptTokenizer::TK_OP_ASSIGN: {
  905. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN;
  906. if (tokenizer->get_token(1) == GDScriptTokenizer::TK_CURSOR) {
  907. //code complete assignment
  908. completion_type = COMPLETION_ASSIGN;
  909. completion_node = expr;
  910. completion_class = current_class;
  911. completion_function = current_function;
  912. completion_line = tokenizer->get_token_line();
  913. completion_block = current_block;
  914. completion_found = true;
  915. tokenizer->advance();
  916. }
  917. } break;
  918. case GDScriptTokenizer::TK_OP_ASSIGN_ADD: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_ADD; break;
  919. case GDScriptTokenizer::TK_OP_ASSIGN_SUB: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SUB; break;
  920. case GDScriptTokenizer::TK_OP_ASSIGN_MUL: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_MUL; break;
  921. case GDScriptTokenizer::TK_OP_ASSIGN_DIV: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_DIV; break;
  922. case GDScriptTokenizer::TK_OP_ASSIGN_MOD: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_MOD; break;
  923. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_LEFT: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SHIFT_LEFT; break;
  924. case GDScriptTokenizer::TK_OP_ASSIGN_SHIFT_RIGHT: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SHIFT_RIGHT; break;
  925. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_AND: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_AND; break;
  926. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_OR: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_OR; break;
  927. case GDScriptTokenizer::TK_OP_ASSIGN_BIT_XOR: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_XOR; break;
  928. case GDScriptTokenizer::TK_OP_BIT_AND: op = OperatorNode::OP_BIT_AND; break;
  929. case GDScriptTokenizer::TK_OP_BIT_OR: op = OperatorNode::OP_BIT_OR; break;
  930. case GDScriptTokenizer::TK_OP_BIT_XOR: op = OperatorNode::OP_BIT_XOR; break;
  931. case GDScriptTokenizer::TK_PR_IS: op = OperatorNode::OP_IS; break;
  932. case GDScriptTokenizer::TK_CF_IF: op = OperatorNode::OP_TERNARY_IF; break;
  933. case GDScriptTokenizer::TK_CF_ELSE: op = OperatorNode::OP_TERNARY_ELSE; break;
  934. default: valid = false; break;
  935. }
  936. if (valid) {
  937. e.is_op = true;
  938. e.op = op;
  939. expression.push_back(e);
  940. tokenizer->advance();
  941. } else {
  942. break;
  943. }
  944. }
  945. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  946. while (expression.size() > 1) {
  947. int next_op = -1;
  948. int min_priority = 0xFFFFF;
  949. bool is_unary = false;
  950. bool is_ternary = false;
  951. for (int i = 0; i < expression.size(); i++) {
  952. if (!expression[i].is_op) {
  953. continue;
  954. }
  955. int priority;
  956. bool unary = false;
  957. bool ternary = false;
  958. bool error = false;
  959. bool right_to_left = false;
  960. switch (expression[i].op) {
  961. case OperatorNode::OP_IS:
  962. priority = -1;
  963. break; //before anything
  964. case OperatorNode::OP_BIT_INVERT:
  965. priority = 0;
  966. unary = true;
  967. break;
  968. case OperatorNode::OP_NEG:
  969. priority = 1;
  970. unary = true;
  971. break;
  972. case OperatorNode::OP_POS:
  973. priority = 1;
  974. unary = true;
  975. break;
  976. case OperatorNode::OP_MUL: priority = 2; break;
  977. case OperatorNode::OP_DIV: priority = 2; break;
  978. case OperatorNode::OP_MOD: priority = 2; break;
  979. case OperatorNode::OP_ADD: priority = 3; break;
  980. case OperatorNode::OP_SUB: priority = 3; break;
  981. case OperatorNode::OP_SHIFT_LEFT: priority = 4; break;
  982. case OperatorNode::OP_SHIFT_RIGHT: priority = 4; break;
  983. case OperatorNode::OP_BIT_AND: priority = 5; break;
  984. case OperatorNode::OP_BIT_XOR: priority = 6; break;
  985. case OperatorNode::OP_BIT_OR: priority = 7; break;
  986. case OperatorNode::OP_LESS: priority = 8; break;
  987. case OperatorNode::OP_LESS_EQUAL: priority = 8; break;
  988. case OperatorNode::OP_GREATER: priority = 8; break;
  989. case OperatorNode::OP_GREATER_EQUAL: priority = 8; break;
  990. case OperatorNode::OP_EQUAL: priority = 8; break;
  991. case OperatorNode::OP_NOT_EQUAL: priority = 8; break;
  992. case OperatorNode::OP_IN: priority = 10; break;
  993. case OperatorNode::OP_NOT:
  994. priority = 11;
  995. unary = true;
  996. break;
  997. case OperatorNode::OP_AND: priority = 12; break;
  998. case OperatorNode::OP_OR: priority = 13; break;
  999. case OperatorNode::OP_TERNARY_IF:
  1000. priority = 14;
  1001. ternary = true;
  1002. right_to_left = true;
  1003. break;
  1004. case OperatorNode::OP_TERNARY_ELSE:
  1005. priority = 14;
  1006. error = true;
  1007. // Rigth-to-left should be false in this case, otherwise it would always error.
  1008. break;
  1009. case OperatorNode::OP_ASSIGN: priority = 15; break;
  1010. case OperatorNode::OP_ASSIGN_ADD: priority = 15; break;
  1011. case OperatorNode::OP_ASSIGN_SUB: priority = 15; break;
  1012. case OperatorNode::OP_ASSIGN_MUL: priority = 15; break;
  1013. case OperatorNode::OP_ASSIGN_DIV: priority = 15; break;
  1014. case OperatorNode::OP_ASSIGN_MOD: priority = 15; break;
  1015. case OperatorNode::OP_ASSIGN_SHIFT_LEFT: priority = 15; break;
  1016. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT: priority = 15; break;
  1017. case OperatorNode::OP_ASSIGN_BIT_AND: priority = 15; break;
  1018. case OperatorNode::OP_ASSIGN_BIT_OR: priority = 15; break;
  1019. case OperatorNode::OP_ASSIGN_BIT_XOR: priority = 15; break;
  1020. default: {
  1021. _set_error("GDScriptParser bug, invalid operator in expression: " + itos(expression[i].op));
  1022. return NULL;
  1023. }
  1024. }
  1025. if (priority < min_priority || (right_to_left && priority == min_priority)) {
  1026. // < is used for left to right (default)
  1027. // <= is used for right to left
  1028. if (error) {
  1029. _set_error("Unexpected operator");
  1030. return NULL;
  1031. }
  1032. next_op = i;
  1033. min_priority = priority;
  1034. is_unary = unary;
  1035. is_ternary = ternary;
  1036. }
  1037. }
  1038. if (next_op == -1) {
  1039. _set_error("Yet another parser bug....");
  1040. ERR_FAIL_COND_V(next_op == -1, NULL);
  1041. }
  1042. // OK! create operator..
  1043. if (is_unary) {
  1044. int expr_pos = next_op;
  1045. while (expression[expr_pos].is_op) {
  1046. expr_pos++;
  1047. if (expr_pos == expression.size()) {
  1048. //can happen..
  1049. _set_error("Unexpected end of expression...");
  1050. return NULL;
  1051. }
  1052. }
  1053. //consecutively do unary opeators
  1054. for (int i = expr_pos - 1; i >= next_op; i--) {
  1055. OperatorNode *op = alloc_node<OperatorNode>();
  1056. op->op = expression[i].op;
  1057. op->arguments.push_back(expression[i + 1].node);
  1058. op->line = op_line; //line might have been changed from a \n
  1059. expression[i].is_op = false;
  1060. expression[i].node = op;
  1061. expression.remove(i + 1);
  1062. }
  1063. } else if (is_ternary) {
  1064. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  1065. _set_error("Parser bug...");
  1066. ERR_FAIL_V(NULL);
  1067. }
  1068. if (next_op >= (expression.size() - 2) || expression[next_op + 2].op != OperatorNode::OP_TERNARY_ELSE) {
  1069. _set_error("Expected else after ternary if.");
  1070. ERR_FAIL_V(NULL);
  1071. }
  1072. if (next_op >= (expression.size() - 3)) {
  1073. _set_error("Expected value after ternary else.");
  1074. ERR_FAIL_V(NULL);
  1075. }
  1076. OperatorNode *op = alloc_node<OperatorNode>();
  1077. op->op = expression[next_op].op;
  1078. op->line = op_line; //line might have been changed from a \n
  1079. if (expression[next_op - 1].is_op) {
  1080. _set_error("Parser bug...");
  1081. ERR_FAIL_V(NULL);
  1082. }
  1083. if (expression[next_op + 1].is_op) {
  1084. // this is not invalid and can really appear
  1085. // but it becomes invalid anyway because no binary op
  1086. // can be followed by a unary op in a valid combination,
  1087. // due to how precedence works, unaries will always disappear first
  1088. _set_error("Unexpected two consecutive operators after ternary if.");
  1089. return NULL;
  1090. }
  1091. if (expression[next_op + 3].is_op) {
  1092. // this is not invalid and can really appear
  1093. // but it becomes invalid anyway because no binary op
  1094. // can be followed by a unary op in a valid combination,
  1095. // due to how precedence works, unaries will always disappear first
  1096. _set_error("Unexpected two consecutive operators after ternary else.");
  1097. return NULL;
  1098. }
  1099. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as first
  1100. op->arguments.push_back(expression[next_op - 1].node); //left expression goes as when-true
  1101. op->arguments.push_back(expression[next_op + 3].node); //expression after next goes as when-false
  1102. //replace all 3 nodes by this operator and make it an expression
  1103. expression[next_op - 1].node = op;
  1104. expression.remove(next_op);
  1105. expression.remove(next_op);
  1106. expression.remove(next_op);
  1107. expression.remove(next_op);
  1108. } else {
  1109. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  1110. _set_error("Parser bug...");
  1111. ERR_FAIL_V(NULL);
  1112. }
  1113. OperatorNode *op = alloc_node<OperatorNode>();
  1114. op->op = expression[next_op].op;
  1115. op->line = op_line; //line might have been changed from a \n
  1116. if (expression[next_op - 1].is_op) {
  1117. _set_error("Parser bug...");
  1118. ERR_FAIL_V(NULL);
  1119. }
  1120. if (expression[next_op + 1].is_op) {
  1121. // this is not invalid and can really appear
  1122. // but it becomes invalid anyway because no binary op
  1123. // can be followed by a unary op in a valid combination,
  1124. // due to how precedence works, unaries will always disappear first
  1125. _set_error("Unexpected two consecutive operators.");
  1126. return NULL;
  1127. }
  1128. op->arguments.push_back(expression[next_op - 1].node); //expression goes as left
  1129. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as right
  1130. //replace all 3 nodes by this operator and make it an expression
  1131. expression[next_op - 1].node = op;
  1132. expression.remove(next_op);
  1133. expression.remove(next_op);
  1134. }
  1135. }
  1136. return expression[0].node;
  1137. }
  1138. GDScriptParser::Node *GDScriptParser::_reduce_expression(Node *p_node, bool p_to_const) {
  1139. switch (p_node->type) {
  1140. case Node::TYPE_BUILT_IN_FUNCTION: {
  1141. //many may probably be optimizable
  1142. return p_node;
  1143. } break;
  1144. case Node::TYPE_ARRAY: {
  1145. ArrayNode *an = static_cast<ArrayNode *>(p_node);
  1146. bool all_constants = true;
  1147. for (int i = 0; i < an->elements.size(); i++) {
  1148. an->elements[i] = _reduce_expression(an->elements[i], p_to_const);
  1149. if (an->elements[i]->type != Node::TYPE_CONSTANT)
  1150. all_constants = false;
  1151. }
  1152. if (all_constants && p_to_const) {
  1153. //reduce constant array expression
  1154. ConstantNode *cn = alloc_node<ConstantNode>();
  1155. Array arr;
  1156. //print_line("mk array "+itos(!p_to_const));
  1157. arr.resize(an->elements.size());
  1158. for (int i = 0; i < an->elements.size(); i++) {
  1159. ConstantNode *acn = static_cast<ConstantNode *>(an->elements[i]);
  1160. arr[i] = acn->value;
  1161. }
  1162. cn->value = arr;
  1163. return cn;
  1164. }
  1165. return an;
  1166. } break;
  1167. case Node::TYPE_DICTIONARY: {
  1168. DictionaryNode *dn = static_cast<DictionaryNode *>(p_node);
  1169. bool all_constants = true;
  1170. for (int i = 0; i < dn->elements.size(); i++) {
  1171. dn->elements[i].key = _reduce_expression(dn->elements[i].key, p_to_const);
  1172. if (dn->elements[i].key->type != Node::TYPE_CONSTANT)
  1173. all_constants = false;
  1174. dn->elements[i].value = _reduce_expression(dn->elements[i].value, p_to_const);
  1175. if (dn->elements[i].value->type != Node::TYPE_CONSTANT)
  1176. all_constants = false;
  1177. }
  1178. if (all_constants && p_to_const) {
  1179. //reduce constant array expression
  1180. ConstantNode *cn = alloc_node<ConstantNode>();
  1181. Dictionary dict;
  1182. for (int i = 0; i < dn->elements.size(); i++) {
  1183. ConstantNode *key_c = static_cast<ConstantNode *>(dn->elements[i].key);
  1184. ConstantNode *value_c = static_cast<ConstantNode *>(dn->elements[i].value);
  1185. dict[key_c->value] = value_c->value;
  1186. }
  1187. cn->value = dict;
  1188. return cn;
  1189. }
  1190. return dn;
  1191. } break;
  1192. case Node::TYPE_OPERATOR: {
  1193. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  1194. bool all_constants = true;
  1195. int last_not_constant = -1;
  1196. for (int i = 0; i < op->arguments.size(); i++) {
  1197. op->arguments[i] = _reduce_expression(op->arguments[i], p_to_const);
  1198. if (op->arguments[i]->type != Node::TYPE_CONSTANT) {
  1199. all_constants = false;
  1200. last_not_constant = i;
  1201. }
  1202. }
  1203. if (op->op == OperatorNode::OP_IS) {
  1204. //nothing much
  1205. return op;
  1206. }
  1207. if (op->op == OperatorNode::OP_PARENT_CALL) {
  1208. //nothing much
  1209. return op;
  1210. } else if (op->op == OperatorNode::OP_CALL) {
  1211. //can reduce base type constructors
  1212. 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) {
  1213. //native type constructor or intrinsic function
  1214. const Variant **vptr = NULL;
  1215. Vector<Variant *> ptrs;
  1216. if (op->arguments.size() > 1) {
  1217. ptrs.resize(op->arguments.size() - 1);
  1218. for (int i = 0; i < ptrs.size(); i++) {
  1219. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i + 1]);
  1220. ptrs[i] = &cn->value;
  1221. }
  1222. vptr = (const Variant **)&ptrs[0];
  1223. }
  1224. Variant::CallError ce;
  1225. Variant v;
  1226. if (op->arguments[0]->type == Node::TYPE_TYPE) {
  1227. TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
  1228. v = Variant::construct(tn->vtype, vptr, ptrs.size(), ce);
  1229. } else {
  1230. GDScriptFunctions::Function func = static_cast<BuiltInFunctionNode *>(op->arguments[0])->function;
  1231. GDScriptFunctions::call(func, vptr, ptrs.size(), v, ce);
  1232. }
  1233. if (ce.error != Variant::CallError::CALL_OK) {
  1234. String errwhere;
  1235. if (op->arguments[0]->type == Node::TYPE_TYPE) {
  1236. TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
  1237. errwhere = "'" + Variant::get_type_name(tn->vtype) + "' constructor";
  1238. } else {
  1239. GDScriptFunctions::Function func = static_cast<BuiltInFunctionNode *>(op->arguments[0])->function;
  1240. errwhere = String("'") + GDScriptFunctions::get_func_name(func) + "' intrinsic function";
  1241. }
  1242. switch (ce.error) {
  1243. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1244. _set_error("Invalid argument (#" + itos(ce.argument + 1) + ") for " + errwhere + ".");
  1245. } break;
  1246. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1247. _set_error("Too many arguments for " + errwhere + ".");
  1248. } break;
  1249. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  1250. _set_error("Too few arguments for " + errwhere + ".");
  1251. } break;
  1252. default: {
  1253. _set_error("Invalid arguments for " + errwhere + ".");
  1254. } break;
  1255. }
  1256. error_line = op->line;
  1257. return p_node;
  1258. }
  1259. ConstantNode *cn = alloc_node<ConstantNode>();
  1260. cn->value = v;
  1261. return cn;
  1262. } else if (op->arguments[0]->type == Node::TYPE_BUILT_IN_FUNCTION && last_not_constant == 0) {
  1263. }
  1264. return op; //don't reduce yet
  1265. } else if (op->op == OperatorNode::OP_YIELD) {
  1266. return op;
  1267. } else if (op->op == OperatorNode::OP_INDEX) {
  1268. //can reduce indices into constant arrays or dictionaries
  1269. if (all_constants) {
  1270. ConstantNode *ca = static_cast<ConstantNode *>(op->arguments[0]);
  1271. ConstantNode *cb = static_cast<ConstantNode *>(op->arguments[1]);
  1272. bool valid;
  1273. Variant v = ca->value.get(cb->value, &valid);
  1274. if (!valid) {
  1275. _set_error("invalid index in constant expression");
  1276. error_line = op->line;
  1277. return op;
  1278. }
  1279. ConstantNode *cn = alloc_node<ConstantNode>();
  1280. cn->value = v;
  1281. return cn;
  1282. } /*else if (op->arguments[0]->type==Node::TYPE_CONSTANT && op->arguments[1]->type==Node::TYPE_IDENTIFIER) {
  1283. ConstantNode *ca = static_cast<ConstantNode*>(op->arguments[0]);
  1284. IdentifierNode *ib = static_cast<IdentifierNode*>(op->arguments[1]);
  1285. bool valid;
  1286. Variant v = ca->value.get_named(ib->name,&valid);
  1287. if (!valid) {
  1288. _set_error("invalid index '"+String(ib->name)+"' in constant expression");
  1289. return op;
  1290. }
  1291. ConstantNode *cn = alloc_node<ConstantNode>();
  1292. cn->value=v;
  1293. return cn;
  1294. }*/
  1295. return op;
  1296. } else if (op->op == OperatorNode::OP_INDEX_NAMED) {
  1297. if (op->arguments[0]->type == Node::TYPE_CONSTANT && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  1298. ConstantNode *ca = static_cast<ConstantNode *>(op->arguments[0]);
  1299. IdentifierNode *ib = static_cast<IdentifierNode *>(op->arguments[1]);
  1300. bool valid;
  1301. Variant v = ca->value.get_named(ib->name, &valid);
  1302. if (!valid) {
  1303. _set_error("invalid index '" + String(ib->name) + "' in constant expression");
  1304. error_line = op->line;
  1305. return op;
  1306. }
  1307. ConstantNode *cn = alloc_node<ConstantNode>();
  1308. cn->value = v;
  1309. return cn;
  1310. }
  1311. return op;
  1312. }
  1313. //validate assignment (don't assign to cosntant expression
  1314. switch (op->op) {
  1315. case OperatorNode::OP_ASSIGN:
  1316. case OperatorNode::OP_ASSIGN_ADD:
  1317. case OperatorNode::OP_ASSIGN_SUB:
  1318. case OperatorNode::OP_ASSIGN_MUL:
  1319. case OperatorNode::OP_ASSIGN_DIV:
  1320. case OperatorNode::OP_ASSIGN_MOD:
  1321. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  1322. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  1323. case OperatorNode::OP_ASSIGN_BIT_AND:
  1324. case OperatorNode::OP_ASSIGN_BIT_OR:
  1325. case OperatorNode::OP_ASSIGN_BIT_XOR: {
  1326. if (op->arguments[0]->type == Node::TYPE_CONSTANT) {
  1327. _set_error("Can't assign to constant", tokenizer->get_token_line() - 1);
  1328. error_line = op->line;
  1329. return op;
  1330. }
  1331. if (op->arguments[0]->type == Node::TYPE_OPERATOR) {
  1332. OperatorNode *on = static_cast<OperatorNode *>(op->arguments[0]);
  1333. if (on->op != OperatorNode::OP_INDEX && on->op != OperatorNode::OP_INDEX_NAMED) {
  1334. _set_error("Can't assign to an expression", tokenizer->get_token_line() - 1);
  1335. error_line = op->line;
  1336. return op;
  1337. }
  1338. }
  1339. } break;
  1340. default: { break; }
  1341. }
  1342. //now se if all are constants
  1343. if (!all_constants)
  1344. return op; //nothing to reduce from here on
  1345. #define _REDUCE_UNARY(m_vop) \
  1346. bool valid = false; \
  1347. Variant res; \
  1348. Variant::evaluate(m_vop, static_cast<ConstantNode *>(op->arguments[0])->value, Variant(), res, valid); \
  1349. if (!valid) { \
  1350. _set_error("Invalid operand for unary operator"); \
  1351. error_line = op->line; \
  1352. return p_node; \
  1353. } \
  1354. ConstantNode *cn = alloc_node<ConstantNode>(); \
  1355. cn->value = res; \
  1356. return cn;
  1357. #define _REDUCE_BINARY(m_vop) \
  1358. bool valid = false; \
  1359. Variant res; \
  1360. Variant::evaluate(m_vop, static_cast<ConstantNode *>(op->arguments[0])->value, static_cast<ConstantNode *>(op->arguments[1])->value, res, valid); \
  1361. if (!valid) { \
  1362. _set_error("Invalid operands for operator"); \
  1363. error_line = op->line; \
  1364. return p_node; \
  1365. } \
  1366. ConstantNode *cn = alloc_node<ConstantNode>(); \
  1367. cn->value = res; \
  1368. return cn;
  1369. switch (op->op) {
  1370. //unary operators
  1371. case OperatorNode::OP_NEG: {
  1372. _REDUCE_UNARY(Variant::OP_NEGATE);
  1373. } break;
  1374. case OperatorNode::OP_POS: {
  1375. _REDUCE_UNARY(Variant::OP_POSITIVE);
  1376. } break;
  1377. case OperatorNode::OP_NOT: {
  1378. _REDUCE_UNARY(Variant::OP_NOT);
  1379. } break;
  1380. case OperatorNode::OP_BIT_INVERT: {
  1381. _REDUCE_UNARY(Variant::OP_BIT_NEGATE);
  1382. } break;
  1383. //binary operators (in precedence order)
  1384. case OperatorNode::OP_IN: {
  1385. _REDUCE_BINARY(Variant::OP_IN);
  1386. } break;
  1387. case OperatorNode::OP_EQUAL: {
  1388. _REDUCE_BINARY(Variant::OP_EQUAL);
  1389. } break;
  1390. case OperatorNode::OP_NOT_EQUAL: {
  1391. _REDUCE_BINARY(Variant::OP_NOT_EQUAL);
  1392. } break;
  1393. case OperatorNode::OP_LESS: {
  1394. _REDUCE_BINARY(Variant::OP_LESS);
  1395. } break;
  1396. case OperatorNode::OP_LESS_EQUAL: {
  1397. _REDUCE_BINARY(Variant::OP_LESS_EQUAL);
  1398. } break;
  1399. case OperatorNode::OP_GREATER: {
  1400. _REDUCE_BINARY(Variant::OP_GREATER);
  1401. } break;
  1402. case OperatorNode::OP_GREATER_EQUAL: {
  1403. _REDUCE_BINARY(Variant::OP_GREATER_EQUAL);
  1404. } break;
  1405. case OperatorNode::OP_AND: {
  1406. _REDUCE_BINARY(Variant::OP_AND);
  1407. } break;
  1408. case OperatorNode::OP_OR: {
  1409. _REDUCE_BINARY(Variant::OP_OR);
  1410. } break;
  1411. case OperatorNode::OP_ADD: {
  1412. _REDUCE_BINARY(Variant::OP_ADD);
  1413. } break;
  1414. case OperatorNode::OP_SUB: {
  1415. _REDUCE_BINARY(Variant::OP_SUBTRACT);
  1416. } break;
  1417. case OperatorNode::OP_MUL: {
  1418. _REDUCE_BINARY(Variant::OP_MULTIPLY);
  1419. } break;
  1420. case OperatorNode::OP_DIV: {
  1421. _REDUCE_BINARY(Variant::OP_DIVIDE);
  1422. } break;
  1423. case OperatorNode::OP_MOD: {
  1424. _REDUCE_BINARY(Variant::OP_MODULE);
  1425. } break;
  1426. case OperatorNode::OP_SHIFT_LEFT: {
  1427. _REDUCE_BINARY(Variant::OP_SHIFT_LEFT);
  1428. } break;
  1429. case OperatorNode::OP_SHIFT_RIGHT: {
  1430. _REDUCE_BINARY(Variant::OP_SHIFT_RIGHT);
  1431. } break;
  1432. case OperatorNode::OP_BIT_AND: {
  1433. _REDUCE_BINARY(Variant::OP_BIT_AND);
  1434. } break;
  1435. case OperatorNode::OP_BIT_OR: {
  1436. _REDUCE_BINARY(Variant::OP_BIT_OR);
  1437. } break;
  1438. case OperatorNode::OP_BIT_XOR: {
  1439. _REDUCE_BINARY(Variant::OP_BIT_XOR);
  1440. } break;
  1441. default: { ERR_FAIL_V(op); }
  1442. }
  1443. ERR_FAIL_V(op);
  1444. } break;
  1445. default: {
  1446. return p_node;
  1447. } break;
  1448. }
  1449. }
  1450. GDScriptParser::Node *GDScriptParser::_parse_and_reduce_expression(Node *p_parent, bool p_static, bool p_reduce_const, bool p_allow_assign) {
  1451. Node *expr = _parse_expression(p_parent, p_static, p_allow_assign, p_reduce_const);
  1452. if (!expr || error_set)
  1453. return NULL;
  1454. expr = _reduce_expression(expr, p_reduce_const);
  1455. if (!expr || error_set)
  1456. return NULL;
  1457. return expr;
  1458. }
  1459. bool GDScriptParser::_recover_from_completion() {
  1460. if (!completion_found) {
  1461. return false; //can't recover if no completion
  1462. }
  1463. //skip stuff until newline
  1464. while (tokenizer->get_token() != GDScriptTokenizer::TK_NEWLINE && tokenizer->get_token() != GDScriptTokenizer::TK_EOF && tokenizer->get_token() != GDScriptTokenizer::TK_ERROR) {
  1465. tokenizer->advance();
  1466. }
  1467. completion_found = false;
  1468. error_set = false;
  1469. if (tokenizer->get_token() == GDScriptTokenizer::TK_ERROR) {
  1470. error_set = true;
  1471. }
  1472. return true;
  1473. }
  1474. GDScriptParser::PatternNode *GDScriptParser::_parse_pattern(bool p_static) {
  1475. PatternNode *pattern = alloc_node<PatternNode>();
  1476. GDScriptTokenizer::Token token = tokenizer->get_token();
  1477. if (error_set)
  1478. return NULL;
  1479. if (token == GDScriptTokenizer::TK_EOF) {
  1480. return NULL;
  1481. }
  1482. switch (token) {
  1483. // array
  1484. case GDScriptTokenizer::TK_BRACKET_OPEN: {
  1485. tokenizer->advance();
  1486. pattern->pt_type = GDScriptParser::PatternNode::PT_ARRAY;
  1487. while (true) {
  1488. if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1489. tokenizer->advance();
  1490. break;
  1491. }
  1492. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) {
  1493. // match everything
  1494. tokenizer->advance(2);
  1495. PatternNode *sub_pattern = alloc_node<PatternNode>();
  1496. sub_pattern->pt_type = GDScriptParser::PatternNode::PT_IGNORE_REST;
  1497. pattern->array.push_back(sub_pattern);
  1498. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA && tokenizer->get_token(1) == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1499. tokenizer->advance(2);
  1500. break;
  1501. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1502. tokenizer->advance(1);
  1503. break;
  1504. } else {
  1505. _set_error("'..' pattern only allowed at the end of an array pattern");
  1506. return NULL;
  1507. }
  1508. }
  1509. PatternNode *sub_pattern = _parse_pattern(p_static);
  1510. if (!sub_pattern) {
  1511. return NULL;
  1512. }
  1513. pattern->array.push_back(sub_pattern);
  1514. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  1515. tokenizer->advance();
  1516. continue;
  1517. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_BRACKET_CLOSE) {
  1518. tokenizer->advance();
  1519. break;
  1520. } else {
  1521. _set_error("Not a valid pattern");
  1522. return NULL;
  1523. }
  1524. }
  1525. } break;
  1526. // bind
  1527. case GDScriptTokenizer::TK_PR_VAR: {
  1528. tokenizer->advance();
  1529. pattern->pt_type = GDScriptParser::PatternNode::PT_BIND;
  1530. pattern->bind = tokenizer->get_token_identifier();
  1531. tokenizer->advance();
  1532. } break;
  1533. // dictionary
  1534. case GDScriptTokenizer::TK_CURLY_BRACKET_OPEN: {
  1535. tokenizer->advance();
  1536. pattern->pt_type = GDScriptParser::PatternNode::PT_DICTIONARY;
  1537. while (true) {
  1538. if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1539. tokenizer->advance();
  1540. break;
  1541. }
  1542. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) {
  1543. // match everything
  1544. tokenizer->advance(2);
  1545. PatternNode *sub_pattern = alloc_node<PatternNode>();
  1546. sub_pattern->pt_type = PatternNode::PT_IGNORE_REST;
  1547. pattern->array.push_back(sub_pattern);
  1548. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA && tokenizer->get_token(1) == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1549. tokenizer->advance(2);
  1550. break;
  1551. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1552. tokenizer->advance(1);
  1553. break;
  1554. } else {
  1555. _set_error("'..' pattern only allowed at the end of a dictionary pattern");
  1556. return NULL;
  1557. }
  1558. }
  1559. Node *key = _parse_and_reduce_expression(pattern, p_static);
  1560. if (!key) {
  1561. _set_error("Not a valid key in pattern");
  1562. return NULL;
  1563. }
  1564. if (key->type != GDScriptParser::Node::TYPE_CONSTANT) {
  1565. _set_error("Not a constant expression as key");
  1566. return NULL;
  1567. }
  1568. if (tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
  1569. tokenizer->advance();
  1570. PatternNode *value = _parse_pattern(p_static);
  1571. if (!value) {
  1572. _set_error("Expected pattern in dictionary value");
  1573. return NULL;
  1574. }
  1575. pattern->dictionary.insert(static_cast<ConstantNode *>(key), value);
  1576. } else {
  1577. pattern->dictionary.insert(static_cast<ConstantNode *>(key), NULL);
  1578. }
  1579. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  1580. tokenizer->advance();
  1581. continue;
  1582. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1583. tokenizer->advance();
  1584. break;
  1585. } else {
  1586. _set_error("Not a valid pattern");
  1587. return NULL;
  1588. }
  1589. }
  1590. } break;
  1591. case GDScriptTokenizer::TK_WILDCARD: {
  1592. tokenizer->advance();
  1593. pattern->pt_type = PatternNode::PT_WILDCARD;
  1594. } break;
  1595. // all the constants like strings and numbers
  1596. default: {
  1597. Node *value = _parse_and_reduce_expression(pattern, p_static);
  1598. if (!value) {
  1599. _set_error("Expect constant expression or variables in a pattern");
  1600. return NULL;
  1601. }
  1602. if (value->type == Node::TYPE_OPERATOR) {
  1603. // Maybe it's SomeEnum.VALUE
  1604. Node *current_value = value;
  1605. while (current_value->type == Node::TYPE_OPERATOR) {
  1606. OperatorNode *op_node = static_cast<OperatorNode *>(current_value);
  1607. if (op_node->op != OperatorNode::OP_INDEX_NAMED) {
  1608. _set_error("Invalid operator in pattern. Only index (`A.B`) is allowed");
  1609. return NULL;
  1610. }
  1611. current_value = op_node->arguments[0];
  1612. }
  1613. if (current_value->type != Node::TYPE_IDENTIFIER) {
  1614. _set_error("Only constant expression or variables allowed in a pattern");
  1615. return NULL;
  1616. }
  1617. } else if (value->type != Node::TYPE_IDENTIFIER && value->type != Node::TYPE_CONSTANT) {
  1618. _set_error("Only constant expressions or variables allowed in a pattern");
  1619. return NULL;
  1620. }
  1621. pattern->pt_type = PatternNode::PT_CONSTANT;
  1622. pattern->constant = value;
  1623. } break;
  1624. }
  1625. return pattern;
  1626. }
  1627. void GDScriptParser::_parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static) {
  1628. int indent_level = tab_level.back()->get();
  1629. while (true) {
  1630. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline())
  1631. ;
  1632. // GDScriptTokenizer::Token token = tokenizer->get_token();
  1633. if (error_set)
  1634. return;
  1635. if (indent_level > tab_level.back()->get()) {
  1636. return; // go back a level
  1637. }
  1638. if (pending_newline != -1) {
  1639. pending_newline = -1;
  1640. }
  1641. PatternBranchNode *branch = alloc_node<PatternBranchNode>();
  1642. branch->patterns.push_back(_parse_pattern(p_static));
  1643. if (!branch->patterns[0]) {
  1644. return;
  1645. }
  1646. while (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  1647. tokenizer->advance();
  1648. branch->patterns.push_back(_parse_pattern(p_static));
  1649. if (!branch->patterns[branch->patterns.size() - 1]) {
  1650. return;
  1651. }
  1652. }
  1653. if (!_enter_indent_block()) {
  1654. _set_error("Expected block in pattern branch");
  1655. return;
  1656. }
  1657. branch->body = alloc_node<BlockNode>();
  1658. branch->body->parent_block = p_block;
  1659. p_block->sub_blocks.push_back(branch->body);
  1660. current_block = branch->body;
  1661. _parse_block(branch->body, p_static);
  1662. current_block = p_block;
  1663. p_branches.push_back(branch);
  1664. }
  1665. }
  1666. void GDScriptParser::_generate_pattern(PatternNode *p_pattern, Node *p_node_to_match, Node *&p_resulting_node, Map<StringName, Node *> &p_bindings) {
  1667. switch (p_pattern->pt_type) {
  1668. case PatternNode::PT_CONSTANT: {
  1669. // typecheck
  1670. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1671. typeof_node->function = GDScriptFunctions::TYPE_OF;
  1672. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1673. typeof_match_value->op = OperatorNode::OP_CALL;
  1674. typeof_match_value->arguments.push_back(typeof_node);
  1675. typeof_match_value->arguments.push_back(p_node_to_match);
  1676. OperatorNode *typeof_pattern_value = alloc_node<OperatorNode>();
  1677. typeof_pattern_value->op = OperatorNode::OP_CALL;
  1678. typeof_pattern_value->arguments.push_back(typeof_node);
  1679. typeof_pattern_value->arguments.push_back(p_pattern->constant);
  1680. OperatorNode *type_comp = alloc_node<OperatorNode>();
  1681. type_comp->op = OperatorNode::OP_EQUAL;
  1682. type_comp->arguments.push_back(typeof_match_value);
  1683. type_comp->arguments.push_back(typeof_pattern_value);
  1684. // comare the actual values
  1685. OperatorNode *value_comp = alloc_node<OperatorNode>();
  1686. value_comp->op = OperatorNode::OP_EQUAL;
  1687. value_comp->arguments.push_back(p_pattern->constant);
  1688. value_comp->arguments.push_back(p_node_to_match);
  1689. OperatorNode *comparison = alloc_node<OperatorNode>();
  1690. comparison->op = OperatorNode::OP_AND;
  1691. comparison->arguments.push_back(type_comp);
  1692. comparison->arguments.push_back(value_comp);
  1693. p_resulting_node = comparison;
  1694. } break;
  1695. case PatternNode::PT_BIND: {
  1696. p_bindings[p_pattern->bind] = p_node_to_match;
  1697. // a bind always matches
  1698. ConstantNode *true_value = alloc_node<ConstantNode>();
  1699. true_value->value = Variant(true);
  1700. p_resulting_node = true_value;
  1701. } break;
  1702. case PatternNode::PT_ARRAY: {
  1703. bool open_ended = false;
  1704. if (p_pattern->array.size() > 0) {
  1705. if (p_pattern->array[p_pattern->array.size() - 1]->pt_type == PatternNode::PT_IGNORE_REST) {
  1706. open_ended = true;
  1707. }
  1708. }
  1709. // typeof(value_to_match) == TYPE_ARRAY && value_to_match.size() >= length
  1710. // typeof(value_to_match) == TYPE_ARRAY && value_to_match.size() == length
  1711. {
  1712. // typecheck
  1713. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1714. typeof_node->function = GDScriptFunctions::TYPE_OF;
  1715. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1716. typeof_match_value->op = OperatorNode::OP_CALL;
  1717. typeof_match_value->arguments.push_back(typeof_node);
  1718. typeof_match_value->arguments.push_back(p_node_to_match);
  1719. IdentifierNode *typeof_array = alloc_node<IdentifierNode>();
  1720. typeof_array->name = "TYPE_ARRAY";
  1721. OperatorNode *type_comp = alloc_node<OperatorNode>();
  1722. type_comp->op = OperatorNode::OP_EQUAL;
  1723. type_comp->arguments.push_back(typeof_match_value);
  1724. type_comp->arguments.push_back(typeof_array);
  1725. // size
  1726. ConstantNode *length = alloc_node<ConstantNode>();
  1727. length->value = Variant(open_ended ? p_pattern->array.size() - 1 : p_pattern->array.size());
  1728. OperatorNode *call = alloc_node<OperatorNode>();
  1729. call->op = OperatorNode::OP_CALL;
  1730. call->arguments.push_back(p_node_to_match);
  1731. IdentifierNode *size = alloc_node<IdentifierNode>();
  1732. size->name = "size";
  1733. call->arguments.push_back(size);
  1734. OperatorNode *length_comparison = alloc_node<OperatorNode>();
  1735. length_comparison->op = open_ended ? OperatorNode::OP_GREATER_EQUAL : OperatorNode::OP_EQUAL;
  1736. length_comparison->arguments.push_back(call);
  1737. length_comparison->arguments.push_back(length);
  1738. OperatorNode *type_and_length_comparison = alloc_node<OperatorNode>();
  1739. type_and_length_comparison->op = OperatorNode::OP_AND;
  1740. type_and_length_comparison->arguments.push_back(type_comp);
  1741. type_and_length_comparison->arguments.push_back(length_comparison);
  1742. p_resulting_node = type_and_length_comparison;
  1743. }
  1744. for (int i = 0; i < p_pattern->array.size(); i++) {
  1745. PatternNode *pattern = p_pattern->array[i];
  1746. Node *condition = NULL;
  1747. ConstantNode *index = alloc_node<ConstantNode>();
  1748. index->value = Variant(i);
  1749. OperatorNode *indexed_value = alloc_node<OperatorNode>();
  1750. indexed_value->op = OperatorNode::OP_INDEX;
  1751. indexed_value->arguments.push_back(p_node_to_match);
  1752. indexed_value->arguments.push_back(index);
  1753. _generate_pattern(pattern, indexed_value, condition, p_bindings);
  1754. // concatenate all the patterns with &&
  1755. OperatorNode *and_node = alloc_node<OperatorNode>();
  1756. and_node->op = OperatorNode::OP_AND;
  1757. and_node->arguments.push_back(p_resulting_node);
  1758. and_node->arguments.push_back(condition);
  1759. p_resulting_node = and_node;
  1760. }
  1761. } break;
  1762. case PatternNode::PT_DICTIONARY: {
  1763. bool open_ended = false;
  1764. if (p_pattern->array.size() > 0) {
  1765. open_ended = true;
  1766. }
  1767. // typeof(value_to_match) == TYPE_DICTIONARY && value_to_match.size() >= length
  1768. // typeof(value_to_match) == TYPE_DICTIONARY && value_to_match.size() == length
  1769. {
  1770. // typecheck
  1771. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1772. typeof_node->function = GDScriptFunctions::TYPE_OF;
  1773. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1774. typeof_match_value->op = OperatorNode::OP_CALL;
  1775. typeof_match_value->arguments.push_back(typeof_node);
  1776. typeof_match_value->arguments.push_back(p_node_to_match);
  1777. IdentifierNode *typeof_dictionary = alloc_node<IdentifierNode>();
  1778. typeof_dictionary->name = "TYPE_DICTIONARY";
  1779. OperatorNode *type_comp = alloc_node<OperatorNode>();
  1780. type_comp->op = OperatorNode::OP_EQUAL;
  1781. type_comp->arguments.push_back(typeof_match_value);
  1782. type_comp->arguments.push_back(typeof_dictionary);
  1783. // size
  1784. ConstantNode *length = alloc_node<ConstantNode>();
  1785. length->value = Variant(open_ended ? p_pattern->dictionary.size() - 1 : p_pattern->dictionary.size());
  1786. OperatorNode *call = alloc_node<OperatorNode>();
  1787. call->op = OperatorNode::OP_CALL;
  1788. call->arguments.push_back(p_node_to_match);
  1789. IdentifierNode *size = alloc_node<IdentifierNode>();
  1790. size->name = "size";
  1791. call->arguments.push_back(size);
  1792. OperatorNode *length_comparison = alloc_node<OperatorNode>();
  1793. length_comparison->op = open_ended ? OperatorNode::OP_GREATER_EQUAL : OperatorNode::OP_EQUAL;
  1794. length_comparison->arguments.push_back(call);
  1795. length_comparison->arguments.push_back(length);
  1796. OperatorNode *type_and_length_comparison = alloc_node<OperatorNode>();
  1797. type_and_length_comparison->op = OperatorNode::OP_AND;
  1798. type_and_length_comparison->arguments.push_back(type_comp);
  1799. type_and_length_comparison->arguments.push_back(length_comparison);
  1800. p_resulting_node = type_and_length_comparison;
  1801. }
  1802. for (Map<ConstantNode *, PatternNode *>::Element *e = p_pattern->dictionary.front(); e; e = e->next()) {
  1803. Node *condition = NULL;
  1804. // chech for has, then for pattern
  1805. IdentifierNode *has = alloc_node<IdentifierNode>();
  1806. has->name = "has";
  1807. OperatorNode *has_call = alloc_node<OperatorNode>();
  1808. has_call->op = OperatorNode::OP_CALL;
  1809. has_call->arguments.push_back(p_node_to_match);
  1810. has_call->arguments.push_back(has);
  1811. has_call->arguments.push_back(e->key());
  1812. if (e->value()) {
  1813. OperatorNode *indexed_value = alloc_node<OperatorNode>();
  1814. indexed_value->op = OperatorNode::OP_INDEX;
  1815. indexed_value->arguments.push_back(p_node_to_match);
  1816. indexed_value->arguments.push_back(e->key());
  1817. _generate_pattern(e->value(), indexed_value, condition, p_bindings);
  1818. OperatorNode *has_and_pattern = alloc_node<OperatorNode>();
  1819. has_and_pattern->op = OperatorNode::OP_AND;
  1820. has_and_pattern->arguments.push_back(has_call);
  1821. has_and_pattern->arguments.push_back(condition);
  1822. condition = has_and_pattern;
  1823. } else {
  1824. condition = has_call;
  1825. }
  1826. // concatenate all the patterns with &&
  1827. OperatorNode *and_node = alloc_node<OperatorNode>();
  1828. and_node->op = OperatorNode::OP_AND;
  1829. and_node->arguments.push_back(p_resulting_node);
  1830. and_node->arguments.push_back(condition);
  1831. p_resulting_node = and_node;
  1832. }
  1833. } break;
  1834. case PatternNode::PT_IGNORE_REST:
  1835. case PatternNode::PT_WILDCARD: {
  1836. // simply generate a `true`
  1837. ConstantNode *true_value = alloc_node<ConstantNode>();
  1838. true_value->value = Variant(true);
  1839. p_resulting_node = true_value;
  1840. } break;
  1841. default: {
  1842. } break;
  1843. }
  1844. }
  1845. void GDScriptParser::_transform_match_statment(BlockNode *p_block, MatchNode *p_match_statement) {
  1846. IdentifierNode *id = alloc_node<IdentifierNode>();
  1847. id->name = "#match_value";
  1848. for (int i = 0; i < p_match_statement->branches.size(); i++) {
  1849. PatternBranchNode *branch = p_match_statement->branches[i];
  1850. MatchNode::CompiledPatternBranch compiled_branch;
  1851. compiled_branch.compiled_pattern = NULL;
  1852. Map<StringName, Node *> binding;
  1853. for (int j = 0; j < branch->patterns.size(); j++) {
  1854. PatternNode *pattern = branch->patterns[j];
  1855. Map<StringName, Node *> bindings;
  1856. Node *resulting_node;
  1857. _generate_pattern(pattern, id, resulting_node, bindings);
  1858. if (!binding.empty() && !bindings.empty()) {
  1859. _set_error("Multipatterns can't contain bindings");
  1860. return;
  1861. } else {
  1862. binding = bindings;
  1863. }
  1864. if (compiled_branch.compiled_pattern) {
  1865. OperatorNode *or_node = alloc_node<OperatorNode>();
  1866. or_node->op = OperatorNode::OP_OR;
  1867. or_node->arguments.push_back(compiled_branch.compiled_pattern);
  1868. or_node->arguments.push_back(resulting_node);
  1869. compiled_branch.compiled_pattern = or_node;
  1870. } else {
  1871. // single pattern | first one
  1872. compiled_branch.compiled_pattern = resulting_node;
  1873. }
  1874. }
  1875. // prepare the body ...hehe
  1876. for (Map<StringName, Node *>::Element *e = binding.front(); e; e = e->next()) {
  1877. LocalVarNode *local_var = alloc_node<LocalVarNode>();
  1878. local_var->name = e->key();
  1879. local_var->assign = e->value();
  1880. IdentifierNode *id = alloc_node<IdentifierNode>();
  1881. id->name = local_var->name;
  1882. OperatorNode *op = alloc_node<OperatorNode>();
  1883. op->op = OperatorNode::OP_ASSIGN;
  1884. op->arguments.push_back(id);
  1885. op->arguments.push_back(local_var->assign);
  1886. branch->body->statements.push_front(op);
  1887. branch->body->statements.push_front(local_var);
  1888. }
  1889. compiled_branch.body = branch->body;
  1890. p_match_statement->compiled_pattern_branches.push_back(compiled_branch);
  1891. }
  1892. }
  1893. void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
  1894. int indent_level = tab_level.back()->get();
  1895. #ifdef DEBUG_ENABLED
  1896. NewLineNode *nl = alloc_node<NewLineNode>();
  1897. nl->line = tokenizer->get_token_line();
  1898. p_block->statements.push_back(nl);
  1899. #endif
  1900. bool is_first_line = true;
  1901. while (true) {
  1902. if (!is_first_line && tab_level.back()->prev() && tab_level.back()->prev()->get() == indent_level) {
  1903. // pythonic single-line expression, don't parse future lines
  1904. tab_level.pop_back();
  1905. p_block->end_line = tokenizer->get_token_line();
  1906. return;
  1907. }
  1908. is_first_line = false;
  1909. GDScriptTokenizer::Token token = tokenizer->get_token();
  1910. if (error_set)
  1911. return;
  1912. if (indent_level > tab_level.back()->get()) {
  1913. p_block->end_line = tokenizer->get_token_line();
  1914. return; //go back a level
  1915. }
  1916. if (pending_newline != -1) {
  1917. NewLineNode *nl = alloc_node<NewLineNode>();
  1918. nl->line = pending_newline;
  1919. p_block->statements.push_back(nl);
  1920. pending_newline = -1;
  1921. }
  1922. switch (token) {
  1923. case GDScriptTokenizer::TK_EOF:
  1924. p_block->end_line = tokenizer->get_token_line();
  1925. case GDScriptTokenizer::TK_ERROR: {
  1926. return; //go back
  1927. //end of file!
  1928. } break;
  1929. case GDScriptTokenizer::TK_NEWLINE: {
  1930. if (!_parse_newline()) {
  1931. if (!error_set) {
  1932. p_block->end_line = tokenizer->get_token_line();
  1933. pending_newline = p_block->end_line;
  1934. }
  1935. return;
  1936. }
  1937. NewLineNode *nl = alloc_node<NewLineNode>();
  1938. nl->line = tokenizer->get_token_line();
  1939. p_block->statements.push_back(nl);
  1940. } break;
  1941. case GDScriptTokenizer::TK_CF_PASS: {
  1942. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_SEMICOLON && tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE && tokenizer->get_token(1) != GDScriptTokenizer::TK_EOF) {
  1943. _set_error("Expected ';' or <NewLine>.");
  1944. return;
  1945. }
  1946. tokenizer->advance();
  1947. if (tokenizer->get_token() == GDScriptTokenizer::TK_SEMICOLON) {
  1948. // Ignore semicolon after 'pass'
  1949. tokenizer->advance();
  1950. }
  1951. } break;
  1952. case GDScriptTokenizer::TK_PR_VAR: {
  1953. //variale declaration and (eventual) initialization
  1954. tokenizer->advance();
  1955. if (!tokenizer->is_token_literal(0, true)) {
  1956. _set_error("Expected identifier for local variable name.");
  1957. return;
  1958. }
  1959. StringName n = tokenizer->get_token_literal();
  1960. tokenizer->advance();
  1961. if (current_function) {
  1962. for (int i = 0; i < current_function->arguments.size(); i++) {
  1963. if (n == current_function->arguments[i]) {
  1964. _set_error("Variable '" + String(n) + "' already defined in the scope (at line: " + itos(current_function->line) + ").");
  1965. return;
  1966. }
  1967. }
  1968. }
  1969. BlockNode *check_block = p_block;
  1970. while (check_block) {
  1971. for (int i = 0; i < check_block->variables.size(); i++) {
  1972. if (n == check_block->variables[i]) {
  1973. _set_error("Variable '" + String(n) + "' already defined in the scope (at line: " + itos(check_block->variable_lines[i]) + ").");
  1974. return;
  1975. }
  1976. }
  1977. check_block = check_block->parent_block;
  1978. }
  1979. int var_line = tokenizer->get_token_line();
  1980. //must know when the local variable is declared
  1981. LocalVarNode *lv = alloc_node<LocalVarNode>();
  1982. lv->name = n;
  1983. p_block->statements.push_back(lv);
  1984. Node *assigned = NULL;
  1985. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  1986. tokenizer->advance();
  1987. Node *subexpr = _parse_and_reduce_expression(p_block, p_static);
  1988. if (!subexpr) {
  1989. if (_recover_from_completion()) {
  1990. break;
  1991. }
  1992. return;
  1993. }
  1994. lv->assign = subexpr;
  1995. assigned = subexpr;
  1996. } else {
  1997. ConstantNode *c = alloc_node<ConstantNode>();
  1998. c->value = Variant();
  1999. assigned = c;
  2000. }
  2001. //must be added later, to avoid self-referencing.
  2002. p_block->variables.push_back(n); //line?
  2003. p_block->variable_lines.push_back(var_line);
  2004. IdentifierNode *id = alloc_node<IdentifierNode>();
  2005. id->name = n;
  2006. OperatorNode *op = alloc_node<OperatorNode>();
  2007. op->op = OperatorNode::OP_ASSIGN;
  2008. op->arguments.push_back(id);
  2009. op->arguments.push_back(assigned);
  2010. p_block->statements.push_back(op);
  2011. if (!_end_statement()) {
  2012. _set_error("Expected end of statement (var)");
  2013. return;
  2014. }
  2015. } break;
  2016. case GDScriptTokenizer::TK_CF_IF: {
  2017. tokenizer->advance();
  2018. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2019. if (!condition) {
  2020. if (_recover_from_completion()) {
  2021. break;
  2022. }
  2023. return;
  2024. }
  2025. ControlFlowNode *cf_if = alloc_node<ControlFlowNode>();
  2026. cf_if->cf_type = ControlFlowNode::CF_IF;
  2027. cf_if->arguments.push_back(condition);
  2028. cf_if->body = alloc_node<BlockNode>();
  2029. cf_if->body->parent_block = p_block;
  2030. cf_if->body->if_condition = condition; //helps code completion
  2031. p_block->sub_blocks.push_back(cf_if->body);
  2032. if (!_enter_indent_block(cf_if->body)) {
  2033. _set_error("Expected indented block after 'if'");
  2034. p_block->end_line = tokenizer->get_token_line();
  2035. return;
  2036. }
  2037. current_block = cf_if->body;
  2038. _parse_block(cf_if->body, p_static);
  2039. current_block = p_block;
  2040. if (error_set)
  2041. return;
  2042. p_block->statements.push_back(cf_if);
  2043. while (true) {
  2044. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE && _parse_newline())
  2045. ;
  2046. if (tab_level.back()->get() < indent_level) { //not at current indent level
  2047. p_block->end_line = tokenizer->get_token_line();
  2048. return;
  2049. }
  2050. if (tokenizer->get_token() == GDScriptTokenizer::TK_CF_ELIF) {
  2051. if (tab_level.back()->get() > indent_level) {
  2052. _set_error("Invalid indent");
  2053. return;
  2054. }
  2055. tokenizer->advance();
  2056. cf_if->body_else = alloc_node<BlockNode>();
  2057. cf_if->body_else->parent_block = p_block;
  2058. p_block->sub_blocks.push_back(cf_if->body_else);
  2059. ControlFlowNode *cf_else = alloc_node<ControlFlowNode>();
  2060. cf_else->cf_type = ControlFlowNode::CF_IF;
  2061. //condition
  2062. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2063. if (!condition) {
  2064. if (_recover_from_completion()) {
  2065. break;
  2066. }
  2067. return;
  2068. }
  2069. cf_else->arguments.push_back(condition);
  2070. cf_else->cf_type = ControlFlowNode::CF_IF;
  2071. cf_if->body_else->statements.push_back(cf_else);
  2072. cf_if = cf_else;
  2073. cf_if->body = alloc_node<BlockNode>();
  2074. cf_if->body->parent_block = p_block;
  2075. p_block->sub_blocks.push_back(cf_if->body);
  2076. if (!_enter_indent_block(cf_if->body)) {
  2077. _set_error("Expected indented block after 'elif'");
  2078. p_block->end_line = tokenizer->get_token_line();
  2079. return;
  2080. }
  2081. current_block = cf_else->body;
  2082. _parse_block(cf_else->body, p_static);
  2083. current_block = p_block;
  2084. if (error_set)
  2085. return;
  2086. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CF_ELSE) {
  2087. if (tab_level.back()->get() > indent_level) {
  2088. _set_error("Invalid indent");
  2089. return;
  2090. }
  2091. tokenizer->advance();
  2092. cf_if->body_else = alloc_node<BlockNode>();
  2093. cf_if->body_else->parent_block = p_block;
  2094. p_block->sub_blocks.push_back(cf_if->body_else);
  2095. if (!_enter_indent_block(cf_if->body_else)) {
  2096. _set_error("Expected indented block after 'else'");
  2097. p_block->end_line = tokenizer->get_token_line();
  2098. return;
  2099. }
  2100. current_block = cf_if->body_else;
  2101. _parse_block(cf_if->body_else, p_static);
  2102. current_block = p_block;
  2103. if (error_set)
  2104. return;
  2105. break; //after else, exit
  2106. } else
  2107. break;
  2108. }
  2109. } break;
  2110. case GDScriptTokenizer::TK_CF_WHILE: {
  2111. tokenizer->advance();
  2112. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2113. if (!condition) {
  2114. if (_recover_from_completion()) {
  2115. break;
  2116. }
  2117. return;
  2118. }
  2119. ControlFlowNode *cf_while = alloc_node<ControlFlowNode>();
  2120. cf_while->cf_type = ControlFlowNode::CF_WHILE;
  2121. cf_while->arguments.push_back(condition);
  2122. cf_while->body = alloc_node<BlockNode>();
  2123. cf_while->body->parent_block = p_block;
  2124. p_block->sub_blocks.push_back(cf_while->body);
  2125. if (!_enter_indent_block(cf_while->body)) {
  2126. _set_error("Expected indented block after 'while'");
  2127. p_block->end_line = tokenizer->get_token_line();
  2128. return;
  2129. }
  2130. current_block = cf_while->body;
  2131. _parse_block(cf_while->body, p_static);
  2132. current_block = p_block;
  2133. if (error_set)
  2134. return;
  2135. p_block->statements.push_back(cf_while);
  2136. } break;
  2137. case GDScriptTokenizer::TK_CF_FOR: {
  2138. tokenizer->advance();
  2139. if (!tokenizer->is_token_literal(0, true)) {
  2140. _set_error("identifier expected after 'for'");
  2141. }
  2142. IdentifierNode *id = alloc_node<IdentifierNode>();
  2143. id->name = tokenizer->get_token_identifier();
  2144. tokenizer->advance();
  2145. if (tokenizer->get_token() != GDScriptTokenizer::TK_OP_IN) {
  2146. _set_error("'in' expected after identifier");
  2147. return;
  2148. }
  2149. tokenizer->advance();
  2150. Node *container = _parse_and_reduce_expression(p_block, p_static);
  2151. if (!container) {
  2152. if (_recover_from_completion()) {
  2153. break;
  2154. }
  2155. return;
  2156. }
  2157. if (container->type == Node::TYPE_OPERATOR) {
  2158. OperatorNode *op = static_cast<OperatorNode *>(container);
  2159. 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) {
  2160. //iterating a range, so see if range() can be optimized without allocating memory, by replacing it by vectors (which can work as iterable too!)
  2161. Vector<Node *> args;
  2162. Vector<double> constants;
  2163. bool constant = false;
  2164. for (int i = 1; i < op->arguments.size(); i++) {
  2165. args.push_back(op->arguments[i]);
  2166. if (constant && op->arguments[i]->type == Node::TYPE_CONSTANT) {
  2167. ConstantNode *c = static_cast<ConstantNode *>(op->arguments[i]);
  2168. if (c->value.get_type() == Variant::REAL || c->value.get_type() == Variant::INT) {
  2169. constants.push_back(c->value);
  2170. constant = true;
  2171. }
  2172. } else {
  2173. constant = false;
  2174. }
  2175. }
  2176. if (args.size() > 0 && args.size() < 4) {
  2177. if (constant) {
  2178. ConstantNode *cn = alloc_node<ConstantNode>();
  2179. switch (args.size()) {
  2180. case 1: cn->value = (int)constants[0]; break;
  2181. case 2: cn->value = Vector2(constants[0], constants[1]); break;
  2182. case 3: cn->value = Vector3(constants[0], constants[1], constants[2]); break;
  2183. }
  2184. container = cn;
  2185. } else {
  2186. OperatorNode *on = alloc_node<OperatorNode>();
  2187. on->op = OperatorNode::OP_CALL;
  2188. TypeNode *tn = alloc_node<TypeNode>();
  2189. on->arguments.push_back(tn);
  2190. switch (args.size()) {
  2191. case 1: tn->vtype = Variant::INT; break;
  2192. case 2: tn->vtype = Variant::VECTOR2; break;
  2193. case 3: tn->vtype = Variant::VECTOR3; break;
  2194. }
  2195. for (int i = 0; i < args.size(); i++) {
  2196. on->arguments.push_back(args[i]);
  2197. }
  2198. container = on;
  2199. }
  2200. }
  2201. }
  2202. }
  2203. ControlFlowNode *cf_for = alloc_node<ControlFlowNode>();
  2204. cf_for->cf_type = ControlFlowNode::CF_FOR;
  2205. cf_for->arguments.push_back(id);
  2206. cf_for->arguments.push_back(container);
  2207. cf_for->body = alloc_node<BlockNode>();
  2208. cf_for->body->parent_block = p_block;
  2209. p_block->sub_blocks.push_back(cf_for->body);
  2210. if (!_enter_indent_block(cf_for->body)) {
  2211. _set_error("Expected indented block after 'for'");
  2212. p_block->end_line = tokenizer->get_token_line();
  2213. return;
  2214. }
  2215. current_block = cf_for->body;
  2216. // this is for checking variable for redefining
  2217. // inside this _parse_block
  2218. cf_for->body->variables.push_back(id->name);
  2219. cf_for->body->variable_lines.push_back(id->line);
  2220. _parse_block(cf_for->body, p_static);
  2221. cf_for->body->variables.remove(0);
  2222. cf_for->body->variable_lines.remove(0);
  2223. current_block = p_block;
  2224. if (error_set)
  2225. return;
  2226. p_block->statements.push_back(cf_for);
  2227. } break;
  2228. case GDScriptTokenizer::TK_CF_CONTINUE: {
  2229. tokenizer->advance();
  2230. ControlFlowNode *cf_continue = alloc_node<ControlFlowNode>();
  2231. cf_continue->cf_type = ControlFlowNode::CF_CONTINUE;
  2232. p_block->statements.push_back(cf_continue);
  2233. if (!_end_statement()) {
  2234. _set_error("Expected end of statement (continue)");
  2235. return;
  2236. }
  2237. } break;
  2238. case GDScriptTokenizer::TK_CF_BREAK: {
  2239. tokenizer->advance();
  2240. ControlFlowNode *cf_break = alloc_node<ControlFlowNode>();
  2241. cf_break->cf_type = ControlFlowNode::CF_BREAK;
  2242. p_block->statements.push_back(cf_break);
  2243. if (!_end_statement()) {
  2244. _set_error("Expected end of statement (break)");
  2245. return;
  2246. }
  2247. } break;
  2248. case GDScriptTokenizer::TK_CF_RETURN: {
  2249. tokenizer->advance();
  2250. ControlFlowNode *cf_return = alloc_node<ControlFlowNode>();
  2251. cf_return->cf_type = ControlFlowNode::CF_RETURN;
  2252. if (tokenizer->get_token() == GDScriptTokenizer::TK_SEMICOLON || tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE || tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  2253. //expect end of statement
  2254. p_block->statements.push_back(cf_return);
  2255. if (!_end_statement()) {
  2256. return;
  2257. }
  2258. } else {
  2259. //expect expression
  2260. Node *retexpr = _parse_and_reduce_expression(p_block, p_static);
  2261. if (!retexpr) {
  2262. if (_recover_from_completion()) {
  2263. break;
  2264. }
  2265. return;
  2266. }
  2267. cf_return->arguments.push_back(retexpr);
  2268. p_block->statements.push_back(cf_return);
  2269. if (!_end_statement()) {
  2270. _set_error("Expected end of statement after return expression.");
  2271. return;
  2272. }
  2273. }
  2274. } break;
  2275. case GDScriptTokenizer::TK_CF_MATCH: {
  2276. tokenizer->advance();
  2277. MatchNode *match_node = alloc_node<MatchNode>();
  2278. Node *val_to_match = _parse_and_reduce_expression(p_block, p_static);
  2279. if (!val_to_match) {
  2280. if (_recover_from_completion()) {
  2281. break;
  2282. }
  2283. return;
  2284. }
  2285. match_node->val_to_match = val_to_match;
  2286. if (!_enter_indent_block()) {
  2287. _set_error("Expected indented pattern matching block after 'match'");
  2288. return;
  2289. }
  2290. BlockNode *compiled_branches = alloc_node<BlockNode>();
  2291. compiled_branches->parent_block = p_block;
  2292. compiled_branches->parent_class = p_block->parent_class;
  2293. p_block->sub_blocks.push_back(compiled_branches);
  2294. _parse_pattern_block(compiled_branches, match_node->branches, p_static);
  2295. _transform_match_statment(compiled_branches, match_node);
  2296. ControlFlowNode *match_cf_node = alloc_node<ControlFlowNode>();
  2297. match_cf_node->cf_type = ControlFlowNode::CF_MATCH;
  2298. match_cf_node->match = match_node;
  2299. p_block->statements.push_back(match_cf_node);
  2300. _end_statement();
  2301. } break;
  2302. case GDScriptTokenizer::TK_PR_ASSERT: {
  2303. tokenizer->advance();
  2304. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2305. if (!condition) {
  2306. if (_recover_from_completion()) {
  2307. break;
  2308. }
  2309. return;
  2310. }
  2311. AssertNode *an = alloc_node<AssertNode>();
  2312. an->condition = condition;
  2313. p_block->statements.push_back(an);
  2314. if (!_end_statement()) {
  2315. _set_error("Expected end of statement after assert.");
  2316. return;
  2317. }
  2318. } break;
  2319. case GDScriptTokenizer::TK_PR_BREAKPOINT: {
  2320. tokenizer->advance();
  2321. BreakpointNode *bn = alloc_node<BreakpointNode>();
  2322. p_block->statements.push_back(bn);
  2323. if (!_end_statement()) {
  2324. _set_error("Expected end of statement after breakpoint.");
  2325. return;
  2326. }
  2327. } break;
  2328. default: {
  2329. Node *expression = _parse_and_reduce_expression(p_block, p_static, false, true);
  2330. if (!expression) {
  2331. if (_recover_from_completion()) {
  2332. break;
  2333. }
  2334. return;
  2335. }
  2336. p_block->statements.push_back(expression);
  2337. if (!_end_statement()) {
  2338. _set_error("Expected end of statement after expression.");
  2339. return;
  2340. }
  2341. } break;
  2342. /*
  2343. case GDScriptTokenizer::TK_CF_LOCAL: {
  2344. if (tokenizer->get_token(1)!=GDScriptTokenizer::TK_SEMICOLON && tokenizer->get_token(1)!=GDScriptTokenizer::TK_NEWLINE ) {
  2345. _set_error("Expected ';' or <NewLine>.");
  2346. }
  2347. tokenizer->advance();
  2348. } break;
  2349. */
  2350. }
  2351. }
  2352. }
  2353. bool GDScriptParser::_parse_newline() {
  2354. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_EOF && tokenizer->get_token(1) != GDScriptTokenizer::TK_NEWLINE) {
  2355. int indent = tokenizer->get_token_line_indent();
  2356. int current_indent = tab_level.back()->get();
  2357. if (indent > current_indent) {
  2358. _set_error("Unexpected indent.");
  2359. return false;
  2360. }
  2361. if (indent < current_indent) {
  2362. while (indent < current_indent) {
  2363. //exit block
  2364. if (tab_level.size() == 1) {
  2365. _set_error("Invalid indent. BUG?");
  2366. return false;
  2367. }
  2368. tab_level.pop_back();
  2369. if (tab_level.back()->get() < indent) {
  2370. _set_error("Unindent does not match any outer indentation level.");
  2371. return false;
  2372. }
  2373. current_indent = tab_level.back()->get();
  2374. }
  2375. tokenizer->advance();
  2376. return false;
  2377. }
  2378. }
  2379. tokenizer->advance();
  2380. return true;
  2381. }
  2382. void GDScriptParser::_parse_extends(ClassNode *p_class) {
  2383. if (p_class->extends_used) {
  2384. _set_error("'extends' already used for this class.");
  2385. return;
  2386. }
  2387. if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty()) {
  2388. _set_error("'extends' must be used before anything else.");
  2389. return;
  2390. }
  2391. p_class->extends_used = true;
  2392. tokenizer->advance();
  2393. if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token_type() == Variant::OBJECT) {
  2394. p_class->extends_class.push_back(Variant::get_type_name(Variant::OBJECT));
  2395. tokenizer->advance();
  2396. return;
  2397. }
  2398. // see if inheritance happens from a file
  2399. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT) {
  2400. Variant constant = tokenizer->get_token_constant();
  2401. if (constant.get_type() != Variant::STRING) {
  2402. _set_error("'extends' constant must be a string.");
  2403. return;
  2404. }
  2405. p_class->extends_file = constant;
  2406. tokenizer->advance();
  2407. if (tokenizer->get_token() != GDScriptTokenizer::TK_PERIOD) {
  2408. return;
  2409. } else
  2410. tokenizer->advance();
  2411. }
  2412. while (true) {
  2413. switch (tokenizer->get_token()) {
  2414. case GDScriptTokenizer::TK_IDENTIFIER: {
  2415. StringName identifier = tokenizer->get_token_identifier();
  2416. p_class->extends_class.push_back(identifier);
  2417. } break;
  2418. case GDScriptTokenizer::TK_PERIOD:
  2419. break;
  2420. default: {
  2421. _set_error("Invalid 'extends' syntax, expected string constant (path) and/or identifier (parent class).");
  2422. return;
  2423. }
  2424. }
  2425. tokenizer->advance(1);
  2426. switch (tokenizer->get_token()) {
  2427. case GDScriptTokenizer::TK_IDENTIFIER:
  2428. case GDScriptTokenizer::TK_PERIOD:
  2429. continue;
  2430. default:
  2431. return;
  2432. }
  2433. }
  2434. }
  2435. void GDScriptParser::_parse_class(ClassNode *p_class) {
  2436. int indent_level = tab_level.back()->get();
  2437. while (true) {
  2438. GDScriptTokenizer::Token token = tokenizer->get_token();
  2439. if (error_set)
  2440. return;
  2441. if (indent_level > tab_level.back()->get()) {
  2442. p_class->end_line = tokenizer->get_token_line();
  2443. return; //go back a level
  2444. }
  2445. switch (token) {
  2446. case GDScriptTokenizer::TK_EOF:
  2447. p_class->end_line = tokenizer->get_token_line();
  2448. case GDScriptTokenizer::TK_ERROR: {
  2449. return; //go back
  2450. //end of file!
  2451. } break;
  2452. case GDScriptTokenizer::TK_NEWLINE: {
  2453. if (!_parse_newline()) {
  2454. if (!error_set) {
  2455. p_class->end_line = tokenizer->get_token_line();
  2456. }
  2457. return;
  2458. }
  2459. } break;
  2460. case GDScriptTokenizer::TK_PR_EXTENDS: {
  2461. _parse_extends(p_class);
  2462. if (error_set)
  2463. return;
  2464. if (!_end_statement()) {
  2465. _set_error("Expected end of statement after extends");
  2466. return;
  2467. }
  2468. } break;
  2469. case GDScriptTokenizer::TK_PR_TOOL: {
  2470. if (p_class->tool) {
  2471. _set_error("tool used more than once");
  2472. return;
  2473. }
  2474. p_class->tool = true;
  2475. tokenizer->advance();
  2476. } break;
  2477. case GDScriptTokenizer::TK_PR_CLASS: {
  2478. //class inside class :D
  2479. StringName name;
  2480. StringName extends;
  2481. if (tokenizer->get_token(1) != GDScriptTokenizer::TK_IDENTIFIER) {
  2482. _set_error("'class' syntax: 'class <Name>:' or 'class <Name> extends <BaseClass>:'");
  2483. return;
  2484. }
  2485. name = tokenizer->get_token_identifier(1);
  2486. tokenizer->advance(2);
  2487. ClassNode *newclass = alloc_node<ClassNode>();
  2488. newclass->initializer = alloc_node<BlockNode>();
  2489. newclass->initializer->parent_class = newclass;
  2490. newclass->ready = alloc_node<BlockNode>();
  2491. newclass->ready->parent_class = newclass;
  2492. newclass->name = name;
  2493. newclass->owner = p_class;
  2494. p_class->subclasses.push_back(newclass);
  2495. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_EXTENDS) {
  2496. _parse_extends(newclass);
  2497. if (error_set)
  2498. return;
  2499. }
  2500. if (!_enter_indent_block()) {
  2501. _set_error("Indented block expected.");
  2502. return;
  2503. }
  2504. current_class = newclass;
  2505. _parse_class(newclass);
  2506. current_class = p_class;
  2507. } break;
  2508. /* this is for functions....
  2509. case GDScriptTokenizer::TK_CF_PASS: {
  2510. tokenizer->advance(1);
  2511. } break;
  2512. */
  2513. case GDScriptTokenizer::TK_PR_STATIC: {
  2514. tokenizer->advance();
  2515. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  2516. _set_error("Expected 'func'.");
  2517. return;
  2518. }
  2519. }; //fallthrough to function
  2520. case GDScriptTokenizer::TK_PR_FUNCTION: {
  2521. bool _static = false;
  2522. pending_newline = -1;
  2523. if (tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_STATIC) {
  2524. _static = true;
  2525. }
  2526. tokenizer->advance();
  2527. StringName name;
  2528. if (_get_completable_identifier(COMPLETION_VIRTUAL_FUNC, name)) {
  2529. }
  2530. if (name == StringName()) {
  2531. _set_error("Expected identifier after 'func' (syntax: 'func <identifier>([arguments]):' ).");
  2532. return;
  2533. }
  2534. for (int i = 0; i < p_class->functions.size(); i++) {
  2535. if (p_class->functions[i]->name == name) {
  2536. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->functions[i]->line) + ").");
  2537. }
  2538. }
  2539. for (int i = 0; i < p_class->static_functions.size(); i++) {
  2540. if (p_class->static_functions[i]->name == name) {
  2541. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->static_functions[i]->line) + ").");
  2542. }
  2543. }
  2544. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2545. _set_error("Expected '(' after identifier (syntax: 'func <identifier>([arguments]):' ).");
  2546. return;
  2547. }
  2548. tokenizer->advance();
  2549. Vector<StringName> arguments;
  2550. Vector<Node *> default_values;
  2551. int fnline = tokenizer->get_token_line();
  2552. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2553. //has arguments
  2554. bool defaulting = false;
  2555. while (true) {
  2556. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2557. tokenizer->advance();
  2558. continue;
  2559. }
  2560. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_VAR) {
  2561. tokenizer->advance(); //var before the identifier is allowed
  2562. }
  2563. if (!tokenizer->is_token_literal(0, true)) {
  2564. _set_error("Expected identifier for argument.");
  2565. return;
  2566. }
  2567. StringName argname = tokenizer->get_token_identifier();
  2568. arguments.push_back(argname);
  2569. tokenizer->advance();
  2570. if (defaulting && tokenizer->get_token() != GDScriptTokenizer::TK_OP_ASSIGN) {
  2571. _set_error("Default parameter expected.");
  2572. return;
  2573. }
  2574. //tokenizer->advance();
  2575. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  2576. defaulting = true;
  2577. tokenizer->advance(1);
  2578. Node *defval = _parse_and_reduce_expression(p_class, _static);
  2579. if (!defval || error_set)
  2580. return;
  2581. OperatorNode *on = alloc_node<OperatorNode>();
  2582. on->op = OperatorNode::OP_ASSIGN;
  2583. IdentifierNode *in = alloc_node<IdentifierNode>();
  2584. in->name = argname;
  2585. on->arguments.push_back(in);
  2586. on->arguments.push_back(defval);
  2587. /* no ..
  2588. if (defval->type!=Node::TYPE_CONSTANT) {
  2589. _set_error("default argument must be constant");
  2590. }
  2591. */
  2592. default_values.push_back(on);
  2593. }
  2594. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2595. tokenizer->advance();
  2596. }
  2597. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2598. tokenizer->advance();
  2599. continue;
  2600. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2601. _set_error("Expected ',' or ')'.");
  2602. return;
  2603. }
  2604. break;
  2605. }
  2606. }
  2607. tokenizer->advance();
  2608. BlockNode *block = alloc_node<BlockNode>();
  2609. block->parent_class = p_class;
  2610. if (name == "_init") {
  2611. if (p_class->extends_used) {
  2612. OperatorNode *cparent = alloc_node<OperatorNode>();
  2613. cparent->op = OperatorNode::OP_PARENT_CALL;
  2614. block->statements.push_back(cparent);
  2615. IdentifierNode *id = alloc_node<IdentifierNode>();
  2616. id->name = "_init";
  2617. cparent->arguments.push_back(id);
  2618. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
  2619. tokenizer->advance();
  2620. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2621. _set_error("expected '(' for parent constructor arguments.");
  2622. }
  2623. tokenizer->advance();
  2624. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2625. //has arguments
  2626. parenthesis++;
  2627. while (true) {
  2628. Node *arg = _parse_and_reduce_expression(p_class, _static);
  2629. cparent->arguments.push_back(arg);
  2630. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2631. tokenizer->advance();
  2632. continue;
  2633. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2634. _set_error("Expected ',' or ')'.");
  2635. return;
  2636. }
  2637. break;
  2638. }
  2639. parenthesis--;
  2640. }
  2641. tokenizer->advance();
  2642. }
  2643. } else {
  2644. if (tokenizer->get_token() == GDScriptTokenizer::TK_PERIOD) {
  2645. _set_error("Parent constructor call found for a class without inheritance.");
  2646. return;
  2647. }
  2648. }
  2649. }
  2650. if (!_enter_indent_block(block)) {
  2651. _set_error("Indented block expected.");
  2652. return;
  2653. }
  2654. FunctionNode *function = alloc_node<FunctionNode>();
  2655. function->name = name;
  2656. function->arguments = arguments;
  2657. function->default_values = default_values;
  2658. function->_static = _static;
  2659. function->line = fnline;
  2660. function->rpc_mode = rpc_mode;
  2661. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  2662. if (_static)
  2663. p_class->static_functions.push_back(function);
  2664. else
  2665. p_class->functions.push_back(function);
  2666. current_function = function;
  2667. function->body = block;
  2668. current_block = block;
  2669. _parse_block(block, _static);
  2670. current_block = NULL;
  2671. //arguments
  2672. } break;
  2673. case GDScriptTokenizer::TK_PR_SIGNAL: {
  2674. tokenizer->advance();
  2675. if (!tokenizer->is_token_literal()) {
  2676. _set_error("Expected identifier after 'signal'.");
  2677. return;
  2678. }
  2679. ClassNode::Signal sig;
  2680. sig.name = tokenizer->get_token_identifier();
  2681. tokenizer->advance();
  2682. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2683. tokenizer->advance();
  2684. while (true) {
  2685. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2686. tokenizer->advance();
  2687. continue;
  2688. }
  2689. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2690. tokenizer->advance();
  2691. break;
  2692. }
  2693. if (!tokenizer->is_token_literal(0, true)) {
  2694. _set_error("Expected identifier in signal argument.");
  2695. return;
  2696. }
  2697. sig.arguments.push_back(tokenizer->get_token_identifier());
  2698. tokenizer->advance();
  2699. while (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  2700. tokenizer->advance();
  2701. }
  2702. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2703. tokenizer->advance();
  2704. } else if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2705. _set_error("Expected ',' or ')' after signal parameter identifier.");
  2706. return;
  2707. }
  2708. }
  2709. }
  2710. p_class->_signals.push_back(sig);
  2711. if (!_end_statement()) {
  2712. _set_error("Expected end of statement (signal)");
  2713. return;
  2714. }
  2715. } break;
  2716. case GDScriptTokenizer::TK_PR_EXPORT: {
  2717. tokenizer->advance();
  2718. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_OPEN) {
  2719. tokenizer->advance();
  2720. String hint_prefix = "";
  2721. bool is_arrayed = false;
  2722. while (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE &&
  2723. tokenizer->get_token_type() == Variant::ARRAY &&
  2724. tokenizer->get_token(1) == GDScriptTokenizer::TK_COMMA) {
  2725. tokenizer->advance(); // Array
  2726. tokenizer->advance(); // Comma
  2727. if (is_arrayed) {
  2728. hint_prefix += itos(Variant::ARRAY) + ":";
  2729. } else {
  2730. is_arrayed = true;
  2731. }
  2732. }
  2733. if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE) {
  2734. Variant::Type type = tokenizer->get_token_type();
  2735. if (type == Variant::NIL) {
  2736. _set_error("Can't export null type.");
  2737. return;
  2738. }
  2739. if (type == Variant::OBJECT) {
  2740. _set_error("Can't export raw object type.");
  2741. return;
  2742. }
  2743. current_export.type = type;
  2744. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  2745. tokenizer->advance();
  2746. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2747. // hint expected next!
  2748. tokenizer->advance();
  2749. switch (type) {
  2750. case Variant::INT: {
  2751. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  2752. //current_export.hint=PROPERTY_HINT_ALL_FLAGS;
  2753. tokenizer->advance();
  2754. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2755. break;
  2756. }
  2757. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  2758. _set_error("Expected ')' or ',' in bit flags hint.");
  2759. return;
  2760. }
  2761. current_export.hint = PROPERTY_HINT_FLAGS;
  2762. tokenizer->advance();
  2763. bool first = true;
  2764. while (true) {
  2765. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2766. current_export = PropertyInfo();
  2767. _set_error("Expected a string constant in named bit flags hint.");
  2768. return;
  2769. }
  2770. String c = tokenizer->get_token_constant();
  2771. if (!first)
  2772. current_export.hint_string += ",";
  2773. else
  2774. first = false;
  2775. current_export.hint_string += c.xml_escape();
  2776. tokenizer->advance();
  2777. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  2778. break;
  2779. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  2780. current_export = PropertyInfo();
  2781. _set_error("Expected ')' or ',' in named bit flags hint.");
  2782. return;
  2783. }
  2784. tokenizer->advance();
  2785. }
  2786. break;
  2787. }
  2788. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  2789. //enumeration
  2790. current_export.hint = PROPERTY_HINT_ENUM;
  2791. bool first = true;
  2792. while (true) {
  2793. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2794. current_export = PropertyInfo();
  2795. _set_error("Expected a string constant in enumeration hint.");
  2796. return;
  2797. }
  2798. String c = tokenizer->get_token_constant();
  2799. if (!first)
  2800. current_export.hint_string += ",";
  2801. else
  2802. first = false;
  2803. current_export.hint_string += c.xml_escape();
  2804. tokenizer->advance();
  2805. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  2806. break;
  2807. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  2808. current_export = PropertyInfo();
  2809. _set_error("Expected ')' or ',' in enumeration hint.");
  2810. return;
  2811. }
  2812. tokenizer->advance();
  2813. }
  2814. break;
  2815. }
  2816. }; //fallthrough to use the same
  2817. case Variant::REAL: {
  2818. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EASE") {
  2819. current_export.hint = PROPERTY_HINT_EXP_EASING;
  2820. tokenizer->advance();
  2821. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2822. _set_error("Expected ')' in hint.");
  2823. return;
  2824. }
  2825. break;
  2826. }
  2827. // range
  2828. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EXP") {
  2829. current_export.hint = PROPERTY_HINT_EXP_RANGE;
  2830. tokenizer->advance();
  2831. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  2832. break;
  2833. else if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  2834. _set_error("Expected ')' or ',' in exponential range hint.");
  2835. return;
  2836. }
  2837. tokenizer->advance();
  2838. } else
  2839. current_export.hint = PROPERTY_HINT_RANGE;
  2840. float sign = 1.0;
  2841. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  2842. sign = -1;
  2843. tokenizer->advance();
  2844. }
  2845. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2846. current_export = PropertyInfo();
  2847. _set_error("Expected a range in numeric hint.");
  2848. return;
  2849. }
  2850. current_export.hint_string = rtos(sign * double(tokenizer->get_token_constant()));
  2851. tokenizer->advance();
  2852. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2853. current_export.hint_string = "0," + current_export.hint_string;
  2854. break;
  2855. }
  2856. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  2857. current_export = PropertyInfo();
  2858. _set_error("Expected ',' or ')' in numeric range hint.");
  2859. return;
  2860. }
  2861. tokenizer->advance();
  2862. sign = 1.0;
  2863. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  2864. sign = -1;
  2865. tokenizer->advance();
  2866. }
  2867. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2868. current_export = PropertyInfo();
  2869. _set_error("Expected a number as upper bound in numeric range hint.");
  2870. return;
  2871. }
  2872. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  2873. tokenizer->advance();
  2874. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  2875. break;
  2876. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  2877. current_export = PropertyInfo();
  2878. _set_error("Expected ',' or ')' in numeric range hint.");
  2879. return;
  2880. }
  2881. tokenizer->advance();
  2882. sign = 1.0;
  2883. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_SUB) {
  2884. sign = -1;
  2885. tokenizer->advance();
  2886. }
  2887. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2888. current_export = PropertyInfo();
  2889. _set_error("Expected a number as step in numeric range hint.");
  2890. return;
  2891. }
  2892. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  2893. tokenizer->advance();
  2894. } break;
  2895. case Variant::STRING: {
  2896. if (tokenizer->get_token() == GDScriptTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  2897. //enumeration
  2898. current_export.hint = PROPERTY_HINT_ENUM;
  2899. bool first = true;
  2900. while (true) {
  2901. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2902. current_export = PropertyInfo();
  2903. _set_error("Expected a string constant in enumeration hint.");
  2904. return;
  2905. }
  2906. String c = tokenizer->get_token_constant();
  2907. if (!first)
  2908. current_export.hint_string += ",";
  2909. else
  2910. first = false;
  2911. current_export.hint_string += c.xml_escape();
  2912. tokenizer->advance();
  2913. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  2914. break;
  2915. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  2916. current_export = PropertyInfo();
  2917. _set_error("Expected ')' or ',' in enumeration hint.");
  2918. return;
  2919. }
  2920. tokenizer->advance();
  2921. }
  2922. break;
  2923. }
  2924. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "DIR") {
  2925. tokenizer->advance();
  2926. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  2927. current_export.hint = PROPERTY_HINT_DIR;
  2928. else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2929. tokenizer->advance();
  2930. if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier() == "GLOBAL")) {
  2931. _set_error("Expected 'GLOBAL' after comma in directory hint.");
  2932. return;
  2933. }
  2934. if (!p_class->tool) {
  2935. _set_error("Global filesystem hints may only be used in tool scripts.");
  2936. return;
  2937. }
  2938. current_export.hint = PROPERTY_HINT_GLOBAL_DIR;
  2939. tokenizer->advance();
  2940. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2941. _set_error("Expected ')' in hint.");
  2942. return;
  2943. }
  2944. } else {
  2945. _set_error("Expected ')' or ',' in hint.");
  2946. return;
  2947. }
  2948. break;
  2949. }
  2950. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FILE") {
  2951. current_export.hint = PROPERTY_HINT_FILE;
  2952. tokenizer->advance();
  2953. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  2954. tokenizer->advance();
  2955. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "GLOBAL") {
  2956. if (!p_class->tool) {
  2957. _set_error("Global filesystem hints may only be used in tool scripts.");
  2958. return;
  2959. }
  2960. current_export.hint = PROPERTY_HINT_GLOBAL_FILE;
  2961. tokenizer->advance();
  2962. if (tokenizer->get_token() == GDScriptTokenizer::TK_PARENTHESIS_CLOSE)
  2963. break;
  2964. else if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA)
  2965. tokenizer->advance();
  2966. else {
  2967. _set_error("Expected ')' or ',' in hint.");
  2968. return;
  2969. }
  2970. }
  2971. if (tokenizer->get_token() != GDScriptTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2972. if (current_export.hint == PROPERTY_HINT_GLOBAL_FILE)
  2973. _set_error("Expected string constant with filter");
  2974. else
  2975. _set_error("Expected 'GLOBAL' or string constant with filter");
  2976. return;
  2977. }
  2978. current_export.hint_string = tokenizer->get_token_constant();
  2979. tokenizer->advance();
  2980. }
  2981. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2982. _set_error("Expected ')' in hint.");
  2983. return;
  2984. }
  2985. break;
  2986. }
  2987. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "MULTILINE") {
  2988. current_export.hint = PROPERTY_HINT_MULTILINE_TEXT;
  2989. tokenizer->advance();
  2990. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  2991. _set_error("Expected ')' in hint.");
  2992. return;
  2993. }
  2994. break;
  2995. }
  2996. } break;
  2997. case Variant::COLOR: {
  2998. if (tokenizer->get_token() != GDScriptTokenizer::TK_IDENTIFIER) {
  2999. current_export = PropertyInfo();
  3000. _set_error("Color type hint expects RGB or RGBA as hints");
  3001. return;
  3002. }
  3003. String identifier = tokenizer->get_token_identifier();
  3004. if (identifier == "RGB") {
  3005. current_export.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  3006. } else if (identifier == "RGBA") {
  3007. //none
  3008. } else {
  3009. current_export = PropertyInfo();
  3010. _set_error("Color type hint expects RGB or RGBA as hints");
  3011. return;
  3012. }
  3013. tokenizer->advance();
  3014. } break;
  3015. default: {
  3016. current_export = PropertyInfo();
  3017. _set_error("Type '" + Variant::get_type_name(type) + "' can't take hints.");
  3018. return;
  3019. } break;
  3020. }
  3021. }
  3022. } else {
  3023. parenthesis++;
  3024. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3025. if (!subexpr) {
  3026. if (_recover_from_completion()) {
  3027. break;
  3028. }
  3029. return;
  3030. }
  3031. parenthesis--;
  3032. if (subexpr->type != Node::TYPE_CONSTANT) {
  3033. current_export = PropertyInfo();
  3034. _set_error("Expected a constant expression.");
  3035. }
  3036. Variant constant = static_cast<ConstantNode *>(subexpr)->value;
  3037. if (constant.get_type() == Variant::OBJECT) {
  3038. GDScriptNativeClass *native_class = Object::cast_to<GDScriptNativeClass>(constant);
  3039. if (native_class && ClassDB::is_parent_class(native_class->get_name(), "Resource")) {
  3040. current_export.type = Variant::OBJECT;
  3041. current_export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3042. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3043. current_export.hint_string = native_class->get_name();
  3044. } else {
  3045. current_export = PropertyInfo();
  3046. _set_error("Export hint not a resource type.");
  3047. }
  3048. } else if (constant.get_type() == Variant::DICTIONARY) {
  3049. // Enumeration
  3050. bool is_flags = false;
  3051. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3052. tokenizer->advance();
  3053. if (tokenizer->get_token() == GDScriptTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  3054. is_flags = true;
  3055. tokenizer->advance();
  3056. } else {
  3057. current_export = PropertyInfo();
  3058. _set_error("Expected 'FLAGS' after comma.");
  3059. }
  3060. }
  3061. current_export.type = Variant::INT;
  3062. current_export.hint = is_flags ? PROPERTY_HINT_FLAGS : PROPERTY_HINT_ENUM;
  3063. Dictionary enum_values = constant;
  3064. List<Variant> keys;
  3065. enum_values.get_key_list(&keys);
  3066. bool first = true;
  3067. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  3068. if (enum_values[E->get()].get_type() == Variant::INT) {
  3069. if (!first)
  3070. current_export.hint_string += ",";
  3071. else
  3072. first = false;
  3073. current_export.hint_string += E->get().operator String().camelcase_to_underscore(true).capitalize().xml_escape();
  3074. if (!is_flags) {
  3075. current_export.hint_string += ":";
  3076. current_export.hint_string += enum_values[E->get()].operator String().xml_escape();
  3077. }
  3078. }
  3079. }
  3080. } else {
  3081. current_export = PropertyInfo();
  3082. _set_error("Expected type for export.");
  3083. return;
  3084. }
  3085. }
  3086. if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
  3087. current_export = PropertyInfo();
  3088. _set_error("Expected ')' or ',' after export hint.");
  3089. return;
  3090. }
  3091. if (is_arrayed) {
  3092. hint_prefix += itos(current_export.type);
  3093. if (current_export.hint) {
  3094. hint_prefix += "/" + itos(current_export.hint);
  3095. }
  3096. current_export.hint_string = hint_prefix + ":" + current_export.hint_string;
  3097. current_export.hint = PROPERTY_HINT_TYPE_STRING;
  3098. current_export.type = Variant::ARRAY;
  3099. }
  3100. tokenizer->advance();
  3101. }
  3102. 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) {
  3103. current_export = PropertyInfo();
  3104. _set_error("Expected 'var', 'onready', 'remote', 'master', 'slave', 'sync', 'remotesync', 'mastersync', 'slavesync'.");
  3105. return;
  3106. }
  3107. continue;
  3108. } break;
  3109. case GDScriptTokenizer::TK_PR_ONREADY: {
  3110. //may be fallthrough from export, ignore if so
  3111. tokenizer->advance();
  3112. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3113. _set_error("Expected 'var'.");
  3114. return;
  3115. }
  3116. continue;
  3117. } break;
  3118. case GDScriptTokenizer::TK_PR_REMOTE: {
  3119. //may be fallthrough from export, ignore if so
  3120. tokenizer->advance();
  3121. if (current_export.type) {
  3122. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3123. _set_error("Expected 'var'.");
  3124. return;
  3125. }
  3126. } else {
  3127. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3128. _set_error("Expected 'var' or 'func'.");
  3129. return;
  3130. }
  3131. }
  3132. rpc_mode = MultiplayerAPI::RPC_MODE_REMOTE;
  3133. continue;
  3134. } break;
  3135. case GDScriptTokenizer::TK_PR_MASTER: {
  3136. //may be fallthrough from export, ignore if so
  3137. tokenizer->advance();
  3138. if (current_export.type) {
  3139. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3140. _set_error("Expected 'var'.");
  3141. return;
  3142. }
  3143. } else {
  3144. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3145. _set_error("Expected 'var' or 'func'.");
  3146. return;
  3147. }
  3148. }
  3149. rpc_mode = MultiplayerAPI::RPC_MODE_MASTER;
  3150. continue;
  3151. } break;
  3152. case GDScriptTokenizer::TK_PR_SLAVE: {
  3153. //may be fallthrough from export, ignore if so
  3154. tokenizer->advance();
  3155. if (current_export.type) {
  3156. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR) {
  3157. _set_error("Expected 'var'.");
  3158. return;
  3159. }
  3160. } else {
  3161. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3162. _set_error("Expected 'var' or 'func'.");
  3163. return;
  3164. }
  3165. }
  3166. rpc_mode = MultiplayerAPI::RPC_MODE_SLAVE;
  3167. continue;
  3168. } break;
  3169. case GDScriptTokenizer::TK_PR_REMOTESYNC:
  3170. case GDScriptTokenizer::TK_PR_SYNC: {
  3171. //may be fallthrough from export, ignore if so
  3172. tokenizer->advance();
  3173. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3174. if (current_export.type)
  3175. _set_error("Expected 'var'.");
  3176. else
  3177. _set_error("Expected 'var' or 'func'.");
  3178. return;
  3179. }
  3180. rpc_mode = MultiplayerAPI::RPC_MODE_SYNC;
  3181. continue;
  3182. } break;
  3183. case GDScriptTokenizer::TK_PR_MASTERSYNC: {
  3184. //may be fallthrough from export, ignore if so
  3185. tokenizer->advance();
  3186. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3187. if (current_export.type)
  3188. _set_error("Expected 'var'.");
  3189. else
  3190. _set_error("Expected 'var' or 'func'.");
  3191. return;
  3192. }
  3193. rpc_mode = MultiplayerAPI::RPC_MODE_MASTERSYNC;
  3194. continue;
  3195. } break;
  3196. case GDScriptTokenizer::TK_PR_SLAVESYNC: {
  3197. //may be fallthrough from export, ignore if so
  3198. tokenizer->advance();
  3199. if (tokenizer->get_token() != GDScriptTokenizer::TK_PR_VAR && tokenizer->get_token() != GDScriptTokenizer::TK_PR_FUNCTION) {
  3200. if (current_export.type)
  3201. _set_error("Expected 'var'.");
  3202. else
  3203. _set_error("Expected 'var' or 'func'.");
  3204. return;
  3205. }
  3206. rpc_mode = MultiplayerAPI::RPC_MODE_SLAVESYNC;
  3207. continue;
  3208. } break;
  3209. case GDScriptTokenizer::TK_PR_VAR: {
  3210. //variale declaration and (eventual) initialization
  3211. ClassNode::Member member;
  3212. bool autoexport = tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_EXPORT;
  3213. if (current_export.type != Variant::NIL) {
  3214. member._export = current_export;
  3215. current_export = PropertyInfo();
  3216. }
  3217. bool onready = tokenizer->get_token(-1) == GDScriptTokenizer::TK_PR_ONREADY;
  3218. tokenizer->advance();
  3219. if (!tokenizer->is_token_literal(0, true)) {
  3220. _set_error("Expected identifier for member variable name.");
  3221. return;
  3222. }
  3223. member.identifier = tokenizer->get_token_literal();
  3224. member.expression = NULL;
  3225. member._export.name = member.identifier;
  3226. member.line = tokenizer->get_token_line();
  3227. member.rpc_mode = rpc_mode;
  3228. tokenizer->advance();
  3229. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  3230. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  3231. #ifdef DEBUG_ENABLED
  3232. int line = tokenizer->get_token_line();
  3233. #endif
  3234. tokenizer->advance();
  3235. Node *subexpr = _parse_and_reduce_expression(p_class, false, autoexport || member._export.type != Variant::NIL);
  3236. if (!subexpr) {
  3237. if (_recover_from_completion()) {
  3238. break;
  3239. }
  3240. return;
  3241. }
  3242. //discourage common error
  3243. if (!onready && subexpr->type == Node::TYPE_OPERATOR) {
  3244. OperatorNode *op = static_cast<OperatorNode *>(subexpr);
  3245. if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_SELF && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  3246. IdentifierNode *id = static_cast<IdentifierNode *>(op->arguments[1]);
  3247. if (id->name == "get_node") {
  3248. _set_error("Use 'onready var " + String(member.identifier) + " = get_node(..)' instead");
  3249. return;
  3250. }
  3251. }
  3252. }
  3253. member.expression = subexpr;
  3254. if (autoexport) {
  3255. if (1) /*(subexpr->type==Node::TYPE_ARRAY) {
  3256. member._export.type=Variant::ARRAY;
  3257. } else if (subexpr->type==Node::TYPE_DICTIONARY) {
  3258. member._export.type=Variant::DICTIONARY;
  3259. } else*/
  3260. {
  3261. if (subexpr->type != Node::TYPE_CONSTANT) {
  3262. _set_error("Type-less export needs a constant expression assigned to infer type.");
  3263. return;
  3264. }
  3265. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  3266. if (cn->value.get_type() == Variant::NIL) {
  3267. _set_error("Can't accept a null constant expression for inferring export type.");
  3268. return;
  3269. }
  3270. member._export.type = cn->value.get_type();
  3271. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3272. if (cn->value.get_type() == Variant::OBJECT) {
  3273. Object *obj = cn->value;
  3274. Resource *res = Object::cast_to<Resource>(obj);
  3275. if (res == NULL) {
  3276. _set_error("Exported constant not a type or resource.");
  3277. return;
  3278. }
  3279. member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3280. member._export.hint_string = res->get_class();
  3281. }
  3282. }
  3283. }
  3284. #ifdef TOOLS_ENABLED
  3285. if (subexpr->type == Node::TYPE_CONSTANT && member._export.type != Variant::NIL) {
  3286. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  3287. if (cn->value.get_type() != Variant::NIL) {
  3288. member.default_value = cn->value;
  3289. }
  3290. }
  3291. #endif
  3292. IdentifierNode *id = alloc_node<IdentifierNode>();
  3293. id->name = member.identifier;
  3294. OperatorNode *op = alloc_node<OperatorNode>();
  3295. op->op = OperatorNode::OP_INIT_ASSIGN;
  3296. op->arguments.push_back(id);
  3297. op->arguments.push_back(subexpr);
  3298. #ifdef DEBUG_ENABLED
  3299. NewLineNode *nl = alloc_node<NewLineNode>();
  3300. nl->line = line;
  3301. if (onready)
  3302. p_class->ready->statements.push_back(nl);
  3303. else
  3304. p_class->initializer->statements.push_back(nl);
  3305. #endif
  3306. if (onready)
  3307. p_class->ready->statements.push_back(op);
  3308. else
  3309. p_class->initializer->statements.push_back(op);
  3310. } else {
  3311. if (autoexport) {
  3312. _set_error("Type-less export needs a constant expression assigned to infer type.");
  3313. return;
  3314. }
  3315. }
  3316. if (tokenizer->get_token() == GDScriptTokenizer::TK_PR_SETGET) {
  3317. tokenizer->advance();
  3318. if (tokenizer->get_token() != GDScriptTokenizer::TK_COMMA) {
  3319. //just comma means using only getter
  3320. if (!tokenizer->is_token_literal()) {
  3321. _set_error("Expected identifier for setter function after 'setget'.");
  3322. }
  3323. member.setter = tokenizer->get_token_literal();
  3324. tokenizer->advance();
  3325. }
  3326. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3327. //there is a getter
  3328. tokenizer->advance();
  3329. if (!tokenizer->is_token_literal()) {
  3330. _set_error("Expected identifier for getter function after ','.");
  3331. }
  3332. member.getter = tokenizer->get_token_literal();
  3333. tokenizer->advance();
  3334. }
  3335. }
  3336. p_class->variables.push_back(member);
  3337. if (!_end_statement()) {
  3338. _set_error("Expected end of statement (continue)");
  3339. return;
  3340. }
  3341. } break;
  3342. case GDScriptTokenizer::TK_PR_CONST: {
  3343. //variale declaration and (eventual) initialization
  3344. ClassNode::Constant constant;
  3345. tokenizer->advance();
  3346. if (!tokenizer->is_token_literal(0, true)) {
  3347. _set_error("Expected name (identifier) for constant.");
  3348. return;
  3349. }
  3350. constant.identifier = tokenizer->get_token_literal();
  3351. tokenizer->advance();
  3352. if (tokenizer->get_token() != GDScriptTokenizer::TK_OP_ASSIGN) {
  3353. _set_error("Constant expects assignment.");
  3354. return;
  3355. }
  3356. tokenizer->advance();
  3357. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3358. if (!subexpr) {
  3359. if (_recover_from_completion()) {
  3360. break;
  3361. }
  3362. return;
  3363. }
  3364. if (subexpr->type != Node::TYPE_CONSTANT) {
  3365. _set_error("Expected constant expression");
  3366. }
  3367. constant.expression = subexpr;
  3368. p_class->constant_expressions.push_back(constant);
  3369. if (!_end_statement()) {
  3370. _set_error("Expected end of statement (constant)");
  3371. return;
  3372. }
  3373. } break;
  3374. case GDScriptTokenizer::TK_PR_ENUM: {
  3375. //multiple constant declarations..
  3376. int last_assign = -1; // Incremented by 1 right before the assingment.
  3377. String enum_name;
  3378. Dictionary enum_dict;
  3379. tokenizer->advance();
  3380. if (tokenizer->is_token_literal(0, true)) {
  3381. enum_name = tokenizer->get_token_literal();
  3382. tokenizer->advance();
  3383. }
  3384. if (tokenizer->get_token() != GDScriptTokenizer::TK_CURLY_BRACKET_OPEN) {
  3385. _set_error("Expected '{' in enum declaration");
  3386. return;
  3387. }
  3388. tokenizer->advance();
  3389. while (true) {
  3390. if (tokenizer->get_token() == GDScriptTokenizer::TK_NEWLINE) {
  3391. tokenizer->advance(); // Ignore newlines
  3392. } else if (tokenizer->get_token() == GDScriptTokenizer::TK_CURLY_BRACKET_CLOSE) {
  3393. tokenizer->advance();
  3394. break; // End of enum
  3395. } else if (!tokenizer->is_token_literal(0, true)) {
  3396. if (tokenizer->get_token() == GDScriptTokenizer::TK_EOF) {
  3397. _set_error("Unexpected end of file.");
  3398. } else {
  3399. _set_error(String("Unexpected ") + GDScriptTokenizer::get_token_name(tokenizer->get_token()) + ", expected identifier");
  3400. }
  3401. return;
  3402. } else { // tokenizer->is_token_literal(0, true)
  3403. ClassNode::Constant constant;
  3404. constant.identifier = tokenizer->get_token_literal();
  3405. tokenizer->advance();
  3406. if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
  3407. tokenizer->advance();
  3408. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3409. if (!subexpr) {
  3410. if (_recover_from_completion()) {
  3411. break;
  3412. }
  3413. return;
  3414. }
  3415. if (subexpr->type != Node::TYPE_CONSTANT) {
  3416. _set_error("Expected constant expression");
  3417. }
  3418. const ConstantNode *subexpr_const = static_cast<const ConstantNode *>(subexpr);
  3419. if (subexpr_const->value.get_type() != Variant::INT) {
  3420. _set_error("Expected an int value for enum");
  3421. }
  3422. last_assign = subexpr_const->value;
  3423. constant.expression = subexpr;
  3424. } else {
  3425. last_assign = last_assign + 1;
  3426. ConstantNode *cn = alloc_node<ConstantNode>();
  3427. cn->value = last_assign;
  3428. constant.expression = cn;
  3429. }
  3430. if (tokenizer->get_token() == GDScriptTokenizer::TK_COMMA) {
  3431. tokenizer->advance();
  3432. }
  3433. if (enum_name != "") {
  3434. const ConstantNode *cn = static_cast<const ConstantNode *>(constant.expression);
  3435. enum_dict[constant.identifier] = cn->value;
  3436. }
  3437. p_class->constant_expressions.push_back(constant);
  3438. }
  3439. }
  3440. if (enum_name != "") {
  3441. ClassNode::Constant enum_constant;
  3442. enum_constant.identifier = enum_name;
  3443. ConstantNode *cn = alloc_node<ConstantNode>();
  3444. cn->value = enum_dict;
  3445. enum_constant.expression = cn;
  3446. p_class->constant_expressions.push_back(enum_constant);
  3447. }
  3448. if (!_end_statement()) {
  3449. _set_error("Expected end of statement (enum)");
  3450. return;
  3451. }
  3452. } break;
  3453. case GDScriptTokenizer::TK_CONSTANT: {
  3454. if (tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3455. tokenizer->advance();
  3456. // Ignore
  3457. } else {
  3458. _set_error(String() + "Unexpected constant of type: " + Variant::get_type_name(tokenizer->get_token_constant().get_type()));
  3459. return;
  3460. }
  3461. } break;
  3462. default: {
  3463. _set_error(String() + "Unexpected token: " + tokenizer->get_token_name(tokenizer->get_token()) + ":" + tokenizer->get_token_identifier());
  3464. return;
  3465. } break;
  3466. }
  3467. }
  3468. }
  3469. void GDScriptParser::_set_error(const String &p_error, int p_line, int p_column) {
  3470. if (error_set)
  3471. return; //allow no further errors
  3472. error = p_error;
  3473. error_line = p_line < 0 ? tokenizer->get_token_line() : p_line;
  3474. error_column = p_column < 0 ? tokenizer->get_token_column() : p_column;
  3475. error_set = true;
  3476. }
  3477. String GDScriptParser::get_error() const {
  3478. return error;
  3479. }
  3480. int GDScriptParser::get_error_line() const {
  3481. return error_line;
  3482. }
  3483. int GDScriptParser::get_error_column() const {
  3484. return error_column;
  3485. }
  3486. Error GDScriptParser::_parse(const String &p_base_path) {
  3487. base_path = p_base_path;
  3488. //assume class
  3489. ClassNode *main_class = alloc_node<ClassNode>();
  3490. main_class->initializer = alloc_node<BlockNode>();
  3491. main_class->initializer->parent_class = main_class;
  3492. main_class->ready = alloc_node<BlockNode>();
  3493. main_class->ready->parent_class = main_class;
  3494. current_class = main_class;
  3495. _parse_class(main_class);
  3496. if (tokenizer->get_token() == GDScriptTokenizer::TK_ERROR) {
  3497. error_set = false;
  3498. _set_error("Parse Error: " + tokenizer->get_token_error());
  3499. }
  3500. if (error_set) {
  3501. return ERR_PARSE_ERROR;
  3502. }
  3503. return OK;
  3504. }
  3505. Error GDScriptParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path, const String &p_self_path) {
  3506. clear();
  3507. self_path = p_self_path;
  3508. GDScriptTokenizerBuffer *tb = memnew(GDScriptTokenizerBuffer);
  3509. tb->set_code_buffer(p_bytecode);
  3510. tokenizer = tb;
  3511. Error ret = _parse(p_base_path);
  3512. memdelete(tb);
  3513. tokenizer = NULL;
  3514. return ret;
  3515. }
  3516. 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) {
  3517. clear();
  3518. self_path = p_self_path;
  3519. GDScriptTokenizerText *tt = memnew(GDScriptTokenizerText);
  3520. tt->set_code(p_code);
  3521. validating = p_just_validate;
  3522. for_completion = p_for_completion;
  3523. tokenizer = tt;
  3524. Error ret = _parse(p_base_path);
  3525. memdelete(tt);
  3526. tokenizer = NULL;
  3527. return ret;
  3528. }
  3529. bool GDScriptParser::is_tool_script() const {
  3530. return (head && head->type == Node::TYPE_CLASS && static_cast<const ClassNode *>(head)->tool);
  3531. }
  3532. const GDScriptParser::Node *GDScriptParser::get_parse_tree() const {
  3533. return head;
  3534. }
  3535. void GDScriptParser::clear() {
  3536. while (list) {
  3537. Node *l = list;
  3538. list = list->next;
  3539. memdelete(l);
  3540. }
  3541. head = NULL;
  3542. list = NULL;
  3543. completion_type = COMPLETION_NONE;
  3544. completion_node = NULL;
  3545. completion_class = NULL;
  3546. completion_function = NULL;
  3547. completion_block = NULL;
  3548. current_block = NULL;
  3549. current_class = NULL;
  3550. completion_found = false;
  3551. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  3552. current_function = NULL;
  3553. validating = false;
  3554. for_completion = false;
  3555. error_set = false;
  3556. tab_level.clear();
  3557. tab_level.push_back(0);
  3558. error_line = 0;
  3559. error_column = 0;
  3560. pending_newline = -1;
  3561. parenthesis = 0;
  3562. current_export.type = Variant::NIL;
  3563. error = "";
  3564. }
  3565. GDScriptParser::CompletionType GDScriptParser::get_completion_type() {
  3566. return completion_type;
  3567. }
  3568. StringName GDScriptParser::get_completion_cursor() {
  3569. return completion_cursor;
  3570. }
  3571. int GDScriptParser::get_completion_line() {
  3572. return completion_line;
  3573. }
  3574. Variant::Type GDScriptParser::get_completion_built_in_constant() {
  3575. return completion_built_in_constant;
  3576. }
  3577. GDScriptParser::Node *GDScriptParser::get_completion_node() {
  3578. return completion_node;
  3579. }
  3580. GDScriptParser::BlockNode *GDScriptParser::get_completion_block() {
  3581. return completion_block;
  3582. }
  3583. GDScriptParser::ClassNode *GDScriptParser::get_completion_class() {
  3584. return completion_class;
  3585. }
  3586. GDScriptParser::FunctionNode *GDScriptParser::get_completion_function() {
  3587. return completion_function;
  3588. }
  3589. int GDScriptParser::get_completion_argument_index() {
  3590. return completion_argument;
  3591. }
  3592. int GDScriptParser::get_completion_identifier_is_function() {
  3593. return completion_ident_is_call;
  3594. }
  3595. GDScriptParser::GDScriptParser() {
  3596. head = NULL;
  3597. list = NULL;
  3598. tokenizer = NULL;
  3599. pending_newline = -1;
  3600. clear();
  3601. }
  3602. GDScriptParser::~GDScriptParser() {
  3603. clear();
  3604. }