gd_parser.cpp 126 KB

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