gd_parser.cpp 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728
  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. p_block->variables.push_back(n); //line?
  1333. p_block->variable_lines.push_back(tokenizer->get_token_line());
  1334. //must know when the local variable is declared
  1335. LocalVarNode *lv = alloc_node<LocalVarNode>();
  1336. lv->name=n;
  1337. p_block->statements.push_back(lv);
  1338. Node *assigned=NULL;
  1339. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  1340. tokenizer->advance();
  1341. Node *subexpr=NULL;
  1342. subexpr = _parse_and_reduce_expression(p_block,p_static);
  1343. if (!subexpr) {
  1344. if (_recover_from_completion()) {
  1345. break;
  1346. }
  1347. return;
  1348. }
  1349. lv->assign=subexpr;
  1350. assigned=subexpr;
  1351. } else {
  1352. ConstantNode *c = alloc_node<ConstantNode>();
  1353. c->value=Variant();
  1354. assigned = c;
  1355. }
  1356. IdentifierNode *id = alloc_node<IdentifierNode>();
  1357. id->name=n;
  1358. OperatorNode *op = alloc_node<OperatorNode>();
  1359. op->op=OperatorNode::OP_ASSIGN;
  1360. op->arguments.push_back(id);
  1361. op->arguments.push_back(assigned);
  1362. p_block->statements.push_back(op);
  1363. if (!_end_statement()) {
  1364. _set_error("Expected end of statement (var)");
  1365. return;
  1366. }
  1367. } break;
  1368. case GDTokenizer::TK_CF_IF: {
  1369. tokenizer->advance();
  1370. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1371. if (!condition) {
  1372. if (_recover_from_completion()) {
  1373. break;
  1374. }
  1375. return;
  1376. }
  1377. ControlFlowNode *cf_if = alloc_node<ControlFlowNode>();
  1378. cf_if->cf_type=ControlFlowNode::CF_IF;
  1379. cf_if->arguments.push_back(condition);
  1380. cf_if->body = alloc_node<BlockNode>();
  1381. cf_if->body->parent_block=p_block;
  1382. p_block->sub_blocks.push_back(cf_if->body);
  1383. if (!_enter_indent_block(cf_if->body)) {
  1384. _set_error("Expected intended block after 'if'");
  1385. p_block->end_line=tokenizer->get_token_line();
  1386. return;
  1387. }
  1388. current_block=cf_if->body;
  1389. _parse_block(cf_if->body,p_static);
  1390. current_block=p_block;
  1391. if (error_set)
  1392. return;
  1393. p_block->statements.push_back(cf_if);
  1394. while(true) {
  1395. while(tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  1396. tokenizer->advance();
  1397. }
  1398. if (tab_level.back()->get() < indent_level) { //not at current indent level
  1399. p_block->end_line=tokenizer->get_token_line();
  1400. return;
  1401. }
  1402. if (tokenizer->get_token()==GDTokenizer::TK_CF_ELIF) {
  1403. if (tab_level.back()->get() > indent_level) {
  1404. _set_error("Invalid indent");
  1405. return;
  1406. }
  1407. tokenizer->advance();
  1408. cf_if->body_else=alloc_node<BlockNode>();
  1409. cf_if->body_else->parent_block=p_block;
  1410. p_block->sub_blocks.push_back(cf_if->body_else);
  1411. ControlFlowNode *cf_else = alloc_node<ControlFlowNode>();
  1412. cf_else->cf_type=ControlFlowNode::CF_IF;
  1413. //condition
  1414. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1415. if (!condition) {
  1416. if (_recover_from_completion()) {
  1417. break;
  1418. }
  1419. return;
  1420. }
  1421. cf_else->arguments.push_back(condition);
  1422. cf_else->cf_type=ControlFlowNode::CF_IF;
  1423. cf_if->body_else->statements.push_back(cf_else);
  1424. cf_if=cf_else;
  1425. cf_if->body=alloc_node<BlockNode>();
  1426. cf_if->body->parent_block=p_block;
  1427. p_block->sub_blocks.push_back(cf_if->body);
  1428. if (!_enter_indent_block(cf_if->body)) {
  1429. _set_error("Expected indented block after 'elif'");
  1430. p_block->end_line=tokenizer->get_token_line();
  1431. return;
  1432. }
  1433. current_block=cf_else->body;
  1434. _parse_block(cf_else->body,p_static);
  1435. current_block=p_block;
  1436. if (error_set)
  1437. return;
  1438. } else if (tokenizer->get_token()==GDTokenizer::TK_CF_ELSE) {
  1439. if (tab_level.back()->get() > indent_level) {
  1440. _set_error("Invalid indent");
  1441. return;
  1442. }
  1443. tokenizer->advance();
  1444. cf_if->body_else=alloc_node<BlockNode>();
  1445. cf_if->body_else->parent_block=p_block;
  1446. p_block->sub_blocks.push_back(cf_if->body_else);
  1447. if (!_enter_indent_block(cf_if->body_else)) {
  1448. _set_error("Expected indented block after 'else'");
  1449. p_block->end_line=tokenizer->get_token_line();
  1450. return;
  1451. }
  1452. current_block=cf_if->body_else;
  1453. _parse_block(cf_if->body_else,p_static);
  1454. current_block=p_block;
  1455. if (error_set)
  1456. return;
  1457. break; //after else, exit
  1458. } else
  1459. break;
  1460. }
  1461. } break;
  1462. case GDTokenizer::TK_CF_WHILE: {
  1463. tokenizer->advance();
  1464. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1465. if (!condition) {
  1466. if (_recover_from_completion()) {
  1467. break;
  1468. }
  1469. return;
  1470. }
  1471. ControlFlowNode *cf_while = alloc_node<ControlFlowNode>();
  1472. cf_while->cf_type=ControlFlowNode::CF_WHILE;
  1473. cf_while->arguments.push_back(condition);
  1474. cf_while->body = alloc_node<BlockNode>();
  1475. cf_while->body->parent_block=p_block;
  1476. p_block->sub_blocks.push_back(cf_while->body);
  1477. if (!_enter_indent_block(cf_while->body)) {
  1478. _set_error("Expected indented block after 'while'");
  1479. p_block->end_line=tokenizer->get_token_line();
  1480. return;
  1481. }
  1482. current_block=cf_while->body;
  1483. _parse_block(cf_while->body,p_static);
  1484. current_block=p_block;
  1485. if (error_set)
  1486. return;
  1487. p_block->statements.push_back(cf_while);
  1488. } break;
  1489. case GDTokenizer::TK_CF_FOR: {
  1490. tokenizer->advance();
  1491. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1492. _set_error("identifier expected after 'for'");
  1493. }
  1494. IdentifierNode *id = alloc_node<IdentifierNode>();
  1495. id->name=tokenizer->get_token_identifier();
  1496. tokenizer->advance();
  1497. if (tokenizer->get_token()!=GDTokenizer::TK_OP_IN) {
  1498. _set_error("'in' expected after identifier");
  1499. return;
  1500. }
  1501. tokenizer->advance();
  1502. Node *container = _parse_and_reduce_expression(p_block,p_static);
  1503. if (!container) {
  1504. if (_recover_from_completion()) {
  1505. break;
  1506. }
  1507. return;
  1508. }
  1509. ControlFlowNode *cf_for = alloc_node<ControlFlowNode>();
  1510. cf_for->cf_type=ControlFlowNode::CF_FOR;
  1511. cf_for->arguments.push_back(id);
  1512. cf_for->arguments.push_back(container);
  1513. cf_for->body = alloc_node<BlockNode>();
  1514. cf_for->body->parent_block=p_block;
  1515. p_block->sub_blocks.push_back(cf_for->body);
  1516. if (!_enter_indent_block(cf_for->body)) {
  1517. _set_error("Expected indented block after 'for'");
  1518. p_block->end_line=tokenizer->get_token_line();
  1519. return;
  1520. }
  1521. current_block=cf_for->body;
  1522. _parse_block(cf_for->body,p_static);
  1523. current_block=p_block;
  1524. if (error_set)
  1525. return;
  1526. p_block->statements.push_back(cf_for);
  1527. } break;
  1528. case GDTokenizer::TK_CF_CONTINUE: {
  1529. tokenizer->advance();
  1530. ControlFlowNode *cf_continue = alloc_node<ControlFlowNode>();
  1531. cf_continue->cf_type=ControlFlowNode::CF_CONTINUE;
  1532. p_block->statements.push_back(cf_continue);
  1533. if (!_end_statement()) {
  1534. _set_error("Expected end of statement (continue)");
  1535. return;
  1536. }
  1537. } break;
  1538. case GDTokenizer::TK_CF_BREAK: {
  1539. tokenizer->advance();
  1540. ControlFlowNode *cf_break = alloc_node<ControlFlowNode>();
  1541. cf_break->cf_type=ControlFlowNode::CF_BREAK;
  1542. p_block->statements.push_back(cf_break);
  1543. if (!_end_statement()) {
  1544. _set_error("Expected end of statement (break)");
  1545. return;
  1546. }
  1547. } break;
  1548. case GDTokenizer::TK_CF_RETURN: {
  1549. tokenizer->advance();
  1550. ControlFlowNode *cf_return = alloc_node<ControlFlowNode>();
  1551. cf_return->cf_type=ControlFlowNode::CF_RETURN;
  1552. if (tokenizer->get_token()==GDTokenizer::TK_SEMICOLON || tokenizer->get_token()==GDTokenizer::TK_NEWLINE || tokenizer->get_token()==GDTokenizer::TK_EOF) {
  1553. //expect end of statement
  1554. p_block->statements.push_back(cf_return);
  1555. if (!_end_statement()) {
  1556. return;
  1557. }
  1558. } else {
  1559. //expect expression
  1560. Node *retexpr = _parse_and_reduce_expression(p_block,p_static);
  1561. if (!retexpr) {
  1562. if (_recover_from_completion()) {
  1563. break;
  1564. }
  1565. return;
  1566. }
  1567. cf_return->arguments.push_back(retexpr);
  1568. p_block->statements.push_back(cf_return);
  1569. if (!_end_statement()) {
  1570. _set_error("Expected end of statement after return expression.");
  1571. return;
  1572. }
  1573. }
  1574. } break;
  1575. case GDTokenizer::TK_PR_ASSERT: {
  1576. tokenizer->advance();
  1577. Node *condition = _parse_and_reduce_expression(p_block,p_static);
  1578. if (!condition) {
  1579. if (_recover_from_completion()) {
  1580. break;
  1581. }
  1582. return;
  1583. }
  1584. AssertNode *an = alloc_node<AssertNode>();
  1585. an->condition=condition;
  1586. p_block->statements.push_back(an);
  1587. if (!_end_statement()) {
  1588. _set_error("Expected end of statement after assert.");
  1589. return;
  1590. }
  1591. } break;
  1592. case GDTokenizer::TK_PR_BREAKPOINT: {
  1593. tokenizer->advance();
  1594. BreakpointNode *bn = alloc_node<BreakpointNode>();
  1595. p_block->statements.push_back(bn);
  1596. if (!_end_statement()) {
  1597. _set_error("Expected end of statement after breakpoint.");
  1598. return;
  1599. }
  1600. } break;
  1601. default: {
  1602. Node *expression = _parse_and_reduce_expression(p_block,p_static,false,true);
  1603. if (!expression) {
  1604. if (_recover_from_completion()) {
  1605. break;
  1606. }
  1607. return;
  1608. }
  1609. p_block->statements.push_back(expression);
  1610. if (!_end_statement()) {
  1611. _set_error("Expected end of statement after expression.");
  1612. return;
  1613. }
  1614. } break;
  1615. /*
  1616. case GDTokenizer::TK_CF_LOCAL: {
  1617. if (tokenizer->get_token(1)!=GDTokenizer::TK_SEMICOLON && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE ) {
  1618. _set_error("Expected ';' or <NewLine>.");
  1619. }
  1620. tokenizer->advance();
  1621. } break;
  1622. */
  1623. }
  1624. }
  1625. }
  1626. bool GDParser::_parse_newline() {
  1627. if (tokenizer->get_token(1)!=GDTokenizer::TK_EOF && tokenizer->get_token(1)!=GDTokenizer::TK_NEWLINE) {
  1628. int indent = tokenizer->get_token_line_indent();
  1629. int current_indent = tab_level.back()->get();
  1630. if (indent>current_indent) {
  1631. _set_error("Unexpected indent.");
  1632. return false;
  1633. }
  1634. if (indent<current_indent) {
  1635. while(indent<current_indent) {
  1636. //exit block
  1637. if (tab_level.size()==1) {
  1638. _set_error("Invalid indent. BUG?");
  1639. return false;
  1640. }
  1641. tab_level.pop_back();
  1642. if (tab_level.back()->get()<indent) {
  1643. _set_error("Unindent does not match any outer indentation level.");
  1644. return false;
  1645. }
  1646. current_indent = tab_level.back()->get();
  1647. }
  1648. tokenizer->advance();
  1649. return false;
  1650. }
  1651. }
  1652. tokenizer->advance();
  1653. return true;
  1654. }
  1655. void GDParser::_parse_extends(ClassNode *p_class) {
  1656. if (p_class->extends_used) {
  1657. _set_error("'extends' already used for this class.");
  1658. return;
  1659. }
  1660. if (!p_class->constant_expressions.empty() || !p_class->subclasses.empty() || !p_class->functions.empty() || !p_class->variables.empty()) {
  1661. _set_error("'extends' must be used before anything else.");
  1662. return;
  1663. }
  1664. p_class->extends_used=true;
  1665. tokenizer->advance();
  1666. if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token_type()==Variant::OBJECT) {
  1667. p_class->extends_class.push_back(Variant::get_type_name(Variant::OBJECT));
  1668. tokenizer->advance();
  1669. return;
  1670. }
  1671. // see if inheritance happens from a file
  1672. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT) {
  1673. Variant constant = tokenizer->get_token_constant();
  1674. if (constant.get_type()!=Variant::STRING) {
  1675. _set_error("'extends' constant must be a string.");
  1676. return;
  1677. }
  1678. p_class->extends_file=constant;
  1679. tokenizer->advance();
  1680. if (tokenizer->get_token()!=GDTokenizer::TK_PERIOD) {
  1681. return;
  1682. } else
  1683. tokenizer->advance();
  1684. }
  1685. while(true) {
  1686. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1687. _set_error("Invalid 'extends' syntax, expected string constant (path) and/or identifier (parent class).");
  1688. return;
  1689. }
  1690. StringName identifier=tokenizer->get_token_identifier();
  1691. p_class->extends_class.push_back(identifier);
  1692. tokenizer->advance(1);
  1693. if (tokenizer->get_token()!=GDTokenizer::TK_PERIOD)
  1694. return;
  1695. }
  1696. }
  1697. void GDParser::_parse_class(ClassNode *p_class) {
  1698. int indent_level = tab_level.back()->get();
  1699. while(true) {
  1700. GDTokenizer::Token token = tokenizer->get_token();
  1701. if (error_set)
  1702. return;
  1703. if (indent_level>tab_level.back()->get()) {
  1704. p_class->end_line=tokenizer->get_token_line();
  1705. return; //go back a level
  1706. }
  1707. switch(token) {
  1708. case GDTokenizer::TK_EOF:
  1709. p_class->end_line=tokenizer->get_token_line();
  1710. case GDTokenizer::TK_ERROR: {
  1711. return; //go back
  1712. //end of file!
  1713. } break;
  1714. case GDTokenizer::TK_NEWLINE: {
  1715. if (!_parse_newline()) {
  1716. if (!error_set) {
  1717. p_class->end_line=tokenizer->get_token_line();
  1718. }
  1719. return;
  1720. }
  1721. } break;
  1722. case GDTokenizer::TK_PR_EXTENDS: {
  1723. _parse_extends(p_class);
  1724. if (error_set)
  1725. return;
  1726. if (!_end_statement()) {
  1727. _set_error("Expected end of statement after extends");
  1728. return;
  1729. }
  1730. } break;
  1731. case GDTokenizer::TK_PR_TOOL: {
  1732. if (p_class->tool) {
  1733. _set_error("tool used more than once");
  1734. return;
  1735. }
  1736. p_class->tool=true;
  1737. tokenizer->advance();
  1738. } break;
  1739. case GDTokenizer::TK_PR_CLASS: {
  1740. //class inside class :D
  1741. StringName name;
  1742. StringName extends;
  1743. if (tokenizer->get_token(1)!=GDTokenizer::TK_IDENTIFIER) {
  1744. _set_error("'class' syntax: 'class <Name>:' or 'class <Name> extends <BaseClass>:'");
  1745. return;
  1746. }
  1747. name = tokenizer->get_token_identifier(1);
  1748. tokenizer->advance(2);
  1749. ClassNode *newclass = alloc_node<ClassNode>();
  1750. newclass->initializer = alloc_node<BlockNode>();
  1751. newclass->initializer->parent_class=newclass;
  1752. newclass->ready = alloc_node<BlockNode>();
  1753. newclass->ready->parent_class=newclass;
  1754. newclass->name=name;
  1755. newclass->owner=p_class;
  1756. p_class->subclasses.push_back(newclass);
  1757. if (tokenizer->get_token()==GDTokenizer::TK_PR_EXTENDS) {
  1758. _parse_extends(newclass);
  1759. if (error_set)
  1760. return;
  1761. }
  1762. if (!_enter_indent_block()) {
  1763. _set_error("Indented block expected.");
  1764. return;
  1765. }
  1766. current_class=newclass;
  1767. _parse_class(newclass);
  1768. current_class=p_class;
  1769. } break;
  1770. /* this is for functions....
  1771. case GDTokenizer::TK_CF_PASS: {
  1772. tokenizer->advance(1);
  1773. } break;
  1774. */
  1775. case GDTokenizer::TK_PR_STATIC: {
  1776. tokenizer->advance();
  1777. if (tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
  1778. _set_error("Expected 'func'.");
  1779. return;
  1780. }
  1781. }; //fallthrough to function
  1782. case GDTokenizer::TK_PR_FUNCTION: {
  1783. bool _static=false;
  1784. pending_newline=-1;
  1785. if (tokenizer->get_token(-1)==GDTokenizer::TK_PR_STATIC) {
  1786. _static=true;
  1787. }
  1788. tokenizer->advance();
  1789. StringName name;
  1790. if (_get_completable_identifier(COMPLETION_VIRTUAL_FUNC,name)) {
  1791. }
  1792. if (name==StringName()) {
  1793. _set_error("Expected identifier after 'func' (syntax: 'func <identifier>([arguments]):' ).");
  1794. return;
  1795. }
  1796. for(int i=0;i<p_class->functions.size();i++) {
  1797. if (p_class->functions[i]->name==name) {
  1798. _set_error("Function '"+String(name)+"' already exists in this class (at line: "+itos(p_class->functions[i]->line)+").");
  1799. }
  1800. }
  1801. for(int i=0;i<p_class->static_functions.size();i++) {
  1802. if (p_class->static_functions[i]->name==name) {
  1803. _set_error("Function '"+String(name)+"' already exists in this class (at line: "+itos(p_class->static_functions[i]->line)+").");
  1804. }
  1805. }
  1806. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_OPEN) {
  1807. _set_error("Expected '(' after identifier (syntax: 'func <identifier>([arguments]):' ).");
  1808. return;
  1809. }
  1810. tokenizer->advance();
  1811. Vector<StringName> arguments;
  1812. Vector<Node*> default_values;
  1813. int fnline = tokenizer->get_token_line();
  1814. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1815. //has arguments
  1816. bool defaulting=false;
  1817. while(true) {
  1818. if (tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  1819. tokenizer->advance();
  1820. continue;
  1821. }
  1822. if (tokenizer->get_token()==GDTokenizer::TK_PR_VAR) {
  1823. tokenizer->advance(); //var before the identifier is allowed
  1824. }
  1825. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1826. _set_error("Expected identifier for argument.");
  1827. return;
  1828. }
  1829. StringName argname=tokenizer->get_token_identifier();
  1830. arguments.push_back(argname);
  1831. tokenizer->advance();
  1832. if (defaulting && tokenizer->get_token()!=GDTokenizer::TK_OP_ASSIGN) {
  1833. _set_error("Default parameter expected.");
  1834. return;
  1835. }
  1836. //tokenizer->advance();
  1837. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  1838. defaulting=true;
  1839. tokenizer->advance(1);
  1840. Node *defval=NULL;
  1841. defval=_parse_and_reduce_expression(p_class,_static);
  1842. if (!defval || error_set)
  1843. return;
  1844. OperatorNode *on = alloc_node<OperatorNode>();
  1845. on->op=OperatorNode::OP_ASSIGN;
  1846. IdentifierNode *in = alloc_node<IdentifierNode>();
  1847. in->name=argname;
  1848. on->arguments.push_back(in);
  1849. on->arguments.push_back(defval);
  1850. /* no ..
  1851. if (defval->type!=Node::TYPE_CONSTANT) {
  1852. _set_error("default argument must be constant");
  1853. }
  1854. */
  1855. default_values.push_back(on);
  1856. }
  1857. while (tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  1858. tokenizer->advance();
  1859. }
  1860. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1861. tokenizer->advance();
  1862. continue;
  1863. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1864. _set_error("Expected ',' or ')'.");
  1865. return;
  1866. }
  1867. break;
  1868. }
  1869. }
  1870. tokenizer->advance();
  1871. BlockNode *block = alloc_node<BlockNode>();
  1872. block->parent_class=p_class;
  1873. if (name=="_init") {
  1874. if (p_class->extends_used) {
  1875. OperatorNode *cparent = alloc_node<OperatorNode>();
  1876. cparent->op=OperatorNode::OP_PARENT_CALL;
  1877. block->statements.push_back(cparent);
  1878. IdentifierNode *id = alloc_node<IdentifierNode>();
  1879. id->name="_init";
  1880. cparent->arguments.push_back(id);
  1881. if (tokenizer->get_token()==GDTokenizer::TK_PERIOD) {
  1882. tokenizer->advance();
  1883. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_OPEN) {
  1884. _set_error("expected '(' for parent constructor arguments.");
  1885. }
  1886. tokenizer->advance();
  1887. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1888. //has arguments
  1889. parenthesis ++;
  1890. while(true) {
  1891. Node *arg = _parse_and_reduce_expression(p_class,_static);
  1892. cparent->arguments.push_back(arg);
  1893. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1894. tokenizer->advance();
  1895. continue;
  1896. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1897. _set_error("Expected ',' or ')'.");
  1898. return;
  1899. }
  1900. break;
  1901. }
  1902. parenthesis --;
  1903. }
  1904. tokenizer->advance();
  1905. }
  1906. } else {
  1907. if (tokenizer->get_token()==GDTokenizer::TK_PERIOD) {
  1908. _set_error("Parent constructor call found for a class without inheritance.");
  1909. return;
  1910. }
  1911. }
  1912. }
  1913. if (!_enter_indent_block(block)) {
  1914. _set_error("Indented block expected.");
  1915. return;
  1916. }
  1917. FunctionNode *function = alloc_node<FunctionNode>();
  1918. function->name=name;
  1919. function->arguments=arguments;
  1920. function->default_values=default_values;
  1921. function->_static=_static;
  1922. function->line=fnline;
  1923. function->rpc_mode=rpc_mode;
  1924. rpc_mode=ScriptInstance::RPC_MODE_DISABLED;
  1925. if (_static)
  1926. p_class->static_functions.push_back(function);
  1927. else
  1928. p_class->functions.push_back(function);
  1929. current_function=function;
  1930. function->body=block;
  1931. current_block=block;
  1932. _parse_block(block,_static);
  1933. current_block=NULL;
  1934. //arguments
  1935. } break;
  1936. case GDTokenizer::TK_PR_SIGNAL: {
  1937. tokenizer->advance();
  1938. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1939. _set_error("Expected identifier after 'signal'.");
  1940. return;
  1941. }
  1942. ClassNode::Signal sig;
  1943. sig.name = tokenizer->get_token_identifier();
  1944. tokenizer->advance();
  1945. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_OPEN) {
  1946. tokenizer->advance();
  1947. while(true) {
  1948. if (tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  1949. tokenizer->advance();
  1950. continue;
  1951. }
  1952. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1953. tokenizer->advance();
  1954. break;
  1955. }
  1956. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  1957. _set_error("Expected identifier in signal argument.");
  1958. return;
  1959. }
  1960. sig.arguments.push_back(tokenizer->get_token_identifier());
  1961. tokenizer->advance();
  1962. while (tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  1963. tokenizer->advance();
  1964. }
  1965. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1966. tokenizer->advance();
  1967. } else if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  1968. _set_error("Expected ',' or ')' after signal parameter identifier.");
  1969. return;
  1970. }
  1971. }
  1972. }
  1973. p_class->_signals.push_back(sig);
  1974. if (!_end_statement()) {
  1975. _set_error("Expected end of statement (signal)");
  1976. return;
  1977. }
  1978. } break;
  1979. case GDTokenizer::TK_PR_EXPORT: {
  1980. tokenizer->advance();
  1981. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_OPEN) {
  1982. tokenizer->advance();
  1983. if (tokenizer->get_token()==GDTokenizer::TK_BUILT_IN_TYPE) {
  1984. Variant::Type type = tokenizer->get_token_type();
  1985. if (type==Variant::NIL) {
  1986. _set_error("Can't export null type.");
  1987. return;
  1988. }
  1989. current_export.type=type;
  1990. current_export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
  1991. tokenizer->advance();
  1992. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  1993. // hint expected next!
  1994. tokenizer->advance();
  1995. switch(current_export.type) {
  1996. case Variant::INT: {
  1997. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="FLAGS") {
  1998. //current_export.hint=PROPERTY_HINT_ALL_FLAGS;
  1999. tokenizer->advance();
  2000. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2001. break;
  2002. }
  2003. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA)
  2004. {
  2005. _set_error("Expected ')' or ',' in bit flags hint.");
  2006. return;
  2007. }
  2008. current_export.hint=PROPERTY_HINT_FLAGS;
  2009. tokenizer->advance();
  2010. bool first = true;
  2011. while(true) {
  2012. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  2013. current_export=PropertyInfo();
  2014. _set_error("Expected a string constant in named bit flags hint.");
  2015. return;
  2016. }
  2017. String c = tokenizer->get_token_constant();
  2018. if (!first)
  2019. current_export.hint_string+=",";
  2020. else
  2021. first=false;
  2022. current_export.hint_string+=c.xml_escape();
  2023. tokenizer->advance();
  2024. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2025. break;
  2026. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  2027. current_export=PropertyInfo();
  2028. _set_error("Expected ')' or ',' in named bit flags hint.");
  2029. return;
  2030. }
  2031. tokenizer->advance();
  2032. }
  2033. break;
  2034. }
  2035. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type()==Variant::STRING) {
  2036. //enumeration
  2037. current_export.hint=PROPERTY_HINT_ENUM;
  2038. bool first=true;
  2039. while(true) {
  2040. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  2041. current_export=PropertyInfo();
  2042. _set_error("Expected a string constant in enumeration hint.");
  2043. return;
  2044. }
  2045. String c = tokenizer->get_token_constant();
  2046. if (!first)
  2047. current_export.hint_string+=",";
  2048. else
  2049. first=false;
  2050. current_export.hint_string+=c.xml_escape();
  2051. tokenizer->advance();
  2052. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2053. break;
  2054. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  2055. current_export=PropertyInfo();
  2056. _set_error("Expected ')' or ',' in enumeration hint.");
  2057. return;
  2058. }
  2059. tokenizer->advance();
  2060. }
  2061. break;
  2062. }
  2063. }; //fallthrough to use the same
  2064. case Variant::REAL: {
  2065. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="EASE") {
  2066. current_export.hint=PROPERTY_HINT_EXP_EASING;
  2067. tokenizer->advance();
  2068. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2069. _set_error("Expected ')' in hint.");
  2070. return;
  2071. }
  2072. break;
  2073. }
  2074. // range
  2075. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="EXP") {
  2076. current_export.hint=PROPERTY_HINT_EXP_RANGE;
  2077. tokenizer->advance();
  2078. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2079. break;
  2080. else if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  2081. _set_error("Expected ')' or ',' in exponential range hint.");
  2082. return;
  2083. }
  2084. tokenizer->advance();
  2085. }
  2086. else
  2087. current_export.hint=PROPERTY_HINT_RANGE;
  2088. float sign=1.0;
  2089. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  2090. sign=-1;
  2091. tokenizer->advance();
  2092. }
  2093. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2094. current_export=PropertyInfo();
  2095. _set_error("Expected a range in numeric hint.");
  2096. return;
  2097. }
  2098. current_export.hint_string=rtos(sign*double(tokenizer->get_token_constant()));
  2099. tokenizer->advance();
  2100. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2101. current_export.hint_string="0,"+current_export.hint_string;
  2102. break;
  2103. }
  2104. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  2105. current_export=PropertyInfo();
  2106. _set_error("Expected ',' or ')' in numeric range hint.");
  2107. return;
  2108. }
  2109. tokenizer->advance();
  2110. sign=1.0;
  2111. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  2112. sign=-1;
  2113. tokenizer->advance();
  2114. }
  2115. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2116. current_export=PropertyInfo();
  2117. _set_error("Expected a number as upper bound in numeric range hint.");
  2118. return;
  2119. }
  2120. current_export.hint_string+=","+rtos(sign*double(tokenizer->get_token_constant()));
  2121. tokenizer->advance();
  2122. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2123. break;
  2124. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  2125. current_export=PropertyInfo();
  2126. _set_error("Expected ',' or ')' in numeric range hint.");
  2127. return;
  2128. }
  2129. tokenizer->advance();
  2130. sign=1.0;
  2131. if (tokenizer->get_token()==GDTokenizer::TK_OP_SUB) {
  2132. sign=-1;
  2133. tokenizer->advance();
  2134. }
  2135. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || !tokenizer->get_token_constant().is_num()) {
  2136. current_export=PropertyInfo();
  2137. _set_error("Expected a number as step in numeric range hint.");
  2138. return;
  2139. }
  2140. current_export.hint_string+=","+rtos(sign*double(tokenizer->get_token_constant()));
  2141. tokenizer->advance();
  2142. } break;
  2143. case Variant::STRING: {
  2144. if (tokenizer->get_token()==GDTokenizer::TK_CONSTANT && tokenizer->get_token_constant().get_type()==Variant::STRING) {
  2145. //enumeration
  2146. current_export.hint=PROPERTY_HINT_ENUM;
  2147. bool first=true;
  2148. while(true) {
  2149. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  2150. current_export=PropertyInfo();
  2151. _set_error("Expected a string constant in enumeration hint.");
  2152. return;
  2153. }
  2154. String c = tokenizer->get_token_constant();
  2155. if (!first)
  2156. current_export.hint_string+=",";
  2157. else
  2158. first=false;
  2159. current_export.hint_string+=c.xml_escape();
  2160. tokenizer->advance();
  2161. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2162. break;
  2163. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  2164. current_export=PropertyInfo();
  2165. _set_error("Expected ')' or ',' in enumeration hint.");
  2166. return;
  2167. }
  2168. tokenizer->advance();
  2169. }
  2170. break;
  2171. }
  2172. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="DIR") {
  2173. tokenizer->advance();
  2174. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2175. current_export.hint=PROPERTY_HINT_DIR;
  2176. else if (tokenizer->get_token()==GDTokenizer::TK_COMMA ) {
  2177. tokenizer->advance();
  2178. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER || !(tokenizer->get_token_identifier()=="GLOBAL")) {
  2179. _set_error("Expected 'GLOBAL' after comma in directory hint.");
  2180. return;
  2181. }
  2182. if (!p_class->tool) {
  2183. _set_error("Global filesystem hints may only be used in tool scripts.");
  2184. return;
  2185. }
  2186. current_export.hint=PROPERTY_HINT_GLOBAL_DIR;
  2187. tokenizer->advance();
  2188. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2189. _set_error("Expected ')' in hint.");
  2190. return;
  2191. }
  2192. }
  2193. else {
  2194. _set_error("Expected ')' or ',' in hint.");
  2195. return;
  2196. }
  2197. break;
  2198. }
  2199. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="FILE") {
  2200. current_export.hint=PROPERTY_HINT_FILE;
  2201. tokenizer->advance();
  2202. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  2203. tokenizer->advance();
  2204. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="GLOBAL") {
  2205. if (!p_class->tool) {
  2206. _set_error("Global filesystem hints may only be used in tool scripts.");
  2207. return;
  2208. }
  2209. current_export.hint=PROPERTY_HINT_GLOBAL_FILE;
  2210. tokenizer->advance();
  2211. if (tokenizer->get_token()==GDTokenizer::TK_PARENTHESIS_CLOSE)
  2212. break;
  2213. else if (tokenizer->get_token()==GDTokenizer::TK_COMMA)
  2214. tokenizer->advance();
  2215. else {
  2216. _set_error("Expected ')' or ',' in hint.");
  2217. return;
  2218. }
  2219. }
  2220. if (tokenizer->get_token()!=GDTokenizer::TK_CONSTANT || tokenizer->get_token_constant().get_type()!=Variant::STRING) {
  2221. if (current_export.hint==PROPERTY_HINT_GLOBAL_FILE)
  2222. _set_error("Expected string constant with filter");
  2223. else
  2224. _set_error("Expected 'GLOBAL' or string constant with filter");
  2225. return;
  2226. }
  2227. current_export.hint_string=tokenizer->get_token_constant();
  2228. tokenizer->advance();
  2229. }
  2230. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2231. _set_error("Expected ')' in hint.");
  2232. return;
  2233. }
  2234. break;
  2235. }
  2236. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER && tokenizer->get_token_identifier()=="MULTILINE") {
  2237. current_export.hint=PROPERTY_HINT_MULTILINE_TEXT;
  2238. tokenizer->advance();
  2239. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2240. _set_error("Expected ')' in hint.");
  2241. return;
  2242. }
  2243. break;
  2244. }
  2245. } break;
  2246. case Variant::COLOR: {
  2247. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER ) {
  2248. current_export=PropertyInfo();
  2249. _set_error("Color type hint expects RGB or RGBA as hints");
  2250. return;
  2251. }
  2252. String identifier = tokenizer->get_token_identifier();
  2253. if (identifier=="RGB") {
  2254. current_export.hint=PROPERTY_HINT_COLOR_NO_ALPHA;
  2255. } else if (identifier=="RGBA") {
  2256. //none
  2257. } else {
  2258. current_export=PropertyInfo();
  2259. _set_error("Color type hint expects RGB or RGBA as hints");
  2260. return;
  2261. }
  2262. tokenizer->advance();
  2263. } break;
  2264. default: {
  2265. current_export=PropertyInfo();
  2266. _set_error("Type '"+Variant::get_type_name(type)+"' can't take hints.");
  2267. return;
  2268. } break;
  2269. }
  2270. }
  2271. } else if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER) {
  2272. String identifier = tokenizer->get_token_identifier();
  2273. if (!ClassDB::is_parent_class(identifier,"Resource")) {
  2274. current_export=PropertyInfo();
  2275. _set_error("Export hint not a type or resource.");
  2276. }
  2277. current_export.type=Variant::OBJECT;
  2278. current_export.hint=PROPERTY_HINT_RESOURCE_TYPE;
  2279. current_export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
  2280. current_export.hint_string=identifier;
  2281. tokenizer->advance();
  2282. }
  2283. if (tokenizer->get_token()!=GDTokenizer::TK_PARENTHESIS_CLOSE) {
  2284. current_export=PropertyInfo();
  2285. _set_error("Expected ')' or ',' after export hint.");
  2286. return;
  2287. }
  2288. tokenizer->advance();
  2289. }
  2290. 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) {
  2291. current_export=PropertyInfo();
  2292. _set_error("Expected 'var', 'onready', 'remote', 'master', 'slave' or 'sync'.");
  2293. return;
  2294. }
  2295. continue;
  2296. } break;
  2297. case GDTokenizer::TK_PR_ONREADY: {
  2298. //may be fallthrough from export, ignore if so
  2299. tokenizer->advance();
  2300. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
  2301. _set_error("Expected 'var'.");
  2302. return;
  2303. }
  2304. continue;
  2305. } break;
  2306. case GDTokenizer::TK_PR_REMOTE: {
  2307. //may be fallthrough from export, ignore if so
  2308. tokenizer->advance();
  2309. if (current_export.type) {
  2310. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
  2311. _set_error("Expected 'var'.");
  2312. return;
  2313. }
  2314. } else {
  2315. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR && tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
  2316. _set_error("Expected 'var' or 'func'.");
  2317. return;
  2318. }
  2319. }
  2320. rpc_mode=ScriptInstance::RPC_MODE_REMOTE;
  2321. continue;
  2322. } break;
  2323. case GDTokenizer::TK_PR_MASTER: {
  2324. //may be fallthrough from export, ignore if so
  2325. tokenizer->advance();
  2326. if (current_export.type) {
  2327. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
  2328. _set_error("Expected 'var'.");
  2329. return;
  2330. }
  2331. } else {
  2332. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR && tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
  2333. _set_error("Expected 'var' or 'func'.");
  2334. return;
  2335. }
  2336. }
  2337. rpc_mode=ScriptInstance::RPC_MODE_MASTER;
  2338. continue;
  2339. } break;
  2340. case GDTokenizer::TK_PR_SLAVE: {
  2341. //may be fallthrough from export, ignore if so
  2342. tokenizer->advance();
  2343. if (current_export.type) {
  2344. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) {
  2345. _set_error("Expected 'var'.");
  2346. return;
  2347. }
  2348. } else {
  2349. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR && tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
  2350. _set_error("Expected 'var' or 'func'.");
  2351. return;
  2352. }
  2353. }
  2354. rpc_mode=ScriptInstance::RPC_MODE_SLAVE;
  2355. continue;
  2356. } break;
  2357. case GDTokenizer::TK_PR_SYNC: {
  2358. //may be fallthrough from export, ignore if so
  2359. tokenizer->advance();
  2360. if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR && tokenizer->get_token()!=GDTokenizer::TK_PR_FUNCTION) {
  2361. if (current_export.type)
  2362. _set_error("Expected 'var'.");
  2363. else
  2364. _set_error("Expected 'var' or 'func'.");
  2365. return;
  2366. }
  2367. rpc_mode=ScriptInstance::RPC_MODE_SYNC;
  2368. continue;
  2369. } break;
  2370. case GDTokenizer::TK_PR_VAR: {
  2371. //variale declaration and (eventual) initialization
  2372. ClassNode::Member member;
  2373. bool autoexport = tokenizer->get_token(-1)==GDTokenizer::TK_PR_EXPORT;
  2374. if (current_export.type!=Variant::NIL) {
  2375. member._export=current_export;
  2376. current_export=PropertyInfo();
  2377. }
  2378. bool onready = tokenizer->get_token(-1)==GDTokenizer::TK_PR_ONREADY;
  2379. tokenizer->advance();
  2380. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2381. _set_error("Expected identifier for member variable name.");
  2382. return;
  2383. }
  2384. member.identifier=tokenizer->get_token_identifier();
  2385. member.expression=NULL;
  2386. member._export.name=member.identifier;
  2387. member.line=tokenizer->get_token_line();
  2388. member.rpc_mode=rpc_mode;
  2389. tokenizer->advance();
  2390. rpc_mode=ScriptInstance::RPC_MODE_DISABLED;
  2391. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  2392. #ifdef DEBUG_ENABLED
  2393. int line = tokenizer->get_token_line();
  2394. #endif
  2395. tokenizer->advance();
  2396. Node *subexpr=NULL;
  2397. subexpr = _parse_and_reduce_expression(p_class,false,autoexport);
  2398. if (!subexpr) {
  2399. if (_recover_from_completion()) {
  2400. break;
  2401. }
  2402. return;
  2403. }
  2404. //discourage common error
  2405. if (!onready && subexpr->type==Node::TYPE_OPERATOR) {
  2406. OperatorNode *op=static_cast<OperatorNode*>(subexpr);
  2407. if (op->op==OperatorNode::OP_CALL && op->arguments[0]->type==Node::TYPE_SELF && op->arguments[1]->type==Node::TYPE_IDENTIFIER) {
  2408. IdentifierNode *id=static_cast<IdentifierNode*>(op->arguments[1]);
  2409. if (id->name=="get_node") {
  2410. _set_error("Use 'onready var "+String(member.identifier)+" = get_node(..)' instead");
  2411. return;
  2412. }
  2413. }
  2414. }
  2415. member.expression=subexpr;
  2416. if (autoexport) {
  2417. if (1)/*(subexpr->type==Node::TYPE_ARRAY) {
  2418. member._export.type=Variant::ARRAY;
  2419. } else if (subexpr->type==Node::TYPE_DICTIONARY) {
  2420. member._export.type=Variant::DICTIONARY;
  2421. } else*/ {
  2422. if (subexpr->type!=Node::TYPE_CONSTANT) {
  2423. _set_error("Type-less export needs a constant expression assigned to infer type.");
  2424. return;
  2425. }
  2426. ConstantNode *cn = static_cast<ConstantNode*>(subexpr);
  2427. if (cn->value.get_type()==Variant::NIL) {
  2428. _set_error("Can't accept a null constant expression for infering export type.");
  2429. return;
  2430. }
  2431. member._export.type=cn->value.get_type();
  2432. member._export.usage|=PROPERTY_USAGE_SCRIPT_VARIABLE;
  2433. if (cn->value.get_type()==Variant::OBJECT) {
  2434. Object *obj = cn->value;
  2435. Resource *res = obj->cast_to<Resource>();
  2436. if(res==NULL) {
  2437. _set_error("Exported constant not a type or resource.");
  2438. return;
  2439. }
  2440. member._export.hint=PROPERTY_HINT_RESOURCE_TYPE;
  2441. member._export.hint_string=res->get_class();
  2442. }
  2443. }
  2444. }
  2445. #ifdef TOOLS_ENABLED
  2446. if (subexpr->type==Node::TYPE_CONSTANT && member._export.type!=Variant::NIL) {
  2447. ConstantNode *cn = static_cast<ConstantNode*>(subexpr);
  2448. if (cn->value.get_type()!=Variant::NIL) {
  2449. member.default_value=cn->value;
  2450. }
  2451. }
  2452. #endif
  2453. IdentifierNode *id = alloc_node<IdentifierNode>();
  2454. id->name=member.identifier;
  2455. OperatorNode *op = alloc_node<OperatorNode>();
  2456. op->op=OperatorNode::OP_INIT_ASSIGN;
  2457. op->arguments.push_back(id);
  2458. op->arguments.push_back(subexpr);
  2459. #ifdef DEBUG_ENABLED
  2460. NewLineNode *nl = alloc_node<NewLineNode>();
  2461. nl->line=line;
  2462. if (onready)
  2463. p_class->ready->statements.push_back(nl);
  2464. else
  2465. p_class->initializer->statements.push_back(nl);
  2466. #endif
  2467. if (onready)
  2468. p_class->ready->statements.push_back(op);
  2469. else
  2470. p_class->initializer->statements.push_back(op);
  2471. } else {
  2472. if (autoexport) {
  2473. _set_error("Type-less export needs a constant expression assigned to infer type.");
  2474. return;
  2475. }
  2476. }
  2477. if (tokenizer->get_token()==GDTokenizer::TK_PR_SETGET) {
  2478. tokenizer->advance();
  2479. if (tokenizer->get_token()!=GDTokenizer::TK_COMMA) {
  2480. //just comma means using only getter
  2481. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2482. _set_error("Expected identifier for setter function after 'notify'.");
  2483. }
  2484. member.setter=tokenizer->get_token_identifier();
  2485. tokenizer->advance();
  2486. }
  2487. if (tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  2488. //there is a getter
  2489. tokenizer->advance();
  2490. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2491. _set_error("Expected identifier for getter function after ','.");
  2492. }
  2493. member.getter=tokenizer->get_token_identifier();
  2494. tokenizer->advance();
  2495. }
  2496. }
  2497. p_class->variables.push_back(member);
  2498. if (!_end_statement()) {
  2499. _set_error("Expected end of statement (continue)");
  2500. return;
  2501. }
  2502. } break;
  2503. case GDTokenizer::TK_PR_CONST: {
  2504. //variale declaration and (eventual) initialization
  2505. ClassNode::Constant constant;
  2506. tokenizer->advance();
  2507. if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2508. _set_error("Expected name (identifier) for constant.");
  2509. return;
  2510. }
  2511. constant.identifier=tokenizer->get_token_identifier();
  2512. tokenizer->advance();
  2513. if (tokenizer->get_token()!=GDTokenizer::TK_OP_ASSIGN) {
  2514. _set_error("Constant expects assignment.");
  2515. return;
  2516. }
  2517. tokenizer->advance();
  2518. Node *subexpr=NULL;
  2519. subexpr = _parse_and_reduce_expression(p_class,true,true);
  2520. if (!subexpr) {
  2521. if (_recover_from_completion()) {
  2522. break;
  2523. }
  2524. return;
  2525. }
  2526. if (subexpr->type!=Node::TYPE_CONSTANT) {
  2527. _set_error("Expected constant expression");
  2528. }
  2529. constant.expression=subexpr;
  2530. p_class->constant_expressions.push_back(constant);
  2531. if (!_end_statement()) {
  2532. _set_error("Expected end of statement (constant)");
  2533. return;
  2534. }
  2535. } break;
  2536. case GDTokenizer::TK_PR_ENUM: {
  2537. //mutiple constant declarations..
  2538. int last_assign = -1; // Incremented by 1 right before the assingment.
  2539. String enum_name;
  2540. Dictionary enum_dict;
  2541. tokenizer->advance();
  2542. if (tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER) {
  2543. enum_name=tokenizer->get_token_identifier();
  2544. tokenizer->advance();
  2545. }
  2546. if (tokenizer->get_token()!=GDTokenizer::TK_CURLY_BRACKET_OPEN) {
  2547. _set_error("Expected '{' in enum declaration");
  2548. return;
  2549. }
  2550. tokenizer->advance();
  2551. while(true) {
  2552. if(tokenizer->get_token()==GDTokenizer::TK_NEWLINE) {
  2553. tokenizer->advance(); // Ignore newlines
  2554. } else if (tokenizer->get_token()==GDTokenizer::TK_CURLY_BRACKET_CLOSE) {
  2555. tokenizer->advance();
  2556. break; // End of enum
  2557. } else if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) {
  2558. if(tokenizer->get_token()==GDTokenizer::TK_EOF) {
  2559. _set_error("Unexpected end of file.");
  2560. } else {
  2561. _set_error(String("Unexpected ") + GDTokenizer::get_token_name(tokenizer->get_token()) + ", expected identifier");
  2562. }
  2563. return;
  2564. } else { // tokenizer->get_token()==GDTokenizer::TK_IDENTIFIER
  2565. ClassNode::Constant constant;
  2566. constant.identifier=tokenizer->get_token_identifier();
  2567. tokenizer->advance();
  2568. if (tokenizer->get_token()==GDTokenizer::TK_OP_ASSIGN) {
  2569. tokenizer->advance();
  2570. Node *subexpr=NULL;
  2571. subexpr = _parse_and_reduce_expression(p_class,true,true);
  2572. if (!subexpr) {
  2573. if (_recover_from_completion()) {
  2574. break;
  2575. }
  2576. return;
  2577. }
  2578. if (subexpr->type!=Node::TYPE_CONSTANT) {
  2579. _set_error("Expected constant expression");
  2580. }
  2581. const ConstantNode *subexpr_const = static_cast<const ConstantNode*>(subexpr);
  2582. if(subexpr_const->value.get_type() != Variant::INT) {
  2583. _set_error("Expected an int value for enum");
  2584. }
  2585. last_assign = subexpr_const->value;
  2586. constant.expression=subexpr;
  2587. } else {
  2588. last_assign = last_assign + 1;
  2589. ConstantNode *cn = alloc_node<ConstantNode>();
  2590. cn->value = last_assign;
  2591. constant.expression = cn;
  2592. }
  2593. if(tokenizer->get_token()==GDTokenizer::TK_COMMA) {
  2594. tokenizer->advance();
  2595. }
  2596. if(enum_name != "") {
  2597. const ConstantNode *cn = static_cast<const ConstantNode*>(constant.expression);
  2598. enum_dict[constant.identifier] = cn->value;
  2599. }
  2600. p_class->constant_expressions.push_back(constant);
  2601. }
  2602. }
  2603. if(enum_name != "") {
  2604. ClassNode::Constant enum_constant;
  2605. enum_constant.identifier=enum_name;
  2606. ConstantNode *cn = alloc_node<ConstantNode>();
  2607. cn->value = enum_dict;
  2608. enum_constant.expression=cn;
  2609. p_class->constant_expressions.push_back(enum_constant);
  2610. }
  2611. if (!_end_statement()) {
  2612. _set_error("Expected end of statement (enum)");
  2613. return;
  2614. }
  2615. } break;
  2616. case GDTokenizer::TK_CONSTANT: {
  2617. if(tokenizer->get_token_constant().get_type() == Variant::STRING) {
  2618. tokenizer->advance();
  2619. // Ignore
  2620. } else {
  2621. _set_error(String()+"Unexpected constant of type: "+Variant::get_type_name(tokenizer->get_token_constant().get_type()));
  2622. return;
  2623. }
  2624. } break;
  2625. default: {
  2626. _set_error(String()+"Unexpected token: "+tokenizer->get_token_name(tokenizer->get_token())+":"+tokenizer->get_token_identifier());
  2627. return;
  2628. } break;
  2629. }
  2630. }
  2631. }
  2632. void GDParser::_set_error(const String& p_error, int p_line, int p_column) {
  2633. if (error_set)
  2634. return; //allow no further errors
  2635. error=p_error;
  2636. error_line=p_line<0?tokenizer->get_token_line():p_line;
  2637. error_column=p_column<0?tokenizer->get_token_column():p_column;
  2638. error_set=true;
  2639. }
  2640. String GDParser::get_error() const {
  2641. return error;
  2642. }
  2643. int GDParser::get_error_line() const {
  2644. return error_line;
  2645. }
  2646. int GDParser::get_error_column() const {
  2647. return error_column;
  2648. }
  2649. Error GDParser::_parse(const String& p_base_path) {
  2650. base_path=p_base_path;
  2651. clear();
  2652. //assume class
  2653. ClassNode *main_class = alloc_node<ClassNode>();
  2654. main_class->initializer = alloc_node<BlockNode>();
  2655. main_class->initializer->parent_class=main_class;
  2656. main_class->ready = alloc_node<BlockNode>();
  2657. main_class->ready->parent_class=main_class;
  2658. current_class=main_class;
  2659. _parse_class(main_class);
  2660. if (tokenizer->get_token()==GDTokenizer::TK_ERROR) {
  2661. error_set=false;
  2662. _set_error("Parse Error: "+tokenizer->get_token_error());
  2663. }
  2664. if (error_set) {
  2665. return ERR_PARSE_ERROR;
  2666. }
  2667. return OK;
  2668. }
  2669. Error GDParser::parse_bytecode(const Vector<uint8_t> &p_bytecode,const String& p_base_path, const String &p_self_path) {
  2670. for_completion=false;
  2671. validating=false;
  2672. completion_type=COMPLETION_NONE;
  2673. completion_node=NULL;
  2674. completion_class=NULL;
  2675. completion_function=NULL;
  2676. completion_block=NULL;
  2677. completion_found=false;
  2678. current_block=NULL;
  2679. current_class=NULL;
  2680. current_function=NULL;
  2681. self_path=p_self_path;
  2682. GDTokenizerBuffer *tb = memnew( GDTokenizerBuffer );
  2683. tb->set_code_buffer(p_bytecode);
  2684. tokenizer=tb;
  2685. Error ret = _parse(p_base_path);
  2686. memdelete(tb);
  2687. tokenizer=NULL;
  2688. return ret;
  2689. }
  2690. 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) {
  2691. completion_type=COMPLETION_NONE;
  2692. completion_node=NULL;
  2693. completion_class=NULL;
  2694. completion_function=NULL;
  2695. completion_block=NULL;
  2696. completion_found=false;
  2697. current_block=NULL;
  2698. current_class=NULL;
  2699. current_function=NULL;
  2700. self_path=p_self_path;
  2701. GDTokenizerText *tt = memnew( GDTokenizerText );
  2702. tt->set_code(p_code);
  2703. validating=p_just_validate;
  2704. for_completion=p_for_completion;
  2705. tokenizer=tt;
  2706. Error ret = _parse(p_base_path);
  2707. memdelete(tt);
  2708. tokenizer=NULL;
  2709. return ret;
  2710. }
  2711. bool GDParser::is_tool_script() const {
  2712. return (head && head->type==Node::TYPE_CLASS && static_cast<const ClassNode*>(head)->tool);
  2713. }
  2714. const GDParser::Node *GDParser::get_parse_tree() const {
  2715. return head;
  2716. }
  2717. void GDParser::clear() {
  2718. while(list) {
  2719. Node *l=list;
  2720. list=list->next;
  2721. memdelete(l);
  2722. }
  2723. head=NULL;
  2724. list=NULL;
  2725. completion_type=COMPLETION_NONE;
  2726. completion_node=NULL;
  2727. completion_class=NULL;
  2728. completion_function=NULL;
  2729. completion_block=NULL;
  2730. current_block=NULL;
  2731. current_class=NULL;
  2732. completion_found=false;
  2733. rpc_mode=ScriptInstance::RPC_MODE_DISABLED;
  2734. current_function=NULL;
  2735. validating=false;
  2736. for_completion=false;
  2737. error_set=false;
  2738. tab_level.clear();
  2739. tab_level.push_back(0);
  2740. error_line=0;
  2741. error_column=0;
  2742. pending_newline=-1;
  2743. parenthesis=0;
  2744. current_export.type=Variant::NIL;
  2745. error="";
  2746. }
  2747. GDParser::CompletionType GDParser::get_completion_type() {
  2748. return completion_type;
  2749. }
  2750. StringName GDParser::get_completion_cursor() {
  2751. return completion_cursor;
  2752. }
  2753. int GDParser::get_completion_line() {
  2754. return completion_line;
  2755. }
  2756. Variant::Type GDParser::get_completion_built_in_constant(){
  2757. return completion_built_in_constant;
  2758. }
  2759. GDParser::Node *GDParser::get_completion_node(){
  2760. return completion_node;
  2761. }
  2762. GDParser::BlockNode *GDParser::get_completion_block() {
  2763. return completion_block;
  2764. }
  2765. GDParser::ClassNode *GDParser::get_completion_class(){
  2766. return completion_class;
  2767. }
  2768. GDParser::FunctionNode *GDParser::get_completion_function(){
  2769. return completion_function;
  2770. }
  2771. int GDParser::get_completion_argument_index() {
  2772. return completion_argument;
  2773. }
  2774. int GDParser::get_completion_identifier_is_function() {
  2775. return completion_ident_is_call;
  2776. }
  2777. GDParser::GDParser() {
  2778. head=NULL;
  2779. list=NULL;
  2780. tokenizer=NULL;
  2781. pending_newline=-1;
  2782. clear();
  2783. }
  2784. GDParser::~GDParser() {
  2785. clear();
  2786. }