gd_parser.cpp 121 KB

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