gd_parser.cpp 126 KB

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