gdscript_parser.cpp 133 KB

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