gd_parser.cpp 70 KB

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