gdscript_parser.cpp 138 KB

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