gd_parser.cpp 97 KB

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