gd_parser.cpp 120 KB

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