gdscript_parser.cpp 133 KB

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