2
0

gd_parser.cpp 126 KB

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