gd_parser.cpp 79 KB

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