gd_parser.cpp 86 KB

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