gd_parser.cpp 127 KB

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