gd_parser.cpp 87 KB

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