gdscript_parser.cpp 134 KB

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