gd_parser.cpp 95 KB

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