gd_parser.cpp 121 KB

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