gd_parser.cpp 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424
  1. /*************************************************************************/
  2. /* gd_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* 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 an 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 an 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 an 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_SUBSTRACT);
  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 an 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 (error_set) {
  1537. return NULL;
  1538. }
  1539. if (value->type == Node::TYPE_OPERATOR) {
  1540. // Maybe it's SomeEnum.VALUE
  1541. Node *current_value = value;
  1542. while (current_value->type == Node::TYPE_OPERATOR) {
  1543. OperatorNode *op_node = static_cast<OperatorNode *>(current_value);
  1544. if (op_node->op != OperatorNode::OP_INDEX_NAMED) {
  1545. _set_error("Invalid operator in pattern. Only index (`A.B`) is allowed");
  1546. return NULL;
  1547. }
  1548. current_value = op_node->arguments[0];
  1549. }
  1550. if (current_value->type != Node::TYPE_IDENTIFIER) {
  1551. _set_error("Only constant expression or variables allowed in a pattern");
  1552. return NULL;
  1553. }
  1554. } else if (value->type != Node::TYPE_IDENTIFIER && value->type != Node::TYPE_CONSTANT) {
  1555. _set_error("Only constant expressions or variables allowed in a pattern");
  1556. return NULL;
  1557. }
  1558. pattern->pt_type = PatternNode::PT_CONSTANT;
  1559. pattern->constant = value;
  1560. } break;
  1561. }
  1562. return pattern;
  1563. }
  1564. void GDParser::_parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static) {
  1565. int indent_level = tab_level.back()->get();
  1566. while (true) {
  1567. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE && _parse_newline())
  1568. ;
  1569. // GDTokenizer::Token token = tokenizer->get_token();
  1570. if (error_set)
  1571. return;
  1572. if (indent_level > tab_level.back()->get()) {
  1573. return; // go back a level
  1574. }
  1575. if (pending_newline != -1) {
  1576. pending_newline = -1;
  1577. }
  1578. PatternBranchNode *branch = alloc_node<PatternBranchNode>();
  1579. branch->patterns.push_back(_parse_pattern(p_static));
  1580. if (!branch->patterns[0]) {
  1581. return;
  1582. }
  1583. while (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  1584. tokenizer->advance();
  1585. branch->patterns.push_back(_parse_pattern(p_static));
  1586. if (!branch->patterns[branch->patterns.size() - 1]) {
  1587. return;
  1588. }
  1589. }
  1590. if (!_enter_indent_block()) {
  1591. _set_error("Expected block in pattern branch");
  1592. return;
  1593. }
  1594. branch->body = alloc_node<BlockNode>();
  1595. branch->body->parent_block = p_block;
  1596. p_block->sub_blocks.push_back(branch->body);
  1597. current_block = branch->body;
  1598. _parse_block(branch->body, p_static);
  1599. current_block = p_block;
  1600. p_branches.push_back(branch);
  1601. }
  1602. }
  1603. void GDParser::_generate_pattern(PatternNode *p_pattern, Node *p_node_to_match, Node *&p_resulting_node, Map<StringName, Node *> &p_bindings) {
  1604. switch (p_pattern->pt_type) {
  1605. case PatternNode::PT_CONSTANT: {
  1606. // typecheck
  1607. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1608. typeof_node->function = GDFunctions::TYPE_OF;
  1609. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1610. typeof_match_value->op = OperatorNode::OP_CALL;
  1611. typeof_match_value->arguments.push_back(typeof_node);
  1612. typeof_match_value->arguments.push_back(p_node_to_match);
  1613. OperatorNode *typeof_pattern_value = alloc_node<OperatorNode>();
  1614. typeof_pattern_value->op = OperatorNode::OP_CALL;
  1615. typeof_pattern_value->arguments.push_back(typeof_node);
  1616. typeof_pattern_value->arguments.push_back(p_pattern->constant);
  1617. OperatorNode *type_comp = alloc_node<OperatorNode>();
  1618. type_comp->op = OperatorNode::OP_EQUAL;
  1619. type_comp->arguments.push_back(typeof_match_value);
  1620. type_comp->arguments.push_back(typeof_pattern_value);
  1621. // comare the actual values
  1622. OperatorNode *value_comp = alloc_node<OperatorNode>();
  1623. value_comp->op = OperatorNode::OP_EQUAL;
  1624. value_comp->arguments.push_back(p_pattern->constant);
  1625. value_comp->arguments.push_back(p_node_to_match);
  1626. OperatorNode *comparison = alloc_node<OperatorNode>();
  1627. comparison->op = OperatorNode::OP_AND;
  1628. comparison->arguments.push_back(type_comp);
  1629. comparison->arguments.push_back(value_comp);
  1630. p_resulting_node = comparison;
  1631. } break;
  1632. case PatternNode::PT_BIND: {
  1633. p_bindings[p_pattern->bind] = p_node_to_match;
  1634. // a bind always matches
  1635. ConstantNode *true_value = alloc_node<ConstantNode>();
  1636. true_value->value = Variant(true);
  1637. p_resulting_node = true_value;
  1638. } break;
  1639. case PatternNode::PT_ARRAY: {
  1640. bool open_ended = false;
  1641. if (p_pattern->array.size() > 0) {
  1642. if (p_pattern->array[p_pattern->array.size() - 1]->pt_type == PatternNode::PT_IGNORE_REST) {
  1643. open_ended = true;
  1644. }
  1645. }
  1646. // typeof(value_to_match) == TYPE_ARRAY && value_to_match.size() >= length
  1647. // typeof(value_to_match) == TYPE_ARRAY && value_to_match.size() == length
  1648. {
  1649. // typecheck
  1650. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1651. typeof_node->function = GDFunctions::TYPE_OF;
  1652. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1653. typeof_match_value->op = OperatorNode::OP_CALL;
  1654. typeof_match_value->arguments.push_back(typeof_node);
  1655. typeof_match_value->arguments.push_back(p_node_to_match);
  1656. IdentifierNode *typeof_array = alloc_node<IdentifierNode>();
  1657. typeof_array->name = "TYPE_ARRAY";
  1658. OperatorNode *type_comp = alloc_node<OperatorNode>();
  1659. type_comp->op = OperatorNode::OP_EQUAL;
  1660. type_comp->arguments.push_back(typeof_match_value);
  1661. type_comp->arguments.push_back(typeof_array);
  1662. // size
  1663. ConstantNode *length = alloc_node<ConstantNode>();
  1664. length->value = Variant(open_ended ? p_pattern->array.size() - 1 : p_pattern->array.size());
  1665. OperatorNode *call = alloc_node<OperatorNode>();
  1666. call->op = OperatorNode::OP_CALL;
  1667. call->arguments.push_back(p_node_to_match);
  1668. IdentifierNode *size = alloc_node<IdentifierNode>();
  1669. size->name = "size";
  1670. call->arguments.push_back(size);
  1671. OperatorNode *length_comparison = alloc_node<OperatorNode>();
  1672. length_comparison->op = open_ended ? OperatorNode::OP_GREATER_EQUAL : OperatorNode::OP_EQUAL;
  1673. length_comparison->arguments.push_back(call);
  1674. length_comparison->arguments.push_back(length);
  1675. OperatorNode *type_and_length_comparison = alloc_node<OperatorNode>();
  1676. type_and_length_comparison->op = OperatorNode::OP_AND;
  1677. type_and_length_comparison->arguments.push_back(type_comp);
  1678. type_and_length_comparison->arguments.push_back(length_comparison);
  1679. p_resulting_node = type_and_length_comparison;
  1680. }
  1681. for (int i = 0; i < p_pattern->array.size(); i++) {
  1682. PatternNode *pattern = p_pattern->array[i];
  1683. Node *condition = NULL;
  1684. ConstantNode *index = alloc_node<ConstantNode>();
  1685. index->value = Variant(i);
  1686. OperatorNode *indexed_value = alloc_node<OperatorNode>();
  1687. indexed_value->op = OperatorNode::OP_INDEX;
  1688. indexed_value->arguments.push_back(p_node_to_match);
  1689. indexed_value->arguments.push_back(index);
  1690. _generate_pattern(pattern, indexed_value, condition, p_bindings);
  1691. // concatenate all the patterns with &&
  1692. OperatorNode *and_node = alloc_node<OperatorNode>();
  1693. and_node->op = OperatorNode::OP_AND;
  1694. and_node->arguments.push_back(p_resulting_node);
  1695. and_node->arguments.push_back(condition);
  1696. p_resulting_node = and_node;
  1697. }
  1698. } break;
  1699. case PatternNode::PT_DICTIONARY: {
  1700. bool open_ended = false;
  1701. if (p_pattern->array.size() > 0) {
  1702. open_ended = true;
  1703. }
  1704. // typeof(value_to_match) == TYPE_DICTIONARY && value_to_match.size() >= length
  1705. // typeof(value_to_match) == TYPE_DICTIONARY && value_to_match.size() == length
  1706. {
  1707. // typecheck
  1708. BuiltInFunctionNode *typeof_node = alloc_node<BuiltInFunctionNode>();
  1709. typeof_node->function = GDFunctions::TYPE_OF;
  1710. OperatorNode *typeof_match_value = alloc_node<OperatorNode>();
  1711. typeof_match_value->op = OperatorNode::OP_CALL;
  1712. typeof_match_value->arguments.push_back(typeof_node);
  1713. typeof_match_value->arguments.push_back(p_node_to_match);
  1714. IdentifierNode *typeof_dictionary = alloc_node<IdentifierNode>();
  1715. typeof_dictionary->name = "TYPE_DICTIONARY";
  1716. OperatorNode *type_comp = alloc_node<OperatorNode>();
  1717. type_comp->op = OperatorNode::OP_EQUAL;
  1718. type_comp->arguments.push_back(typeof_match_value);
  1719. type_comp->arguments.push_back(typeof_dictionary);
  1720. // size
  1721. ConstantNode *length = alloc_node<ConstantNode>();
  1722. length->value = Variant(open_ended ? p_pattern->dictionary.size() - 1 : p_pattern->dictionary.size());
  1723. OperatorNode *call = alloc_node<OperatorNode>();
  1724. call->op = OperatorNode::OP_CALL;
  1725. call->arguments.push_back(p_node_to_match);
  1726. IdentifierNode *size = alloc_node<IdentifierNode>();
  1727. size->name = "size";
  1728. call->arguments.push_back(size);
  1729. OperatorNode *length_comparison = alloc_node<OperatorNode>();
  1730. length_comparison->op = open_ended ? OperatorNode::OP_GREATER_EQUAL : OperatorNode::OP_EQUAL;
  1731. length_comparison->arguments.push_back(call);
  1732. length_comparison->arguments.push_back(length);
  1733. OperatorNode *type_and_length_comparison = alloc_node<OperatorNode>();
  1734. type_and_length_comparison->op = OperatorNode::OP_AND;
  1735. type_and_length_comparison->arguments.push_back(type_comp);
  1736. type_and_length_comparison->arguments.push_back(length_comparison);
  1737. p_resulting_node = type_and_length_comparison;
  1738. }
  1739. for (Map<ConstantNode *, PatternNode *>::Element *e = p_pattern->dictionary.front(); e; e = e->next()) {
  1740. Node *condition = NULL;
  1741. // chech for has, then for pattern
  1742. IdentifierNode *has = alloc_node<IdentifierNode>();
  1743. has->name = "has";
  1744. OperatorNode *has_call = alloc_node<OperatorNode>();
  1745. has_call->op = OperatorNode::OP_CALL;
  1746. has_call->arguments.push_back(p_node_to_match);
  1747. has_call->arguments.push_back(has);
  1748. has_call->arguments.push_back(e->key());
  1749. if (e->value()) {
  1750. OperatorNode *indexed_value = alloc_node<OperatorNode>();
  1751. indexed_value->op = OperatorNode::OP_INDEX;
  1752. indexed_value->arguments.push_back(p_node_to_match);
  1753. indexed_value->arguments.push_back(e->key());
  1754. _generate_pattern(e->value(), indexed_value, condition, p_bindings);
  1755. OperatorNode *has_and_pattern = alloc_node<OperatorNode>();
  1756. has_and_pattern->op = OperatorNode::OP_AND;
  1757. has_and_pattern->arguments.push_back(has_call);
  1758. has_and_pattern->arguments.push_back(condition);
  1759. condition = has_and_pattern;
  1760. } else {
  1761. condition = has_call;
  1762. }
  1763. // concatenate all the patterns with &&
  1764. OperatorNode *and_node = alloc_node<OperatorNode>();
  1765. and_node->op = OperatorNode::OP_AND;
  1766. and_node->arguments.push_back(p_resulting_node);
  1767. and_node->arguments.push_back(condition);
  1768. p_resulting_node = and_node;
  1769. }
  1770. } break;
  1771. case PatternNode::PT_IGNORE_REST:
  1772. case PatternNode::PT_WILDCARD: {
  1773. // simply generate a `true`
  1774. ConstantNode *true_value = alloc_node<ConstantNode>();
  1775. true_value->value = Variant(true);
  1776. p_resulting_node = true_value;
  1777. } break;
  1778. default: {
  1779. } break;
  1780. }
  1781. }
  1782. void GDParser::_transform_match_statment(BlockNode *p_block, MatchNode *p_match_statement) {
  1783. IdentifierNode *id = alloc_node<IdentifierNode>();
  1784. id->name = "#match_value";
  1785. for (int i = 0; i < p_match_statement->branches.size(); i++) {
  1786. PatternBranchNode *branch = p_match_statement->branches[i];
  1787. MatchNode::CompiledPatternBranch compiled_branch;
  1788. compiled_branch.compiled_pattern = NULL;
  1789. Map<StringName, Node *> binding;
  1790. for (int j = 0; j < branch->patterns.size(); j++) {
  1791. PatternNode *pattern = branch->patterns[j];
  1792. Map<StringName, Node *> bindings;
  1793. Node *resulting_node;
  1794. _generate_pattern(pattern, id, resulting_node, bindings);
  1795. if (!binding.empty() && !bindings.empty()) {
  1796. _set_error("Multipatterns can't contain bindings");
  1797. return;
  1798. } else {
  1799. binding = bindings;
  1800. }
  1801. if (compiled_branch.compiled_pattern) {
  1802. OperatorNode *or_node = alloc_node<OperatorNode>();
  1803. or_node->op = OperatorNode::OP_OR;
  1804. or_node->arguments.push_back(compiled_branch.compiled_pattern);
  1805. or_node->arguments.push_back(resulting_node);
  1806. compiled_branch.compiled_pattern = or_node;
  1807. } else {
  1808. // single pattern | first one
  1809. compiled_branch.compiled_pattern = resulting_node;
  1810. }
  1811. }
  1812. // prepare the body ...hehe
  1813. for (Map<StringName, Node *>::Element *e = binding.front(); e; e = e->next()) {
  1814. LocalVarNode *local_var = alloc_node<LocalVarNode>();
  1815. local_var->name = e->key();
  1816. local_var->assign = e->value();
  1817. IdentifierNode *id = alloc_node<IdentifierNode>();
  1818. id->name = local_var->name;
  1819. OperatorNode *op = alloc_node<OperatorNode>();
  1820. op->op = OperatorNode::OP_ASSIGN;
  1821. op->arguments.push_back(id);
  1822. op->arguments.push_back(local_var->assign);
  1823. branch->body->statements.push_front(op);
  1824. branch->body->statements.push_front(local_var);
  1825. }
  1826. compiled_branch.body = branch->body;
  1827. p_match_statement->compiled_pattern_branches.push_back(compiled_branch);
  1828. }
  1829. }
  1830. void GDParser::_parse_block(BlockNode *p_block, bool p_static) {
  1831. int indent_level = tab_level.back()->get();
  1832. #ifdef DEBUG_ENABLED
  1833. NewLineNode *nl = alloc_node<NewLineNode>();
  1834. nl->line = tokenizer->get_token_line();
  1835. p_block->statements.push_back(nl);
  1836. #endif
  1837. bool is_first_line = true;
  1838. while (true) {
  1839. if (!is_first_line && tab_level.back()->prev() && tab_level.back()->prev()->get() == indent_level) {
  1840. // pythonic single-line expression, don't parse future lines
  1841. tab_level.pop_back();
  1842. p_block->end_line = tokenizer->get_token_line();
  1843. return;
  1844. }
  1845. is_first_line = false;
  1846. GDTokenizer::Token token = tokenizer->get_token();
  1847. if (error_set)
  1848. return;
  1849. if (indent_level > tab_level.back()->get()) {
  1850. p_block->end_line = tokenizer->get_token_line();
  1851. return; //go back a level
  1852. }
  1853. if (pending_newline != -1) {
  1854. NewLineNode *nl = alloc_node<NewLineNode>();
  1855. nl->line = pending_newline;
  1856. p_block->statements.push_back(nl);
  1857. pending_newline = -1;
  1858. }
  1859. switch (token) {
  1860. case GDTokenizer::TK_EOF:
  1861. p_block->end_line = tokenizer->get_token_line();
  1862. case GDTokenizer::TK_ERROR: {
  1863. return; //go back
  1864. //end of file!
  1865. } break;
  1866. case GDTokenizer::TK_NEWLINE: {
  1867. if (!_parse_newline()) {
  1868. if (!error_set) {
  1869. p_block->end_line = tokenizer->get_token_line();
  1870. pending_newline = p_block->end_line;
  1871. }
  1872. return;
  1873. }
  1874. NewLineNode *nl = alloc_node<NewLineNode>();
  1875. nl->line = tokenizer->get_token_line();
  1876. p_block->statements.push_back(nl);
  1877. } break;
  1878. case GDTokenizer::TK_CF_PASS: {
  1879. if (tokenizer->get_token(1) != GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1) != GDTokenizer::TK_NEWLINE && tokenizer->get_token(1) != GDTokenizer::TK_EOF) {
  1880. _set_error("Expected ';' or <NewLine>.");
  1881. return;
  1882. }
  1883. tokenizer->advance();
  1884. if (tokenizer->get_token() == GDTokenizer::TK_SEMICOLON) {
  1885. // Ignore semicolon after 'pass'
  1886. tokenizer->advance();
  1887. }
  1888. } break;
  1889. case GDTokenizer::TK_PR_VAR: {
  1890. //variale declaration and (eventual) initialization
  1891. tokenizer->advance();
  1892. if (!tokenizer->is_token_literal(0, true)) {
  1893. _set_error("Expected identifier for local variable name.");
  1894. return;
  1895. }
  1896. StringName n = tokenizer->get_token_literal();
  1897. tokenizer->advance();
  1898. if (current_function) {
  1899. for (int i = 0; i < current_function->arguments.size(); i++) {
  1900. if (n == current_function->arguments[i]) {
  1901. _set_error("Variable '" + String(n) + "' already defined in the scope (at line: " + itos(current_function->line) + ").");
  1902. return;
  1903. }
  1904. }
  1905. }
  1906. BlockNode *check_block = p_block;
  1907. while (check_block) {
  1908. for (int i = 0; i < check_block->variables.size(); i++) {
  1909. if (n == check_block->variables[i]) {
  1910. _set_error("Variable '" + String(n) + "' already defined in the scope (at line: " + itos(check_block->variable_lines[i]) + ").");
  1911. return;
  1912. }
  1913. }
  1914. check_block = check_block->parent_block;
  1915. }
  1916. int var_line = tokenizer->get_token_line();
  1917. //must know when the local variable is declared
  1918. LocalVarNode *lv = alloc_node<LocalVarNode>();
  1919. lv->name = n;
  1920. p_block->statements.push_back(lv);
  1921. Node *assigned = NULL;
  1922. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  1923. tokenizer->advance();
  1924. Node *subexpr = _parse_and_reduce_expression(p_block, p_static);
  1925. if (!subexpr) {
  1926. if (_recover_from_completion()) {
  1927. break;
  1928. }
  1929. return;
  1930. }
  1931. lv->assign = subexpr;
  1932. assigned = subexpr;
  1933. } else {
  1934. ConstantNode *c = alloc_node<ConstantNode>();
  1935. c->value = Variant();
  1936. assigned = c;
  1937. }
  1938. //must be added later, to avoid self-referencing.
  1939. p_block->variables.push_back(n); //line?
  1940. p_block->variable_lines.push_back(var_line);
  1941. IdentifierNode *id = alloc_node<IdentifierNode>();
  1942. id->name = n;
  1943. OperatorNode *op = alloc_node<OperatorNode>();
  1944. op->op = OperatorNode::OP_ASSIGN;
  1945. op->arguments.push_back(id);
  1946. op->arguments.push_back(assigned);
  1947. p_block->statements.push_back(op);
  1948. if (!_end_statement()) {
  1949. _set_error("Expected end of statement (var)");
  1950. return;
  1951. }
  1952. } break;
  1953. case GDTokenizer::TK_CF_IF: {
  1954. tokenizer->advance();
  1955. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  1956. if (!condition) {
  1957. if (_recover_from_completion()) {
  1958. break;
  1959. }
  1960. return;
  1961. }
  1962. ControlFlowNode *cf_if = alloc_node<ControlFlowNode>();
  1963. cf_if->cf_type = ControlFlowNode::CF_IF;
  1964. cf_if->arguments.push_back(condition);
  1965. cf_if->body = alloc_node<BlockNode>();
  1966. cf_if->body->parent_block = p_block;
  1967. p_block->sub_blocks.push_back(cf_if->body);
  1968. if (!_enter_indent_block(cf_if->body)) {
  1969. _set_error("Expected indented block after 'if'");
  1970. p_block->end_line = tokenizer->get_token_line();
  1971. return;
  1972. }
  1973. current_block = cf_if->body;
  1974. _parse_block(cf_if->body, p_static);
  1975. current_block = p_block;
  1976. if (error_set)
  1977. return;
  1978. p_block->statements.push_back(cf_if);
  1979. while (true) {
  1980. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE && _parse_newline())
  1981. ;
  1982. if (tab_level.back()->get() < indent_level) { //not at current indent level
  1983. p_block->end_line = tokenizer->get_token_line();
  1984. return;
  1985. }
  1986. if (tokenizer->get_token() == GDTokenizer::TK_CF_ELIF) {
  1987. if (tab_level.back()->get() > indent_level) {
  1988. _set_error("Invalid indent");
  1989. return;
  1990. }
  1991. tokenizer->advance();
  1992. cf_if->body_else = alloc_node<BlockNode>();
  1993. cf_if->body_else->parent_block = p_block;
  1994. p_block->sub_blocks.push_back(cf_if->body_else);
  1995. ControlFlowNode *cf_else = alloc_node<ControlFlowNode>();
  1996. cf_else->cf_type = ControlFlowNode::CF_IF;
  1997. //condition
  1998. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  1999. if (!condition) {
  2000. if (_recover_from_completion()) {
  2001. break;
  2002. }
  2003. return;
  2004. }
  2005. cf_else->arguments.push_back(condition);
  2006. cf_else->cf_type = ControlFlowNode::CF_IF;
  2007. cf_if->body_else->statements.push_back(cf_else);
  2008. cf_if = cf_else;
  2009. cf_if->body = alloc_node<BlockNode>();
  2010. cf_if->body->parent_block = p_block;
  2011. p_block->sub_blocks.push_back(cf_if->body);
  2012. if (!_enter_indent_block(cf_if->body)) {
  2013. _set_error("Expected indented block after 'elif'");
  2014. p_block->end_line = tokenizer->get_token_line();
  2015. return;
  2016. }
  2017. current_block = cf_else->body;
  2018. _parse_block(cf_else->body, p_static);
  2019. current_block = p_block;
  2020. if (error_set)
  2021. return;
  2022. } else if (tokenizer->get_token() == GDTokenizer::TK_CF_ELSE) {
  2023. if (tab_level.back()->get() > indent_level) {
  2024. _set_error("Invalid indent");
  2025. return;
  2026. }
  2027. tokenizer->advance();
  2028. cf_if->body_else = alloc_node<BlockNode>();
  2029. cf_if->body_else->parent_block = p_block;
  2030. p_block->sub_blocks.push_back(cf_if->body_else);
  2031. if (!_enter_indent_block(cf_if->body_else)) {
  2032. _set_error("Expected indented block after 'else'");
  2033. p_block->end_line = tokenizer->get_token_line();
  2034. return;
  2035. }
  2036. current_block = cf_if->body_else;
  2037. _parse_block(cf_if->body_else, p_static);
  2038. current_block = p_block;
  2039. if (error_set)
  2040. return;
  2041. break; //after else, exit
  2042. } else
  2043. break;
  2044. }
  2045. } break;
  2046. case GDTokenizer::TK_CF_WHILE: {
  2047. tokenizer->advance();
  2048. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2049. if (!condition) {
  2050. if (_recover_from_completion()) {
  2051. break;
  2052. }
  2053. return;
  2054. }
  2055. ControlFlowNode *cf_while = alloc_node<ControlFlowNode>();
  2056. cf_while->cf_type = ControlFlowNode::CF_WHILE;
  2057. cf_while->arguments.push_back(condition);
  2058. cf_while->body = alloc_node<BlockNode>();
  2059. cf_while->body->parent_block = p_block;
  2060. p_block->sub_blocks.push_back(cf_while->body);
  2061. if (!_enter_indent_block(cf_while->body)) {
  2062. _set_error("Expected indented block after 'while'");
  2063. p_block->end_line = tokenizer->get_token_line();
  2064. return;
  2065. }
  2066. current_block = cf_while->body;
  2067. _parse_block(cf_while->body, p_static);
  2068. current_block = p_block;
  2069. if (error_set)
  2070. return;
  2071. p_block->statements.push_back(cf_while);
  2072. } break;
  2073. case GDTokenizer::TK_CF_FOR: {
  2074. tokenizer->advance();
  2075. if (!tokenizer->is_token_literal(0, true)) {
  2076. _set_error("identifier expected after 'for'");
  2077. }
  2078. IdentifierNode *id = alloc_node<IdentifierNode>();
  2079. id->name = tokenizer->get_token_identifier();
  2080. tokenizer->advance();
  2081. if (tokenizer->get_token() != GDTokenizer::TK_OP_IN) {
  2082. _set_error("'in' expected after identifier");
  2083. return;
  2084. }
  2085. tokenizer->advance();
  2086. Node *container = _parse_and_reduce_expression(p_block, p_static);
  2087. if (!container) {
  2088. if (_recover_from_completion()) {
  2089. break;
  2090. }
  2091. return;
  2092. }
  2093. if (container->type == Node::TYPE_OPERATOR) {
  2094. OperatorNode *op = static_cast<OperatorNode *>(container);
  2095. 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) {
  2096. //iterating a range, so see if range() can be optimized without allocating memory, by replacing it by vectors (which can work as iterable too!)
  2097. Vector<Node *> args;
  2098. Vector<double> constants;
  2099. bool constant = false;
  2100. for (int i = 1; i < op->arguments.size(); i++) {
  2101. args.push_back(op->arguments[i]);
  2102. if (constant && op->arguments[i]->type == Node::TYPE_CONSTANT) {
  2103. ConstantNode *c = static_cast<ConstantNode *>(op->arguments[i]);
  2104. if (c->value.get_type() == Variant::REAL || c->value.get_type() == Variant::INT) {
  2105. constants.push_back(c->value);
  2106. constant = true;
  2107. }
  2108. } else {
  2109. constant = false;
  2110. }
  2111. }
  2112. if (args.size() > 0 && args.size() < 4) {
  2113. if (constant) {
  2114. ConstantNode *cn = alloc_node<ConstantNode>();
  2115. switch (args.size()) {
  2116. case 1: cn->value = (int)constants[0]; break;
  2117. case 2: cn->value = Vector2(constants[0], constants[1]); break;
  2118. case 3: cn->value = Vector3(constants[0], constants[1], constants[2]); break;
  2119. }
  2120. container = cn;
  2121. } else {
  2122. OperatorNode *on = alloc_node<OperatorNode>();
  2123. on->op = OperatorNode::OP_CALL;
  2124. TypeNode *tn = alloc_node<TypeNode>();
  2125. on->arguments.push_back(tn);
  2126. switch (args.size()) {
  2127. case 1: tn->vtype = Variant::INT; break;
  2128. case 2: tn->vtype = Variant::VECTOR2; break;
  2129. case 3: tn->vtype = Variant::VECTOR3; break;
  2130. }
  2131. for (int i = 0; i < args.size(); i++) {
  2132. on->arguments.push_back(args[i]);
  2133. }
  2134. container = on;
  2135. }
  2136. }
  2137. }
  2138. }
  2139. ControlFlowNode *cf_for = alloc_node<ControlFlowNode>();
  2140. cf_for->cf_type = ControlFlowNode::CF_FOR;
  2141. cf_for->arguments.push_back(id);
  2142. cf_for->arguments.push_back(container);
  2143. cf_for->body = alloc_node<BlockNode>();
  2144. cf_for->body->parent_block = p_block;
  2145. p_block->sub_blocks.push_back(cf_for->body);
  2146. if (!_enter_indent_block(cf_for->body)) {
  2147. _set_error("Expected indented block after 'for'");
  2148. p_block->end_line = tokenizer->get_token_line();
  2149. return;
  2150. }
  2151. current_block = cf_for->body;
  2152. // this is for checking variable for redefining
  2153. // inside this _parse_block
  2154. cf_for->body->variables.push_back(id->name);
  2155. cf_for->body->variable_lines.push_back(id->line);
  2156. _parse_block(cf_for->body, p_static);
  2157. cf_for->body->variables.remove(0);
  2158. cf_for->body->variable_lines.remove(0);
  2159. current_block = p_block;
  2160. if (error_set)
  2161. return;
  2162. p_block->statements.push_back(cf_for);
  2163. } break;
  2164. case GDTokenizer::TK_CF_CONTINUE: {
  2165. tokenizer->advance();
  2166. ControlFlowNode *cf_continue = alloc_node<ControlFlowNode>();
  2167. cf_continue->cf_type = ControlFlowNode::CF_CONTINUE;
  2168. p_block->statements.push_back(cf_continue);
  2169. if (!_end_statement()) {
  2170. _set_error("Expected end of statement (continue)");
  2171. return;
  2172. }
  2173. } break;
  2174. case GDTokenizer::TK_CF_BREAK: {
  2175. tokenizer->advance();
  2176. ControlFlowNode *cf_break = alloc_node<ControlFlowNode>();
  2177. cf_break->cf_type = ControlFlowNode::CF_BREAK;
  2178. p_block->statements.push_back(cf_break);
  2179. if (!_end_statement()) {
  2180. _set_error("Expected end of statement (break)");
  2181. return;
  2182. }
  2183. } break;
  2184. case GDTokenizer::TK_CF_RETURN: {
  2185. tokenizer->advance();
  2186. ControlFlowNode *cf_return = alloc_node<ControlFlowNode>();
  2187. cf_return->cf_type = ControlFlowNode::CF_RETURN;
  2188. if (tokenizer->get_token() == GDTokenizer::TK_SEMICOLON || tokenizer->get_token() == GDTokenizer::TK_NEWLINE || tokenizer->get_token() == GDTokenizer::TK_EOF) {
  2189. //expect end of statement
  2190. p_block->statements.push_back(cf_return);
  2191. if (!_end_statement()) {
  2192. return;
  2193. }
  2194. } else {
  2195. //expect expression
  2196. Node *retexpr = _parse_and_reduce_expression(p_block, p_static);
  2197. if (!retexpr) {
  2198. if (_recover_from_completion()) {
  2199. break;
  2200. }
  2201. return;
  2202. }
  2203. cf_return->arguments.push_back(retexpr);
  2204. p_block->statements.push_back(cf_return);
  2205. if (!_end_statement()) {
  2206. _set_error("Expected end of statement after return expression.");
  2207. return;
  2208. }
  2209. }
  2210. } break;
  2211. case GDTokenizer::TK_CF_MATCH: {
  2212. tokenizer->advance();
  2213. MatchNode *match_node = alloc_node<MatchNode>();
  2214. Node *val_to_match = _parse_and_reduce_expression(p_block, p_static);
  2215. if (!val_to_match) {
  2216. if (_recover_from_completion()) {
  2217. break;
  2218. }
  2219. return;
  2220. }
  2221. match_node->val_to_match = val_to_match;
  2222. if (!_enter_indent_block()) {
  2223. _set_error("Expected indented pattern matching block after 'match'");
  2224. return;
  2225. }
  2226. BlockNode *compiled_branches = alloc_node<BlockNode>();
  2227. compiled_branches->parent_block = p_block;
  2228. compiled_branches->parent_class = p_block->parent_class;
  2229. p_block->sub_blocks.push_back(compiled_branches);
  2230. _parse_pattern_block(compiled_branches, match_node->branches, p_static);
  2231. _transform_match_statment(compiled_branches, match_node);
  2232. ControlFlowNode *match_cf_node = alloc_node<ControlFlowNode>();
  2233. match_cf_node->cf_type = ControlFlowNode::CF_MATCH;
  2234. match_cf_node->match = match_node;
  2235. p_block->statements.push_back(match_cf_node);
  2236. _end_statement();
  2237. } break;
  2238. case GDTokenizer::TK_PR_ASSERT: {
  2239. tokenizer->advance();
  2240. Node *condition = _parse_and_reduce_expression(p_block, p_static);
  2241. if (!condition) {
  2242. if (_recover_from_completion()) {
  2243. break;
  2244. }
  2245. return;
  2246. }
  2247. AssertNode *an = alloc_node<AssertNode>();
  2248. an->condition = condition;
  2249. p_block->statements.push_back(an);
  2250. if (!_end_statement()) {
  2251. _set_error("Expected end of statement after assert.");
  2252. return;
  2253. }
  2254. } break;
  2255. case GDTokenizer::TK_PR_BREAKPOINT: {
  2256. tokenizer->advance();
  2257. BreakpointNode *bn = alloc_node<BreakpointNode>();
  2258. p_block->statements.push_back(bn);
  2259. if (!_end_statement()) {
  2260. _set_error("Expected end of statement after breakpoint.");
  2261. return;
  2262. }
  2263. } break;
  2264. default: {
  2265. Node *expression = _parse_and_reduce_expression(p_block, p_static, false, true);
  2266. if (!expression) {
  2267. if (_recover_from_completion()) {
  2268. break;
  2269. }
  2270. return;
  2271. }
  2272. p_block->statements.push_back(expression);
  2273. if (!_end_statement()) {
  2274. _set_error("Expected end of statement after expression.");
  2275. return;
  2276. }
  2277. } break;
  2278. /*
  2279. case GDTokenizer::TK_CF_LOCAL: {
  2280. if (tokenizer->get_token(1)!=GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE ) {
  2281. _set_error("Expected ';' or <NewLine>.");
  2282. }
  2283. tokenizer->advance();
  2284. } break;
  2285. */
  2286. }
  2287. }
  2288. }
  2289. bool GDParser::_parse_newline() {
  2290. if (tokenizer->get_token(1) != GDTokenizer::TK_EOF && tokenizer->get_token(1) != GDTokenizer::TK_NEWLINE) {
  2291. int indent = tokenizer->get_token_line_indent();
  2292. int current_indent = tab_level.back()->get();
  2293. if (indent > current_indent) {
  2294. _set_error("Unexpected indent.");
  2295. return false;
  2296. }
  2297. if (indent < current_indent) {
  2298. while (indent < current_indent) {
  2299. //exit block
  2300. if (tab_level.size() == 1) {
  2301. _set_error("Invalid indent. BUG?");
  2302. return false;
  2303. }
  2304. tab_level.pop_back();
  2305. if (tab_level.back()->get() < indent) {
  2306. _set_error("Unindent does not match any outer indentation level.");
  2307. return false;
  2308. }
  2309. current_indent = tab_level.back()->get();
  2310. }
  2311. tokenizer->advance();
  2312. return false;
  2313. }
  2314. }
  2315. tokenizer->advance();
  2316. return true;
  2317. }
  2318. void GDParser::_parse_extends(ClassNode *p_class) {
  2319. if (p_class->extends_used) {
  2320. _set_error("'extends' already used for this class.");
  2321. return;
  2322. }
  2323. if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty()) {
  2324. _set_error("'extends' must be used before anything else.");
  2325. return;
  2326. }
  2327. p_class->extends_used = true;
  2328. tokenizer->advance();
  2329. if (tokenizer->get_token() == GDTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token_type() == Variant::OBJECT) {
  2330. p_class->extends_class.push_back(Variant::get_type_name(Variant::OBJECT));
  2331. tokenizer->advance();
  2332. return;
  2333. }
  2334. // see if inheritance happens from a file
  2335. if (tokenizer->get_token() == GDTokenizer::TK_CONSTANT) {
  2336. Variant constant = tokenizer->get_token_constant();
  2337. if (constant.get_type() != Variant::STRING) {
  2338. _set_error("'extends' constant must be a string.");
  2339. return;
  2340. }
  2341. p_class->extends_file = constant;
  2342. tokenizer->advance();
  2343. if (tokenizer->get_token() != GDTokenizer::TK_PERIOD) {
  2344. return;
  2345. } else
  2346. tokenizer->advance();
  2347. }
  2348. while (true) {
  2349. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2350. _set_error("Invalid 'extends' syntax, expected string constant (path) and/or identifier (parent class).");
  2351. return;
  2352. }
  2353. StringName identifier = tokenizer->get_token_identifier();
  2354. p_class->extends_class.push_back(identifier);
  2355. tokenizer->advance(1);
  2356. if (tokenizer->get_token() != GDTokenizer::TK_PERIOD)
  2357. return;
  2358. }
  2359. }
  2360. void GDParser::_parse_class(ClassNode *p_class) {
  2361. int indent_level = tab_level.back()->get();
  2362. while (true) {
  2363. GDTokenizer::Token token = tokenizer->get_token();
  2364. if (error_set)
  2365. return;
  2366. if (indent_level > tab_level.back()->get()) {
  2367. p_class->end_line = tokenizer->get_token_line();
  2368. return; //go back a level
  2369. }
  2370. switch (token) {
  2371. case GDTokenizer::TK_EOF:
  2372. p_class->end_line = tokenizer->get_token_line();
  2373. case GDTokenizer::TK_ERROR: {
  2374. return; //go back
  2375. //end of file!
  2376. } break;
  2377. case GDTokenizer::TK_NEWLINE: {
  2378. if (!_parse_newline()) {
  2379. if (!error_set) {
  2380. p_class->end_line = tokenizer->get_token_line();
  2381. }
  2382. return;
  2383. }
  2384. } break;
  2385. case GDTokenizer::TK_PR_EXTENDS: {
  2386. _parse_extends(p_class);
  2387. if (error_set)
  2388. return;
  2389. if (!_end_statement()) {
  2390. _set_error("Expected end of statement after extends");
  2391. return;
  2392. }
  2393. } break;
  2394. case GDTokenizer::TK_PR_TOOL: {
  2395. if (p_class->tool) {
  2396. _set_error("tool used more than once");
  2397. return;
  2398. }
  2399. p_class->tool = true;
  2400. tokenizer->advance();
  2401. } break;
  2402. case GDTokenizer::TK_PR_CLASS: {
  2403. //class inside class :D
  2404. StringName name;
  2405. StringName extends;
  2406. if (tokenizer->get_token(1) != GDTokenizer::TK_IDENTIFIER) {
  2407. _set_error("'class' syntax: 'class <Name>:' or 'class <Name> extends <BaseClass>:'");
  2408. return;
  2409. }
  2410. name = tokenizer->get_token_identifier(1);
  2411. tokenizer->advance(2);
  2412. ClassNode *newclass = alloc_node<ClassNode>();
  2413. newclass->initializer = alloc_node<BlockNode>();
  2414. newclass->initializer->parent_class = newclass;
  2415. newclass->ready = alloc_node<BlockNode>();
  2416. newclass->ready->parent_class = newclass;
  2417. newclass->name = name;
  2418. newclass->owner = p_class;
  2419. p_class->subclasses.push_back(newclass);
  2420. if (tokenizer->get_token() == GDTokenizer::TK_PR_EXTENDS) {
  2421. _parse_extends(newclass);
  2422. if (error_set)
  2423. return;
  2424. }
  2425. if (!_enter_indent_block()) {
  2426. _set_error("Indented block expected.");
  2427. return;
  2428. }
  2429. current_class = newclass;
  2430. _parse_class(newclass);
  2431. current_class = p_class;
  2432. } break;
  2433. /* this is for functions....
  2434. case GDTokenizer::TK_CF_PASS: {
  2435. tokenizer->advance(1);
  2436. } break;
  2437. */
  2438. case GDTokenizer::TK_PR_STATIC: {
  2439. tokenizer->advance();
  2440. if (tokenizer->get_token() != GDTokenizer::TK_PR_FUNCTION) {
  2441. _set_error("Expected 'func'.");
  2442. return;
  2443. }
  2444. }; //fallthrough to function
  2445. case GDTokenizer::TK_PR_FUNCTION: {
  2446. bool _static = false;
  2447. pending_newline = -1;
  2448. if (tokenizer->get_token(-1) == GDTokenizer::TK_PR_STATIC) {
  2449. _static = true;
  2450. }
  2451. tokenizer->advance();
  2452. StringName name;
  2453. if (_get_completable_identifier(COMPLETION_VIRTUAL_FUNC, name)) {
  2454. }
  2455. if (name == StringName()) {
  2456. _set_error("Expected identifier after 'func' (syntax: 'func <identifier>([arguments]):' ).");
  2457. return;
  2458. }
  2459. for (int i = 0; i < p_class->functions.size(); i++) {
  2460. if (p_class->functions[i]->name == name) {
  2461. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->functions[i]->line) + ").");
  2462. }
  2463. }
  2464. for (int i = 0; i < p_class->static_functions.size(); i++) {
  2465. if (p_class->static_functions[i]->name == name) {
  2466. _set_error("Function '" + String(name) + "' already exists in this class (at line: " + itos(p_class->static_functions[i]->line) + ").");
  2467. }
  2468. }
  2469. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_OPEN) {
  2470. _set_error("Expected '(' after identifier (syntax: 'func <identifier>([arguments]):' ).");
  2471. return;
  2472. }
  2473. tokenizer->advance();
  2474. Vector<StringName> arguments;
  2475. Vector<Node *> default_values;
  2476. int fnline = tokenizer->get_token_line();
  2477. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2478. //has arguments
  2479. bool defaulting = false;
  2480. while (true) {
  2481. if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  2482. tokenizer->advance();
  2483. continue;
  2484. }
  2485. if (tokenizer->get_token() == GDTokenizer::TK_PR_VAR) {
  2486. tokenizer->advance(); //var before the identifier is allowed
  2487. }
  2488. if (!tokenizer->is_token_literal(0, true)) {
  2489. _set_error("Expected identifier for argument.");
  2490. return;
  2491. }
  2492. StringName argname = tokenizer->get_token_identifier();
  2493. arguments.push_back(argname);
  2494. tokenizer->advance();
  2495. if (defaulting && tokenizer->get_token() != GDTokenizer::TK_OP_ASSIGN) {
  2496. _set_error("Default parameter expected.");
  2497. return;
  2498. }
  2499. //tokenizer->advance();
  2500. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  2501. defaulting = true;
  2502. tokenizer->advance(1);
  2503. Node *defval = _parse_and_reduce_expression(p_class, _static);
  2504. if (!defval || error_set)
  2505. return;
  2506. OperatorNode *on = alloc_node<OperatorNode>();
  2507. on->op = OperatorNode::OP_ASSIGN;
  2508. IdentifierNode *in = alloc_node<IdentifierNode>();
  2509. in->name = argname;
  2510. on->arguments.push_back(in);
  2511. on->arguments.push_back(defval);
  2512. /* no ..
  2513. if (defval->type!=Node::TYPE_CONSTANT) {
  2514. _set_error("default argument must be constant");
  2515. }
  2516. */
  2517. default_values.push_back(on);
  2518. }
  2519. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  2520. tokenizer->advance();
  2521. }
  2522. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2523. tokenizer->advance();
  2524. continue;
  2525. } else if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2526. _set_error("Expected ',' or ')'.");
  2527. return;
  2528. }
  2529. break;
  2530. }
  2531. }
  2532. tokenizer->advance();
  2533. BlockNode *block = alloc_node<BlockNode>();
  2534. block->parent_class = p_class;
  2535. if (name == "_init") {
  2536. if (p_class->extends_used) {
  2537. OperatorNode *cparent = alloc_node<OperatorNode>();
  2538. cparent->op = OperatorNode::OP_PARENT_CALL;
  2539. block->statements.push_back(cparent);
  2540. IdentifierNode *id = alloc_node<IdentifierNode>();
  2541. id->name = "_init";
  2542. cparent->arguments.push_back(id);
  2543. if (tokenizer->get_token() == GDTokenizer::TK_PERIOD) {
  2544. tokenizer->advance();
  2545. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_OPEN) {
  2546. _set_error("expected '(' for parent constructor arguments.");
  2547. }
  2548. tokenizer->advance();
  2549. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2550. //has arguments
  2551. parenthesis++;
  2552. while (true) {
  2553. Node *arg = _parse_and_reduce_expression(p_class, _static);
  2554. cparent->arguments.push_back(arg);
  2555. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2556. tokenizer->advance();
  2557. continue;
  2558. } else if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2559. _set_error("Expected ',' or ')'.");
  2560. return;
  2561. }
  2562. break;
  2563. }
  2564. parenthesis--;
  2565. }
  2566. tokenizer->advance();
  2567. }
  2568. } else {
  2569. if (tokenizer->get_token() == GDTokenizer::TK_PERIOD) {
  2570. _set_error("Parent constructor call found for a class without inheritance.");
  2571. return;
  2572. }
  2573. }
  2574. }
  2575. if (!_enter_indent_block(block)) {
  2576. _set_error("Indented block expected.");
  2577. return;
  2578. }
  2579. FunctionNode *function = alloc_node<FunctionNode>();
  2580. function->name = name;
  2581. function->arguments = arguments;
  2582. function->default_values = default_values;
  2583. function->_static = _static;
  2584. function->line = fnline;
  2585. function->rpc_mode = rpc_mode;
  2586. rpc_mode = ScriptInstance::RPC_MODE_DISABLED;
  2587. if (_static)
  2588. p_class->static_functions.push_back(function);
  2589. else
  2590. p_class->functions.push_back(function);
  2591. current_function = function;
  2592. function->body = block;
  2593. current_block = block;
  2594. _parse_block(block, _static);
  2595. current_block = NULL;
  2596. //arguments
  2597. } break;
  2598. case GDTokenizer::TK_PR_SIGNAL: {
  2599. tokenizer->advance();
  2600. if (!tokenizer->is_token_literal()) {
  2601. _set_error("Expected identifier after 'signal'.");
  2602. return;
  2603. }
  2604. ClassNode::Signal sig;
  2605. sig.name = tokenizer->get_token_identifier();
  2606. tokenizer->advance();
  2607. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_OPEN) {
  2608. tokenizer->advance();
  2609. while (true) {
  2610. if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  2611. tokenizer->advance();
  2612. continue;
  2613. }
  2614. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2615. tokenizer->advance();
  2616. break;
  2617. }
  2618. if (!tokenizer->is_token_literal(0, true)) {
  2619. _set_error("Expected identifier in signal argument.");
  2620. return;
  2621. }
  2622. sig.arguments.push_back(tokenizer->get_token_identifier());
  2623. tokenizer->advance();
  2624. while (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  2625. tokenizer->advance();
  2626. }
  2627. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2628. tokenizer->advance();
  2629. } else if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2630. _set_error("Expected ',' or ')' after signal parameter identifier.");
  2631. return;
  2632. }
  2633. }
  2634. }
  2635. p_class->_signals.push_back(sig);
  2636. if (!_end_statement()) {
  2637. _set_error("Expected end of statement (signal)");
  2638. return;
  2639. }
  2640. } break;
  2641. case GDTokenizer::TK_PR_EXPORT: {
  2642. tokenizer->advance();
  2643. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_OPEN) {
  2644. tokenizer->advance();
  2645. if (tokenizer->get_token() == GDTokenizer::TK_BUILT_IN_TYPE) {
  2646. Variant::Type type = tokenizer->get_token_type();
  2647. if (type == Variant::NIL) {
  2648. _set_error("Can't export null type.");
  2649. return;
  2650. }
  2651. current_export.type = type;
  2652. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  2653. tokenizer->advance();
  2654. String hint_prefix = "";
  2655. if (type == Variant::ARRAY && tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2656. tokenizer->advance();
  2657. while (tokenizer->get_token() == GDTokenizer::TK_BUILT_IN_TYPE) {
  2658. type = tokenizer->get_token_type();
  2659. tokenizer->advance();
  2660. if (type == Variant::ARRAY) {
  2661. hint_prefix += itos(Variant::ARRAY) + ":";
  2662. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2663. tokenizer->advance();
  2664. }
  2665. } else {
  2666. hint_prefix += itos(type);
  2667. break;
  2668. }
  2669. }
  2670. }
  2671. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2672. // hint expected next!
  2673. tokenizer->advance();
  2674. switch (type) {
  2675. case Variant::INT: {
  2676. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FLAGS") {
  2677. //current_export.hint=PROPERTY_HINT_ALL_FLAGS;
  2678. tokenizer->advance();
  2679. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2680. break;
  2681. }
  2682. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2683. _set_error("Expected ')' or ',' in bit flags hint.");
  2684. return;
  2685. }
  2686. current_export.hint = PROPERTY_HINT_FLAGS;
  2687. tokenizer->advance();
  2688. bool first = true;
  2689. while (true) {
  2690. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2691. current_export = PropertyInfo();
  2692. _set_error("Expected a string constant in named bit flags hint.");
  2693. return;
  2694. }
  2695. String c = tokenizer->get_token_constant();
  2696. if (!first)
  2697. current_export.hint_string += ",";
  2698. else
  2699. first = false;
  2700. current_export.hint_string += c.xml_escape();
  2701. tokenizer->advance();
  2702. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2703. break;
  2704. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2705. current_export = PropertyInfo();
  2706. _set_error("Expected ')' or ',' in named bit flags hint.");
  2707. return;
  2708. }
  2709. tokenizer->advance();
  2710. }
  2711. break;
  2712. }
  2713. if (tokenizer->get_token() == GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  2714. //enumeration
  2715. current_export.hint = PROPERTY_HINT_ENUM;
  2716. bool first = true;
  2717. while (true) {
  2718. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2719. current_export = PropertyInfo();
  2720. _set_error("Expected a string constant in enumeration hint.");
  2721. return;
  2722. }
  2723. String c = tokenizer->get_token_constant();
  2724. if (!first)
  2725. current_export.hint_string += ",";
  2726. else
  2727. first = false;
  2728. current_export.hint_string += c.xml_escape();
  2729. tokenizer->advance();
  2730. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2731. break;
  2732. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2733. current_export = PropertyInfo();
  2734. _set_error("Expected ')' or ',' in enumeration hint.");
  2735. return;
  2736. }
  2737. tokenizer->advance();
  2738. }
  2739. break;
  2740. }
  2741. }; //fallthrough to use the same
  2742. case Variant::REAL: {
  2743. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EASE") {
  2744. current_export.hint = PROPERTY_HINT_EXP_EASING;
  2745. tokenizer->advance();
  2746. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2747. _set_error("Expected ')' in hint.");
  2748. return;
  2749. }
  2750. break;
  2751. }
  2752. // range
  2753. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "EXP") {
  2754. current_export.hint = PROPERTY_HINT_EXP_RANGE;
  2755. tokenizer->advance();
  2756. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2757. break;
  2758. else if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2759. _set_error("Expected ')' or ',' in exponential range hint.");
  2760. return;
  2761. }
  2762. tokenizer->advance();
  2763. } else
  2764. current_export.hint = PROPERTY_HINT_RANGE;
  2765. float sign = 1.0;
  2766. if (tokenizer->get_token() == GDTokenizer::TK_OP_SUB) {
  2767. sign = -1;
  2768. tokenizer->advance();
  2769. }
  2770. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2771. current_export = PropertyInfo();
  2772. _set_error("Expected a range in numeric hint.");
  2773. return;
  2774. }
  2775. current_export.hint_string = rtos(sign * double(tokenizer->get_token_constant()));
  2776. tokenizer->advance();
  2777. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2778. current_export.hint_string = "0," + current_export.hint_string;
  2779. break;
  2780. }
  2781. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2782. current_export = PropertyInfo();
  2783. _set_error("Expected ',' or ')' in numeric range hint.");
  2784. return;
  2785. }
  2786. tokenizer->advance();
  2787. sign = 1.0;
  2788. if (tokenizer->get_token() == GDTokenizer::TK_OP_SUB) {
  2789. sign = -1;
  2790. tokenizer->advance();
  2791. }
  2792. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2793. current_export = PropertyInfo();
  2794. _set_error("Expected a number as upper bound in numeric range hint.");
  2795. return;
  2796. }
  2797. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  2798. tokenizer->advance();
  2799. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2800. break;
  2801. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2802. current_export = PropertyInfo();
  2803. _set_error("Expected ',' or ')' in numeric range hint.");
  2804. return;
  2805. }
  2806. tokenizer->advance();
  2807. sign = 1.0;
  2808. if (tokenizer->get_token() == GDTokenizer::TK_OP_SUB) {
  2809. sign = -1;
  2810. tokenizer->advance();
  2811. }
  2812. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2813. current_export = PropertyInfo();
  2814. _set_error("Expected a number as step in numeric range hint.");
  2815. return;
  2816. }
  2817. current_export.hint_string += "," + rtos(sign * double(tokenizer->get_token_constant()));
  2818. tokenizer->advance();
  2819. } break;
  2820. case Variant::STRING: {
  2821. if (tokenizer->get_token() == GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type() == Variant::STRING) {
  2822. //enumeration
  2823. current_export.hint = PROPERTY_HINT_ENUM;
  2824. bool first = true;
  2825. while (true) {
  2826. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2827. current_export = PropertyInfo();
  2828. _set_error("Expected a string constant in enumeration hint.");
  2829. return;
  2830. }
  2831. String c = tokenizer->get_token_constant();
  2832. if (!first)
  2833. current_export.hint_string += ",";
  2834. else
  2835. first = false;
  2836. current_export.hint_string += c.xml_escape();
  2837. tokenizer->advance();
  2838. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2839. break;
  2840. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  2841. current_export = PropertyInfo();
  2842. _set_error("Expected ')' or ',' in enumeration hint.");
  2843. return;
  2844. }
  2845. tokenizer->advance();
  2846. }
  2847. break;
  2848. }
  2849. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "DIR") {
  2850. tokenizer->advance();
  2851. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2852. current_export.hint = PROPERTY_HINT_DIR;
  2853. else if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2854. tokenizer->advance();
  2855. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier() == "GLOBAL")) {
  2856. _set_error("Expected 'GLOBAL' after comma in directory hint.");
  2857. return;
  2858. }
  2859. if (!p_class->tool) {
  2860. _set_error("Global filesystem hints may only be used in tool scripts.");
  2861. return;
  2862. }
  2863. current_export.hint = PROPERTY_HINT_GLOBAL_DIR;
  2864. tokenizer->advance();
  2865. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2866. _set_error("Expected ')' in hint.");
  2867. return;
  2868. }
  2869. } else {
  2870. _set_error("Expected ')' or ',' in hint.");
  2871. return;
  2872. }
  2873. break;
  2874. }
  2875. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "FILE") {
  2876. current_export.hint = PROPERTY_HINT_FILE;
  2877. tokenizer->advance();
  2878. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  2879. tokenizer->advance();
  2880. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "GLOBAL") {
  2881. if (!p_class->tool) {
  2882. _set_error("Global filesystem hints may only be used in tool scripts.");
  2883. return;
  2884. }
  2885. current_export.hint = PROPERTY_HINT_GLOBAL_FILE;
  2886. tokenizer->advance();
  2887. if (tokenizer->get_token() == GDTokenizer::TK_PARENTHESIS_CLOSE)
  2888. break;
  2889. else if (tokenizer->get_token() == GDTokenizer::TK_COMMA)
  2890. tokenizer->advance();
  2891. else {
  2892. _set_error("Expected ')' or ',' in hint.");
  2893. return;
  2894. }
  2895. }
  2896. if (tokenizer->get_token() != GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type() != Variant::STRING) {
  2897. if (current_export.hint == PROPERTY_HINT_GLOBAL_FILE)
  2898. _set_error("Expected string constant with filter");
  2899. else
  2900. _set_error("Expected 'GLOBAL' or string constant with filter");
  2901. return;
  2902. }
  2903. current_export.hint_string = tokenizer->get_token_constant();
  2904. tokenizer->advance();
  2905. }
  2906. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2907. _set_error("Expected ')' in hint.");
  2908. return;
  2909. }
  2910. break;
  2911. }
  2912. if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier() == "MULTILINE") {
  2913. current_export.hint = PROPERTY_HINT_MULTILINE_TEXT;
  2914. tokenizer->advance();
  2915. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2916. _set_error("Expected ')' in hint.");
  2917. return;
  2918. }
  2919. break;
  2920. }
  2921. } break;
  2922. case Variant::COLOR: {
  2923. if (tokenizer->get_token() != GDTokenizer::TK_IDENTIFIER) {
  2924. current_export = PropertyInfo();
  2925. _set_error("Color type hint expects RGB or RGBA as hints");
  2926. return;
  2927. }
  2928. String identifier = tokenizer->get_token_identifier();
  2929. if (identifier == "RGB") {
  2930. current_export.hint = PROPERTY_HINT_COLOR_NO_ALPHA;
  2931. } else if (identifier == "RGBA") {
  2932. //none
  2933. } else {
  2934. current_export = PropertyInfo();
  2935. _set_error("Color type hint expects RGB or RGBA as hints");
  2936. return;
  2937. }
  2938. tokenizer->advance();
  2939. } break;
  2940. default: {
  2941. current_export = PropertyInfo();
  2942. _set_error("Type '" + Variant::get_type_name(type) + "' can't take hints.");
  2943. return;
  2944. } break;
  2945. }
  2946. }
  2947. if (current_export.type == Variant::ARRAY && !hint_prefix.empty()) {
  2948. if (current_export.hint) {
  2949. hint_prefix += "/" + itos(current_export.hint);
  2950. }
  2951. current_export.hint_string = hint_prefix + ":" + current_export.hint_string;
  2952. current_export.hint = PROPERTY_HINT_NONE;
  2953. }
  2954. } else if (tokenizer->get_token() == GDTokenizer::TK_IDENTIFIER) {
  2955. String identifier = tokenizer->get_token_identifier();
  2956. if (!ClassDB::is_parent_class(identifier, "Resource")) {
  2957. current_export = PropertyInfo();
  2958. _set_error("Export hint not a type or resource.");
  2959. }
  2960. current_export.type = Variant::OBJECT;
  2961. current_export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  2962. current_export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  2963. current_export.hint_string = identifier;
  2964. tokenizer->advance();
  2965. }
  2966. if (tokenizer->get_token() != GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2967. current_export = PropertyInfo();
  2968. _set_error("Expected ')' or ',' after export hint.");
  2969. return;
  2970. }
  2971. tokenizer->advance();
  2972. }
  2973. 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) {
  2974. current_export = PropertyInfo();
  2975. _set_error("Expected 'var', 'onready', 'remote', 'master', 'slave' or 'sync'.");
  2976. return;
  2977. }
  2978. continue;
  2979. } break;
  2980. case GDTokenizer::TK_PR_ONREADY: {
  2981. //may be fallthrough from export, ignore if so
  2982. tokenizer->advance();
  2983. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR) {
  2984. _set_error("Expected 'var'.");
  2985. return;
  2986. }
  2987. continue;
  2988. } break;
  2989. case GDTokenizer::TK_PR_REMOTE: {
  2990. //may be fallthrough from export, ignore if so
  2991. tokenizer->advance();
  2992. if (current_export.type) {
  2993. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR) {
  2994. _set_error("Expected 'var'.");
  2995. return;
  2996. }
  2997. } else {
  2998. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR && tokenizer->get_token() != GDTokenizer::TK_PR_FUNCTION) {
  2999. _set_error("Expected 'var' or 'func'.");
  3000. return;
  3001. }
  3002. }
  3003. rpc_mode = ScriptInstance::RPC_MODE_REMOTE;
  3004. continue;
  3005. } break;
  3006. case GDTokenizer::TK_PR_MASTER: {
  3007. //may be fallthrough from export, ignore if so
  3008. tokenizer->advance();
  3009. if (current_export.type) {
  3010. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR) {
  3011. _set_error("Expected 'var'.");
  3012. return;
  3013. }
  3014. } else {
  3015. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR && tokenizer->get_token() != GDTokenizer::TK_PR_FUNCTION) {
  3016. _set_error("Expected 'var' or 'func'.");
  3017. return;
  3018. }
  3019. }
  3020. rpc_mode = ScriptInstance::RPC_MODE_MASTER;
  3021. continue;
  3022. } break;
  3023. case GDTokenizer::TK_PR_SLAVE: {
  3024. //may be fallthrough from export, ignore if so
  3025. tokenizer->advance();
  3026. if (current_export.type) {
  3027. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR) {
  3028. _set_error("Expected 'var'.");
  3029. return;
  3030. }
  3031. } else {
  3032. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR && tokenizer->get_token() != GDTokenizer::TK_PR_FUNCTION) {
  3033. _set_error("Expected 'var' or 'func'.");
  3034. return;
  3035. }
  3036. }
  3037. rpc_mode = ScriptInstance::RPC_MODE_SLAVE;
  3038. continue;
  3039. } break;
  3040. case GDTokenizer::TK_PR_SYNC: {
  3041. //may be fallthrough from export, ignore if so
  3042. tokenizer->advance();
  3043. if (tokenizer->get_token() != GDTokenizer::TK_PR_VAR && tokenizer->get_token() != GDTokenizer::TK_PR_FUNCTION) {
  3044. if (current_export.type)
  3045. _set_error("Expected 'var'.");
  3046. else
  3047. _set_error("Expected 'var' or 'func'.");
  3048. return;
  3049. }
  3050. rpc_mode = ScriptInstance::RPC_MODE_SYNC;
  3051. continue;
  3052. } break;
  3053. case GDTokenizer::TK_PR_VAR: {
  3054. //variale declaration and (eventual) initialization
  3055. ClassNode::Member member;
  3056. bool autoexport = tokenizer->get_token(-1) == GDTokenizer::TK_PR_EXPORT;
  3057. if (current_export.type != Variant::NIL) {
  3058. member._export = current_export;
  3059. current_export = PropertyInfo();
  3060. }
  3061. bool onready = tokenizer->get_token(-1) == GDTokenizer::TK_PR_ONREADY;
  3062. tokenizer->advance();
  3063. if (!tokenizer->is_token_literal(0, true)) {
  3064. _set_error("Expected identifier for member variable name.");
  3065. return;
  3066. }
  3067. member.identifier = tokenizer->get_token_literal();
  3068. member.expression = NULL;
  3069. member._export.name = member.identifier;
  3070. member.line = tokenizer->get_token_line();
  3071. member.rpc_mode = rpc_mode;
  3072. tokenizer->advance();
  3073. rpc_mode = ScriptInstance::RPC_MODE_DISABLED;
  3074. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  3075. #ifdef DEBUG_ENABLED
  3076. int line = tokenizer->get_token_line();
  3077. #endif
  3078. tokenizer->advance();
  3079. Node *subexpr = _parse_and_reduce_expression(p_class, false, autoexport);
  3080. if (!subexpr) {
  3081. if (_recover_from_completion()) {
  3082. break;
  3083. }
  3084. return;
  3085. }
  3086. //discourage common error
  3087. if (!onready && subexpr->type == Node::TYPE_OPERATOR) {
  3088. OperatorNode *op = static_cast<OperatorNode *>(subexpr);
  3089. if (op->op == OperatorNode::OP_CALL && op->arguments[0]->type == Node::TYPE_SELF && op->arguments[1]->type == Node::TYPE_IDENTIFIER) {
  3090. IdentifierNode *id = static_cast<IdentifierNode *>(op->arguments[1]);
  3091. if (id->name == "get_node") {
  3092. _set_error("Use 'onready var " + String(member.identifier) + " = get_node(..)' instead");
  3093. return;
  3094. }
  3095. }
  3096. }
  3097. member.expression = subexpr;
  3098. if (autoexport) {
  3099. if (1) /*(subexpr->type==Node::TYPE_ARRAY) {
  3100. member._export.type=Variant::ARRAY;
  3101. } else if (subexpr->type==Node::TYPE_DICTIONARY) {
  3102. member._export.type=Variant::DICTIONARY;
  3103. } else*/ {
  3104. if (subexpr->type != Node::TYPE_CONSTANT) {
  3105. _set_error("Type-less export needs a constant expression assigned to infer type.");
  3106. return;
  3107. }
  3108. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  3109. if (cn->value.get_type() == Variant::NIL) {
  3110. _set_error("Can't accept a null constant expression for infering export type.");
  3111. return;
  3112. }
  3113. member._export.type = cn->value.get_type();
  3114. member._export.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  3115. if (cn->value.get_type() == Variant::OBJECT) {
  3116. Object *obj = cn->value;
  3117. Resource *res = obj->cast_to<Resource>();
  3118. if (res == NULL) {
  3119. _set_error("Exported constant not a type or resource.");
  3120. return;
  3121. }
  3122. member._export.hint = PROPERTY_HINT_RESOURCE_TYPE;
  3123. member._export.hint_string = res->get_class();
  3124. }
  3125. }
  3126. }
  3127. #ifdef TOOLS_ENABLED
  3128. if (subexpr->type == Node::TYPE_CONSTANT && member._export.type != Variant::NIL) {
  3129. ConstantNode *cn = static_cast<ConstantNode *>(subexpr);
  3130. if (cn->value.get_type() != Variant::NIL) {
  3131. member.default_value = cn->value;
  3132. }
  3133. }
  3134. #endif
  3135. IdentifierNode *id = alloc_node<IdentifierNode>();
  3136. id->name = member.identifier;
  3137. OperatorNode *op = alloc_node<OperatorNode>();
  3138. op->op = OperatorNode::OP_INIT_ASSIGN;
  3139. op->arguments.push_back(id);
  3140. op->arguments.push_back(subexpr);
  3141. #ifdef DEBUG_ENABLED
  3142. NewLineNode *nl = alloc_node<NewLineNode>();
  3143. nl->line = line;
  3144. if (onready)
  3145. p_class->ready->statements.push_back(nl);
  3146. else
  3147. p_class->initializer->statements.push_back(nl);
  3148. #endif
  3149. if (onready)
  3150. p_class->ready->statements.push_back(op);
  3151. else
  3152. p_class->initializer->statements.push_back(op);
  3153. } else {
  3154. if (autoexport) {
  3155. _set_error("Type-less export needs a constant expression assigned to infer type.");
  3156. return;
  3157. }
  3158. }
  3159. if (tokenizer->get_token() == GDTokenizer::TK_PR_SETGET) {
  3160. tokenizer->advance();
  3161. if (tokenizer->get_token() != GDTokenizer::TK_COMMA) {
  3162. //just comma means using only getter
  3163. if (!tokenizer->is_token_literal()) {
  3164. _set_error("Expected identifier for setter function after 'setget'.");
  3165. }
  3166. member.setter = tokenizer->get_token_literal();
  3167. tokenizer->advance();
  3168. }
  3169. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  3170. //there is a getter
  3171. tokenizer->advance();
  3172. if (!tokenizer->is_token_literal()) {
  3173. _set_error("Expected identifier for getter function after ','.");
  3174. }
  3175. member.getter = tokenizer->get_token_literal();
  3176. tokenizer->advance();
  3177. }
  3178. }
  3179. p_class->variables.push_back(member);
  3180. if (!_end_statement()) {
  3181. _set_error("Expected end of statement (continue)");
  3182. return;
  3183. }
  3184. } break;
  3185. case GDTokenizer::TK_PR_CONST: {
  3186. //variale declaration and (eventual) initialization
  3187. ClassNode::Constant constant;
  3188. tokenizer->advance();
  3189. if (!tokenizer->is_token_literal(0, true)) {
  3190. _set_error("Expected name (identifier) for constant.");
  3191. return;
  3192. }
  3193. constant.identifier = tokenizer->get_token_literal();
  3194. tokenizer->advance();
  3195. if (tokenizer->get_token() != GDTokenizer::TK_OP_ASSIGN) {
  3196. _set_error("Constant expects assignment.");
  3197. return;
  3198. }
  3199. tokenizer->advance();
  3200. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3201. if (!subexpr) {
  3202. if (_recover_from_completion()) {
  3203. break;
  3204. }
  3205. return;
  3206. }
  3207. if (subexpr->type != Node::TYPE_CONSTANT) {
  3208. _set_error("Expected constant expression");
  3209. }
  3210. constant.expression = subexpr;
  3211. p_class->constant_expressions.push_back(constant);
  3212. if (!_end_statement()) {
  3213. _set_error("Expected end of statement (constant)");
  3214. return;
  3215. }
  3216. } break;
  3217. case GDTokenizer::TK_PR_ENUM: {
  3218. //mutiple constant declarations..
  3219. int last_assign = -1; // Incremented by 1 right before the assingment.
  3220. String enum_name;
  3221. Dictionary enum_dict;
  3222. tokenizer->advance();
  3223. if (tokenizer->is_token_literal(0, true)) {
  3224. enum_name = tokenizer->get_token_literal();
  3225. tokenizer->advance();
  3226. }
  3227. if (tokenizer->get_token() != GDTokenizer::TK_CURLY_BRACKET_OPEN) {
  3228. _set_error("Expected '{' in enum declaration");
  3229. return;
  3230. }
  3231. tokenizer->advance();
  3232. while (true) {
  3233. if (tokenizer->get_token() == GDTokenizer::TK_NEWLINE) {
  3234. tokenizer->advance(); // Ignore newlines
  3235. } else if (tokenizer->get_token() == GDTokenizer::TK_CURLY_BRACKET_CLOSE) {
  3236. tokenizer->advance();
  3237. break; // End of enum
  3238. } else if (!tokenizer->is_token_literal(0, true)) {
  3239. if (tokenizer->get_token() == GDTokenizer::TK_EOF) {
  3240. _set_error("Unexpected end of file.");
  3241. } else {
  3242. _set_error(String("Unexpected ") + GDTokenizer::get_token_name(tokenizer->get_token()) + ", expected identifier");
  3243. }
  3244. return;
  3245. } else { // tokenizer->is_token_literal(0, true)
  3246. ClassNode::Constant constant;
  3247. constant.identifier = tokenizer->get_token_literal();
  3248. tokenizer->advance();
  3249. if (tokenizer->get_token() == GDTokenizer::TK_OP_ASSIGN) {
  3250. tokenizer->advance();
  3251. Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
  3252. if (!subexpr) {
  3253. if (_recover_from_completion()) {
  3254. break;
  3255. }
  3256. return;
  3257. }
  3258. if (subexpr->type != Node::TYPE_CONSTANT) {
  3259. _set_error("Expected constant expression");
  3260. }
  3261. const ConstantNode *subexpr_const = static_cast<const ConstantNode *>(subexpr);
  3262. if (subexpr_const->value.get_type() != Variant::INT) {
  3263. _set_error("Expected an int value for enum");
  3264. }
  3265. last_assign = subexpr_const->value;
  3266. constant.expression = subexpr;
  3267. } else {
  3268. last_assign = last_assign + 1;
  3269. ConstantNode *cn = alloc_node<ConstantNode>();
  3270. cn->value = last_assign;
  3271. constant.expression = cn;
  3272. }
  3273. if (tokenizer->get_token() == GDTokenizer::TK_COMMA) {
  3274. tokenizer->advance();
  3275. }
  3276. if (enum_name != "") {
  3277. const ConstantNode *cn = static_cast<const ConstantNode *>(constant.expression);
  3278. enum_dict[constant.identifier] = cn->value;
  3279. }
  3280. p_class->constant_expressions.push_back(constant);
  3281. }
  3282. }
  3283. if (enum_name != "") {
  3284. ClassNode::Constant enum_constant;
  3285. enum_constant.identifier = enum_name;
  3286. ConstantNode *cn = alloc_node<ConstantNode>();
  3287. cn->value = enum_dict;
  3288. enum_constant.expression = cn;
  3289. p_class->constant_expressions.push_back(enum_constant);
  3290. }
  3291. if (!_end_statement()) {
  3292. _set_error("Expected end of statement (enum)");
  3293. return;
  3294. }
  3295. } break;
  3296. case GDTokenizer::TK_CONSTANT: {
  3297. if (tokenizer->get_token_constant().get_type() == Variant::STRING) {
  3298. tokenizer->advance();
  3299. // Ignore
  3300. } else {
  3301. _set_error(String() + "Unexpected constant of type: " + Variant::get_type_name(tokenizer->get_token_constant().get_type()));
  3302. return;
  3303. }
  3304. } break;
  3305. default: {
  3306. _set_error(String() + "Unexpected token: " + tokenizer->get_token_name(tokenizer->get_token()) + ":" + tokenizer->get_token_identifier());
  3307. return;
  3308. } break;
  3309. }
  3310. }
  3311. }
  3312. void GDParser::_set_error(const String &p_error, int p_line, int p_column) {
  3313. if (error_set)
  3314. return; //allow no further errors
  3315. error = p_error;
  3316. error_line = p_line < 0 ? tokenizer->get_token_line() : p_line;
  3317. error_column = p_column < 0 ? tokenizer->get_token_column() : p_column;
  3318. error_set = true;
  3319. }
  3320. String GDParser::get_error() const {
  3321. return error;
  3322. }
  3323. int GDParser::get_error_line() const {
  3324. return error_line;
  3325. }
  3326. int GDParser::get_error_column() const {
  3327. return error_column;
  3328. }
  3329. Error GDParser::_parse(const String &p_base_path) {
  3330. base_path = p_base_path;
  3331. clear();
  3332. //assume class
  3333. ClassNode *main_class = alloc_node<ClassNode>();
  3334. main_class->initializer = alloc_node<BlockNode>();
  3335. main_class->initializer->parent_class = main_class;
  3336. main_class->ready = alloc_node<BlockNode>();
  3337. main_class->ready->parent_class = main_class;
  3338. current_class = main_class;
  3339. _parse_class(main_class);
  3340. if (tokenizer->get_token() == GDTokenizer::TK_ERROR) {
  3341. error_set = false;
  3342. _set_error("Parse Error: " + tokenizer->get_token_error());
  3343. }
  3344. if (error_set) {
  3345. return ERR_PARSE_ERROR;
  3346. }
  3347. return OK;
  3348. }
  3349. Error GDParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path, const String &p_self_path) {
  3350. for_completion = false;
  3351. validating = false;
  3352. completion_type = COMPLETION_NONE;
  3353. completion_node = NULL;
  3354. completion_class = NULL;
  3355. completion_function = NULL;
  3356. completion_block = NULL;
  3357. completion_found = false;
  3358. current_block = NULL;
  3359. current_class = NULL;
  3360. current_function = NULL;
  3361. self_path = p_self_path;
  3362. GDTokenizerBuffer *tb = memnew(GDTokenizerBuffer);
  3363. tb->set_code_buffer(p_bytecode);
  3364. tokenizer = tb;
  3365. Error ret = _parse(p_base_path);
  3366. memdelete(tb);
  3367. tokenizer = NULL;
  3368. return ret;
  3369. }
  3370. 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) {
  3371. completion_type = COMPLETION_NONE;
  3372. completion_node = NULL;
  3373. completion_class = NULL;
  3374. completion_function = NULL;
  3375. completion_block = NULL;
  3376. completion_found = false;
  3377. current_block = NULL;
  3378. current_class = NULL;
  3379. current_function = NULL;
  3380. self_path = p_self_path;
  3381. GDTokenizerText *tt = memnew(GDTokenizerText);
  3382. tt->set_code(p_code);
  3383. validating = p_just_validate;
  3384. for_completion = p_for_completion;
  3385. tokenizer = tt;
  3386. Error ret = _parse(p_base_path);
  3387. memdelete(tt);
  3388. tokenizer = NULL;
  3389. return ret;
  3390. }
  3391. bool GDParser::is_tool_script() const {
  3392. return (head && head->type == Node::TYPE_CLASS && static_cast<const ClassNode *>(head)->tool);
  3393. }
  3394. const GDParser::Node *GDParser::get_parse_tree() const {
  3395. return head;
  3396. }
  3397. void GDParser::clear() {
  3398. while (list) {
  3399. Node *l = list;
  3400. list = list->next;
  3401. memdelete(l);
  3402. }
  3403. head = NULL;
  3404. list = NULL;
  3405. completion_type = COMPLETION_NONE;
  3406. completion_node = NULL;
  3407. completion_class = NULL;
  3408. completion_function = NULL;
  3409. completion_block = NULL;
  3410. current_block = NULL;
  3411. current_class = NULL;
  3412. completion_found = false;
  3413. rpc_mode = ScriptInstance::RPC_MODE_DISABLED;
  3414. current_function = NULL;
  3415. validating = false;
  3416. for_completion = false;
  3417. error_set = false;
  3418. tab_level.clear();
  3419. tab_level.push_back(0);
  3420. error_line = 0;
  3421. error_column = 0;
  3422. pending_newline = -1;
  3423. parenthesis = 0;
  3424. current_export.type = Variant::NIL;
  3425. error = "";
  3426. }
  3427. GDParser::CompletionType GDParser::get_completion_type() {
  3428. return completion_type;
  3429. }
  3430. StringName GDParser::get_completion_cursor() {
  3431. return completion_cursor;
  3432. }
  3433. int GDParser::get_completion_line() {
  3434. return completion_line;
  3435. }
  3436. Variant::Type GDParser::get_completion_built_in_constant() {
  3437. return completion_built_in_constant;
  3438. }
  3439. GDParser::Node *GDParser::get_completion_node() {
  3440. return completion_node;
  3441. }
  3442. GDParser::BlockNode *GDParser::get_completion_block() {
  3443. return completion_block;
  3444. }
  3445. GDParser::ClassNode *GDParser::get_completion_class() {
  3446. return completion_class;
  3447. }
  3448. GDParser::FunctionNode *GDParser::get_completion_function() {
  3449. return completion_function;
  3450. }
  3451. int GDParser::get_completion_argument_index() {
  3452. return completion_argument;
  3453. }
  3454. int GDParser::get_completion_identifier_is_function() {
  3455. return completion_ident_is_call;
  3456. }
  3457. GDParser::GDParser() {
  3458. head = NULL;
  3459. list = NULL;
  3460. tokenizer = NULL;
  3461. pending_newline = -1;
  3462. clear();
  3463. }
  3464. GDParser::~GDParser() {
  3465. clear();
  3466. }