gd_parser.cpp 120 KB

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