gd_parser.cpp 95 KB

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