gd_parser.cpp 125 KB

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