2
0

gd_parser.cpp 70 KB

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