gdscript_parser.cpp 132 KB

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