gd_parser.cpp 113 KB

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