gd_parser.cpp 127 KB

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