gd_parser.cpp 69 KB

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