gd_parser.cpp 68 KB

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