gd_parser.cpp 125 KB

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