gd_parser.cpp 103 KB

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