gdscript_parser.cpp 132 KB

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