gd_parser.cpp 66 KB

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