gdscript_parser.cpp 133 KB

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