2
0

gd_parser.cpp 99 KB

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