gd_parser.cpp 65 KB

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