gd_parser.cpp 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471
  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. p_block->variables.push_back(n); //line?
  1336. p_block->variable_lines.push_back(tokenizer->get_token_line());
  1337. //must know when the local variable is declared
  1338. LocalVarNode *lv = alloc_node<LocalVarNode>();
  1339. lv->name = n;
  1340. p_block->statements.push_back(lv);
  1341. Node *assigned = NULL;
  1342. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  1343. tokenizer->advance();
  1344. Node *subexpr = NULL;
  1345. subexpr = _parse_and_reduce_expression(p_block, p_static);
  1346. if (!subexpr) {
  1347. if (_recover_from_completion()) {
  1348. break;
  1349. }
  1350. return;
  1351. }
  1352. lv->assign = subexpr;
  1353. assigned = subexpr;
  1354. } else {
  1355. ConstantNode *c = alloc_node<ConstantNode>();
  1356. c->value = Variant();
  1357. assigned = c;
  1358. }
  1359. IdentifierNode *id = alloc_node<IdentifierNode>();
  1360. id->name = n;
  1361. OperatorNode *op = alloc_node<OperatorNode>();
  1362. op->op = OperatorNode::OP_ASSIGN;
  1363. op->arguments.push_back(id);
  1364. op->arguments.push_back(assigned);
  1365. p_block->statements.push_back(op);
  1366. if (!_end_statement()) {
  1367. _set_error("Expected end of statement (var)");
  1368. return;
  1369. }
  1370. } break;
  1371. case GDTokenizer::TK_CF_IF: {
  1372. tokenizer->advance();
  1373. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  1374. if (!condition) {
  1375. if (_recover_from_completion()) {
  1376. break;
  1377. }
  1378. return;
  1379. }
  1380. ControlFlowNode *cf_if = alloc_node<ControlFlowNode>();
  1381. cf_if->cf_type = ControlFlowNode::CF_IF;
  1382. cf_if->arguments.push_back(condition);
  1383. cf_if->body = alloc_node<BlockNode>();
  1384. cf_if->body->parent_block = p_block;
  1385. p_block->sub_blocks.push_back(cf_if->body);
  1386. if (!_enter_indent_block(cf_if->body)) {
  1387. _set_error("Expected intended block after 'if'");
  1388. p_block->end_line = tokenizer->get_token_line();
  1389. return;
  1390. }
  1391. current_block = cf_if->body;
  1392. _parse_block(cf_if->body, p_static);
  1393. current_block = p_block;
  1394. if (error_set)
  1395. return;
  1396. p_block->statements.push_back(cf_if);
  1397. while (true) {
  1398. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  1399. tokenizer->advance();
  1400. }
  1401. if (tab_level.back()->get() < indent_level) { //not at current indent level
  1402. p_block->end_line = tokenizer->get_token_line();
  1403. return;
  1404. }
  1405. if (tokenizer->get_token() == GDTokenizer::TK_CF_ELIF) {
  1406. if (tab_level.back()->get() > indent_level) {
  1407. _set_error("Invalid indent");
  1408. return;
  1409. }
  1410. tokenizer->advance();
  1411. cf_if->body_else = alloc_node<BlockNode>();
  1412. cf_if->body_else->parent_block = p_block;
  1413. p_block->sub_blocks.push_back(cf_if->body_else);
  1414. ControlFlowNode *cf_else = alloc_node<ControlFlowNode>();
  1415. cf_else->cf_type = ControlFlowNode::CF_IF;
  1416. //condition
  1417. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  1418. if (!condition) {
  1419. if (_recover_from_completion()) {
  1420. break;
  1421. }
  1422. return;
  1423. }
  1424. cf_else->arguments.push_back(condition);
  1425. cf_else->cf_type = ControlFlowNode::CF_IF;
  1426. cf_if->body_else->statements.push_back(cf_else);
  1427. cf_if = cf_else;
  1428. cf_if->body = alloc_node<BlockNode>();
  1429. cf_if->body->parent_block = p_block;
  1430. p_block->sub_blocks.push_back(cf_if->body);
  1431. if (!_enter_indent_block(cf_if->body)) {
  1432. _set_error("Expected indented block after 'elif'");
  1433. p_block->end_line = tokenizer->get_token_line();
  1434. return;
  1435. }
  1436. current_block = cf_else->body;
  1437. _parse_block(cf_else->body, p_static);
  1438. current_block = p_block;
  1439. if (error_set)
  1440. return;
  1441. } else if (tokenizer->get_token() == GDTokenizer::TK_CF_ELSE) {
  1442. if (tab_level.back()->get() > indent_level) {
  1443. _set_error("Invalid indent");
  1444. return;
  1445. }
  1446. tokenizer->advance();
  1447. cf_if->body_else = alloc_node<BlockNode>();
  1448. cf_if->body_else->parent_block = p_block;
  1449. p_block->sub_blocks.push_back(cf_if->body_else);
  1450. if (!_enter_indent_block(cf_if->body_else)) {
  1451. _set_error("Expected indented block after 'else'");
  1452. p_block->end_line = tokenizer->get_token_line();
  1453. return;
  1454. }
  1455. current_block = cf_if->body_else;
  1456. _parse_block(cf_if->body_else, p_static);
  1457. current_block = p_block;
  1458. if (error_set)
  1459. return;
  1460. break; //after else, exit
  1461. } else
  1462. break;
  1463. }
  1464. } break;
  1465. case GDTokenizer::TK_CF_WHILE: {
  1466. tokenizer->advance();
  1467. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  1468. if (!condition) {
  1469. if (_recover_from_completion()) {
  1470. break;
  1471. }
  1472. return;
  1473. }
  1474. ControlFlowNode *cf_while = alloc_node<ControlFlowNode>();
  1475. cf_while->cf_type = ControlFlowNode::CF_WHILE;
  1476. cf_while->arguments.push_back(condition);
  1477. cf_while->body = alloc_node<BlockNode>();
  1478. cf_while->body->parent_block = p_block;
  1479. p_block->sub_blocks.push_back(cf_while->body);
  1480. if (!_enter_indent_block(cf_while->body)) {
  1481. _set_error("Expected indented block after 'while'");
  1482. p_block->end_line = tokenizer->get_token_line();
  1483. return;
  1484. }
  1485. current_block = cf_while->body;
  1486. _parse_block(cf_while->body, p_static);
  1487. current_block = p_block;
  1488. if (error_set)
  1489. return;
  1490. p_block->statements.push_back(cf_while);
  1491. } break;
  1492. case GDTokenizer::TK_CF_FOR: {
  1493. tokenizer->advance();
  1494. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  1495. _set_error("identifier expected after 'for'");
  1496. }
  1497. IdentifierNode *id = alloc_node<IdentifierNode>();
  1498. id->name = tokenizer->get_token_identifier();
  1499. tokenizer->advance();
  1500. if (tokenizer->get_token() != GDTokenizer::TK_OP_IN) {
  1501. _set_error("'in' expected after identifier");
  1502. return;
  1503. }
  1504. tokenizer->advance();
  1505. Node *container = _parse_and_reduce_expression(p_block, p_static);
  1506. if (!container) {
  1507. if (_recover_from_completion()) {
  1508. break;
  1509. }
  1510. return;
  1511. }
  1512. ControlFlowNode *cf_for = alloc_node<ControlFlowNode>();
  1513. cf_for->cf_type = ControlFlowNode::CF_FOR;
  1514. cf_for->arguments.push_back(id);
  1515. cf_for->arguments.push_back(container);
  1516. cf_for->body = alloc_node<BlockNode>();
  1517. cf_for->body->parent_block = p_block;
  1518. p_block->sub_blocks.push_back(cf_for->body);
  1519. if (!_enter_indent_block(cf_for->body)) {
  1520. _set_error("Expected indented block after 'for'");
  1521. p_block->end_line = tokenizer->get_token_line();
  1522. return;
  1523. }
  1524. current_block = cf_for->body;
  1525. _parse_block(cf_for->body, p_static);
  1526. current_block = p_block;
  1527. if (error_set)
  1528. return;
  1529. p_block->statements.push_back(cf_for);
  1530. } break;
  1531. case GDTokenizer::TK_CF_CONTINUE: {
  1532. tokenizer->advance();
  1533. ControlFlowNode *cf_continue = alloc_node<ControlFlowNode>();
  1534. cf_continue->cf_type = ControlFlowNode::CF_CONTINUE;
  1535. p_block->statements.push_back(cf_continue);
  1536. if (!_end_statement()) {
  1537. _set_error("Expected end of statement (continue)");
  1538. return;
  1539. }
  1540. } break;
  1541. case GDTokenizer::TK_CF_BREAK: {
  1542. tokenizer->advance();
  1543. ControlFlowNode *cf_break = alloc_node<ControlFlowNode>();
  1544. cf_break->cf_type = ControlFlowNode::CF_BREAK;
  1545. p_block->statements.push_back(cf_break);
  1546. if (!_end_statement()) {
  1547. _set_error("Expected end of statement (break)");
  1548. return;
  1549. }
  1550. } break;
  1551. case GDTokenizer::TK_CF_RETURN: {
  1552. tokenizer->advance();
  1553. ControlFlowNode *cf_return = alloc_node<ControlFlowNode>();
  1554. cf_return->cf_type = ControlFlowNode::CF_RETURN;
  1555. if (tokenizer->get_token() == GDTokenizer::TK_SEMICOLON || tokenizer->get_token() == GDTokenizer::TK_NEWLINE || tokenizer->get_token() == GDTokenizer::TK_EOF) {
  1556. //expect end of statement
  1557. p_block->statements.push_back(cf_return);
  1558. if (!_end_statement()) {
  1559. return;
  1560. }
  1561. } else {
  1562. //expect expression
  1563. Node *retexpr = _parse_and_reduce_expression(p_block, p_static);
  1564. if (!retexpr) {
  1565. if (_recover_from_completion()) {
  1566. break;
  1567. }
  1568. return;
  1569. }
  1570. cf_return->arguments.push_back(retexpr);
  1571. p_block->statements.push_back(cf_return);
  1572. if (!_end_statement()) {
  1573. _set_error("Expected end of statement after return expression.");
  1574. return;
  1575. }
  1576. }
  1577. } break;
  1578. case GDTokenizer::TK_PR_ASSERT: {
  1579. tokenizer->advance();
  1580. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  1581. if (!condition) {
  1582. if (_recover_from_completion()) {
  1583. break;
  1584. }
  1585. return;
  1586. }
  1587. AssertNode *an = alloc_node<AssertNode>();
  1588. an->condition = condition;
  1589. p_block->statements.push_back(an);
  1590. if (!_end_statement()) {
  1591. _set_error("Expected end of statement after assert.");
  1592. return;
  1593. }
  1594. } break;
  1595. case GDTokenizer::TK_PR_BREAKPOINT: {
  1596. tokenizer->advance();
  1597. BreakpointNode *bn = alloc_node<BreakpointNode>();
  1598. p_block->statements.push_back(bn);
  1599. if (!_end_statement()) {
  1600. _set_error("Expected end of statement after breakpoint.");
  1601. return;
  1602. }
  1603. } break;
  1604. default: {
  1605. Node *expression = _parse_and_reduce_expression(p_block, p_static, false, true);
  1606. if (!expression) {
  1607. if (_recover_from_completion()) {
  1608. break;
  1609. }
  1610. return;
  1611. }
  1612. p_block->statements.push_back(expression);
  1613. if (!_end_statement()) {
  1614. _set_error("Expected end of statement after expression.");
  1615. return;
  1616. }
  1617. } break;
  1618. /*
  1619. case GDTokenizer::TK_CF_LOCAL: {
  1620. if (tokenizer->get_token(1)!=GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE ) {
  1621. _set_error("Expected ';' or <NewLine>.");
  1622. }
  1623. tokenizer->advance();
  1624. } break;
  1625. */
  1626. }
  1627. }
  1628. }
  1629. bool GDParser::_parse_newline() {
  1630. if (tokenizer->get_token(1) != GDTokenizer::TK_EOF && tokenizer->get_token(1) != GDTokenizer::TK_NEWLINE) {
  1631. int indent = tokenizer->get_token_line_indent();
  1632. int current_indent = tab_level.back()->get();
  1633. if (indent > current_indent) {
  1634. _set_error("Unexpected indent.");
  1635. return false;
  1636. }
  1637. if (indent < current_indent) {
  1638. while (indent < current_indent) {
  1639. //exit block
  1640. if (tab_level.size() == 1) {
  1641. _set_error("Invalid indent. BUG?");
  1642. return false;
  1643. }
  1644. tab_level.pop_back();
  1645. if (tab_level.back()->get() < indent) {
  1646. _set_error("Unindent does not match any outer indentation level.");
  1647. return false;
  1648. }
  1649. current_indent = tab_level.back()->get();
  1650. }
  1651. tokenizer->advance();
  1652. return false;
  1653. }
  1654. }
  1655. tokenizer->advance();
  1656. return true;
  1657. }
  1658. void GDParser::_parse_extends(ClassNode *p_class) {
  1659. if (p_class->extends_used) {
  1660. _set_error("'extends' already used for this class.");
  1661. return;
  1662. }
  1663. if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty()) {
  1664. _set_error("'extends' must be used before anything else.");
  1665. return;
  1666. }
  1667. p_class->extends_used = true;
  1668. tokenizer->advance();
  1669. if (tokenizer->get_token() == GDTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token_type() == Variant::OBJECT) {
  1670. p_class->extends_class.push_back(Variant::get_type_name(Variant::OBJECT));
  1671. tokenizer->advance();
  1672. return;
  1673. }
  1674. // see if inheritance happens from a file
  1675. if (tokenizer->get_token() == GDTokenizer::TK_CONSTANT) {
  1676. Variant constant = tokenizer->get_token_constant();
  1677. if (constant.get_type() != Variant::STRING) {
  1678. _set_error("'extends' constant must be a string.");
  1679. return;
  1680. }
  1681. p_class->extends_file = constant;
  1682. tokenizer->advance();
  1683. if (tokenizer->get_token() != GDTokenizer::TK_PERIOD) {
  1684. return;
  1685. } else
  1686. tokenizer->advance();
  1687. }
  1688. while (true) {
  1689. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  1690. _set_error("Invalid 'extends' syntax, expected string constant (path) and/or identifier (parent class).");
  1691. return;
  1692. }
  1693. StringName identifier = tokenizer->get_token_identifier();
  1694. p_class->extends_class.push_back(identifier);
  1695. tokenizer->advance(1);
  1696. if (tokenizer->get_token() != GDTokenizer::TK_PERIOD)
  1697. return;
  1698. }
  1699. }
  1700. void GDParser::_parse_class(ClassNode *p_class) {
  1701. int indent_level = tab_level.back()->get();
  1702. while (true) {
  1703. GDTokenizer::Token token = tokenizer->get_token();
  1704. if (error_set)
  1705. return;
  1706. if (indent_level > tab_level.back()->get()) {
  1707. p_class->end_line = tokenizer->get_token_line();
  1708. return; //go back a level
  1709. }
  1710. switch (token) {
  1711. case GDTokenizer::TK_EOF:
  1712. p_class->end_line = tokenizer->get_token_line();
  1713. case GDTokenizer::TK_ERROR: {
  1714. return; //go back
  1715. //end of file!
  1716. } break;
  1717. case GDTokenizer::TK_NEWLINE: {
  1718. if (!_parse_newline()) {
  1719. if (!error_set) {
  1720. p_class->end_line = tokenizer->get_token_line();
  1721. }
  1722. return;
  1723. }
  1724. } break;
  1725. case GDTokenizer::TK_PR_EXTENDS: {
  1726. _parse_extends(p_class);
  1727. if (error_set)
  1728. return;
  1729. if (!_end_statement()) {
  1730. _set_error("Expected end of statement after extends");
  1731. return;
  1732. }
  1733. } break;
  1734. case GDTokenizer::TK_PR_TOOL: {
  1735. if (p_class->tool) {
  1736. _set_error("tool used more than once");
  1737. return;
  1738. }
  1739. p_class->tool = true;
  1740. tokenizer->advance();
  1741. } break;
  1742. case GDTokenizer::TK_PR_CLASS: {
  1743. //class inside class :D
  1744. StringName name;
  1745. StringName extends;
  1746. if (tokenizer->get_token(1) != GDTokenizer::TK_IDENTIFIER) {
  1747. _set_error("'class' syntax: 'class <Name>:' or 'class <Name> extends <BaseClass>:'");
  1748. return;
  1749. }
  1750. name = tokenizer->get_token_identifier(1);
  1751. tokenizer->advance(2);
  1752. ClassNode *newclass = alloc_node<ClassNode>();
  1753. newclass->initializer = alloc_node<BlockNode>();
  1754. newclass->initializer->parent_class = newclass;
  1755. newclass->ready = alloc_node<BlockNode>();
  1756. newclass->ready->parent_class = newclass;
  1757. newclass->name = name;
  1758. newclass->owner = p_class;
  1759. p_class->subclasses.push_back(newclass);
  1760. if (tokenizer->get_token() == GDTokenizer::TK_PR_EXTENDS) {
  1761. _parse_extends(newclass);
  1762. if (error_set)
  1763. return;
  1764. }
  1765. if (!_enter_indent_block()) {
  1766. _set_error("Indented block expected.");
  1767. return;
  1768. }
  1769. current_class = newclass;
  1770. _parse_class(newclass);
  1771. current_class = p_class;
  1772. } break;
  1773. /* this is for functions....
  1774. case GDTokenizer::TK_CF_PASS: {
  1775. tokenizer->advance(1);
  1776. } break;
  1777. */
  1778. case GDTokenizer::TK_PR_STATIC: {
  1779. tokenizer->advance();
  1780. if (tokenizer->get_token() != GDTokenizer::TK_PR_FUNCTION) {
  1781. _set_error("Expected 'func'.");
  1782. return;
  1783. }
  1784. }; //fallthrough to function
  1785. case GDTokenizer::TK_PR_FUNCTION: {
  1786. bool _static = false;
  1787. pending_newline = -1;
  1788. if (tokenizer->get_token(-1) == GDTokenizer::TK_PR_STATIC) {
  1789. _static = true;
  1790. }
  1791. tokenizer->advance();
  1792. StringName name;
  1793. if (_get_completable_identifier(COMPLETION_VIRTUAL_FUNC, name)) {
  1794. }
  1795. if (name == StringName()) {
  1796. _set_error("Expected identifier after 'func' (syntax: 'func <identifier>([arguments]):' ).");
  1797. return;
  1798. }
  1799. for (int i = 0; i < p_class->functions.size(); i++) {
  1800. if (p_class->functions[i]->name == name) {
  1801. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->functions[i]->line) + ").");
  1802. }
  1803. }
  1804. for (int i = 0; i < p_class->static_functions.size(); i++) {
  1805. if (p_class->static_functions[i]->name == name) {
  1806. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->static_functions[i]->line) + ").");
  1807. }
  1808. }
  1809. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_OPEN) {
  1810. _set_error("Expected '(' after identifier (syntax: 'func <identifier>([arguments]):' ).");
  1811. return;
  1812. }
  1813. tokenizer->advance();
  1814. Vector<StringName> arguments;
  1815. Vector<Node *> default_values;
  1816. int fnline = tokenizer->get_token_line();
  1817. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1818. //has arguments
  1819. bool defaulting = false;
  1820. while (true) {
  1821. if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  1822. tokenizer->advance();
  1823. continue;
  1824. }
  1825. if (tokenizer->get_token() == GDTokenizer::TK_PR_VAR) {
  1826. tokenizer->advance(); //var before the identifier is allowed
  1827. }
  1828. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  1829. _set_error("Expected identifier for argument.");
  1830. return;
  1831. }
  1832. StringName argname = tokenizer->get_token_identifier();
  1833. arguments.push_back(argname);
  1834. tokenizer->advance();
  1835. if (defaulting && tokenizer->get_token() != GDTokenizer::TK_OP_ASSIGN) {
  1836. _set_error("Default parameter expected.");
  1837. return;
  1838. }
  1839. //tokenizer->advance();
  1840. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  1841. defaulting = true;
  1842. tokenizer->advance(1);
  1843. Node *defval = NULL;
  1844. defval = _parse_and_reduce_expression(p_class, _static);
  1845. if (!defval || error_set)
  1846. return;
  1847. OperatorNode *on = alloc_node<OperatorNode>();
  1848. on->op = OperatorNode::OP_ASSIGN;
  1849. IdentifierNode *in = alloc_node<IdentifierNode>();
  1850. in->name = argname;
  1851. on->arguments.push_back(in);
  1852. on->arguments.push_back(defval);
  1853. /* no ..
  1854. if (defval->type!=Node::TYPE_CONSTANT) {
  1855. _set_error("default argument must be constant");
  1856. }
  1857. */
  1858. default_values.push_back(on);
  1859. }
  1860. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  1861. tokenizer->advance();
  1862. }
  1863. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  1864. tokenizer->advance();
  1865. continue;
  1866. } else if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1867. _set_error("Expected ',' or ')'.");
  1868. return;
  1869. }
  1870. break;
  1871. }
  1872. }
  1873. tokenizer->advance();
  1874. BlockNode *block = alloc_node<BlockNode>();
  1875. block->parent_class = p_class;
  1876. if (name == "_init") {
  1877. if (p_class->extends_used) {
  1878. OperatorNode *cparent = alloc_node<OperatorNode>();
  1879. cparent->op = OperatorNode::OP_PARENT_CALL;
  1880. block->statements.push_back(cparent);
  1881. IdentifierNode *id = alloc_node<IdentifierNode>();
  1882. id->name = "_init";
  1883. cparent->arguments.push_back(id);
  1884. if (tokenizer->get_token() == GDTokenizer::TK_PERIOD) {
  1885. tokenizer->advance();
  1886. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_OPEN) {
  1887. _set_error("expected '(' for parent constructor arguments.");
  1888. }
  1889. tokenizer->advance();
  1890. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1891. //has arguments
  1892. parenthesis++;
  1893. while (true) {
  1894. Node *arg = _parse_and_reduce_expression(p_class, _static);
  1895. cparent->arguments.push_back(arg);
  1896. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  1897. tokenizer->advance();
  1898. continue;
  1899. } else if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1900. _set_error("Expected ',' or ')'.");
  1901. return;
  1902. }
  1903. break;
  1904. }
  1905. parenthesis--;
  1906. }
  1907. tokenizer->advance();
  1908. }
  1909. } else {
  1910. if (tokenizer->get_token() == GDTokenizer::TK_PERIOD) {
  1911. _set_error("Parent constructor call found for a class without inheritance.");
  1912. return;
  1913. }
  1914. }
  1915. }
  1916. if (!_enter_indent_block(block)) {
  1917. _set_error("Indented block expected.");
  1918. return;
  1919. }
  1920. FunctionNode *function = alloc_node<FunctionNode>();
  1921. function->name = name;
  1922. function->arguments = arguments;
  1923. function->default_values = default_values;
  1924. function->_static = _static;
  1925. function->line = fnline;
  1926. if (_static)
  1927. p_class->static_functions.push_back(function);
  1928. else
  1929. p_class->functions.push_back(function);
  1930. current_function = function;
  1931. function->body = block;
  1932. current_block = block;
  1933. _parse_block(block, _static);
  1934. current_block = NULL;
  1935. //arguments
  1936. } break;
  1937. case GDTokenizer::TK_PR_SIGNAL: {
  1938. tokenizer->advance();
  1939. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  1940. _set_error("Expected identifier after 'signal'.");
  1941. return;
  1942. }
  1943. ClassNode::Signal sig;
  1944. sig.name = tokenizer->get_token_identifier();
  1945. tokenizer->advance();
  1946. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_OPEN) {
  1947. tokenizer->advance();
  1948. while (true) {
  1949. if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  1950. tokenizer->advance();
  1951. continue;
  1952. }
  1953. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1954. tokenizer->advance();
  1955. break;
  1956. }
  1957. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  1958. _set_error("Expected identifier in signal argument.");
  1959. return;
  1960. }
  1961. sig.arguments.push_back(tokenizer->get_token_identifier());
  1962. tokenizer->advance();
  1963. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  1964. tokenizer->advance();
  1965. }
  1966. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  1967. tokenizer->advance();
  1968. } else if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1969. _set_error("Expected ',' or ')' after signal parameter identifier.");
  1970. return;
  1971. }
  1972. }
  1973. }
  1974. p_class->_signals.push_back(sig);
  1975. if (!_end_statement()) {
  1976. _set_error("Expected end of statement (signal)");
  1977. return;
  1978. }
  1979. } break;
  1980. case GDTokenizer::TK_PR_EXPORT: {
  1981. tokenizer->advance();
  1982. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_OPEN) {
  1983. tokenizer->advance();
  1984. if (tokenizer->get_token() == GDTokenizer::TK_BUILT_IN_TYPE) {
  1985. Variant::Type type = tokenizer->get_token_type();
  1986. if (type == Variant::NIL) {
  1987. _set_error("Can't export null type.");
  1988. return;
  1989. }
  1990. current_export.type = type;
  1991. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  1992. tokenizer->advance();
  1993. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  1994. // hint expected next!
  1995. tokenizer->advance();
  1996. switch (current_export.type) {
  1997. case Variant::INT: {
  1998. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  1999. current_export.hint = PROPERTY_HINT_ALL_FLAGS;
  2000. tokenizer->advance();
  2001. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2002. break;
  2003. }
  2004. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2005. _set_error("Expected ')' or ',' in bit flags hint.");
  2006. return;
  2007. }
  2008. current_export.hint = PROPERTY_HINT_FLAGS;
  2009. tokenizer->advance();
  2010. bool first = true;
  2011. while (true) {
  2012. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2013. current_export = PropertyInfo();
  2014. _set_error("Expected a string constant in named bit flags hint.");
  2015. return;
  2016. }
  2017. String c = tokenizer->get_token_constant();
  2018. if (!first)
  2019. current_export.hint_string += ",";
  2020. else
  2021. first = false;
  2022. current_export.hint_string += c.xml_escape();
  2023. tokenizer->advance();
  2024. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2025. break;
  2026. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2027. current_export = PropertyInfo();
  2028. _set_error("Expected ')' or ',' in named bit flags hint.");
  2029. return;
  2030. }
  2031. tokenizer->advance();
  2032. }
  2033. break;
  2034. }
  2035. if (tokenizer->get_token() == GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  2036. //enumeration
  2037. current_export.hint = PROPERTY_HINT_ENUM;
  2038. bool first = true;
  2039. while (true) {
  2040. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2041. current_export = PropertyInfo();
  2042. _set_error("Expected a string constant in enumeration hint.");
  2043. return;
  2044. }
  2045. String c = tokenizer->get_token_constant();
  2046. if (!first)
  2047. current_export.hint_string += ",";
  2048. else
  2049. first = false;
  2050. current_export.hint_string += c.xml_escape();
  2051. tokenizer->advance();
  2052. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2053. break;
  2054. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2055. current_export = PropertyInfo();
  2056. _set_error("Expected ')' or ',' in enumeration hint.");
  2057. return;
  2058. }
  2059. tokenizer->advance();
  2060. }
  2061. break;
  2062. }
  2063. }; //fallthrough to use the same
  2064. case Variant::REAL: {
  2065. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EASE") {
  2066. current_export.hint = PROPERTY_HINT_EXP_EASING;
  2067. tokenizer->advance();
  2068. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2069. _set_error("Expected ')' in hint.");
  2070. return;
  2071. }
  2072. break;
  2073. }
  2074. // range
  2075. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EXP") {
  2076. current_export.hint = PROPERTY_HINT_EXP_RANGE;
  2077. tokenizer->advance();
  2078. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2079. break;
  2080. else if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2081. _set_error("Expected ')' or ',' in exponential range hint.");
  2082. return;
  2083. }
  2084. tokenizer->advance();
  2085. } else
  2086. current_export.hint = PROPERTY_HINT_RANGE;
  2087. float sign = 1.0;
  2088. if (tokenizer->get_token() == GDTokenizer::TK_OP_SUB) {
  2089. sign = -1;
  2090. tokenizer->advance();
  2091. }
  2092. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2093. current_export = PropertyInfo();
  2094. _set_error("Expected a range in numeric hint.");
  2095. return;
  2096. }
  2097. current_export.hint_string = rtos(sign * double(tokenizer->get_token_constant()));
  2098. tokenizer->advance();
  2099. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2100. current_export.hint_string = "0," + current_export.hint_string;
  2101. break;
  2102. }
  2103. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2104. current_export = PropertyInfo();
  2105. _set_error("Expected ',' or ')' in numeric range hint.");
  2106. return;
  2107. }
  2108. tokenizer->advance();
  2109. sign = 1.0;
  2110. if (tokenizer->get_token() == GDTokenizer::TK_OP_SUB) {
  2111. sign = -1;
  2112. tokenizer->advance();
  2113. }
  2114. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2115. current_export = PropertyInfo();
  2116. _set_error("Expected a number as upper bound in numeric range hint.");
  2117. return;
  2118. }
  2119. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  2120. tokenizer->advance();
  2121. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2122. break;
  2123. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2124. current_export = PropertyInfo();
  2125. _set_error("Expected ',' or ')' in numeric range hint.");
  2126. return;
  2127. }
  2128. tokenizer->advance();
  2129. sign = 1.0;
  2130. if (tokenizer->get_token() == GDTokenizer::TK_OP_SUB) {
  2131. sign = -1;
  2132. tokenizer->advance();
  2133. }
  2134. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2135. current_export = PropertyInfo();
  2136. _set_error("Expected a number as step in numeric range hint.");
  2137. return;
  2138. }
  2139. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  2140. tokenizer->advance();
  2141. } break;
  2142. case Variant::STRING: {
  2143. if (tokenizer->get_token() == GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  2144. //enumeration
  2145. current_export.hint = PROPERTY_HINT_ENUM;
  2146. bool first = true;
  2147. while (true) {
  2148. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2149. current_export = PropertyInfo();
  2150. _set_error("Expected a string constant in enumeration hint.");
  2151. return;
  2152. }
  2153. String c = tokenizer->get_token_constant();
  2154. if (!first)
  2155. current_export.hint_string += ",";
  2156. else
  2157. first = false;
  2158. current_export.hint_string += c.xml_escape();
  2159. tokenizer->advance();
  2160. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2161. break;
  2162. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2163. current_export = PropertyInfo();
  2164. _set_error("Expected ')' or ',' in enumeration hint.");
  2165. return;
  2166. }
  2167. tokenizer->advance();
  2168. }
  2169. break;
  2170. }
  2171. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "DIR") {
  2172. tokenizer->advance();
  2173. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2174. current_export.hint = PROPERTY_HINT_DIR;
  2175. else if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2176. tokenizer->advance();
  2177. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier() == "GLOBAL")) {
  2178. _set_error("Expected 'GLOBAL' after comma in directory hint.");
  2179. return;
  2180. }
  2181. if (!p_class->tool) {
  2182. _set_error("Global filesystem hints may only be used in tool scripts.");
  2183. return;
  2184. }
  2185. current_export.hint = PROPERTY_HINT_GLOBAL_DIR;
  2186. tokenizer->advance();
  2187. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2188. _set_error("Expected ')' in hint.");
  2189. return;
  2190. }
  2191. } else {
  2192. _set_error("Expected ')' or ',' in hint.");
  2193. return;
  2194. }
  2195. break;
  2196. }
  2197. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FILE") {
  2198. current_export.hint = PROPERTY_HINT_FILE;
  2199. tokenizer->advance();
  2200. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2201. tokenizer->advance();
  2202. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "GLOBAL") {
  2203. if (!p_class->tool) {
  2204. _set_error("Global filesystem hints may only be used in tool scripts.");
  2205. return;
  2206. }
  2207. current_export.hint = PROPERTY_HINT_GLOBAL_FILE;
  2208. tokenizer->advance();
  2209. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2210. break;
  2211. else if (tokenizer->get_token() == GDTokenizer::TK_COMMA)
  2212. tokenizer->advance();
  2213. else {
  2214. _set_error("Expected ')' or ',' in hint.");
  2215. return;
  2216. }
  2217. }
  2218. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2219. if (current_export.hint == PROPERTY_HINT_GLOBAL_FILE)
  2220. _set_error("Expected string constant with filter");
  2221. else
  2222. _set_error("Expected 'GLOBAL' or string constant with filter");
  2223. return;
  2224. }
  2225. current_export.hint_string = tokenizer->get_token_constant();
  2226. tokenizer->advance();
  2227. }
  2228. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2229. _set_error("Expected ')' in hint.");
  2230. return;
  2231. }
  2232. break;
  2233. }
  2234. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "MULTILINE") {
  2235. current_export.hint = PROPERTY_HINT_MULTILINE_TEXT;
  2236. tokenizer->advance();
  2237. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2238. _set_error("Expected ')' in hint.");
  2239. return;
  2240. }
  2241. break;
  2242. }
  2243. } break;
  2244. case Variant::COLOR: {
  2245. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2246. current_export = PropertyInfo();
  2247. _set_error("Color type hint expects RGB or RGBA as hints");
  2248. return;
  2249. }
  2250. String identifier = tokenizer->get_token_identifier();
  2251. if (identifier == "RGB") {
  2252. current_export.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  2253. } else if (identifier == "RGBA") {
  2254. //none
  2255. } else {
  2256. current_export = PropertyInfo();
  2257. _set_error("Color type hint expects RGB or RGBA as hints");
  2258. return;
  2259. }
  2260. tokenizer->advance();
  2261. } break;
  2262. default: {
  2263. current_export = PropertyInfo();
  2264. _set_error("Type '" + Variant::get_type_name(type) + "' can't take hints.");
  2265. return;
  2266. } break;
  2267. }
  2268. }
  2269. } else if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER) {
  2270. String identifier = tokenizer->get_token_identifier();
  2271. if (!ObjectTypeDB::is_type(identifier, "Resource")) {
  2272. current_export = PropertyInfo();
  2273. _set_error("Export hint not a type or resource.");
  2274. }
  2275. current_export.type = Variant::OBJECT;
  2276. current_export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  2277. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  2278. current_export.hint_string = identifier;
  2279. tokenizer->advance();
  2280. }
  2281. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2282. current_export = PropertyInfo();
  2283. _set_error("Expected ')' or ',' after export hint.");
  2284. return;
  2285. }
  2286. tokenizer->advance();
  2287. }
  2288. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR) {
  2289. current_export = PropertyInfo();
  2290. _set_error("Expected 'var'.");
  2291. return;
  2292. }
  2293. }; //fallthrough to var
  2294. case GDTokenizer::TK_PR_ONREADY: {
  2295. if (token == GDTokenizer::TK_PR_ONREADY) {
  2296. //may be fallthrough from export, ignore if so
  2297. tokenizer->advance();
  2298. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR) {
  2299. _set_error("Expected 'var'.");
  2300. return;
  2301. }
  2302. }
  2303. }; //fallthrough to var
  2304. case GDTokenizer::TK_PR_VAR: {
  2305. //variale declaration and (eventual) initialization
  2306. ClassNode::Member member;
  2307. bool autoexport = tokenizer->get_token(-1) == GDTokenizer::TK_PR_EXPORT;
  2308. if (current_export.type != Variant::NIL) {
  2309. member._export = current_export;
  2310. current_export = PropertyInfo();
  2311. }
  2312. bool onready = tokenizer->get_token(-1) == GDTokenizer::TK_PR_ONREADY;
  2313. tokenizer->advance();
  2314. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2315. _set_error("Expected identifier for member variable name.");
  2316. return;
  2317. }
  2318. member.identifier = tokenizer->get_token_identifier();
  2319. member.expression = NULL;
  2320. member._export.name = member.identifier;
  2321. member.line = tokenizer->get_token_line();
  2322. tokenizer->advance();
  2323. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  2324. #ifdef DEBUG_ENABLED
  2325. int line = tokenizer->get_token_line();
  2326. #endif
  2327. tokenizer->advance();
  2328. Node *subexpr = NULL;
  2329. subexpr = _parse_and_reduce_expression(p_class, false, autoexport);
  2330. if (!subexpr) {
  2331. if (_recover_from_completion()) {
  2332. break;
  2333. }
  2334. return;
  2335. }
  2336. //discourage common error
  2337. if (!onready && subexpr->type == Node::TYPE_OPERATOR) {
  2338. OperatorNode *op = static_cast<OperatorNode *>(subexpr);
  2339. if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_SELF && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  2340. IdentifierNode *id = static_cast<IdentifierNode *>(op->arguments[1]);
  2341. if (id->name == "get_node") {
  2342. _set_error("Use 'onready var " + String(member.identifier) + " = get_node(..)' instead");
  2343. return;
  2344. }
  2345. }
  2346. }
  2347. member.expression = subexpr;
  2348. if (autoexport) {
  2349. if (1) /*(subexpr->type==Node::TYPE_ARRAY) {
  2350. member._export.type=Variant::ARRAY;
  2351. } else if (subexpr->type==Node::TYPE_DICTIONARY) {
  2352. member._export.type=Variant::DICTIONARY;
  2353. } else*/ {
  2354. if (subexpr->type != Node::TYPE_CONSTANT) {
  2355. _set_error("Type-less export needs a constant expression assigned to infer type.");
  2356. return;
  2357. }
  2358. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  2359. if (cn->value.get_type() == Variant::NIL) {
  2360. _set_error("Can't accept a null constant expression for infering export type.");
  2361. return;
  2362. }
  2363. member._export.type = cn->value.get_type();
  2364. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  2365. if (cn->value.get_type() == Variant::OBJECT) {
  2366. Object *obj = cn->value;
  2367. Resource *res = obj->cast_to<Resource>();
  2368. if (res == NULL) {
  2369. _set_error("Exported constant not a type or resource.");
  2370. return;
  2371. }
  2372. member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  2373. member._export.hint_string = res->get_type();
  2374. }
  2375. }
  2376. }
  2377. #ifdef TOOLS_ENABLED
  2378. if (subexpr->type == Node::TYPE_CONSTANT && member._export.type != Variant::NIL) {
  2379. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  2380. if (cn->value.get_type() != Variant::NIL) {
  2381. member.default_value = cn->value;
  2382. }
  2383. }
  2384. #endif
  2385. IdentifierNode *id = alloc_node<IdentifierNode>();
  2386. id->name = member.identifier;
  2387. OperatorNode *op = alloc_node<OperatorNode>();
  2388. op->op = OperatorNode::OP_INIT_ASSIGN;
  2389. op->arguments.push_back(id);
  2390. op->arguments.push_back(subexpr);
  2391. #ifdef DEBUG_ENABLED
  2392. NewLineNode *nl = alloc_node<NewLineNode>();
  2393. nl->line = line;
  2394. if (onready)
  2395. p_class->ready->statements.push_back(nl);
  2396. else
  2397. p_class->initializer->statements.push_back(nl);
  2398. #endif
  2399. if (onready)
  2400. p_class->ready->statements.push_back(op);
  2401. else
  2402. p_class->initializer->statements.push_back(op);
  2403. } else {
  2404. if (autoexport) {
  2405. _set_error("Type-less export needs a constant expression assigned to infer type.");
  2406. return;
  2407. }
  2408. }
  2409. if (tokenizer->get_token() == GDTokenizer::TK_PR_SETGET) {
  2410. tokenizer->advance();
  2411. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2412. //just comma means using only getter
  2413. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2414. _set_error("Expected identifier for setter function after 'notify'.");
  2415. }
  2416. member.setter = tokenizer->get_token_identifier();
  2417. tokenizer->advance();
  2418. }
  2419. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2420. //there is a getter
  2421. tokenizer->advance();
  2422. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2423. _set_error("Expected identifier for getter function after ','.");
  2424. }
  2425. member.getter = tokenizer->get_token_identifier();
  2426. tokenizer->advance();
  2427. }
  2428. }
  2429. p_class->variables.push_back(member);
  2430. if (!_end_statement()) {
  2431. _set_error("Expected end of statement (continue)");
  2432. return;
  2433. }
  2434. } break;
  2435. case GDTokenizer::TK_PR_CONST: {
  2436. //variale declaration and (eventual) initialization
  2437. ClassNode::Constant constant;
  2438. tokenizer->advance();
  2439. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2440. _set_error("Expected name (identifier) for constant.");
  2441. return;
  2442. }
  2443. constant.identifier = tokenizer->get_token_identifier();
  2444. tokenizer->advance();
  2445. if (tokenizer->get_token() != GDTokenizer::TK_OP_ASSIGN) {
  2446. _set_error("Constant expects assignment.");
  2447. return;
  2448. }
  2449. tokenizer->advance();
  2450. Node *subexpr = NULL;
  2451. subexpr = _parse_and_reduce_expression(p_class, true, true);
  2452. if (!subexpr) {
  2453. if (_recover_from_completion()) {
  2454. break;
  2455. }
  2456. return;
  2457. }
  2458. if (subexpr->type != Node::TYPE_CONSTANT) {
  2459. _set_error("Expected constant expression");
  2460. }
  2461. constant.expression = subexpr;
  2462. p_class->constant_expressions.push_back(constant);
  2463. if (!_end_statement()) {
  2464. _set_error("Expected end of statement (constant)");
  2465. return;
  2466. }
  2467. } break;
  2468. case GDTokenizer::TK_PR_ENUM: {
  2469. //mutiple constant declarations..
  2470. int last_assign = -1; // Incremented by 1 right before the assingment.
  2471. String enum_name;
  2472. Dictionary enum_dict;
  2473. tokenizer->advance();
  2474. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER) {
  2475. enum_name = tokenizer->get_token_identifier();
  2476. tokenizer->advance();
  2477. }
  2478. if (tokenizer->get_token() != GDTokenizer::TK_CURLY_BRACKET_OPEN) {
  2479. _set_error("Expected '{' in enum declaration");
  2480. return;
  2481. }
  2482. tokenizer->advance();
  2483. while (true) {
  2484. if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  2485. tokenizer->advance(); // Ignore newlines
  2486. } else if (tokenizer->get_token() == GDTokenizer::TK_CURLY_BRACKET_CLOSE) {
  2487. tokenizer->advance();
  2488. break; // End of enum
  2489. } else if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2490. if (tokenizer->get_token() == GDTokenizer::TK_EOF) {
  2491. _set_error("Unexpected end of file.");
  2492. } else {
  2493. _set_error(String("Unexpected ") + GDTokenizer::get_token_name(tokenizer->get_token()) + ", expected identifier");
  2494. }
  2495. return;
  2496. } else { // tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER
  2497. ClassNode::Constant constant;
  2498. constant.identifier = tokenizer->get_token_identifier();
  2499. tokenizer->advance();
  2500. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  2501. tokenizer->advance();
  2502. Node *subexpr = NULL;
  2503. subexpr = _parse_and_reduce_expression(p_class, true, true);
  2504. if (!subexpr) {
  2505. if (_recover_from_completion()) {
  2506. break;
  2507. }
  2508. return;
  2509. }
  2510. if (subexpr->type != Node::TYPE_CONSTANT) {
  2511. _set_error("Expected constant expression");
  2512. }
  2513. const ConstantNode *subexpr_const = static_cast<const ConstantNode *>(subexpr);
  2514. if (subexpr_const->value.get_type() != Variant::INT) {
  2515. _set_error("Expected an int value for enum");
  2516. }
  2517. last_assign = subexpr_const->value;
  2518. constant.expression = subexpr;
  2519. } else {
  2520. last_assign = last_assign + 1;
  2521. ConstantNode *cn = alloc_node<ConstantNode>();
  2522. cn->value = last_assign;
  2523. constant.expression = cn;
  2524. }
  2525. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2526. tokenizer->advance();
  2527. }
  2528. if (enum_name != "") {
  2529. const ConstantNode *cn = static_cast<const ConstantNode *>(constant.expression);
  2530. enum_dict[constant.identifier] = cn->value;
  2531. }
  2532. p_class->constant_expressions.push_back(constant);
  2533. }
  2534. }
  2535. if (enum_name != "") {
  2536. ClassNode::Constant enum_constant;
  2537. enum_constant.identifier = enum_name;
  2538. ConstantNode *cn = alloc_node<ConstantNode>();
  2539. cn->value = enum_dict;
  2540. enum_constant.expression = cn;
  2541. p_class->constant_expressions.push_back(enum_constant);
  2542. }
  2543. if (!_end_statement()) {
  2544. _set_error("Expected end of statement (enum)");
  2545. return;
  2546. }
  2547. } break;
  2548. default: {
  2549. _set_error(String() + "Unexpected token: " + tokenizer->get_token_name(tokenizer->get_token()) + ":" + tokenizer->get_token_identifier());
  2550. return;
  2551. } break;
  2552. }
  2553. }
  2554. }
  2555. void GDParser::_set_error(const String &p_error, int p_line, int p_column) {
  2556. if (error_set)
  2557. return; //allow no further errors
  2558. error = p_error;
  2559. error_line = p_line < 0 ? tokenizer->get_token_line() : p_line;
  2560. error_column = p_column < 0 ? tokenizer->get_token_column() : p_column;
  2561. error_set = true;
  2562. }
  2563. String GDParser::get_error() const {
  2564. return error;
  2565. }
  2566. int GDParser::get_error_line() const {
  2567. return error_line;
  2568. }
  2569. int GDParser::get_error_column() const {
  2570. return error_column;
  2571. }
  2572. Error GDParser::_parse(const String &p_base_path) {
  2573. base_path = p_base_path;
  2574. clear();
  2575. //assume class
  2576. ClassNode *main_class = alloc_node<ClassNode>();
  2577. main_class->initializer = alloc_node<BlockNode>();
  2578. main_class->initializer->parent_class = main_class;
  2579. main_class->ready = alloc_node<BlockNode>();
  2580. main_class->ready->parent_class = main_class;
  2581. current_class = main_class;
  2582. _parse_class(main_class);
  2583. if (tokenizer->get_token() == GDTokenizer::TK_ERROR) {
  2584. error_set = false;
  2585. _set_error("Parse Error: " + tokenizer->get_token_error());
  2586. }
  2587. if (error_set) {
  2588. return ERR_PARSE_ERROR;
  2589. }
  2590. return OK;
  2591. }
  2592. Error GDParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path, const String &p_self_path) {
  2593. for_completion = false;
  2594. validating = false;
  2595. completion_type = COMPLETION_NONE;
  2596. completion_node = NULL;
  2597. completion_class = NULL;
  2598. completion_function = NULL;
  2599. completion_block = NULL;
  2600. completion_found = false;
  2601. current_block = NULL;
  2602. current_class = NULL;
  2603. current_function = NULL;
  2604. self_path = p_self_path;
  2605. GDTokenizerBuffer *tb = memnew(GDTokenizerBuffer);
  2606. tb->set_code_buffer(p_bytecode);
  2607. tokenizer = tb;
  2608. Error ret = _parse(p_base_path);
  2609. memdelete(tb);
  2610. tokenizer = NULL;
  2611. return ret;
  2612. }
  2613. 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) {
  2614. completion_type = COMPLETION_NONE;
  2615. completion_node = NULL;
  2616. completion_class = NULL;
  2617. completion_function = NULL;
  2618. completion_block = NULL;
  2619. completion_found = false;
  2620. current_block = NULL;
  2621. current_class = NULL;
  2622. current_function = NULL;
  2623. self_path = p_self_path;
  2624. GDTokenizerText *tt = memnew(GDTokenizerText);
  2625. tt->set_code(p_code);
  2626. validating = p_just_validate;
  2627. for_completion = p_for_completion;
  2628. tokenizer = tt;
  2629. Error ret = _parse(p_base_path);
  2630. memdelete(tt);
  2631. tokenizer = NULL;
  2632. return ret;
  2633. }
  2634. bool GDParser::is_tool_script() const {
  2635. return (head && head->type == Node::TYPE_CLASS && static_cast<const ClassNode *>(head)->tool);
  2636. }
  2637. const GDParser::Node *GDParser::get_parse_tree() const {
  2638. return head;
  2639. }
  2640. void GDParser::clear() {
  2641. while (list) {
  2642. Node *l = list;
  2643. list = list->next;
  2644. memdelete(l);
  2645. }
  2646. head = NULL;
  2647. list = NULL;
  2648. completion_type = COMPLETION_NONE;
  2649. completion_node = NULL;
  2650. completion_class = NULL;
  2651. completion_function = NULL;
  2652. completion_block = NULL;
  2653. current_block = NULL;
  2654. current_class = NULL;
  2655. completion_found = false;
  2656. current_function = NULL;
  2657. validating = false;
  2658. for_completion = false;
  2659. error_set = false;
  2660. tab_level.clear();
  2661. tab_level.push_back(0);
  2662. error_line = 0;
  2663. error_column = 0;
  2664. pending_newline = -1;
  2665. parenthesis = 0;
  2666. current_export.type = Variant::NIL;
  2667. error = "";
  2668. }
  2669. GDParser::CompletionType GDParser::get_completion_type() {
  2670. return completion_type;
  2671. }
  2672. StringName GDParser::get_completion_cursor() {
  2673. return completion_cursor;
  2674. }
  2675. int GDParser::get_completion_line() {
  2676. return completion_line;
  2677. }
  2678. Variant::Type GDParser::get_completion_built_in_constant() {
  2679. return completion_built_in_constant;
  2680. }
  2681. GDParser::Node *GDParser::get_completion_node() {
  2682. return completion_node;
  2683. }
  2684. GDParser::BlockNode *GDParser::get_completion_block() {
  2685. return completion_block;
  2686. }
  2687. GDParser::ClassNode *GDParser::get_completion_class() {
  2688. return completion_class;
  2689. }
  2690. GDParser::FunctionNode *GDParser::get_completion_function() {
  2691. return completion_function;
  2692. }
  2693. int GDParser::get_completion_argument_index() {
  2694. return completion_argument;
  2695. }
  2696. GDParser::GDParser() {
  2697. head = NULL;
  2698. list = NULL;
  2699. tokenizer = NULL;
  2700. pending_newline = -1;
  2701. clear();
  2702. }
  2703. GDParser::~GDParser() {
  2704. clear();
  2705. }