gd_parser.cpp 65 KB

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