gd_parser.cpp 126 KB

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