gd_parser.cpp 66 KB

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