gdscript_parser.cpp 144 KB

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