gd_parser.cpp 98 KB

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