gd_parser.cpp 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330
  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-2016 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. #include "script_language.h"
  34. #include "gd_script.h"
  35. template<class T>
  36. T* GDParser::alloc_node() {
  37. T *t = memnew( T);
  38. t->next=list;
  39. list=t;
  40. if (!head)
  41. head=t;
  42. t->line=tokenizer->get_token_line();
  43. t->column=tokenizer->get_token_column();
  44. return t;
  45. }
  46. bool GDParser::_end_statement() {
  47. if (tokenizer->get_token()==GDTokenizer::TK_SEMICOLON) {
  48. tokenizer->advance();
  49. return true; //handle next
  50. } else if (tokenizer->get_token()==GDTokenizer::TK_NEWLINE || tokenizer->get_token()==GDTokenizer::TK_EOF) {
  51. return true; //will be handled properly
  52. }
  53. return false;
  54. }
  55. bool GDParser::_enter_indent_block(BlockNode* p_block) {
  56. if (tokenizer->get_token()!=GDTokenizer::TK_COLON) {
  57. // report location at the previous token (on the previous line)
  58. int error_line = tokenizer->get_token_line(-1);
  59. int error_column = tokenizer->get_token_column(-1);
  60. _set_error("':' expected at end of line.",error_line,error_column);
  61. return false;
  62. }
  63. tokenizer->advance();
  64. if (tokenizer->get_token()!=GDTokenizer::TK_NEWLINE) {
  65. // be more python-like
  66. int current = tab_level.back()->get();
  67. tab_level.push_back(current+1);
  68. return true;
  69. //_set_error("newline expected after ':'.");
  70. //return false;
  71. }
  72. while(true) {
  73. if (tokenizer->get_token()!=GDTokenizer::TK_NEWLINE) {
  74. return false; //wtf
  75. } else if (tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE) {
  76. int indent = tokenizer->get_token_line_indent();
  77. int current = tab_level.back()->get();
  78. if (indent<=current) {
  79. print_line("current: "+itos(current)+" indent: "+itos(indent));
  80. print_line("less than current");
  81. return false;
  82. }
  83. tab_level.push_back(indent);
  84. tokenizer->advance();
  85. return true;
  86. } else if (p_block) {
  87. NewLineNode *nl = alloc_node<NewLineNode>();
  88. nl->line=tokenizer->get_token_line();
  89. p_block->statements.push_back(nl);
  90. }
  91. tokenizer->advance(); // go to next newline
  92. }
  93. }
  94. bool GDParser::_parse_arguments(Node* p_parent,Vector<Node*>& p_args,bool p_static,bool p_can_codecomplete) {
  95. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  96. tokenizer->advance();
  97. } else {
  98. parenthesis ++;
  99. int argidx=0;
  100. while(true) {
  101. if (tokenizer->get_token()==GDTokenizer::TK_CURSOR) {
  102. _make_completable_call(argidx);
  103. completion_node=p_parent;
  104. } else if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type()==Variant::STRING && tokenizer->get_token(1)==GDTokenizer::TK_CURSOR) {
  105. //completing a string argument..
  106. completion_cursor=tokenizer->get_token_constant();
  107. _make_completable_call(argidx);
  108. completion_node=p_parent;
  109. tokenizer->advance(1);
  110. return false;
  111. }
  112. Node*arg = _parse_expression(p_parent,p_static);
  113. if (!arg)
  114. return false;
  115. p_args.push_back(arg);
  116. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  117. tokenizer->advance();
  118. break;
  119. } else if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  120. if (tokenizer->get_token(1)==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  121. _set_error("Expression expected");
  122. return false;
  123. }
  124. tokenizer->advance();
  125. argidx++;
  126. } else {
  127. // something is broken
  128. _set_error("Expected ',' or ')'");
  129. return false;
  130. }
  131. }
  132. parenthesis --;
  133. }
  134. return true;
  135. }
  136. void GDParser::_make_completable_call(int p_arg) {
  137. completion_cursor=StringName();
  138. completion_type=COMPLETION_CALL_ARGUMENTS;
  139. completion_class=current_class;
  140. completion_function=current_function;
  141. completion_line=tokenizer->get_token_line();
  142. completion_argument=p_arg;
  143. completion_block=current_block;
  144. completion_found=true;
  145. tokenizer->advance();
  146. }
  147. bool GDParser::_get_completable_identifier(CompletionType p_type,StringName& identifier) {
  148. identifier=StringName();
  149. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER) {
  150. identifier=tokenizer->get_token_identifier();
  151. tokenizer->advance();
  152. }
  153. if (tokenizer->get_token()==GDTokenizer::TK_CURSOR) {
  154. completion_cursor=identifier;
  155. completion_type=p_type;
  156. completion_class=current_class;
  157. completion_function=current_function;
  158. completion_line=tokenizer->get_token_line();
  159. completion_block=current_block;
  160. completion_found=true;
  161. tokenizer->advance();
  162. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER) {
  163. identifier=identifier.operator String() + tokenizer->get_token_identifier().operator String();
  164. tokenizer->advance();
  165. }
  166. return true;
  167. }
  168. return false;
  169. }
  170. GDParser::Node* GDParser::_parse_expression(Node *p_parent,bool p_static,bool p_allow_assign,bool p_parsing_constant) {
  171. // Vector<Node*> expressions;
  172. // Vector<OperatorNode::Operator> operators;
  173. Vector<Expression> expression;
  174. Node *expr=NULL;
  175. int op_line = tokenizer->get_token_line(); // when operators are created at the bottom, the line might have been changed (\n found)
  176. while(true) {
  177. /*****************/
  178. /* Parse Operand */
  179. /*****************/
  180. if (parenthesis>0) {
  181. //remove empty space (only allowed if inside parenthesis
  182. while(tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  183. tokenizer->advance();
  184. }
  185. }
  186. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_OPEN) {
  187. //subexpression ()
  188. tokenizer->advance();
  189. parenthesis++;
  190. Node* subexpr = _parse_expression(p_parent,p_static,p_allow_assign,p_parsing_constant);
  191. parenthesis--;
  192. if (!subexpr)
  193. return NULL;
  194. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  195. _set_error("Expected ')' in expression");
  196. return NULL;
  197. }
  198. tokenizer->advance();
  199. expr=subexpr;
  200. } else if (tokenizer->get_token()==GDTokenizer::TK_CURSOR) {
  201. tokenizer->advance();
  202. continue; //no point in cursor in the middle of expression
  203. } else if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT) {
  204. //constant defined by tokenizer
  205. ConstantNode *constant = alloc_node<ConstantNode>();
  206. constant->value=tokenizer->get_token_constant();
  207. tokenizer->advance();
  208. expr=constant;
  209. } else if (tokenizer->get_token()==GDTokenizer::TK_CONST_PI) {
  210. //constant defined by tokenizer
  211. ConstantNode *constant = alloc_node<ConstantNode>();
  212. constant->value=Math_PI;
  213. tokenizer->advance();
  214. expr=constant;
  215. } else if (tokenizer->get_token()==GDTokenizer::TK_PR_PRELOAD) {
  216. //constant defined by tokenizer
  217. tokenizer->advance();
  218. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_OPEN) {
  219. _set_error("Expected '(' after 'preload'");
  220. return NULL;
  221. }
  222. tokenizer->advance();
  223. String path;
  224. bool valid = false;
  225. Node *subexpr = _parse_and_reduce_expression(p_parent, p_static);
  226. if (subexpr) {
  227. if (subexpr->type == Node::TYPE_CONSTANT) {
  228. ConstantNode *cn = static_cast<ConstantNode*>(subexpr);
  229. if (cn->value.get_type() == Variant::STRING) {
  230. valid = true;
  231. path = (String) cn->value;
  232. }
  233. }
  234. }
  235. if (!valid) {
  236. _set_error("expected string constant as 'preload' argument.");
  237. return NULL;
  238. }
  239. if (!path.is_abs_path() && base_path!="")
  240. path=base_path+"/"+path;
  241. path = path.replace("///","//").simplify_path();
  242. if (path==self_path) {
  243. _set_error("Can't preload itself (use 'get_script()').");
  244. return NULL;
  245. }
  246. Ref<Resource> res;
  247. if (!validating) {
  248. //this can be too slow for just validating code
  249. if (for_completion && ScriptCodeCompletionCache::get_sigleton()) {
  250. res = ScriptCodeCompletionCache::get_sigleton()->get_cached_resource(path);
  251. } else {
  252. res = ResourceLoader::load(path);
  253. }
  254. if (!res.is_valid()) {
  255. _set_error("Can't preload resource at path: "+path);
  256. return NULL;
  257. }
  258. } else {
  259. if (!FileAccess::exists(path)) {
  260. _set_error("Can't preload resource at path: "+path);
  261. return NULL;
  262. }
  263. }
  264. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  265. _set_error("Expected ')' after 'preload' path");
  266. return NULL;
  267. }
  268. ConstantNode *constant = alloc_node<ConstantNode>();
  269. constant->value=res;
  270. tokenizer->advance();
  271. expr=constant;
  272. } else if (tokenizer->get_token()==GDTokenizer::TK_PR_YIELD) {
  273. //constant defined by tokenizer
  274. tokenizer->advance();
  275. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_OPEN) {
  276. _set_error("Expected '(' after 'yield'");
  277. return NULL;
  278. }
  279. tokenizer->advance();
  280. OperatorNode *yield = alloc_node<OperatorNode>();
  281. yield->op=OperatorNode::OP_YIELD;
  282. while (tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  283. tokenizer->advance();
  284. }
  285. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  286. expr=yield;
  287. tokenizer->advance();
  288. } else {
  289. parenthesis ++;
  290. Node *object = _parse_and_reduce_expression(p_parent,p_static);
  291. if (!object)
  292. return NULL;
  293. yield->arguments.push_back(object);
  294. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  295. _set_error("Expected ',' after first argument of 'yield'");
  296. return NULL;
  297. }
  298. tokenizer->advance();
  299. Node *signal = _parse_and_reduce_expression(p_parent,p_static);
  300. if (!signal)
  301. return NULL;
  302. yield->arguments.push_back(signal);
  303. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  304. _set_error("Expected ')' after second argument of 'yield'");
  305. return NULL;
  306. }
  307. parenthesis --;
  308. tokenizer->advance();
  309. expr=yield;
  310. }
  311. } else if (tokenizer->get_token()==GDTokenizer::TK_SELF) {
  312. if (p_static) {
  313. _set_error("'self'' not allowed in static function or constant expression");
  314. return NULL;
  315. }
  316. //constant defined by tokenizer
  317. SelfNode *self = alloc_node<SelfNode>();
  318. tokenizer->advance();
  319. expr=self;
  320. } else if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token(1)==GDTokenizer::TK_PERIOD) {
  321. Variant::Type bi_type = tokenizer->get_token_type();
  322. tokenizer->advance(2);
  323. StringName identifier;
  324. if (_get_completable_identifier(COMPLETION_BUILT_IN_TYPE_CONSTANT,identifier)) {
  325. completion_built_in_constant=bi_type;
  326. }
  327. if (identifier==StringName()) {
  328. _set_error("Built-in type constant expected after '.'");
  329. return NULL;
  330. }
  331. if (!Variant::has_numeric_constant(bi_type,identifier)) {
  332. _set_error("Static constant '"+identifier.operator String()+"' not present in built-in type "+Variant::get_type_name(bi_type)+".");
  333. return NULL;
  334. }
  335. ConstantNode *cn = alloc_node<ConstantNode>();
  336. cn->value=Variant::get_numeric_constant_value(bi_type,identifier);
  337. expr=cn;
  338. } 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)) {
  339. //function or constructor
  340. OperatorNode *op = alloc_node<OperatorNode>();
  341. op->op=OperatorNode::OP_CALL;
  342. if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_TYPE) {
  343. TypeNode *tn = alloc_node<TypeNode>();
  344. tn->vtype=tokenizer->get_token_type();
  345. op->arguments.push_back(tn);
  346. tokenizer->advance(2);
  347. } else if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_FUNC) {
  348. BuiltInFunctionNode *bn = alloc_node<BuiltInFunctionNode>();
  349. bn->function=tokenizer->get_token_built_in_func();
  350. op->arguments.push_back(bn);
  351. tokenizer->advance(2);
  352. } else {
  353. SelfNode *self = alloc_node<SelfNode>();
  354. op->arguments.push_back(self);
  355. StringName identifier;
  356. if (_get_completable_identifier(COMPLETION_FUNCTION,identifier)) {
  357. }
  358. IdentifierNode* id = alloc_node<IdentifierNode>();
  359. id->name=identifier;
  360. op->arguments.push_back(id);
  361. tokenizer->advance(1);
  362. }
  363. if (tokenizer->get_token()==GDTokenizer::TK_CURSOR) {
  364. _make_completable_call(0);
  365. completion_node=op;
  366. }
  367. if (!_parse_arguments(op,op->arguments,p_static,true))
  368. return NULL;
  369. expr=op;
  370. } else if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER) {
  371. //identifier (reference)
  372. const ClassNode* cln = current_class;
  373. bool bfn = false;
  374. StringName identifier;
  375. if (_get_completable_identifier(COMPLETION_IDENTIFIER,identifier)) {
  376. }
  377. if (p_parsing_constant) {
  378. for( int i=0; i<cln->constant_expressions.size(); ++i ) {
  379. if( cln->constant_expressions[i].identifier == identifier ) {
  380. expr = cln->constant_expressions[i].expression;
  381. bfn = true;
  382. break;
  383. }
  384. }
  385. if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) {
  386. //check from constants
  387. ConstantNode *constant = alloc_node<ConstantNode>();
  388. constant->value = GDScriptLanguage::get_singleton()->get_global_array()[ GDScriptLanguage::get_singleton()->get_global_map()[identifier] ];
  389. expr=constant;
  390. bfn = true;
  391. }
  392. }
  393. if ( !bfn ) {
  394. IdentifierNode *id = alloc_node<IdentifierNode>();
  395. id->name = identifier;
  396. expr = id;
  397. }
  398. } 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) {
  399. //single prefix operators like !expr -expr ++expr --expr
  400. alloc_node<OperatorNode>();
  401. Expression e;
  402. e.is_op=true;
  403. switch(tokenizer->get_token()) {
  404. case GDTokenizer::TK_OP_SUB: e.op=OperatorNode::OP_NEG; break;
  405. case GDTokenizer::TK_OP_NOT: e.op=OperatorNode::OP_NOT; break;
  406. case GDTokenizer::TK_OP_BIT_INVERT: e.op=OperatorNode::OP_BIT_INVERT;; break;
  407. default: {}
  408. }
  409. tokenizer->advance();
  410. if (e.op!=OperatorNode::OP_NOT && tokenizer->get_token()==GDTokenizer::TK_OP_NOT) {
  411. _set_error("Misplaced 'not'.");
  412. return NULL;
  413. }
  414. expression.push_back(e);
  415. continue; //only exception, must continue...
  416. /*
  417. Node *subexpr=_parse_expression(op,p_static);
  418. if (!subexpr)
  419. return NULL;
  420. op->arguments.push_back(subexpr);
  421. expr=op;*/
  422. } else if (tokenizer->get_token()==GDTokenizer::TK_BRACKET_OPEN) {
  423. // array
  424. tokenizer->advance();
  425. ArrayNode *arr = alloc_node<ArrayNode>();
  426. bool expecting_comma=false;
  427. while(true) {
  428. if (tokenizer->get_token()==GDTokenizer::TK_EOF) {
  429. _set_error("Unterminated array");
  430. return NULL;
  431. } else if (tokenizer->get_token()==GDTokenizer::TK_BRACKET_CLOSE) {
  432. tokenizer->advance();
  433. break;
  434. } else if (tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  435. tokenizer->advance(); //ignore newline
  436. } else if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  437. if (!expecting_comma) {
  438. _set_error("expression or ']' expected");
  439. return NULL;
  440. }
  441. expecting_comma=false;
  442. tokenizer->advance(); //ignore newline
  443. } else {
  444. //parse expression
  445. if (expecting_comma) {
  446. _set_error("',' or ']' expected");
  447. return NULL;
  448. }
  449. Node *n = _parse_expression(arr,p_static,p_allow_assign,p_parsing_constant);
  450. if (!n)
  451. return NULL;
  452. arr->elements.push_back(n);
  453. expecting_comma=true;
  454. }
  455. }
  456. expr=arr;
  457. } else if (tokenizer->get_token()==GDTokenizer::TK_CURLY_BRACKET_OPEN) {
  458. // array
  459. tokenizer->advance();
  460. DictionaryNode *dict = alloc_node<DictionaryNode>();
  461. enum DictExpect {
  462. DICT_EXPECT_KEY,
  463. DICT_EXPECT_COLON,
  464. DICT_EXPECT_VALUE,
  465. DICT_EXPECT_COMMA
  466. };
  467. Node *key=NULL;
  468. DictExpect expecting=DICT_EXPECT_KEY;
  469. while(true) {
  470. if (tokenizer->get_token()==GDTokenizer::TK_EOF) {
  471. _set_error("Unterminated dictionary");
  472. return NULL;
  473. } else if (tokenizer->get_token()==GDTokenizer::TK_CURLY_BRACKET_CLOSE) {
  474. if (expecting==DICT_EXPECT_COLON) {
  475. _set_error("':' expected");
  476. return NULL;
  477. }
  478. if (expecting==DICT_EXPECT_VALUE) {
  479. _set_error("value expected");
  480. return NULL;
  481. }
  482. tokenizer->advance();
  483. break;
  484. } else if (tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  485. tokenizer->advance(); //ignore newline
  486. } else if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  487. if (expecting==DICT_EXPECT_KEY) {
  488. _set_error("key or '}' expected");
  489. return NULL;
  490. }
  491. if (expecting==DICT_EXPECT_VALUE) {
  492. _set_error("value expected");
  493. return NULL;
  494. }
  495. if (expecting==DICT_EXPECT_COLON) {
  496. _set_error("':' expected");
  497. return NULL;
  498. }
  499. expecting=DICT_EXPECT_KEY;
  500. tokenizer->advance(); //ignore newline
  501. } else if (tokenizer->get_token()==GDTokenizer::TK_COLON) {
  502. if (expecting==DICT_EXPECT_KEY) {
  503. _set_error("key or '}' expected");
  504. return NULL;
  505. }
  506. if (expecting==DICT_EXPECT_VALUE) {
  507. _set_error("value expected");
  508. return NULL;
  509. }
  510. if (expecting==DICT_EXPECT_COMMA) {
  511. _set_error("',' or '}' expected");
  512. return NULL;
  513. }
  514. expecting=DICT_EXPECT_VALUE;
  515. tokenizer->advance(); //ignore newline
  516. } else {
  517. if (expecting==DICT_EXPECT_COMMA) {
  518. _set_error("',' or '}' expected");
  519. return NULL;
  520. }
  521. if (expecting==DICT_EXPECT_COLON) {
  522. _set_error("':' expected");
  523. return NULL;
  524. }
  525. if (expecting==DICT_EXPECT_KEY) {
  526. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token(1)==GDTokenizer::TK_OP_ASSIGN) {
  527. //lua style identifier, easier to write
  528. ConstantNode *cn = alloc_node<ConstantNode>();
  529. cn->value = tokenizer->get_token_identifier();
  530. key = cn;
  531. tokenizer->advance(2);
  532. expecting=DICT_EXPECT_VALUE;
  533. } else {
  534. //python/js style more flexible
  535. key = _parse_expression(dict,p_static,p_allow_assign,p_parsing_constant);
  536. if (!key)
  537. return NULL;
  538. expecting=DICT_EXPECT_COLON;
  539. }
  540. }
  541. if (expecting==DICT_EXPECT_VALUE) {
  542. Node *value = _parse_expression(dict,p_static,p_allow_assign,p_parsing_constant);
  543. if (!value)
  544. return NULL;
  545. expecting=DICT_EXPECT_COMMA;
  546. DictionaryNode::Pair pair;
  547. pair.key=key;
  548. pair.value=value;
  549. dict->elements.push_back(pair);
  550. key=NULL;
  551. }
  552. }
  553. }
  554. expr=dict;
  555. } else if (tokenizer->get_token()==GDTokenizer::TK_PERIOD && (tokenizer->get_token(1)==GDTokenizer::TK_IDENTIFIER || tokenizer->get_token(1)==GDTokenizer::TK_CURSOR) && tokenizer->get_token(2)==GDTokenizer::TK_PARENTHESIS_OPEN) {
  556. // parent call
  557. tokenizer->advance(); //goto identifier
  558. OperatorNode *op = alloc_node<OperatorNode>();
  559. op->op=OperatorNode::OP_PARENT_CALL;
  560. /*SelfNode *self = alloc_node<SelfNode>();
  561. op->arguments.push_back(self);
  562. forbidden for now */
  563. StringName identifier;
  564. if (_get_completable_identifier(COMPLETION_PARENT_FUNCTION,identifier)) {
  565. //indexing stuff
  566. }
  567. IdentifierNode *id = alloc_node<IdentifierNode>();
  568. id->name=identifier;
  569. op->arguments.push_back(id);
  570. tokenizer->advance(1);
  571. if (!_parse_arguments(op,op->arguments,p_static))
  572. return NULL;
  573. expr=op;
  574. } else {
  575. //find list [ or find dictionary {
  576. //print_line("found bug?");
  577. _set_error("Error parsing expression, misplaced: "+String(tokenizer->get_token_name(tokenizer->get_token())));
  578. return NULL; //nothing
  579. }
  580. if (!expr) {
  581. ERR_EXPLAIN("GDParser bug, couldn't figure out what expression is..");
  582. ERR_FAIL_COND_V(!expr,NULL);
  583. }
  584. /******************/
  585. /* Parse Indexing */
  586. /******************/
  587. while (true) {
  588. //expressions can be indexed any number of times
  589. if (tokenizer->get_token()==GDTokenizer::TK_PERIOD) {
  590. //indexing using "."
  591. if (tokenizer->get_token(1)!=GDTokenizer::TK_CURSOR && tokenizer->get_token(1)!=GDTokenizer::TK_IDENTIFIER && tokenizer->get_token(1)!=GDTokenizer::TK_BUILT_IN_FUNC ) {
  592. _set_error("Expected identifier as member");
  593. return NULL;
  594. } else if (tokenizer->get_token(2)==GDTokenizer::TK_PARENTHESIS_OPEN) {
  595. //call!!
  596. OperatorNode * op = alloc_node<OperatorNode>();
  597. op->op=OperatorNode::OP_CALL;
  598. tokenizer->advance();
  599. IdentifierNode * id = alloc_node<IdentifierNode>();
  600. if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_FUNC ) {
  601. //small hack so built in funcs don't obfuscate methods
  602. id->name=GDFunctions::get_func_name(tokenizer->get_token_built_in_func());
  603. tokenizer->advance();
  604. } else {
  605. StringName identifier;
  606. if (_get_completable_identifier(COMPLETION_METHOD,identifier)) {
  607. completion_node=op;
  608. //indexing stuff
  609. }
  610. id->name=identifier;
  611. }
  612. op->arguments.push_back(expr); // call what
  613. op->arguments.push_back(id); // call func
  614. //get arguments
  615. tokenizer->advance(1);
  616. if (tokenizer->get_token()==GDTokenizer::TK_CURSOR) {
  617. _make_completable_call(0);
  618. completion_node=op;
  619. }
  620. if (!_parse_arguments(op,op->arguments,p_static,true))
  621. return NULL;
  622. expr=op;
  623. } else {
  624. //simple indexing!
  625. OperatorNode * op = alloc_node<OperatorNode>();
  626. op->op=OperatorNode::OP_INDEX_NAMED;
  627. tokenizer->advance();
  628. StringName identifier;
  629. if (_get_completable_identifier(COMPLETION_INDEX,identifier)) {
  630. if (identifier==StringName()) {
  631. identifier="@temp"; //so it parses allright
  632. }
  633. completion_node=op;
  634. //indexing stuff
  635. }
  636. IdentifierNode * id = alloc_node<IdentifierNode>();
  637. id->name=identifier;
  638. op->arguments.push_back(expr);
  639. op->arguments.push_back(id);
  640. expr=op;
  641. }
  642. } else if (tokenizer->get_token()==GDTokenizer::TK_BRACKET_OPEN) {
  643. //indexing using "[]"
  644. OperatorNode * op = alloc_node<OperatorNode>();
  645. op->op=OperatorNode::OP_INDEX;
  646. tokenizer->advance(1);
  647. Node *subexpr = _parse_expression(op,p_static,p_allow_assign,p_parsing_constant);
  648. if (!subexpr) {
  649. return NULL;
  650. }
  651. if (tokenizer->get_token()!=GDTokenizer::TK_BRACKET_CLOSE) {
  652. _set_error("Expected ']'");
  653. return NULL;
  654. }
  655. op->arguments.push_back(expr);
  656. op->arguments.push_back(subexpr);
  657. tokenizer->advance(1);
  658. expr=op;
  659. } else
  660. break;
  661. }
  662. /******************/
  663. /* Parse Operator */
  664. /******************/
  665. if (parenthesis>0) {
  666. //remove empty space (only allowed if inside parenthesis
  667. while(tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  668. tokenizer->advance();
  669. }
  670. }
  671. Expression e;
  672. e.is_op=false;
  673. e.node=expr;
  674. expression.push_back(e);
  675. // determine which operator is next
  676. OperatorNode::Operator op;
  677. bool valid=true;
  678. //assign, if allowed is only alowed on the first operator
  679. #define _VALIDATE_ASSIGN if (!p_allow_assign) { _set_error("Unexpected assign."); return NULL; } p_allow_assign=false;
  680. switch(tokenizer->get_token()) { //see operator
  681. case GDTokenizer::TK_OP_IN: op=OperatorNode::OP_IN; break;
  682. case GDTokenizer::TK_OP_EQUAL: op=OperatorNode::OP_EQUAL ; break;
  683. case GDTokenizer::TK_OP_NOT_EQUAL: op=OperatorNode::OP_NOT_EQUAL ; break;
  684. case GDTokenizer::TK_OP_LESS: op=OperatorNode::OP_LESS ; break;
  685. case GDTokenizer::TK_OP_LESS_EQUAL: op=OperatorNode::OP_LESS_EQUAL ; break;
  686. case GDTokenizer::TK_OP_GREATER: op=OperatorNode::OP_GREATER ; break;
  687. case GDTokenizer::TK_OP_GREATER_EQUAL: op=OperatorNode::OP_GREATER_EQUAL ; break;
  688. case GDTokenizer::TK_OP_AND: op=OperatorNode::OP_AND ; break;
  689. case GDTokenizer::TK_OP_OR: op=OperatorNode::OP_OR ; break;
  690. case GDTokenizer::TK_OP_ADD: op=OperatorNode::OP_ADD ; break;
  691. case GDTokenizer::TK_OP_SUB: op=OperatorNode::OP_SUB ; break;
  692. case GDTokenizer::TK_OP_MUL: op=OperatorNode::OP_MUL ; break;
  693. case GDTokenizer::TK_OP_DIV: op=OperatorNode::OP_DIV ; break;
  694. case GDTokenizer::TK_OP_MOD: op=OperatorNode::OP_MOD ; break;
  695. //case GDTokenizer::TK_OP_NEG: op=OperatorNode::OP_NEG ; break;
  696. case GDTokenizer::TK_OP_SHIFT_LEFT: op=OperatorNode::OP_SHIFT_LEFT ; break;
  697. case GDTokenizer::TK_OP_SHIFT_RIGHT: op=OperatorNode::OP_SHIFT_RIGHT ; break;
  698. case GDTokenizer::TK_OP_ASSIGN: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN ; break;
  699. case GDTokenizer::TK_OP_ASSIGN_ADD: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_ADD ; break;
  700. case GDTokenizer::TK_OP_ASSIGN_SUB: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_SUB ; break;
  701. case GDTokenizer::TK_OP_ASSIGN_MUL: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_MUL ; break;
  702. case GDTokenizer::TK_OP_ASSIGN_DIV: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_DIV ; break;
  703. case GDTokenizer::TK_OP_ASSIGN_MOD: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_MOD ; break;
  704. case GDTokenizer::TK_OP_ASSIGN_SHIFT_LEFT: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_SHIFT_LEFT; ; break;
  705. case GDTokenizer::TK_OP_ASSIGN_SHIFT_RIGHT: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_SHIFT_RIGHT; ; break;
  706. case GDTokenizer::TK_OP_ASSIGN_BIT_AND: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_BIT_AND ; break;
  707. case GDTokenizer::TK_OP_ASSIGN_BIT_OR: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_BIT_OR ; break;
  708. case GDTokenizer::TK_OP_ASSIGN_BIT_XOR: _VALIDATE_ASSIGN op=OperatorNode::OP_ASSIGN_BIT_XOR ; break;
  709. case GDTokenizer::TK_OP_BIT_AND: op=OperatorNode::OP_BIT_AND ; break;
  710. case GDTokenizer::TK_OP_BIT_OR: op=OperatorNode::OP_BIT_OR ; break;
  711. case GDTokenizer::TK_OP_BIT_XOR: op=OperatorNode::OP_BIT_XOR ; break;
  712. case GDTokenizer::TK_PR_EXTENDS: op=OperatorNode::OP_EXTENDS; break;
  713. default: valid=false; break;
  714. }
  715. if (valid) {
  716. e.is_op=true;
  717. e.op=op;
  718. expression.push_back(e);
  719. tokenizer->advance();
  720. } else {
  721. break;
  722. }
  723. }
  724. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  725. while(expression.size()>1) {
  726. int next_op=-1;
  727. int min_priority=0xFFFFF;
  728. bool is_unary=false;
  729. for(int i=0;i<expression.size();i++) {
  730. if (!expression[i].is_op) {
  731. continue;
  732. }
  733. int priority;
  734. bool unary=false;
  735. switch(expression[i].op) {
  736. case OperatorNode::OP_EXTENDS: priority=-1; break; //before anything
  737. case OperatorNode::OP_BIT_INVERT: priority=0; unary=true; break;
  738. case OperatorNode::OP_NEG: priority=1; unary=true; break;
  739. case OperatorNode::OP_MUL: priority=2; break;
  740. case OperatorNode::OP_DIV: priority=2; break;
  741. case OperatorNode::OP_MOD: priority=2; break;
  742. case OperatorNode::OP_ADD: priority=3; break;
  743. case OperatorNode::OP_SUB: priority=3; break;
  744. case OperatorNode::OP_SHIFT_LEFT: priority=4; break;
  745. case OperatorNode::OP_SHIFT_RIGHT: priority=4; break;
  746. case OperatorNode::OP_BIT_AND: priority=5; break;
  747. case OperatorNode::OP_BIT_XOR: priority=6; break;
  748. case OperatorNode::OP_BIT_OR: priority=7; break;
  749. case OperatorNode::OP_LESS: priority=8; break;
  750. case OperatorNode::OP_LESS_EQUAL: priority=8; break;
  751. case OperatorNode::OP_GREATER: priority=8; break;
  752. case OperatorNode::OP_GREATER_EQUAL: priority=8; break;
  753. case OperatorNode::OP_EQUAL: priority=8; break;
  754. case OperatorNode::OP_NOT_EQUAL: priority=8; break;
  755. case OperatorNode::OP_IN: priority=10; break;
  756. case OperatorNode::OP_NOT: priority=11; unary=true; break;
  757. case OperatorNode::OP_AND: priority=12; break;
  758. case OperatorNode::OP_OR: priority=13; break;
  759. // ?: = 10
  760. case OperatorNode::OP_ASSIGN: priority=14; break;
  761. case OperatorNode::OP_ASSIGN_ADD: priority=14; break;
  762. case OperatorNode::OP_ASSIGN_SUB: priority=14; break;
  763. case OperatorNode::OP_ASSIGN_MUL: priority=14; break;
  764. case OperatorNode::OP_ASSIGN_DIV: priority=14; break;
  765. case OperatorNode::OP_ASSIGN_MOD: priority=14; break;
  766. case OperatorNode::OP_ASSIGN_SHIFT_LEFT: priority=14; break;
  767. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT: priority=14; break;
  768. case OperatorNode::OP_ASSIGN_BIT_AND: priority=14; break;
  769. case OperatorNode::OP_ASSIGN_BIT_OR: priority=14; break;
  770. case OperatorNode::OP_ASSIGN_BIT_XOR: priority=14; break;
  771. default: {
  772. _set_error("GDParser bug, invalid operator in expression: "+itos(expression[i].op));
  773. return NULL;
  774. }
  775. }
  776. if (priority<min_priority) {
  777. // < is used for left to right (default)
  778. // <= is used for right to left
  779. next_op=i;
  780. min_priority=priority;
  781. is_unary=unary;
  782. }
  783. }
  784. if (next_op==-1) {
  785. _set_error("Yet another parser bug....");
  786. ERR_FAIL_COND_V(next_op==-1,NULL);
  787. }
  788. // OK! create operator..
  789. if (is_unary) {
  790. int expr_pos=next_op;
  791. while(expression[expr_pos].is_op) {
  792. expr_pos++;
  793. if (expr_pos==expression.size()) {
  794. //can happen..
  795. _set_error("Unexpected end of expression..");
  796. return NULL;
  797. }
  798. }
  799. //consecutively do unary opeators
  800. for(int i=expr_pos-1;i>=next_op;i--) {
  801. OperatorNode *op = alloc_node<OperatorNode>();
  802. op->op=expression[i].op;
  803. op->arguments.push_back(expression[i+1].node);
  804. op->line=op_line; //line might have been changed from a \n
  805. expression[i].is_op=false;
  806. expression[i].node=op;
  807. expression.remove(i+1);
  808. }
  809. } else {
  810. if (next_op <1 || next_op>=(expression.size()-1)) {
  811. _set_error("Parser bug..");
  812. ERR_FAIL_V(NULL);
  813. }
  814. OperatorNode *op = alloc_node<OperatorNode>();
  815. op->op=expression[next_op].op;
  816. op->line=op_line; //line might have been changed from a \n
  817. if (expression[next_op-1].is_op) {
  818. _set_error("Parser bug..");
  819. ERR_FAIL_V(NULL);
  820. }
  821. if (expression[next_op+1].is_op) {
  822. // this is not invalid and can really appear
  823. // but it becomes invalid anyway because no binary op
  824. // can be followed by an unary op in a valid combination,
  825. // due to how precedence works, unaries will always dissapear first
  826. _set_error("Unexpected two consecutive operators.");
  827. return NULL;
  828. }
  829. op->arguments.push_back(expression[next_op-1].node); //expression goes as left
  830. op->arguments.push_back(expression[next_op+1].node); //next expression goes as right
  831. //replace all 3 nodes by this operator and make it an expression
  832. expression[next_op-1].node=op;
  833. expression.remove(next_op);
  834. expression.remove(next_op);
  835. }
  836. }
  837. return expression[0].node;
  838. }
  839. GDParser::Node* GDParser::_reduce_expression(Node *p_node,bool p_to_const) {
  840. switch(p_node->type) {
  841. case Node::TYPE_BUILT_IN_FUNCTION: {
  842. //many may probably be optimizable
  843. return p_node;
  844. } break;
  845. case Node::TYPE_ARRAY: {
  846. ArrayNode *an = static_cast<ArrayNode*>(p_node);
  847. bool all_constants=true;
  848. for(int i=0;i<an->elements.size();i++) {
  849. an->elements[i]=_reduce_expression(an->elements[i],p_to_const);
  850. if (an->elements[i]->type!=Node::TYPE_CONSTANT)
  851. all_constants=false;
  852. }
  853. if (all_constants && p_to_const) {
  854. //reduce constant array expression
  855. ConstantNode *cn = alloc_node<ConstantNode>();
  856. Array arr(!p_to_const);
  857. //print_line("mk array "+itos(!p_to_const));
  858. arr.resize(an->elements.size());
  859. for(int i=0;i<an->elements.size();i++) {
  860. ConstantNode *acn = static_cast<ConstantNode*>(an->elements[i]);
  861. arr[i]=acn->value;
  862. }
  863. cn->value=arr;
  864. return cn;
  865. }
  866. return an;
  867. } break;
  868. case Node::TYPE_DICTIONARY: {
  869. DictionaryNode *dn = static_cast<DictionaryNode*>(p_node);
  870. bool all_constants=true;
  871. for(int i=0;i<dn->elements.size();i++) {
  872. dn->elements[i].key=_reduce_expression(dn->elements[i].key,p_to_const);
  873. if (dn->elements[i].key->type!=Node::TYPE_CONSTANT)
  874. all_constants=false;
  875. dn->elements[i].value=_reduce_expression(dn->elements[i].value,p_to_const);
  876. if (dn->elements[i].value->type!=Node::TYPE_CONSTANT)
  877. all_constants=false;
  878. }
  879. if (all_constants && p_to_const) {
  880. //reduce constant array expression
  881. ConstantNode *cn = alloc_node<ConstantNode>();
  882. Dictionary dict(!p_to_const);
  883. for(int i=0;i<dn->elements.size();i++) {
  884. ConstantNode *key_c = static_cast<ConstantNode*>(dn->elements[i].key);
  885. ConstantNode *value_c = static_cast<ConstantNode*>(dn->elements[i].value);
  886. dict[key_c->value]=value_c->value;
  887. }
  888. cn->value=dict;
  889. return cn;
  890. }
  891. return dn;
  892. } break;
  893. case Node::TYPE_OPERATOR: {
  894. OperatorNode *op=static_cast<OperatorNode*>(p_node);
  895. bool all_constants=true;
  896. int last_not_constant=-1;
  897. for(int i=0;i<op->arguments.size();i++) {
  898. op->arguments[i]=_reduce_expression(op->arguments[i],p_to_const);
  899. if (op->arguments[i]->type!=Node::TYPE_CONSTANT) {
  900. all_constants=false;
  901. last_not_constant=i;
  902. }
  903. }
  904. if (op->op==OperatorNode::OP_EXTENDS) {
  905. //nothing much
  906. return op;
  907. } if (op->op==OperatorNode::OP_PARENT_CALL) {
  908. //nothing much
  909. return op;
  910. } else if (op->op==OperatorNode::OP_CALL) {
  911. //can reduce base type constructors
  912. 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) {
  913. //native type constructor or intrinsic function
  914. const Variant **vptr=NULL;
  915. Vector<Variant*> ptrs;
  916. if (op->arguments.size()>1) {
  917. ptrs.resize(op->arguments.size()-1);
  918. for(int i=0;i<ptrs.size();i++) {
  919. ConstantNode *cn = static_cast<ConstantNode*>(op->arguments[i+1]);
  920. ptrs[i]=&cn->value;
  921. }
  922. vptr=(const Variant**)&ptrs[0];
  923. }
  924. Variant::CallError ce;
  925. Variant v;
  926. if (op->arguments[0]->type==Node::TYPE_TYPE) {
  927. TypeNode *tn = static_cast<TypeNode*>(op->arguments[0]);
  928. v = Variant::construct(tn->vtype,vptr,ptrs.size(),ce);
  929. } else {
  930. GDFunctions::Function func = static_cast<BuiltInFunctionNode*>(op->arguments[0])->function;
  931. GDFunctions::call(func,vptr,ptrs.size(),v,ce);
  932. }
  933. if (ce.error!=Variant::CallError::CALL_OK) {
  934. String errwhere;
  935. if (op->arguments[0]->type==Node::TYPE_TYPE) {
  936. TypeNode *tn = static_cast<TypeNode*>(op->arguments[0]);
  937. errwhere="'"+Variant::get_type_name(tn->vtype)+"'' constructor";
  938. } else {
  939. GDFunctions::Function func = static_cast<BuiltInFunctionNode*>(op->arguments[0])->function;
  940. errwhere=String("'")+GDFunctions::get_func_name(func)+"'' intrinsic function";
  941. }
  942. switch(ce.error) {
  943. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  944. _set_error("Invalid argument (#"+itos(ce.argument+1)+") for "+errwhere+".");
  945. } break;
  946. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  947. _set_error("Too many arguments for "+errwhere+".");
  948. } break;
  949. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  950. _set_error("Too few arguments for "+errwhere+".");
  951. } break;
  952. default: {
  953. _set_error("Invalid arguments for "+errwhere+".");
  954. } break;
  955. }
  956. error_line=op->line;
  957. return p_node;
  958. }
  959. ConstantNode *cn = alloc_node<ConstantNode>();
  960. cn->value=v;
  961. return cn;
  962. } else if (op->arguments[0]->type==Node::TYPE_BUILT_IN_FUNCTION && last_not_constant==0) {
  963. }
  964. return op; //don't reduce yet
  965. } else if (op->op==OperatorNode::OP_YIELD) {
  966. return op;
  967. } else if (op->op==OperatorNode::OP_INDEX) {
  968. //can reduce indices into constant arrays or dictionaries
  969. if (all_constants) {
  970. ConstantNode *ca = static_cast<ConstantNode*>(op->arguments[0]);
  971. ConstantNode *cb = static_cast<ConstantNode*>(op->arguments[1]);
  972. bool valid;
  973. Variant v = ca->value.get(cb->value,&valid);
  974. if (!valid) {
  975. _set_error("invalid index in constant expression");
  976. error_line=op->line;
  977. return op;
  978. }
  979. ConstantNode *cn = alloc_node<ConstantNode>();
  980. cn->value=v;
  981. return cn;
  982. } /*else if (op->arguments[0]->type==Node::TYPE_CONSTANT && op->arguments[1]->type==Node::TYPE_IDENTIFIER) {
  983. ConstantNode *ca = static_cast<ConstantNode*>(op->arguments[0]);
  984. IdentifierNode *ib = static_cast<IdentifierNode*>(op->arguments[1]);
  985. bool valid;
  986. Variant v = ca->value.get_named(ib->name,&valid);
  987. if (!valid) {
  988. _set_error("invalid index '"+String(ib->name)+"' in constant expression");
  989. return op;
  990. }
  991. ConstantNode *cn = alloc_node<ConstantNode>();
  992. cn->value=v;
  993. return cn;
  994. }*/
  995. return op;
  996. } else if (op->op==OperatorNode::OP_INDEX_NAMED) {
  997. if (op->arguments[0]->type==Node::TYPE_CONSTANT && op->arguments[1]->type==Node::TYPE_IDENTIFIER) {
  998. ConstantNode *ca = static_cast<ConstantNode*>(op->arguments[0]);
  999. IdentifierNode *ib = static_cast<IdentifierNode*>(op->arguments[1]);
  1000. bool valid;
  1001. Variant v = ca->value.get_named(ib->name,&valid);
  1002. if (!valid) {
  1003. _set_error("invalid index '"+String(ib->name)+"' in constant expression");
  1004. error_line=op->line;
  1005. return op;
  1006. }
  1007. ConstantNode *cn = alloc_node<ConstantNode>();
  1008. cn->value=v;
  1009. return cn;
  1010. }
  1011. return op;
  1012. }
  1013. //validate assignment (don't assign to cosntant expression
  1014. switch(op->op) {
  1015. case OperatorNode::OP_ASSIGN:
  1016. case OperatorNode::OP_ASSIGN_ADD:
  1017. case OperatorNode::OP_ASSIGN_SUB:
  1018. case OperatorNode::OP_ASSIGN_MUL:
  1019. case OperatorNode::OP_ASSIGN_DIV:
  1020. case OperatorNode::OP_ASSIGN_MOD:
  1021. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  1022. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  1023. case OperatorNode::OP_ASSIGN_BIT_AND:
  1024. case OperatorNode::OP_ASSIGN_BIT_OR:
  1025. case OperatorNode::OP_ASSIGN_BIT_XOR: {
  1026. if (op->arguments[0]->type==Node::TYPE_CONSTANT) {
  1027. _set_error("Can't assign to constant",tokenizer->get_token_line()-1);
  1028. error_line=op->line;
  1029. return op;
  1030. }
  1031. } break;
  1032. default: { break; }
  1033. }
  1034. //now se if all are constants
  1035. if (!all_constants)
  1036. return op; //nothing to reduce from here on
  1037. #define _REDUCE_UNARY(m_vop)\
  1038. bool valid=false;\
  1039. Variant res;\
  1040. Variant::evaluate(m_vop,static_cast<ConstantNode*>(op->arguments[0])->value,Variant(),res,valid);\
  1041. if (!valid) {\
  1042. _set_error("Invalid operand for unary operator");\
  1043. error_line=op->line;\
  1044. return p_node;\
  1045. }\
  1046. ConstantNode *cn = alloc_node<ConstantNode>();\
  1047. cn->value=res;\
  1048. return cn;
  1049. #define _REDUCE_BINARY(m_vop)\
  1050. bool valid=false;\
  1051. Variant res;\
  1052. Variant::evaluate(m_vop,static_cast<ConstantNode*>(op->arguments[0])->value,static_cast<ConstantNode*>(op->arguments[1])->value,res,valid);\
  1053. if (!valid) {\
  1054. _set_error("Invalid operands for operator");\
  1055. error_line=op->line;\
  1056. return p_node;\
  1057. }\
  1058. ConstantNode *cn = alloc_node<ConstantNode>();\
  1059. cn->value=res;\
  1060. return cn;
  1061. switch(op->op) {
  1062. //unary operators
  1063. case OperatorNode::OP_NEG: { _REDUCE_UNARY(Variant::OP_NEGATE); } break;
  1064. case OperatorNode::OP_NOT: { _REDUCE_UNARY(Variant::OP_NOT); } break;
  1065. case OperatorNode::OP_BIT_INVERT: { _REDUCE_UNARY(Variant::OP_BIT_NEGATE); } break;
  1066. //binary operators (in precedence order)
  1067. case OperatorNode::OP_IN: { _REDUCE_BINARY(Variant::OP_IN); } break;
  1068. case OperatorNode::OP_EQUAL: { _REDUCE_BINARY(Variant::OP_EQUAL); } break;
  1069. case OperatorNode::OP_NOT_EQUAL: { _REDUCE_BINARY(Variant::OP_NOT_EQUAL); } break;
  1070. case OperatorNode::OP_LESS: { _REDUCE_BINARY(Variant::OP_LESS); } break;
  1071. case OperatorNode::OP_LESS_EQUAL: { _REDUCE_BINARY(Variant::OP_LESS_EQUAL); } break;
  1072. case OperatorNode::OP_GREATER: { _REDUCE_BINARY(Variant::OP_GREATER); } break;
  1073. case OperatorNode::OP_GREATER_EQUAL: { _REDUCE_BINARY(Variant::OP_GREATER_EQUAL); } break;
  1074. case OperatorNode::OP_AND: { _REDUCE_BINARY(Variant::OP_AND); } break;
  1075. case OperatorNode::OP_OR: { _REDUCE_BINARY(Variant::OP_OR); } break;
  1076. case OperatorNode::OP_ADD: { _REDUCE_BINARY(Variant::OP_ADD); } break;
  1077. case OperatorNode::OP_SUB: { _REDUCE_BINARY(Variant::OP_SUBSTRACT); } break;
  1078. case OperatorNode::OP_MUL: { _REDUCE_BINARY(Variant::OP_MULTIPLY); } break;
  1079. case OperatorNode::OP_DIV: { _REDUCE_BINARY(Variant::OP_DIVIDE); } break;
  1080. case OperatorNode::OP_MOD: { _REDUCE_BINARY(Variant::OP_MODULE); } break;
  1081. case OperatorNode::OP_SHIFT_LEFT: { _REDUCE_BINARY(Variant::OP_SHIFT_LEFT); } break;
  1082. case OperatorNode::OP_SHIFT_RIGHT: { _REDUCE_BINARY(Variant::OP_SHIFT_RIGHT); } break;
  1083. case OperatorNode::OP_BIT_AND: { _REDUCE_BINARY(Variant::OP_BIT_AND); } break;
  1084. case OperatorNode::OP_BIT_OR: { _REDUCE_BINARY(Variant::OP_BIT_OR); } break;
  1085. case OperatorNode::OP_BIT_XOR: { _REDUCE_BINARY(Variant::OP_BIT_XOR); } break;
  1086. default: { ERR_FAIL_V(op); }
  1087. }
  1088. ERR_FAIL_V(op);
  1089. } break;
  1090. default: {
  1091. return p_node;
  1092. } break;
  1093. }
  1094. }
  1095. GDParser::Node* GDParser::_parse_and_reduce_expression(Node *p_parent,bool p_static,bool p_reduce_const,bool p_allow_assign) {
  1096. Node* expr=_parse_expression(p_parent,p_static,p_allow_assign,p_reduce_const);
  1097. if (!expr || error_set)
  1098. return NULL;
  1099. expr = _reduce_expression(expr,p_reduce_const);
  1100. if (!expr || error_set)
  1101. return NULL;
  1102. return expr;
  1103. }
  1104. bool GDParser::_recover_from_completion() {
  1105. if (!completion_found) {
  1106. return false; //can't recover if no completion
  1107. }
  1108. //skip stuff until newline
  1109. while(tokenizer->get_token()!=GDTokenizer::TK_NEWLINE && tokenizer->get_token()!=GDTokenizer::TK_EOF && tokenizer->get_token()!=GDTokenizer::TK_ERROR) {
  1110. tokenizer->advance();
  1111. }
  1112. completion_found=false;
  1113. error_set=false;
  1114. if(tokenizer->get_token() == GDTokenizer::TK_ERROR){
  1115. error_set = true;
  1116. }
  1117. return true;
  1118. }
  1119. void GDParser::_parse_block(BlockNode *p_block,bool p_static) {
  1120. int indent_level = tab_level.back()->get();
  1121. #ifdef DEBUG_ENABLED
  1122. NewLineNode *nl = alloc_node<NewLineNode>();
  1123. nl->line=tokenizer->get_token_line();
  1124. p_block->statements.push_back(nl);
  1125. #endif
  1126. while(true) {
  1127. GDTokenizer::Token token = tokenizer->get_token();
  1128. if (error_set)
  1129. return;
  1130. if (indent_level>tab_level.back()->get()) {
  1131. p_block->end_line=tokenizer->get_token_line();
  1132. return; //go back a level
  1133. }
  1134. if (pending_newline!=-1) {
  1135. NewLineNode *nl = alloc_node<NewLineNode>();
  1136. nl->line=pending_newline;
  1137. p_block->statements.push_back(nl);
  1138. pending_newline=-1;
  1139. }
  1140. switch(token) {
  1141. case GDTokenizer::TK_EOF:
  1142. p_block->end_line=tokenizer->get_token_line();
  1143. case GDTokenizer::TK_ERROR: {
  1144. return; //go back
  1145. //end of file!
  1146. } break;
  1147. case GDTokenizer::TK_NEWLINE: {
  1148. if (!_parse_newline()) {
  1149. if (!error_set) {
  1150. p_block->end_line=tokenizer->get_token_line();
  1151. pending_newline=p_block->end_line;
  1152. }
  1153. return;
  1154. }
  1155. NewLineNode *nl = alloc_node<NewLineNode>();
  1156. nl->line=tokenizer->get_token_line();
  1157. p_block->statements.push_back(nl);
  1158. } break;
  1159. case GDTokenizer::TK_CF_PASS: {
  1160. if (tokenizer->get_token(1)!=GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE && tokenizer->get_token(1)!=GDTokenizer::TK_EOF) {
  1161. _set_error("Expected ';' or <NewLine>.");
  1162. return;
  1163. }
  1164. tokenizer->advance();
  1165. if(tokenizer->get_token()==GDTokenizer::TK_SEMICOLON) {
  1166. // Ignore semicolon after 'pass'
  1167. tokenizer->advance();
  1168. }
  1169. } break;
  1170. case GDTokenizer::TK_PR_VAR: {
  1171. //variale declaration and (eventual) initialization
  1172. tokenizer->advance();
  1173. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1174. _set_error("Expected identifier for local variable name.");
  1175. return;
  1176. }
  1177. StringName n = tokenizer->get_token_identifier();
  1178. tokenizer->advance();
  1179. p_block->variables.push_back(n); //line?
  1180. p_block->variable_lines.push_back(tokenizer->get_token_line());
  1181. //must know when the local variable is declared
  1182. LocalVarNode *lv = alloc_node<LocalVarNode>();
  1183. lv->name=n;
  1184. p_block->statements.push_back(lv);
  1185. Node *assigned=NULL;
  1186. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  1187. tokenizer->advance();
  1188. Node *subexpr=NULL;
  1189. subexpr = _parse_and_reduce_expression(p_block,p_static);
  1190. if (!subexpr) {
  1191. if (_recover_from_completion()) {
  1192. break;
  1193. }
  1194. return;
  1195. }
  1196. lv->assign=subexpr;
  1197. assigned=subexpr;
  1198. } else {
  1199. ConstantNode *c = alloc_node<ConstantNode>();
  1200. c->value=Variant();
  1201. assigned = c;
  1202. }
  1203. IdentifierNode *id = alloc_node<IdentifierNode>();
  1204. id->name=n;
  1205. OperatorNode *op = alloc_node<OperatorNode>();
  1206. op->op=OperatorNode::OP_ASSIGN;
  1207. op->arguments.push_back(id);
  1208. op->arguments.push_back(assigned);
  1209. p_block->statements.push_back(op);
  1210. if (!_end_statement()) {
  1211. _set_error("Expected end of statement (var)");
  1212. return;
  1213. }
  1214. } break;
  1215. case GDTokenizer::TK_CF_IF: {
  1216. tokenizer->advance();
  1217. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1218. if (!condition) {
  1219. if (_recover_from_completion()) {
  1220. break;
  1221. }
  1222. return;
  1223. }
  1224. ControlFlowNode *cf_if = alloc_node<ControlFlowNode>();
  1225. cf_if->cf_type=ControlFlowNode::CF_IF;
  1226. cf_if->arguments.push_back(condition);
  1227. cf_if->body = alloc_node<BlockNode>();
  1228. cf_if->body->parent_block=p_block;
  1229. p_block->sub_blocks.push_back(cf_if->body);
  1230. if (!_enter_indent_block(cf_if->body)) {
  1231. _set_error("Expected intended block after 'if'");
  1232. p_block->end_line=tokenizer->get_token_line();
  1233. return;
  1234. }
  1235. current_block=cf_if->body;
  1236. _parse_block(cf_if->body,p_static);
  1237. current_block=p_block;
  1238. if (error_set)
  1239. return;
  1240. p_block->statements.push_back(cf_if);
  1241. while(true) {
  1242. while(tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  1243. tokenizer->advance();
  1244. }
  1245. if (tab_level.back()->get() < indent_level) { //not at current indent level
  1246. p_block->end_line=tokenizer->get_token_line();
  1247. return;
  1248. }
  1249. if (tokenizer->get_token()==GDTokenizer::TK_CF_ELIF) {
  1250. if (tab_level.back()->get() > indent_level) {
  1251. _set_error("Invalid indent");
  1252. return;
  1253. }
  1254. tokenizer->advance();
  1255. cf_if->body_else=alloc_node<BlockNode>();
  1256. cf_if->body_else->parent_block=p_block;
  1257. p_block->sub_blocks.push_back(cf_if->body_else);
  1258. ControlFlowNode *cf_else = alloc_node<ControlFlowNode>();
  1259. cf_else->cf_type=ControlFlowNode::CF_IF;
  1260. //condition
  1261. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1262. if (!condition) {
  1263. if (_recover_from_completion()) {
  1264. break;
  1265. }
  1266. return;
  1267. }
  1268. cf_else->arguments.push_back(condition);
  1269. cf_else->cf_type=ControlFlowNode::CF_IF;
  1270. cf_if->body_else->statements.push_back(cf_else);
  1271. cf_if=cf_else;
  1272. cf_if->body=alloc_node<BlockNode>();
  1273. cf_if->body->parent_block=p_block;
  1274. p_block->sub_blocks.push_back(cf_if->body);
  1275. if (!_enter_indent_block(cf_if->body)) {
  1276. _set_error("Expected indented block after 'elif'");
  1277. p_block->end_line=tokenizer->get_token_line();
  1278. return;
  1279. }
  1280. current_block=cf_else->body;
  1281. _parse_block(cf_else->body,p_static);
  1282. current_block=p_block;
  1283. if (error_set)
  1284. return;
  1285. } else if (tokenizer->get_token()==GDTokenizer::TK_CF_ELSE) {
  1286. if (tab_level.back()->get() > indent_level) {
  1287. _set_error("Invalid indent");
  1288. return;
  1289. }
  1290. tokenizer->advance();
  1291. cf_if->body_else=alloc_node<BlockNode>();
  1292. cf_if->body_else->parent_block=p_block;
  1293. p_block->sub_blocks.push_back(cf_if->body_else);
  1294. if (!_enter_indent_block(cf_if->body_else)) {
  1295. _set_error("Expected indented block after 'else'");
  1296. p_block->end_line=tokenizer->get_token_line();
  1297. return;
  1298. }
  1299. current_block=cf_if->body_else;
  1300. _parse_block(cf_if->body_else,p_static);
  1301. current_block=p_block;
  1302. if (error_set)
  1303. return;
  1304. break; //after else, exit
  1305. } else
  1306. break;
  1307. }
  1308. } break;
  1309. case GDTokenizer::TK_CF_WHILE: {
  1310. tokenizer->advance();
  1311. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1312. if (!condition) {
  1313. if (_recover_from_completion()) {
  1314. break;
  1315. }
  1316. return;
  1317. }
  1318. ControlFlowNode *cf_while = alloc_node<ControlFlowNode>();
  1319. cf_while->cf_type=ControlFlowNode::CF_WHILE;
  1320. cf_while->arguments.push_back(condition);
  1321. cf_while->body = alloc_node<BlockNode>();
  1322. cf_while->body->parent_block=p_block;
  1323. p_block->sub_blocks.push_back(cf_while->body);
  1324. if (!_enter_indent_block(cf_while->body)) {
  1325. _set_error("Expected indented block after 'while'");
  1326. p_block->end_line=tokenizer->get_token_line();
  1327. return;
  1328. }
  1329. current_block=cf_while->body;
  1330. _parse_block(cf_while->body,p_static);
  1331. current_block=p_block;
  1332. if (error_set)
  1333. return;
  1334. p_block->statements.push_back(cf_while);
  1335. } break;
  1336. case GDTokenizer::TK_CF_FOR: {
  1337. tokenizer->advance();
  1338. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1339. _set_error("identifier expected after 'for'");
  1340. }
  1341. IdentifierNode *id = alloc_node<IdentifierNode>();
  1342. id->name=tokenizer->get_token_identifier();
  1343. tokenizer->advance();
  1344. if (tokenizer->get_token()!=GDTokenizer::TK_OP_IN) {
  1345. _set_error("'in' expected after identifier");
  1346. return;
  1347. }
  1348. tokenizer->advance();
  1349. Node *container = _parse_and_reduce_expression(p_block,p_static);
  1350. if (!container) {
  1351. if (_recover_from_completion()) {
  1352. break;
  1353. }
  1354. return;
  1355. }
  1356. ControlFlowNode *cf_for = alloc_node<ControlFlowNode>();
  1357. cf_for->cf_type=ControlFlowNode::CF_FOR;
  1358. cf_for->arguments.push_back(id);
  1359. cf_for->arguments.push_back(container);
  1360. cf_for->body = alloc_node<BlockNode>();
  1361. cf_for->body->parent_block=p_block;
  1362. p_block->sub_blocks.push_back(cf_for->body);
  1363. if (!_enter_indent_block(cf_for->body)) {
  1364. _set_error("Expected indented block after 'for'");
  1365. p_block->end_line=tokenizer->get_token_line();
  1366. return;
  1367. }
  1368. current_block=cf_for->body;
  1369. _parse_block(cf_for->body,p_static);
  1370. current_block=p_block;
  1371. if (error_set)
  1372. return;
  1373. p_block->statements.push_back(cf_for);
  1374. } break;
  1375. case GDTokenizer::TK_CF_CONTINUE: {
  1376. tokenizer->advance();
  1377. ControlFlowNode *cf_continue = alloc_node<ControlFlowNode>();
  1378. cf_continue->cf_type=ControlFlowNode::CF_CONTINUE;
  1379. p_block->statements.push_back(cf_continue);
  1380. if (!_end_statement()) {
  1381. _set_error("Expected end of statement (continue)");
  1382. return;
  1383. }
  1384. } break;
  1385. case GDTokenizer::TK_CF_BREAK: {
  1386. tokenizer->advance();
  1387. ControlFlowNode *cf_break = alloc_node<ControlFlowNode>();
  1388. cf_break->cf_type=ControlFlowNode::CF_BREAK;
  1389. p_block->statements.push_back(cf_break);
  1390. if (!_end_statement()) {
  1391. _set_error("Expected end of statement (break)");
  1392. return;
  1393. }
  1394. } break;
  1395. case GDTokenizer::TK_CF_RETURN: {
  1396. tokenizer->advance();
  1397. ControlFlowNode *cf_return = alloc_node<ControlFlowNode>();
  1398. cf_return->cf_type=ControlFlowNode::CF_RETURN;
  1399. if (tokenizer->get_token()==GDTokenizer::TK_SEMICOLON || tokenizer->get_token()==GDTokenizer::TK_NEWLINE || tokenizer->get_token()==GDTokenizer::TK_EOF) {
  1400. //expect end of statement
  1401. p_block->statements.push_back(cf_return);
  1402. if (!_end_statement()) {
  1403. return;
  1404. }
  1405. } else {
  1406. //expect expression
  1407. Node *retexpr = _parse_and_reduce_expression(p_block,p_static);
  1408. if (!retexpr) {
  1409. if (_recover_from_completion()) {
  1410. break;
  1411. }
  1412. return;
  1413. }
  1414. cf_return->arguments.push_back(retexpr);
  1415. p_block->statements.push_back(cf_return);
  1416. if (!_end_statement()) {
  1417. _set_error("Expected end of statement after return expression.");
  1418. return;
  1419. }
  1420. }
  1421. } break;
  1422. case GDTokenizer::TK_PR_ASSERT: {
  1423. tokenizer->advance();
  1424. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1425. if (!condition) {
  1426. if (_recover_from_completion()) {
  1427. break;
  1428. }
  1429. return;
  1430. }
  1431. AssertNode *an = alloc_node<AssertNode>();
  1432. an->condition=condition;
  1433. p_block->statements.push_back(an);
  1434. if (!_end_statement()) {
  1435. _set_error("Expected end of statement after assert.");
  1436. return;
  1437. }
  1438. } break;
  1439. case GDTokenizer::TK_PR_BREAKPOINT: {
  1440. tokenizer->advance();
  1441. BreakpointNode *bn = alloc_node<BreakpointNode>();
  1442. p_block->statements.push_back(bn);
  1443. if (!_end_statement()) {
  1444. _set_error("Expected end of statement after breakpoint.");
  1445. return;
  1446. }
  1447. } break;
  1448. default: {
  1449. Node *expression = _parse_and_reduce_expression(p_block,p_static,false,true);
  1450. if (!expression) {
  1451. if (_recover_from_completion()) {
  1452. break;
  1453. }
  1454. return;
  1455. }
  1456. p_block->statements.push_back(expression);
  1457. if (!_end_statement()) {
  1458. _set_error("Expected end of statement after expression.");
  1459. return;
  1460. }
  1461. } break;
  1462. /*
  1463. case GDTokenizer::TK_CF_LOCAL: {
  1464. if (tokenizer->get_token(1)!=GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE ) {
  1465. _set_error("Expected ';' or <NewLine>.");
  1466. }
  1467. tokenizer->advance();
  1468. } break;
  1469. */
  1470. }
  1471. }
  1472. }
  1473. bool GDParser::_parse_newline() {
  1474. if (tokenizer->get_token(1)!=GDTokenizer::TK_EOF && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE) {
  1475. int indent = tokenizer->get_token_line_indent();
  1476. int current_indent = tab_level.back()->get();
  1477. if (indent>current_indent) {
  1478. _set_error("Unexpected indent.");
  1479. return false;
  1480. }
  1481. if (indent<current_indent) {
  1482. while(indent<current_indent) {
  1483. //exit block
  1484. if (tab_level.size()==1) {
  1485. _set_error("Invalid indent. BUG?");
  1486. return false;
  1487. }
  1488. tab_level.pop_back();
  1489. if (tab_level.back()->get()<indent) {
  1490. _set_error("Unindent does not match any outer indentation level.");
  1491. return false;
  1492. }
  1493. current_indent = tab_level.back()->get();
  1494. }
  1495. tokenizer->advance();
  1496. return false;
  1497. }
  1498. }
  1499. tokenizer->advance();
  1500. return true;
  1501. }
  1502. void GDParser::_parse_extends(ClassNode *p_class) {
  1503. if (p_class->extends_used) {
  1504. _set_error("'extends' already used for this class.");
  1505. return;
  1506. }
  1507. if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty()) {
  1508. _set_error("'extends' must be used before anything else.");
  1509. return;
  1510. }
  1511. p_class->extends_used=true;
  1512. tokenizer->advance();
  1513. if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token_type()==Variant::OBJECT) {
  1514. p_class->extends_class.push_back(Variant::get_type_name(Variant::OBJECT));
  1515. tokenizer->advance();
  1516. return;
  1517. }
  1518. // see if inheritance happens from a file
  1519. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT) {
  1520. Variant constant = tokenizer->get_token_constant();
  1521. if (constant.get_type()!=Variant::STRING) {
  1522. _set_error("'extends' constant must be a string.");
  1523. return;
  1524. }
  1525. p_class->extends_file=constant;
  1526. tokenizer->advance();
  1527. if (tokenizer->get_token()!=GDTokenizer::TK_PERIOD) {
  1528. return;
  1529. } else
  1530. tokenizer->advance();
  1531. }
  1532. while(true) {
  1533. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1534. _set_error("Invalid 'extends' syntax, expected string constant (path) and/or identifier (parent class).");
  1535. return;
  1536. }
  1537. StringName identifier=tokenizer->get_token_identifier();
  1538. p_class->extends_class.push_back(identifier);
  1539. tokenizer->advance(1);
  1540. if (tokenizer->get_token()!=GDTokenizer::TK_PERIOD)
  1541. return;
  1542. }
  1543. }
  1544. void GDParser::_parse_class(ClassNode *p_class) {
  1545. int indent_level = tab_level.back()->get();
  1546. while(true) {
  1547. GDTokenizer::Token token = tokenizer->get_token();
  1548. if (error_set)
  1549. return;
  1550. if (indent_level>tab_level.back()->get()) {
  1551. p_class->end_line=tokenizer->get_token_line();
  1552. return; //go back a level
  1553. }
  1554. switch(token) {
  1555. case GDTokenizer::TK_EOF:
  1556. p_class->end_line=tokenizer->get_token_line();
  1557. case GDTokenizer::TK_ERROR: {
  1558. return; //go back
  1559. //end of file!
  1560. } break;
  1561. case GDTokenizer::TK_NEWLINE: {
  1562. if (!_parse_newline()) {
  1563. if (!error_set) {
  1564. p_class->end_line=tokenizer->get_token_line();
  1565. }
  1566. return;
  1567. }
  1568. } break;
  1569. case GDTokenizer::TK_PR_EXTENDS: {
  1570. _parse_extends(p_class);
  1571. if (error_set)
  1572. return;
  1573. if (!_end_statement()) {
  1574. _set_error("Expected end of statement after extends");
  1575. return;
  1576. }
  1577. } break;
  1578. case GDTokenizer::TK_PR_TOOL: {
  1579. if (p_class->tool) {
  1580. _set_error("tool used more than once");
  1581. return;
  1582. }
  1583. p_class->tool=true;
  1584. tokenizer->advance();
  1585. } break;
  1586. case GDTokenizer::TK_PR_CLASS: {
  1587. //class inside class :D
  1588. StringName name;
  1589. StringName extends;
  1590. if (tokenizer->get_token(1)!=GDTokenizer::TK_IDENTIFIER) {
  1591. _set_error("'class' syntax: 'class <Name>:' or 'class <Name> extends <BaseClass>:'");
  1592. return;
  1593. }
  1594. name = tokenizer->get_token_identifier(1);
  1595. tokenizer->advance(2);
  1596. ClassNode *newclass = alloc_node<ClassNode>();
  1597. newclass->initializer = alloc_node<BlockNode>();
  1598. newclass->initializer->parent_class=newclass;
  1599. newclass->ready = alloc_node<BlockNode>();
  1600. newclass->ready->parent_class=newclass;
  1601. newclass->name=name;
  1602. newclass->owner=p_class;
  1603. p_class->subclasses.push_back(newclass);
  1604. if (tokenizer->get_token()==GDTokenizer::TK_PR_EXTENDS) {
  1605. _parse_extends(newclass);
  1606. if (error_set)
  1607. return;
  1608. }
  1609. if (!_enter_indent_block()) {
  1610. _set_error("Indented block expected.");
  1611. return;
  1612. }
  1613. current_class=newclass;
  1614. _parse_class(newclass);
  1615. current_class=p_class;
  1616. } break;
  1617. /* this is for functions....
  1618. case GDTokenizer::TK_CF_PASS: {
  1619. tokenizer->advance(1);
  1620. } break;
  1621. */
  1622. case GDTokenizer::TK_PR_STATIC: {
  1623. tokenizer->advance();
  1624. if (tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
  1625. _set_error("Expected 'func'.");
  1626. return;
  1627. }
  1628. }; //fallthrough to function
  1629. case GDTokenizer::TK_PR_FUNCTION: {
  1630. bool _static=false;
  1631. pending_newline=-1;
  1632. if (tokenizer->get_token(-1)==GDTokenizer::TK_PR_STATIC) {
  1633. _static=true;
  1634. }
  1635. tokenizer->advance();
  1636. StringName name;
  1637. if (_get_completable_identifier(COMPLETION_VIRTUAL_FUNC,name)) {
  1638. }
  1639. if (name==StringName()) {
  1640. _set_error("Expected identifier after 'func' (syntax: 'func <identifier>([arguments]):' ).");
  1641. return;
  1642. }
  1643. for(int i=0;i<p_class->functions.size();i++) {
  1644. if (p_class->functions[i]->name==name) {
  1645. _set_error("Function '"+String(name)+"' already exists in this class (at line: "+itos(p_class->functions[i]->line)+").");
  1646. }
  1647. }
  1648. for(int i=0;i<p_class->static_functions.size();i++) {
  1649. if (p_class->static_functions[i]->name==name) {
  1650. _set_error("Function '"+String(name)+"' already exists in this class (at line: "+itos(p_class->static_functions[i]->line)+").");
  1651. }
  1652. }
  1653. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_OPEN) {
  1654. _set_error("Expected '(' after identifier (syntax: 'func <identifier>([arguments]):' ).");
  1655. return;
  1656. }
  1657. tokenizer->advance();
  1658. Vector<StringName> arguments;
  1659. Vector<Node*> default_values;
  1660. int fnline = tokenizer->get_token_line();
  1661. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1662. //has arguments
  1663. bool defaulting=false;
  1664. while(true) {
  1665. if (tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  1666. tokenizer->advance();
  1667. continue;
  1668. }
  1669. if (tokenizer->get_token()==GDTokenizer::TK_PR_VAR) {
  1670. tokenizer->advance(); //var before the identifier is allowed
  1671. }
  1672. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1673. _set_error("Expected identifier for argument.");
  1674. return;
  1675. }
  1676. StringName argname=tokenizer->get_token_identifier();
  1677. arguments.push_back(argname);
  1678. tokenizer->advance();
  1679. if (defaulting && tokenizer->get_token()!=GDTokenizer::TK_OP_ASSIGN) {
  1680. _set_error("Default parameter expected.");
  1681. return;
  1682. }
  1683. //tokenizer->advance();
  1684. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  1685. defaulting=true;
  1686. tokenizer->advance(1);
  1687. Node *defval=NULL;
  1688. defval=_parse_and_reduce_expression(p_class,_static);
  1689. if (!defval || error_set)
  1690. return;
  1691. OperatorNode *on = alloc_node<OperatorNode>();
  1692. on->op=OperatorNode::OP_ASSIGN;
  1693. IdentifierNode *in = alloc_node<IdentifierNode>();
  1694. in->name=argname;
  1695. on->arguments.push_back(in);
  1696. on->arguments.push_back(defval);
  1697. /* no ..
  1698. if (defval->type!=Node::TYPE_CONSTANT) {
  1699. _set_error("default argument must be constant");
  1700. }
  1701. */
  1702. default_values.push_back(on);
  1703. }
  1704. while (tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  1705. tokenizer->advance();
  1706. }
  1707. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1708. tokenizer->advance();
  1709. continue;
  1710. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1711. _set_error("Expected ',' or ')'.");
  1712. return;
  1713. }
  1714. break;
  1715. }
  1716. }
  1717. tokenizer->advance();
  1718. BlockNode *block = alloc_node<BlockNode>();
  1719. block->parent_class=p_class;
  1720. if (name=="_init") {
  1721. if (p_class->extends_used) {
  1722. OperatorNode *cparent = alloc_node<OperatorNode>();
  1723. cparent->op=OperatorNode::OP_PARENT_CALL;
  1724. block->statements.push_back(cparent);
  1725. IdentifierNode *id = alloc_node<IdentifierNode>();
  1726. id->name="_init";
  1727. cparent->arguments.push_back(id);
  1728. if (tokenizer->get_token()==GDTokenizer::TK_PERIOD) {
  1729. tokenizer->advance();
  1730. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_OPEN) {
  1731. _set_error("expected '(' for parent constructor arguments.");
  1732. }
  1733. tokenizer->advance();
  1734. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1735. //has arguments
  1736. parenthesis ++;
  1737. while(true) {
  1738. Node *arg = _parse_and_reduce_expression(p_class,_static);
  1739. cparent->arguments.push_back(arg);
  1740. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1741. tokenizer->advance();
  1742. continue;
  1743. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1744. _set_error("Expected ',' or ')'.");
  1745. return;
  1746. }
  1747. break;
  1748. }
  1749. parenthesis --;
  1750. }
  1751. tokenizer->advance();
  1752. }
  1753. } else {
  1754. if (tokenizer->get_token()==GDTokenizer::TK_PERIOD) {
  1755. _set_error("Parent constructor call found for a class without inheritance.");
  1756. return;
  1757. }
  1758. }
  1759. }
  1760. if (!_enter_indent_block(block)) {
  1761. _set_error("Indented block expected.");
  1762. return;
  1763. }
  1764. FunctionNode *function = alloc_node<FunctionNode>();
  1765. function->name=name;
  1766. function->arguments=arguments;
  1767. function->default_values=default_values;
  1768. function->_static=_static;
  1769. function->line=fnline;
  1770. if (_static)
  1771. p_class->static_functions.push_back(function);
  1772. else
  1773. p_class->functions.push_back(function);
  1774. current_function=function;
  1775. function->body=block;
  1776. current_block=block;
  1777. _parse_block(block,_static);
  1778. current_block=NULL;
  1779. //arguments
  1780. } break;
  1781. case GDTokenizer::TK_PR_SIGNAL: {
  1782. tokenizer->advance();
  1783. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1784. _set_error("Expected identifier after 'signal'.");
  1785. return;
  1786. }
  1787. ClassNode::Signal sig;
  1788. sig.name = tokenizer->get_token_identifier();
  1789. tokenizer->advance();
  1790. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_OPEN) {
  1791. tokenizer->advance();
  1792. while(true) {
  1793. if (tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  1794. tokenizer->advance();
  1795. continue;
  1796. }
  1797. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1798. tokenizer->advance();
  1799. break;
  1800. }
  1801. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1802. _set_error("Expected identifier in signal argument.");
  1803. return;
  1804. }
  1805. sig.arguments.push_back(tokenizer->get_token_identifier());
  1806. tokenizer->advance();
  1807. while (tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  1808. tokenizer->advance();
  1809. }
  1810. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1811. tokenizer->advance();
  1812. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1813. _set_error("Expected ',' or ')' after signal parameter identifier.");
  1814. return;
  1815. }
  1816. }
  1817. }
  1818. p_class->_signals.push_back(sig);
  1819. if (!_end_statement()) {
  1820. _set_error("Expected end of statement (signal)");
  1821. return;
  1822. }
  1823. } break;
  1824. case GDTokenizer::TK_PR_EXPORT: {
  1825. tokenizer->advance();
  1826. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_OPEN) {
  1827. tokenizer->advance();
  1828. if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_TYPE) {
  1829. Variant::Type type = tokenizer->get_token_type();
  1830. if (type==Variant::NIL) {
  1831. _set_error("Can't export null type.");
  1832. return;
  1833. }
  1834. current_export.type=type;
  1835. current_export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
  1836. tokenizer->advance();
  1837. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1838. // hint expected next!
  1839. tokenizer->advance();
  1840. switch(current_export.type) {
  1841. case Variant::INT: {
  1842. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="FLAGS") {
  1843. current_export.hint=PROPERTY_HINT_ALL_FLAGS;
  1844. tokenizer->advance();
  1845. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1846. break;
  1847. }
  1848. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA)
  1849. {
  1850. _set_error("Expected ')' or ',' in bit flags hint.");
  1851. return;
  1852. }
  1853. current_export.hint=PROPERTY_HINT_FLAGS;
  1854. tokenizer->advance();
  1855. bool first = true;
  1856. while(true) {
  1857. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  1858. current_export=PropertyInfo();
  1859. _set_error("Expected a string constant in named bit flags hint.");
  1860. return;
  1861. }
  1862. String c = tokenizer->get_token_constant();
  1863. if (!first)
  1864. current_export.hint_string+=",";
  1865. else
  1866. first=false;
  1867. current_export.hint_string+=c.xml_escape();
  1868. tokenizer->advance();
  1869. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1870. break;
  1871. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1872. current_export=PropertyInfo();
  1873. _set_error("Expected ')' or ',' in named bit flags hint.");
  1874. return;
  1875. }
  1876. tokenizer->advance();
  1877. }
  1878. break;
  1879. }
  1880. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type()==Variant::STRING) {
  1881. //enumeration
  1882. current_export.hint=PROPERTY_HINT_ENUM;
  1883. bool first=true;
  1884. while(true) {
  1885. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  1886. current_export=PropertyInfo();
  1887. _set_error("Expected a string constant in enumeration hint.");
  1888. return;
  1889. }
  1890. String c = tokenizer->get_token_constant();
  1891. if (!first)
  1892. current_export.hint_string+=",";
  1893. else
  1894. first=false;
  1895. current_export.hint_string+=c.xml_escape();
  1896. tokenizer->advance();
  1897. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1898. break;
  1899. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1900. current_export=PropertyInfo();
  1901. _set_error("Expected ')' or ',' in enumeration hint.");
  1902. return;
  1903. }
  1904. tokenizer->advance();
  1905. }
  1906. break;
  1907. }
  1908. }; //fallthrough to use the same
  1909. case Variant::REAL: {
  1910. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="EASE") {
  1911. current_export.hint=PROPERTY_HINT_EXP_EASING;
  1912. tokenizer->advance();
  1913. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1914. _set_error("Expected ')' in hint.");
  1915. return;
  1916. }
  1917. break;
  1918. }
  1919. // range
  1920. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="EXP") {
  1921. current_export.hint=PROPERTY_HINT_EXP_RANGE;
  1922. tokenizer->advance();
  1923. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1924. break;
  1925. else if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1926. _set_error("Expected ')' or ',' in exponential range hint.");
  1927. return;
  1928. }
  1929. tokenizer->advance();
  1930. }
  1931. else
  1932. current_export.hint=PROPERTY_HINT_RANGE;
  1933. float sign=1.0;
  1934. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  1935. sign=-1;
  1936. tokenizer->advance();
  1937. }
  1938. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  1939. current_export=PropertyInfo();
  1940. _set_error("Expected a range in numeric hint.");
  1941. return;
  1942. }
  1943. current_export.hint_string=rtos(sign*double(tokenizer->get_token_constant()));
  1944. tokenizer->advance();
  1945. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1946. current_export.hint_string="0,"+current_export.hint_string;
  1947. break;
  1948. }
  1949. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1950. current_export=PropertyInfo();
  1951. _set_error("Expected ',' or ')' in numeric range hint.");
  1952. return;
  1953. }
  1954. tokenizer->advance();
  1955. sign=1.0;
  1956. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  1957. sign=-1;
  1958. tokenizer->advance();
  1959. }
  1960. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  1961. current_export=PropertyInfo();
  1962. _set_error("Expected a number as upper bound in numeric range hint.");
  1963. return;
  1964. }
  1965. current_export.hint_string+=","+rtos(sign*double(tokenizer->get_token_constant()));
  1966. tokenizer->advance();
  1967. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  1968. break;
  1969. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  1970. current_export=PropertyInfo();
  1971. _set_error("Expected ',' or ')' in numeric range hint.");
  1972. return;
  1973. }
  1974. tokenizer->advance();
  1975. sign=1.0;
  1976. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  1977. sign=-1;
  1978. tokenizer->advance();
  1979. }
  1980. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  1981. current_export=PropertyInfo();
  1982. _set_error("Expected a number as step in numeric range hint.");
  1983. return;
  1984. }
  1985. current_export.hint_string+=","+rtos(sign*double(tokenizer->get_token_constant()));
  1986. tokenizer->advance();
  1987. } break;
  1988. case Variant::STRING: {
  1989. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type()==Variant::STRING) {
  1990. //enumeration
  1991. current_export.hint=PROPERTY_HINT_ENUM;
  1992. bool first=true;
  1993. while(true) {
  1994. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  1995. current_export=PropertyInfo();
  1996. _set_error("Expected a string constant in enumeration hint.");
  1997. return;
  1998. }
  1999. String c = tokenizer->get_token_constant();
  2000. if (!first)
  2001. current_export.hint_string+=",";
  2002. else
  2003. first=false;
  2004. current_export.hint_string+=c.xml_escape();
  2005. tokenizer->advance();
  2006. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2007. break;
  2008. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  2009. current_export=PropertyInfo();
  2010. _set_error("Expected ')' or ',' in enumeration hint.");
  2011. return;
  2012. }
  2013. tokenizer->advance();
  2014. }
  2015. break;
  2016. }
  2017. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="DIR") {
  2018. tokenizer->advance();
  2019. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2020. current_export.hint=PROPERTY_HINT_DIR;
  2021. else if (tokenizer->get_token()==GDTokenizer::TK_COMMA ) {
  2022. tokenizer->advance();
  2023. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier()=="GLOBAL")) {
  2024. _set_error("Expected 'GLOBAL' after comma in directory hint.");
  2025. return;
  2026. }
  2027. if (!p_class->tool) {
  2028. _set_error("Global filesystem hints may only be used in tool scripts.");
  2029. return;
  2030. }
  2031. current_export.hint=PROPERTY_HINT_GLOBAL_DIR;
  2032. tokenizer->advance();
  2033. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2034. _set_error("Expected ')' in hint.");
  2035. return;
  2036. }
  2037. }
  2038. else {
  2039. _set_error("Expected ')' or ',' in hint.");
  2040. return;
  2041. }
  2042. break;
  2043. }
  2044. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="FILE") {
  2045. current_export.hint=PROPERTY_HINT_FILE;
  2046. tokenizer->advance();
  2047. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  2048. tokenizer->advance();
  2049. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="GLOBAL") {
  2050. if (!p_class->tool) {
  2051. _set_error("Global filesystem hints may only be used in tool scripts.");
  2052. return;
  2053. }
  2054. current_export.hint=PROPERTY_HINT_GLOBAL_FILE;
  2055. tokenizer->advance();
  2056. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2057. break;
  2058. else if (tokenizer->get_token()==GDTokenizer::TK_COMMA)
  2059. tokenizer->advance();
  2060. else {
  2061. _set_error("Expected ')' or ',' in hint.");
  2062. return;
  2063. }
  2064. }
  2065. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  2066. if (current_export.hint==PROPERTY_HINT_GLOBAL_FILE)
  2067. _set_error("Expected string constant with filter");
  2068. else
  2069. _set_error("Expected 'GLOBAL' or string constant with filter");
  2070. return;
  2071. }
  2072. current_export.hint_string=tokenizer->get_token_constant();
  2073. tokenizer->advance();
  2074. }
  2075. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2076. _set_error("Expected ')' in hint.");
  2077. return;
  2078. }
  2079. break;
  2080. }
  2081. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="MULTILINE") {
  2082. current_export.hint=PROPERTY_HINT_MULTILINE_TEXT;
  2083. tokenizer->advance();
  2084. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2085. _set_error("Expected ')' in hint.");
  2086. return;
  2087. }
  2088. break;
  2089. }
  2090. } break;
  2091. case Variant::COLOR: {
  2092. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER ) {
  2093. current_export=PropertyInfo();
  2094. _set_error("Color type hint expects RGB or RGBA as hints");
  2095. return;
  2096. }
  2097. String identifier = tokenizer->get_token_identifier();
  2098. if (identifier=="RGB") {
  2099. current_export.hint=PROPERTY_HINT_COLOR_NO_ALPHA;
  2100. } else if (identifier=="RGBA") {
  2101. //none
  2102. } else {
  2103. current_export=PropertyInfo();
  2104. _set_error("Color type hint expects RGB or RGBA as hints");
  2105. return;
  2106. }
  2107. tokenizer->advance();
  2108. } break;
  2109. default: {
  2110. current_export=PropertyInfo();
  2111. _set_error("Type '"+Variant::get_type_name(type)+"' can't take hints.");
  2112. return;
  2113. } break;
  2114. }
  2115. }
  2116. } else if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER) {
  2117. String identifier = tokenizer->get_token_identifier();
  2118. if (!ObjectTypeDB::is_type(identifier,"Resource")) {
  2119. current_export=PropertyInfo();
  2120. _set_error("Export hint not a type or resource.");
  2121. }
  2122. current_export.type=Variant::OBJECT;
  2123. current_export.hint=PROPERTY_HINT_RESOURCE_TYPE;
  2124. current_export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
  2125. current_export.hint_string=identifier;
  2126. tokenizer->advance();
  2127. }
  2128. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2129. current_export=PropertyInfo();
  2130. _set_error("Expected ')' or ',' after export hint.");
  2131. return;
  2132. }
  2133. tokenizer->advance();
  2134. }
  2135. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
  2136. current_export=PropertyInfo();
  2137. _set_error("Expected 'var'.");
  2138. return;
  2139. }
  2140. }; //fallthrough to var
  2141. case GDTokenizer::TK_PR_ONREADY: {
  2142. if (token==GDTokenizer::TK_PR_ONREADY) {
  2143. //may be fallthrough from export, ignore if so
  2144. tokenizer->advance();
  2145. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
  2146. _set_error("Expected 'var'.");
  2147. return;
  2148. }
  2149. }
  2150. }; //fallthrough to var
  2151. case GDTokenizer::TK_PR_VAR: {
  2152. //variale declaration and (eventual) initialization
  2153. ClassNode::Member member;
  2154. bool autoexport = tokenizer->get_token(-1)==GDTokenizer::TK_PR_EXPORT;
  2155. if (current_export.type!=Variant::NIL) {
  2156. member._export=current_export;
  2157. current_export=PropertyInfo();
  2158. }
  2159. bool onready = tokenizer->get_token(-1)==GDTokenizer::TK_PR_ONREADY;
  2160. tokenizer->advance();
  2161. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2162. _set_error("Expected identifier for member variable name.");
  2163. return;
  2164. }
  2165. member.identifier=tokenizer->get_token_identifier();
  2166. member.expression=NULL;
  2167. member._export.name=member.identifier;
  2168. member.line=tokenizer->get_token_line();
  2169. tokenizer->advance();
  2170. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  2171. #ifdef DEBUG_ENABLED
  2172. int line = tokenizer->get_token_line();
  2173. #endif
  2174. tokenizer->advance();
  2175. Node *subexpr=NULL;
  2176. subexpr = _parse_and_reduce_expression(p_class,false,autoexport);
  2177. if (!subexpr) {
  2178. if (_recover_from_completion()) {
  2179. break;
  2180. }
  2181. return;
  2182. }
  2183. //discourage common error
  2184. if (!onready && subexpr->type==Node::TYPE_OPERATOR) {
  2185. OperatorNode *op=static_cast<OperatorNode*>(subexpr);
  2186. if (op->op==OperatorNode::OP_CALL && op->arguments[0]->type==Node::TYPE_SELF && op->arguments[1]->type==Node::TYPE_IDENTIFIER) {
  2187. IdentifierNode *id=static_cast<IdentifierNode*>(op->arguments[1]);
  2188. if (id->name=="get_node") {
  2189. _set_error("Use 'onready var "+String(member.identifier)+" = get_node(..)' instead");
  2190. return;
  2191. }
  2192. }
  2193. }
  2194. member.expression=subexpr;
  2195. if (autoexport) {
  2196. if (1)/*(subexpr->type==Node::TYPE_ARRAY) {
  2197. member._export.type=Variant::ARRAY;
  2198. } else if (subexpr->type==Node::TYPE_DICTIONARY) {
  2199. member._export.type=Variant::DICTIONARY;
  2200. } else*/ {
  2201. if (subexpr->type!=Node::TYPE_CONSTANT) {
  2202. _set_error("Type-less export needs a constant expression assigned to infer type.");
  2203. return;
  2204. }
  2205. ConstantNode *cn = static_cast<ConstantNode*>(subexpr);
  2206. if (cn->value.get_type()==Variant::NIL) {
  2207. _set_error("Can't accept a null constant expression for infering export type.");
  2208. return;
  2209. }
  2210. member._export.type=cn->value.get_type();
  2211. member._export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
  2212. if (cn->value.get_type()==Variant::OBJECT) {
  2213. Object *obj = cn->value;
  2214. Resource *res = obj->cast_to<Resource>();
  2215. if(res==NULL) {
  2216. _set_error("Exported constant not a type or resource.");
  2217. return;
  2218. }
  2219. member._export.hint=PROPERTY_HINT_RESOURCE_TYPE;
  2220. member._export.hint_string=res->get_type();
  2221. }
  2222. }
  2223. }
  2224. #ifdef TOOLS_ENABLED
  2225. if (subexpr->type==Node::TYPE_CONSTANT && member._export.type!=Variant::NIL) {
  2226. ConstantNode *cn = static_cast<ConstantNode*>(subexpr);
  2227. if (cn->value.get_type()!=Variant::NIL) {
  2228. member.default_value=cn->value;
  2229. }
  2230. }
  2231. #endif
  2232. IdentifierNode *id = alloc_node<IdentifierNode>();
  2233. id->name=member.identifier;
  2234. OperatorNode *op = alloc_node<OperatorNode>();
  2235. op->op=OperatorNode::OP_INIT_ASSIGN;
  2236. op->arguments.push_back(id);
  2237. op->arguments.push_back(subexpr);
  2238. #ifdef DEBUG_ENABLED
  2239. NewLineNode *nl = alloc_node<NewLineNode>();
  2240. nl->line=line;
  2241. if (onready)
  2242. p_class->ready->statements.push_back(nl);
  2243. else
  2244. p_class->initializer->statements.push_back(nl);
  2245. #endif
  2246. if (onready)
  2247. p_class->ready->statements.push_back(op);
  2248. else
  2249. p_class->initializer->statements.push_back(op);
  2250. } else {
  2251. if (autoexport) {
  2252. _set_error("Type-less export needs a constant expression assigned to infer type.");
  2253. return;
  2254. }
  2255. }
  2256. if (tokenizer->get_token()==GDTokenizer::TK_PR_SETGET) {
  2257. tokenizer->advance();
  2258. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  2259. //just comma means using only getter
  2260. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2261. _set_error("Expected identifier for setter function after 'notify'.");
  2262. }
  2263. member.setter=tokenizer->get_token_identifier();
  2264. tokenizer->advance();
  2265. }
  2266. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  2267. //there is a getter
  2268. tokenizer->advance();
  2269. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2270. _set_error("Expected identifier for getter function after ','.");
  2271. }
  2272. member.getter=tokenizer->get_token_identifier();
  2273. tokenizer->advance();
  2274. }
  2275. }
  2276. p_class->variables.push_back(member);
  2277. if (!_end_statement()) {
  2278. _set_error("Expected end of statement (continue)");
  2279. return;
  2280. }
  2281. } break;
  2282. case GDTokenizer::TK_PR_CONST: {
  2283. //variale declaration and (eventual) initialization
  2284. ClassNode::Constant constant;
  2285. tokenizer->advance();
  2286. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2287. _set_error("Expected name (identifier) for constant.");
  2288. return;
  2289. }
  2290. constant.identifier=tokenizer->get_token_identifier();
  2291. tokenizer->advance();
  2292. if (tokenizer->get_token()!=GDTokenizer::TK_OP_ASSIGN) {
  2293. _set_error("Constant expects assignment.");
  2294. return;
  2295. }
  2296. tokenizer->advance();
  2297. Node *subexpr=NULL;
  2298. subexpr = _parse_and_reduce_expression(p_class,true,true);
  2299. if (!subexpr) {
  2300. if (_recover_from_completion()) {
  2301. break;
  2302. }
  2303. return;
  2304. }
  2305. if (subexpr->type!=Node::TYPE_CONSTANT) {
  2306. _set_error("Expected constant expression");
  2307. }
  2308. constant.expression=subexpr;
  2309. p_class->constant_expressions.push_back(constant);
  2310. if (!_end_statement()) {
  2311. _set_error("Expected end of statement (constant)");
  2312. return;
  2313. }
  2314. } break;
  2315. default: {
  2316. _set_error(String()+"Unexpected token: "+tokenizer->get_token_name(tokenizer->get_token())+":"+tokenizer->get_token_identifier());
  2317. return;
  2318. } break;
  2319. }
  2320. }
  2321. }
  2322. void GDParser::_set_error(const String& p_error, int p_line, int p_column) {
  2323. if (error_set)
  2324. return; //allow no further errors
  2325. error=p_error;
  2326. error_line=p_line<0?tokenizer->get_token_line():p_line;
  2327. error_column=p_column<0?tokenizer->get_token_column():p_column;
  2328. error_set=true;
  2329. }
  2330. String GDParser::get_error() const {
  2331. return error;
  2332. }
  2333. int GDParser::get_error_line() const {
  2334. return error_line;
  2335. }
  2336. int GDParser::get_error_column() const {
  2337. return error_column;
  2338. }
  2339. Error GDParser::_parse(const String& p_base_path) {
  2340. base_path=p_base_path;
  2341. clear();
  2342. //assume class
  2343. ClassNode *main_class = alloc_node<ClassNode>();
  2344. main_class->initializer = alloc_node<BlockNode>();
  2345. main_class->initializer->parent_class=main_class;
  2346. main_class->ready = alloc_node<BlockNode>();
  2347. main_class->ready->parent_class=main_class;
  2348. current_class=main_class;
  2349. _parse_class(main_class);
  2350. if (tokenizer->get_token()==GDTokenizer::TK_ERROR) {
  2351. error_set=false;
  2352. _set_error("Parse Error: "+tokenizer->get_token_error());
  2353. }
  2354. if (error_set) {
  2355. return ERR_PARSE_ERROR;
  2356. }
  2357. return OK;
  2358. }
  2359. Error GDParser::parse_bytecode(const Vector<uint8_t> &p_bytecode,const String& p_base_path, const String &p_self_path) {
  2360. for_completion=false;
  2361. validating=false;
  2362. completion_type=COMPLETION_NONE;
  2363. completion_node=NULL;
  2364. completion_class=NULL;
  2365. completion_function=NULL;
  2366. completion_block=NULL;
  2367. completion_found=false;
  2368. current_block=NULL;
  2369. current_class=NULL;
  2370. current_function=NULL;
  2371. self_path=p_self_path;
  2372. GDTokenizerBuffer *tb = memnew( GDTokenizerBuffer );
  2373. tb->set_code_buffer(p_bytecode);
  2374. tokenizer=tb;
  2375. Error ret = _parse(p_base_path);
  2376. memdelete(tb);
  2377. tokenizer=NULL;
  2378. return ret;
  2379. }
  2380. Error GDParser::parse(const String& p_code, const String& p_base_path, bool p_just_validate, const String &p_self_path,bool p_for_completion) {
  2381. completion_type=COMPLETION_NONE;
  2382. completion_node=NULL;
  2383. completion_class=NULL;
  2384. completion_function=NULL;
  2385. completion_block=NULL;
  2386. completion_found=false;
  2387. current_block=NULL;
  2388. current_class=NULL;
  2389. current_function=NULL;
  2390. self_path=p_self_path;
  2391. GDTokenizerText *tt = memnew( GDTokenizerText );
  2392. tt->set_code(p_code);
  2393. validating=p_just_validate;
  2394. for_completion=p_for_completion;
  2395. tokenizer=tt;
  2396. Error ret = _parse(p_base_path);
  2397. memdelete(tt);
  2398. tokenizer=NULL;
  2399. return ret;
  2400. }
  2401. bool GDParser::is_tool_script() const {
  2402. return (head && head->type==Node::TYPE_CLASS && static_cast<const ClassNode*>(head)->tool);
  2403. }
  2404. const GDParser::Node *GDParser::get_parse_tree() const {
  2405. return head;
  2406. }
  2407. void GDParser::clear() {
  2408. while(list) {
  2409. Node *l=list;
  2410. list=list->next;
  2411. memdelete(l);
  2412. }
  2413. head=NULL;
  2414. list=NULL;
  2415. completion_type=COMPLETION_NONE;
  2416. completion_node=NULL;
  2417. completion_class=NULL;
  2418. completion_function=NULL;
  2419. completion_block=NULL;
  2420. current_block=NULL;
  2421. current_class=NULL;
  2422. completion_found=false;
  2423. current_function=NULL;
  2424. validating=false;
  2425. for_completion=false;
  2426. error_set=false;
  2427. tab_level.clear();
  2428. tab_level.push_back(0);
  2429. error_line=0;
  2430. error_column=0;
  2431. pending_newline=-1;
  2432. parenthesis=0;
  2433. current_export.type=Variant::NIL;
  2434. error="";
  2435. }
  2436. GDParser::CompletionType GDParser::get_completion_type() {
  2437. return completion_type;
  2438. }
  2439. StringName GDParser::get_completion_cursor() {
  2440. return completion_cursor;
  2441. }
  2442. int GDParser::get_completion_line() {
  2443. return completion_line;
  2444. }
  2445. Variant::Type GDParser::get_completion_built_in_constant(){
  2446. return completion_built_in_constant;
  2447. }
  2448. GDParser::Node *GDParser::get_completion_node(){
  2449. return completion_node;
  2450. }
  2451. GDParser::BlockNode *GDParser::get_completion_block() {
  2452. return completion_block;
  2453. }
  2454. GDParser::ClassNode *GDParser::get_completion_class(){
  2455. return completion_class;
  2456. }
  2457. GDParser::FunctionNode *GDParser::get_completion_function(){
  2458. return completion_function;
  2459. }
  2460. int GDParser::get_completion_argument_index() {
  2461. return completion_argument;
  2462. }
  2463. GDParser::GDParser() {
  2464. head=NULL;
  2465. list=NULL;
  2466. tokenizer=NULL;
  2467. pending_newline=-1;
  2468. clear();
  2469. }
  2470. GDParser::~GDParser() {
  2471. clear();
  2472. }