gd_parser.cpp 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495
  1. /*************************************************************************/
  2. /* gd_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://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. bool right_to_left = false;
  771. switch (expression[i].op) {
  772. case OperatorNode::OP_EXTENDS:
  773. priority = -1;
  774. break; //before anything
  775. case OperatorNode::OP_BIT_INVERT:
  776. priority = 0;
  777. unary = true;
  778. break;
  779. case OperatorNode::OP_NEG:
  780. priority = 1;
  781. unary = true;
  782. break;
  783. case OperatorNode::OP_MUL: priority = 2; break;
  784. case OperatorNode::OP_DIV: priority = 2; break;
  785. case OperatorNode::OP_MOD: priority = 2; break;
  786. case OperatorNode::OP_ADD: priority = 3; break;
  787. case OperatorNode::OP_SUB: priority = 3; break;
  788. case OperatorNode::OP_SHIFT_LEFT: priority = 4; break;
  789. case OperatorNode::OP_SHIFT_RIGHT: priority = 4; break;
  790. case OperatorNode::OP_BIT_AND: priority = 5; break;
  791. case OperatorNode::OP_BIT_XOR: priority = 6; break;
  792. case OperatorNode::OP_BIT_OR: priority = 7; break;
  793. case OperatorNode::OP_LESS: priority = 8; break;
  794. case OperatorNode::OP_LESS_EQUAL: priority = 8; break;
  795. case OperatorNode::OP_GREATER: priority = 8; break;
  796. case OperatorNode::OP_GREATER_EQUAL: priority = 8; break;
  797. case OperatorNode::OP_EQUAL: priority = 8; break;
  798. case OperatorNode::OP_NOT_EQUAL: priority = 8; break;
  799. case OperatorNode::OP_IN: priority = 10; break;
  800. case OperatorNode::OP_NOT:
  801. priority = 11;
  802. unary = true;
  803. break;
  804. case OperatorNode::OP_AND: priority = 12; break;
  805. case OperatorNode::OP_OR: priority = 13; break;
  806. case OperatorNode::OP_TERNARY_IF:
  807. priority = 14;
  808. ternary = true;
  809. right_to_left = true;
  810. break;
  811. case OperatorNode::OP_TERNARY_ELSE:
  812. priority = 14;
  813. error = true;
  814. // Rigth-to-left should be false in this case, otherwise it would always error.
  815. break;
  816. case OperatorNode::OP_ASSIGN: priority = 15; break;
  817. case OperatorNode::OP_ASSIGN_ADD: priority = 15; break;
  818. case OperatorNode::OP_ASSIGN_SUB: priority = 15; break;
  819. case OperatorNode::OP_ASSIGN_MUL: priority = 15; break;
  820. case OperatorNode::OP_ASSIGN_DIV: priority = 15; break;
  821. case OperatorNode::OP_ASSIGN_MOD: priority = 15; break;
  822. case OperatorNode::OP_ASSIGN_SHIFT_LEFT: priority = 15; break;
  823. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT: priority = 15; break;
  824. case OperatorNode::OP_ASSIGN_BIT_AND: priority = 15; break;
  825. case OperatorNode::OP_ASSIGN_BIT_OR: priority = 15; break;
  826. case OperatorNode::OP_ASSIGN_BIT_XOR: priority = 15; break;
  827. default: {
  828. _set_error("GDParser bug, invalid operator in expression: " + itos(expression[i].op));
  829. return NULL;
  830. }
  831. }
  832. if (priority < min_priority || (right_to_left && priority == min_priority)) {
  833. // < is used for left to right (default)
  834. // <= is used for right to left
  835. if (error) {
  836. _set_error("Unexpected operator");
  837. return NULL;
  838. }
  839. next_op = i;
  840. min_priority = priority;
  841. is_unary = unary;
  842. is_ternary = ternary;
  843. }
  844. }
  845. if (next_op == -1) {
  846. _set_error("Yet another parser bug....");
  847. ERR_FAIL_COND_V(next_op == -1, NULL);
  848. }
  849. // OK! create operator..
  850. if (is_unary) {
  851. int expr_pos = next_op;
  852. while (expression[expr_pos].is_op) {
  853. expr_pos++;
  854. if (expr_pos == expression.size()) {
  855. //can happen..
  856. _set_error("Unexpected end of expression..");
  857. return NULL;
  858. }
  859. }
  860. //consecutively do unary opeators
  861. for (int i = expr_pos - 1; i >= next_op; i--) {
  862. OperatorNode *op = alloc_node<OperatorNode>();
  863. op->op = expression[i].op;
  864. op->arguments.push_back(expression[i + 1].node);
  865. op->line = op_line; //line might have been changed from a \n
  866. expression[i].is_op = false;
  867. expression[i].node = op;
  868. expression.remove(i + 1);
  869. }
  870. } else if (is_ternary) {
  871. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  872. _set_error("Parser bug..");
  873. ERR_FAIL_V(NULL);
  874. }
  875. if (next_op >= (expression.size() - 2) || expression[next_op + 2].op != OperatorNode::OP_TERNARY_ELSE) {
  876. _set_error("Expected else after ternary if.");
  877. ERR_FAIL_V(NULL);
  878. }
  879. if (next_op >= (expression.size() - 3)) {
  880. _set_error("Expected value after ternary else.");
  881. ERR_FAIL_V(NULL);
  882. }
  883. OperatorNode *op = alloc_node<OperatorNode>();
  884. op->op = expression[next_op].op;
  885. op->line = op_line; //line might have been changed from a \n
  886. if (expression[next_op - 1].is_op) {
  887. _set_error("Parser bug..");
  888. ERR_FAIL_V(NULL);
  889. }
  890. if (expression[next_op + 1].is_op) {
  891. // this is not invalid and can really appear
  892. // but it becomes invalid anyway because no binary op
  893. // can be followed by an unary op in a valid combination,
  894. // due to how precedence works, unaries will always dissapear first
  895. _set_error("Unexpected two consecutive operators after ternary if.");
  896. return NULL;
  897. }
  898. if (expression[next_op + 3].is_op) {
  899. // this is not invalid and can really appear
  900. // but it becomes invalid anyway because no binary op
  901. // can be followed by an unary op in a valid combination,
  902. // due to how precedence works, unaries will always dissapear first
  903. _set_error("Unexpected two consecutive operators after ternary else.");
  904. return NULL;
  905. }
  906. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as first
  907. op->arguments.push_back(expression[next_op - 1].node); //left expression goes as when-true
  908. op->arguments.push_back(expression[next_op + 3].node); //expression after next goes as when-false
  909. //replace all 3 nodes by this operator and make it an expression
  910. expression[next_op - 1].node = op;
  911. expression.remove(next_op);
  912. expression.remove(next_op);
  913. expression.remove(next_op);
  914. expression.remove(next_op);
  915. } else {
  916. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  917. _set_error("Parser bug..");
  918. ERR_FAIL_V(NULL);
  919. }
  920. OperatorNode *op = alloc_node<OperatorNode>();
  921. op->op = expression[next_op].op;
  922. op->line = op_line; //line might have been changed from a \n
  923. if (expression[next_op - 1].is_op) {
  924. _set_error("Parser bug..");
  925. ERR_FAIL_V(NULL);
  926. }
  927. if (expression[next_op + 1].is_op) {
  928. // this is not invalid and can really appear
  929. // but it becomes invalid anyway because no binary op
  930. // can be followed by an unary op in a valid combination,
  931. // due to how precedence works, unaries will always dissapear first
  932. _set_error("Unexpected two consecutive operators.");
  933. return NULL;
  934. }
  935. op->arguments.push_back(expression[next_op - 1].node); //expression goes as left
  936. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as right
  937. //replace all 3 nodes by this operator and make it an expression
  938. expression[next_op - 1].node = op;
  939. expression.remove(next_op);
  940. expression.remove(next_op);
  941. }
  942. }
  943. return expression[0].node;
  944. }
  945. GDParser::Node *GDParser::_reduce_expression(Node *p_node, bool p_to_const) {
  946. switch (p_node->type) {
  947. case Node::TYPE_BUILT_IN_FUNCTION: {
  948. //many may probably be optimizable
  949. return p_node;
  950. } break;
  951. case Node::TYPE_ARRAY: {
  952. ArrayNode *an = static_cast<ArrayNode *>(p_node);
  953. bool all_constants = true;
  954. for (int i = 0; i < an->elements.size(); i++) {
  955. an->elements[i] = _reduce_expression(an->elements[i], p_to_const);
  956. if (an->elements[i]->type != Node::TYPE_CONSTANT)
  957. all_constants = false;
  958. }
  959. if (all_constants && p_to_const) {
  960. //reduce constant array expression
  961. ConstantNode *cn = alloc_node<ConstantNode>();
  962. Array arr(!p_to_const);
  963. //print_line("mk array "+itos(!p_to_const));
  964. arr.resize(an->elements.size());
  965. for (int i = 0; i < an->elements.size(); i++) {
  966. ConstantNode *acn = static_cast<ConstantNode *>(an->elements[i]);
  967. arr[i] = acn->value;
  968. }
  969. cn->value = arr;
  970. return cn;
  971. }
  972. return an;
  973. } break;
  974. case Node::TYPE_DICTIONARY: {
  975. DictionaryNode *dn = static_cast<DictionaryNode *>(p_node);
  976. bool all_constants = true;
  977. for (int i = 0; i < dn->elements.size(); i++) {
  978. dn->elements[i].key = _reduce_expression(dn->elements[i].key, p_to_const);
  979. if (dn->elements[i].key->type != Node::TYPE_CONSTANT)
  980. all_constants = false;
  981. dn->elements[i].value = _reduce_expression(dn->elements[i].value, p_to_const);
  982. if (dn->elements[i].value->type != Node::TYPE_CONSTANT)
  983. all_constants = false;
  984. }
  985. if (all_constants && p_to_const) {
  986. //reduce constant array expression
  987. ConstantNode *cn = alloc_node<ConstantNode>();
  988. Dictionary dict(!p_to_const);
  989. for (int i = 0; i < dn->elements.size(); i++) {
  990. ConstantNode *key_c = static_cast<ConstantNode *>(dn->elements[i].key);
  991. ConstantNode *value_c = static_cast<ConstantNode *>(dn->elements[i].value);
  992. dict[key_c->value] = value_c->value;
  993. }
  994. cn->value = dict;
  995. return cn;
  996. }
  997. return dn;
  998. } break;
  999. case Node::TYPE_OPERATOR: {
  1000. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  1001. bool all_constants = true;
  1002. int last_not_constant = -1;
  1003. for (int i = 0; i < op->arguments.size(); i++) {
  1004. op->arguments[i] = _reduce_expression(op->arguments[i], p_to_const);
  1005. if (op->arguments[i]->type != Node::TYPE_CONSTANT) {
  1006. all_constants = false;
  1007. last_not_constant = i;
  1008. }
  1009. }
  1010. if (op->op == OperatorNode::OP_EXTENDS) {
  1011. //nothing much
  1012. return op;
  1013. }
  1014. if (op->op == OperatorNode::OP_PARENT_CALL) {
  1015. //nothing much
  1016. return op;
  1017. } else if (op->op == OperatorNode::OP_CALL) {
  1018. //can reduce base type constructors
  1019. 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) {
  1020. //native type constructor or intrinsic function
  1021. const Variant **vptr = NULL;
  1022. Vector<Variant *> ptrs;
  1023. if (op->arguments.size() > 1) {
  1024. ptrs.resize(op->arguments.size() - 1);
  1025. for (int i = 0; i < ptrs.size(); i++) {
  1026. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i + 1]);
  1027. ptrs[i] = &cn->value;
  1028. }
  1029. vptr = (const Variant **)&ptrs[0];
  1030. }
  1031. Variant::CallError ce;
  1032. Variant v;
  1033. if (op->arguments[0]->type == Node::TYPE_TYPE) {
  1034. TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
  1035. v = Variant::construct(tn->vtype, vptr, ptrs.size(), ce);
  1036. } else {
  1037. GDFunctions::Function func = static_cast<BuiltInFunctionNode *>(op->arguments[0])->function;
  1038. GDFunctions::call(func, vptr, ptrs.size(), v, ce);
  1039. }
  1040. if (ce.error != Variant::CallError::CALL_OK) {
  1041. String errwhere;
  1042. if (op->arguments[0]->type == Node::TYPE_TYPE) {
  1043. TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
  1044. errwhere = "'" + Variant::get_type_name(tn->vtype) + "'' constructor";
  1045. } else {
  1046. GDFunctions::Function func = static_cast<BuiltInFunctionNode *>(op->arguments[0])->function;
  1047. errwhere = String("'") + GDFunctions::get_func_name(func) + "'' intrinsic function";
  1048. }
  1049. switch (ce.error) {
  1050. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1051. _set_error("Invalid argument (#" + itos(ce.argument + 1) + ") for " + errwhere + ".");
  1052. } break;
  1053. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1054. _set_error("Too many arguments for " + errwhere + ".");
  1055. } break;
  1056. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  1057. _set_error("Too few arguments for " + errwhere + ".");
  1058. } break;
  1059. default: {
  1060. _set_error("Invalid arguments for " + errwhere + ".");
  1061. } break;
  1062. }
  1063. error_line = op->line;
  1064. return p_node;
  1065. }
  1066. ConstantNode *cn = alloc_node<ConstantNode>();
  1067. cn->value = v;
  1068. return cn;
  1069. } else if (op->arguments[0]->type == Node::TYPE_BUILT_IN_FUNCTION && last_not_constant == 0) {
  1070. }
  1071. return op; //don't reduce yet
  1072. } else if (op->op == OperatorNode::OP_YIELD) {
  1073. return op;
  1074. } else if (op->op == OperatorNode::OP_INDEX) {
  1075. //can reduce indices into constant arrays or dictionaries
  1076. if (all_constants) {
  1077. ConstantNode *ca = static_cast<ConstantNode *>(op->arguments[0]);
  1078. ConstantNode *cb = static_cast<ConstantNode *>(op->arguments[1]);
  1079. bool valid;
  1080. Variant v = ca->value.get(cb->value, &valid);
  1081. if (!valid) {
  1082. _set_error("invalid index in constant expression");
  1083. error_line = op->line;
  1084. return op;
  1085. }
  1086. ConstantNode *cn = alloc_node<ConstantNode>();
  1087. cn->value = v;
  1088. return cn;
  1089. } /*else if (op->arguments[0]->type==Node::TYPE_CONSTANT && op->arguments[1]->type==Node::TYPE_IDENTIFIER) {
  1090. ConstantNode *ca = static_cast<ConstantNode*>(op->arguments[0]);
  1091. IdentifierNode *ib = static_cast<IdentifierNode*>(op->arguments[1]);
  1092. bool valid;
  1093. Variant v = ca->value.get_named(ib->name,&valid);
  1094. if (!valid) {
  1095. _set_error("invalid index '"+String(ib->name)+"' in constant expression");
  1096. return op;
  1097. }
  1098. ConstantNode *cn = alloc_node<ConstantNode>();
  1099. cn->value=v;
  1100. return cn;
  1101. }*/
  1102. return op;
  1103. } else if (op->op == OperatorNode::OP_INDEX_NAMED) {
  1104. if (op->arguments[0]->type == Node::TYPE_CONSTANT && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  1105. ConstantNode *ca = static_cast<ConstantNode *>(op->arguments[0]);
  1106. IdentifierNode *ib = static_cast<IdentifierNode *>(op->arguments[1]);
  1107. bool valid;
  1108. Variant v = ca->value.get_named(ib->name, &valid);
  1109. if (!valid) {
  1110. _set_error("invalid index '" + String(ib->name) + "' in constant expression");
  1111. error_line = op->line;
  1112. return op;
  1113. }
  1114. ConstantNode *cn = alloc_node<ConstantNode>();
  1115. cn->value = v;
  1116. return cn;
  1117. }
  1118. return op;
  1119. }
  1120. //validate assignment (don't assign to cosntant expression
  1121. switch (op->op) {
  1122. case OperatorNode::OP_ASSIGN:
  1123. case OperatorNode::OP_ASSIGN_ADD:
  1124. case OperatorNode::OP_ASSIGN_SUB:
  1125. case OperatorNode::OP_ASSIGN_MUL:
  1126. case OperatorNode::OP_ASSIGN_DIV:
  1127. case OperatorNode::OP_ASSIGN_MOD:
  1128. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  1129. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  1130. case OperatorNode::OP_ASSIGN_BIT_AND:
  1131. case OperatorNode::OP_ASSIGN_BIT_OR:
  1132. case OperatorNode::OP_ASSIGN_BIT_XOR: {
  1133. if (op->arguments[0]->type == Node::TYPE_CONSTANT) {
  1134. _set_error("Can't assign to constant", tokenizer->get_token_line() - 1);
  1135. error_line = op->line;
  1136. return op;
  1137. }
  1138. if (op->arguments[0]->type == Node::TYPE_OPERATOR) {
  1139. OperatorNode *on = static_cast<OperatorNode *>(op->arguments[0]);
  1140. if (on->op != OperatorNode::OP_INDEX && on->op != OperatorNode::OP_INDEX_NAMED) {
  1141. _set_error("Can't assign to an expression", tokenizer->get_token_line() - 1);
  1142. error_line = op->line;
  1143. return op;
  1144. }
  1145. }
  1146. } break;
  1147. default: { break; }
  1148. }
  1149. //now se if all are constants
  1150. if (!all_constants)
  1151. return op; //nothing to reduce from here on
  1152. #define _REDUCE_UNARY(m_vop) \
  1153. bool valid = false; \
  1154. Variant res; \
  1155. Variant::evaluate(m_vop, static_cast<ConstantNode *>(op->arguments[0])->value, Variant(), res, valid); \
  1156. if (!valid) { \
  1157. _set_error("Invalid operand for unary operator"); \
  1158. error_line = op->line; \
  1159. return p_node; \
  1160. } \
  1161. ConstantNode *cn = alloc_node<ConstantNode>(); \
  1162. cn->value = res; \
  1163. return cn;
  1164. #define _REDUCE_BINARY(m_vop) \
  1165. bool valid = false; \
  1166. Variant res; \
  1167. Variant::evaluate(m_vop, static_cast<ConstantNode *>(op->arguments[0])->value, static_cast<ConstantNode *>(op->arguments[1])->value, res, valid); \
  1168. if (!valid) { \
  1169. _set_error("Invalid operands for operator"); \
  1170. error_line = op->line; \
  1171. return p_node; \
  1172. } \
  1173. ConstantNode *cn = alloc_node<ConstantNode>(); \
  1174. cn->value = res; \
  1175. return cn;
  1176. switch (op->op) {
  1177. //unary operators
  1178. case OperatorNode::OP_NEG: {
  1179. _REDUCE_UNARY(Variant::OP_NEGATE);
  1180. } break;
  1181. case OperatorNode::OP_NOT: {
  1182. _REDUCE_UNARY(Variant::OP_NOT);
  1183. } break;
  1184. case OperatorNode::OP_BIT_INVERT: {
  1185. _REDUCE_UNARY(Variant::OP_BIT_NEGATE);
  1186. } break;
  1187. //binary operators (in precedence order)
  1188. case OperatorNode::OP_IN: {
  1189. _REDUCE_BINARY(Variant::OP_IN);
  1190. } break;
  1191. case OperatorNode::OP_EQUAL: {
  1192. _REDUCE_BINARY(Variant::OP_EQUAL);
  1193. } break;
  1194. case OperatorNode::OP_NOT_EQUAL: {
  1195. _REDUCE_BINARY(Variant::OP_NOT_EQUAL);
  1196. } break;
  1197. case OperatorNode::OP_LESS: {
  1198. _REDUCE_BINARY(Variant::OP_LESS);
  1199. } break;
  1200. case OperatorNode::OP_LESS_EQUAL: {
  1201. _REDUCE_BINARY(Variant::OP_LESS_EQUAL);
  1202. } break;
  1203. case OperatorNode::OP_GREATER: {
  1204. _REDUCE_BINARY(Variant::OP_GREATER);
  1205. } break;
  1206. case OperatorNode::OP_GREATER_EQUAL: {
  1207. _REDUCE_BINARY(Variant::OP_GREATER_EQUAL);
  1208. } break;
  1209. case OperatorNode::OP_AND: {
  1210. _REDUCE_BINARY(Variant::OP_AND);
  1211. } break;
  1212. case OperatorNode::OP_OR: {
  1213. _REDUCE_BINARY(Variant::OP_OR);
  1214. } break;
  1215. case OperatorNode::OP_ADD: {
  1216. _REDUCE_BINARY(Variant::OP_ADD);
  1217. } break;
  1218. case OperatorNode::OP_SUB: {
  1219. _REDUCE_BINARY(Variant::OP_SUBSTRACT);
  1220. } break;
  1221. case OperatorNode::OP_MUL: {
  1222. _REDUCE_BINARY(Variant::OP_MULTIPLY);
  1223. } break;
  1224. case OperatorNode::OP_DIV: {
  1225. _REDUCE_BINARY(Variant::OP_DIVIDE);
  1226. } break;
  1227. case OperatorNode::OP_MOD: {
  1228. _REDUCE_BINARY(Variant::OP_MODULE);
  1229. } break;
  1230. case OperatorNode::OP_SHIFT_LEFT: {
  1231. _REDUCE_BINARY(Variant::OP_SHIFT_LEFT);
  1232. } break;
  1233. case OperatorNode::OP_SHIFT_RIGHT: {
  1234. _REDUCE_BINARY(Variant::OP_SHIFT_RIGHT);
  1235. } break;
  1236. case OperatorNode::OP_BIT_AND: {
  1237. _REDUCE_BINARY(Variant::OP_BIT_AND);
  1238. } break;
  1239. case OperatorNode::OP_BIT_OR: {
  1240. _REDUCE_BINARY(Variant::OP_BIT_OR);
  1241. } break;
  1242. case OperatorNode::OP_BIT_XOR: {
  1243. _REDUCE_BINARY(Variant::OP_BIT_XOR);
  1244. } break;
  1245. default: { ERR_FAIL_V(op); }
  1246. }
  1247. ERR_FAIL_V(op);
  1248. } break;
  1249. default: {
  1250. return p_node;
  1251. } break;
  1252. }
  1253. }
  1254. GDParser::Node *GDParser::_parse_and_reduce_expression(Node *p_parent, bool p_static, bool p_reduce_const, bool p_allow_assign) {
  1255. Node *expr = _parse_expression(p_parent, p_static, p_allow_assign, p_reduce_const);
  1256. if (!expr || error_set)
  1257. return NULL;
  1258. expr = _reduce_expression(expr, p_reduce_const);
  1259. if (!expr || error_set)
  1260. return NULL;
  1261. return expr;
  1262. }
  1263. bool GDParser::_recover_from_completion() {
  1264. if (!completion_found) {
  1265. return false; //can't recover if no completion
  1266. }
  1267. //skip stuff until newline
  1268. while (tokenizer->get_token() != GDTokenizer::TK_NEWLINE && tokenizer->get_token() != GDTokenizer::TK_EOF && tokenizer->get_token() != GDTokenizer::TK_ERROR) {
  1269. tokenizer->advance();
  1270. }
  1271. completion_found = false;
  1272. error_set = false;
  1273. if (tokenizer->get_token() == GDTokenizer::TK_ERROR) {
  1274. error_set = true;
  1275. }
  1276. return true;
  1277. }
  1278. void GDParser::_parse_block(BlockNode *p_block, bool p_static) {
  1279. int indent_level = tab_level.back()->get();
  1280. #ifdef DEBUG_ENABLED
  1281. NewLineNode *nl = alloc_node<NewLineNode>();
  1282. nl->line = tokenizer->get_token_line();
  1283. p_block->statements.push_back(nl);
  1284. #endif
  1285. while (true) {
  1286. GDTokenizer::Token token = tokenizer->get_token();
  1287. if (error_set)
  1288. return;
  1289. if (indent_level > tab_level.back()->get()) {
  1290. p_block->end_line = tokenizer->get_token_line();
  1291. return; //go back a level
  1292. }
  1293. if (pending_newline != -1) {
  1294. NewLineNode *nl = alloc_node<NewLineNode>();
  1295. nl->line = pending_newline;
  1296. p_block->statements.push_back(nl);
  1297. pending_newline = -1;
  1298. }
  1299. switch (token) {
  1300. case GDTokenizer::TK_EOF:
  1301. p_block->end_line = tokenizer->get_token_line();
  1302. case GDTokenizer::TK_ERROR: {
  1303. return; //go back
  1304. //end of file!
  1305. } break;
  1306. case GDTokenizer::TK_NEWLINE: {
  1307. if (!_parse_newline()) {
  1308. if (!error_set) {
  1309. p_block->end_line = tokenizer->get_token_line();
  1310. pending_newline = p_block->end_line;
  1311. }
  1312. return;
  1313. }
  1314. NewLineNode *nl = alloc_node<NewLineNode>();
  1315. nl->line = tokenizer->get_token_line();
  1316. p_block->statements.push_back(nl);
  1317. } break;
  1318. case GDTokenizer::TK_CF_PASS: {
  1319. if (tokenizer->get_token(1) != GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1) != GDTokenizer::TK_NEWLINE && tokenizer->get_token(1) != GDTokenizer::TK_EOF) {
  1320. _set_error("Expected ';' or <NewLine>.");
  1321. return;
  1322. }
  1323. tokenizer->advance();
  1324. if (tokenizer->get_token() == GDTokenizer::TK_SEMICOLON) {
  1325. // Ignore semicolon after 'pass'
  1326. tokenizer->advance();
  1327. }
  1328. } break;
  1329. case GDTokenizer::TK_PR_VAR: {
  1330. //variale declaration and (eventual) initialization
  1331. tokenizer->advance();
  1332. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  1333. _set_error("Expected identifier for local variable name.");
  1334. return;
  1335. }
  1336. StringName n = tokenizer->get_token_identifier();
  1337. tokenizer->advance();
  1338. int var_line = tokenizer->get_token_line();
  1339. //must know when the local variable is declared
  1340. LocalVarNode *lv = alloc_node<LocalVarNode>();
  1341. lv->name = n;
  1342. p_block->statements.push_back(lv);
  1343. Node *assigned = NULL;
  1344. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  1345. tokenizer->advance();
  1346. Node *subexpr = NULL;
  1347. subexpr = _parse_and_reduce_expression(p_block, p_static);
  1348. if (!subexpr) {
  1349. if (_recover_from_completion()) {
  1350. break;
  1351. }
  1352. return;
  1353. }
  1354. lv->assign = subexpr;
  1355. assigned = subexpr;
  1356. } else {
  1357. ConstantNode *c = alloc_node<ConstantNode>();
  1358. c->value = Variant();
  1359. assigned = c;
  1360. }
  1361. //must be added later, to avoid self-referencing.
  1362. p_block->variables.push_back(n); //line?
  1363. p_block->variable_lines.push_back(var_line);
  1364. IdentifierNode *id = alloc_node<IdentifierNode>();
  1365. id->name = n;
  1366. OperatorNode *op = alloc_node<OperatorNode>();
  1367. op->op = OperatorNode::OP_ASSIGN;
  1368. op->arguments.push_back(id);
  1369. op->arguments.push_back(assigned);
  1370. p_block->statements.push_back(op);
  1371. if (!_end_statement()) {
  1372. _set_error("Expected end of statement (var)");
  1373. return;
  1374. }
  1375. } break;
  1376. case GDTokenizer::TK_CF_IF: {
  1377. tokenizer->advance();
  1378. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  1379. if (!condition) {
  1380. if (_recover_from_completion()) {
  1381. break;
  1382. }
  1383. return;
  1384. }
  1385. ControlFlowNode *cf_if = alloc_node<ControlFlowNode>();
  1386. cf_if->cf_type = ControlFlowNode::CF_IF;
  1387. cf_if->arguments.push_back(condition);
  1388. cf_if->body = alloc_node<BlockNode>();
  1389. cf_if->body->parent_block = p_block;
  1390. p_block->sub_blocks.push_back(cf_if->body);
  1391. if (!_enter_indent_block(cf_if->body)) {
  1392. _set_error("Expected indented block after 'if'");
  1393. p_block->end_line = tokenizer->get_token_line();
  1394. return;
  1395. }
  1396. current_block = cf_if->body;
  1397. _parse_block(cf_if->body, p_static);
  1398. current_block = p_block;
  1399. if (error_set)
  1400. return;
  1401. p_block->statements.push_back(cf_if);
  1402. while (true) {
  1403. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  1404. tokenizer->advance();
  1405. }
  1406. if (tab_level.back()->get() < indent_level) { //not at current indent level
  1407. p_block->end_line = tokenizer->get_token_line();
  1408. return;
  1409. }
  1410. if (tokenizer->get_token() == GDTokenizer::TK_CF_ELIF) {
  1411. if (tab_level.back()->get() > indent_level) {
  1412. _set_error("Invalid indent");
  1413. return;
  1414. }
  1415. tokenizer->advance();
  1416. cf_if->body_else = alloc_node<BlockNode>();
  1417. cf_if->body_else->parent_block = p_block;
  1418. p_block->sub_blocks.push_back(cf_if->body_else);
  1419. ControlFlowNode *cf_else = alloc_node<ControlFlowNode>();
  1420. cf_else->cf_type = ControlFlowNode::CF_IF;
  1421. //condition
  1422. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  1423. if (!condition) {
  1424. if (_recover_from_completion()) {
  1425. break;
  1426. }
  1427. return;
  1428. }
  1429. cf_else->arguments.push_back(condition);
  1430. cf_else->cf_type = ControlFlowNode::CF_IF;
  1431. cf_if->body_else->statements.push_back(cf_else);
  1432. cf_if = cf_else;
  1433. cf_if->body = alloc_node<BlockNode>();
  1434. cf_if->body->parent_block = p_block;
  1435. p_block->sub_blocks.push_back(cf_if->body);
  1436. if (!_enter_indent_block(cf_if->body)) {
  1437. _set_error("Expected indented block after 'elif'");
  1438. p_block->end_line = tokenizer->get_token_line();
  1439. return;
  1440. }
  1441. current_block = cf_else->body;
  1442. _parse_block(cf_else->body, p_static);
  1443. current_block = p_block;
  1444. if (error_set)
  1445. return;
  1446. } else if (tokenizer->get_token() == GDTokenizer::TK_CF_ELSE) {
  1447. if (tab_level.back()->get() > indent_level) {
  1448. _set_error("Invalid indent");
  1449. return;
  1450. }
  1451. tokenizer->advance();
  1452. cf_if->body_else = alloc_node<BlockNode>();
  1453. cf_if->body_else->parent_block = p_block;
  1454. p_block->sub_blocks.push_back(cf_if->body_else);
  1455. if (!_enter_indent_block(cf_if->body_else)) {
  1456. _set_error("Expected indented block after 'else'");
  1457. p_block->end_line = tokenizer->get_token_line();
  1458. return;
  1459. }
  1460. current_block = cf_if->body_else;
  1461. _parse_block(cf_if->body_else, p_static);
  1462. current_block = p_block;
  1463. if (error_set)
  1464. return;
  1465. break; //after else, exit
  1466. } else
  1467. break;
  1468. }
  1469. } break;
  1470. case GDTokenizer::TK_CF_WHILE: {
  1471. tokenizer->advance();
  1472. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  1473. if (!condition) {
  1474. if (_recover_from_completion()) {
  1475. break;
  1476. }
  1477. return;
  1478. }
  1479. ControlFlowNode *cf_while = alloc_node<ControlFlowNode>();
  1480. cf_while->cf_type = ControlFlowNode::CF_WHILE;
  1481. cf_while->arguments.push_back(condition);
  1482. cf_while->body = alloc_node<BlockNode>();
  1483. cf_while->body->parent_block = p_block;
  1484. p_block->sub_blocks.push_back(cf_while->body);
  1485. if (!_enter_indent_block(cf_while->body)) {
  1486. _set_error("Expected indented block after 'while'");
  1487. p_block->end_line = tokenizer->get_token_line();
  1488. return;
  1489. }
  1490. current_block = cf_while->body;
  1491. _parse_block(cf_while->body, p_static);
  1492. current_block = p_block;
  1493. if (error_set)
  1494. return;
  1495. p_block->statements.push_back(cf_while);
  1496. } break;
  1497. case GDTokenizer::TK_CF_FOR: {
  1498. tokenizer->advance();
  1499. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  1500. _set_error("identifier expected after 'for'");
  1501. }
  1502. IdentifierNode *id = alloc_node<IdentifierNode>();
  1503. id->name = tokenizer->get_token_identifier();
  1504. tokenizer->advance();
  1505. if (tokenizer->get_token() != GDTokenizer::TK_OP_IN) {
  1506. _set_error("'in' expected after identifier");
  1507. return;
  1508. }
  1509. tokenizer->advance();
  1510. Node *container = _parse_and_reduce_expression(p_block, p_static);
  1511. if (!container) {
  1512. if (_recover_from_completion()) {
  1513. break;
  1514. }
  1515. return;
  1516. }
  1517. ControlFlowNode *cf_for = alloc_node<ControlFlowNode>();
  1518. cf_for->cf_type = ControlFlowNode::CF_FOR;
  1519. cf_for->arguments.push_back(id);
  1520. cf_for->arguments.push_back(container);
  1521. cf_for->body = alloc_node<BlockNode>();
  1522. cf_for->body->parent_block = p_block;
  1523. p_block->sub_blocks.push_back(cf_for->body);
  1524. if (!_enter_indent_block(cf_for->body)) {
  1525. _set_error("Expected indented block after 'for'");
  1526. p_block->end_line = tokenizer->get_token_line();
  1527. return;
  1528. }
  1529. current_block = cf_for->body;
  1530. _parse_block(cf_for->body, p_static);
  1531. current_block = p_block;
  1532. if (error_set)
  1533. return;
  1534. p_block->statements.push_back(cf_for);
  1535. } break;
  1536. case GDTokenizer::TK_CF_CONTINUE: {
  1537. tokenizer->advance();
  1538. ControlFlowNode *cf_continue = alloc_node<ControlFlowNode>();
  1539. cf_continue->cf_type = ControlFlowNode::CF_CONTINUE;
  1540. p_block->statements.push_back(cf_continue);
  1541. if (!_end_statement()) {
  1542. _set_error("Expected end of statement (continue)");
  1543. return;
  1544. }
  1545. } break;
  1546. case GDTokenizer::TK_CF_BREAK: {
  1547. tokenizer->advance();
  1548. ControlFlowNode *cf_break = alloc_node<ControlFlowNode>();
  1549. cf_break->cf_type = ControlFlowNode::CF_BREAK;
  1550. p_block->statements.push_back(cf_break);
  1551. if (!_end_statement()) {
  1552. _set_error("Expected end of statement (break)");
  1553. return;
  1554. }
  1555. } break;
  1556. case GDTokenizer::TK_CF_RETURN: {
  1557. tokenizer->advance();
  1558. ControlFlowNode *cf_return = alloc_node<ControlFlowNode>();
  1559. cf_return->cf_type = ControlFlowNode::CF_RETURN;
  1560. if (tokenizer->get_token() == GDTokenizer::TK_SEMICOLON || tokenizer->get_token() == GDTokenizer::TK_NEWLINE || tokenizer->get_token() == GDTokenizer::TK_EOF) {
  1561. //expect end of statement
  1562. p_block->statements.push_back(cf_return);
  1563. if (!_end_statement()) {
  1564. return;
  1565. }
  1566. } else {
  1567. //expect expression
  1568. Node *retexpr = _parse_and_reduce_expression(p_block, p_static);
  1569. if (!retexpr) {
  1570. if (_recover_from_completion()) {
  1571. break;
  1572. }
  1573. return;
  1574. }
  1575. cf_return->arguments.push_back(retexpr);
  1576. p_block->statements.push_back(cf_return);
  1577. if (!_end_statement()) {
  1578. _set_error("Expected end of statement after return expression.");
  1579. return;
  1580. }
  1581. }
  1582. } break;
  1583. case GDTokenizer::TK_PR_ASSERT: {
  1584. tokenizer->advance();
  1585. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  1586. if (!condition) {
  1587. if (_recover_from_completion()) {
  1588. break;
  1589. }
  1590. return;
  1591. }
  1592. AssertNode *an = alloc_node<AssertNode>();
  1593. an->condition = condition;
  1594. p_block->statements.push_back(an);
  1595. if (!_end_statement()) {
  1596. _set_error("Expected end of statement after assert.");
  1597. return;
  1598. }
  1599. } break;
  1600. case GDTokenizer::TK_PR_BREAKPOINT: {
  1601. tokenizer->advance();
  1602. BreakpointNode *bn = alloc_node<BreakpointNode>();
  1603. p_block->statements.push_back(bn);
  1604. if (!_end_statement()) {
  1605. _set_error("Expected end of statement after breakpoint.");
  1606. return;
  1607. }
  1608. } break;
  1609. default: {
  1610. Node *expression = _parse_and_reduce_expression(p_block, p_static, false, true);
  1611. if (!expression) {
  1612. if (_recover_from_completion()) {
  1613. break;
  1614. }
  1615. return;
  1616. }
  1617. p_block->statements.push_back(expression);
  1618. if (!_end_statement()) {
  1619. _set_error("Expected end of statement after expression.");
  1620. return;
  1621. }
  1622. } break;
  1623. /*
  1624. case GDTokenizer::TK_CF_LOCAL: {
  1625. if (tokenizer->get_token(1)!=GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE ) {
  1626. _set_error("Expected ';' or <NewLine>.");
  1627. }
  1628. tokenizer->advance();
  1629. } break;
  1630. */
  1631. }
  1632. }
  1633. }
  1634. bool GDParser::_parse_newline() {
  1635. if (tokenizer->get_token(1) != GDTokenizer::TK_EOF && tokenizer->get_token(1) != GDTokenizer::TK_NEWLINE) {
  1636. int indent = tokenizer->get_token_line_indent();
  1637. int current_indent = tab_level.back()->get();
  1638. if (indent > current_indent) {
  1639. _set_error("Unexpected indent.");
  1640. return false;
  1641. }
  1642. if (indent < current_indent) {
  1643. while (indent < current_indent) {
  1644. //exit block
  1645. if (tab_level.size() == 1) {
  1646. _set_error("Invalid indent. BUG?");
  1647. return false;
  1648. }
  1649. tab_level.pop_back();
  1650. if (tab_level.back()->get() < indent) {
  1651. _set_error("Unindent does not match any outer indentation level.");
  1652. return false;
  1653. }
  1654. current_indent = tab_level.back()->get();
  1655. }
  1656. tokenizer->advance();
  1657. return false;
  1658. }
  1659. }
  1660. tokenizer->advance();
  1661. return true;
  1662. }
  1663. void GDParser::_parse_extends(ClassNode *p_class) {
  1664. if (p_class->extends_used) {
  1665. _set_error("'extends' already used for this class.");
  1666. return;
  1667. }
  1668. if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty()) {
  1669. _set_error("'extends' must be used before anything else.");
  1670. return;
  1671. }
  1672. p_class->extends_used = true;
  1673. tokenizer->advance();
  1674. if (tokenizer->get_token() == GDTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token_type() == Variant::OBJECT) {
  1675. p_class->extends_class.push_back(Variant::get_type_name(Variant::OBJECT));
  1676. tokenizer->advance();
  1677. return;
  1678. }
  1679. // see if inheritance happens from a file
  1680. if (tokenizer->get_token() == GDTokenizer::TK_CONSTANT) {
  1681. Variant constant = tokenizer->get_token_constant();
  1682. if (constant.get_type() != Variant::STRING) {
  1683. _set_error("'extends' constant must be a string.");
  1684. return;
  1685. }
  1686. p_class->extends_file = constant;
  1687. tokenizer->advance();
  1688. if (tokenizer->get_token() != GDTokenizer::TK_PERIOD) {
  1689. return;
  1690. } else
  1691. tokenizer->advance();
  1692. }
  1693. while (true) {
  1694. switch (tokenizer->get_token()) {
  1695. case GDTokenizer::TK_IDENTIFIER: {
  1696. StringName identifier = tokenizer->get_token_identifier();
  1697. p_class->extends_class.push_back(identifier);
  1698. } break;
  1699. case GDTokenizer::TK_PERIOD:
  1700. break;
  1701. default: {
  1702. _set_error("Invalid 'extends' syntax, expected string constant (path) and/or identifier (parent class).");
  1703. return;
  1704. }
  1705. }
  1706. tokenizer->advance(1);
  1707. switch (tokenizer->get_token()) {
  1708. case GDTokenizer::TK_IDENTIFIER:
  1709. case GDTokenizer::TK_PERIOD:
  1710. continue;
  1711. default:
  1712. return;
  1713. }
  1714. }
  1715. }
  1716. void GDParser::_parse_class(ClassNode *p_class) {
  1717. int indent_level = tab_level.back()->get();
  1718. while (true) {
  1719. GDTokenizer::Token token = tokenizer->get_token();
  1720. if (error_set)
  1721. return;
  1722. if (indent_level > tab_level.back()->get()) {
  1723. p_class->end_line = tokenizer->get_token_line();
  1724. return; //go back a level
  1725. }
  1726. switch (token) {
  1727. case GDTokenizer::TK_EOF:
  1728. p_class->end_line = tokenizer->get_token_line();
  1729. case GDTokenizer::TK_ERROR: {
  1730. return; //go back
  1731. //end of file!
  1732. } break;
  1733. case GDTokenizer::TK_NEWLINE: {
  1734. if (!_parse_newline()) {
  1735. if (!error_set) {
  1736. p_class->end_line = tokenizer->get_token_line();
  1737. }
  1738. return;
  1739. }
  1740. } break;
  1741. case GDTokenizer::TK_PR_EXTENDS: {
  1742. _parse_extends(p_class);
  1743. if (error_set)
  1744. return;
  1745. if (!_end_statement()) {
  1746. _set_error("Expected end of statement after extends");
  1747. return;
  1748. }
  1749. } break;
  1750. case GDTokenizer::TK_PR_TOOL: {
  1751. if (p_class->tool) {
  1752. _set_error("tool used more than once");
  1753. return;
  1754. }
  1755. p_class->tool = true;
  1756. tokenizer->advance();
  1757. } break;
  1758. case GDTokenizer::TK_PR_CLASS: {
  1759. //class inside class :D
  1760. StringName name;
  1761. StringName extends;
  1762. if (tokenizer->get_token(1) != GDTokenizer::TK_IDENTIFIER) {
  1763. _set_error("'class' syntax: 'class <Name>:' or 'class <Name> extends <BaseClass>:'");
  1764. return;
  1765. }
  1766. name = tokenizer->get_token_identifier(1);
  1767. tokenizer->advance(2);
  1768. ClassNode *newclass = alloc_node<ClassNode>();
  1769. newclass->initializer = alloc_node<BlockNode>();
  1770. newclass->initializer->parent_class = newclass;
  1771. newclass->ready = alloc_node<BlockNode>();
  1772. newclass->ready->parent_class = newclass;
  1773. newclass->name = name;
  1774. newclass->owner = p_class;
  1775. p_class->subclasses.push_back(newclass);
  1776. if (tokenizer->get_token() == GDTokenizer::TK_PR_EXTENDS) {
  1777. _parse_extends(newclass);
  1778. if (error_set)
  1779. return;
  1780. }
  1781. if (!_enter_indent_block()) {
  1782. _set_error("Indented block expected.");
  1783. return;
  1784. }
  1785. current_class = newclass;
  1786. _parse_class(newclass);
  1787. current_class = p_class;
  1788. } break;
  1789. /* this is for functions....
  1790. case GDTokenizer::TK_CF_PASS: {
  1791. tokenizer->advance(1);
  1792. } break;
  1793. */
  1794. case GDTokenizer::TK_PR_STATIC: {
  1795. tokenizer->advance();
  1796. if (tokenizer->get_token() != GDTokenizer::TK_PR_FUNCTION) {
  1797. _set_error("Expected 'func'.");
  1798. return;
  1799. }
  1800. }; //fallthrough to function
  1801. case GDTokenizer::TK_PR_FUNCTION: {
  1802. bool _static = false;
  1803. pending_newline = -1;
  1804. if (tokenizer->get_token(-1) == GDTokenizer::TK_PR_STATIC) {
  1805. _static = true;
  1806. }
  1807. tokenizer->advance();
  1808. StringName name;
  1809. if (_get_completable_identifier(COMPLETION_VIRTUAL_FUNC, name)) {
  1810. }
  1811. if (name == StringName()) {
  1812. _set_error("Expected identifier after 'func' (syntax: 'func <identifier>([arguments]):' ).");
  1813. return;
  1814. }
  1815. for (int i = 0; i < p_class->functions.size(); i++) {
  1816. if (p_class->functions[i]->name == name) {
  1817. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->functions[i]->line) + ").");
  1818. }
  1819. }
  1820. for (int i = 0; i < p_class->static_functions.size(); i++) {
  1821. if (p_class->static_functions[i]->name == name) {
  1822. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->static_functions[i]->line) + ").");
  1823. }
  1824. }
  1825. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_OPEN) {
  1826. _set_error("Expected '(' after identifier (syntax: 'func <identifier>([arguments]):' ).");
  1827. return;
  1828. }
  1829. tokenizer->advance();
  1830. Vector<StringName> arguments;
  1831. Vector<Node *> default_values;
  1832. int fnline = tokenizer->get_token_line();
  1833. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1834. //has arguments
  1835. bool defaulting = false;
  1836. while (true) {
  1837. if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  1838. tokenizer->advance();
  1839. continue;
  1840. }
  1841. if (tokenizer->get_token() == GDTokenizer::TK_PR_VAR) {
  1842. tokenizer->advance(); //var before the identifier is allowed
  1843. }
  1844. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  1845. _set_error("Expected identifier for argument.");
  1846. return;
  1847. }
  1848. StringName argname = tokenizer->get_token_identifier();
  1849. arguments.push_back(argname);
  1850. tokenizer->advance();
  1851. if (defaulting && tokenizer->get_token() != GDTokenizer::TK_OP_ASSIGN) {
  1852. _set_error("Default parameter expected.");
  1853. return;
  1854. }
  1855. //tokenizer->advance();
  1856. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  1857. defaulting = true;
  1858. tokenizer->advance(1);
  1859. Node *defval = NULL;
  1860. defval = _parse_and_reduce_expression(p_class, _static);
  1861. if (!defval || error_set)
  1862. return;
  1863. OperatorNode *on = alloc_node<OperatorNode>();
  1864. on->op = OperatorNode::OP_ASSIGN;
  1865. IdentifierNode *in = alloc_node<IdentifierNode>();
  1866. in->name = argname;
  1867. on->arguments.push_back(in);
  1868. on->arguments.push_back(defval);
  1869. /* no ..
  1870. if (defval->type!=Node::TYPE_CONSTANT) {
  1871. _set_error("default argument must be constant");
  1872. }
  1873. */
  1874. default_values.push_back(on);
  1875. }
  1876. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  1877. tokenizer->advance();
  1878. }
  1879. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  1880. tokenizer->advance();
  1881. continue;
  1882. } else if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1883. _set_error("Expected ',' or ')'.");
  1884. return;
  1885. }
  1886. break;
  1887. }
  1888. }
  1889. tokenizer->advance();
  1890. BlockNode *block = alloc_node<BlockNode>();
  1891. block->parent_class = p_class;
  1892. if (name == "_init") {
  1893. if (p_class->extends_used) {
  1894. OperatorNode *cparent = alloc_node<OperatorNode>();
  1895. cparent->op = OperatorNode::OP_PARENT_CALL;
  1896. block->statements.push_back(cparent);
  1897. IdentifierNode *id = alloc_node<IdentifierNode>();
  1898. id->name = "_init";
  1899. cparent->arguments.push_back(id);
  1900. if (tokenizer->get_token() == GDTokenizer::TK_PERIOD) {
  1901. tokenizer->advance();
  1902. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_OPEN) {
  1903. _set_error("expected '(' for parent constructor arguments.");
  1904. }
  1905. tokenizer->advance();
  1906. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1907. //has arguments
  1908. parenthesis++;
  1909. while (true) {
  1910. Node *arg = _parse_and_reduce_expression(p_class, _static);
  1911. cparent->arguments.push_back(arg);
  1912. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  1913. tokenizer->advance();
  1914. continue;
  1915. } else if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1916. _set_error("Expected ',' or ')'.");
  1917. return;
  1918. }
  1919. break;
  1920. }
  1921. parenthesis--;
  1922. }
  1923. tokenizer->advance();
  1924. }
  1925. } else {
  1926. if (tokenizer->get_token() == GDTokenizer::TK_PERIOD) {
  1927. _set_error("Parent constructor call found for a class without inheritance.");
  1928. return;
  1929. }
  1930. }
  1931. }
  1932. if (!_enter_indent_block(block)) {
  1933. _set_error("Indented block expected.");
  1934. return;
  1935. }
  1936. FunctionNode *function = alloc_node<FunctionNode>();
  1937. function->name = name;
  1938. function->arguments = arguments;
  1939. function->default_values = default_values;
  1940. function->_static = _static;
  1941. function->line = fnline;
  1942. if (_static)
  1943. p_class->static_functions.push_back(function);
  1944. else
  1945. p_class->functions.push_back(function);
  1946. current_function = function;
  1947. function->body = block;
  1948. current_block = block;
  1949. _parse_block(block, _static);
  1950. current_block = NULL;
  1951. //arguments
  1952. } break;
  1953. case GDTokenizer::TK_PR_SIGNAL: {
  1954. tokenizer->advance();
  1955. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  1956. _set_error("Expected identifier after 'signal'.");
  1957. return;
  1958. }
  1959. ClassNode::Signal sig;
  1960. sig.name = tokenizer->get_token_identifier();
  1961. tokenizer->advance();
  1962. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_OPEN) {
  1963. tokenizer->advance();
  1964. while (true) {
  1965. if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  1966. tokenizer->advance();
  1967. continue;
  1968. }
  1969. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1970. tokenizer->advance();
  1971. break;
  1972. }
  1973. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  1974. _set_error("Expected identifier in signal argument.");
  1975. return;
  1976. }
  1977. sig.arguments.push_back(tokenizer->get_token_identifier());
  1978. tokenizer->advance();
  1979. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  1980. tokenizer->advance();
  1981. }
  1982. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  1983. tokenizer->advance();
  1984. } else if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1985. _set_error("Expected ',' or ')' after signal parameter identifier.");
  1986. return;
  1987. }
  1988. }
  1989. }
  1990. p_class->_signals.push_back(sig);
  1991. if (!_end_statement()) {
  1992. _set_error("Expected end of statement (signal)");
  1993. return;
  1994. }
  1995. } break;
  1996. case GDTokenizer::TK_PR_EXPORT: {
  1997. tokenizer->advance();
  1998. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_OPEN) {
  1999. tokenizer->advance();
  2000. if (tokenizer->get_token() == GDTokenizer::TK_BUILT_IN_TYPE) {
  2001. Variant::Type type = tokenizer->get_token_type();
  2002. if (type == Variant::NIL) {
  2003. _set_error("Can't export null type.");
  2004. return;
  2005. }
  2006. current_export.type = type;
  2007. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  2008. tokenizer->advance();
  2009. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2010. // hint expected next!
  2011. tokenizer->advance();
  2012. switch (current_export.type) {
  2013. case Variant::INT: {
  2014. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  2015. current_export.hint = PROPERTY_HINT_ALL_FLAGS;
  2016. tokenizer->advance();
  2017. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2018. break;
  2019. }
  2020. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2021. _set_error("Expected ')' or ',' in bit flags hint.");
  2022. return;
  2023. }
  2024. current_export.hint = PROPERTY_HINT_FLAGS;
  2025. tokenizer->advance();
  2026. bool first = true;
  2027. while (true) {
  2028. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2029. current_export = PropertyInfo();
  2030. _set_error("Expected a string constant in named bit flags hint.");
  2031. return;
  2032. }
  2033. String c = tokenizer->get_token_constant();
  2034. if (!first)
  2035. current_export.hint_string += ",";
  2036. else
  2037. first = false;
  2038. current_export.hint_string += c.xml_escape();
  2039. tokenizer->advance();
  2040. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2041. break;
  2042. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2043. current_export = PropertyInfo();
  2044. _set_error("Expected ')' or ',' in named bit flags hint.");
  2045. return;
  2046. }
  2047. tokenizer->advance();
  2048. }
  2049. break;
  2050. }
  2051. if (tokenizer->get_token() == GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  2052. //enumeration
  2053. current_export.hint = PROPERTY_HINT_ENUM;
  2054. bool first = true;
  2055. while (true) {
  2056. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2057. current_export = PropertyInfo();
  2058. _set_error("Expected a string constant in enumeration hint.");
  2059. return;
  2060. }
  2061. String c = tokenizer->get_token_constant();
  2062. if (!first)
  2063. current_export.hint_string += ",";
  2064. else
  2065. first = false;
  2066. current_export.hint_string += c.xml_escape();
  2067. tokenizer->advance();
  2068. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2069. break;
  2070. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2071. current_export = PropertyInfo();
  2072. _set_error("Expected ')' or ',' in enumeration hint.");
  2073. return;
  2074. }
  2075. tokenizer->advance();
  2076. }
  2077. break;
  2078. }
  2079. }; //fallthrough to use the same
  2080. case Variant::REAL: {
  2081. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EASE") {
  2082. current_export.hint = PROPERTY_HINT_EXP_EASING;
  2083. tokenizer->advance();
  2084. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2085. _set_error("Expected ')' in hint.");
  2086. return;
  2087. }
  2088. break;
  2089. }
  2090. // range
  2091. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EXP") {
  2092. current_export.hint = PROPERTY_HINT_EXP_RANGE;
  2093. tokenizer->advance();
  2094. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2095. break;
  2096. else if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2097. _set_error("Expected ')' or ',' in exponential range hint.");
  2098. return;
  2099. }
  2100. tokenizer->advance();
  2101. } else
  2102. current_export.hint = PROPERTY_HINT_RANGE;
  2103. float sign = 1.0;
  2104. if (tokenizer->get_token() == GDTokenizer::TK_OP_SUB) {
  2105. sign = -1;
  2106. tokenizer->advance();
  2107. }
  2108. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2109. current_export = PropertyInfo();
  2110. _set_error("Expected a range in numeric hint.");
  2111. return;
  2112. }
  2113. current_export.hint_string = rtos(sign * double(tokenizer->get_token_constant()));
  2114. tokenizer->advance();
  2115. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2116. current_export.hint_string = "0," + current_export.hint_string;
  2117. break;
  2118. }
  2119. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2120. current_export = PropertyInfo();
  2121. _set_error("Expected ',' or ')' in numeric range hint.");
  2122. return;
  2123. }
  2124. tokenizer->advance();
  2125. sign = 1.0;
  2126. if (tokenizer->get_token() == GDTokenizer::TK_OP_SUB) {
  2127. sign = -1;
  2128. tokenizer->advance();
  2129. }
  2130. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2131. current_export = PropertyInfo();
  2132. _set_error("Expected a number as upper bound in numeric range hint.");
  2133. return;
  2134. }
  2135. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  2136. tokenizer->advance();
  2137. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2138. break;
  2139. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2140. current_export = PropertyInfo();
  2141. _set_error("Expected ',' or ')' in numeric range hint.");
  2142. return;
  2143. }
  2144. tokenizer->advance();
  2145. sign = 1.0;
  2146. if (tokenizer->get_token() == GDTokenizer::TK_OP_SUB) {
  2147. sign = -1;
  2148. tokenizer->advance();
  2149. }
  2150. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2151. current_export = PropertyInfo();
  2152. _set_error("Expected a number as step in numeric range hint.");
  2153. return;
  2154. }
  2155. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  2156. tokenizer->advance();
  2157. } break;
  2158. case Variant::STRING: {
  2159. if (tokenizer->get_token() == GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  2160. //enumeration
  2161. current_export.hint = PROPERTY_HINT_ENUM;
  2162. bool first = true;
  2163. while (true) {
  2164. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2165. current_export = PropertyInfo();
  2166. _set_error("Expected a string constant in enumeration hint.");
  2167. return;
  2168. }
  2169. String c = tokenizer->get_token_constant();
  2170. if (!first)
  2171. current_export.hint_string += ",";
  2172. else
  2173. first = false;
  2174. current_export.hint_string += c.xml_escape();
  2175. tokenizer->advance();
  2176. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2177. break;
  2178. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2179. current_export = PropertyInfo();
  2180. _set_error("Expected ')' or ',' in enumeration hint.");
  2181. return;
  2182. }
  2183. tokenizer->advance();
  2184. }
  2185. break;
  2186. }
  2187. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "DIR") {
  2188. tokenizer->advance();
  2189. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2190. current_export.hint = PROPERTY_HINT_DIR;
  2191. else if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2192. tokenizer->advance();
  2193. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier() == "GLOBAL")) {
  2194. _set_error("Expected 'GLOBAL' after comma in directory hint.");
  2195. return;
  2196. }
  2197. if (!p_class->tool) {
  2198. _set_error("Global filesystem hints may only be used in tool scripts.");
  2199. return;
  2200. }
  2201. current_export.hint = PROPERTY_HINT_GLOBAL_DIR;
  2202. tokenizer->advance();
  2203. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2204. _set_error("Expected ')' in hint.");
  2205. return;
  2206. }
  2207. } else {
  2208. _set_error("Expected ')' or ',' in hint.");
  2209. return;
  2210. }
  2211. break;
  2212. }
  2213. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FILE") {
  2214. current_export.hint = PROPERTY_HINT_FILE;
  2215. tokenizer->advance();
  2216. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2217. tokenizer->advance();
  2218. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "GLOBAL") {
  2219. if (!p_class->tool) {
  2220. _set_error("Global filesystem hints may only be used in tool scripts.");
  2221. return;
  2222. }
  2223. current_export.hint = PROPERTY_HINT_GLOBAL_FILE;
  2224. tokenizer->advance();
  2225. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2226. break;
  2227. else if (tokenizer->get_token() == GDTokenizer::TK_COMMA)
  2228. tokenizer->advance();
  2229. else {
  2230. _set_error("Expected ')' or ',' in hint.");
  2231. return;
  2232. }
  2233. }
  2234. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2235. if (current_export.hint == PROPERTY_HINT_GLOBAL_FILE)
  2236. _set_error("Expected string constant with filter");
  2237. else
  2238. _set_error("Expected 'GLOBAL' or string constant with filter");
  2239. return;
  2240. }
  2241. current_export.hint_string = tokenizer->get_token_constant();
  2242. tokenizer->advance();
  2243. }
  2244. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2245. _set_error("Expected ')' in hint.");
  2246. return;
  2247. }
  2248. break;
  2249. }
  2250. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "MULTILINE") {
  2251. current_export.hint = PROPERTY_HINT_MULTILINE_TEXT;
  2252. tokenizer->advance();
  2253. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2254. _set_error("Expected ')' in hint.");
  2255. return;
  2256. }
  2257. break;
  2258. }
  2259. } break;
  2260. case Variant::COLOR: {
  2261. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2262. current_export = PropertyInfo();
  2263. _set_error("Color type hint expects RGB or RGBA as hints");
  2264. return;
  2265. }
  2266. String identifier = tokenizer->get_token_identifier();
  2267. if (identifier == "RGB") {
  2268. current_export.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  2269. } else if (identifier == "RGBA") {
  2270. //none
  2271. } else {
  2272. current_export = PropertyInfo();
  2273. _set_error("Color type hint expects RGB or RGBA as hints");
  2274. return;
  2275. }
  2276. tokenizer->advance();
  2277. } break;
  2278. default: {
  2279. current_export = PropertyInfo();
  2280. _set_error("Type '" + Variant::get_type_name(type) + "' can't take hints.");
  2281. return;
  2282. } break;
  2283. }
  2284. }
  2285. } else if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER) {
  2286. String identifier = tokenizer->get_token_identifier();
  2287. if (!ObjectTypeDB::is_type(identifier, "Resource")) {
  2288. current_export = PropertyInfo();
  2289. _set_error("Export hint not a type or resource.");
  2290. }
  2291. current_export.type = Variant::OBJECT;
  2292. current_export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  2293. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  2294. current_export.hint_string = identifier;
  2295. tokenizer->advance();
  2296. }
  2297. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2298. current_export = PropertyInfo();
  2299. _set_error("Expected ')' or ',' after export hint.");
  2300. return;
  2301. }
  2302. tokenizer->advance();
  2303. }
  2304. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR) {
  2305. current_export = PropertyInfo();
  2306. _set_error("Expected 'var'.");
  2307. return;
  2308. }
  2309. }; //fallthrough to var
  2310. case GDTokenizer::TK_PR_ONREADY: {
  2311. if (token == GDTokenizer::TK_PR_ONREADY) {
  2312. //may be fallthrough from export, ignore if so
  2313. tokenizer->advance();
  2314. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR) {
  2315. _set_error("Expected 'var'.");
  2316. return;
  2317. }
  2318. }
  2319. }; //fallthrough to var
  2320. case GDTokenizer::TK_PR_VAR: {
  2321. //variale declaration and (eventual) initialization
  2322. ClassNode::Member member;
  2323. bool autoexport = tokenizer->get_token(-1) == GDTokenizer::TK_PR_EXPORT;
  2324. if (current_export.type != Variant::NIL) {
  2325. member._export = current_export;
  2326. current_export = PropertyInfo();
  2327. }
  2328. bool onready = tokenizer->get_token(-1) == GDTokenizer::TK_PR_ONREADY;
  2329. tokenizer->advance();
  2330. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2331. _set_error("Expected identifier for member variable name.");
  2332. return;
  2333. }
  2334. member.identifier = tokenizer->get_token_identifier();
  2335. member.expression = NULL;
  2336. member._export.name = member.identifier;
  2337. member.line = tokenizer->get_token_line();
  2338. tokenizer->advance();
  2339. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  2340. #ifdef DEBUG_ENABLED
  2341. int line = tokenizer->get_token_line();
  2342. #endif
  2343. tokenizer->advance();
  2344. Node *subexpr = NULL;
  2345. subexpr = _parse_and_reduce_expression(p_class, false, autoexport);
  2346. if (!subexpr) {
  2347. if (_recover_from_completion()) {
  2348. break;
  2349. }
  2350. return;
  2351. }
  2352. //discourage common error
  2353. if (!onready && subexpr->type == Node::TYPE_OPERATOR) {
  2354. OperatorNode *op = static_cast<OperatorNode *>(subexpr);
  2355. if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_SELF && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  2356. IdentifierNode *id = static_cast<IdentifierNode *>(op->arguments[1]);
  2357. if (id->name == "get_node") {
  2358. _set_error("Use 'onready var " + String(member.identifier) + " = get_node(..)' instead");
  2359. return;
  2360. }
  2361. }
  2362. }
  2363. member.expression = subexpr;
  2364. if (autoexport) {
  2365. if (1) /*(subexpr->type==Node::TYPE_ARRAY) {
  2366. member._export.type=Variant::ARRAY;
  2367. } else if (subexpr->type==Node::TYPE_DICTIONARY) {
  2368. member._export.type=Variant::DICTIONARY;
  2369. } else*/ {
  2370. if (subexpr->type != Node::TYPE_CONSTANT) {
  2371. _set_error("Type-less export needs a constant expression assigned to infer type.");
  2372. return;
  2373. }
  2374. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  2375. if (cn->value.get_type() == Variant::NIL) {
  2376. _set_error("Can't accept a null constant expression for infering export type.");
  2377. return;
  2378. }
  2379. member._export.type = cn->value.get_type();
  2380. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  2381. if (cn->value.get_type() == Variant::OBJECT) {
  2382. Object *obj = cn->value;
  2383. Resource *res = obj->cast_to<Resource>();
  2384. if (res == NULL) {
  2385. _set_error("Exported constant not a type or resource.");
  2386. return;
  2387. }
  2388. member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  2389. member._export.hint_string = res->get_type();
  2390. }
  2391. }
  2392. }
  2393. #ifdef TOOLS_ENABLED
  2394. if (subexpr->type == Node::TYPE_CONSTANT && member._export.type != Variant::NIL) {
  2395. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  2396. if (cn->value.get_type() != Variant::NIL) {
  2397. member.default_value = cn->value;
  2398. }
  2399. }
  2400. #endif
  2401. IdentifierNode *id = alloc_node<IdentifierNode>();
  2402. id->name = member.identifier;
  2403. OperatorNode *op = alloc_node<OperatorNode>();
  2404. op->op = OperatorNode::OP_INIT_ASSIGN;
  2405. op->arguments.push_back(id);
  2406. op->arguments.push_back(subexpr);
  2407. #ifdef DEBUG_ENABLED
  2408. NewLineNode *nl = alloc_node<NewLineNode>();
  2409. nl->line = line;
  2410. if (onready)
  2411. p_class->ready->statements.push_back(nl);
  2412. else
  2413. p_class->initializer->statements.push_back(nl);
  2414. #endif
  2415. if (onready)
  2416. p_class->ready->statements.push_back(op);
  2417. else
  2418. p_class->initializer->statements.push_back(op);
  2419. } else {
  2420. if (autoexport) {
  2421. _set_error("Type-less export needs a constant expression assigned to infer type.");
  2422. return;
  2423. }
  2424. }
  2425. if (tokenizer->get_token() == GDTokenizer::TK_PR_SETGET) {
  2426. tokenizer->advance();
  2427. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2428. //just comma means using only getter
  2429. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2430. _set_error("Expected identifier for setter function after 'notify'.");
  2431. }
  2432. member.setter = tokenizer->get_token_identifier();
  2433. tokenizer->advance();
  2434. }
  2435. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2436. //there is a getter
  2437. tokenizer->advance();
  2438. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2439. _set_error("Expected identifier for getter function after ','.");
  2440. }
  2441. member.getter = tokenizer->get_token_identifier();
  2442. tokenizer->advance();
  2443. }
  2444. }
  2445. p_class->variables.push_back(member);
  2446. if (!_end_statement()) {
  2447. _set_error("Expected end of statement (continue)");
  2448. return;
  2449. }
  2450. } break;
  2451. case GDTokenizer::TK_PR_CONST: {
  2452. //variale declaration and (eventual) initialization
  2453. ClassNode::Constant constant;
  2454. tokenizer->advance();
  2455. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2456. _set_error("Expected name (identifier) for constant.");
  2457. return;
  2458. }
  2459. constant.identifier = tokenizer->get_token_identifier();
  2460. tokenizer->advance();
  2461. if (tokenizer->get_token() != GDTokenizer::TK_OP_ASSIGN) {
  2462. _set_error("Constant expects assignment.");
  2463. return;
  2464. }
  2465. tokenizer->advance();
  2466. Node *subexpr = NULL;
  2467. subexpr = _parse_and_reduce_expression(p_class, true, true);
  2468. if (!subexpr) {
  2469. if (_recover_from_completion()) {
  2470. break;
  2471. }
  2472. return;
  2473. }
  2474. if (subexpr->type != Node::TYPE_CONSTANT) {
  2475. _set_error("Expected constant expression");
  2476. }
  2477. constant.expression = subexpr;
  2478. p_class->constant_expressions.push_back(constant);
  2479. if (!_end_statement()) {
  2480. _set_error("Expected end of statement (constant)");
  2481. return;
  2482. }
  2483. } break;
  2484. case GDTokenizer::TK_PR_ENUM: {
  2485. //mutiple constant declarations..
  2486. int last_assign = -1; // Incremented by 1 right before the assingment.
  2487. String enum_name;
  2488. Dictionary enum_dict;
  2489. tokenizer->advance();
  2490. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER) {
  2491. enum_name = tokenizer->get_token_identifier();
  2492. tokenizer->advance();
  2493. }
  2494. if (tokenizer->get_token() != GDTokenizer::TK_CURLY_BRACKET_OPEN) {
  2495. _set_error("Expected '{' in enum declaration");
  2496. return;
  2497. }
  2498. tokenizer->advance();
  2499. while (true) {
  2500. if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  2501. tokenizer->advance(); // Ignore newlines
  2502. } else if (tokenizer->get_token() == GDTokenizer::TK_CURLY_BRACKET_CLOSE) {
  2503. tokenizer->advance();
  2504. break; // End of enum
  2505. } else if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2506. if (tokenizer->get_token() == GDTokenizer::TK_EOF) {
  2507. _set_error("Unexpected end of file.");
  2508. } else {
  2509. _set_error(String("Unexpected ") + GDTokenizer::get_token_name(tokenizer->get_token()) + ", expected identifier");
  2510. }
  2511. return;
  2512. } else { // tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER
  2513. ClassNode::Constant constant;
  2514. constant.identifier = tokenizer->get_token_identifier();
  2515. tokenizer->advance();
  2516. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  2517. tokenizer->advance();
  2518. Node *subexpr = NULL;
  2519. subexpr = _parse_and_reduce_expression(p_class, true, true);
  2520. if (!subexpr) {
  2521. if (_recover_from_completion()) {
  2522. break;
  2523. }
  2524. return;
  2525. }
  2526. if (subexpr->type != Node::TYPE_CONSTANT) {
  2527. _set_error("Expected constant expression");
  2528. }
  2529. const ConstantNode *subexpr_const = static_cast<const ConstantNode *>(subexpr);
  2530. if (subexpr_const->value.get_type() != Variant::INT) {
  2531. _set_error("Expected an int value for enum");
  2532. }
  2533. last_assign = subexpr_const->value;
  2534. constant.expression = subexpr;
  2535. } else {
  2536. last_assign = last_assign + 1;
  2537. ConstantNode *cn = alloc_node<ConstantNode>();
  2538. cn->value = last_assign;
  2539. constant.expression = cn;
  2540. }
  2541. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2542. tokenizer->advance();
  2543. }
  2544. if (enum_name != "") {
  2545. const ConstantNode *cn = static_cast<const ConstantNode *>(constant.expression);
  2546. enum_dict[constant.identifier] = cn->value;
  2547. }
  2548. p_class->constant_expressions.push_back(constant);
  2549. }
  2550. }
  2551. if (enum_name != "") {
  2552. ClassNode::Constant enum_constant;
  2553. enum_constant.identifier = enum_name;
  2554. ConstantNode *cn = alloc_node<ConstantNode>();
  2555. cn->value = enum_dict;
  2556. enum_constant.expression = cn;
  2557. p_class->constant_expressions.push_back(enum_constant);
  2558. }
  2559. if (!_end_statement()) {
  2560. _set_error("Expected end of statement (enum)");
  2561. return;
  2562. }
  2563. } break;
  2564. default: {
  2565. _set_error(String() + "Unexpected token: " + tokenizer->get_token_name(tokenizer->get_token()) + ":" + tokenizer->get_token_identifier());
  2566. return;
  2567. } break;
  2568. }
  2569. }
  2570. }
  2571. void GDParser::_set_error(const String &p_error, int p_line, int p_column) {
  2572. if (error_set)
  2573. return; //allow no further errors
  2574. error = p_error;
  2575. error_line = p_line < 0 ? tokenizer->get_token_line() : p_line;
  2576. error_column = p_column < 0 ? tokenizer->get_token_column() : p_column;
  2577. error_set = true;
  2578. }
  2579. String GDParser::get_error() const {
  2580. return error;
  2581. }
  2582. int GDParser::get_error_line() const {
  2583. return error_line;
  2584. }
  2585. int GDParser::get_error_column() const {
  2586. return error_column;
  2587. }
  2588. Error GDParser::_parse(const String &p_base_path) {
  2589. base_path = p_base_path;
  2590. clear();
  2591. //assume class
  2592. ClassNode *main_class = alloc_node<ClassNode>();
  2593. main_class->initializer = alloc_node<BlockNode>();
  2594. main_class->initializer->parent_class = main_class;
  2595. main_class->ready = alloc_node<BlockNode>();
  2596. main_class->ready->parent_class = main_class;
  2597. current_class = main_class;
  2598. _parse_class(main_class);
  2599. if (tokenizer->get_token() == GDTokenizer::TK_ERROR) {
  2600. error_set = false;
  2601. _set_error("Parse Error: " + tokenizer->get_token_error());
  2602. }
  2603. if (error_set) {
  2604. return ERR_PARSE_ERROR;
  2605. }
  2606. return OK;
  2607. }
  2608. Error GDParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path, const String &p_self_path) {
  2609. for_completion = false;
  2610. validating = false;
  2611. completion_type = COMPLETION_NONE;
  2612. completion_node = NULL;
  2613. completion_class = NULL;
  2614. completion_function = NULL;
  2615. completion_block = NULL;
  2616. completion_found = false;
  2617. current_block = NULL;
  2618. current_class = NULL;
  2619. current_function = NULL;
  2620. self_path = p_self_path;
  2621. GDTokenizerBuffer *tb = memnew(GDTokenizerBuffer);
  2622. tb->set_code_buffer(p_bytecode);
  2623. tokenizer = tb;
  2624. Error ret = _parse(p_base_path);
  2625. memdelete(tb);
  2626. tokenizer = NULL;
  2627. return ret;
  2628. }
  2629. 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) {
  2630. completion_type = COMPLETION_NONE;
  2631. completion_node = NULL;
  2632. completion_class = NULL;
  2633. completion_function = NULL;
  2634. completion_block = NULL;
  2635. completion_found = false;
  2636. current_block = NULL;
  2637. current_class = NULL;
  2638. current_function = NULL;
  2639. self_path = p_self_path;
  2640. GDTokenizerText *tt = memnew(GDTokenizerText);
  2641. tt->set_code(p_code);
  2642. validating = p_just_validate;
  2643. for_completion = p_for_completion;
  2644. tokenizer = tt;
  2645. Error ret = _parse(p_base_path);
  2646. memdelete(tt);
  2647. tokenizer = NULL;
  2648. return ret;
  2649. }
  2650. bool GDParser::is_tool_script() const {
  2651. return (head && head->type == Node::TYPE_CLASS && static_cast<const ClassNode *>(head)->tool);
  2652. }
  2653. const GDParser::Node *GDParser::get_parse_tree() const {
  2654. return head;
  2655. }
  2656. void GDParser::clear() {
  2657. while (list) {
  2658. Node *l = list;
  2659. list = list->next;
  2660. memdelete(l);
  2661. }
  2662. head = NULL;
  2663. list = NULL;
  2664. completion_type = COMPLETION_NONE;
  2665. completion_node = NULL;
  2666. completion_class = NULL;
  2667. completion_function = NULL;
  2668. completion_block = NULL;
  2669. current_block = NULL;
  2670. current_class = NULL;
  2671. completion_found = false;
  2672. current_function = NULL;
  2673. validating = false;
  2674. for_completion = false;
  2675. error_set = false;
  2676. tab_level.clear();
  2677. tab_level.push_back(0);
  2678. error_line = 0;
  2679. error_column = 0;
  2680. pending_newline = -1;
  2681. parenthesis = 0;
  2682. current_export.type = Variant::NIL;
  2683. error = "";
  2684. }
  2685. GDParser::CompletionType GDParser::get_completion_type() {
  2686. return completion_type;
  2687. }
  2688. StringName GDParser::get_completion_cursor() {
  2689. return completion_cursor;
  2690. }
  2691. int GDParser::get_completion_line() {
  2692. return completion_line;
  2693. }
  2694. Variant::Type GDParser::get_completion_built_in_constant() {
  2695. return completion_built_in_constant;
  2696. }
  2697. GDParser::Node *GDParser::get_completion_node() {
  2698. return completion_node;
  2699. }
  2700. GDParser::BlockNode *GDParser::get_completion_block() {
  2701. return completion_block;
  2702. }
  2703. GDParser::ClassNode *GDParser::get_completion_class() {
  2704. return completion_class;
  2705. }
  2706. GDParser::FunctionNode *GDParser::get_completion_function() {
  2707. return completion_function;
  2708. }
  2709. int GDParser::get_completion_argument_index() {
  2710. return completion_argument;
  2711. }
  2712. GDParser::GDParser() {
  2713. head = NULL;
  2714. list = NULL;
  2715. tokenizer = NULL;
  2716. pending_newline = -1;
  2717. clear();
  2718. }
  2719. GDParser::~GDParser() {
  2720. clear();
  2721. }