gd_parser.cpp 121 KB

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