gd_parser.cpp 126 KB

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