gdscript_parser.cpp 133 KB

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