gd_parser.cpp 120 KB

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