gd_parser.cpp 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434
  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);
  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->is_token_literal()) {
  151. identifier = tokenizer->get_token_literal();
  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. completion_ident_is_call = false;
  163. tokenizer->advance();
  164. if (tokenizer->is_token_literal()) {
  165. identifier = identifier.operator String() + tokenizer->get_token_literal().operator String();
  166. tokenizer->advance();
  167. }
  168. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_OPEN) {
  169. completion_ident_is_call = true;
  170. }
  171. return true;
  172. }
  173. return false;
  174. }
  175. GDParser::Node *GDParser::_parse_expression(Node *p_parent, bool p_static, bool p_allow_assign, bool p_parsing_constant) {
  176. //Vector<Node*> expressions;
  177. //Vector<OperatorNode::Operator> operators;
  178. Vector<Expression> expression;
  179. Node *expr = NULL;
  180. int op_line = tokenizer->get_token_line(); // when operators are created at the bottom, the line might have been changed (\n found)
  181. while (true) {
  182. /*****************/
  183. /* Parse Operand */
  184. /*****************/
  185. if (parenthesis > 0) {
  186. //remove empty space (only allowed if inside parenthesis
  187. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  188. tokenizer->advance();
  189. }
  190. }
  191. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_OPEN) {
  192. //subexpression ()
  193. tokenizer->advance();
  194. parenthesis++;
  195. Node *subexpr = _parse_expression(p_parent, p_static, p_allow_assign, p_parsing_constant);
  196. parenthesis--;
  197. if (!subexpr)
  198. return NULL;
  199. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  200. _set_error("Expected ')' in expression");
  201. return NULL;
  202. }
  203. tokenizer->advance();
  204. expr = subexpr;
  205. } else if (tokenizer->get_token() == GDTokenizer::TK_DOLLAR) {
  206. tokenizer->advance();
  207. String path;
  208. bool need_identifier = true;
  209. bool done = false;
  210. while (!done) {
  211. switch (tokenizer->get_token()) {
  212. case GDTokenizer::TK_CURSOR: {
  213. completion_cursor = StringName();
  214. completion_type = COMPLETION_GET_NODE;
  215. completion_class = current_class;
  216. completion_function = current_function;
  217. completion_line = tokenizer->get_token_line();
  218. completion_cursor = path;
  219. completion_argument = 0;
  220. completion_block = current_block;
  221. completion_found = true;
  222. tokenizer->advance();
  223. } break;
  224. case GDTokenizer::TK_CONSTANT: {
  225. if (!need_identifier) {
  226. done = true;
  227. break;
  228. }
  229. if (tokenizer->get_token_constant().get_type() != Variant::STRING) {
  230. _set_error("Expected string constant or identifier after '$' or '/'.");
  231. return NULL;
  232. }
  233. path += String(tokenizer->get_token_constant());
  234. tokenizer->advance();
  235. need_identifier = false;
  236. } break;
  237. case GDTokenizer::TK_OP_DIV: {
  238. if (need_identifier) {
  239. done = true;
  240. break;
  241. }
  242. path += "/";
  243. tokenizer->advance();
  244. need_identifier = true;
  245. } break;
  246. default: {
  247. // Instead of checking for TK_IDENTIFIER, we check with is_token_literal, as this allows us to use match/sync/etc. as a name
  248. if (need_identifier && tokenizer->is_token_literal()) {
  249. path += String(tokenizer->get_token_literal());
  250. tokenizer->advance();
  251. need_identifier = false;
  252. } else {
  253. done = true;
  254. }
  255. break;
  256. }
  257. }
  258. }
  259. if (path == "") {
  260. _set_error("Path expected after $.");
  261. return NULL;
  262. }
  263. OperatorNode *op = alloc_node<OperatorNode>();
  264. op->op = OperatorNode::OP_CALL;
  265. op->arguments.push_back(alloc_node<SelfNode>());
  266. IdentifierNode *funcname = alloc_node<IdentifierNode>();
  267. funcname->name = "get_node";
  268. op->arguments.push_back(funcname);
  269. ConstantNode *nodepath = alloc_node<ConstantNode>();
  270. nodepath->value = NodePath(StringName(path));
  271. op->arguments.push_back(nodepath);
  272. expr = op;
  273. } else if (tokenizer->get_token() == GDTokenizer::TK_CURSOR) {
  274. tokenizer->advance();
  275. continue; //no point in cursor in the middle of expression
  276. } else if (tokenizer->get_token() == GDTokenizer::TK_CONSTANT) {
  277. //constant defined by tokenizer
  278. ConstantNode *constant = alloc_node<ConstantNode>();
  279. constant->value = tokenizer->get_token_constant();
  280. tokenizer->advance();
  281. expr = constant;
  282. } else if (tokenizer->get_token() == GDTokenizer::TK_CONST_PI) {
  283. //constant defined by tokenizer
  284. ConstantNode *constant = alloc_node<ConstantNode>();
  285. constant->value = Math_PI;
  286. tokenizer->advance();
  287. expr = constant;
  288. } else if (tokenizer->get_token() == GDTokenizer::TK_CONST_TAU) {
  289. //constant defined by tokenizer
  290. ConstantNode *constant = alloc_node<ConstantNode>();
  291. constant->value = Math_TAU;
  292. tokenizer->advance();
  293. expr = constant;
  294. } else if (tokenizer->get_token() == GDTokenizer::TK_CONST_INF) {
  295. //constant defined by tokenizer
  296. ConstantNode *constant = alloc_node<ConstantNode>();
  297. constant->value = Math_INF;
  298. tokenizer->advance();
  299. expr = constant;
  300. } else if (tokenizer->get_token() == GDTokenizer::TK_CONST_NAN) {
  301. //constant defined by tokenizer
  302. ConstantNode *constant = alloc_node<ConstantNode>();
  303. constant->value = Math_NAN;
  304. tokenizer->advance();
  305. expr = constant;
  306. } else if (tokenizer->get_token() == GDTokenizer::TK_PR_PRELOAD) {
  307. //constant defined by tokenizer
  308. tokenizer->advance();
  309. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_OPEN) {
  310. _set_error("Expected '(' after 'preload'");
  311. return NULL;
  312. }
  313. tokenizer->advance();
  314. if (tokenizer->get_token() == GDTokenizer::TK_CURSOR) {
  315. completion_cursor = StringName();
  316. completion_node = p_parent;
  317. completion_type = COMPLETION_RESOURCE_PATH;
  318. completion_class = current_class;
  319. completion_function = current_function;
  320. completion_line = tokenizer->get_token_line();
  321. completion_block = current_block;
  322. completion_argument = 0;
  323. completion_found = true;
  324. tokenizer->advance();
  325. }
  326. String path;
  327. bool found_constant = false;
  328. bool valid = false;
  329. ConstantNode *cn;
  330. Node *subexpr = _parse_and_reduce_expression(p_parent, p_static);
  331. if (subexpr) {
  332. if (subexpr->type == Node::TYPE_CONSTANT) {
  333. cn = static_cast<ConstantNode *>(subexpr);
  334. found_constant = true;
  335. }
  336. if (subexpr->type == Node::TYPE_IDENTIFIER) {
  337. IdentifierNode *in = static_cast<IdentifierNode *>(subexpr);
  338. Vector<ClassNode::Constant> ce = current_class->constant_expressions;
  339. // Try to find the constant expression by the identifier
  340. for (int i = 0; i < ce.size(); ++i) {
  341. if (ce[i].identifier == in->name) {
  342. if (ce[i].expression->type == Node::TYPE_CONSTANT) {
  343. cn = static_cast<ConstantNode *>(ce[i].expression);
  344. found_constant = true;
  345. }
  346. }
  347. }
  348. }
  349. if (found_constant && cn->value.get_type() == Variant::STRING) {
  350. valid = true;
  351. path = (String)cn->value;
  352. }
  353. }
  354. if (!valid) {
  355. _set_error("expected string constant as 'preload' argument.");
  356. return NULL;
  357. }
  358. if (!path.is_abs_path() && base_path != "")
  359. path = base_path + "/" + path;
  360. path = path.replace("///", "//").simplify_path();
  361. if (path == self_path) {
  362. _set_error("Can't preload itself (use 'get_script()').");
  363. return NULL;
  364. }
  365. Ref<Resource> res;
  366. if (!validating) {
  367. //this can be too slow for just validating code
  368. if (for_completion && ScriptCodeCompletionCache::get_sigleton()) {
  369. res = ScriptCodeCompletionCache::get_sigleton()->get_cached_resource(path);
  370. } else {
  371. res = ResourceLoader::load(path);
  372. }
  373. if (!res.is_valid()) {
  374. _set_error("Can't preload resource at path: " + path);
  375. return NULL;
  376. }
  377. } else {
  378. if (!FileAccess::exists(path)) {
  379. _set_error("Can't preload resource at path: " + path);
  380. return NULL;
  381. }
  382. }
  383. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  384. _set_error("Expected ')' after 'preload' path");
  385. return NULL;
  386. }
  387. tokenizer->advance();
  388. ConstantNode *constant = alloc_node<ConstantNode>();
  389. constant->value = res;
  390. expr = constant;
  391. } else if (tokenizer->get_token() == GDTokenizer::TK_PR_YIELD) {
  392. //constant defined by tokenizer
  393. tokenizer->advance();
  394. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_OPEN) {
  395. _set_error("Expected '(' after 'yield'");
  396. return NULL;
  397. }
  398. tokenizer->advance();
  399. OperatorNode *yield = alloc_node<OperatorNode>();
  400. yield->op = OperatorNode::OP_YIELD;
  401. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  402. tokenizer->advance();
  403. }
  404. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE) {
  405. expr = yield;
  406. tokenizer->advance();
  407. } else {
  408. parenthesis++;
  409. Node *object = _parse_and_reduce_expression(p_parent, p_static);
  410. if (!object)
  411. return NULL;
  412. yield->arguments.push_back(object);
  413. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  414. _set_error("Expected ',' after first argument of 'yield'");
  415. return NULL;
  416. }
  417. tokenizer->advance();
  418. if (tokenizer->get_token() == GDTokenizer::TK_CURSOR) {
  419. completion_cursor = StringName();
  420. completion_node = object;
  421. completion_type = COMPLETION_YIELD;
  422. completion_class = current_class;
  423. completion_function = current_function;
  424. completion_line = tokenizer->get_token_line();
  425. completion_argument = 0;
  426. completion_block = current_block;
  427. completion_found = true;
  428. tokenizer->advance();
  429. }
  430. Node *signal = _parse_and_reduce_expression(p_parent, p_static);
  431. if (!signal)
  432. return NULL;
  433. yield->arguments.push_back(signal);
  434. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  435. _set_error("Expected ')' after second argument of 'yield'");
  436. return NULL;
  437. }
  438. parenthesis--;
  439. tokenizer->advance();
  440. expr = yield;
  441. }
  442. } else if (tokenizer->get_token() == GDTokenizer::TK_SELF) {
  443. if (p_static) {
  444. _set_error("'self'' not allowed in static function or constant expression");
  445. return NULL;
  446. }
  447. //constant defined by tokenizer
  448. SelfNode *self = alloc_node<SelfNode>();
  449. tokenizer->advance();
  450. expr = self;
  451. } else if (tokenizer->get_token() == GDTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token(1) == GDTokenizer::TK_PERIOD) {
  452. Variant::Type bi_type = tokenizer->get_token_type();
  453. tokenizer->advance(2);
  454. StringName identifier;
  455. if (_get_completable_identifier(COMPLETION_BUILT_IN_TYPE_CONSTANT, identifier)) {
  456. completion_built_in_constant = bi_type;
  457. }
  458. if (identifier == StringName()) {
  459. _set_error("Built-in type constant expected after '.'");
  460. return NULL;
  461. }
  462. if (!Variant::has_numeric_constant(bi_type, identifier)) {
  463. _set_error("Static constant '" + identifier.operator String() + "' not present in built-in type " + Variant::get_type_name(bi_type) + ".");
  464. return NULL;
  465. }
  466. ConstantNode *cn = alloc_node<ConstantNode>();
  467. cn->value = Variant::get_numeric_constant_value(bi_type, identifier);
  468. expr = cn;
  469. } else if (tokenizer->get_token(1) == GDTokenizer::TK_PARENTHESIS_OPEN && tokenizer->is_token_literal()) {
  470. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  471. //function or constructor
  472. OperatorNode *op = alloc_node<OperatorNode>();
  473. op->op = OperatorNode::OP_CALL;
  474. if (tokenizer->get_token() == GDTokenizer::TK_BUILT_IN_TYPE) {
  475. TypeNode *tn = alloc_node<TypeNode>();
  476. tn->vtype = tokenizer->get_token_type();
  477. op->arguments.push_back(tn);
  478. tokenizer->advance(2);
  479. } else if (tokenizer->get_token() == GDTokenizer::TK_BUILT_IN_FUNC) {
  480. BuiltInFunctionNode *bn = alloc_node<BuiltInFunctionNode>();
  481. bn->function = tokenizer->get_token_built_in_func();
  482. op->arguments.push_back(bn);
  483. tokenizer->advance(2);
  484. } else {
  485. SelfNode *self = alloc_node<SelfNode>();
  486. op->arguments.push_back(self);
  487. StringName identifier;
  488. if (_get_completable_identifier(COMPLETION_FUNCTION, identifier)) {
  489. }
  490. IdentifierNode *id = alloc_node<IdentifierNode>();
  491. id->name = identifier;
  492. op->arguments.push_back(id);
  493. tokenizer->advance(1);
  494. }
  495. if (tokenizer->get_token() == GDTokenizer::TK_CURSOR) {
  496. _make_completable_call(0);
  497. completion_node = op;
  498. }
  499. if (!_parse_arguments(op, op->arguments, p_static, true))
  500. return NULL;
  501. expr = op;
  502. } else if (tokenizer->is_token_literal(0, true)) {
  503. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  504. //identifier (reference)
  505. const ClassNode *cln = current_class;
  506. bool bfn = false;
  507. StringName identifier;
  508. if (_get_completable_identifier(COMPLETION_IDENTIFIER, identifier)) {
  509. }
  510. if (p_parsing_constant) {
  511. for (int i = 0; i < cln->constant_expressions.size(); ++i) {
  512. if (cln->constant_expressions[i].identifier == identifier) {
  513. expr = cln->constant_expressions[i].expression;
  514. bfn = true;
  515. break;
  516. }
  517. }
  518. if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) {
  519. //check from constants
  520. ConstantNode *constant = alloc_node<ConstantNode>();
  521. constant->value = GDScriptLanguage::get_singleton()->get_global_array()[GDScriptLanguage::get_singleton()->get_global_map()[identifier]];
  522. expr = constant;
  523. bfn = true;
  524. }
  525. }
  526. if (!bfn) {
  527. IdentifierNode *id = alloc_node<IdentifierNode>();
  528. id->name = identifier;
  529. expr = id;
  530. }
  531. } 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) {
  532. //single prefix operators like !expr +expr -expr ++expr --expr
  533. alloc_node<OperatorNode>();
  534. Expression e;
  535. e.is_op = true;
  536. switch (tokenizer->get_token()) {
  537. case GDTokenizer::TK_OP_ADD: e.op = OperatorNode::OP_POS; break;
  538. case GDTokenizer::TK_OP_SUB: e.op = OperatorNode::OP_NEG; break;
  539. case GDTokenizer::TK_OP_NOT: e.op = OperatorNode::OP_NOT; break;
  540. case GDTokenizer::TK_OP_BIT_INVERT: e.op = OperatorNode::OP_BIT_INVERT; break;
  541. default: {}
  542. }
  543. tokenizer->advance();
  544. if (e.op != OperatorNode::OP_NOT && tokenizer->get_token() == GDTokenizer::TK_OP_NOT) {
  545. _set_error("Misplaced 'not'.");
  546. return NULL;
  547. }
  548. expression.push_back(e);
  549. continue; //only exception, must continue...
  550. /*
  551. Node *subexpr=_parse_expression(op,p_static);
  552. if (!subexpr)
  553. return NULL;
  554. op->arguments.push_back(subexpr);
  555. expr=op;*/
  556. } else if (tokenizer->get_token() == GDTokenizer::TK_BRACKET_OPEN) {
  557. // array
  558. tokenizer->advance();
  559. ArrayNode *arr = alloc_node<ArrayNode>();
  560. bool expecting_comma = false;
  561. while (true) {
  562. if (tokenizer->get_token() == GDTokenizer::TK_EOF) {
  563. _set_error("Unterminated array");
  564. return NULL;
  565. } else if (tokenizer->get_token() == GDTokenizer::TK_BRACKET_CLOSE) {
  566. tokenizer->advance();
  567. break;
  568. } else if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  569. tokenizer->advance(); //ignore newline
  570. } else if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  571. if (!expecting_comma) {
  572. _set_error("expression or ']' expected");
  573. return NULL;
  574. }
  575. expecting_comma = false;
  576. tokenizer->advance(); //ignore newline
  577. } else {
  578. //parse expression
  579. if (expecting_comma) {
  580. _set_error("',' or ']' expected");
  581. return NULL;
  582. }
  583. Node *n = _parse_expression(arr, p_static, p_allow_assign, p_parsing_constant);
  584. if (!n)
  585. return NULL;
  586. arr->elements.push_back(n);
  587. expecting_comma = true;
  588. }
  589. }
  590. expr = arr;
  591. } else if (tokenizer->get_token() == GDTokenizer::TK_CURLY_BRACKET_OPEN) {
  592. // array
  593. tokenizer->advance();
  594. DictionaryNode *dict = alloc_node<DictionaryNode>();
  595. enum DictExpect {
  596. DICT_EXPECT_KEY,
  597. DICT_EXPECT_COLON,
  598. DICT_EXPECT_VALUE,
  599. DICT_EXPECT_COMMA
  600. };
  601. Node *key = NULL;
  602. Set<Variant> keys;
  603. DictExpect expecting = DICT_EXPECT_KEY;
  604. while (true) {
  605. if (tokenizer->get_token() == GDTokenizer::TK_EOF) {
  606. _set_error("Unterminated dictionary");
  607. return NULL;
  608. } else if (tokenizer->get_token() == GDTokenizer::TK_CURLY_BRACKET_CLOSE) {
  609. if (expecting == DICT_EXPECT_COLON) {
  610. _set_error("':' expected");
  611. return NULL;
  612. }
  613. if (expecting == DICT_EXPECT_VALUE) {
  614. _set_error("value expected");
  615. return NULL;
  616. }
  617. tokenizer->advance();
  618. break;
  619. } else if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  620. tokenizer->advance(); //ignore newline
  621. } else if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  622. if (expecting == DICT_EXPECT_KEY) {
  623. _set_error("key or '}' expected");
  624. return NULL;
  625. }
  626. if (expecting == DICT_EXPECT_VALUE) {
  627. _set_error("value expected");
  628. return NULL;
  629. }
  630. if (expecting == DICT_EXPECT_COLON) {
  631. _set_error("':' expected");
  632. return NULL;
  633. }
  634. expecting = DICT_EXPECT_KEY;
  635. tokenizer->advance(); //ignore newline
  636. } else if (tokenizer->get_token() == GDTokenizer::TK_COLON) {
  637. if (expecting == DICT_EXPECT_KEY) {
  638. _set_error("key or '}' expected");
  639. return NULL;
  640. }
  641. if (expecting == DICT_EXPECT_VALUE) {
  642. _set_error("value expected");
  643. return NULL;
  644. }
  645. if (expecting == DICT_EXPECT_COMMA) {
  646. _set_error("',' or '}' expected");
  647. return NULL;
  648. }
  649. expecting = DICT_EXPECT_VALUE;
  650. tokenizer->advance(); //ignore newline
  651. } else {
  652. if (expecting == DICT_EXPECT_COMMA) {
  653. _set_error("',' or '}' expected");
  654. return NULL;
  655. }
  656. if (expecting == DICT_EXPECT_COLON) {
  657. _set_error("':' expected");
  658. return NULL;
  659. }
  660. if (expecting == DICT_EXPECT_KEY) {
  661. if (tokenizer->is_token_literal() && tokenizer->get_token(1) == GDTokenizer::TK_OP_ASSIGN) {
  662. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  663. //lua style identifier, easier to write
  664. ConstantNode *cn = alloc_node<ConstantNode>();
  665. cn->value = tokenizer->get_token_literal();
  666. key = cn;
  667. tokenizer->advance(2);
  668. expecting = DICT_EXPECT_VALUE;
  669. } else {
  670. //python/js style more flexible
  671. key = _parse_expression(dict, p_static, p_allow_assign, p_parsing_constant);
  672. if (!key)
  673. return NULL;
  674. expecting = DICT_EXPECT_COLON;
  675. }
  676. }
  677. if (expecting == DICT_EXPECT_VALUE) {
  678. Node *value = _parse_expression(dict, p_static, p_allow_assign, p_parsing_constant);
  679. if (!value)
  680. return NULL;
  681. expecting = DICT_EXPECT_COMMA;
  682. if (key->type == GDParser::Node::TYPE_CONSTANT) {
  683. Variant const &keyName = static_cast<const GDParser::ConstantNode *>(key)->value;
  684. if (keys.has(keyName)) {
  685. _set_error("Duplicate key found in Dictionary literal");
  686. return NULL;
  687. }
  688. keys.insert(keyName);
  689. }
  690. DictionaryNode::Pair pair;
  691. pair.key = key;
  692. pair.value = value;
  693. dict->elements.push_back(pair);
  694. key = NULL;
  695. }
  696. }
  697. }
  698. expr = dict;
  699. } else if (tokenizer->get_token() == GDTokenizer::TK_PERIOD && (tokenizer->is_token_literal(1) || tokenizer->get_token(1) == GDTokenizer::TK_CURSOR) && tokenizer->get_token(2) == GDTokenizer::TK_PARENTHESIS_OPEN) {
  700. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  701. // parent call
  702. tokenizer->advance(); //goto identifier
  703. OperatorNode *op = alloc_node<OperatorNode>();
  704. op->op = OperatorNode::OP_PARENT_CALL;
  705. /*SelfNode *self = alloc_node<SelfNode>();
  706. op->arguments.push_back(self);
  707. forbidden for now */
  708. StringName identifier;
  709. if (_get_completable_identifier(COMPLETION_PARENT_FUNCTION, identifier)) {
  710. //indexing stuff
  711. }
  712. IdentifierNode *id = alloc_node<IdentifierNode>();
  713. id->name = identifier;
  714. op->arguments.push_back(id);
  715. tokenizer->advance(1);
  716. if (!_parse_arguments(op, op->arguments, p_static))
  717. return NULL;
  718. expr = op;
  719. } else {
  720. //find list [ or find dictionary {
  721. //print_line("found bug?");
  722. _set_error("Error parsing expression, misplaced: " + String(tokenizer->get_token_name(tokenizer->get_token())));
  723. return NULL; //nothing
  724. }
  725. if (!expr) {
  726. ERR_EXPLAIN("GDParser bug, couldn't figure out what expression is..");
  727. ERR_FAIL_COND_V(!expr, NULL);
  728. }
  729. /******************/
  730. /* Parse Indexing */
  731. /******************/
  732. while (true) {
  733. //expressions can be indexed any number of times
  734. if (tokenizer->get_token() == GDTokenizer::TK_PERIOD) {
  735. //indexing using "."
  736. if (tokenizer->get_token(1) != GDTokenizer::TK_CURSOR && !tokenizer->is_token_literal(1)) {
  737. // We check with is_token_literal, as this allows us to use match/sync/etc. as a name
  738. _set_error("Expected identifier as member");
  739. return NULL;
  740. } else if (tokenizer->get_token(2) == GDTokenizer::TK_PARENTHESIS_OPEN) {
  741. //call!!
  742. OperatorNode *op = alloc_node<OperatorNode>();
  743. op->op = OperatorNode::OP_CALL;
  744. tokenizer->advance();
  745. IdentifierNode *id = alloc_node<IdentifierNode>();
  746. if (tokenizer->get_token() == GDTokenizer::TK_BUILT_IN_FUNC) {
  747. //small hack so built in funcs don't obfuscate methods
  748. id->name = GDFunctions::get_func_name(tokenizer->get_token_built_in_func());
  749. tokenizer->advance();
  750. } else {
  751. StringName identifier;
  752. if (_get_completable_identifier(COMPLETION_METHOD, identifier)) {
  753. completion_node = op;
  754. //indexing stuff
  755. }
  756. id->name = identifier;
  757. }
  758. op->arguments.push_back(expr); // call what
  759. op->arguments.push_back(id); // call func
  760. //get arguments
  761. tokenizer->advance(1);
  762. if (tokenizer->get_token() == GDTokenizer::TK_CURSOR) {
  763. _make_completable_call(0);
  764. completion_node = op;
  765. }
  766. if (!_parse_arguments(op, op->arguments, p_static, true))
  767. return NULL;
  768. expr = op;
  769. } else {
  770. //simple indexing!
  771. OperatorNode *op = alloc_node<OperatorNode>();
  772. op->op = OperatorNode::OP_INDEX_NAMED;
  773. tokenizer->advance();
  774. StringName identifier;
  775. if (_get_completable_identifier(COMPLETION_INDEX, identifier)) {
  776. if (identifier == StringName()) {
  777. identifier = "@temp"; //so it parses allright
  778. }
  779. completion_node = op;
  780. //indexing stuff
  781. }
  782. IdentifierNode *id = alloc_node<IdentifierNode>();
  783. id->name = identifier;
  784. op->arguments.push_back(expr);
  785. op->arguments.push_back(id);
  786. expr = op;
  787. }
  788. } else if (tokenizer->get_token() == GDTokenizer::TK_BRACKET_OPEN) {
  789. //indexing using "[]"
  790. OperatorNode *op = alloc_node<OperatorNode>();
  791. op->op = OperatorNode::OP_INDEX;
  792. tokenizer->advance(1);
  793. Node *subexpr = _parse_expression(op, p_static, p_allow_assign, p_parsing_constant);
  794. if (!subexpr) {
  795. return NULL;
  796. }
  797. if (tokenizer->get_token() != GDTokenizer::TK_BRACKET_CLOSE) {
  798. _set_error("Expected ']'");
  799. return NULL;
  800. }
  801. op->arguments.push_back(expr);
  802. op->arguments.push_back(subexpr);
  803. tokenizer->advance(1);
  804. expr = op;
  805. } else
  806. break;
  807. }
  808. /******************/
  809. /* Parse Operator */
  810. /******************/
  811. if (parenthesis > 0) {
  812. //remove empty space (only allowed if inside parenthesis
  813. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  814. tokenizer->advance();
  815. }
  816. }
  817. Expression e;
  818. e.is_op = false;
  819. e.node = expr;
  820. expression.push_back(e);
  821. // determine which operator is next
  822. OperatorNode::Operator op;
  823. bool valid = true;
  824. //assign, if allowed is only allowed on the first operator
  825. #define _VALIDATE_ASSIGN \
  826. if (!p_allow_assign) { \
  827. _set_error("Unexpected assign."); \
  828. return NULL; \
  829. } \
  830. p_allow_assign = false;
  831. switch (tokenizer->get_token()) { //see operator
  832. case GDTokenizer::TK_OP_IN: op = OperatorNode::OP_IN; break;
  833. case GDTokenizer::TK_OP_EQUAL: op = OperatorNode::OP_EQUAL; break;
  834. case GDTokenizer::TK_OP_NOT_EQUAL: op = OperatorNode::OP_NOT_EQUAL; break;
  835. case GDTokenizer::TK_OP_LESS: op = OperatorNode::OP_LESS; break;
  836. case GDTokenizer::TK_OP_LESS_EQUAL: op = OperatorNode::OP_LESS_EQUAL; break;
  837. case GDTokenizer::TK_OP_GREATER: op = OperatorNode::OP_GREATER; break;
  838. case GDTokenizer::TK_OP_GREATER_EQUAL: op = OperatorNode::OP_GREATER_EQUAL; break;
  839. case GDTokenizer::TK_OP_AND: op = OperatorNode::OP_AND; break;
  840. case GDTokenizer::TK_OP_OR: op = OperatorNode::OP_OR; break;
  841. case GDTokenizer::TK_OP_ADD: op = OperatorNode::OP_ADD; break;
  842. case GDTokenizer::TK_OP_SUB: op = OperatorNode::OP_SUB; break;
  843. case GDTokenizer::TK_OP_MUL: op = OperatorNode::OP_MUL; break;
  844. case GDTokenizer::TK_OP_DIV: op = OperatorNode::OP_DIV; break;
  845. case GDTokenizer::TK_OP_MOD:
  846. op = OperatorNode::OP_MOD;
  847. break;
  848. //case GDTokenizer::TK_OP_NEG: op=OperatorNode::OP_NEG ; break;
  849. case GDTokenizer::TK_OP_SHIFT_LEFT: op = OperatorNode::OP_SHIFT_LEFT; break;
  850. case GDTokenizer::TK_OP_SHIFT_RIGHT: op = OperatorNode::OP_SHIFT_RIGHT; break;
  851. case GDTokenizer::TK_OP_ASSIGN: {
  852. _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN;
  853. if (tokenizer->get_token(1) == GDTokenizer::TK_CURSOR) {
  854. //code complete assignment
  855. completion_type = COMPLETION_ASSIGN;
  856. completion_node = expr;
  857. completion_class = current_class;
  858. completion_function = current_function;
  859. completion_line = tokenizer->get_token_line();
  860. completion_block = current_block;
  861. completion_found = true;
  862. tokenizer->advance();
  863. }
  864. } break;
  865. case GDTokenizer::TK_OP_ASSIGN_ADD: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_ADD; break;
  866. case GDTokenizer::TK_OP_ASSIGN_SUB: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SUB; break;
  867. case GDTokenizer::TK_OP_ASSIGN_MUL: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_MUL; break;
  868. case GDTokenizer::TK_OP_ASSIGN_DIV: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_DIV; break;
  869. case GDTokenizer::TK_OP_ASSIGN_MOD: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_MOD; break;
  870. case GDTokenizer::TK_OP_ASSIGN_SHIFT_LEFT: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SHIFT_LEFT; break;
  871. case GDTokenizer::TK_OP_ASSIGN_SHIFT_RIGHT: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_SHIFT_RIGHT; break;
  872. case GDTokenizer::TK_OP_ASSIGN_BIT_AND: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_AND; break;
  873. case GDTokenizer::TK_OP_ASSIGN_BIT_OR: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_OR; break;
  874. case GDTokenizer::TK_OP_ASSIGN_BIT_XOR: _VALIDATE_ASSIGN op = OperatorNode::OP_ASSIGN_BIT_XOR; break;
  875. case GDTokenizer::TK_OP_BIT_AND: op = OperatorNode::OP_BIT_AND; break;
  876. case GDTokenizer::TK_OP_BIT_OR: op = OperatorNode::OP_BIT_OR; break;
  877. case GDTokenizer::TK_OP_BIT_XOR: op = OperatorNode::OP_BIT_XOR; break;
  878. case GDTokenizer::TK_PR_IS: op = OperatorNode::OP_IS; break;
  879. case GDTokenizer::TK_CF_IF: op = OperatorNode::OP_TERNARY_IF; break;
  880. case GDTokenizer::TK_CF_ELSE: op = OperatorNode::OP_TERNARY_ELSE; break;
  881. default: valid = false; break;
  882. }
  883. if (valid) {
  884. e.is_op = true;
  885. e.op = op;
  886. expression.push_back(e);
  887. tokenizer->advance();
  888. } else {
  889. break;
  890. }
  891. }
  892. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  893. while (expression.size() > 1) {
  894. int next_op = -1;
  895. int min_priority = 0xFFFFF;
  896. bool is_unary = false;
  897. bool is_ternary = false;
  898. for (int i = 0; i < expression.size(); i++) {
  899. if (!expression[i].is_op) {
  900. continue;
  901. }
  902. int priority;
  903. bool unary = false;
  904. bool ternary = false;
  905. bool error = false;
  906. switch (expression[i].op) {
  907. case OperatorNode::OP_IS:
  908. priority = -1;
  909. break; //before anything
  910. case OperatorNode::OP_BIT_INVERT:
  911. priority = 0;
  912. unary = true;
  913. break;
  914. case OperatorNode::OP_NEG:
  915. priority = 1;
  916. unary = true;
  917. break;
  918. case OperatorNode::OP_POS:
  919. priority = 1;
  920. unary = true;
  921. break;
  922. case OperatorNode::OP_MUL: priority = 2; break;
  923. case OperatorNode::OP_DIV: priority = 2; break;
  924. case OperatorNode::OP_MOD: priority = 2; break;
  925. case OperatorNode::OP_ADD: priority = 3; break;
  926. case OperatorNode::OP_SUB: priority = 3; break;
  927. case OperatorNode::OP_SHIFT_LEFT: priority = 4; break;
  928. case OperatorNode::OP_SHIFT_RIGHT: priority = 4; break;
  929. case OperatorNode::OP_BIT_AND: priority = 5; break;
  930. case OperatorNode::OP_BIT_XOR: priority = 6; break;
  931. case OperatorNode::OP_BIT_OR: priority = 7; break;
  932. case OperatorNode::OP_LESS: priority = 8; break;
  933. case OperatorNode::OP_LESS_EQUAL: priority = 8; break;
  934. case OperatorNode::OP_GREATER: priority = 8; break;
  935. case OperatorNode::OP_GREATER_EQUAL: priority = 8; break;
  936. case OperatorNode::OP_EQUAL: priority = 8; break;
  937. case OperatorNode::OP_NOT_EQUAL: priority = 8; break;
  938. case OperatorNode::OP_IN: priority = 10; break;
  939. case OperatorNode::OP_NOT:
  940. priority = 11;
  941. unary = true;
  942. break;
  943. case OperatorNode::OP_AND: priority = 12; break;
  944. case OperatorNode::OP_OR: priority = 13; break;
  945. case OperatorNode::OP_TERNARY_IF:
  946. priority = 14;
  947. ternary = true;
  948. break;
  949. case OperatorNode::OP_TERNARY_ELSE:
  950. priority = 14;
  951. error = true;
  952. break; // Errors out when found without IF (since IF would consume it)
  953. case OperatorNode::OP_ASSIGN: priority = 15; break;
  954. case OperatorNode::OP_ASSIGN_ADD: priority = 15; break;
  955. case OperatorNode::OP_ASSIGN_SUB: priority = 15; break;
  956. case OperatorNode::OP_ASSIGN_MUL: priority = 15; break;
  957. case OperatorNode::OP_ASSIGN_DIV: priority = 15; break;
  958. case OperatorNode::OP_ASSIGN_MOD: priority = 15; break;
  959. case OperatorNode::OP_ASSIGN_SHIFT_LEFT: priority = 15; break;
  960. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT: priority = 15; break;
  961. case OperatorNode::OP_ASSIGN_BIT_AND: priority = 15; break;
  962. case OperatorNode::OP_ASSIGN_BIT_OR: priority = 15; break;
  963. case OperatorNode::OP_ASSIGN_BIT_XOR: priority = 15; break;
  964. default: {
  965. _set_error("GDParser bug, invalid operator in expression: " + itos(expression[i].op));
  966. return NULL;
  967. }
  968. }
  969. if (priority < min_priority) {
  970. if (error) {
  971. _set_error("Unexpected operator");
  972. return NULL;
  973. }
  974. // < is used for left to right (default)
  975. // <= is used for right to left
  976. next_op = i;
  977. min_priority = priority;
  978. is_unary = unary;
  979. is_ternary = ternary;
  980. }
  981. }
  982. if (next_op == -1) {
  983. _set_error("Yet another parser bug....");
  984. ERR_FAIL_COND_V(next_op == -1, NULL);
  985. }
  986. // OK! create operator..
  987. if (is_unary) {
  988. int expr_pos = next_op;
  989. while (expression[expr_pos].is_op) {
  990. expr_pos++;
  991. if (expr_pos == expression.size()) {
  992. //can happen..
  993. _set_error("Unexpected end of expression..");
  994. return NULL;
  995. }
  996. }
  997. //consecutively do unary opeators
  998. for (int i = expr_pos - 1; i >= next_op; i--) {
  999. OperatorNode *op = alloc_node<OperatorNode>();
  1000. op->op = expression[i].op;
  1001. op->arguments.push_back(expression[i + 1].node);
  1002. op->line = op_line; //line might have been changed from a \n
  1003. expression[i].is_op = false;
  1004. expression[i].node = op;
  1005. expression.remove(i + 1);
  1006. }
  1007. } else if (is_ternary) {
  1008. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  1009. _set_error("Parser bug..");
  1010. ERR_FAIL_V(NULL);
  1011. }
  1012. if (next_op >= (expression.size() - 2) || expression[next_op + 2].op != OperatorNode::OP_TERNARY_ELSE) {
  1013. _set_error("Expected else after ternary if.");
  1014. ERR_FAIL_V(NULL);
  1015. }
  1016. if (next_op >= (expression.size() - 3)) {
  1017. _set_error("Expected value after ternary else.");
  1018. ERR_FAIL_V(NULL);
  1019. }
  1020. OperatorNode *op = alloc_node<OperatorNode>();
  1021. op->op = expression[next_op].op;
  1022. op->line = op_line; //line might have been changed from a \n
  1023. if (expression[next_op - 1].is_op) {
  1024. _set_error("Parser bug..");
  1025. ERR_FAIL_V(NULL);
  1026. }
  1027. if (expression[next_op + 1].is_op) {
  1028. // this is not invalid and can really appear
  1029. // but it becomes invalid anyway because no binary op
  1030. // can be followed by a unary op in a valid combination,
  1031. // due to how precedence works, unaries will always disappear first
  1032. _set_error("Unexpected two consecutive operators after ternary if.");
  1033. return NULL;
  1034. }
  1035. if (expression[next_op + 3].is_op) {
  1036. // this is not invalid and can really appear
  1037. // but it becomes invalid anyway because no binary op
  1038. // can be followed by a unary op in a valid combination,
  1039. // due to how precedence works, unaries will always disappear first
  1040. _set_error("Unexpected two consecutive operators after ternary else.");
  1041. return NULL;
  1042. }
  1043. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as first
  1044. op->arguments.push_back(expression[next_op - 1].node); //left expression goes as when-true
  1045. op->arguments.push_back(expression[next_op + 3].node); //expression after next goes as when-false
  1046. //replace all 3 nodes by this operator and make it an expression
  1047. expression[next_op - 1].node = op;
  1048. expression.remove(next_op);
  1049. expression.remove(next_op);
  1050. expression.remove(next_op);
  1051. expression.remove(next_op);
  1052. } else {
  1053. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  1054. _set_error("Parser bug..");
  1055. ERR_FAIL_V(NULL);
  1056. }
  1057. OperatorNode *op = alloc_node<OperatorNode>();
  1058. op->op = expression[next_op].op;
  1059. op->line = op_line; //line might have been changed from a \n
  1060. if (expression[next_op - 1].is_op) {
  1061. _set_error("Parser bug..");
  1062. ERR_FAIL_V(NULL);
  1063. }
  1064. if (expression[next_op + 1].is_op) {
  1065. // this is not invalid and can really appear
  1066. // but it becomes invalid anyway because no binary op
  1067. // can be followed by a unary op in a valid combination,
  1068. // due to how precedence works, unaries will always disappear first
  1069. _set_error("Unexpected two consecutive operators.");
  1070. return NULL;
  1071. }
  1072. op->arguments.push_back(expression[next_op - 1].node); //expression goes as left
  1073. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as right
  1074. //replace all 3 nodes by this operator and make it an expression
  1075. expression[next_op - 1].node = op;
  1076. expression.remove(next_op);
  1077. expression.remove(next_op);
  1078. }
  1079. }
  1080. return expression[0].node;
  1081. }
  1082. GDParser::Node *GDParser::_reduce_expression(Node *p_node, bool p_to_const) {
  1083. switch (p_node->type) {
  1084. case Node::TYPE_BUILT_IN_FUNCTION: {
  1085. //many may probably be optimizable
  1086. return p_node;
  1087. } break;
  1088. case Node::TYPE_ARRAY: {
  1089. ArrayNode *an = static_cast<ArrayNode *>(p_node);
  1090. bool all_constants = true;
  1091. for (int i = 0; i < an->elements.size(); i++) {
  1092. an->elements[i] = _reduce_expression(an->elements[i], p_to_const);
  1093. if (an->elements[i]->type != Node::TYPE_CONSTANT)
  1094. all_constants = false;
  1095. }
  1096. if (all_constants && p_to_const) {
  1097. //reduce constant array expression
  1098. ConstantNode *cn = alloc_node<ConstantNode>();
  1099. Array arr;
  1100. //print_line("mk array "+itos(!p_to_const));
  1101. arr.resize(an->elements.size());
  1102. for (int i = 0; i < an->elements.size(); i++) {
  1103. ConstantNode *acn = static_cast<ConstantNode *>(an->elements[i]);
  1104. arr[i] = acn->value;
  1105. }
  1106. cn->value = arr;
  1107. return cn;
  1108. }
  1109. return an;
  1110. } break;
  1111. case Node::TYPE_DICTIONARY: {
  1112. DictionaryNode *dn = static_cast<DictionaryNode *>(p_node);
  1113. bool all_constants = true;
  1114. for (int i = 0; i < dn->elements.size(); i++) {
  1115. dn->elements[i].key = _reduce_expression(dn->elements[i].key, p_to_const);
  1116. if (dn->elements[i].key->type != Node::TYPE_CONSTANT)
  1117. all_constants = false;
  1118. dn->elements[i].value = _reduce_expression(dn->elements[i].value, p_to_const);
  1119. if (dn->elements[i].value->type != Node::TYPE_CONSTANT)
  1120. all_constants = false;
  1121. }
  1122. if (all_constants && p_to_const) {
  1123. //reduce constant array expression
  1124. ConstantNode *cn = alloc_node<ConstantNode>();
  1125. Dictionary dict;
  1126. for (int i = 0; i < dn->elements.size(); i++) {
  1127. ConstantNode *key_c = static_cast<ConstantNode *>(dn->elements[i].key);
  1128. ConstantNode *value_c = static_cast<ConstantNode *>(dn->elements[i].value);
  1129. dict[key_c->value] = value_c->value;
  1130. }
  1131. cn->value = dict;
  1132. return cn;
  1133. }
  1134. return dn;
  1135. } break;
  1136. case Node::TYPE_OPERATOR: {
  1137. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  1138. bool all_constants = true;
  1139. int last_not_constant = -1;
  1140. for (int i = 0; i < op->arguments.size(); i++) {
  1141. op->arguments[i] = _reduce_expression(op->arguments[i], p_to_const);
  1142. if (op->arguments[i]->type != Node::TYPE_CONSTANT) {
  1143. all_constants = false;
  1144. last_not_constant = i;
  1145. }
  1146. }
  1147. if (op->op == OperatorNode::OP_IS) {
  1148. //nothing much
  1149. return op;
  1150. }
  1151. if (op->op == OperatorNode::OP_PARENT_CALL) {
  1152. //nothing much
  1153. return op;
  1154. } else if (op->op == OperatorNode::OP_CALL) {
  1155. //can reduce base type constructors
  1156. 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) {
  1157. //native type constructor or intrinsic function
  1158. const Variant **vptr = NULL;
  1159. Vector<Variant *> ptrs;
  1160. if (op->arguments.size() > 1) {
  1161. ptrs.resize(op->arguments.size() - 1);
  1162. for (int i = 0; i < ptrs.size(); i++) {
  1163. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i + 1]);
  1164. ptrs[i] = &cn->value;
  1165. }
  1166. vptr = (const Variant **)&ptrs[0];
  1167. }
  1168. Variant::CallError ce;
  1169. Variant v;
  1170. if (op->arguments[0]->type == Node::TYPE_TYPE) {
  1171. TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
  1172. v = Variant::construct(tn->vtype, vptr, ptrs.size(), ce);
  1173. } else {
  1174. GDFunctions::Function func = static_cast<BuiltInFunctionNode *>(op->arguments[0])->function;
  1175. GDFunctions::call(func, vptr, ptrs.size(), v, ce);
  1176. }
  1177. if (ce.error != Variant::CallError::CALL_OK) {
  1178. String errwhere;
  1179. if (op->arguments[0]->type == Node::TYPE_TYPE) {
  1180. TypeNode *tn = static_cast<TypeNode *>(op->arguments[0]);
  1181. errwhere = "'" + Variant::get_type_name(tn->vtype) + "'' constructor";
  1182. } else {
  1183. GDFunctions::Function func = static_cast<BuiltInFunctionNode *>(op->arguments[0])->function;
  1184. errwhere = String("'") + GDFunctions::get_func_name(func) + "'' intrinsic function";
  1185. }
  1186. switch (ce.error) {
  1187. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1188. _set_error("Invalid argument (#" + itos(ce.argument + 1) + ") for " + errwhere + ".");
  1189. } break;
  1190. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1191. _set_error("Too many arguments for " + errwhere + ".");
  1192. } break;
  1193. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  1194. _set_error("Too few arguments for " + errwhere + ".");
  1195. } break;
  1196. default: {
  1197. _set_error("Invalid arguments for " + errwhere + ".");
  1198. } break;
  1199. }
  1200. error_line = op->line;
  1201. return p_node;
  1202. }
  1203. ConstantNode *cn = alloc_node<ConstantNode>();
  1204. cn->value = v;
  1205. return cn;
  1206. } else if (op->arguments[0]->type == Node::TYPE_BUILT_IN_FUNCTION && last_not_constant == 0) {
  1207. }
  1208. return op; //don't reduce yet
  1209. } else if (op->op == OperatorNode::OP_YIELD) {
  1210. return op;
  1211. } else if (op->op == OperatorNode::OP_INDEX) {
  1212. //can reduce indices into constant arrays or dictionaries
  1213. if (all_constants) {
  1214. ConstantNode *ca = static_cast<ConstantNode *>(op->arguments[0]);
  1215. ConstantNode *cb = static_cast<ConstantNode *>(op->arguments[1]);
  1216. bool valid;
  1217. Variant v = ca->value.get(cb->value, &valid);
  1218. if (!valid) {
  1219. _set_error("invalid index in constant expression");
  1220. error_line = op->line;
  1221. return op;
  1222. }
  1223. ConstantNode *cn = alloc_node<ConstantNode>();
  1224. cn->value = v;
  1225. return cn;
  1226. } /*else if (op->arguments[0]->type==Node::TYPE_CONSTANT && op->arguments[1]->type==Node::TYPE_IDENTIFIER) {
  1227. ConstantNode *ca = static_cast<ConstantNode*>(op->arguments[0]);
  1228. IdentifierNode *ib = static_cast<IdentifierNode*>(op->arguments[1]);
  1229. bool valid;
  1230. Variant v = ca->value.get_named(ib->name,&valid);
  1231. if (!valid) {
  1232. _set_error("invalid index '"+String(ib->name)+"' in constant expression");
  1233. return op;
  1234. }
  1235. ConstantNode *cn = alloc_node<ConstantNode>();
  1236. cn->value=v;
  1237. return cn;
  1238. }*/
  1239. return op;
  1240. } else if (op->op == OperatorNode::OP_INDEX_NAMED) {
  1241. if (op->arguments[0]->type == Node::TYPE_CONSTANT && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  1242. ConstantNode *ca = static_cast<ConstantNode *>(op->arguments[0]);
  1243. IdentifierNode *ib = static_cast<IdentifierNode *>(op->arguments[1]);
  1244. bool valid;
  1245. Variant v = ca->value.get_named(ib->name, &valid);
  1246. if (!valid) {
  1247. _set_error("invalid index '" + String(ib->name) + "' in constant expression");
  1248. error_line = op->line;
  1249. return op;
  1250. }
  1251. ConstantNode *cn = alloc_node<ConstantNode>();
  1252. cn->value = v;
  1253. return cn;
  1254. }
  1255. return op;
  1256. }
  1257. //validate assignment (don't assign to cosntant expression
  1258. switch (op->op) {
  1259. case OperatorNode::OP_ASSIGN:
  1260. case OperatorNode::OP_ASSIGN_ADD:
  1261. case OperatorNode::OP_ASSIGN_SUB:
  1262. case OperatorNode::OP_ASSIGN_MUL:
  1263. case OperatorNode::OP_ASSIGN_DIV:
  1264. case OperatorNode::OP_ASSIGN_MOD:
  1265. case OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  1266. case OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  1267. case OperatorNode::OP_ASSIGN_BIT_AND:
  1268. case OperatorNode::OP_ASSIGN_BIT_OR:
  1269. case OperatorNode::OP_ASSIGN_BIT_XOR: {
  1270. if (op->arguments[0]->type == Node::TYPE_CONSTANT) {
  1271. _set_error("Can't assign to constant", tokenizer->get_token_line() - 1);
  1272. error_line = op->line;
  1273. return op;
  1274. }
  1275. if (op->arguments[0]->type == Node::TYPE_OPERATOR) {
  1276. OperatorNode *on = static_cast<OperatorNode *>(op->arguments[0]);
  1277. if (on->op != OperatorNode::OP_INDEX && on->op != OperatorNode::OP_INDEX_NAMED) {
  1278. _set_error("Can't assign to an expression", tokenizer->get_token_line() - 1);
  1279. error_line = op->line;
  1280. return op;
  1281. }
  1282. }
  1283. } break;
  1284. default: { break; }
  1285. }
  1286. //now se if all are constants
  1287. if (!all_constants)
  1288. return op; //nothing to reduce from here on
  1289. #define _REDUCE_UNARY(m_vop) \
  1290. bool valid = false; \
  1291. Variant res; \
  1292. Variant::evaluate(m_vop, static_cast<ConstantNode *>(op->arguments[0])->value, Variant(), res, valid); \
  1293. if (!valid) { \
  1294. _set_error("Invalid operand for unary operator"); \
  1295. error_line = op->line; \
  1296. return p_node; \
  1297. } \
  1298. ConstantNode *cn = alloc_node<ConstantNode>(); \
  1299. cn->value = res; \
  1300. return cn;
  1301. #define _REDUCE_BINARY(m_vop) \
  1302. bool valid = false; \
  1303. Variant res; \
  1304. Variant::evaluate(m_vop, static_cast<ConstantNode *>(op->arguments[0])->value, static_cast<ConstantNode *>(op->arguments[1])->value, res, valid); \
  1305. if (!valid) { \
  1306. _set_error("Invalid operands for operator"); \
  1307. error_line = op->line; \
  1308. return p_node; \
  1309. } \
  1310. ConstantNode *cn = alloc_node<ConstantNode>(); \
  1311. cn->value = res; \
  1312. return cn;
  1313. switch (op->op) {
  1314. //unary operators
  1315. case OperatorNode::OP_NEG: {
  1316. _REDUCE_UNARY(Variant::OP_NEGATE);
  1317. } break;
  1318. case OperatorNode::OP_POS: {
  1319. _REDUCE_UNARY(Variant::OP_POSITIVE);
  1320. } break;
  1321. case OperatorNode::OP_NOT: {
  1322. _REDUCE_UNARY(Variant::OP_NOT);
  1323. } break;
  1324. case OperatorNode::OP_BIT_INVERT: {
  1325. _REDUCE_UNARY(Variant::OP_BIT_NEGATE);
  1326. } break;
  1327. //binary operators (in precedence order)
  1328. case OperatorNode::OP_IN: {
  1329. _REDUCE_BINARY(Variant::OP_IN);
  1330. } break;
  1331. case OperatorNode::OP_EQUAL: {
  1332. _REDUCE_BINARY(Variant::OP_EQUAL);
  1333. } break;
  1334. case OperatorNode::OP_NOT_EQUAL: {
  1335. _REDUCE_BINARY(Variant::OP_NOT_EQUAL);
  1336. } break;
  1337. case OperatorNode::OP_LESS: {
  1338. _REDUCE_BINARY(Variant::OP_LESS);
  1339. } break;
  1340. case OperatorNode::OP_LESS_EQUAL: {
  1341. _REDUCE_BINARY(Variant::OP_LESS_EQUAL);
  1342. } break;
  1343. case OperatorNode::OP_GREATER: {
  1344. _REDUCE_BINARY(Variant::OP_GREATER);
  1345. } break;
  1346. case OperatorNode::OP_GREATER_EQUAL: {
  1347. _REDUCE_BINARY(Variant::OP_GREATER_EQUAL);
  1348. } break;
  1349. case OperatorNode::OP_AND: {
  1350. _REDUCE_BINARY(Variant::OP_AND);
  1351. } break;
  1352. case OperatorNode::OP_OR: {
  1353. _REDUCE_BINARY(Variant::OP_OR);
  1354. } break;
  1355. case OperatorNode::OP_ADD: {
  1356. _REDUCE_BINARY(Variant::OP_ADD);
  1357. } break;
  1358. case OperatorNode::OP_SUB: {
  1359. _REDUCE_BINARY(Variant::OP_SUBTRACT);
  1360. } break;
  1361. case OperatorNode::OP_MUL: {
  1362. _REDUCE_BINARY(Variant::OP_MULTIPLY);
  1363. } break;
  1364. case OperatorNode::OP_DIV: {
  1365. _REDUCE_BINARY(Variant::OP_DIVIDE);
  1366. } break;
  1367. case OperatorNode::OP_MOD: {
  1368. _REDUCE_BINARY(Variant::OP_MODULE);
  1369. } break;
  1370. case OperatorNode::OP_SHIFT_LEFT: {
  1371. _REDUCE_BINARY(Variant::OP_SHIFT_LEFT);
  1372. } break;
  1373. case OperatorNode::OP_SHIFT_RIGHT: {
  1374. _REDUCE_BINARY(Variant::OP_SHIFT_RIGHT);
  1375. } break;
  1376. case OperatorNode::OP_BIT_AND: {
  1377. _REDUCE_BINARY(Variant::OP_BIT_AND);
  1378. } break;
  1379. case OperatorNode::OP_BIT_OR: {
  1380. _REDUCE_BINARY(Variant::OP_BIT_OR);
  1381. } break;
  1382. case OperatorNode::OP_BIT_XOR: {
  1383. _REDUCE_BINARY(Variant::OP_BIT_XOR);
  1384. } break;
  1385. default: { ERR_FAIL_V(op); }
  1386. }
  1387. ERR_FAIL_V(op);
  1388. } break;
  1389. default: {
  1390. return p_node;
  1391. } break;
  1392. }
  1393. }
  1394. GDParser::Node *GDParser::_parse_and_reduce_expression(Node *p_parent, bool p_static, bool p_reduce_const, bool p_allow_assign) {
  1395. Node *expr = _parse_expression(p_parent, p_static, p_allow_assign, p_reduce_const);
  1396. if (!expr || error_set)
  1397. return NULL;
  1398. expr = _reduce_expression(expr, p_reduce_const);
  1399. if (!expr || error_set)
  1400. return NULL;
  1401. return expr;
  1402. }
  1403. bool GDParser::_recover_from_completion() {
  1404. if (!completion_found) {
  1405. return false; //can't recover if no completion
  1406. }
  1407. //skip stuff until newline
  1408. while (tokenizer->get_token() != GDTokenizer::TK_NEWLINE && tokenizer->get_token() != GDTokenizer::TK_EOF && tokenizer->get_token() != GDTokenizer::TK_ERROR) {
  1409. tokenizer->advance();
  1410. }
  1411. completion_found = false;
  1412. error_set = false;
  1413. if (tokenizer->get_token() == GDTokenizer::TK_ERROR) {
  1414. error_set = true;
  1415. }
  1416. return true;
  1417. }
  1418. GDParser::PatternNode *GDParser::_parse_pattern(bool p_static) {
  1419. PatternNode *pattern = alloc_node<PatternNode>();
  1420. GDTokenizer::Token token = tokenizer->get_token();
  1421. if (error_set)
  1422. return NULL;
  1423. if (token == GDTokenizer::TK_EOF) {
  1424. return NULL;
  1425. }
  1426. switch (token) {
  1427. // array
  1428. case GDTokenizer::TK_BRACKET_OPEN: {
  1429. tokenizer->advance();
  1430. pattern->pt_type = GDParser::PatternNode::PT_ARRAY;
  1431. while (true) {
  1432. if (tokenizer->get_token() == GDTokenizer::TK_BRACKET_CLOSE) {
  1433. tokenizer->advance();
  1434. break;
  1435. }
  1436. if (tokenizer->get_token() == GDTokenizer::TK_PERIOD && tokenizer->get_token(1) == GDTokenizer::TK_PERIOD) {
  1437. // match everything
  1438. tokenizer->advance(2);
  1439. PatternNode *sub_pattern = alloc_node<PatternNode>();
  1440. sub_pattern->pt_type = GDParser::PatternNode::PT_IGNORE_REST;
  1441. pattern->array.push_back(sub_pattern);
  1442. if (tokenizer->get_token() == GDTokenizer::TK_COMMA && tokenizer->get_token(1) == GDTokenizer::TK_BRACKET_CLOSE) {
  1443. tokenizer->advance(2);
  1444. break;
  1445. } else if (tokenizer->get_token() == GDTokenizer::TK_BRACKET_CLOSE) {
  1446. tokenizer->advance(1);
  1447. break;
  1448. } else {
  1449. _set_error("'..' pattern only allowed at the end of an array pattern");
  1450. return NULL;
  1451. }
  1452. }
  1453. PatternNode *sub_pattern = _parse_pattern(p_static);
  1454. if (!sub_pattern) {
  1455. return NULL;
  1456. }
  1457. pattern->array.push_back(sub_pattern);
  1458. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  1459. tokenizer->advance();
  1460. continue;
  1461. } else if (tokenizer->get_token() == GDTokenizer::TK_BRACKET_CLOSE) {
  1462. tokenizer->advance();
  1463. break;
  1464. } else {
  1465. _set_error("Not a valid pattern");
  1466. return NULL;
  1467. }
  1468. }
  1469. } break;
  1470. // bind
  1471. case GDTokenizer::TK_PR_VAR: {
  1472. tokenizer->advance();
  1473. pattern->pt_type = GDParser::PatternNode::PT_BIND;
  1474. pattern->bind = tokenizer->get_token_identifier();
  1475. tokenizer->advance();
  1476. } break;
  1477. // dictionary
  1478. case GDTokenizer::TK_CURLY_BRACKET_OPEN: {
  1479. tokenizer->advance();
  1480. pattern->pt_type = GDParser::PatternNode::PT_DICTIONARY;
  1481. while (true) {
  1482. if (tokenizer->get_token() == GDTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1483. tokenizer->advance();
  1484. break;
  1485. }
  1486. if (tokenizer->get_token() == GDTokenizer::TK_PERIOD && tokenizer->get_token(1) == GDTokenizer::TK_PERIOD) {
  1487. // match everything
  1488. tokenizer->advance(2);
  1489. PatternNode *sub_pattern = alloc_node<PatternNode>();
  1490. sub_pattern->pt_type = PatternNode::PT_IGNORE_REST;
  1491. pattern->array.push_back(sub_pattern);
  1492. if (tokenizer->get_token() == GDTokenizer::TK_COMMA && tokenizer->get_token(1) == GDTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1493. tokenizer->advance(2);
  1494. break;
  1495. } else if (tokenizer->get_token() == GDTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1496. tokenizer->advance(1);
  1497. break;
  1498. } else {
  1499. _set_error("'..' pattern only allowed at the end of a dictionary pattern");
  1500. return NULL;
  1501. }
  1502. }
  1503. Node *key = _parse_and_reduce_expression(pattern, p_static);
  1504. if (!key) {
  1505. _set_error("Not a valid key in pattern");
  1506. return NULL;
  1507. }
  1508. if (key->type != GDParser::Node::TYPE_CONSTANT) {
  1509. _set_error("Not a constant expression as key");
  1510. return NULL;
  1511. }
  1512. if (tokenizer->get_token() == GDTokenizer::TK_COLON) {
  1513. tokenizer->advance();
  1514. PatternNode *value = _parse_pattern(p_static);
  1515. if (!value) {
  1516. _set_error("Expected pattern in dictionary value");
  1517. return NULL;
  1518. }
  1519. pattern->dictionary.insert(static_cast<ConstantNode *>(key), value);
  1520. } else {
  1521. pattern->dictionary.insert(static_cast<ConstantNode *>(key), NULL);
  1522. }
  1523. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  1524. tokenizer->advance();
  1525. continue;
  1526. } else if (tokenizer->get_token() == GDTokenizer::TK_CURLY_BRACKET_CLOSE) {
  1527. tokenizer->advance();
  1528. break;
  1529. } else {
  1530. _set_error("Not a valid pattern");
  1531. return NULL;
  1532. }
  1533. }
  1534. } break;
  1535. case GDTokenizer::TK_WILDCARD: {
  1536. tokenizer->advance();
  1537. pattern->pt_type = PatternNode::PT_WILDCARD;
  1538. } break;
  1539. // all the constants like strings and numbers
  1540. default: {
  1541. Node *value = _parse_and_reduce_expression(pattern, p_static);
  1542. if (!value) {
  1543. _set_error("Expect constant expression or variables in a pattern");
  1544. return NULL;
  1545. }
  1546. if (value->type == Node::TYPE_OPERATOR) {
  1547. // Maybe it's SomeEnum.VALUE
  1548. Node *current_value = value;
  1549. while (current_value->type == Node::TYPE_OPERATOR) {
  1550. OperatorNode *op_node = static_cast<OperatorNode *>(current_value);
  1551. if (op_node->op != OperatorNode::OP_INDEX_NAMED) {
  1552. _set_error("Invalid operator in pattern. Only index (`A.B`) is allowed");
  1553. return NULL;
  1554. }
  1555. current_value = op_node->arguments[0];
  1556. }
  1557. if (current_value->type != Node::TYPE_IDENTIFIER) {
  1558. _set_error("Only constant expression or variables allowed in a pattern");
  1559. return NULL;
  1560. }
  1561. } else if (value->type != Node::TYPE_IDENTIFIER && value->type != Node::TYPE_CONSTANT) {
  1562. _set_error("Only constant expressions or variables allowed in a pattern");
  1563. return NULL;
  1564. }
  1565. pattern->pt_type = PatternNode::PT_CONSTANT;
  1566. pattern->constant = value;
  1567. } break;
  1568. }
  1569. return pattern;
  1570. }
  1571. void GDParser::_parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static) {
  1572. int indent_level = tab_level.back()->get();
  1573. while (true) {
  1574. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE && _parse_newline())
  1575. ;
  1576. // GDTokenizer::Token token = tokenizer->get_token();
  1577. if (error_set)
  1578. return;
  1579. if (indent_level > tab_level.back()->get()) {
  1580. return; // go back a level
  1581. }
  1582. if (pending_newline != -1) {
  1583. pending_newline = -1;
  1584. }
  1585. PatternBranchNode *branch = alloc_node<PatternBranchNode>();
  1586. branch->patterns.push_back(_parse_pattern(p_static));
  1587. if (!branch->patterns[0]) {
  1588. return;
  1589. }
  1590. while (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  1591. tokenizer->advance();
  1592. branch->patterns.push_back(_parse_pattern(p_static));
  1593. if (!branch->patterns[branch->patterns.size() - 1]) {
  1594. return;
  1595. }
  1596. }
  1597. if (!_enter_indent_block()) {
  1598. _set_error("Expected block in pattern branch");
  1599. return;
  1600. }
  1601. branch->body = alloc_node<BlockNode>();
  1602. branch->body->parent_block = p_block;
  1603. p_block->sub_blocks.push_back(branch->body);
  1604. current_block = branch->body;
  1605. _parse_block(branch->body, p_static);
  1606. current_block = p_block;
  1607. p_branches.push_back(branch);
  1608. }
  1609. }
  1610. void GDParser::_generate_pattern(PatternNode *p_pattern, Node *p_node_to_match, Node *&p_resulting_node, Map<StringName, Node *> &p_bindings) {
  1611. switch (p_pattern->pt_type) {
  1612. case PatternNode::PT_CONSTANT: {
  1613. // typecheck
  1614. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1615. typeof_node->function = GDFunctions::TYPE_OF;
  1616. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1617. typeof_match_value->op = OperatorNode::OP_CALL;
  1618. typeof_match_value->arguments.push_back(typeof_node);
  1619. typeof_match_value->arguments.push_back(p_node_to_match);
  1620. OperatorNode *typeof_pattern_value = alloc_node<OperatorNode>();
  1621. typeof_pattern_value->op = OperatorNode::OP_CALL;
  1622. typeof_pattern_value->arguments.push_back(typeof_node);
  1623. typeof_pattern_value->arguments.push_back(p_pattern->constant);
  1624. OperatorNode *type_comp = alloc_node<OperatorNode>();
  1625. type_comp->op = OperatorNode::OP_EQUAL;
  1626. type_comp->arguments.push_back(typeof_match_value);
  1627. type_comp->arguments.push_back(typeof_pattern_value);
  1628. // comare the actual values
  1629. OperatorNode *value_comp = alloc_node<OperatorNode>();
  1630. value_comp->op = OperatorNode::OP_EQUAL;
  1631. value_comp->arguments.push_back(p_pattern->constant);
  1632. value_comp->arguments.push_back(p_node_to_match);
  1633. OperatorNode *comparison = alloc_node<OperatorNode>();
  1634. comparison->op = OperatorNode::OP_AND;
  1635. comparison->arguments.push_back(type_comp);
  1636. comparison->arguments.push_back(value_comp);
  1637. p_resulting_node = comparison;
  1638. } break;
  1639. case PatternNode::PT_BIND: {
  1640. p_bindings[p_pattern->bind] = p_node_to_match;
  1641. // a bind always matches
  1642. ConstantNode *true_value = alloc_node<ConstantNode>();
  1643. true_value->value = Variant(true);
  1644. p_resulting_node = true_value;
  1645. } break;
  1646. case PatternNode::PT_ARRAY: {
  1647. bool open_ended = false;
  1648. if (p_pattern->array.size() > 0) {
  1649. if (p_pattern->array[p_pattern->array.size() - 1]->pt_type == PatternNode::PT_IGNORE_REST) {
  1650. open_ended = true;
  1651. }
  1652. }
  1653. // typeof(value_to_match) == TYPE_ARRAY && value_to_match.size() >= length
  1654. // typeof(value_to_match) == TYPE_ARRAY && value_to_match.size() == length
  1655. {
  1656. // typecheck
  1657. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1658. typeof_node->function = GDFunctions::TYPE_OF;
  1659. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1660. typeof_match_value->op = OperatorNode::OP_CALL;
  1661. typeof_match_value->arguments.push_back(typeof_node);
  1662. typeof_match_value->arguments.push_back(p_node_to_match);
  1663. IdentifierNode *typeof_array = alloc_node<IdentifierNode>();
  1664. typeof_array->name = "TYPE_ARRAY";
  1665. OperatorNode *type_comp = alloc_node<OperatorNode>();
  1666. type_comp->op = OperatorNode::OP_EQUAL;
  1667. type_comp->arguments.push_back(typeof_match_value);
  1668. type_comp->arguments.push_back(typeof_array);
  1669. // size
  1670. ConstantNode *length = alloc_node<ConstantNode>();
  1671. length->value = Variant(open_ended ? p_pattern->array.size() - 1 : p_pattern->array.size());
  1672. OperatorNode *call = alloc_node<OperatorNode>();
  1673. call->op = OperatorNode::OP_CALL;
  1674. call->arguments.push_back(p_node_to_match);
  1675. IdentifierNode *size = alloc_node<IdentifierNode>();
  1676. size->name = "size";
  1677. call->arguments.push_back(size);
  1678. OperatorNode *length_comparison = alloc_node<OperatorNode>();
  1679. length_comparison->op = open_ended ? OperatorNode::OP_GREATER_EQUAL : OperatorNode::OP_EQUAL;
  1680. length_comparison->arguments.push_back(call);
  1681. length_comparison->arguments.push_back(length);
  1682. OperatorNode *type_and_length_comparison = alloc_node<OperatorNode>();
  1683. type_and_length_comparison->op = OperatorNode::OP_AND;
  1684. type_and_length_comparison->arguments.push_back(type_comp);
  1685. type_and_length_comparison->arguments.push_back(length_comparison);
  1686. p_resulting_node = type_and_length_comparison;
  1687. }
  1688. for (int i = 0; i < p_pattern->array.size(); i++) {
  1689. PatternNode *pattern = p_pattern->array[i];
  1690. Node *condition = NULL;
  1691. ConstantNode *index = alloc_node<ConstantNode>();
  1692. index->value = Variant(i);
  1693. OperatorNode *indexed_value = alloc_node<OperatorNode>();
  1694. indexed_value->op = OperatorNode::OP_INDEX;
  1695. indexed_value->arguments.push_back(p_node_to_match);
  1696. indexed_value->arguments.push_back(index);
  1697. _generate_pattern(pattern, indexed_value, condition, p_bindings);
  1698. // concatenate all the patterns with &&
  1699. OperatorNode *and_node = alloc_node<OperatorNode>();
  1700. and_node->op = OperatorNode::OP_AND;
  1701. and_node->arguments.push_back(p_resulting_node);
  1702. and_node->arguments.push_back(condition);
  1703. p_resulting_node = and_node;
  1704. }
  1705. } break;
  1706. case PatternNode::PT_DICTIONARY: {
  1707. bool open_ended = false;
  1708. if (p_pattern->array.size() > 0) {
  1709. open_ended = true;
  1710. }
  1711. // typeof(value_to_match) == TYPE_DICTIONARY && value_to_match.size() >= length
  1712. // typeof(value_to_match) == TYPE_DICTIONARY && value_to_match.size() == length
  1713. {
  1714. // typecheck
  1715. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1716. typeof_node->function = GDFunctions::TYPE_OF;
  1717. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1718. typeof_match_value->op = OperatorNode::OP_CALL;
  1719. typeof_match_value->arguments.push_back(typeof_node);
  1720. typeof_match_value->arguments.push_back(p_node_to_match);
  1721. IdentifierNode *typeof_dictionary = alloc_node<IdentifierNode>();
  1722. typeof_dictionary->name = "TYPE_DICTIONARY";
  1723. OperatorNode *type_comp = alloc_node<OperatorNode>();
  1724. type_comp->op = OperatorNode::OP_EQUAL;
  1725. type_comp->arguments.push_back(typeof_match_value);
  1726. type_comp->arguments.push_back(typeof_dictionary);
  1727. // size
  1728. ConstantNode *length = alloc_node<ConstantNode>();
  1729. length->value = Variant(open_ended ? p_pattern->dictionary.size() - 1 : p_pattern->dictionary.size());
  1730. OperatorNode *call = alloc_node<OperatorNode>();
  1731. call->op = OperatorNode::OP_CALL;
  1732. call->arguments.push_back(p_node_to_match);
  1733. IdentifierNode *size = alloc_node<IdentifierNode>();
  1734. size->name = "size";
  1735. call->arguments.push_back(size);
  1736. OperatorNode *length_comparison = alloc_node<OperatorNode>();
  1737. length_comparison->op = open_ended ? OperatorNode::OP_GREATER_EQUAL : OperatorNode::OP_EQUAL;
  1738. length_comparison->arguments.push_back(call);
  1739. length_comparison->arguments.push_back(length);
  1740. OperatorNode *type_and_length_comparison = alloc_node<OperatorNode>();
  1741. type_and_length_comparison->op = OperatorNode::OP_AND;
  1742. type_and_length_comparison->arguments.push_back(type_comp);
  1743. type_and_length_comparison->arguments.push_back(length_comparison);
  1744. p_resulting_node = type_and_length_comparison;
  1745. }
  1746. for (Map<ConstantNode *, PatternNode *>::Element *e = p_pattern->dictionary.front(); e; e = e->next()) {
  1747. Node *condition = NULL;
  1748. // chech for has, then for pattern
  1749. IdentifierNode *has = alloc_node<IdentifierNode>();
  1750. has->name = "has";
  1751. OperatorNode *has_call = alloc_node<OperatorNode>();
  1752. has_call->op = OperatorNode::OP_CALL;
  1753. has_call->arguments.push_back(p_node_to_match);
  1754. has_call->arguments.push_back(has);
  1755. has_call->arguments.push_back(e->key());
  1756. if (e->value()) {
  1757. OperatorNode *indexed_value = alloc_node<OperatorNode>();
  1758. indexed_value->op = OperatorNode::OP_INDEX;
  1759. indexed_value->arguments.push_back(p_node_to_match);
  1760. indexed_value->arguments.push_back(e->key());
  1761. _generate_pattern(e->value(), indexed_value, condition, p_bindings);
  1762. OperatorNode *has_and_pattern = alloc_node<OperatorNode>();
  1763. has_and_pattern->op = OperatorNode::OP_AND;
  1764. has_and_pattern->arguments.push_back(has_call);
  1765. has_and_pattern->arguments.push_back(condition);
  1766. condition = has_and_pattern;
  1767. } else {
  1768. condition = has_call;
  1769. }
  1770. // concatenate all the patterns with &&
  1771. OperatorNode *and_node = alloc_node<OperatorNode>();
  1772. and_node->op = OperatorNode::OP_AND;
  1773. and_node->arguments.push_back(p_resulting_node);
  1774. and_node->arguments.push_back(condition);
  1775. p_resulting_node = and_node;
  1776. }
  1777. } break;
  1778. case PatternNode::PT_IGNORE_REST:
  1779. case PatternNode::PT_WILDCARD: {
  1780. // simply generate a `true`
  1781. ConstantNode *true_value = alloc_node<ConstantNode>();
  1782. true_value->value = Variant(true);
  1783. p_resulting_node = true_value;
  1784. } break;
  1785. default: {
  1786. } break;
  1787. }
  1788. }
  1789. void GDParser::_transform_match_statment(BlockNode *p_block, MatchNode *p_match_statement) {
  1790. IdentifierNode *id = alloc_node<IdentifierNode>();
  1791. id->name = "#match_value";
  1792. for (int i = 0; i < p_match_statement->branches.size(); i++) {
  1793. PatternBranchNode *branch = p_match_statement->branches[i];
  1794. MatchNode::CompiledPatternBranch compiled_branch;
  1795. compiled_branch.compiled_pattern = NULL;
  1796. Map<StringName, Node *> binding;
  1797. for (int j = 0; j < branch->patterns.size(); j++) {
  1798. PatternNode *pattern = branch->patterns[j];
  1799. Map<StringName, Node *> bindings;
  1800. Node *resulting_node;
  1801. _generate_pattern(pattern, id, resulting_node, bindings);
  1802. if (!binding.empty() && !bindings.empty()) {
  1803. _set_error("Multipatterns can't contain bindings");
  1804. return;
  1805. } else {
  1806. binding = bindings;
  1807. }
  1808. if (compiled_branch.compiled_pattern) {
  1809. OperatorNode *or_node = alloc_node<OperatorNode>();
  1810. or_node->op = OperatorNode::OP_OR;
  1811. or_node->arguments.push_back(compiled_branch.compiled_pattern);
  1812. or_node->arguments.push_back(resulting_node);
  1813. compiled_branch.compiled_pattern = or_node;
  1814. } else {
  1815. // single pattern | first one
  1816. compiled_branch.compiled_pattern = resulting_node;
  1817. }
  1818. }
  1819. // prepare the body ...hehe
  1820. for (Map<StringName, Node *>::Element *e = binding.front(); e; e = e->next()) {
  1821. LocalVarNode *local_var = alloc_node<LocalVarNode>();
  1822. local_var->name = e->key();
  1823. local_var->assign = e->value();
  1824. IdentifierNode *id = alloc_node<IdentifierNode>();
  1825. id->name = local_var->name;
  1826. OperatorNode *op = alloc_node<OperatorNode>();
  1827. op->op = OperatorNode::OP_ASSIGN;
  1828. op->arguments.push_back(id);
  1829. op->arguments.push_back(local_var->assign);
  1830. branch->body->statements.push_front(op);
  1831. branch->body->statements.push_front(local_var);
  1832. }
  1833. compiled_branch.body = branch->body;
  1834. p_match_statement->compiled_pattern_branches.push_back(compiled_branch);
  1835. }
  1836. }
  1837. void GDParser::_parse_block(BlockNode *p_block, bool p_static) {
  1838. int indent_level = tab_level.back()->get();
  1839. #ifdef DEBUG_ENABLED
  1840. NewLineNode *nl = alloc_node<NewLineNode>();
  1841. nl->line = tokenizer->get_token_line();
  1842. p_block->statements.push_back(nl);
  1843. #endif
  1844. bool is_first_line = true;
  1845. while (true) {
  1846. if (!is_first_line && tab_level.back()->prev() && tab_level.back()->prev()->get() == indent_level) {
  1847. // pythonic single-line expression, don't parse future lines
  1848. tab_level.pop_back();
  1849. p_block->end_line = tokenizer->get_token_line();
  1850. return;
  1851. }
  1852. is_first_line = false;
  1853. GDTokenizer::Token token = tokenizer->get_token();
  1854. if (error_set)
  1855. return;
  1856. if (indent_level > tab_level.back()->get()) {
  1857. p_block->end_line = tokenizer->get_token_line();
  1858. return; //go back a level
  1859. }
  1860. if (pending_newline != -1) {
  1861. NewLineNode *nl = alloc_node<NewLineNode>();
  1862. nl->line = pending_newline;
  1863. p_block->statements.push_back(nl);
  1864. pending_newline = -1;
  1865. }
  1866. switch (token) {
  1867. case GDTokenizer::TK_EOF:
  1868. p_block->end_line = tokenizer->get_token_line();
  1869. case GDTokenizer::TK_ERROR: {
  1870. return; //go back
  1871. //end of file!
  1872. } break;
  1873. case GDTokenizer::TK_NEWLINE: {
  1874. if (!_parse_newline()) {
  1875. if (!error_set) {
  1876. p_block->end_line = tokenizer->get_token_line();
  1877. pending_newline = p_block->end_line;
  1878. }
  1879. return;
  1880. }
  1881. NewLineNode *nl = alloc_node<NewLineNode>();
  1882. nl->line = tokenizer->get_token_line();
  1883. p_block->statements.push_back(nl);
  1884. } break;
  1885. case GDTokenizer::TK_CF_PASS: {
  1886. if (tokenizer->get_token(1) != GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1) != GDTokenizer::TK_NEWLINE && tokenizer->get_token(1) != GDTokenizer::TK_EOF) {
  1887. _set_error("Expected ';' or <NewLine>.");
  1888. return;
  1889. }
  1890. tokenizer->advance();
  1891. if (tokenizer->get_token() == GDTokenizer::TK_SEMICOLON) {
  1892. // Ignore semicolon after 'pass'
  1893. tokenizer->advance();
  1894. }
  1895. } break;
  1896. case GDTokenizer::TK_PR_VAR: {
  1897. //variale declaration and (eventual) initialization
  1898. tokenizer->advance();
  1899. if (!tokenizer->is_token_literal(0, true)) {
  1900. _set_error("Expected identifier for local variable name.");
  1901. return;
  1902. }
  1903. StringName n = tokenizer->get_token_literal();
  1904. tokenizer->advance();
  1905. if (current_function) {
  1906. for (int i = 0; i < current_function->arguments.size(); i++) {
  1907. if (n == current_function->arguments[i]) {
  1908. _set_error("Variable '" + String(n) + "' already defined in the scope (at line: " + itos(current_function->line) + ").");
  1909. return;
  1910. }
  1911. }
  1912. }
  1913. BlockNode *check_block = p_block;
  1914. while (check_block) {
  1915. for (int i = 0; i < check_block->variables.size(); i++) {
  1916. if (n == check_block->variables[i]) {
  1917. _set_error("Variable '" + String(n) + "' already defined in the scope (at line: " + itos(check_block->variable_lines[i]) + ").");
  1918. return;
  1919. }
  1920. }
  1921. check_block = check_block->parent_block;
  1922. }
  1923. int var_line = tokenizer->get_token_line();
  1924. //must know when the local variable is declared
  1925. LocalVarNode *lv = alloc_node<LocalVarNode>();
  1926. lv->name = n;
  1927. p_block->statements.push_back(lv);
  1928. Node *assigned = NULL;
  1929. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  1930. tokenizer->advance();
  1931. Node *subexpr = _parse_and_reduce_expression(p_block, p_static);
  1932. if (!subexpr) {
  1933. if (_recover_from_completion()) {
  1934. break;
  1935. }
  1936. return;
  1937. }
  1938. lv->assign = subexpr;
  1939. assigned = subexpr;
  1940. } else {
  1941. ConstantNode *c = alloc_node<ConstantNode>();
  1942. c->value = Variant();
  1943. assigned = c;
  1944. }
  1945. //must be added later, to avoid self-referencing.
  1946. p_block->variables.push_back(n); //line?
  1947. p_block->variable_lines.push_back(var_line);
  1948. IdentifierNode *id = alloc_node<IdentifierNode>();
  1949. id->name = n;
  1950. OperatorNode *op = alloc_node<OperatorNode>();
  1951. op->op = OperatorNode::OP_ASSIGN;
  1952. op->arguments.push_back(id);
  1953. op->arguments.push_back(assigned);
  1954. p_block->statements.push_back(op);
  1955. if (!_end_statement()) {
  1956. _set_error("Expected end of statement (var)");
  1957. return;
  1958. }
  1959. } break;
  1960. case GDTokenizer::TK_CF_IF: {
  1961. tokenizer->advance();
  1962. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  1963. if (!condition) {
  1964. if (_recover_from_completion()) {
  1965. break;
  1966. }
  1967. return;
  1968. }
  1969. ControlFlowNode *cf_if = alloc_node<ControlFlowNode>();
  1970. cf_if->cf_type = ControlFlowNode::CF_IF;
  1971. cf_if->arguments.push_back(condition);
  1972. cf_if->body = alloc_node<BlockNode>();
  1973. cf_if->body->parent_block = p_block;
  1974. cf_if->body->if_condition = condition; //helps code completion
  1975. p_block->sub_blocks.push_back(cf_if->body);
  1976. if (!_enter_indent_block(cf_if->body)) {
  1977. _set_error("Expected indented block after 'if'");
  1978. p_block->end_line = tokenizer->get_token_line();
  1979. return;
  1980. }
  1981. current_block = cf_if->body;
  1982. _parse_block(cf_if->body, p_static);
  1983. current_block = p_block;
  1984. if (error_set)
  1985. return;
  1986. p_block->statements.push_back(cf_if);
  1987. while (true) {
  1988. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE && _parse_newline())
  1989. ;
  1990. if (tab_level.back()->get() < indent_level) { //not at current indent level
  1991. p_block->end_line = tokenizer->get_token_line();
  1992. return;
  1993. }
  1994. if (tokenizer->get_token() == GDTokenizer::TK_CF_ELIF) {
  1995. if (tab_level.back()->get() > indent_level) {
  1996. _set_error("Invalid indent");
  1997. return;
  1998. }
  1999. tokenizer->advance();
  2000. cf_if->body_else = alloc_node<BlockNode>();
  2001. cf_if->body_else->parent_block = p_block;
  2002. p_block->sub_blocks.push_back(cf_if->body_else);
  2003. ControlFlowNode *cf_else = alloc_node<ControlFlowNode>();
  2004. cf_else->cf_type = ControlFlowNode::CF_IF;
  2005. //condition
  2006. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2007. if (!condition) {
  2008. if (_recover_from_completion()) {
  2009. break;
  2010. }
  2011. return;
  2012. }
  2013. cf_else->arguments.push_back(condition);
  2014. cf_else->cf_type = ControlFlowNode::CF_IF;
  2015. cf_if->body_else->statements.push_back(cf_else);
  2016. cf_if = cf_else;
  2017. cf_if->body = alloc_node<BlockNode>();
  2018. cf_if->body->parent_block = p_block;
  2019. p_block->sub_blocks.push_back(cf_if->body);
  2020. if (!_enter_indent_block(cf_if->body)) {
  2021. _set_error("Expected indented block after 'elif'");
  2022. p_block->end_line = tokenizer->get_token_line();
  2023. return;
  2024. }
  2025. current_block = cf_else->body;
  2026. _parse_block(cf_else->body, p_static);
  2027. current_block = p_block;
  2028. if (error_set)
  2029. return;
  2030. } else if (tokenizer->get_token() == GDTokenizer::TK_CF_ELSE) {
  2031. if (tab_level.back()->get() > indent_level) {
  2032. _set_error("Invalid indent");
  2033. return;
  2034. }
  2035. tokenizer->advance();
  2036. cf_if->body_else = alloc_node<BlockNode>();
  2037. cf_if->body_else->parent_block = p_block;
  2038. p_block->sub_blocks.push_back(cf_if->body_else);
  2039. if (!_enter_indent_block(cf_if->body_else)) {
  2040. _set_error("Expected indented block after 'else'");
  2041. p_block->end_line = tokenizer->get_token_line();
  2042. return;
  2043. }
  2044. current_block = cf_if->body_else;
  2045. _parse_block(cf_if->body_else, p_static);
  2046. current_block = p_block;
  2047. if (error_set)
  2048. return;
  2049. break; //after else, exit
  2050. } else
  2051. break;
  2052. }
  2053. } break;
  2054. case GDTokenizer::TK_CF_WHILE: {
  2055. tokenizer->advance();
  2056. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2057. if (!condition) {
  2058. if (_recover_from_completion()) {
  2059. break;
  2060. }
  2061. return;
  2062. }
  2063. ControlFlowNode *cf_while = alloc_node<ControlFlowNode>();
  2064. cf_while->cf_type = ControlFlowNode::CF_WHILE;
  2065. cf_while->arguments.push_back(condition);
  2066. cf_while->body = alloc_node<BlockNode>();
  2067. cf_while->body->parent_block = p_block;
  2068. p_block->sub_blocks.push_back(cf_while->body);
  2069. if (!_enter_indent_block(cf_while->body)) {
  2070. _set_error("Expected indented block after 'while'");
  2071. p_block->end_line = tokenizer->get_token_line();
  2072. return;
  2073. }
  2074. current_block = cf_while->body;
  2075. _parse_block(cf_while->body, p_static);
  2076. current_block = p_block;
  2077. if (error_set)
  2078. return;
  2079. p_block->statements.push_back(cf_while);
  2080. } break;
  2081. case GDTokenizer::TK_CF_FOR: {
  2082. tokenizer->advance();
  2083. if (!tokenizer->is_token_literal(0, true)) {
  2084. _set_error("identifier expected after 'for'");
  2085. }
  2086. IdentifierNode *id = alloc_node<IdentifierNode>();
  2087. id->name = tokenizer->get_token_identifier();
  2088. tokenizer->advance();
  2089. if (tokenizer->get_token() != GDTokenizer::TK_OP_IN) {
  2090. _set_error("'in' expected after identifier");
  2091. return;
  2092. }
  2093. tokenizer->advance();
  2094. Node *container = _parse_and_reduce_expression(p_block, p_static);
  2095. if (!container) {
  2096. if (_recover_from_completion()) {
  2097. break;
  2098. }
  2099. return;
  2100. }
  2101. if (container->type == Node::TYPE_OPERATOR) {
  2102. OperatorNode *op = static_cast<OperatorNode *>(container);
  2103. if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_BUILT_IN_FUNCTION && static_cast<BuiltInFunctionNode *>(op->arguments[0])->function == GDFunctions::GEN_RANGE) {
  2104. //iterating a range, so see if range() can be optimized without allocating memory, by replacing it by vectors (which can work as iterable too!)
  2105. Vector<Node *> args;
  2106. Vector<double> constants;
  2107. bool constant = false;
  2108. for (int i = 1; i < op->arguments.size(); i++) {
  2109. args.push_back(op->arguments[i]);
  2110. if (constant && op->arguments[i]->type == Node::TYPE_CONSTANT) {
  2111. ConstantNode *c = static_cast<ConstantNode *>(op->arguments[i]);
  2112. if (c->value.get_type() == Variant::REAL || c->value.get_type() == Variant::INT) {
  2113. constants.push_back(c->value);
  2114. constant = true;
  2115. }
  2116. } else {
  2117. constant = false;
  2118. }
  2119. }
  2120. if (args.size() > 0 && args.size() < 4) {
  2121. if (constant) {
  2122. ConstantNode *cn = alloc_node<ConstantNode>();
  2123. switch (args.size()) {
  2124. case 1: cn->value = (int)constants[0]; break;
  2125. case 2: cn->value = Vector2(constants[0], constants[1]); break;
  2126. case 3: cn->value = Vector3(constants[0], constants[1], constants[2]); break;
  2127. }
  2128. container = cn;
  2129. } else {
  2130. OperatorNode *on = alloc_node<OperatorNode>();
  2131. on->op = OperatorNode::OP_CALL;
  2132. TypeNode *tn = alloc_node<TypeNode>();
  2133. on->arguments.push_back(tn);
  2134. switch (args.size()) {
  2135. case 1: tn->vtype = Variant::INT; break;
  2136. case 2: tn->vtype = Variant::VECTOR2; break;
  2137. case 3: tn->vtype = Variant::VECTOR3; break;
  2138. }
  2139. for (int i = 0; i < args.size(); i++) {
  2140. on->arguments.push_back(args[i]);
  2141. }
  2142. container = on;
  2143. }
  2144. }
  2145. }
  2146. }
  2147. ControlFlowNode *cf_for = alloc_node<ControlFlowNode>();
  2148. cf_for->cf_type = ControlFlowNode::CF_FOR;
  2149. cf_for->arguments.push_back(id);
  2150. cf_for->arguments.push_back(container);
  2151. cf_for->body = alloc_node<BlockNode>();
  2152. cf_for->body->parent_block = p_block;
  2153. p_block->sub_blocks.push_back(cf_for->body);
  2154. if (!_enter_indent_block(cf_for->body)) {
  2155. _set_error("Expected indented block after 'for'");
  2156. p_block->end_line = tokenizer->get_token_line();
  2157. return;
  2158. }
  2159. current_block = cf_for->body;
  2160. // this is for checking variable for redefining
  2161. // inside this _parse_block
  2162. cf_for->body->variables.push_back(id->name);
  2163. cf_for->body->variable_lines.push_back(id->line);
  2164. _parse_block(cf_for->body, p_static);
  2165. cf_for->body->variables.remove(0);
  2166. cf_for->body->variable_lines.remove(0);
  2167. current_block = p_block;
  2168. if (error_set)
  2169. return;
  2170. p_block->statements.push_back(cf_for);
  2171. } break;
  2172. case GDTokenizer::TK_CF_CONTINUE: {
  2173. tokenizer->advance();
  2174. ControlFlowNode *cf_continue = alloc_node<ControlFlowNode>();
  2175. cf_continue->cf_type = ControlFlowNode::CF_CONTINUE;
  2176. p_block->statements.push_back(cf_continue);
  2177. if (!_end_statement()) {
  2178. _set_error("Expected end of statement (continue)");
  2179. return;
  2180. }
  2181. } break;
  2182. case GDTokenizer::TK_CF_BREAK: {
  2183. tokenizer->advance();
  2184. ControlFlowNode *cf_break = alloc_node<ControlFlowNode>();
  2185. cf_break->cf_type = ControlFlowNode::CF_BREAK;
  2186. p_block->statements.push_back(cf_break);
  2187. if (!_end_statement()) {
  2188. _set_error("Expected end of statement (break)");
  2189. return;
  2190. }
  2191. } break;
  2192. case GDTokenizer::TK_CF_RETURN: {
  2193. tokenizer->advance();
  2194. ControlFlowNode *cf_return = alloc_node<ControlFlowNode>();
  2195. cf_return->cf_type = ControlFlowNode::CF_RETURN;
  2196. if (tokenizer->get_token() == GDTokenizer::TK_SEMICOLON || tokenizer->get_token() == GDTokenizer::TK_NEWLINE || tokenizer->get_token() == GDTokenizer::TK_EOF) {
  2197. //expect end of statement
  2198. p_block->statements.push_back(cf_return);
  2199. if (!_end_statement()) {
  2200. return;
  2201. }
  2202. } else {
  2203. //expect expression
  2204. Node *retexpr = _parse_and_reduce_expression(p_block, p_static);
  2205. if (!retexpr) {
  2206. if (_recover_from_completion()) {
  2207. break;
  2208. }
  2209. return;
  2210. }
  2211. cf_return->arguments.push_back(retexpr);
  2212. p_block->statements.push_back(cf_return);
  2213. if (!_end_statement()) {
  2214. _set_error("Expected end of statement after return expression.");
  2215. return;
  2216. }
  2217. }
  2218. } break;
  2219. case GDTokenizer::TK_CF_MATCH: {
  2220. tokenizer->advance();
  2221. MatchNode *match_node = alloc_node<MatchNode>();
  2222. Node *val_to_match = _parse_and_reduce_expression(p_block, p_static);
  2223. if (!val_to_match) {
  2224. if (_recover_from_completion()) {
  2225. break;
  2226. }
  2227. return;
  2228. }
  2229. match_node->val_to_match = val_to_match;
  2230. if (!_enter_indent_block()) {
  2231. _set_error("Expected indented pattern matching block after 'match'");
  2232. return;
  2233. }
  2234. BlockNode *compiled_branches = alloc_node<BlockNode>();
  2235. compiled_branches->parent_block = p_block;
  2236. compiled_branches->parent_class = p_block->parent_class;
  2237. p_block->sub_blocks.push_back(compiled_branches);
  2238. _parse_pattern_block(compiled_branches, match_node->branches, p_static);
  2239. _transform_match_statment(compiled_branches, match_node);
  2240. ControlFlowNode *match_cf_node = alloc_node<ControlFlowNode>();
  2241. match_cf_node->cf_type = ControlFlowNode::CF_MATCH;
  2242. match_cf_node->match = match_node;
  2243. p_block->statements.push_back(match_cf_node);
  2244. _end_statement();
  2245. } break;
  2246. case GDTokenizer::TK_PR_ASSERT: {
  2247. tokenizer->advance();
  2248. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2249. if (!condition) {
  2250. if (_recover_from_completion()) {
  2251. break;
  2252. }
  2253. return;
  2254. }
  2255. AssertNode *an = alloc_node<AssertNode>();
  2256. an->condition = condition;
  2257. p_block->statements.push_back(an);
  2258. if (!_end_statement()) {
  2259. _set_error("Expected end of statement after assert.");
  2260. return;
  2261. }
  2262. } break;
  2263. case GDTokenizer::TK_PR_BREAKPOINT: {
  2264. tokenizer->advance();
  2265. BreakpointNode *bn = alloc_node<BreakpointNode>();
  2266. p_block->statements.push_back(bn);
  2267. if (!_end_statement()) {
  2268. _set_error("Expected end of statement after breakpoint.");
  2269. return;
  2270. }
  2271. } break;
  2272. default: {
  2273. Node *expression = _parse_and_reduce_expression(p_block, p_static, false, true);
  2274. if (!expression) {
  2275. if (_recover_from_completion()) {
  2276. break;
  2277. }
  2278. return;
  2279. }
  2280. p_block->statements.push_back(expression);
  2281. if (!_end_statement()) {
  2282. _set_error("Expected end of statement after expression.");
  2283. return;
  2284. }
  2285. } break;
  2286. /*
  2287. case GDTokenizer::TK_CF_LOCAL: {
  2288. if (tokenizer->get_token(1)!=GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE ) {
  2289. _set_error("Expected ';' or <NewLine>.");
  2290. }
  2291. tokenizer->advance();
  2292. } break;
  2293. */
  2294. }
  2295. }
  2296. }
  2297. bool GDParser::_parse_newline() {
  2298. if (tokenizer->get_token(1) != GDTokenizer::TK_EOF && tokenizer->get_token(1) != GDTokenizer::TK_NEWLINE) {
  2299. int indent = tokenizer->get_token_line_indent();
  2300. int current_indent = tab_level.back()->get();
  2301. if (indent > current_indent) {
  2302. _set_error("Unexpected indent.");
  2303. return false;
  2304. }
  2305. if (indent < current_indent) {
  2306. while (indent < current_indent) {
  2307. //exit block
  2308. if (tab_level.size() == 1) {
  2309. _set_error("Invalid indent. BUG?");
  2310. return false;
  2311. }
  2312. tab_level.pop_back();
  2313. if (tab_level.back()->get() < indent) {
  2314. _set_error("Unindent does not match any outer indentation level.");
  2315. return false;
  2316. }
  2317. current_indent = tab_level.back()->get();
  2318. }
  2319. tokenizer->advance();
  2320. return false;
  2321. }
  2322. }
  2323. tokenizer->advance();
  2324. return true;
  2325. }
  2326. void GDParser::_parse_extends(ClassNode *p_class) {
  2327. if (p_class->extends_used) {
  2328. _set_error("'extends' already used for this class.");
  2329. return;
  2330. }
  2331. if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty()) {
  2332. _set_error("'extends' must be used before anything else.");
  2333. return;
  2334. }
  2335. p_class->extends_used = true;
  2336. tokenizer->advance();
  2337. if (tokenizer->get_token() == GDTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token_type() == Variant::OBJECT) {
  2338. p_class->extends_class.push_back(Variant::get_type_name(Variant::OBJECT));
  2339. tokenizer->advance();
  2340. return;
  2341. }
  2342. // see if inheritance happens from a file
  2343. if (tokenizer->get_token() == GDTokenizer::TK_CONSTANT) {
  2344. Variant constant = tokenizer->get_token_constant();
  2345. if (constant.get_type() != Variant::STRING) {
  2346. _set_error("'extends' constant must be a string.");
  2347. return;
  2348. }
  2349. p_class->extends_file = constant;
  2350. tokenizer->advance();
  2351. if (tokenizer->get_token() != GDTokenizer::TK_PERIOD) {
  2352. return;
  2353. } else
  2354. tokenizer->advance();
  2355. }
  2356. while (true) {
  2357. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2358. _set_error("Invalid 'extends' syntax, expected string constant (path) and/or identifier (parent class).");
  2359. return;
  2360. }
  2361. StringName identifier = tokenizer->get_token_identifier();
  2362. p_class->extends_class.push_back(identifier);
  2363. tokenizer->advance(1);
  2364. if (tokenizer->get_token() != GDTokenizer::TK_PERIOD)
  2365. return;
  2366. }
  2367. }
  2368. void GDParser::_parse_class(ClassNode *p_class) {
  2369. int indent_level = tab_level.back()->get();
  2370. while (true) {
  2371. GDTokenizer::Token token = tokenizer->get_token();
  2372. if (error_set)
  2373. return;
  2374. if (indent_level > tab_level.back()->get()) {
  2375. p_class->end_line = tokenizer->get_token_line();
  2376. return; //go back a level
  2377. }
  2378. switch (token) {
  2379. case GDTokenizer::TK_EOF:
  2380. p_class->end_line = tokenizer->get_token_line();
  2381. case GDTokenizer::TK_ERROR: {
  2382. return; //go back
  2383. //end of file!
  2384. } break;
  2385. case GDTokenizer::TK_NEWLINE: {
  2386. if (!_parse_newline()) {
  2387. if (!error_set) {
  2388. p_class->end_line = tokenizer->get_token_line();
  2389. }
  2390. return;
  2391. }
  2392. } break;
  2393. case GDTokenizer::TK_PR_EXTENDS: {
  2394. _parse_extends(p_class);
  2395. if (error_set)
  2396. return;
  2397. if (!_end_statement()) {
  2398. _set_error("Expected end of statement after extends");
  2399. return;
  2400. }
  2401. } break;
  2402. case GDTokenizer::TK_PR_TOOL: {
  2403. if (p_class->tool) {
  2404. _set_error("tool used more than once");
  2405. return;
  2406. }
  2407. p_class->tool = true;
  2408. tokenizer->advance();
  2409. } break;
  2410. case GDTokenizer::TK_PR_CLASS: {
  2411. //class inside class :D
  2412. StringName name;
  2413. StringName extends;
  2414. if (tokenizer->get_token(1) != GDTokenizer::TK_IDENTIFIER) {
  2415. _set_error("'class' syntax: 'class <Name>:' or 'class <Name> extends <BaseClass>:'");
  2416. return;
  2417. }
  2418. name = tokenizer->get_token_identifier(1);
  2419. tokenizer->advance(2);
  2420. ClassNode *newclass = alloc_node<ClassNode>();
  2421. newclass->initializer = alloc_node<BlockNode>();
  2422. newclass->initializer->parent_class = newclass;
  2423. newclass->ready = alloc_node<BlockNode>();
  2424. newclass->ready->parent_class = newclass;
  2425. newclass->name = name;
  2426. newclass->owner = p_class;
  2427. p_class->subclasses.push_back(newclass);
  2428. if (tokenizer->get_token() == GDTokenizer::TK_PR_EXTENDS) {
  2429. _parse_extends(newclass);
  2430. if (error_set)
  2431. return;
  2432. }
  2433. if (!_enter_indent_block()) {
  2434. _set_error("Indented block expected.");
  2435. return;
  2436. }
  2437. current_class = newclass;
  2438. _parse_class(newclass);
  2439. current_class = p_class;
  2440. } break;
  2441. /* this is for functions....
  2442. case GDTokenizer::TK_CF_PASS: {
  2443. tokenizer->advance(1);
  2444. } break;
  2445. */
  2446. case GDTokenizer::TK_PR_STATIC: {
  2447. tokenizer->advance();
  2448. if (tokenizer->get_token() != GDTokenizer::TK_PR_FUNCTION) {
  2449. _set_error("Expected 'func'.");
  2450. return;
  2451. }
  2452. }; //fallthrough to function
  2453. case GDTokenizer::TK_PR_FUNCTION: {
  2454. bool _static = false;
  2455. pending_newline = -1;
  2456. if (tokenizer->get_token(-1) == GDTokenizer::TK_PR_STATIC) {
  2457. _static = true;
  2458. }
  2459. tokenizer->advance();
  2460. StringName name;
  2461. if (_get_completable_identifier(COMPLETION_VIRTUAL_FUNC, name)) {
  2462. }
  2463. if (name == StringName()) {
  2464. _set_error("Expected identifier after 'func' (syntax: 'func <identifier>([arguments]):' ).");
  2465. return;
  2466. }
  2467. for (int i = 0; i < p_class->functions.size(); i++) {
  2468. if (p_class->functions[i]->name == name) {
  2469. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->functions[i]->line) + ").");
  2470. }
  2471. }
  2472. for (int i = 0; i < p_class->static_functions.size(); i++) {
  2473. if (p_class->static_functions[i]->name == name) {
  2474. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->static_functions[i]->line) + ").");
  2475. }
  2476. }
  2477. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_OPEN) {
  2478. _set_error("Expected '(' after identifier (syntax: 'func <identifier>([arguments]):' ).");
  2479. return;
  2480. }
  2481. tokenizer->advance();
  2482. Vector<StringName> arguments;
  2483. Vector<Node *> default_values;
  2484. int fnline = tokenizer->get_token_line();
  2485. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2486. //has arguments
  2487. bool defaulting = false;
  2488. while (true) {
  2489. if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  2490. tokenizer->advance();
  2491. continue;
  2492. }
  2493. if (tokenizer->get_token() == GDTokenizer::TK_PR_VAR) {
  2494. tokenizer->advance(); //var before the identifier is allowed
  2495. }
  2496. if (!tokenizer->is_token_literal(0, true)) {
  2497. _set_error("Expected identifier for argument.");
  2498. return;
  2499. }
  2500. StringName argname = tokenizer->get_token_identifier();
  2501. arguments.push_back(argname);
  2502. tokenizer->advance();
  2503. if (defaulting && tokenizer->get_token() != GDTokenizer::TK_OP_ASSIGN) {
  2504. _set_error("Default parameter expected.");
  2505. return;
  2506. }
  2507. //tokenizer->advance();
  2508. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  2509. defaulting = true;
  2510. tokenizer->advance(1);
  2511. Node *defval = _parse_and_reduce_expression(p_class, _static);
  2512. if (!defval || error_set)
  2513. return;
  2514. OperatorNode *on = alloc_node<OperatorNode>();
  2515. on->op = OperatorNode::OP_ASSIGN;
  2516. IdentifierNode *in = alloc_node<IdentifierNode>();
  2517. in->name = argname;
  2518. on->arguments.push_back(in);
  2519. on->arguments.push_back(defval);
  2520. /* no ..
  2521. if (defval->type!=Node::TYPE_CONSTANT) {
  2522. _set_error("default argument must be constant");
  2523. }
  2524. */
  2525. default_values.push_back(on);
  2526. }
  2527. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  2528. tokenizer->advance();
  2529. }
  2530. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2531. tokenizer->advance();
  2532. continue;
  2533. } else if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2534. _set_error("Expected ',' or ')'.");
  2535. return;
  2536. }
  2537. break;
  2538. }
  2539. }
  2540. tokenizer->advance();
  2541. BlockNode *block = alloc_node<BlockNode>();
  2542. block->parent_class = p_class;
  2543. if (name == "_init") {
  2544. if (p_class->extends_used) {
  2545. OperatorNode *cparent = alloc_node<OperatorNode>();
  2546. cparent->op = OperatorNode::OP_PARENT_CALL;
  2547. block->statements.push_back(cparent);
  2548. IdentifierNode *id = alloc_node<IdentifierNode>();
  2549. id->name = "_init";
  2550. cparent->arguments.push_back(id);
  2551. if (tokenizer->get_token() == GDTokenizer::TK_PERIOD) {
  2552. tokenizer->advance();
  2553. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_OPEN) {
  2554. _set_error("expected '(' for parent constructor arguments.");
  2555. }
  2556. tokenizer->advance();
  2557. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2558. //has arguments
  2559. parenthesis++;
  2560. while (true) {
  2561. Node *arg = _parse_and_reduce_expression(p_class, _static);
  2562. cparent->arguments.push_back(arg);
  2563. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2564. tokenizer->advance();
  2565. continue;
  2566. } else if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2567. _set_error("Expected ',' or ')'.");
  2568. return;
  2569. }
  2570. break;
  2571. }
  2572. parenthesis--;
  2573. }
  2574. tokenizer->advance();
  2575. }
  2576. } else {
  2577. if (tokenizer->get_token() == GDTokenizer::TK_PERIOD) {
  2578. _set_error("Parent constructor call found for a class without inheritance.");
  2579. return;
  2580. }
  2581. }
  2582. }
  2583. if (!_enter_indent_block(block)) {
  2584. _set_error("Indented block expected.");
  2585. return;
  2586. }
  2587. FunctionNode *function = alloc_node<FunctionNode>();
  2588. function->name = name;
  2589. function->arguments = arguments;
  2590. function->default_values = default_values;
  2591. function->_static = _static;
  2592. function->line = fnline;
  2593. function->rpc_mode = rpc_mode;
  2594. rpc_mode = ScriptInstance::RPC_MODE_DISABLED;
  2595. if (_static)
  2596. p_class->static_functions.push_back(function);
  2597. else
  2598. p_class->functions.push_back(function);
  2599. current_function = function;
  2600. function->body = block;
  2601. current_block = block;
  2602. _parse_block(block, _static);
  2603. current_block = NULL;
  2604. //arguments
  2605. } break;
  2606. case GDTokenizer::TK_PR_SIGNAL: {
  2607. tokenizer->advance();
  2608. if (!tokenizer->is_token_literal()) {
  2609. _set_error("Expected identifier after 'signal'.");
  2610. return;
  2611. }
  2612. ClassNode::Signal sig;
  2613. sig.name = tokenizer->get_token_identifier();
  2614. tokenizer->advance();
  2615. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_OPEN) {
  2616. tokenizer->advance();
  2617. while (true) {
  2618. if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  2619. tokenizer->advance();
  2620. continue;
  2621. }
  2622. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2623. tokenizer->advance();
  2624. break;
  2625. }
  2626. if (!tokenizer->is_token_literal(0, true)) {
  2627. _set_error("Expected identifier in signal argument.");
  2628. return;
  2629. }
  2630. sig.arguments.push_back(tokenizer->get_token_identifier());
  2631. tokenizer->advance();
  2632. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  2633. tokenizer->advance();
  2634. }
  2635. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2636. tokenizer->advance();
  2637. } else if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2638. _set_error("Expected ',' or ')' after signal parameter identifier.");
  2639. return;
  2640. }
  2641. }
  2642. }
  2643. p_class->_signals.push_back(sig);
  2644. if (!_end_statement()) {
  2645. _set_error("Expected end of statement (signal)");
  2646. return;
  2647. }
  2648. } break;
  2649. case GDTokenizer::TK_PR_EXPORT: {
  2650. tokenizer->advance();
  2651. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_OPEN) {
  2652. tokenizer->advance();
  2653. if (tokenizer->get_token() == GDTokenizer::TK_BUILT_IN_TYPE) {
  2654. Variant::Type type = tokenizer->get_token_type();
  2655. if (type == Variant::NIL) {
  2656. _set_error("Can't export null type.");
  2657. return;
  2658. }
  2659. current_export.type = type;
  2660. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  2661. tokenizer->advance();
  2662. String hint_prefix = "";
  2663. if (type == Variant::ARRAY && tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2664. tokenizer->advance();
  2665. while (tokenizer->get_token() == GDTokenizer::TK_BUILT_IN_TYPE) {
  2666. type = tokenizer->get_token_type();
  2667. tokenizer->advance();
  2668. if (type == Variant::ARRAY) {
  2669. hint_prefix += itos(Variant::ARRAY) + ":";
  2670. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2671. tokenizer->advance();
  2672. }
  2673. } else {
  2674. hint_prefix += itos(type);
  2675. break;
  2676. }
  2677. }
  2678. }
  2679. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2680. // hint expected next!
  2681. tokenizer->advance();
  2682. switch (type) {
  2683. case Variant::INT: {
  2684. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  2685. //current_export.hint=PROPERTY_HINT_ALL_FLAGS;
  2686. tokenizer->advance();
  2687. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2688. break;
  2689. }
  2690. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2691. _set_error("Expected ')' or ',' in bit flags hint.");
  2692. return;
  2693. }
  2694. current_export.hint = PROPERTY_HINT_FLAGS;
  2695. tokenizer->advance();
  2696. bool first = true;
  2697. while (true) {
  2698. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2699. current_export = PropertyInfo();
  2700. _set_error("Expected a string constant in named bit flags hint.");
  2701. return;
  2702. }
  2703. String c = tokenizer->get_token_constant();
  2704. if (!first)
  2705. current_export.hint_string += ",";
  2706. else
  2707. first = false;
  2708. current_export.hint_string += c.xml_escape();
  2709. tokenizer->advance();
  2710. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2711. break;
  2712. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2713. current_export = PropertyInfo();
  2714. _set_error("Expected ')' or ',' in named bit flags hint.");
  2715. return;
  2716. }
  2717. tokenizer->advance();
  2718. }
  2719. break;
  2720. }
  2721. if (tokenizer->get_token() == GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  2722. //enumeration
  2723. current_export.hint = PROPERTY_HINT_ENUM;
  2724. bool first = true;
  2725. while (true) {
  2726. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2727. current_export = PropertyInfo();
  2728. _set_error("Expected a string constant in enumeration hint.");
  2729. return;
  2730. }
  2731. String c = tokenizer->get_token_constant();
  2732. if (!first)
  2733. current_export.hint_string += ",";
  2734. else
  2735. first = false;
  2736. current_export.hint_string += c.xml_escape();
  2737. tokenizer->advance();
  2738. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2739. break;
  2740. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2741. current_export = PropertyInfo();
  2742. _set_error("Expected ')' or ',' in enumeration hint.");
  2743. return;
  2744. }
  2745. tokenizer->advance();
  2746. }
  2747. break;
  2748. }
  2749. }; //fallthrough to use the same
  2750. case Variant::REAL: {
  2751. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EASE") {
  2752. current_export.hint = PROPERTY_HINT_EXP_EASING;
  2753. tokenizer->advance();
  2754. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2755. _set_error("Expected ')' in hint.");
  2756. return;
  2757. }
  2758. break;
  2759. }
  2760. // range
  2761. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EXP") {
  2762. current_export.hint = PROPERTY_HINT_EXP_RANGE;
  2763. tokenizer->advance();
  2764. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2765. break;
  2766. else if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2767. _set_error("Expected ')' or ',' in exponential range hint.");
  2768. return;
  2769. }
  2770. tokenizer->advance();
  2771. } else
  2772. current_export.hint = PROPERTY_HINT_RANGE;
  2773. float sign = 1.0;
  2774. if (tokenizer->get_token() == GDTokenizer::TK_OP_SUB) {
  2775. sign = -1;
  2776. tokenizer->advance();
  2777. }
  2778. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2779. current_export = PropertyInfo();
  2780. _set_error("Expected a range in numeric hint.");
  2781. return;
  2782. }
  2783. current_export.hint_string = rtos(sign * double(tokenizer->get_token_constant()));
  2784. tokenizer->advance();
  2785. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2786. current_export.hint_string = "0," + current_export.hint_string;
  2787. break;
  2788. }
  2789. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2790. current_export = PropertyInfo();
  2791. _set_error("Expected ',' or ')' in numeric range hint.");
  2792. return;
  2793. }
  2794. tokenizer->advance();
  2795. sign = 1.0;
  2796. if (tokenizer->get_token() == GDTokenizer::TK_OP_SUB) {
  2797. sign = -1;
  2798. tokenizer->advance();
  2799. }
  2800. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2801. current_export = PropertyInfo();
  2802. _set_error("Expected a number as upper bound in numeric range hint.");
  2803. return;
  2804. }
  2805. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  2806. tokenizer->advance();
  2807. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2808. break;
  2809. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2810. current_export = PropertyInfo();
  2811. _set_error("Expected ',' or ')' in numeric range hint.");
  2812. return;
  2813. }
  2814. tokenizer->advance();
  2815. sign = 1.0;
  2816. if (tokenizer->get_token() == GDTokenizer::TK_OP_SUB) {
  2817. sign = -1;
  2818. tokenizer->advance();
  2819. }
  2820. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2821. current_export = PropertyInfo();
  2822. _set_error("Expected a number as step in numeric range hint.");
  2823. return;
  2824. }
  2825. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  2826. tokenizer->advance();
  2827. } break;
  2828. case Variant::STRING: {
  2829. if (tokenizer->get_token() == GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  2830. //enumeration
  2831. current_export.hint = PROPERTY_HINT_ENUM;
  2832. bool first = true;
  2833. while (true) {
  2834. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2835. current_export = PropertyInfo();
  2836. _set_error("Expected a string constant in enumeration hint.");
  2837. return;
  2838. }
  2839. String c = tokenizer->get_token_constant();
  2840. if (!first)
  2841. current_export.hint_string += ",";
  2842. else
  2843. first = false;
  2844. current_export.hint_string += c.xml_escape();
  2845. tokenizer->advance();
  2846. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2847. break;
  2848. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2849. current_export = PropertyInfo();
  2850. _set_error("Expected ')' or ',' in enumeration hint.");
  2851. return;
  2852. }
  2853. tokenizer->advance();
  2854. }
  2855. break;
  2856. }
  2857. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "DIR") {
  2858. tokenizer->advance();
  2859. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2860. current_export.hint = PROPERTY_HINT_DIR;
  2861. else if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2862. tokenizer->advance();
  2863. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier() == "GLOBAL")) {
  2864. _set_error("Expected 'GLOBAL' after comma in directory hint.");
  2865. return;
  2866. }
  2867. if (!p_class->tool) {
  2868. _set_error("Global filesystem hints may only be used in tool scripts.");
  2869. return;
  2870. }
  2871. current_export.hint = PROPERTY_HINT_GLOBAL_DIR;
  2872. tokenizer->advance();
  2873. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2874. _set_error("Expected ')' in hint.");
  2875. return;
  2876. }
  2877. } else {
  2878. _set_error("Expected ')' or ',' in hint.");
  2879. return;
  2880. }
  2881. break;
  2882. }
  2883. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FILE") {
  2884. current_export.hint = PROPERTY_HINT_FILE;
  2885. tokenizer->advance();
  2886. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2887. tokenizer->advance();
  2888. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "GLOBAL") {
  2889. if (!p_class->tool) {
  2890. _set_error("Global filesystem hints may only be used in tool scripts.");
  2891. return;
  2892. }
  2893. current_export.hint = PROPERTY_HINT_GLOBAL_FILE;
  2894. tokenizer->advance();
  2895. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2896. break;
  2897. else if (tokenizer->get_token() == GDTokenizer::TK_COMMA)
  2898. tokenizer->advance();
  2899. else {
  2900. _set_error("Expected ')' or ',' in hint.");
  2901. return;
  2902. }
  2903. }
  2904. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2905. if (current_export.hint == PROPERTY_HINT_GLOBAL_FILE)
  2906. _set_error("Expected string constant with filter");
  2907. else
  2908. _set_error("Expected 'GLOBAL' or string constant with filter");
  2909. return;
  2910. }
  2911. current_export.hint_string = tokenizer->get_token_constant();
  2912. tokenizer->advance();
  2913. }
  2914. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2915. _set_error("Expected ')' in hint.");
  2916. return;
  2917. }
  2918. break;
  2919. }
  2920. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "MULTILINE") {
  2921. current_export.hint = PROPERTY_HINT_MULTILINE_TEXT;
  2922. tokenizer->advance();
  2923. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2924. _set_error("Expected ')' in hint.");
  2925. return;
  2926. }
  2927. break;
  2928. }
  2929. } break;
  2930. case Variant::COLOR: {
  2931. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2932. current_export = PropertyInfo();
  2933. _set_error("Color type hint expects RGB or RGBA as hints");
  2934. return;
  2935. }
  2936. String identifier = tokenizer->get_token_identifier();
  2937. if (identifier == "RGB") {
  2938. current_export.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  2939. } else if (identifier == "RGBA") {
  2940. //none
  2941. } else {
  2942. current_export = PropertyInfo();
  2943. _set_error("Color type hint expects RGB or RGBA as hints");
  2944. return;
  2945. }
  2946. tokenizer->advance();
  2947. } break;
  2948. default: {
  2949. current_export = PropertyInfo();
  2950. _set_error("Type '" + Variant::get_type_name(type) + "' can't take hints.");
  2951. return;
  2952. } break;
  2953. }
  2954. }
  2955. if (current_export.type == Variant::ARRAY && !hint_prefix.empty()) {
  2956. if (current_export.hint) {
  2957. hint_prefix += "/" + itos(current_export.hint);
  2958. }
  2959. current_export.hint_string = hint_prefix + ":" + current_export.hint_string;
  2960. current_export.hint = PROPERTY_HINT_NONE;
  2961. }
  2962. } else if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER) {
  2963. String identifier = tokenizer->get_token_identifier();
  2964. if (!ClassDB::is_parent_class(identifier, "Resource")) {
  2965. current_export = PropertyInfo();
  2966. _set_error("Export hint not a type or resource.");
  2967. }
  2968. current_export.type = Variant::OBJECT;
  2969. current_export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  2970. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  2971. current_export.hint_string = identifier;
  2972. tokenizer->advance();
  2973. }
  2974. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2975. current_export = PropertyInfo();
  2976. _set_error("Expected ')' or ',' after export hint.");
  2977. return;
  2978. }
  2979. tokenizer->advance();
  2980. }
  2981. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR && tokenizer->get_token() != GDTokenizer::TK_PR_ONREADY && tokenizer->get_token() != GDTokenizer::TK_PR_REMOTE && tokenizer->get_token() != GDTokenizer::TK_PR_MASTER && tokenizer->get_token() != GDTokenizer::TK_PR_SLAVE && tokenizer->get_token() != GDTokenizer::TK_PR_SYNC) {
  2982. current_export = PropertyInfo();
  2983. _set_error("Expected 'var', 'onready', 'remote', 'master', 'slave' or 'sync'.");
  2984. return;
  2985. }
  2986. continue;
  2987. } break;
  2988. case GDTokenizer::TK_PR_ONREADY: {
  2989. //may be fallthrough from export, ignore if so
  2990. tokenizer->advance();
  2991. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR) {
  2992. _set_error("Expected 'var'.");
  2993. return;
  2994. }
  2995. continue;
  2996. } break;
  2997. case GDTokenizer::TK_PR_REMOTE: {
  2998. //may be fallthrough from export, ignore if so
  2999. tokenizer->advance();
  3000. if (current_export.type) {
  3001. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR) {
  3002. _set_error("Expected 'var'.");
  3003. return;
  3004. }
  3005. } else {
  3006. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR && tokenizer->get_token() != GDTokenizer::TK_PR_FUNCTION) {
  3007. _set_error("Expected 'var' or 'func'.");
  3008. return;
  3009. }
  3010. }
  3011. rpc_mode = ScriptInstance::RPC_MODE_REMOTE;
  3012. continue;
  3013. } break;
  3014. case GDTokenizer::TK_PR_MASTER: {
  3015. //may be fallthrough from export, ignore if so
  3016. tokenizer->advance();
  3017. if (current_export.type) {
  3018. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR) {
  3019. _set_error("Expected 'var'.");
  3020. return;
  3021. }
  3022. } else {
  3023. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR && tokenizer->get_token() != GDTokenizer::TK_PR_FUNCTION) {
  3024. _set_error("Expected 'var' or 'func'.");
  3025. return;
  3026. }
  3027. }
  3028. rpc_mode = ScriptInstance::RPC_MODE_MASTER;
  3029. continue;
  3030. } break;
  3031. case GDTokenizer::TK_PR_SLAVE: {
  3032. //may be fallthrough from export, ignore if so
  3033. tokenizer->advance();
  3034. if (current_export.type) {
  3035. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR) {
  3036. _set_error("Expected 'var'.");
  3037. return;
  3038. }
  3039. } else {
  3040. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR && tokenizer->get_token() != GDTokenizer::TK_PR_FUNCTION) {
  3041. _set_error("Expected 'var' or 'func'.");
  3042. return;
  3043. }
  3044. }
  3045. rpc_mode = ScriptInstance::RPC_MODE_SLAVE;
  3046. continue;
  3047. } break;
  3048. case GDTokenizer::TK_PR_SYNC: {
  3049. //may be fallthrough from export, ignore if so
  3050. tokenizer->advance();
  3051. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR && tokenizer->get_token() != GDTokenizer::TK_PR_FUNCTION) {
  3052. if (current_export.type)
  3053. _set_error("Expected 'var'.");
  3054. else
  3055. _set_error("Expected 'var' or 'func'.");
  3056. return;
  3057. }
  3058. rpc_mode = ScriptInstance::RPC_MODE_SYNC;
  3059. continue;
  3060. } break;
  3061. case GDTokenizer::TK_PR_VAR: {
  3062. //variale declaration and (eventual) initialization
  3063. ClassNode::Member member;
  3064. bool autoexport = tokenizer->get_token(-1) == GDTokenizer::TK_PR_EXPORT;
  3065. if (current_export.type != Variant::NIL) {
  3066. member._export = current_export;
  3067. current_export = PropertyInfo();
  3068. }
  3069. bool onready = tokenizer->get_token(-1) == GDTokenizer::TK_PR_ONREADY;
  3070. tokenizer->advance();
  3071. if (!tokenizer->is_token_literal(0, true)) {
  3072. _set_error("Expected identifier for member variable name.");
  3073. return;
  3074. }
  3075. member.identifier = tokenizer->get_token_literal();
  3076. member.expression = NULL;
  3077. member._export.name = member.identifier;
  3078. member.line = tokenizer->get_token_line();
  3079. member.rpc_mode = rpc_mode;
  3080. tokenizer->advance();
  3081. rpc_mode = ScriptInstance::RPC_MODE_DISABLED;
  3082. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  3083. #ifdef DEBUG_ENABLED
  3084. int line = tokenizer->get_token_line();
  3085. #endif
  3086. tokenizer->advance();
  3087. Node *subexpr = _parse_and_reduce_expression(p_class, false, autoexport);
  3088. if (!subexpr) {
  3089. if (_recover_from_completion()) {
  3090. break;
  3091. }
  3092. return;
  3093. }
  3094. //discourage common error
  3095. if (!onready && subexpr->type == Node::TYPE_OPERATOR) {
  3096. OperatorNode *op = static_cast<OperatorNode *>(subexpr);
  3097. if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_SELF && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  3098. IdentifierNode *id = static_cast<IdentifierNode *>(op->arguments[1]);
  3099. if (id->name == "get_node") {
  3100. _set_error("Use 'onready var " + String(member.identifier) + " = get_node(..)' instead");
  3101. return;
  3102. }
  3103. }
  3104. }
  3105. member.expression = subexpr;
  3106. if (autoexport) {
  3107. if (1) /*(subexpr->type==Node::TYPE_ARRAY) {
  3108. member._export.type=Variant::ARRAY;
  3109. } else if (subexpr->type==Node::TYPE_DICTIONARY) {
  3110. member._export.type=Variant::DICTIONARY;
  3111. } else*/ {
  3112. if (subexpr->type != Node::TYPE_CONSTANT) {
  3113. _set_error("Type-less export needs a constant expression assigned to infer type.");
  3114. return;
  3115. }
  3116. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  3117. if (cn->value.get_type() == Variant::NIL) {
  3118. _set_error("Can't accept a null constant expression for infering export type.");
  3119. return;
  3120. }
  3121. member._export.type = cn->value.get_type();
  3122. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3123. if (cn->value.get_type() == Variant::OBJECT) {
  3124. Object *obj = cn->value;
  3125. Resource *res = Object::cast_to<Resource>(obj);
  3126. if (res == NULL) {
  3127. _set_error("Exported constant not a type or resource.");
  3128. return;
  3129. }
  3130. member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3131. member._export.hint_string = res->get_class();
  3132. }
  3133. }
  3134. }
  3135. #ifdef TOOLS_ENABLED
  3136. if (subexpr->type == Node::TYPE_CONSTANT && member._export.type != Variant::NIL) {
  3137. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  3138. if (cn->value.get_type() != Variant::NIL) {
  3139. member.default_value = cn->value;
  3140. }
  3141. }
  3142. #endif
  3143. IdentifierNode *id = alloc_node<IdentifierNode>();
  3144. id->name = member.identifier;
  3145. OperatorNode *op = alloc_node<OperatorNode>();
  3146. op->op = OperatorNode::OP_INIT_ASSIGN;
  3147. op->arguments.push_back(id);
  3148. op->arguments.push_back(subexpr);
  3149. #ifdef DEBUG_ENABLED
  3150. NewLineNode *nl = alloc_node<NewLineNode>();
  3151. nl->line = line;
  3152. if (onready)
  3153. p_class->ready->statements.push_back(nl);
  3154. else
  3155. p_class->initializer->statements.push_back(nl);
  3156. #endif
  3157. if (onready)
  3158. p_class->ready->statements.push_back(op);
  3159. else
  3160. p_class->initializer->statements.push_back(op);
  3161. } else {
  3162. if (autoexport) {
  3163. _set_error("Type-less export needs a constant expression assigned to infer type.");
  3164. return;
  3165. }
  3166. }
  3167. if (tokenizer->get_token() == GDTokenizer::TK_PR_SETGET) {
  3168. tokenizer->advance();
  3169. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  3170. //just comma means using only getter
  3171. if (!tokenizer->is_token_literal()) {
  3172. _set_error("Expected identifier for setter function after 'setget'.");
  3173. }
  3174. member.setter = tokenizer->get_token_literal();
  3175. tokenizer->advance();
  3176. }
  3177. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  3178. //there is a getter
  3179. tokenizer->advance();
  3180. if (!tokenizer->is_token_literal()) {
  3181. _set_error("Expected identifier for getter function after ','.");
  3182. }
  3183. member.getter = tokenizer->get_token_literal();
  3184. tokenizer->advance();
  3185. }
  3186. }
  3187. p_class->variables.push_back(member);
  3188. if (!_end_statement()) {
  3189. _set_error("Expected end of statement (continue)");
  3190. return;
  3191. }
  3192. } break;
  3193. case GDTokenizer::TK_PR_CONST: {
  3194. //variale declaration and (eventual) initialization
  3195. ClassNode::Constant constant;
  3196. tokenizer->advance();
  3197. if (!tokenizer->is_token_literal(0, true)) {
  3198. _set_error("Expected name (identifier) for constant.");
  3199. return;
  3200. }
  3201. constant.identifier = tokenizer->get_token_literal();
  3202. tokenizer->advance();
  3203. if (tokenizer->get_token() != GDTokenizer::TK_OP_ASSIGN) {
  3204. _set_error("Constant expects assignment.");
  3205. return;
  3206. }
  3207. tokenizer->advance();
  3208. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3209. if (!subexpr) {
  3210. if (_recover_from_completion()) {
  3211. break;
  3212. }
  3213. return;
  3214. }
  3215. if (subexpr->type != Node::TYPE_CONSTANT) {
  3216. _set_error("Expected constant expression");
  3217. }
  3218. constant.expression = subexpr;
  3219. p_class->constant_expressions.push_back(constant);
  3220. if (!_end_statement()) {
  3221. _set_error("Expected end of statement (constant)");
  3222. return;
  3223. }
  3224. } break;
  3225. case GDTokenizer::TK_PR_ENUM: {
  3226. //mutiple constant declarations..
  3227. int last_assign = -1; // Incremented by 1 right before the assingment.
  3228. String enum_name;
  3229. Dictionary enum_dict;
  3230. tokenizer->advance();
  3231. if (tokenizer->is_token_literal(0, true)) {
  3232. enum_name = tokenizer->get_token_literal();
  3233. tokenizer->advance();
  3234. }
  3235. if (tokenizer->get_token() != GDTokenizer::TK_CURLY_BRACKET_OPEN) {
  3236. _set_error("Expected '{' in enum declaration");
  3237. return;
  3238. }
  3239. tokenizer->advance();
  3240. while (true) {
  3241. if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  3242. tokenizer->advance(); // Ignore newlines
  3243. } else if (tokenizer->get_token() == GDTokenizer::TK_CURLY_BRACKET_CLOSE) {
  3244. tokenizer->advance();
  3245. break; // End of enum
  3246. } else if (!tokenizer->is_token_literal(0, true)) {
  3247. if (tokenizer->get_token() == GDTokenizer::TK_EOF) {
  3248. _set_error("Unexpected end of file.");
  3249. } else {
  3250. _set_error(String("Unexpected ") + GDTokenizer::get_token_name(tokenizer->get_token()) + ", expected identifier");
  3251. }
  3252. return;
  3253. } else { // tokenizer->is_token_literal(0, true)
  3254. ClassNode::Constant constant;
  3255. constant.identifier = tokenizer->get_token_literal();
  3256. tokenizer->advance();
  3257. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  3258. tokenizer->advance();
  3259. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3260. if (!subexpr) {
  3261. if (_recover_from_completion()) {
  3262. break;
  3263. }
  3264. return;
  3265. }
  3266. if (subexpr->type != Node::TYPE_CONSTANT) {
  3267. _set_error("Expected constant expression");
  3268. }
  3269. const ConstantNode *subexpr_const = static_cast<const ConstantNode *>(subexpr);
  3270. if (subexpr_const->value.get_type() != Variant::INT) {
  3271. _set_error("Expected an int value for enum");
  3272. }
  3273. last_assign = subexpr_const->value;
  3274. constant.expression = subexpr;
  3275. } else {
  3276. last_assign = last_assign + 1;
  3277. ConstantNode *cn = alloc_node<ConstantNode>();
  3278. cn->value = last_assign;
  3279. constant.expression = cn;
  3280. }
  3281. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  3282. tokenizer->advance();
  3283. }
  3284. if (enum_name != "") {
  3285. const ConstantNode *cn = static_cast<const ConstantNode *>(constant.expression);
  3286. enum_dict[constant.identifier] = cn->value;
  3287. }
  3288. p_class->constant_expressions.push_back(constant);
  3289. }
  3290. }
  3291. if (enum_name != "") {
  3292. ClassNode::Constant enum_constant;
  3293. enum_constant.identifier = enum_name;
  3294. ConstantNode *cn = alloc_node<ConstantNode>();
  3295. cn->value = enum_dict;
  3296. enum_constant.expression = cn;
  3297. p_class->constant_expressions.push_back(enum_constant);
  3298. }
  3299. if (!_end_statement()) {
  3300. _set_error("Expected end of statement (enum)");
  3301. return;
  3302. }
  3303. } break;
  3304. case GDTokenizer::TK_CONSTANT: {
  3305. if (tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3306. tokenizer->advance();
  3307. // Ignore
  3308. } else {
  3309. _set_error(String() + "Unexpected constant of type: " + Variant::get_type_name(tokenizer->get_token_constant().get_type()));
  3310. return;
  3311. }
  3312. } break;
  3313. default: {
  3314. _set_error(String() + "Unexpected token: " + tokenizer->get_token_name(tokenizer->get_token()) + ":" + tokenizer->get_token_identifier());
  3315. return;
  3316. } break;
  3317. }
  3318. }
  3319. }
  3320. void GDParser::_set_error(const String &p_error, int p_line, int p_column) {
  3321. if (error_set)
  3322. return; //allow no further errors
  3323. error = p_error;
  3324. error_line = p_line < 0 ? tokenizer->get_token_line() : p_line;
  3325. error_column = p_column < 0 ? tokenizer->get_token_column() : p_column;
  3326. error_set = true;
  3327. }
  3328. String GDParser::get_error() const {
  3329. return error;
  3330. }
  3331. int GDParser::get_error_line() const {
  3332. return error_line;
  3333. }
  3334. int GDParser::get_error_column() const {
  3335. return error_column;
  3336. }
  3337. Error GDParser::_parse(const String &p_base_path) {
  3338. base_path = p_base_path;
  3339. clear();
  3340. //assume class
  3341. ClassNode *main_class = alloc_node<ClassNode>();
  3342. main_class->initializer = alloc_node<BlockNode>();
  3343. main_class->initializer->parent_class = main_class;
  3344. main_class->ready = alloc_node<BlockNode>();
  3345. main_class->ready->parent_class = main_class;
  3346. current_class = main_class;
  3347. _parse_class(main_class);
  3348. if (tokenizer->get_token() == GDTokenizer::TK_ERROR) {
  3349. error_set = false;
  3350. _set_error("Parse Error: " + tokenizer->get_token_error());
  3351. }
  3352. if (error_set) {
  3353. return ERR_PARSE_ERROR;
  3354. }
  3355. return OK;
  3356. }
  3357. Error GDParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path, const String &p_self_path) {
  3358. for_completion = false;
  3359. validating = false;
  3360. completion_type = COMPLETION_NONE;
  3361. completion_node = NULL;
  3362. completion_class = NULL;
  3363. completion_function = NULL;
  3364. completion_block = NULL;
  3365. completion_found = false;
  3366. current_block = NULL;
  3367. current_class = NULL;
  3368. current_function = NULL;
  3369. self_path = p_self_path;
  3370. GDTokenizerBuffer *tb = memnew(GDTokenizerBuffer);
  3371. tb->set_code_buffer(p_bytecode);
  3372. tokenizer = tb;
  3373. Error ret = _parse(p_base_path);
  3374. memdelete(tb);
  3375. tokenizer = NULL;
  3376. return ret;
  3377. }
  3378. 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) {
  3379. completion_type = COMPLETION_NONE;
  3380. completion_node = NULL;
  3381. completion_class = NULL;
  3382. completion_function = NULL;
  3383. completion_block = NULL;
  3384. completion_found = false;
  3385. current_block = NULL;
  3386. current_class = NULL;
  3387. current_function = NULL;
  3388. self_path = p_self_path;
  3389. GDTokenizerText *tt = memnew(GDTokenizerText);
  3390. tt->set_code(p_code);
  3391. validating = p_just_validate;
  3392. for_completion = p_for_completion;
  3393. tokenizer = tt;
  3394. Error ret = _parse(p_base_path);
  3395. memdelete(tt);
  3396. tokenizer = NULL;
  3397. return ret;
  3398. }
  3399. bool GDParser::is_tool_script() const {
  3400. return (head && head->type == Node::TYPE_CLASS && static_cast<const ClassNode *>(head)->tool);
  3401. }
  3402. const GDParser::Node *GDParser::get_parse_tree() const {
  3403. return head;
  3404. }
  3405. void GDParser::clear() {
  3406. while (list) {
  3407. Node *l = list;
  3408. list = list->next;
  3409. memdelete(l);
  3410. }
  3411. head = NULL;
  3412. list = NULL;
  3413. completion_type = COMPLETION_NONE;
  3414. completion_node = NULL;
  3415. completion_class = NULL;
  3416. completion_function = NULL;
  3417. completion_block = NULL;
  3418. current_block = NULL;
  3419. current_class = NULL;
  3420. completion_found = false;
  3421. rpc_mode = ScriptInstance::RPC_MODE_DISABLED;
  3422. current_function = NULL;
  3423. validating = false;
  3424. for_completion = false;
  3425. error_set = false;
  3426. tab_level.clear();
  3427. tab_level.push_back(0);
  3428. error_line = 0;
  3429. error_column = 0;
  3430. pending_newline = -1;
  3431. parenthesis = 0;
  3432. current_export.type = Variant::NIL;
  3433. error = "";
  3434. }
  3435. GDParser::CompletionType GDParser::get_completion_type() {
  3436. return completion_type;
  3437. }
  3438. StringName GDParser::get_completion_cursor() {
  3439. return completion_cursor;
  3440. }
  3441. int GDParser::get_completion_line() {
  3442. return completion_line;
  3443. }
  3444. Variant::Type GDParser::get_completion_built_in_constant() {
  3445. return completion_built_in_constant;
  3446. }
  3447. GDParser::Node *GDParser::get_completion_node() {
  3448. return completion_node;
  3449. }
  3450. GDParser::BlockNode *GDParser::get_completion_block() {
  3451. return completion_block;
  3452. }
  3453. GDParser::ClassNode *GDParser::get_completion_class() {
  3454. return completion_class;
  3455. }
  3456. GDParser::FunctionNode *GDParser::get_completion_function() {
  3457. return completion_function;
  3458. }
  3459. int GDParser::get_completion_argument_index() {
  3460. return completion_argument;
  3461. }
  3462. int GDParser::get_completion_identifier_is_function() {
  3463. return completion_ident_is_call;
  3464. }
  3465. GDParser::GDParser() {
  3466. head = NULL;
  3467. list = NULL;
  3468. tokenizer = NULL;
  3469. pending_newline = -1;
  3470. clear();
  3471. }
  3472. GDParser::~GDParser() {
  3473. clear();
  3474. }