gd_parser.cpp 74 KB

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