gd_parser.cpp 120 KB

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